refactor structures to pack everything in typedef struct Game

This commit is contained in:
SlyVTT 2023-08-11 08:04:17 +02:00
parent fe2492b7de
commit 1bf630dfcb
8 changed files with 80 additions and 74 deletions

View file

@ -8,6 +8,7 @@
#define USB_FEATURE 0 #define USB_FEATURE 0
#define DEBUGMODE 1
#ifdef FXCG50 #ifdef FXCG50
#define T_HEIGHT 16 #define T_HEIGHT 16

View file

@ -1,8 +1,61 @@
#ifndef GAME_H #ifndef GAME_H
#define GAME_H #define GAME_H
#include "mapstruct.h" #include <gint/display.h>
#include "player.h"
/* The direction where the player is going to. */
typedef enum {
D_UP,
D_DOWN,
D_LEFT,
D_RIGHT
} Direction;
typedef enum {
P_LEFTUP = -1,
P_CENTER = 0,
P_RIGHTDOWN = 1
} Checkpos;
/* Struct that define player parameters */
typedef struct {
int x, y; /* The position of the player */
unsigned int px, py; /* The position of the player on screen */
unsigned short int life; /* How many lives the player still has between 0
* and 100. */
char speed; /* The speed of the movement of the player. */
} Player;
typedef struct {
/* width, height and the number of layer of the map */
uint16_t w, h;
uint16_t nblayers;
uint16_t tileset_size;
uint16_t xmin;
uint16_t ymin;
uint16_t xmax;
uint16_t ymax;
/* the tileset to use */
bopti_image_t *tileset;
/* contain the properties of the tiles */
/* this is given by the layer Walkable of the map in Tiled */
uint8_t *walkable;
/* list of all the tiles */
uint16_t *layers[];
} Map;
/* This struct will contain all the data of the game. It will make it possible /* This struct will contain all the data of the game. It will make it possible
* to pass it to the NPCs to let them interact with the player and the rest of * to pass it to the NPCs to let them interact with the player and the rest of
@ -20,7 +73,6 @@ typedef struct {
/* How many ms the frame already took. */ /* How many ms the frame already took. */
long int frame_duration; long int frame_duration;
/* variables used for debuging */ /* variables used for debuging */
bool debug_player; bool debug_player;
bool debug_map; bool debug_map;

View file

@ -21,13 +21,12 @@
#include "game.h" #include "game.h"
#include "mapdata.h" #include "mapdata.h"
#include "mapstruct.h"
#include "dialogs.h" #include "dialogs.h"
extern bopti_image_t player_face_img; extern bopti_image_t player_face_img;
extern const Map *worldRPG[]; extern Map *worldRPG[];
/* Game data (defined in "game.h")*/ /* Game data (defined in "game.h")*/
@ -124,15 +123,23 @@ int main(void) {
/* render the map */ /* render the map */
draw(&game); draw(&game);
#if DEBUGMODE
if (game.debug_map) if (game.debug_map)
{ {
dfont( NULL ); dfont( NULL );
drect( 5, 5, 200, 50, C_WHITE ); drect( 5, 5,390, 55, C_WHITE );
dprint( 10, 10, C_RED, "Map[ % d ] : Xmin %d Ymin %d Xmax %d Ymax %d", 0, worldRPG[0]->xmin, worldRPG[0]->ymin, worldRPG[0]->xmax, worldRPG[0]->ymax ); dprint( 10, 10, C_RED, "Map[%d] : Xmn %d Ymn %d Xmx %d Ymx %d", 0, worldRPG[0]->xmin, worldRPG[0]->ymin, worldRPG[0]->xmax, worldRPG[0]->ymax );
dprint( 10, 20, C_RED, "Map[ % d ] : Xmin %d Ymin %d Xmax %d Ymax %d", 1, worldRPG[1]->xmin, worldRPG[1]->ymin, worldRPG[1]->xmax, worldRPG[1]->ymax ); dprint( 10, 20, C_RED, "Map[%d] : Xmn %d Ymn %d Xmx %d Ymx %d", 1, worldRPG[1]->xmin, worldRPG[1]->ymin, worldRPG[1]->xmax, worldRPG[1]->ymax );
dprint( 10, 30, C_RED, "Map[ % d ] : Xmin %d Ymin %d Xmax %d Ymax %d", 2, worldRPG[2]->xmin, worldRPG[2]->ymin, worldRPG[2]->xmax, worldRPG[2]->ymax ); dprint( 10, 30, C_RED, "Map[%d] : Xmn %d Ymn %d Xmx %d Ymx %d", 2, worldRPG[2]->xmin, worldRPG[2]->ymin, worldRPG[2]->xmax, worldRPG[2]->ymax );
dprint( 10, 40, C_RED, "Map[ % d ] : Xmin %d Ymin %d Xmax %d Ymax %d", 3, worldRPG[3]->xmin, worldRPG[3]->ymin, worldRPG[3]->xmax, worldRPG[3]->ymax ); dprint( 10, 40, C_RED, "Map[%d] : Xmn %d Ymn %d Xmx %d Ymx %d", 3, worldRPG[3]->xmin, worldRPG[3]->ymin, worldRPG[3]->xmax, worldRPG[3]->ymax );
} }
if (game.debug_player)
{
dfont( NULL );
drect( 5, 55,390, 75, C_WHITE );
dprint( 10, 60, C_BLUE, "X= %d - Y= %d", game.player.x, game.player.y );
}
#endif
/* start the logic of the game */ /* start the logic of the game */
game_logic(&game); game_logic(&game);

View file

@ -8,7 +8,7 @@
#define MAP_OUTSIDE -2 /* Returned by get_tile_at_pos if the point is outside of #define MAP_OUTSIDE -2 /* Returned by get_tile_at_pos if the point is outside of
* the map. */ * the map. */
#include "mapstruct.h" #include "game.h"
#include "player.h" #include "player.h"
/* Draws the map map on the entire screen to be viewed by the player player. */ /* Draws the map map on the entire screen to be viewed by the player player. */

View file

@ -2,9 +2,9 @@
#define MAPDATA_H #define MAPDATA_H
#include <stdint.h> #include <stdint.h>
#include "mapstruct.h" #include "game.h"
extern const Map *worldRPG[]; extern Map *worldRPG[];
#endif #endif

View file

@ -1,33 +0,0 @@
#ifndef MAPSTRUCT_H
#define MAPSTRUCT_H
#include <gint/display.h>
#include <stdint.h>
typedef struct {
/* width, height and the number of layer of the map */
uint16_t w, h;
uint16_t nblayers;
uint16_t tileset_size;
uint16_t xmin;
uint16_t ymin;
uint16_t xmax;
uint16_t ymax;
/* the tileset to use */
bopti_image_t *tileset;
/* contain the properties of the tiles */
/* this is given by the layer Walkable of the map in Tiled */
uint8_t *walkable;
/* list of all the tiles */
uint16_t *layers[];
} Map;
#endif

View file

@ -26,12 +26,13 @@ const char damage_taken_walkable[WALKABLE_TILE_MAX] = {
extern bopti_image_t demo_player_img; extern bopti_image_t demo_player_img;
void player_draw(Player *player) { void player_draw(Player *player) {
dimage(player->px-P_WIDTH/2, player->py-P_HEIGHT/2, &demo_player_img); dimage(player->px-P_WIDTH/2, player->py-P_HEIGHT/2, &demo_player_img);
//dprint( 10, 10, C_RED, "X= %d - Y= %d", player->x, player->y );
} }
void player_move(Map *map_level, Player *player, Direction direction) { void player_move(Map *map_level, Player *player, Direction direction) {
/* How this player movement will modify the player x and y. */ /* How this player movement will modify the player x and y. */
char dx, dy; char dx, dy;
/* If the player will collide with a hard tile or if the will go outside of /* If the player will collide with a hard tile or if the will go outside of

View file

@ -16,31 +16,9 @@
#include <stdbool.h> #include <stdbool.h>
#include "mapstruct.h" #include "game.h"
#include "memory.h" #include "memory.h"
/* The direction where the player is going to. */
typedef enum {
D_UP,
D_DOWN,
D_LEFT,
D_RIGHT
} Direction;
typedef enum {
P_LEFTUP = -1,
P_CENTER = 0,
P_RIGHTDOWN = 1
} Checkpos;
/* Struct that define player parameters */
typedef struct {
int x, y; /* The position of the player */
unsigned int px, py; /* The position of the player on screen */
unsigned short int life; /* How many lives the player still has between 0
* and 100. */
char speed; /* The speed of the movement of the player. */
} Player;
/* Draws the player player. This function should be called after drawing the /* Draws the player player. This function should be called after drawing the
* map! */ * map! */