From 9818d835bee0730b24b934fbee7c3d0db270e6d7 Mon Sep 17 00:00:00 2001 From: mibi88 <76903855+mibi88@users.noreply.github.com> Date: Fri, 7 Jul 2023 14:20:13 +0200 Subject: [PATCH 1/2] Restructured some stuff, now the map is scrolling :), but some optimizations are needed! --- CMakeLists.txt | 1 + assets-fx/demo_player.png | Bin 0 -> 105 bytes assets-fx/fxconv-metadata.txt | 3 + src/main.c | 156 ++++++++++++++++------------------ src/map.c | 116 ++++++++++++++++++------- src/map.h | 25 +++--- src/mapdata.h | 7 ++ src/player.c | 22 +++-- src/player.h | 16 ++-- 9 files changed, 207 insertions(+), 139 deletions(-) create mode 100644 assets-fx/demo_player.png create mode 100644 src/mapdata.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 645fa89..fbf1be8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,6 +43,7 @@ set(ASSETS ) set(ASSETS_fx assets-fx/levels/level0.json + assets-fx/demo_player.png # ... ) diff --git a/assets-fx/demo_player.png b/assets-fx/demo_player.png new file mode 100644 index 0000000000000000000000000000000000000000..e522e612611bb1d9bae1b69e51e570bc385878f4 GIT binary patch literal 105 zcmeAS@N?(olHy`uVBq!ia0vp^93afW0wnX;%77$;fv1aO2*=Filpp6Cm?9(sTqGoC zD2OzrBqcPg{Mj$y5O$DhVsgvsghYoRzQFc0T?Q#d4&e(cueX9Ud%F6$taD0e0sz1l B8@2!d literal 0 HcmV?d00001 diff --git a/assets-fx/fxconv-metadata.txt b/assets-fx/fxconv-metadata.txt index d435d5f..b654bbd 100644 --- a/assets-fx/fxconv-metadata.txt +++ b/assets-fx/fxconv-metadata.txt @@ -1,3 +1,6 @@ example.png: type: bopti-image name: img_example +demo_player.png: + type: bopti-image + name: demo_player_img diff --git a/src/main.c b/src/main.c index 3a6fae8..ce3b384 100644 --- a/src/main.c +++ b/src/main.c @@ -5,68 +5,60 @@ #define USB_FEATURE 1 #if USB_FEATURE==1 - #include - #include + #include + #include #endif //USB_FEATURE #ifdef COLOR2BIT - #include + #include #endif //COLOR2BIT - - - - #include #include #include "map.h" #include "player.h" - - +#include "mapdata.h" /* Player data (defined in "player.h")*/ -struct Player MyPlayer = { 10, 5, 100 }; - +Player MyPlayer = { 10, 5, 0, 0, 100 }; +Map *map_level = &map_level0; /* 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 - +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 /* Key management */ static void get_inputs( void ) { key_event_t ev; - while((ev = pollevent()).type != KEYEV_NONE) - { + while((ev = pollevent()).type != KEYEV_NONE){ } - /* Key binding for the Player action */ - /*************************************/ - + /* Key binding for the Player action */ + + /*************************************/ if(keydown(KEY_EXIT)) exittoOS = true; - /* Player actions - Prototypes in player.h and implementation in player.c */ + /* 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_RIGHT)) PlayerRight(); + if(keydown(KEY_UP)) PlayerUp(); + if(keydown(KEY_DOWN)) PlayerDown(); + if(keydown(KEY_SHIFT)) PlayerAction(); - /* if USB is enabled - keybinding for screencapture */ + /* if USB is enabled - keybinding for screencapture */ #if USB_FEATURE==1 - if(keydown(KEY_7)) screenshot = true; - if(keydown(KEY_8)) record = !record; + if(keydown(KEY_7)) screenshot = true; + if(keydown(KEY_8)) record = !record; #endif //USB_FEATURE } @@ -76,23 +68,23 @@ static void get_inputs( void ) #if USB_FEATURE==1 - void USB_feature( void ) - { - if (screenshot && usb_is_open()) { - if (!dgray_enabled()) - usb_fxlink_screenshot(false); - else - usb_fxlink_screenshot_gray(false); - screenshot = false; - } + void USB_feature( void ) + { + if (screenshot && usb_is_open()) { + if (!dgray_enabled()) + usb_fxlink_screenshot(false); + else + usb_fxlink_screenshot_gray(false); + screenshot = false; + } - if (record && usb_is_open()) { - if (!dgray_enabled()) - usb_fxlink_videocapture(false); - else - usb_fxlink_videocapture_gray(false); - } - } + if (record && usb_is_open()) { + if (!dgray_enabled()) + usb_fxlink_videocapture(false); + else + usb_fxlink_videocapture_gray(false); + } + } #endif @@ -100,64 +92,64 @@ static void get_inputs( void ) void GameLogic( void ) { - } int main(void) { - #if USB_FEATURE==1 - usb_interface_t const *interfaces[] = {&usb_ff_bulk, NULL}; - usb_open(interfaces, GINT_CALL_NULL); - #endif + #if USB_FEATURE==1 + usb_interface_t const *interfaces[] = {&usb_ff_bulk, NULL}; + usb_open(interfaces, GINT_CALL_NULL); + #endif - /* start grayscale engine */ + /* start grayscale engine */ - #ifdef COLOR2BIT - dgray( DGRAY_ON ); - #endif - + #ifdef COLOR2BIT + dgray( DGRAY_ON ); + #endif - do - { - /* clear screen */ - dclear(C_WHITE); - /* render the map */ - RenderMap(); + do + { + /* clear screen */ + dclear(C_WHITE); - /* start the logic of the game */ - GameLogic(); + /* render the map */ + RenderMap(&MyPlayer, map_level); + PlayerDraw(); - /* Screen blit */ - dupdate(); + /* start the logic of the game */ + GameLogic(); - /* Screen capture feature if enabled */ - #if USB_FEATURE==1 - USB_feature(); - #endif + /* Screen blit */ + dupdate(); - /* Management of the inputs */ - get_inputs(); + /* Screen capture feature if enabled */ + #if USB_FEATURE==1 + USB_feature(); + #endif - } - while (exittoOS == false); // want to exit ? + /* Management of the inputs */ + get_inputs(); + + } + while (exittoOS == false); // want to exit ? - /* shutdown grayengine*/ - #ifdef COLOR2BIT - dgray( DGRAY_OFF ); - #endif + /* shutdown grayengine*/ + #ifdef COLOR2BIT + dgray( DGRAY_OFF ); + #endif - /* close USB */ - #if USB_FEATURE==1 - usb_close(); - #endif + /* close USB */ + #if USB_FEATURE==1 + usb_close(); + #endif - return 1; + return 1; } diff --git a/src/map.c b/src/map.c index 0f184d1..2cd990f 100644 --- a/src/map.c +++ b/src/map.c @@ -1,38 +1,92 @@ #include "map.h" + #include -extern struct Map map_level0; +#define T_HEIGHT 8 +#define T_WIDTH 8 -struct Map *map_level = &map_level0; - -void RenderMap( void ) -{ - /* for all Layer (2 in the current configuration: Background is layer 0 and foreground is layer 1 ) */ - for (int u = 0; u < map_level->nblayers; u++) - { - /* for each colum */ - for (int i = 0; i < map_level->w; i++) - { - /* and each line */ - for (int j = 0; j < map_level->h; j++) - { - /* compute the index */ - uint16_t index = j * map_level->w + i; - - /* get the tile index from the map*/ - int16_t currentTile = map_level->layers[u][index]; - - /* currentTile == -1 means nothing to be draw */ - if (currentTile != -1) - { - /* get x and y position in the tileset image */ - uint16_t xtile = (currentTile % map_level->tileset_size) * 8; - uint16_t ytile = (currentTile / map_level->tileset_size) * 8; - - /* render */ - dsubimage(i * 8, j * 8, map_level->tileset, xtile, ytile, 8, 8, DIMAGE_NONE ); +void RenderMap(Player *player, Map *map_level) { + /* for all Layer (2 in the current configuration: Background is layer 0 and + * foreground is layer 1 ) */ + for (int u = 0; u < map_level->nblayers; u++){ + /* Draws a layer of the map on screen. */ + /* x and y will contain the position in the loop. */ + int x, y; + /* The positions where we start drawing the tiles will be in tx and + * ty. */ + int tx, ty; + /* mx and my will contain how many pixels will be hidden on x and on + * y. */ + int mx, my; + /* dw and dh contain the amount of tiles that will be drawn on x and on + * y. */ + int dw = DWIDTH/T_WIDTH+1, dh = DHEIGHT/T_HEIGHT+1; + /* mw and mh will contain the height and the width of the map. */ + int mw = map_level->w*T_WIDTH, mh = map_level->h*T_HEIGHT; + /* tile contains the tile to draw. */ + short int tile; + /* The position where I start drawing */ + int sx, sy; + /* Fix sx. */ + if(player->xpx = player->x; + sx = 0; + }else if(player->x+DWIDTH/2>mw){ + /* If I can't center the player because I'm near the right border of + * the map. */ + sx = mw-DWIDTH; + player->px = player->x-sx; + }else{ + /* I can center the player. */ + player->px = DWIDTH/2; + sx = player->x-player->px; + } + /* Fix sy. */ + if(player->ypy = player->y; + sy = 0; + }else if(player->y+DHEIGHT/2>mh){ + /* If I can't center the player because I'm near the bottom border + * of the map. */ + sy = mh-DHEIGHT; + player->py = player->y-sy; + }else{ + /* I can center the player. */ + player->py = DHEIGHT/2; + sy = player->y-player->py; + } + tx = sx/T_WIDTH; + ty = sy/T_HEIGHT; + mx = sx-tx*T_WIDTH; + my = sy-ty*T_HEIGHT; + for(y=0;y=0 && tx+x < map_level->w && + ty+y>=0 && ty+y < map_level->h){ + tile = map_level->layers[u][(y+ty) * map_level->w + tx+x]; + /* tile == -1 means nothing to be draw */ + if(tile >= 0){ + /* get x and y position in the tileset image */ + unsigned short int xtile = (tile % + map_level->tileset_size) * + T_WIDTH; + unsigned short int ytile = (tile / + map_level->tileset_size) * + T_HEIGHT; + /* render */ + dsubimage(x*T_WIDTH-mx, y*T_HEIGHT-my, + map_level->tileset, xtile, ytile, 8, 8, + DIMAGE_NONE); + } + } + } } - } } - } } + diff --git a/src/map.h b/src/map.h index 2a0332e..dbcb78e 100644 --- a/src/map.h +++ b/src/map.h @@ -4,23 +4,22 @@ #include +#include "player.h" -void RenderMap(void); +typedef struct { + /*width, height and the number of layer of the map*/ + int w, h, nblayers; -struct Map { - - /*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[]; -}; + /*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); #endif + diff --git a/src/mapdata.h b/src/mapdata.h new file mode 100644 index 0000000..5af7705 --- /dev/null +++ b/src/mapdata.h @@ -0,0 +1,7 @@ +#ifndef MAPDATA_H +#define MAPDATA_H + +extern Map map_level0; + +#endif + diff --git a/src/player.c b/src/player.c index cff022c..d7d088c 100644 --- a/src/player.c +++ b/src/player.c @@ -1,30 +1,40 @@ #include "player.h" #include "map.h" +#include -extern struct player; +#define P_WIDTH 16 +#define P_HEIGHT 16 + +extern Player MyPlayer; extern struct 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 PlayerLeft( void ) { - + MyPlayer.x--; } void PlayerRight( void ) { - + MyPlayer.x++; } void PlayerUp( void ) { - + MyPlayer.y--; } void PlayerDown( void ) { - + MyPlayer.y++; } void PlayerAction( void ) { -} \ No newline at end of file +} diff --git a/src/player.h b/src/player.h index a9a3f49..87784a6 100644 --- a/src/player.h +++ b/src/player.h @@ -5,13 +5,15 @@ /* Struct that define player parameters */ -struct Player -{ - uint16_t x, y; - uint16_t life; -}; - +typedef struct { + unsigned short int x, y; /* The position of the player */ + short 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. */ +} Player; +/* This function should be called after drawing the map ! */ +void PlayerDraw( void ); void PlayerLeft( void ); @@ -24,4 +26,4 @@ void PlayerDown( void ); void PlayerAction( void ); -#endif \ No newline at end of file +#endif From d3f1e554227a1a142af42c81dde252b04a5595f5 Mon Sep 17 00:00:00 2001 From: mibi88 <76903855+mibi88@users.noreply.github.com> Date: Fri, 7 Jul 2023 14:50:30 +0200 Subject: [PATCH 2/2] The map displaying is now SUPER fast --- src/main.c | 6 +-- src/map.c | 127 +++++++++++++++++++++++++-------------------------- src/map.h | 2 + src/player.c | 40 ++++++++-------- src/player.h | 17 +++---- 5 files changed, 94 insertions(+), 98 deletions(-) diff --git a/src/main.c b/src/main.c index ce3b384..172f9c1 100644 --- a/src/main.c +++ b/src/main.c @@ -89,14 +89,12 @@ static void get_inputs( void ) #endif -void GameLogic( void ) -{ +void GameLogic(void) { } -int main(void) -{ +int main(void) { #if USB_FEATURE==1 usb_interface_t const *interfaces[] = {&usb_ff_bulk, NULL}; diff --git a/src/map.c b/src/map.c index 2cd990f..71b010d 100644 --- a/src/map.c +++ b/src/map.c @@ -2,83 +2,80 @@ #include -#define T_HEIGHT 8 -#define T_WIDTH 8 - void RenderMap(Player *player, Map *map_level) { /* for all Layer (2 in the current configuration: Background is layer 0 and * foreground is layer 1 ) */ - for (int u = 0; u < map_level->nblayers; u++){ - /* Draws a layer of the map on screen. */ - /* x and y will contain the position in the loop. */ - int x, y; - /* The positions where we start drawing the tiles will be in tx and - * ty. */ - int tx, ty; - /* mx and my will contain how many pixels will be hidden on x and on - * y. */ - int mx, my; - /* dw and dh contain the amount of tiles that will be drawn on x and on - * y. */ - int dw = DWIDTH/T_WIDTH+1, dh = DHEIGHT/T_HEIGHT+1; - /* mw and mh will contain the height and the width of the map. */ - int mw = map_level->w*T_WIDTH, mh = map_level->h*T_HEIGHT; - /* tile contains the tile to draw. */ - short int tile; - /* The position where I start drawing */ - int sx, sy; - /* Fix sx. */ - if(player->xpx = player->x; - sx = 0; - }else if(player->x+DWIDTH/2>mw){ - /* If I can't center the player because I'm near the right border of - * the map. */ - sx = mw-DWIDTH; - player->px = player->x-sx; - }else{ - /* I can center the player. */ - player->px = DWIDTH/2; - sx = player->x-player->px; - } - /* Fix sy. */ - if(player->ypy = player->y; - sy = 0; - }else if(player->y+DHEIGHT/2>mh){ - /* If I can't center the player because I'm near the bottom border - * of the map. */ - sy = mh-DHEIGHT; - player->py = player->y-sy; - }else{ - /* I can center the player. */ - player->py = DHEIGHT/2; - sy = player->y-player->py; - } - tx = sx/T_WIDTH; - ty = sy/T_HEIGHT; - mx = sx-tx*T_WIDTH; - my = sy-ty*T_HEIGHT; + /* x and y will contain the position in the loop. */ + unsigned char x, y; + /* The positions where we start drawing the tiles will be in tx and + * ty. */ + unsigned short int tx, ty; + /* mx and my will contain how many pixels will be hidden on x and on + * y. */ + 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; + /* 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. */ + short int tile; + /* The position where I start drawing */ + unsigned short int sx, sy; + /* The position of the tile in the tileset. */ + unsigned short int xtile, ytile; + /* The layer we're drawing */ + unsigned char l; + /* Fix sx. */ + if(player->xpx = player->x; + sx = 0; + }else if(player->x+DWIDTH/2>mw){ + /* If I can't center the player because I'm near the right border of + * the map. */ + sx = mw-DWIDTH; + player->px = player->x-sx; + }else{ + /* I can center the player. */ + player->px = DWIDTH/2; + sx = player->x-player->px; + } + /* Fix sy. */ + if(player->ypy = player->y; + sy = 0; + }else if(player->y+DHEIGHT/2>mh){ + /* If I can't center the player because I'm near the bottom border + * of the map. */ + sy = mh-DHEIGHT; + player->py = player->y-sy; + }else{ + /* I can center the player. */ + player->py = DHEIGHT/2; + sy = player->y-player->py; + } + tx = sx/T_WIDTH; + ty = sy/T_HEIGHT; + mx = sx-tx*T_WIDTH; + my = sy-ty*T_HEIGHT; + for (l = 0; l < map_level->nblayers; l++){ + /* Draw a layer of the map on screen. */ for(y=0;y=0 && tx+x < map_level->w && ty+y>=0 && ty+y < map_level->h){ - tile = map_level->layers[u][(y+ty) * map_level->w + tx+x]; + tile = map_level->layers[l][(y+ty) * map_level->w + tx+x]; /* tile == -1 means nothing to be draw */ if(tile >= 0){ /* get x and y position in the tileset image */ - unsigned short int xtile = (tile % - map_level->tileset_size) * - T_WIDTH; - unsigned short int ytile = (tile / - map_level->tileset_size) * - T_HEIGHT; + xtile = (tile % map_level->tileset_size) * T_WIDTH; + ytile = (tile / map_level->tileset_size) * T_HEIGHT; /* render */ dsubimage(x*T_WIDTH-mx, y*T_HEIGHT-my, map_level->tileset, xtile, ytile, 8, 8, diff --git a/src/map.h b/src/map.h index dbcb78e..a7bd5b9 100644 --- a/src/map.h +++ b/src/map.h @@ -1,6 +1,8 @@ #ifndef MAP_H #define MAP_H +#define T_HEIGHT 8 +#define T_WIDTH 8 #include diff --git a/src/player.c b/src/player.c index d7d088c..716c67c 100644 --- a/src/player.c +++ b/src/player.c @@ -2,39 +2,41 @@ #include "map.h" #include -#define P_WIDTH 16 -#define P_HEIGHT 16 +#define P_WIDTH 8 +#define P_HEIGHT 8 extern Player MyPlayer; -extern struct Map *map_level; +extern Map *map_level; extern bopti_image_t demo_player_img; -void PlayerDraw( void ) -{ +void PlayerDraw(void) { dimage(MyPlayer.px-P_WIDTH/2, MyPlayer.py-P_HEIGHT/2, &demo_player_img); } -void PlayerLeft( void ) -{ - MyPlayer.x--; +void PlayerLeft(void) { + if(MyPlayer.x > 0){ + MyPlayer.x--; + } } -void PlayerRight( void ) -{ - MyPlayer.x++; +void PlayerRight(void) { + if(MyPlayer.x < map_level->w * T_WIDTH){ + MyPlayer.x++; + } } -void PlayerUp( void ) -{ - MyPlayer.y--; +void PlayerUp(void) { + if(MyPlayer.y > 0){ + MyPlayer.y--; + } } -void PlayerDown( void ) -{ - MyPlayer.y++; +void PlayerDown(void) { + if(MyPlayer.y < map_level->h * T_HEIGHT){ + MyPlayer.y++; + } } -void PlayerAction( void ) -{ +void PlayerAction(void) { } diff --git a/src/player.h b/src/player.h index 87784a6..6277655 100644 --- a/src/player.h +++ b/src/player.h @@ -1,29 +1,26 @@ #ifndef PLAYER_H #define PLAYER_H -#include - - /* Struct that define player parameters */ typedef struct { unsigned short int x, y; /* The position of the player */ - short int px, py; /* The position of the player on screen */ + unsigned char px, py; /* The position of the player on screen */ unsigned short int life; /* How many lives the player still has between 0 * and 100. */ } Player; /* This function should be called after drawing the map ! */ -void PlayerDraw( void ); +void PlayerDraw(void); -void PlayerLeft( void ); +void PlayerLeft(void); -void PlayerRight( void ); +void PlayerRight(void); -void PlayerUp( void ); +void PlayerUp(void); -void PlayerDown( void ); +void PlayerDown(void); -void PlayerAction( void ); +void PlayerAction(void); #endif