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 DEBUGMODE 1
#ifdef FXCG50
#define T_HEIGHT 16

View file

@ -1,8 +1,61 @@
#ifndef GAME_H
#define GAME_H
#include "mapstruct.h"
#include "player.h"
#include <gint/display.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
* 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. */
long int frame_duration;
/* variables used for debuging */
bool debug_player;
bool debug_map;

View file

@ -21,13 +21,12 @@
#include "game.h"
#include "mapdata.h"
#include "mapstruct.h"
#include "dialogs.h"
extern bopti_image_t player_face_img;
extern const Map *worldRPG[];
extern Map *worldRPG[];
/* Game data (defined in "game.h")*/
@ -124,15 +123,23 @@ int main(void) {
/* render the map */
draw(&game);
if (game.debug_map)
{
dfont( NULL );
drect( 5, 5, 200, 50, 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, 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, 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, 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 );
}
#if DEBUGMODE
if (game.debug_map)
{
dfont( NULL );
drect( 5, 5,390, 55, C_WHITE );
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] : 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] : 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] : 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 */
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
* the map. */
#include "mapstruct.h"
#include "game.h"
#include "player.h"
/* Draws the map map on the entire screen to be viewed by the player player. */

View file

@ -2,9 +2,9 @@
#define MAPDATA_H
#include <stdint.h>
#include "mapstruct.h"
#include "game.h"
extern const Map *worldRPG[];
extern Map *worldRPG[];
#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;
void player_draw(Player *player) {
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) {
/* How this player movement will modify the player x and y. */
char dx, dy;
/* 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 "mapstruct.h"
#include "game.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
* map! */