Merge pull request 'Better map displaying :)' (#7) from mibi88/Collab_RPG-map-displaying:master into master

Reviewed-on: https://gitea.planet-casio.com/Slyvtt/Collab_RPG/pulls/7

This is now merged into master.

Thanks Mb88
This commit is contained in:
Sylvain PILLOT 2023-07-07 17:04:41 +02:00
commit 78d45a1e78
9 changed files with 233 additions and 169 deletions

View file

@ -43,6 +43,7 @@ set(ASSETS
) )
set(ASSETS_fx set(ASSETS_fx
assets-fx/levels/level0.json assets-fx/levels/level0.json
assets-fx/demo_player.png
# ... # ...
) )

BIN
assets-fx/demo_player.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 105 B

View file

@ -1,3 +1,6 @@
example.png: example.png:
type: bopti-image type: bopti-image
name: img_example name: img_example
demo_player.png:
type: bopti-image
name: demo_player_img

View file

@ -5,68 +5,60 @@
#define USB_FEATURE 1 #define USB_FEATURE 1
#if USB_FEATURE==1 #if USB_FEATURE==1
#include <gint/usb-ff-bulk.h> #include <gint/usb-ff-bulk.h>
#include <gint/usb.h> #include <gint/usb.h>
#endif //USB_FEATURE #endif //USB_FEATURE
#ifdef COLOR2BIT #ifdef COLOR2BIT
#include <gint/gray.h> #include <gint/gray.h>
#endif //COLOR2BIT #endif //COLOR2BIT
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include "map.h" #include "map.h"
#include "player.h" #include "player.h"
#include "mapdata.h"
/* Player data (defined in "player.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 */ /* some global variables */
bool exittoOS = false; // set to true when asked for exit 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 screenshot = false; // set to true when screenshot is required
bool record = false; // set to true when
/* Key management */ /* Key management */
static void get_inputs( void ) static void get_inputs( void )
{ {
key_event_t ev; 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; 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_LEFT)) PlayerLeft();
if(keydown(KEY_RIGHT)) PlayerRight(); if(keydown(KEY_RIGHT)) PlayerRight();
if(keydown(KEY_UP)) PlayerUp(); if(keydown(KEY_UP)) PlayerUp();
if(keydown(KEY_DOWN)) PlayerDown(); if(keydown(KEY_DOWN)) PlayerDown();
if(keydown(KEY_SHIFT)) PlayerAction(); if(keydown(KEY_SHIFT)) PlayerAction();
/* if USB is enabled - keybinding for screencapture */ /* if USB is enabled - keybinding for screencapture */
#if USB_FEATURE==1 #if USB_FEATURE==1
if(keydown(KEY_7)) screenshot = true; if(keydown(KEY_7)) screenshot = true;
if(keydown(KEY_8)) record = !record; if(keydown(KEY_8)) record = !record;
#endif //USB_FEATURE #endif //USB_FEATURE
} }
@ -76,88 +68,86 @@ static void get_inputs( void )
#if USB_FEATURE==1 #if USB_FEATURE==1
void USB_feature( void ) void USB_feature( void )
{ {
if (screenshot && usb_is_open()) { if (screenshot && usb_is_open()) {
if (!dgray_enabled()) if (!dgray_enabled())
usb_fxlink_screenshot(false); usb_fxlink_screenshot(false);
else else
usb_fxlink_screenshot_gray(false); usb_fxlink_screenshot_gray(false);
screenshot = false; screenshot = false;
} }
if (record && usb_is_open()) { if (record && usb_is_open()) {
if (!dgray_enabled()) if (!dgray_enabled())
usb_fxlink_videocapture(false); usb_fxlink_videocapture(false);
else else
usb_fxlink_videocapture_gray(false); usb_fxlink_videocapture_gray(false);
} }
} }
#endif #endif
void GameLogic( void ) void GameLogic(void) {
{
} }
int main(void) int main(void) {
{
#if USB_FEATURE==1 #if USB_FEATURE==1
usb_interface_t const *interfaces[] = {&usb_ff_bulk, NULL}; usb_interface_t const *interfaces[] = {&usb_ff_bulk, NULL};
usb_open(interfaces, GINT_CALL_NULL); usb_open(interfaces, GINT_CALL_NULL);
#endif #endif
/* start grayscale engine */ /* start grayscale engine */
#ifdef COLOR2BIT #ifdef COLOR2BIT
dgray( DGRAY_ON ); dgray( DGRAY_ON );
#endif #endif
do
{
/* clear screen */
dclear(C_WHITE);
/* render the map */ do
RenderMap(); {
/* clear screen */
dclear(C_WHITE);
/* start the logic of the game */ /* render the map */
GameLogic(); RenderMap(&MyPlayer, map_level);
PlayerDraw();
/* Screen blit */ /* start the logic of the game */
dupdate(); GameLogic();
/* Screen capture feature if enabled */ /* Screen blit */
#if USB_FEATURE==1 dupdate();
USB_feature();
#endif
/* Management of the inputs */ /* Screen capture feature if enabled */
get_inputs(); #if USB_FEATURE==1
USB_feature();
#endif
} /* Management of the inputs */
while (exittoOS == false); // want to exit ? get_inputs();
}
while (exittoOS == false); // want to exit ?
/* shutdown grayengine*/ /* shutdown grayengine*/
#ifdef COLOR2BIT #ifdef COLOR2BIT
dgray( DGRAY_OFF ); dgray( DGRAY_OFF );
#endif #endif
/* close USB */ /* close USB */
#if USB_FEATURE==1 #if USB_FEATURE==1
usb_close(); usb_close();
#endif #endif
return 1; return 1;
} }

117
src/map.c
View file

@ -1,38 +1,89 @@
#include "map.h" #include "map.h"
#include <gint/display.h> #include <gint/display.h>
extern struct Map map_level0; void RenderMap(Player *player, Map *map_level) {
/* for all Layer (2 in the current configuration: Background is layer 0 and
struct Map *map_level = &map_level0; * foreground is layer 1 ) */
/* x and y will contain the position in the loop. */
void RenderMap( void ) unsigned char x, y;
{ /* The positions where we start drawing the tiles will be in tx and
/* for all Layer (2 in the current configuration: Background is layer 0 and foreground is layer 1 ) */ * ty. */
for (int u = 0; u < map_level->nblayers; u++) unsigned short int tx, ty;
{ /* mx and my will contain how many pixels will be hidden on x and on
/* for each colum */ * y. */
for (int i = 0; i < map_level->w; i++) unsigned char mx, my;
{ /* dw and dh contain the amount of tiles that will be drawn on x and on
/* and each line */ * y. */
for (int j = 0; j < map_level->h; j++) 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. */
/* compute the index */ unsigned short int mw = map_level->w*T_WIDTH, mh = map_level->h*T_HEIGHT;
uint16_t index = j * map_level->w + i; /* tile contains the tile to draw. */
short int tile;
/* get the tile index from the map*/ /* The position where I start drawing */
int16_t currentTile = map_level->layers[u][index]; unsigned short int sx, sy;
/* The position of the tile in the tileset. */
/* currentTile == -1 means nothing to be draw */ unsigned short int xtile, ytile;
if (currentTile != -1) /* The layer we're drawing */
{ unsigned char l;
/* get x and y position in the tileset image */ /* Fix sx. */
uint16_t xtile = (currentTile % map_level->tileset_size) * 8; if(player->x<DWIDTH/2){
uint16_t ytile = (currentTile / map_level->tileset_size) * 8; /* If I can't center the player because I'm near the left border of
* the map. */
/* render */ player->px = player->x;
dsubimage(i * 8, j * 8, map_level->tileset, xtile, ytile, 8, 8, DIMAGE_NONE ); 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->y<DHEIGHT/2){
/* If I can't center the player because I'm near the top border of
* the map. */
player->py = 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<dh;y++){
for(x=0;x<dw;x++){
/* I get the tile number if his position is inside the map. Then
* I draw it. */
if(tx+x>=0 && tx+x < map_level->w &&
ty+y>=0 && ty+y < map_level->h){
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 */
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,
DIMAGE_NONE);
}
}
}
}
} }
}
} }

View file

@ -1,26 +1,27 @@
#ifndef MAP_H #ifndef MAP_H
#define MAP_H #define MAP_H
#define T_HEIGHT 8
#define T_WIDTH 8
#include <gint/display.h> #include <gint/display.h>
#include "player.h"
void RenderMap(void); typedef struct {
/*width, height and the number of layer of the map*/
int w, h, nblayers;
struct Map { /*the tileset to use*/
bopti_image_t *tileset;
/*width, height and the number of layer of the map*/ int tileset_size;
int w, h, nblayers;
/*the tileset to use*/
bopti_image_t *tileset;
int tileset_size;
/*list of all the tiles*/
short *layers[];
};
/*list of all the tiles*/
short *layers[];
} Map;
void RenderMap(Player *player, Map *map_level);
#endif #endif

7
src/mapdata.h Normal file
View file

@ -0,0 +1,7 @@
#ifndef MAPDATA_H
#define MAPDATA_H
extern Map map_level0;
#endif

View file

@ -1,30 +1,42 @@
#include "player.h" #include "player.h"
#include "map.h" #include "map.h"
#include <gint/display.h>
extern struct player; #define P_WIDTH 8
extern struct Map *map_level; #define P_HEIGHT 8
void PlayerLeft( void ) 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 PlayerRight( void ) void PlayerLeft(void) {
{ if(MyPlayer.x > 0){
MyPlayer.x--;
}
} }
void PlayerUp( void ) void PlayerRight(void) {
{ if(MyPlayer.x < map_level->w * T_WIDTH){
MyPlayer.x++;
}
} }
void PlayerDown( void ) void PlayerUp(void) {
{ if(MyPlayer.y > 0){
MyPlayer.y--;
}
} }
void PlayerAction( void ) void PlayerDown(void) {
{ if(MyPlayer.y < map_level->h * T_HEIGHT){
MyPlayer.y++;
}
}
} void PlayerAction(void) {
}

View file

@ -1,27 +1,26 @@
#ifndef PLAYER_H #ifndef PLAYER_H
#define PLAYER_H #define PLAYER_H
#include <stdint.h>
/* Struct that define player parameters */ /* Struct that define player parameters */
struct Player typedef struct {
{ unsigned short int x, y; /* The position of the player */
uint16_t x, y; unsigned char px, py; /* The position of the player on screen */
uint16_t life; 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);
void PlayerRight(void);
void PlayerUp(void);
void PlayerDown(void);
void PlayerAction(void);
#endif
void PlayerLeft( void );
void PlayerRight( void );
void PlayerUp( void );
void PlayerDown( void );
void PlayerAction( void );
#endif