diff --git a/assets/player-stand.png b/assets/player.png similarity index 100% rename from assets/player-stand.png rename to assets/player.png diff --git a/src/actor.c b/src/actor.c index 8db4145..7e3c74f 100644 --- a/src/actor.c +++ b/src/actor.c @@ -2,6 +2,7 @@ #include +#include "game.h" #include "player.h" #include "sdk/oam.h" #include "util.h" @@ -16,8 +17,7 @@ static actor_anim_state_t ANIM_TOTAL_FRAMES[] = {2}; static uint8_t mob_ids[MAX_UNIQUE_MOBS]; -static sprite16_mob mob_anim_data[MAX_UNIQUE_MOBS]; -static sprite16_player player_anim_data; +static uint8_t *mob_anim_data[MAX_UNIQUE_MOBS]; static actor_t all_actors[MAX_ACTORS]; static uint8_t actor_load_mob_anim_data(uint8_t mob_id); @@ -28,6 +28,9 @@ void actor_init(void) { actor_reset(); PLAYER_ACTOR->active = 1; + PLAYER_ACTOR->anim = ANIM_STAND; + PLAYER_ACTOR->frame_idx = 0; + PLAYER_ACTOR->frame_counter = 0; PLAYER_ACTOR->x = PLAYER.x; PLAYER_ACTOR->y = PLAYER.y; } @@ -70,10 +73,12 @@ actor_t *actor_create_mob(uint8_t id) { void actor_remove(actor_t *a) { a->active = 0; } void actor_update(void) { + struct oam_entry *oam_ptr = &shadow_oam[0]; actor_t *a = &all_actors[0]; for (uint8_t i = 0; i < ARRSIZE(all_actors); ++i) { if (!a->active) { + a++; continue; } @@ -83,44 +88,35 @@ void actor_update(void) { a->frame_idx = (a->frame_idx + 1) % ANIM_TOTAL_FRAMES[a->anim]; } - a++; - } -} - -void actor_flush_oam(void) { - struct oam_entry *oam_ptr = &shadow_oam[0]; - actor_t *a = &all_actors[0]; - - for (uint8_t i = 0; i < ARRSIZE(all_actors); ++i) { - if (!a->active) { - a++; - continue; + const uint8_t *anim_ptr; + if (i == 0) { + anim_ptr = &player_map[0]; + } else { + anim_ptr = mob_anim_data[a->mob_anim_idx]; } - - uint8_t *anim_ptr = mob_anim_data[a->mob_anim_idx].anims[a->anim]; anim_ptr += a->frame_idx * 4; - oam_ptr->y = a->y; - oam_ptr->x = a->x; - oam_ptr->tile = *anim_ptr++; + oam_ptr->y = a->y + 16; + oam_ptr->x = a->x + 8; + oam_ptr->tile = *anim_ptr++ + TILE_INDEX_PLAYER; oam_ptr->attr = 0; oam_ptr++; - oam_ptr->y = a->y; - oam_ptr->x = a->x + TILE_WIDTH; - oam_ptr->tile = *anim_ptr++; + oam_ptr->y = a->y + 16; + oam_ptr->x = a->x + TILE_WIDTH + 8; + oam_ptr->tile = *anim_ptr++ + TILE_INDEX_PLAYER; oam_ptr->attr = 0; oam_ptr++; - oam_ptr->y = a->y + TILE_WIDTH; - oam_ptr->x = a->x; - oam_ptr->tile = *anim_ptr++; + oam_ptr->y = a->y + TILE_WIDTH + 16; + oam_ptr->x = a->x + 8; + oam_ptr->tile = *anim_ptr++ + TILE_INDEX_PLAYER; oam_ptr->attr = 0; oam_ptr++; - oam_ptr->y = a->y + TILE_WIDTH; - oam_ptr->x = a->x + TILE_WIDTH; - oam_ptr->tile = *anim_ptr++; + oam_ptr->y = a->y + TILE_WIDTH + 16; + oam_ptr->x = a->x + TILE_WIDTH + 8; + oam_ptr->tile = *anim_ptr++ + TILE_INDEX_PLAYER; oam_ptr->attr = 0; oam_ptr++; diff --git a/src/actor.h b/src/actor.h index 4d89446..905981d 100644 --- a/src/actor.h +++ b/src/actor.h @@ -30,17 +30,15 @@ typedef struct { } actor_t; typedef struct { - uint8_t *tiles; - uint16_t num_tiles; uint8_t *anims[ANIM_HURT + 1]; } sprite16_mob; typedef struct { - uint8_t *tiles; - uint16_t num_tiles; uint8_t *anims[ANIM_CLIMB + 1]; } sprite16_player; +void actor_init(void); + /** * @brief Clear all actor data. */ @@ -64,11 +62,7 @@ void actor_remove(actor_t *a); */ void actor_update(void); -/** - * @brief Flush actor sprite data to OAM. - */ -void actor_flush_oam(void); - extern actor_t *PLAYER_ACTOR; +extern sprite16_player player_anim_data; #endif diff --git a/src/level.c b/src/level.c index 9786cb1..eb867d1 100644 --- a/src/level.c +++ b/src/level.c @@ -1,25 +1,32 @@ +#include "actor.h" #include "game.h" #include "intro.h" #include "map.h" #include "player.h" #include "sdk/hardware.h" #include "sdk/joypad.h" +#include "sdk/oam.h" #include "sdk/video.h" #include "vram.h" void level(void) { lcd_disable(); + map_load(&map_intro); + player_init(); + actor_init(); + lcd_enable(); while (1) { joypad_update(); player_update(); - actor_flush_oam(); + actor_update(); HALT(); rIF = 0; vram_update(); + oam_dma_copy(); } } diff --git a/src/main.c b/src/main.c index 335524e..29c789f 100644 --- a/src/main.c +++ b/src/main.c @@ -2,6 +2,7 @@ #include "actor.h" #include "game.h" +#include "player.h" #include "sdk/hardware.h" #include "sdk/oam.h" #include "vram.h" @@ -11,7 +12,6 @@ game_state_t game_state; void main() { game_state = GAME_STATE_TITLE; - // Setup the OAM for sprite drawing oam_init(); vram_init(); diff --git a/src/map.c b/src/map.c index 46e2a9d..00bc23a 100644 --- a/src/map.c +++ b/src/map.c @@ -32,7 +32,8 @@ void map_load(map_t *map) { rSCY = INIT_SCY; PLAYER.x = (MAP.spawn_x - MAP.camera_x) * 8; - PLAYER.y = (MAP.spawn_y - MAP.camera_y) * 8; + uint8_t y = (MAP.spawn_y - MAP.camera_y) * 8; + PLAYER.y = y << 8; memcpy(VRAM_TILE_PTR(TILE_INDEX_BACKGROUND), (uint8_t *)MAP.tile_ptr, MAP.tile_size); diff --git a/src/player.c b/src/player.c index 4769eb2..aec08c5 100644 --- a/src/player.c +++ b/src/player.c @@ -1,7 +1,10 @@ #include "player.h" +#include + #include "actor.h" #include "game.h" +#include "sdk/assets.h" #include "sdk/joypad.h" #define PLAYER_SPEED 2 @@ -12,6 +15,14 @@ player_t PLAYER; +ASSET(player_tiles, "player.2bpp"); +ASSET(player_map, "player.map"); + +void player_init(void) { + memcpy(VRAM_TILE_PTR(TILE_INDEX_PLAYER), &player_tiles[0], + player_tiles_end - player_tiles); +} + void player_update(void) { if ((joypad_pressed & PAD_UP) && PLAYER.state != PLAYER_STATE_JUMP) { PLAYER.state = PLAYER_STATE_JUMP; @@ -51,5 +62,5 @@ void player_update(void) { } PLAYER_ACTOR->x = PLAYER.x; - PLAYER_ACTOR->y = PLAYER.y; + PLAYER_ACTOR->y = PLAYER.y >> 8; } diff --git a/src/player.h b/src/player.h index 1a96e94..9aef0ab 100644 --- a/src/player.h +++ b/src/player.h @@ -21,6 +21,9 @@ typedef struct { } player_t; extern player_t PLAYER; +extern const uint8_t player_map[]; + +void player_init(void); void player_update(void);