more-generic-sprites #3

Merged
alvierahman90 merged 4 commits from more-generic-sprites into main 2022-06-12 18:38:55 +00:00
11 changed files with 203 additions and 46 deletions

74
res/sprite_metadata.c Normal file
View File

@ -0,0 +1,74 @@
#include <gb/gb.h>
#include "../src/sprite.h"
#include "../src/flags.h"
Sprite sprites_info[] = {
{
.frames_since_last_dash = 0,
.frames_since_last_jump = 0,
.name = "arrow",
.has_diag_sprites = 0,
.bitmap_index = SI_LEFT,
.has_joypad = 0,
.pos = { .x = 0, .y = 0},
.vel = { .x = 0, .y = 0},
.acc = { .x = 0, .y = 0},
.bitmap_indexes[SI_UP] = 12, .bitmap_prop[SI_UP] = 0,
.collision_offset[SI_UP] = {
.itl = { .x = 3, .y = 8},
.itr = { .x = 1, .y = 8},
.ibl = { .x = 3, .y = 1},
.ibr = { .x = 1, .y = 1},
},
.bitmap_indexes[SI_DOWN] = 12, .bitmap_prop[SI_DOWN] = S_FLIPY,
.collision_offset[SI_DOWN] = {
.itl = { .x = 8, .y = 8},
.itr = { .x = 5, .y = 8},
.ibl = { .x = 8, .y = 1},
.ibr = { .x = 5, .y = 1},
},
.bitmap_indexes[SI_LEFT] = 11, .bitmap_prop[SI_LEFT] = 0,
.collision_offset[SI_LEFT] = {
.itl = { .x = 8, .y = 3},
.itr = { .x = 1, .y = 3},
.ibl = { .x = 8, .y = 1},
.ibr = { .x = 1, .y = 1},
},
.bitmap_indexes[SI_RIGHT] = 11, .bitmap_prop[SI_RIGHT] = S_FLIPX,
.collision_offset[SI_RIGHT] = {
.itl = { .x = 8, .y = 3},
.itr = { .x = 1, .y = 3},
.ibl = { .x = 8, .y = 1},
.ibr = { .x = 1, .y = 1},
},
.bitmap_indexes[SI_UP_RIGHT] = 13, .bitmap_prop[SI_UP_RIGHT] = 0,
.collision_offset[SI_UP_RIGHT] = {
.itl = { .x = 7, .y = 8},
.itr = { .x = 1, .y = 8},
.ibl = { .x = 7, .y = 2},
.ibr = { .x = 1, .y = 2},
},
.bitmap_indexes[SI_UP_LEFT] = 13, .bitmap_prop[SI_UP_LEFT] = S_FLIPX,
.collision_offset[SI_UP_LEFT] = {
.itl = { .x = 8, .y = 8},
.itr = { .x = 2, .y = 8},
.ibl = { .x = 8, .y = 2},
.ibr = { .x = 2, .y = 2},
},
.bitmap_indexes[SI_DOWN_LEFT] = 13, .bitmap_prop[SI_DOWN_LEFT] = S_FLIPX | S_FLIPY,
.collision_offset[SI_DOWN_LEFT] = {
.itl = { .x = 8, .y = 7},
.itr = { .x = 2, .y = 7},
.ibl = { .x = 8, .y = 1},
.ibr = { .x = 2, .y = 1},
},
.bitmap_indexes[SI_DOWN_RIGHT] = 13, .bitmap_prop[SI_DOWN_RIGHT] = S_FLIPY,
.collision_offset[SI_DOWN_RIGHT] = {
.itl = { .x = 7, .y = 7},
.itr = { .x = 1, .y = 7},
.ibl = { .x = 7, .y = 1},
.ibr = { .x = 1, .y = 1},
}
}
};

3
res/sprite_metadata.h Normal file
View File

@ -0,0 +1,3 @@
#include "../src/sprite.h"
extern Sprite sprites_info[];

View File

@ -10,7 +10,7 @@
Compression : None.
Counter : None.
Tile size : 8 x 8
Tiles : 0 to 10
Tiles : 0 to 13
Palette colors : None.
SGB Palette : None.
@ -25,8 +25,8 @@
/* Start of tile array. */
unsigned char sprites[] =
{
0xF7,0x08,0xED,0x12,0xFE,0x11,0xFF,0x38,
0xFE,0x39,0xFE,0x39,0xFF,0x7E,0xFF,0x6E,
0xFF,0x19,0xEF,0x17,0xFF,0x11,0xFF,0x3B,
0xFE,0x3F,0xFE,0x3D,0xFF,0x7E,0xFF,0x6E,
0xFF,0x00,0xC3,0x42,0x81,0x00,0x81,0x00,
0x81,0x00,0x81,0x00,0xC3,0x42,0xFF,0x00,
0xC3,0xC3,0xDB,0xDB,0x3C,0x3C,0x7E,0x7E,
@ -46,7 +46,13 @@ unsigned char sprites[] =
0xC3,0xC3,0xDB,0xDB,0x3C,0x3C,0x7E,0x7E,
0x7E,0x7E,0x3C,0x3C,0xDB,0xDB,0x42,0xC3,
0x43,0xC3,0xDB,0xDB,0x3C,0x3C,0x7E,0x7E,
0x7E,0x7E,0x3C,0x3C,0xDB,0xDB,0x43,0xC3
0x7E,0x7E,0x3C,0x3C,0xDB,0xDB,0x43,0xC3,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x43,0x38,0xC6,0x00,0x43,
0x00,0x02,0x00,0x07,0x02,0x00,0x02,0x00,
0x02,0x00,0x00,0x02,0x00,0x07,0x00,0x05,
0x00,0x03,0x02,0x01,0x04,0x00,0x08,0x00,
0x00,0x30,0x00,0x50,0x00,0x20,0x00,0x00
};
/* End of SPRITES.C */

Binary file not shown.

View File

@ -10,7 +10,7 @@
Compression : None.
Counter : None.
Tile size : 8 x 8
Tiles : 0 to 10
Tiles : 0 to 13
Palette colors : None.
SGB Palette : None.

View File

@ -25,8 +25,8 @@
/* Start of tile array. */
unsigned char tiles[] =
{
0x10,0x00,0x00,0x00,0x08,0x00,0x10,0x00,
0x30,0x00,0x20,0x00,0x60,0x00,0x50,0x20,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xFF,0xFF,0xFF,0xFF,0x00,0xFF,0x00,0xFF,
0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,
0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,

Binary file not shown.

View File

@ -24,6 +24,15 @@
#define SC_IBL 0b0010
#define SC_IBR 0b0001
#define SI_UP 0
#define SI_DOWN 1
#define SI_LEFT 2
#define SI_RIGHT 3
#define SI_UP_RIGHT 4
#define SI_DOWN_RIGHT 5
#define SI_UP_LEFT 6
#define SI_DOWN_LEFT 7
/* Sprite Collision point diagram:
* x TL LT x
* ------------

View File

@ -1,22 +1,25 @@
#include <gb/gb.h>
#include <gb/drawing.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../res/tiles.h"
#include "../res/map.h"
#include"../res/sprites.h"
#include"../res/sprite_metadata.h"
#include "./flags.h"
#include "./constants.h"
#include "./sprite.h"
#include "./vec.h"
#define NO_SCREEN_SPRITES 2
void init_gfx()
{
// Load Background tiles and then map
set_bkg_data(0, 23, tiles);
set_bkg_tiles(0, 0, map.width, map.height, map.data);
set_sprite_data(0, 11, sprites);
set_sprite_tile(0, 2);
set_sprite_prop(0, 0);
set_sprite_data(0, 14, sprites);
// Turn the background map on to make it visible
SHOW_BKG;
@ -25,25 +28,34 @@ void init_gfx()
void main(void)
{
unsigned int fc = 0;
struct Sprites sprite;
init_gfx();
sprite.pos.x = 72;
sprite.pos.y = 36;
sprite.vel.x = 0;
sprite.vel.y = 0;
sprite.acc.x = 0;
sprite.acc.y = 0;
move_sprite(0, sprite.pos.x, sprite.pos.y);
unsigned int i, jp, fc = 0;
struct Sprites screen_sprites[NO_SCREEN_SPRITES];
memcpy(&(screen_sprites[0]), &(sprites_info[0]), sizeof(Sprite));
memcpy(&(screen_sprites[1]), &(sprites_info[0]), sizeof(Sprite));
screen_sprites[0].has_joypad = 1;
screen_sprites[1].bitmap_index = SI_UP;
for (i = 0; i < NO_SCREEN_SPRITES; i++) {
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);
sprite_draw_to_screen(&(screen_sprites[i]));
}
// Loop forever
while(1) {
fc++;
sprite_iter_frame(&sprite, &map, joypad(), &fc);
move_sprite(0, sprite.pos.x, sprite.pos.y);
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);
//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
wait_vbl_done();

View File

@ -6,16 +6,6 @@
#include "./map.h"
typedef struct Sprites {
int frames_since_last_dash;
int frames_since_last_jump;
int size;
UVec pos;
Vec vel;
Vec acc;
} Sprite;
typedef struct SpriteCorners {
UVec itl;
UVec itr;
@ -24,11 +14,31 @@ 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];
int has_diag_sprites;
int has_joypad;
char name[20];
SpriteCorners collision_offset[8];
UVec pos;
Vec vel;
Vec acc;
} Sprite;
int get_tile_index_by_coord(unsigned int x, unsigned int y);
void getSpriteCorners(Sprite *sprite, SpriteCorners *r);
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)
@ -37,16 +47,16 @@ int get_tile_index_by_coord(unsigned int x, unsigned int y)
}
void getSpriteCorners(Sprite *sprite, SpriteCorners *sc)
void get_sprite_corners(Sprite *sprite, SpriteCorners *sc)
{
sc->itl.x = sprite->pos.x-8;
sc->itl.y = sprite->pos.y-16;
sc->itr.x = sprite->pos.x-1;
sc->itr.y = sprite->pos.y-16;
sc->ibl.x = sprite->pos.x-8;
sc->ibl.y = sprite->pos.y-9 ;
sc->ibr.x = sprite->pos.x-1;
sc->ibr.y = sprite->pos.y-9;
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;
}
@ -141,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);
}
@ -149,7 +162,7 @@ unsigned int sprite_internal_collision(Map *map, Sprite *sprite)
{
unsigned int rv = 0;
SpriteCorners sc;
getSpriteCorners(sprite, &sc);
get_sprite_corners(sprite, &sc);
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;
@ -165,7 +178,7 @@ unsigned int sprite_collision(Map *map, Sprite *sprite)
{
unsigned int rv = 0;
SpriteCorners sc;
getSpriteCorners(sprite, &sc);
get_sprite_corners(sprite, &sc);
// 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;
@ -179,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

@ -4,10 +4,25 @@
#include "./vec.h"
#include "./map.h"
typedef struct SpriteCorners {
UVec itl;
UVec itr;
UVec ibl;
UVec ibr;
} SpriteCorners;
typedef struct Sprites {
int gb_sprite_index;
int frames_since_last_dash;
int frames_since_last_jump;
int size;
int bitmap_index_prev;
int bitmap_index;
int bitmap_indexes[8];
int bitmap_prop[8];
int has_diag_sprites;
int has_joypad;
char name[20];
SpriteCorners collision_offset[8];
UVec pos;
Vec vel;
Vec acc;
@ -16,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