From 908b97131760d7dd5ddaabe246a99a2e30e5fc9f Mon Sep 17 00:00:00 2001 From: mibi88 <76903855+mibi88@users.noreply.github.com> Date: Sat, 8 Jul 2023 15:55:06 +0200 Subject: [PATCH 1/4] Restructured a lot of things. Collisions are not working properly but I'll fix that --- CMakeLists.txt | 2 ++ src/game.c | 13 +++++++++ src/game.h | 22 +++++++++++++++ src/main.c | 32 +++++++++------------- src/map.c | 16 ++++++++++- src/map.h | 23 ++++++---------- src/mapstruct.h | 18 ++++++++++++ src/memory.c | 12 ++++++++ src/memory.h | 9 ++++++ src/player.c | 73 +++++++++++++++++++++++++++++++------------------ src/player.h | 35 ++++++++++++++++++------ 11 files changed, 185 insertions(+), 70 deletions(-) create mode 100644 src/game.c create mode 100644 src/game.h create mode 100644 src/mapstruct.h create mode 100644 src/memory.c create mode 100644 src/memory.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ceb767e..98b9ef1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,6 +37,8 @@ set(SOURCES src/main.c src/map.c src/player.c + src/memory.c + src/game.c # ... ) # Shared assets, fx-9860G-only assets and fx-CG-50-only assets diff --git a/src/game.c b/src/game.c new file mode 100644 index 0000000..f424503 --- /dev/null +++ b/src/game.c @@ -0,0 +1,13 @@ +#include "game.h" + +#include "map.h" + +void game_logic(Game *game) { + // to be done +} + +void draw(Game *game) { + render_map(&game->player, game->map_level); + player_draw(&game->player); +} + diff --git a/src/game.h b/src/game.h new file mode 100644 index 0000000..e01632e --- /dev/null +++ b/src/game.h @@ -0,0 +1,22 @@ +#ifndef GAME_H +#define GAME_H + +#include "mapstruct.h" +#include "player.h" + +/* 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 + * the world. */ +typedef struct { + Map *map_level; /* The level that the player is currently playing */ + Player player; /* The player data (see player.h). */ +} Game; + +/* (Mibi88) TODO: Describe what this function is doing. */ +void game_logic(Game *game); + +/* Draws everything on screen. */ +void draw(Game *game); + +#endif + diff --git a/src/main.c b/src/main.c index d803bff..21ff533 100644 --- a/src/main.c +++ b/src/main.c @@ -21,13 +21,14 @@ #include #include -#include "map.h" -#include "player.h" +#include "game.h" #include "mapdata.h" -/* Player data (defined in "player.h")*/ -Player MyPlayer = { 10, 5, 0, 0, 100 }; -Map *map_level = &map_level0; +/* Game data (defined in "game.h")*/ +Game game = { + &map_level0, + {10, 5, 0, 0, 100} +}; /* some global variables */ bool exittoOS = false; // set to true when asked for exit @@ -51,11 +52,11 @@ static void get_inputs( void ) if(keydown(KEY_EXIT)) exittoOS = true; /* Player actions - Prototypes in player.h and implementation in player.c */ - if(keydown(KEY_LEFT)) PlayerLeft(); - if(keydown(KEY_RIGHT)) PlayerRight(); - if(keydown(KEY_UP)) PlayerUp(); - if(keydown(KEY_DOWN)) PlayerDown(); - if(keydown(KEY_SHIFT)) PlayerAction(); + if(keydown(KEY_LEFT)) player_move(game.map_level, &game.player, D_LEFT); + if(keydown(KEY_RIGHT)) player_move(game.map_level, &game.player, D_RIGHT); + if(keydown(KEY_UP)) player_move(game.map_level, &game.player, D_UP); + if(keydown(KEY_DOWN)) player_move(game.map_level, &game.player, D_DOWN); + if(keydown(KEY_SHIFT)) player_action(&game.player); /* if USB is enabled - keybinding for screencapture */ @@ -105,12 +106,6 @@ static void get_inputs( void ) #endif - -void GameLogic(void) { - // to be done -} - - int main(void) { #if USB_FEATURE==1 @@ -132,11 +127,10 @@ int main(void) { dclear(C_WHITE); /* render the map */ - RenderMap(&MyPlayer, map_level); - PlayerDraw(); + draw(&game); /* start the logic of the game */ - GameLogic(); + game_logic(&game); /* Screen blit */ dupdate(); diff --git a/src/map.c b/src/map.c index 749b6e7..c52c70a 100644 --- a/src/map.c +++ b/src/map.c @@ -2,7 +2,7 @@ #include -void RenderMap(Player *player, Map *map_level) { +void render_map(Player *player, Map *map_level) { /* for all Layer (2 in the current configuration: Background is layer 0 and * foreground is layer 1 ) */ /* x and y will contain the position in the loop. */ @@ -87,3 +87,17 @@ void RenderMap(Player *player, Map *map_level) { } } +/* short int get_tile_at_pos(Map *map_level, int x, int y, int l) { + unsigned short int xtile = x/T_WIDTH; + unsigned short int xtile = x/T_HEIGHT; + unsigned short int map_w = map_level->w*T_WIDTH; + unsigned short int map_h = map_level->h*T_HEIGHT; + return x>=0 && x < map_w && y>=0 && y < map_h ? + map_level->layers[l][ytile * map_level->w + xtile] : MAP_OUTSIDE; +} */ + +short int get_tile(Map *map_level, int x, int y, int l) { + return x>=0 && x < map_level->w && y>=0 && y < map_level->h ? + map_level->layers[l][y * map_level->w + x] : MAP_OUTSIDE; +} + diff --git a/src/map.h b/src/map.h index 0d7fe0f..42293f1 100644 --- a/src/map.h +++ b/src/map.h @@ -10,25 +10,18 @@ #define T_WIDTH 8 #endif +#define MAP_OUTSIDE -2 /* Returned by get_tile_at_pos if the point is outside of + * the map. */ -#include - +#include "mapstruct.h" #include "player.h" -typedef struct { +/* Draws the map map on the entire screen to be viewed by the player player. */ +void render_map(Player *player, Map *map_level); - /*width, height and the number of layer of the map*/ - int w, h, nblayers; - - /*the tileset to use*/ - bopti_image_t *tileset; - int tileset_size; - - /*list of all the tiles*/ - short *layers[]; -} Map; - -void RenderMap(Player *player, Map *map_level); +/* Get the tile at (x, y) of the map map. If the tile is located outside of the + * screen, MAP_OUTSIDE is returned. */ +short int get_tile(Map *map_level, int x, int y, int l); #endif diff --git a/src/mapstruct.h b/src/mapstruct.h new file mode 100644 index 0000000..bcf1c77 --- /dev/null +++ b/src/mapstruct.h @@ -0,0 +1,18 @@ +#ifndef MAPSTRUCT_H +#define MAPSTRUCT_H + +#include + +typedef struct { + /* width, height and the number of layer of the map */ + int w, h, nblayers; + + /* the tileset to use */ + bopti_image_t *tileset; + int tileset_size; + + /* list of all the tiles */ + short *layers[]; +} Map; + +#endif diff --git a/src/memory.c b/src/memory.c new file mode 100644 index 0000000..9e3e58a --- /dev/null +++ b/src/memory.c @@ -0,0 +1,12 @@ +#include "memory.h" + +bool is_in(short int *array, short int array_length, short int item) { + short int i; + for(i=0;i + +bool is_in(short int *array, short int array_length, short int item); + +#endif + diff --git a/src/player.c b/src/player.c index 5d7c582..1932a00 100644 --- a/src/player.c +++ b/src/player.c @@ -2,51 +2,72 @@ #include "map.h" #include - +/* (Mibi88) TODO: Upscale the player for the CG50. */ #define P_WIDTH 8 #define P_HEIGHT 8 - +/* SPEED should NOT be 8 or bigger: it this may cause bugs when handling + * collisions! */ #ifdef FXCG50 #define SPEED 3 #else #define SPEED 1 #endif +const char one_px_mov[8] = { + 0, -1, /* Up */ + 0, 1, /* Down */ + -1, 0, /* Left */ + 1, 0 /* Right */ +}; + +/* TODO: Search for all hard tiles in the tileset. hard_tiles is a list of their + * IDs */ +#define HARD_TILES_AMOUNT 5 +const short int hard_tiles[HARD_TILES_AMOUNT] = { + MAP_OUTSIDE, 124, 148, 125, 149 +}; -extern Player MyPlayer; -extern Map *map_level; extern bopti_image_t demo_player_img; - -void PlayerDraw(void) { - dimage(MyPlayer.px-P_WIDTH/2, MyPlayer.py-P_HEIGHT/2, &demo_player_img); +void player_draw(Player *player) { + dimage(player->px-P_WIDTH/2, player->py-P_HEIGHT/2, &demo_player_img); } -void PlayerLeft(void) { - if(MyPlayer.x >= SPEED){ - MyPlayer.x-=SPEED; +void player_move(Map *map_level, Player *player, Direction direction) { + /* How this player movement will modify the player x and y. */ + const char dx = one_px_mov[direction*2]*SPEED; + const char dy = one_px_mov[direction*2+1]*SPEED; + if(player_collision(map_level, player, direction)){ + player_fix_position(player, dx, dy); + }else{ + player->x += dx; + player->y += dy; } } -void PlayerRight(void) { - if(MyPlayer.x <= map_level->w * T_WIDTH - SPEED){ - MyPlayer.x+=SPEED; +void player_action(Player *player) { + /**/ +} + +bool player_collision(Map *map_level, Player *player, Direction direction) { + /* What's the tile the player is going to. */ + short int i; + const char dx = one_px_mov[direction*2]; + const char dy = one_px_mov[direction*2+1]; + int player_tile_x = player->x/T_WIDTH; + int player_tile_y = player->x/T_WIDTH; + for(i=0;inblayers;i++){ + if(is_in(hard_tiles, HARD_TILES_AMOUNT, + get_tile(map_level, player_tile_x+dx, player_tile_y+dy, i))){ + return true; + } } + return false; } -void PlayerUp(void) { - if(MyPlayer.y >= SPEED){ - MyPlayer.y-=SPEED; - } +void player_fix_position(Player *player, bool fix_x, bool fix_y) { + if(fix_x) player->x = player->x/T_WIDTH*T_WIDTH; + if(fix_y) player->y = player->y/T_HEIGHT*T_HEIGHT; } -void PlayerDown(void) { - if(MyPlayer.y <= map_level->h * T_HEIGHT - SPEED){ - MyPlayer.y+=SPEED; - } -} - -void PlayerAction(void) { - -} diff --git a/src/player.h b/src/player.h index 6277655..e4771a2 100644 --- a/src/player.h +++ b/src/player.h @@ -1,6 +1,19 @@ #ifndef PLAYER_H #define PLAYER_H +#include + +#include "mapstruct.h" +#include "memory.h" + +/* The direction where the player is going to. */ +typedef enum { + D_UP, + D_DOWN, + D_LEFT, + D_RIGHT +} Direction; + /* Struct that define player parameters */ typedef struct { unsigned short int x, y; /* The position of the player */ @@ -9,18 +22,22 @@ typedef struct { * and 100. */ } Player; -/* This function should be called after drawing the map ! */ -void PlayerDraw(void); +/* Draws the player player. This function should be called after drawing the + * map! */ +void player_draw(Player *player); -void PlayerLeft(void); +/* Move the player player in the direction direction. */ +void player_move(Map *map_level, Player *player, Direction direction); -void PlayerRight(void); +/* (Mibi88) TODO: Describe this function please, I've no idea what she's for! */ +void player_action(Player *player); -void PlayerUp(void); - -void PlayerDown(void); - -void PlayerAction(void); +/* Check if the player is in collision with the map or a NPC. */ +bool player_collision(Map *map_level, Player *player, Direction direction); +/* Fix the position of the player so that he's not a bit inside of a hard block + * after a collision. */ +void player_fix_position(Player *player, bool fix_x, bool fix_y); #endif + From 8d316d0d0d12c68cf1d6ea08c88d1640ff32a991 Mon Sep 17 00:00:00 2001 From: mibi88 <76903855+mibi88@users.noreply.github.com> Date: Sat, 8 Jul 2023 16:40:25 +0200 Subject: [PATCH 2/4] Collisions are working :D! I only need to add a framerate limit and add some comments, then everything will be good to merge! --- src/player.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/player.c b/src/player.c index 1932a00..e5094f1 100644 --- a/src/player.c +++ b/src/player.c @@ -3,6 +3,9 @@ #include /* (Mibi88) TODO: Upscale the player for the CG50. */ +/* The player should not be bigger than a tile because it may cause problems + * with the collisions. If it's a problem please ask me (Mibi88) to adapt that. + */ #define P_WIDTH 8 #define P_HEIGHT 8 @@ -56,9 +59,9 @@ bool player_collision(Map *map_level, Player *player, Direction direction) { const char dx = one_px_mov[direction*2]; const char dy = one_px_mov[direction*2+1]; int player_tile_x = player->x/T_WIDTH; - int player_tile_y = player->x/T_WIDTH; + int player_tile_y = player->y/T_HEIGHT; for(i=0;inblayers;i++){ - if(is_in(hard_tiles, HARD_TILES_AMOUNT, + if(is_in((short int*)hard_tiles, HARD_TILES_AMOUNT, get_tile(map_level, player_tile_x+dx, player_tile_y+dy, i))){ return true; } @@ -67,7 +70,7 @@ bool player_collision(Map *map_level, Player *player, Direction direction) { } void player_fix_position(Player *player, bool fix_x, bool fix_y) { - if(fix_x) player->x = player->x/T_WIDTH*T_WIDTH; - if(fix_y) player->y = player->y/T_HEIGHT*T_HEIGHT; + if(fix_x) player->x = player->x/T_WIDTH*T_WIDTH+P_WIDTH/2; + if(fix_y) player->y = player->y/T_HEIGHT*T_HEIGHT+P_HEIGHT/2; } From 94b2187ad44cff2f6dca5db22f0ae24a2b90eaea Mon Sep 17 00:00:00 2001 From: mibi88 <76903855+mibi88@users.noreply.github.com> Date: Sat, 8 Jul 2023 16:57:04 +0200 Subject: [PATCH 3/4] Added some comments. --- src/game.c | 1 + src/main.c | 15 ++++++++++++--- src/map.c | 2 ++ src/player.c | 13 +++++++++++-- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/game.c b/src/game.c index f424503..6488d18 100644 --- a/src/game.c +++ b/src/game.c @@ -7,6 +7,7 @@ void game_logic(Game *game) { } void draw(Game *game) { + /* Draw everything. */ render_map(&game->player, game->map_level); player_draw(&game->player); } diff --git a/src/main.c b/src/main.c index 21ff533..4f62c76 100644 --- a/src/main.c +++ b/src/main.c @@ -31,10 +31,18 @@ Game game = { }; /* some global variables */ -bool exittoOS = false; // set to true when asked for exit -bool screenshot = false; // set to true when screenshot is required -bool record = false; // set to true when +/* Set to true when asked for exit */ +bool exittoOS = false; + +/* Set to true when screenshot is required */ +bool screenshot = false; + +/* Set to true when recording a video of the screen is required */ +bool record = false; + +/* How many ms the frame already took. */ +long int frame_duration; /* Key management */ @@ -162,3 +170,4 @@ int main(void) { return 1; } + diff --git a/src/map.c b/src/map.c index c52c70a..840dcea 100644 --- a/src/map.c +++ b/src/map.c @@ -97,6 +97,8 @@ void render_map(Player *player, Map *map_level) { } */ short int get_tile(Map *map_level, int x, int y, int l) { + /* Get the tile at (x, y) on layer l. Returns the tile ID or MAP_OUTSIDE if + * she's not found. */ return x>=0 && x < map_level->w && y>=0 && y < map_level->h ? map_level->layers[l][y * map_level->w + x] : MAP_OUTSIDE; } diff --git a/src/player.c b/src/player.c index e5094f1..eadf11f 100644 --- a/src/player.c +++ b/src/player.c @@ -26,6 +26,7 @@ const char one_px_mov[8] = { /* TODO: Search for all hard tiles in the tileset. hard_tiles is a list of their * IDs */ +/* The tiles where the player can't go trough. */ #define HARD_TILES_AMOUNT 5 const short int hard_tiles[HARD_TILES_AMOUNT] = { MAP_OUTSIDE, 124, 148, 125, 149 @@ -41,9 +42,12 @@ void player_move(Map *map_level, Player *player, Direction direction) { /* How this player movement will modify the player x and y. */ const char dx = one_px_mov[direction*2]*SPEED; const char dy = one_px_mov[direction*2+1]*SPEED; + /* If the player will collide with a hard tile. */ if(player_collision(map_level, player, direction)){ + /* I fix his position so he won't be partially in the tile. */ player_fix_position(player, dx, dy); }else{ + /* If he won't collide I just move him normally */ player->x += dx; player->y += dy; } @@ -56,20 +60,25 @@ void player_action(Player *player) { bool player_collision(Map *map_level, Player *player, Direction direction) { /* What's the tile the player is going to. */ short int i; + /* Where is the tile where he will go to from his position. */ const char dx = one_px_mov[direction*2]; const char dy = one_px_mov[direction*2+1]; + /* The tile he will go to. */ int player_tile_x = player->x/T_WIDTH; int player_tile_y = player->y/T_HEIGHT; for(i=0;inblayers;i++){ + /* if he's on a hard tile */ if(is_in((short int*)hard_tiles, HARD_TILES_AMOUNT, get_tile(map_level, player_tile_x+dx, player_tile_y+dy, i))){ - return true; + return true; /* He will collide with it. */ } } - return false; + return false; /* He won't collide with a hard tile. */ } void player_fix_position(Player *player, bool fix_x, bool fix_y) { + /* I fix his poition on x or/and on y if y need to, so that he won't be over + * the hard tile that he collided with. */ if(fix_x) player->x = player->x/T_WIDTH*T_WIDTH+P_WIDTH/2; if(fix_y) player->y = player->y/T_HEIGHT*T_HEIGHT+P_HEIGHT/2; } From 23dde6593c817fb63b347937eaaf69a4c912aa93 Mon Sep 17 00:00:00 2001 From: mibi88 <76903855+mibi88@users.noreply.github.com> Date: Sat, 8 Jul 2023 20:07:34 +0200 Subject: [PATCH 4/4] Improved collisions and CG port. Still need to make the collisions less sticky. --- CMakeLists.txt | 3 +- assets-cg/demo_player.png | Bin 0 -> 117 bytes assets-cg/fxconv-metadata.txt | 4 +- {assets => assets-fx}/demo_player.png | Bin assets-fx/fxconv-metadata.txt | 12 ++++ assets/fxconv-metadata.txt | 4 -- src/config.h | 6 ++ src/game.c | 35 +++++++++ src/game.h | 12 ++++ src/main.c | 98 +++++++++----------------- src/map.c | 11 +-- src/player.c | 17 ++--- 12 files changed, 114 insertions(+), 88 deletions(-) create mode 100644 assets-cg/demo_player.png rename {assets => assets-fx}/demo_player.png (100%) create mode 100644 assets-fx/fxconv-metadata.txt create mode 100644 src/config.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e2dfdc..725b16e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,15 +44,16 @@ set(SOURCES # Shared assets, fx-9860G-only assets and fx-CG-50-only assets set(ASSETS assets/level0.json - assets/demo_player.png # ... ) set(ASSETS_cg assets-cg/tileset2b_CG.png + assets-cg/demo_player.png ) set(ASSETS_fx + assets-fx/demo_player.png # ... ) diff --git a/assets-cg/demo_player.png b/assets-cg/demo_player.png new file mode 100644 index 0000000000000000000000000000000000000000..376ade9b7e9d34b20daf378530609196958fee6c GIT binary patch literal 117 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!60wlNoGJgf6Y&=~YLo_BPr~EkIAa3w*dRjt4 zVnV`!Ob%s3wF9CCMg~R+$->XsO3D>BzkJK)regL|ZNY1mTP$u}$-)c^FL7uVaQg58 P&0+9#^>bP0l+XkKByA#* literal 0 HcmV?d00001 diff --git a/assets-cg/fxconv-metadata.txt b/assets-cg/fxconv-metadata.txt index d110fdb..114c15f 100644 --- a/assets-cg/fxconv-metadata.txt +++ b/assets-cg/fxconv-metadata.txt @@ -1,5 +1,7 @@ - tileset2b_CG.png: type: bopti-image name: img_tilesetnpp +demo_player.png: + type: bopti-image + name: demo_player_img diff --git a/assets/demo_player.png b/assets-fx/demo_player.png similarity index 100% rename from assets/demo_player.png rename to assets-fx/demo_player.png diff --git a/assets-fx/fxconv-metadata.txt b/assets-fx/fxconv-metadata.txt new file mode 100644 index 0000000..db0f145 --- /dev/null +++ b/assets-fx/fxconv-metadata.txt @@ -0,0 +1,12 @@ +tileset1b.png: + type: bopti-image + name: img_tilesetnpp + + +tileset2b.png: + type: bopti-image + name: img_tilesetnpp + +demo_player.png: + type: bopti-image + name: demo_player_img diff --git a/assets/fxconv-metadata.txt b/assets/fxconv-metadata.txt index 77a8758..ddb88cb 100644 --- a/assets/fxconv-metadata.txt +++ b/assets/fxconv-metadata.txt @@ -1,7 +1,3 @@ *.json: custom-type: map name_regex: (.*)\.json map_\1 - -demo_player.png: - type: bopti-image - name: demo_player_img \ No newline at end of file diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..87cd453 --- /dev/null +++ b/src/config.h @@ -0,0 +1,6 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#define USB_FEATURE 1 + +#endif diff --git a/src/game.c b/src/game.c index 6488d18..4bf871b 100644 --- a/src/game.c +++ b/src/game.c @@ -2,6 +2,10 @@ #include "map.h" +#include "config.h" + +#include + void game_logic(Game *game) { // to be done } @@ -12,3 +16,34 @@ void draw(Game *game) { player_draw(&game->player); } +/* Key management */ + +void get_inputs(Game *game) { + key_event_t ev; + while((ev = pollevent()).type != KEYEV_NONE){ + /**/ + } + + /* Key binding for the Player action */ + + /*************************************/ + + if(keydown(KEY_EXIT)) game->exittoOS = true; + + /* Player actions - Prototypes in player.h and implementation in player.c */ + if(keydown(KEY_LEFT)) player_move(game->map_level, &game->player, D_LEFT); + if(keydown(KEY_RIGHT)) player_move(game->map_level, &game->player, D_RIGHT); + if(keydown(KEY_UP)) player_move(game->map_level, &game->player, D_UP); + if(keydown(KEY_DOWN)) player_move(game->map_level, &game->player, D_DOWN); + if(keydown(KEY_SHIFT)) player_action(&game->player); + + /* if USB is enabled - keybinding for screencapture */ + +#if USB_FEATURE==1 + + if(keydown(KEY_7)) game->screenshot = true; + if(keydown(KEY_8)) game->record = !game->record; + +#endif //USB_FEATURE +} + diff --git a/src/game.h b/src/game.h index e01632e..0a487a2 100644 --- a/src/game.h +++ b/src/game.h @@ -10,6 +10,15 @@ typedef struct { Map *map_level; /* The level that the player is currently playing */ Player player; /* The player data (see player.h). */ + /* Some global variables */ + /* Set to true when asked for exit */ + bool exittoOS; + /* Set to true when screenshot is required */ + bool screenshot; + /* Set to true when recording a video of the screen is required */ + bool record; + /* How many ms the frame already took. */ + long int frame_duration; } Game; /* (Mibi88) TODO: Describe what this function is doing. */ @@ -18,5 +27,8 @@ void game_logic(Game *game); /* Draws everything on screen. */ void draw(Game *game); +/* Handle key presses. */ +void get_inputs(Game *game); + #endif diff --git a/src/main.c b/src/main.c index 4f62c76..63273ae 100644 --- a/src/main.c +++ b/src/main.c @@ -1,10 +1,12 @@ #include #include +#include +#include -#define USB_FEATURE 1 +#include "config.h" -#if USB_FEATURE==1 +#if USB_FEATURE #include #include #endif //USB_FEATURE @@ -27,65 +29,19 @@ /* Game data (defined in "game.h")*/ Game game = { &map_level0, - {10, 5, 0, 0, 100} + {10, 5, 0, 0, 100}, + false, false, false, 0 }; -/* some global variables */ - -/* Set to true when asked for exit */ -bool exittoOS = false; - -/* Set to true when screenshot is required */ -bool screenshot = false; - -/* Set to true when recording a video of the screen is required */ -bool record = false; - -/* How many ms the frame already took. */ -long int frame_duration; - -/* Key management */ - -static void get_inputs( void ) -{ - key_event_t ev; - while((ev = pollevent()).type != KEYEV_NONE){ - - } - - /* Key binding for the Player action */ - - /*************************************/ - - if(keydown(KEY_EXIT)) exittoOS = true; - - /* Player actions - Prototypes in player.h and implementation in player.c */ - if(keydown(KEY_LEFT)) player_move(game.map_level, &game.player, D_LEFT); - if(keydown(KEY_RIGHT)) player_move(game.map_level, &game.player, D_RIGHT); - if(keydown(KEY_UP)) player_move(game.map_level, &game.player, D_UP); - if(keydown(KEY_DOWN)) player_move(game.map_level, &game.player, D_DOWN); - if(keydown(KEY_SHIFT)) player_action(&game.player); - - /* if USB is enabled - keybinding for screencapture */ - -#if USB_FEATURE==1 - - if(keydown(KEY_7)) screenshot = true; - if(keydown(KEY_8)) record = !record; - -#endif //USB_FEATURE -} - - /* screen capture management code */ #if USB_FEATURE==1 void USB_feature( void ) { - if (screenshot && usb_is_open()) { + if (game.screenshot && usb_is_open()) { - #ifdef GRAYMODEOK // This is a trick, if GRAYMODEOK is defined then we make the code accessible + #ifdef GRAYMODEOK // This is a trick, if GRAYMODEOK is defined then we make the code accessible if (dgray_enabled()) usb_fxlink_screenshot_gray(false); @@ -93,12 +49,12 @@ static void get_inputs( void ) #endif - usb_fxlink_screenshot(false); // else we just let the usual screeshot function - screenshot = false; + usb_fxlink_screenshot(false); // else we just let the usual screeshot function + game.screenshot = false; } - if (record && usb_is_open()) { + if (game.record && usb_is_open()) { #ifdef GRAYMODEOK @@ -114,7 +70,20 @@ static void get_inputs( void ) #endif +/* Timer callback */ + +int update_time(void) { + game.frame_duration++; + return TIMER_CONTINUE; +} + int main(void) { + int timer; + timer = timer_configure(TIMER_TMU, 1000, GINT_CALL(update_time)); + if(timer < 0){ + return -1; + } + timer_start(timer); #if USB_FEATURE==1 usb_interface_t const *interfaces[] = {&usb_ff_bulk, NULL}; @@ -125,12 +94,11 @@ int main(void) { /* start grayscale engine */ #ifdef GRAYMODEOK - dgray( DGRAY_ON ); + dgray(DGRAY_ON); #endif - do - { + do{ /* clear screen */ dclear(C_WHITE); @@ -149,16 +117,18 @@ int main(void) { #endif /* Management of the inputs */ - get_inputs(); - - } - while (exittoOS == false); // want to exit ? + get_inputs(&game); + /* Run the game at max. 50fps */ + while(game.frame_duration < 20) sleep(); + /* Reset frame_duration for the next frame */ + game.frame_duration = 0; + }while(!game.exittoOS); // want to exit ? /* shutdown grayengine*/ #ifdef GRAYMODEOK - dgray( DGRAY_OFF ); + dgray(DGRAY_OFF); #endif @@ -167,7 +137,7 @@ int main(void) { usb_close(); #endif - + timer_stop(timer); return 1; } diff --git a/src/map.c b/src/map.c index 840dcea..0e86663 100644 --- a/src/map.c +++ b/src/map.c @@ -15,7 +15,7 @@ void render_map(Player *player, Map *map_level) { unsigned char mx, my; /* dw and dh contain the amount of tiles that will be drawn on x and on * y. */ - unsigned char dw = DWIDTH/T_WIDTH+1, dh = DHEIGHT/T_HEIGHT+1; + unsigned char dw = DWIDTH/T_WIDTH+2, dh = DHEIGHT/T_HEIGHT+1; /* mw and mh will contain the height and the width of the map. */ unsigned short int mw = map_level->w*T_WIDTH, mh = map_level->h*T_HEIGHT; /* tile contains the tile to draw. */ @@ -87,15 +87,6 @@ void render_map(Player *player, Map *map_level) { } } -/* short int get_tile_at_pos(Map *map_level, int x, int y, int l) { - unsigned short int xtile = x/T_WIDTH; - unsigned short int xtile = x/T_HEIGHT; - unsigned short int map_w = map_level->w*T_WIDTH; - unsigned short int map_h = map_level->h*T_HEIGHT; - return x>=0 && x < map_w && y>=0 && y < map_h ? - map_level->layers[l][ytile * map_level->w + xtile] : MAP_OUTSIDE; -} */ - short int get_tile(Map *map_level, int x, int y, int l) { /* Get the tile at (x, y) on layer l. Returns the tile ID or MAP_OUTSIDE if * she's not found. */ diff --git a/src/player.c b/src/player.c index eadf11f..6d0627d 100644 --- a/src/player.c +++ b/src/player.c @@ -2,17 +2,18 @@ #include "map.h" #include -/* (Mibi88) TODO: Upscale the player for the CG50. */ -/* The player should not be bigger than a tile because it may cause problems - * with the collisions. If it's a problem please ask me (Mibi88) to adapt that. - */ -#define P_WIDTH 8 -#define P_HEIGHT 8 +#ifdef FXCG50 + #define P_WIDTH 16 + #define P_HEIGHT 16 +#else + #define P_WIDTH 8 + #define P_HEIGHT 8 +#endif /* SPEED should NOT be 8 or bigger: it this may cause bugs when handling * collisions! */ #ifdef FXCG50 - #define SPEED 3 + #define SPEED 2 #else #define SPEED 1 #endif @@ -45,7 +46,7 @@ void player_move(Map *map_level, Player *player, Direction direction) { /* If the player will collide with a hard tile. */ if(player_collision(map_level, player, direction)){ /* I fix his position so he won't be partially in the tile. */ - player_fix_position(player, dx, dy); + player_fix_position(player, 1, 1); }else{ /* If he won't collide I just move him normally */ player->x += dx;