Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
timleader committed Jul 6, 2024
1 parent 2786757 commit 18e0f03
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 40 deletions.
45 changes: 44 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,49 @@

## todo

- Settings
- Backlight support enabled
- Volume SFX, Music

- Save Game
- Center the 2 slots
- save text should represent what day your on, eg. day #1
- back and action buttons at buttom of screen

- Manually created z layers

- GBA Z Depth Code (ASM) -- 275,468
- employ the CPU mode switching
- load a of z span

- Collect images of all scenes and characters

- pre-computed visiblity culling
- encode the direction of a triangle as uint8_t
- sort triangles based on direction
- culling based on X and Z not Y

- use triangles instead of quads,
- simplify character models
- LUT table at a given address, so we can simplfy the lookup

- Enemy AI
- Behaviour

- Death

- Credits

- UI for picking up an item

- Animated Environment

- Content Completion ***
- Environments
- Story
- Enemy
- NPCs

- State_Model
- better camera controls
- animation playback support
Expand All @@ -14,7 +57,7 @@

- switching to IRQ enabled gives up access to and alternative r8-r14 (I think r7 might be required for fiq_on / off)

```
``` 8, 9, 10, 11, 12,
.macro fiq_on
msr cpsr, #0x11 // switch r8-r14 to FIQ (IRQ enabled)
.endm
Expand Down
14 changes: 11 additions & 3 deletions source/common/graphics/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "image.h"

//-----------------------------------------------------------------------------
graphics_context_t g_graphics_context; //IWRAM_DATA
graphics_context_t g_graphics_context; //IWRAM_DATA -- put this at a fixed address or patch it in

// this could all be part of the graphics context
image_ptr boundimage = NULL; // think about bandwidth for texture sampling // make this an array
Expand Down Expand Up @@ -212,6 +212,10 @@ edge_t _graphics_left_edge, _graphics_right_edge;
//-----------------------------------------------------------------------------
IWRAM_CODE void create_edge_and_calculate_deltas(edge_t* edge, vertex_t* v1, vertex_t* v2)
{
// this seems un-neccessarily heavy

// lots of un-neccessary de-referencing

vertex_t* tv1 = v1;
vertex_t* tv2 = v2;

Expand Down Expand Up @@ -558,7 +562,7 @@ IWRAM_CODE void graphics_draw_model(model_t* model, uint16_t animation_id, uint1

//debug_printf(DEBUG_LOG_DEBUG, "graphics::draw_model - processed_vertices=0x%x", processed_vertices);

memory_copy(matrix_working_copy, m, sizeof(matrix4x4_t));
memory_copy(matrix_working_copy, m, sizeof(matrix4x4_t)); // should be able to do a faster copy
fixed16_t* p = (fixed16_t*)matrix_working_copy;
for (i = 0; i < 16; ++i)
p[i] >>= 6;
Expand Down Expand Up @@ -696,6 +700,8 @@ IWRAM_CODE void graphics_draw_model(model_t* model, uint16_t animation_id, uint1

// sort and cull polygons move to it's own func

// reduce to one allocation, or better yet, none, just have scratch memory

polygon_t* polygons_to_draw = memory_allocate(sizeof(polygon_t) * 192, MEMORY_EWRAM); // linked list might make alot of sense here .
polygon_t* polygon_ptr = &polygons_to_draw[0];

Expand Down Expand Up @@ -746,6 +752,8 @@ IWRAM_CODE void graphics_draw_model(model_t* model, uint16_t animation_id, uint1
fixed16_t d = fixed16_mul((polygon_vertices[1].position.x - polygon_vertices[0].position.x), (polygon_vertices[2].position.y - polygon_vertices[0].position.y));
d -= fixed16_mul((polygon_vertices[1].position.y - polygon_vertices[0].position.y), (polygon_vertices[2].position.x - polygon_vertices[0].position.x));

// triangles might sort better

if (d > fixed16_zero)//(fixed16_one << 2)) //should be zero, but increasing this will only remove polygons that are at a sharp angle and probably don't take up many pixlelse
{
sort_index_t index = { depth_value, polygons_to_draw_idx };
Expand Down Expand Up @@ -794,7 +802,7 @@ IWRAM_CODE void graphics_draw_model(model_t* model, uint16_t animation_id, uint1

polygon_ptr = &polygons_to_draw[(index_ptr++)->idx];

draw_polygon(polygon_ptr->v, polygon_ptr->vertex_count);
draw_polygon(polygon_ptr->v, polygon_ptr->vertex_count); // jumping in and out of this function isn't ideal
}

profiler_end(phandle);
Expand Down
19 changes: 9 additions & 10 deletions source/common/graphics/graphics.draw_spans.gba.s
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ draw_spans:
ldr r0, =_graphics_right_edge
ldmia r0, { r2-r6 }

mov r8, r2
mov r8, r2 @ these movs seem completely unneeded
mov r9, r4
mov r1, r6

Expand All @@ -98,28 +98,27 @@ draw_spans:
@ int32_t left_edge_floor_x_int = fixed16_to_int(_graphics_left_edge.x);
asr lr, r8, #16 @ int32_t right_edge_floor_x_int = fixed16_to_int(_graphics_right_edge.x);
subs lr, lr, r2, asr #16 @ int32_t width = right_edge_floor_x_int - left_edge_floor_x_int;
ble .draw_spans.scanline_end
@ if (width < 0 && dx is < 0) get out of the whole func
ble .draw_spans.scanline_end @ if (width < 0 && dx is < 0) get out of the whole func


sub r3, r1, r6

ldr r0, =reciprocal_lut @ fixed16_t[] reciprocal_lut
ldr r0, =reciprocal_lut @ fixed16_t[] reciprocal_lut
lsl r1, lr, #2
ldr r0, [r0, r1] @ fixed16_t overWidth = reciprocal_lut[width];
ldr r0, [r0, r1] @ fixed16_t overWidth = reciprocal_lut[width];

asr r3, r3, #8
mul r1, r0, r3 @fixed16_t dv = fixed16_mul_approx2(right_edge_v - left_edge_v, overWidth);
mul r1, r0, r3 @fixed16_t dv = fixed16_mul_approx2(right_edge_v - left_edge_v, overWidth);


sub r3, r9, r4
asr r3, r3, #8
mul r0, r3, r0 @fixed16_t du = fixed16_mul_approx2(right_edge_u - left_edge_u, overWidth);
mul r0, r3, r0 @fixed16_t du = fixed16_mul_approx2(right_edge_u - left_edge_u, overWidth);


asr r3, r2, #16
asr r5, r3, #1
add r11, r11, r5, lsl #1 @uint16_t* output_span_ptr = output_scanline_ptr + (left_edge_floor_x_int >> 1);
add r11, r11, r5, lsl #1 @uint16_t* output_span_ptr = output_scanline_ptr + (left_edge_floor_x_int >> 1);


.draw_spans.uv_mask_assignment:
Expand All @@ -143,8 +142,8 @@ draw_spans:
@ r5 = u_bitmask
@ r6 = v
@ r7 = v_bitmask
@ r8 = * - not used
@ r9 = * - not used
@ r8 = * - scratchd
@ r9 = * - scratch
@ r10 = iheight
@ r11 = output_span_ptr
@ r12 = texture_data_ptr
Expand Down
11 changes: 11 additions & 0 deletions source/common/graphics/graphics.draw_spans.win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ void draw_spans(void)
//-----------------------------------------------------------------------------
void draw_spans_depth(void)
{
/*
This is the wrong approach, we don't need per pixel checks in this manner
array of spans instead of ???
could it be a 1-bit depth map and we just bind the appropriate one
this would mean, only need to read depth value every 32 pixel stride
consider making depth test branchless (use a conditional mov or something)
*/

fixed16_t fheight = fixed16_min(_graphics_left_edge.height, _graphics_right_edge.height); // one of the edges might continue to be used for the next draw_spans, so heights might be different

uint16_t* output_scanline_ptr = g_graphics_context.frame_buffer + (fixed16_to_int(_graphics_left_edge.y) * g_graphics_context.width); // output_scanline_ptr is 16-bit wide as VRAM requires writes of 16-bit wide
Expand Down
1 change: 1 addition & 0 deletions source/common/states/debug/state_model.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "common/math/trigonometry.h"
#include "common/math/point.h"
#include "common/graphics/graphics.h"
#include "common/graphics/overlay.h"
#include "common/graphics/image.h"
#include "common/graphics/camera.h"
#include "common/input/input.h"
Expand Down
4 changes: 2 additions & 2 deletions source/common/utils/bitstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ typedef struct bitstream_s
const uint32_t* buffer;
uint32_t size;

uint32_t buffer_idx;
uint32_t buffer_idx; // byte_idx

uint8_t bit_idx;
uint8_t bit_idx; // might as well be an usize
uint8_t reserved[3];

uint32_t value; // cache value
Expand Down
10 changes: 9 additions & 1 deletion source/common/video/smacker.c
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,14 @@ IWRAM_CODE static char smk_render_video(struct smk_video_t* s, const uint16_t* f
smk_huff16_v5_reset(s->tree[2]);
smk_huff16_v5_reset(s->tree[3]);

// possibly, don't use huff16, pre-decompress,

// tree to IWRAM ??

// lz77 compress chunks,
// decompress these to EWRAM
// playback video

while (row < s->h)
{
unpack = smk_huff16_v5_lookup(&bs, s->tree[SMK_TREE_TYPE]); // decompress this ahead of time???
Expand Down Expand Up @@ -587,7 +595,7 @@ IWRAM_CODE static char smk_render_video(struct smk_video_t* s, const uint16_t* f
{
skip = (row * pitch) + half_col;

switch (type)
switch (type) // lots of branching assembly code ...
{
case 0:
unpack = smk_huff16_v5_lookup(&bs, s->tree[SMK_TREE_MCLR]);
Expand Down
6 changes: 4 additions & 2 deletions source/games/7days/entities/animation.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "games/7days/entities/entity.h"

#include "common/debug/debug.h"
#include "common/graphics/graphics.h"
#include "common/resources/resources.h"

// movement has already been applied, this is just a presentation layer

Expand Down Expand Up @@ -73,10 +75,10 @@ void animation_update(entity_ptr entity) // apply this to player and npc

animation_t* animation = model_find_animation(model, entity->animation_id);

debug_assert(animation, "animation could not be found");
debug_assert(animation != NULL, "animation could not be found");

entity->frame_precise = fixed16_zero;
entity->frame_count = animation->frame_count;
entity->frame_count = (int16_t)animation->frame_count;
}
else
{
Expand Down
1 change: 1 addition & 0 deletions source/games/7days/states/state_savegame.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ void st_savegame_generate_palette_remap(st_savegame_context_ptr context)

palette_remap[idx] = sum + 1;
}
palette_delete(graphics_pal);
}

//-----------------------------------------------------------------------------
Expand Down
47 changes: 26 additions & 21 deletions source/games/7days/world.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "games/7days/sequence.h"
#include "games/7days/states/states.h"

//#define WORLD_DEBUG_DRAW

world_t g_world; // put this on the heap

world_ptr g_main_world = &g_world;
Expand Down Expand Up @@ -63,7 +65,7 @@ void world_initialize(world_ptr world)
}
}

world->ephermeral.highlight_timer = timer_start(20, TIMER_MODE_LOOP);
world->ephermeral.highlight_timer = timer_start(40, TIMER_MODE_LOOP);
world->ephermeral.active_highlight = NULL;
}

Expand Down Expand Up @@ -203,9 +205,11 @@ vector2_t world_closestpointonpath(vector2_t point, uint32_t mask)
vector2_t currentClosestPoint = { 0, 0 }, tmpDist = { 0, 0 };
fixed16_t currentClosestDist = fixed16_maximum;

// this is abit brute force, at least limit based on view or something...

for (uint8_t i = 0; i < g_main_world->ephermeral.numCollision; ++i)
{
// use collision mask
// use collision mask to optimize

vector2_t closestPoint = collisionClosestPointOBB(point, g_main_world->ephermeral.collision[i].box);
if (collisionCheckPointInsideOBB(point, g_main_world->ephermeral.collision[i].box))
Expand Down Expand Up @@ -613,6 +617,8 @@ void world_draw(world_ptr world)
I don't like this code for entity draw order sorting, it feels messy
*/

// can use the existing sorting code

for (idx = 1; idx < draw_list_count; ++idx)
{
if (entity_distance_list[idx - 1] < entity_distance_list[idx])
Expand Down Expand Up @@ -659,24 +665,7 @@ void world_draw(world_ptr world)

profiler_end(phandle);


phandle = profiler_begin("w:d_entites");

// model_entity_draw
for (idx = 0; idx < draw_list_count; ++idx)
{
entity_draw(
entity_draw_list[idx],
&world->ephermeral.views[world->persistent.view_idx].wvp);
}

if (g_graphics_context.frame_dirty_scanline_span.start < g_graphics_context.frame_dirty_scanline_span.end)
world->ephermeral.dirty_scanline_span[g_graphics_context.page_flip] = g_graphics_context.frame_dirty_scanline_span;

profiler_end(phandle);


#if WORLD_DEBUG_DRAW
#ifdef WORLD_DEBUG_DRAW

fixed16_t ground_y = entity_draw_list[0]->position.y;

Expand All @@ -698,14 +687,30 @@ void world_draw(world_ptr world)
if ((trigger->layer & current_level->layer) > 0)
{
graphicsDrawWireframeOBB(
&trigger->area,
&trigger->area,
ground_y,
&world->ephermeral.views[world->persistent.view_idx].wvp);
}
}

#endif

phandle = profiler_begin("w:d_entites");

// model_entity_draw
for (idx = 0; idx < draw_list_count; ++idx)
{
entity_draw(
entity_draw_list[idx],
&world->ephermeral.views[world->persistent.view_idx].wvp);
}

if (g_graphics_context.frame_dirty_scanline_span.start < g_graphics_context.frame_dirty_scanline_span.end)
world->ephermeral.dirty_scanline_span[g_graphics_context.page_flip] = g_graphics_context.frame_dirty_scanline_span;

profiler_end(phandle);


}

//-----------------------------------------------------------------------------
Expand Down

0 comments on commit 18e0f03

Please sign in to comment.