move screen drawing logic outside of main.c

This commit is contained in:
Akbar Rahman 2022-06-12 19:31:27 +01:00
parent 0615ab5073
commit 3eb6cc58ef
Signed by: alvierahman90
GPG Key ID: 20609519444A1269
3 changed files with 37 additions and 4 deletions

View File

@ -40,11 +40,11 @@ void main(void)
screen_sprites[1].bitmap_index = SI_UP;
for (i = 0; i < NO_SCREEN_SPRITES; i++) {
set_sprite_tile(i, screen_sprites[i].bitmap_indexes[screen_sprites[i].bitmap_index]);
set_sprite_prop(i, screen_sprites[i].bitmap_prop[screen_sprites[i].bitmap_index]);
screen_sprites[i].bitmap_index_prev = -1;
screen_sprites[i].gb_sprite_index = i;
screen_sprites[i].pos.x = 72+(8*i);
screen_sprites[i].pos.y = 36+(8*i);
move_sprite(i, screen_sprites[i].pos.x, screen_sprites[i].pos.y);
sprite_draw_to_screen(&(screen_sprites[i]));
}
@ -54,7 +54,6 @@ void main(void)
jp = joypad();
for (i = 0; i < NO_SCREEN_SPRITES; i++) {
sprite_iter_frame(&(screen_sprites[i]), &map, screen_sprites[i].has_joypad ? jp : 0, &fc);
move_sprite(i, screen_sprites[i].pos.x, screen_sprites[i].pos.y);
//gprintf("x: %d y: %d\n", sprites[i].pos.x, sprites[i].pos.y);
}

View File

@ -15,8 +15,10 @@ typedef struct SpriteCorners {
typedef struct Sprites {
int gb_sprite_index;
int frames_since_last_dash;
int frames_since_last_jump;
int bitmap_index_prev;
int bitmap_index;
int bitmap_indexes[8];
int bitmap_prop[8];
@ -35,6 +37,8 @@ void get_sprite_corners(Sprite *sprite, SpriteCorners *r);
void sprite_iter_frame(Sprite *sprite, Map *map, int joypad, unsigned int *fc);
unsigned int sprite_internal_collision(Map *map, Sprite *sprite);
unsigned int sprite_collision(Map *map, Sprite *sprite);
void sprite_draw_to_screen(Sprite *sprite);
void sprite_update_bitmap(Sprite *sprite);
int get_tile_index_by_coord(unsigned int x, unsigned int y)
@ -147,6 +151,9 @@ void sprite_iter_frame(Sprite *sprite, Map *map, int joypad, unsigned int *fc)
if (!sprite_internal_collision(map, sprite)) break;
}
sprite_update_bitmap(sprite);
sprite_draw_to_screen(sprite);
}
@ -185,3 +192,27 @@ unsigned int sprite_collision(Map *map, Sprite *sprite)
return rv;
}
void sprite_update_bitmap(Sprite *sprite)
{
sprite->bitmap_index_prev = sprite->bitmap_index;
if ((sprite->vel.y > 0) && (sprite->vel.x > 0)) sprite->bitmap_index = SI_DOWN_RIGHT;
else if ((sprite->vel.y < 0) && (sprite->vel.x > 0)) sprite->bitmap_index = SI_UP_RIGHT;
else if ((sprite->vel.y > 0) && (sprite->vel.x < 0)) sprite->bitmap_index = SI_DOWN_LEFT;
else if ((sprite->vel.y < 0) && (sprite->vel.x < 0)) sprite->bitmap_index = SI_UP_LEFT;
else if ( sprite->vel.y < 0) sprite->bitmap_index = SI_UP;
else if ( sprite->vel.y > 0) sprite->bitmap_index = SI_DOWN;
else if ( sprite->vel.x > 0) sprite->bitmap_index = SI_RIGHT;
else if ( sprite->vel.x < 0) sprite->bitmap_index = SI_LEFT;
}
void sprite_draw_to_screen(Sprite *sprite)
{
if (sprite->bitmap_index_prev != sprite->bitmap_index) {
set_sprite_tile(sprite->gb_sprite_index, sprite->bitmap_indexes[sprite->bitmap_index]);
set_sprite_prop(sprite->gb_sprite_index, sprite->bitmap_prop[sprite->bitmap_index]);
}
move_sprite(sprite->gb_sprite_index, sprite->pos.x, sprite->pos.y);
}

View File

@ -12,8 +12,10 @@ typedef struct SpriteCorners {
} SpriteCorners;
typedef struct Sprites {
int gb_sprite_index;
int frames_since_last_dash;
int frames_since_last_jump;
int bitmap_index_prev;
int bitmap_index;
int bitmap_indexes[8];
int bitmap_prop[8];
@ -29,5 +31,6 @@ typedef struct Sprites {
void sprite_iter_frame(Sprite *sprite, Map *map, int joypad, unsigned int *fc);
unsigned int sprite_internal_collision(Map *map, Sprite *sprite);
unsigned int sprite_collision(Map *map, Sprite *sprite);
void sprite_draw_to_screen(Sprite *sprite);
#endif