maps are now structs

This commit is contained in:
Akbar Rahman 2022-06-09 14:05:02 +01:00
parent e67254f179
commit 32c2d32e6a
Signed by: alvierahman90
GPG Key ID: 20609519444A1269
6 changed files with 111 additions and 88 deletions

View File

@ -15,15 +15,21 @@
Split data : No
This file was generated by GBMB v1.8
and modfied
*/
#include "../src/map.h"
#define mapWidth 20
#define mapHeight 18
#define mapBank 0
unsigned char map[] =
{
Map map = {
.width = mapWidth,
.height = mapHeight,
.bank = mapBank,
.data = {
0x09,0x02,0x02,0x02,0x02,0x02,0x02,0x08,0x00,0x00,
0x00,0x07,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x0A,
0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
@ -60,6 +66,7 @@ unsigned char map[] =
0x00,0x00,0x00,0x00,0x00,0x05,0x0B,0x13,0x13,0x13,
0x0C,0x01,0x01,0x01,0x01,0x01,0x01,0x06,0x00,0x00,
0x00,0x05,0x01,0x01,0x01,0x0B,0x13,0x13,0x13,0x13
}
};
/* End of MAP.C */

View File

@ -22,6 +22,8 @@
#define mapHeight 18
#define mapBank 0
extern unsigned char map[];
#include "../src/map.h"
extern Map map;
/* End of MAP.H */

View File

@ -12,7 +12,7 @@ void init_gfx()
{
// Load Background tiles and then map
set_bkg_data(0, 23, tiles);
set_bkg_tiles(0, 0, mapWidth, mapHeight, map);
set_bkg_tiles(0, 0, map.width, map.height, map.data);
set_sprite_data(0, 11, sprites);
set_sprite_tile(0, 2);
@ -42,7 +42,7 @@ void main(void)
// Loop forever
while(1) {
fc++;
sprite_iter_frame(&sprite, map, joypad(), &fc);
sprite_iter_frame(&sprite, &map, joypad(), &fc);
move_sprite(0, sprite.pos.x, sprite.pos.y);
// Done processing, yield CPU and wait for start of next frame

11
src/map.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef MAP_H_
#define MAP_H_
typedef struct Map {
unsigned int width;
unsigned int height;
unsigned int bank;
unsigned char data[];
} Map;
#endif

View File

@ -3,6 +3,8 @@
#include "./vec.h"
#include "./constants.h"
#include "./flags.h"
#include "./map.h"
typedef struct Sprites {
int frames_since_last_dash;
@ -22,14 +24,34 @@ typedef struct SpriteCorners {
} SpriteCorners;
void sprite_iter_frame(Sprite *sprite, unsigned char map[], int joypad, unsigned int *fc);
int getTileIndexByCoord(unsigned int x, unsigned int y);
unsigned int sprite_internal_collision(unsigned char map[], Sprite *sprite);
unsigned int sprite_collision(unsigned char map[], Sprite *sprite);
void sprite_decollide(unsigned char map[], Sprite *sprite);
int get_tile_index_by_coord(unsigned int x, unsigned int y);
void getSpriteCorners(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);
int get_tile_index_by_coord(unsigned int x, unsigned int y)
{
return ((y / PIXELS_PER_TILE) * 20) + (x / PIXELS_PER_TILE);
}
void getSpriteCorners(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;
}
// update a sprite for stuff that changes every frame
void sprite_iter_frame(Sprite *sprite, unsigned char map[], int joypad, unsigned int *fc)
void sprite_iter_frame(Sprite *sprite, Map *map, int joypad, unsigned int *fc)
{
int c, collision_check_steps;
@ -122,55 +144,38 @@ void sprite_iter_frame(Sprite *sprite, unsigned char map[], int joypad, unsigned
}
int getTileIndexByCoord(unsigned int x, unsigned int y)
{
return ((y / PIXELS_PER_TILE) * 20) + (x / PIXELS_PER_TILE);
}
SpriteCorners *getSpriteCorners(Sprite *sprite)
{
SpriteCorners r = {
.itl = { .x = sprite->pos.x-8, .y = sprite->pos.y-16 },
.itr = { .x = sprite->pos.x-1, .y = sprite->pos.y-16 },
.ibl = { .x = sprite->pos.x-8, .y = sprite->pos.y-9 },
.ibr = { .x = sprite->pos.x-1, .y = sprite->pos.y-9 }
};
return &r;
}
// calculate internal collisions
unsigned int sprite_internal_collision(unsigned char map[], Sprite *sprite)
unsigned int sprite_internal_collision(Map *map, Sprite *sprite)
{
unsigned int rv = 0;
SpriteCorners *sc = getSpriteCorners(sprite);
SpriteCorners sc;
getSpriteCorners(sprite, &sc);
if (map[getTileIndexByCoord(sc->itl.x, sc->itl.y)]) rv = rv | SC_ITL;
if (map[getTileIndexByCoord(sc->itr.x, sc->itr.y)]) rv = rv | SC_ITR;
if (map[getTileIndexByCoord(sc->ibl.x, sc->ibl.y)]) rv = rv | SC_IBL;
if (map[getTileIndexByCoord(sc->ibr.x, sc->ibr.y)]) rv = rv | SC_IBR;
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.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;
return rv;
}
// check for external collisions (if rectangle is at least touchign something)
unsigned int sprite_collision(unsigned char map[], Sprite *sprite)
unsigned int sprite_collision(Map *map, Sprite *sprite)
{
unsigned int rv = 0;
SpriteCorners *sc = getSpriteCorners(sprite);
SpriteCorners sc;
getSpriteCorners(sprite, &sc);
// check if corners are in a non 0 tile
if (map[getTileIndexByCoord(sc->itl.x, sc->itl.y-1)]) rv = rv | SC_TL;
if (map[getTileIndexByCoord(sc->itr.x, sc->itr.y-1)]) rv = rv | SC_TR;
if (map[getTileIndexByCoord(sc->ibl.x, sc->ibl.y+1)]) rv = rv | SC_BL;
if (map[getTileIndexByCoord(sc->ibr.x, sc->ibr.y+1)]) rv = rv | SC_BR;
if (map[getTileIndexByCoord(sc->itl.x-1, sc->itl.y)]) rv = rv | SC_LT;
if (map[getTileIndexByCoord(sc->ibl.x-1, sc->ibl.y)]) rv = rv | SC_LB;
if (map[getTileIndexByCoord(sc->itr.x+1, sc->itr.y)]) rv = rv | SC_RT;
if (map[getTileIndexByCoord(sc->ibr.x+1, sc->ibr.y)]) rv = rv | SC_RB;
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.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.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.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;
return rv;
}

View File

@ -2,6 +2,7 @@
#define SPRITE_H_
#include "./vec.h"
#include "./map.h"
typedef struct Sprites {
int frames_since_last_dash;
@ -12,11 +13,8 @@ typedef struct Sprites {
Vec acc;
} Sprite;
void sprite_iter_frame(Sprite *sprite, unsigned char map[], int joypad, unsigned int *fc);
int getTileIndexByCoord(unsigned int x, unsigned int y);
unsigned int sprite_internal_collision(unsigned char map[], Sprite *sprite);
unsigned int sprite_collision(unsigned char map[], Sprite *sprite);
void sprite_decollide(unsigned char map[], Sprite *sprite);
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);
#endif