The map displaying is now SUPER fast

This commit is contained in:
mibi88 2023-07-07 14:50:30 +02:00
parent 9818d835be
commit d3f1e55422
5 changed files with 94 additions and 98 deletions

View file

@ -89,14 +89,12 @@ static void get_inputs( void )
#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};

127
src/map.c
View file

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

View file

@ -1,6 +1,8 @@
#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>

View file

@ -2,39 +2,41 @@
#include "map.h" #include "map.h"
#include <gint/display.h> #include <gint/display.h>
#define P_WIDTH 16 #define P_WIDTH 8
#define P_HEIGHT 16 #define P_HEIGHT 8
extern Player MyPlayer; extern Player MyPlayer;
extern struct Map *map_level; extern Map *map_level;
extern bopti_image_t demo_player_img; 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); dimage(MyPlayer.px-P_WIDTH/2, MyPlayer.py-P_HEIGHT/2, &demo_player_img);
} }
void PlayerLeft( void ) void PlayerLeft(void) {
{ if(MyPlayer.x > 0){
MyPlayer.x--; MyPlayer.x--;
}
} }
void PlayerRight( void ) void PlayerRight(void) {
{ if(MyPlayer.x < map_level->w * T_WIDTH){
MyPlayer.x++; MyPlayer.x++;
}
} }
void PlayerUp( void ) void PlayerUp(void) {
{ if(MyPlayer.y > 0){
MyPlayer.y--; MyPlayer.y--;
}
} }
void PlayerDown( void ) void PlayerDown(void) {
{ if(MyPlayer.y < map_level->h * T_HEIGHT){
MyPlayer.y++; MyPlayer.y++;
}
} }
void PlayerAction( void ) void PlayerAction(void) {
{
} }

View file

@ -1,29 +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 */
typedef struct { typedef struct {
unsigned short int x, y; /* The position of the player */ 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 unsigned short int life; /* How many lives the player still has between 0
* and 100. */ * and 100. */
} Player; } Player;
/* This function should be called after drawing the map ! */ /* 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 #endif