optimisation?

This commit is contained in:
Akbar Rahman 2022-06-19 16:42:21 +01:00
parent 9239a34256
commit 565f7eebbd
Signed by: alvierahman90
GPG Key ID: 20609519444A1269
2 changed files with 47 additions and 47 deletions

View File

@ -12,7 +12,7 @@
#include "./sprite.h" #include "./sprite.h"
#include "./vec.h" #include "./vec.h"
#define NO_SCREEN_SPRITES 2 #define NO_SCREEN_SPRITES 6
void init_gfx() void init_gfx()
{ {
@ -36,8 +36,11 @@ void main(void)
memcpy(&(screen_sprites[0]), &(sprites_info[2]), sizeof(Sprite)); memcpy(&(screen_sprites[0]), &(sprites_info[2]), sizeof(Sprite));
memcpy(&(screen_sprites[1]), &(sprites_info[0]), sizeof(Sprite)); memcpy(&(screen_sprites[1]), &(sprites_info[0]), sizeof(Sprite));
screen_sprites[0].has_joypad = 1; memcpy(&(screen_sprites[2]), &(sprites_info[0]), sizeof(Sprite));
screen_sprites[1].bitmap_index = SI_UP; memcpy(&(screen_sprites[3]), &(sprites_info[0]), sizeof(Sprite));
memcpy(&(screen_sprites[4]), &(sprites_info[0]), sizeof(Sprite));
memcpy(&(screen_sprites[5]), &(sprites_info[0]), sizeof(Sprite));
screen_sprites[1].has_joypad = 1;
for (i = 0; i < NO_SCREEN_SPRITES; i++) { for (i = 0; i < NO_SCREEN_SPRITES; i++) {
screen_sprites[i].bitmap_index_prev = -1; screen_sprites[i].bitmap_index_prev = -1;
@ -54,7 +57,6 @@ void main(void)
jp = joypad(); jp = joypad();
for (i = 0; i < NO_SCREEN_SPRITES; i++) { for (i = 0; i < NO_SCREEN_SPRITES; i++) {
sprite_iter_frame(&(screen_sprites[i]), &map, screen_sprites[i].has_joypad ? jp : 0, &fc); sprite_iter_frame(&(screen_sprites[i]), &map, screen_sprites[i].has_joypad ? jp : 0, &fc);
//gprintf("x: %d y: %d\n", sprites[i].pos.x, sprites[i].pos.y);
} }
// Done processing, yield CPU and wait for start of next frame // Done processing, yield CPU and wait for start of next frame

View File

@ -5,6 +5,18 @@
#include "./flags.h" #include "./flags.h"
#include "./map.h" #include "./map.h"
#define GET_TILE_INDEX_BY_COORD(x, y) ((((y) / PIXELS_PER_TILE) * 20) + ((x) / PIXELS_PER_TILE))
#define GET_SPRITE_CORNERS(sprite, sc) \
sc.itl.x = sprite->pos.x - sprite->collision_offset[sprite->bitmap_index].itl.x; \
sc.itl.y = sprite->pos.y - sprite->collision_offset[sprite->bitmap_index].itl.y - 8; \
sc.itr.x = sprite->pos.x - sprite->collision_offset[sprite->bitmap_index].itr.x; \
sc.itr.y = sprite->pos.y - sprite->collision_offset[sprite->bitmap_index].itr.y - 8; \
sc.ibl.x = sprite->pos.x - sprite->collision_offset[sprite->bitmap_index].ibl.x; \
sc.ibl.y = sprite->pos.y - sprite->collision_offset[sprite->bitmap_index].ibl.y - 8; \
sc.ibr.x = sprite->pos.x - sprite->collision_offset[sprite->bitmap_index].ibr.x; \
sc.ibr.y = sprite->pos.y - sprite->collision_offset[sprite->bitmap_index].ibr.y - 8;
typedef struct SpriteCorners { typedef struct SpriteCorners {
UVec itl; UVec itl;
@ -33,7 +45,6 @@ typedef struct Sprites {
int get_tile_index_by_coord(unsigned int x, unsigned int y); int get_tile_index_by_coord(unsigned int x, unsigned int y);
void get_sprite_corners(Sprite *sprite, SpriteCorners *r);
void sprite_iter_frame(Sprite *sprite, Map *map, int joypad, unsigned int *fc); 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_internal_collision(Map *map, Sprite *sprite);
unsigned int sprite_collision(Map *map, Sprite *sprite); unsigned int sprite_collision(Map *map, Sprite *sprite);
@ -41,29 +52,10 @@ void sprite_draw_to_screen(Sprite *sprite);
void sprite_update_bitmap(Sprite *sprite); void sprite_update_bitmap(Sprite *sprite);
int get_tile_index_by_coord(unsigned int x, unsigned int y)
{
return ((y / PIXELS_PER_TILE) * 20) + (x / PIXELS_PER_TILE);
}
void get_sprite_corners(Sprite *sprite, SpriteCorners *sc)
{
sc->itl.x = sprite->pos.x - sprite->collision_offset[sprite->bitmap_index].itl.x;
sc->itl.y = sprite->pos.y - sprite->collision_offset[sprite->bitmap_index].itl.y - 8;
sc->itr.x = sprite->pos.x - sprite->collision_offset[sprite->bitmap_index].itr.x;
sc->itr.y = sprite->pos.y - sprite->collision_offset[sprite->bitmap_index].itr.y - 8;
sc->ibl.x = sprite->pos.x - sprite->collision_offset[sprite->bitmap_index].ibl.x;
sc->ibl.y = sprite->pos.y - sprite->collision_offset[sprite->bitmap_index].ibl.y - 8;
sc->ibr.x = sprite->pos.x - sprite->collision_offset[sprite->bitmap_index].ibr.x;
sc->ibr.y = sprite->pos.y - sprite->collision_offset[sprite->bitmap_index].ibr.y - 8;
}
// update a sprite for stuff that changes every frame // update a sprite for stuff that changes every frame
void sprite_iter_frame(Sprite *sprite, Map *map, int joypad, unsigned int *fc) void sprite_iter_frame(Sprite *sprite, Map *map, int joypad, unsigned int *fc)
{ {
int c, collision_check_steps; int c, collision_check_steps, absvelx = abs(sprite->vel.x), absvely = abs(sprite->vel.y);
// target displacement is sprite->vel but may collide, giving actual displacment // target displacement is sprite->vel but may collide, giving actual displacment
Vec displacement = {.x = 0, .y = 0}; Vec displacement = {.x = 0, .y = 0};
@ -80,7 +72,7 @@ void sprite_iter_frame(Sprite *sprite, Map *map, int joypad, unsigned int *fc)
// player move left/right // player move left/right
if ((joypad & J_RIGHT) && (sprite->vel.x < TERM_VELX)) sprite->acc.x = 1; if ((joypad & J_RIGHT) && (sprite->vel.x < TERM_VELX)) sprite->acc.x = 1;
else if ((joypad & J_LEFT) && (sprite->vel.x > -TERM_VELX)) sprite->acc.x = -1; else if ((joypad & J_LEFT) && (sprite->vel.x > -TERM_VELX)) sprite->acc.x = -1;
else sprite->acc.x = -sprite->vel.x/abs(sprite->vel.x); else sprite->acc.x = -sprite->vel.x/absvelx;
sprite->vel.x += sprite->acc.x; sprite->vel.x += sprite->acc.x;
// player dash // player dash
@ -118,10 +110,16 @@ void sprite_iter_frame(Sprite *sprite, Map *map, int joypad, unsigned int *fc)
// slow down sprite in kind of realistic way // slow down sprite in kind of realistic way
// (in real life i think it's drag is proportional to sprite->vel square but not // (in real life i think it's drag is proportional to sprite->vel square but not
// sure if that is worth the compute yet cause everythign else is shit) // sure if that is worth the compute yet cause everythign else is shit)
if (sprite->vel.y > TERM_VELY) sprite->vel.y -= (abs(sprite->vel.y)-TERM_VELY)/TERM_VELY_DIVISOR; //if (sprite->vel.y > TERM_VELY) sprite->vel.y -= (absvely-TERM_VELY)/TERM_VELY_DIVISOR;
if (sprite->vel.y < -TERM_VELY) sprite->vel.y += (abs(sprite->vel.y)-TERM_VELY)/TERM_VELY_DIVISOR; //if (sprite->vel.y < -TERM_VELY) sprite->vel.y += (absvely-TERM_VELY)/TERM_VELY_DIVISOR;
if (sprite->vel.x > TERM_VELX) sprite->vel.x -= (abs(sprite->vel.x)-TERM_VELX)/TERM_VELX_DIVISOR; //if (sprite->vel.x > TERM_VELX) sprite->vel.x -= (absvelx-TERM_VELX)/TERM_VELX_DIVISOR;
if (sprite->vel.x < -TERM_VELX) sprite->vel.x += (abs(sprite->vel.x)-TERM_VELX)/TERM_VELX_DIVISOR; //if (sprite->vel.x < -TERM_VELX) sprite->vel.x += (absvelx-TERM_VELX)/TERM_VELX_DIVISOR;
//
//absvelx = abs(sprite->vel.x);
//absvely = abs(sprite->vel.y);
// if sprite not moving, no more logic needs to be done --- right?
if ((absvelx == 0) && (absvely == 0)) return;
c = sprite_collision(map, sprite); c = sprite_collision(map, sprite);
@ -138,8 +136,6 @@ void sprite_iter_frame(Sprite *sprite, Map *map, int joypad, unsigned int *fc)
if (((c & SC_RT) | (c & SC_RB)) && (sprite->vel.x > 0)) { sprite->vel.x = 0; } if (((c & SC_RT) | (c & SC_RB)) && (sprite->vel.x > 0)) { sprite->vel.x = 0; }
if (((c & SC_LT) | (c & SC_LB)) && (sprite->vel.x < 0)) { sprite->vel.x = 0; } if (((c & SC_LT) | (c & SC_LB)) && (sprite->vel.x < 0)) { sprite->vel.x = 0; }
int absvelx = abs(sprite->vel.x);
int absvely = abs(sprite->vel.y);
collision_check_steps = (absvelx > absvely) ? absvelx : absvely; collision_check_steps = (absvelx > absvely) ? absvelx : absvely;
original_position.x = sprite->pos.x; original_position.x = sprite->pos.x;
@ -154,6 +150,7 @@ void sprite_iter_frame(Sprite *sprite, Map *map, int joypad, unsigned int *fc)
sprite_update_bitmap(sprite); sprite_update_bitmap(sprite);
sprite_draw_to_screen(sprite); sprite_draw_to_screen(sprite);
move_sprite(sprite->gb_sprite_index, sprite->pos.x, sprite->pos.y);
} }
@ -162,12 +159,12 @@ unsigned int sprite_internal_collision(Map *map, Sprite *sprite)
{ {
unsigned int rv = 0; unsigned int rv = 0;
SpriteCorners sc; SpriteCorners sc;
get_sprite_corners(sprite, &sc); GET_SPRITE_CORNERS(sprite, sc);
// (((y / PIXELS_PER_TILE) * 20) + (x / PIXELS_PER_TILE))
if (map->data[get_tile_index_by_coord(sc.itl.x, sc.itl.y)]) rv = rv | SC_ITL; if (map->data[GET_TILE_INDEX_BY_COORD(sc.itl.x, sc.itl.y)]) rv = rv | SC_ITL;
if (map->data[get_tile_index_by_coord(sc.itr.x, sc.itr.y)]) rv = rv | SC_ITR; if (map->data[GET_TILE_INDEX_BY_COORD(sc.itr.x, sc.itr.y)]) rv = rv | SC_ITR;
if (map->data[get_tile_index_by_coord(sc.ibl.x, sc.ibl.y)]) rv = rv | SC_IBL; if (map->data[GET_TILE_INDEX_BY_COORD(sc.ibl.x, sc.ibl.y)]) rv = rv | SC_IBL;
if (map->data[get_tile_index_by_coord(sc.ibr.x, sc.ibr.y)]) rv = rv | SC_IBR; if (map->data[GET_TILE_INDEX_BY_COORD(sc.ibr.x, sc.ibr.y)]) rv = rv | SC_IBR;
return rv; return rv;
} }
@ -178,23 +175,24 @@ unsigned int sprite_collision(Map *map, Sprite *sprite)
{ {
unsigned int rv = 0; unsigned int rv = 0;
SpriteCorners sc; SpriteCorners sc;
get_sprite_corners(sprite, &sc); GET_SPRITE_CORNERS(sprite, sc);
// check if corners are in a non 0 tile // check if corners are in a non 0 tile
if (map->data[get_tile_index_by_coord(sc.itl.x, sc.itl.y-1)]) rv = rv | SC_TL; if (map->data[GET_TILE_INDEX_BY_COORD(sc.itl.x, sc.itl.y-1)]) rv = rv | SC_TL;
if (map->data[get_tile_index_by_coord(sc.itr.x, sc.itr.y-1)]) rv = rv | SC_TR; if (map->data[GET_TILE_INDEX_BY_COORD(sc.itr.x, sc.itr.y-1)]) rv = rv | SC_TR;
if (map->data[get_tile_index_by_coord(sc.ibl.x, sc.ibl.y+1)]) rv = rv | SC_BL; if (map->data[GET_TILE_INDEX_BY_COORD(sc.ibl.x, sc.ibl.y+1)]) rv = rv | SC_BL;
if (map->data[get_tile_index_by_coord(sc.ibr.x, sc.ibr.y+1)]) rv = rv | SC_BR; if (map->data[GET_TILE_INDEX_BY_COORD(sc.ibr.x, sc.ibr.y+1)]) rv = rv | SC_BR;
if (map->data[get_tile_index_by_coord(sc.itl.x-1, sc.itl.y )]) rv = rv | SC_LT; if (map->data[GET_TILE_INDEX_BY_COORD(sc.itl.x-1, sc.itl.y )]) rv = rv | SC_LT;
if (map->data[get_tile_index_by_coord(sc.ibl.x-1, sc.ibl.y )]) rv = rv | SC_LB; if (map->data[GET_TILE_INDEX_BY_COORD(sc.ibl.x-1, sc.ibl.y )]) rv = rv | SC_LB;
if (map->data[get_tile_index_by_coord(sc.itr.x+1, sc.itr.y )]) rv = rv | SC_RT; if (map->data[GET_TILE_INDEX_BY_COORD(sc.itr.x+1, sc.itr.y )]) rv = rv | SC_RT;
if (map->data[get_tile_index_by_coord(sc.ibr.x+1, sc.ibr.y )]) rv = rv | SC_RB; if (map->data[GET_TILE_INDEX_BY_COORD(sc.ibr.x+1, sc.ibr.y )]) rv = rv | SC_RB;
return rv; return rv;
} }
void sprite_update_bitmap(Sprite *sprite) void sprite_update_bitmap(Sprite *sprite)
{ {
sprite->bitmap_index_prev = sprite->bitmap_index; sprite->bitmap_index_prev = sprite->bitmap_index;
if ((sprite->vel.y > 0) && (sprite->vel.x > 0)) sprite->bitmap_index = SI_DOWN_RIGHT; if ((sprite->vel.y > 0) && (sprite->vel.x > 0)) sprite->bitmap_index = SI_DOWN_RIGHT;