From 8f2be6b456c1411ae799672a2d291bc500754630 Mon Sep 17 00:00:00 2001 From: attilavs2 Date: Sat, 5 Oct 2024 00:56:13 +0200 Subject: [PATCH] Refactorisation --- CMakeLists.txt | 6 +- assets-fx/fxconv-metadata.txt | 3 + assets-fx/sprite.png | Bin 0 -> 699 bytes src/config.h | 16 +++ src/fixed.h | 12 +- src/game.h | 25 ++++ src/main.c | 37 +++--- src/map.h | 10 +- src/moteur.c | 207 +++++++++++++--------------------- src/moteur.h | 23 +--- src/sprites.c | 1 + src/sprites.h | 12 ++ src/utils.h | 9 ++ 13 files changed, 187 insertions(+), 174 deletions(-) create mode 100644 assets-fx/sprite.png create mode 100644 src/config.h create mode 100644 src/game.h create mode 100644 src/sprites.c create mode 100644 src/sprites.h create mode 100644 src/utils.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d51f140..c90a9ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,10 +19,11 @@ set(SOURCES src/main.c src/moteur.c src/map.c -# src/opti.S + src/sprites.c ) set(ASSETS + assets-fx/sprite.png assets-fx/textures/briques0.png assets-fx/textures/buisson1.png ) @@ -30,8 +31,7 @@ set(ASSETS fxconv_declare_assets(${ASSETS} WITH_METADATA) add_executable(Copy3DEngine ${SOURCES} ${ASSETS}) -#the -I is an ugly hack because lephe won't let us use render-fx.h >:( -target_compile_options(Copy3DEngine PRIVATE -flto -Wall -Wextra -O2 -I "$ENV{HOME}/.local/share/giteapc/Lephenixnoir/gint/src/render-fx/") +target_compile_options(Copy3DEngine PRIVATE -Wall -Wextra -Ofast) target_compile_definitions(Copy3DEngine PRIVATE NAMEOFGAME="${NAMEOFGAME}" AUTHOR="${AUTHOR}") target_link_libraries(Copy3DEngine Gint::Gint) target_link_libraries(Copy3DEngine LibProf::LibProf) diff --git a/assets-fx/fxconv-metadata.txt b/assets-fx/fxconv-metadata.txt index e69de29..d6d7d33 100644 --- a/assets-fx/fxconv-metadata.txt +++ b/assets-fx/fxconv-metadata.txt @@ -0,0 +1,3 @@ +sprite.png: + type: bopti-image + name: sprite_tst diff --git a/assets-fx/sprite.png b/assets-fx/sprite.png new file mode 100644 index 0000000000000000000000000000000000000000..3dbb8a7aeea434b1f24cfd59aeb0180a201ec32a GIT binary patch literal 699 zcmV;s0!00ZP)EX>4Tx04R}tkv&MmKpe$i(~43m4i*t{h)~7BE-K;vxAeOiqYdvy3@OO2Tt|-NVP%yC~1{KKJM7RdW^td?Im-8D^DugLr1M zYH;2s4zr@H5}y-Km~=tnN3JU_zi}=&Ebz>*nNH0Uhl#~P2P+-Sil#<9O&n1*o$`f@ z%PQwB&RV&~TKD8H4CVBdWvu?YAB(MDx$RNq*zGNe%!-95AEQ9`F3_yo_V=-EH%|cnGjOH1{gnnV`$>Ae ztwoN2zHQ**x~(aDz~v4w_+-eY>_~o^LOu_?pV2pEf&N<{xaRiO+Q;bwkfyGZH^9Lm zFjk=Kb&q%VboTb|nO1*4t@Luf%54pG00006VoOIv0RI600RN!9r;`8x010qNS#tmY zE+YT{E+YYWr9XB6000McNliru=n4cDEDkj@CDH%@02y>eSad^gZEa<4bO1wgWnpw> zWFU8GbZ8()Nlj2!fese{005IoL_t(o!|jx@4!|G?M6bln|Nl@1H-lXqj7ggosSM>3 zClh_ZaX=^((yR6(0C@N3Jb|EtY_0wd>aj=(P^>B;!>;7rJ`ezHFru3Ympowg`8Ei? z*nn|6_-hju%Zdoj3IGj2_68tt0P!AMRo +#include "config.h" + typedef int32_t fixed_t; /* Standard arithmetic. */ @@ -69,11 +72,6 @@ static inline double f2double(fixed_t f) return (double)f / 65536; } -static inline double f2int(fixed_t f) -{ - return (int)f / 65536; -} - static inline fixed_t feasein(fixed_t x) { return fmul(x, x); @@ -90,6 +88,10 @@ static inline fixed_t fease(fixed_t x) } } +static inline fixed_t fixabs(fixed_t x){ + return x & 0x80000000; +} + static inline fixed_t fV2d_dotp(V2d u, V2d v){ return fmul(u.x,v.x) + fmul(u.y,v.y); } diff --git a/src/game.h b/src/game.h new file mode 100644 index 0000000..17021e9 --- /dev/null +++ b/src/game.h @@ -0,0 +1,25 @@ +// Voir README.md pour license précise, par Fcalva 2023-2024 et est sous GPLv3 + +#ifndef game__h +#define game__h + +#include "utils.h" +#include "map.h" + +typedef struct { + + V2d pos; + V2d dir; + V2d plane; + +} RcActor; + +typedef struct { + + RcActor player; + + uint8_t *current_map; + +} RcGame; + +#endif diff --git a/src/main.c b/src/main.c index 9a444e2..3549b44 100644 --- a/src/main.c +++ b/src/main.c @@ -13,9 +13,12 @@ #include "fixed.h" +//#include "utils.h" +#include "game.h" #include "gint/display-fx.h" #include "moteur.h" #include "map.h" +#include "config.h" //====== Copy3DEngine ===== // Git du moteur : https://gitea.planet-casio.com/Fcalva/Copy3DEngine @@ -35,25 +38,28 @@ extern uint8_t map_test[map_w][map_h]; extern bopti_image_t briques0; extern bopti_image_t buisson0; +extern bopti_image_t sprite_tst; char exit_game = 0; char disp_frame_time = 0; char first_frame = 0; int frame_time_timer = 1; -fixed_t posX; -fixed_t posY; -fixed_t dirX; -fixed_t dirY; -fixed_t planeX; -fixed_t planeY; +RcGame game = { + .player = { + .pos = {fix(3.1), fix(3.1)}, + .dir = {fix(0), fix(1)}, + .plane = {fix(0.66), fix(0)} + }, + .current_map = (void*)&map_test +}; int frame_time = 1; void keys_get(){ pollevent(); - move(); + move(&game); if (keydown(KEY_F1) && frame_time_timer <= 0) { if (disp_frame_time == 0) { @@ -94,8 +100,8 @@ int main(){ //Vos textures générées procéduralement - tex_index[0] = &briques0;//bopti_make(64,64,1); - tex_index[3] = &briques0;//bopti_make(64, 64,0); + tex_index[0] = &briques0; + tex_index[3] = &briques0; prof_init(); @@ -107,6 +113,7 @@ int main(){ #if debug EngineTimers timers; + //gdb_start_on_exception(); #endif while (!exit_game) { @@ -123,21 +130,22 @@ int main(){ draw_walls( #if debug - &timers + &timers, #endif + &game ); if (disp_frame_time == 1) dprint( 0, 0, C_BLACK, "%d ms", frame_time); #if debug - dprint( 1, 9, C_BLACK, "plX %d", planeX); + /*dprint( 1, 9, C_BLACK, "plX %d", planeX); dprint( 1, 18, C_BLACK, "plY %d", planeY); dprint( 1, 27, C_BLACK, "dX %d", dirX); dprint( 1, 36, C_BLACK, "dY %d", dirY); dprint( 1, 45, C_BLACK, "pX %d", posX); - dprint( 1, 54, C_BLACK, "pY %d", posY); - dprint( 1, 63, C_BLACK, "Rct %d", prof_time(timers.raycast_time)); - dprint( 1, 72, C_BLACK, "Dt %d", prof_time(timers.draw_time)); + dprint( 1, 54, C_BLACK, "pY %d", posY);*/ + dprint( 1, 9, C_BLACK, "Rct %d", prof_time(timers.raycast_time)); + dprint( 1, 18, C_BLACK, "Dt %d", prof_time(timers.draw_time)); timers.raycast_time = prof_make(); timers.draw_time = prof_make(); #endif @@ -147,6 +155,7 @@ int main(){ frame_time = (int)prof_time(frame)/1000; first_frame = 0; } + c3d_abort: dgray(DGRAY_OFF); diff --git a/src/map.h b/src/map.h index e9289b9..6ce2bba 100644 --- a/src/map.h +++ b/src/map.h @@ -1,12 +1,16 @@ // Voir README.md pour license précise, par Fcalva 2023-2024 et est sous GPLv3 #include "fixed.h" -#ifndef map_test_M -#define map_test_M +#ifndef map__h +#define map__h + +typedef struct { + +} RcMap; //! Attention à avoir *exactement* la même taille entre ces valeurs et la carte dans map.c ! #define map_w 64 #define map_h 128 -#endif /* map_test.h */ +#endif /* map__h */ diff --git a/src/moteur.c b/src/moteur.c index 31fed2d..c3db427 100644 --- a/src/moteur.c +++ b/src/moteur.c @@ -8,7 +8,6 @@ #include #include -#include "render-fx.h" #include #include @@ -18,26 +17,25 @@ #include "gint/display-fx.h" #include "moteur.h" #include "map.h" +#include "game.h" +#include "config.h" // moteur.c : // ici se trouvent tout ce qui concerne les graphismes, mouvement et collisions // // -void move() { +void move(RcGame *game) { extern int frame_time; fixed_t moveSpeed = fmul(fix(frame_time), 0x148); //frame_time * fix(carrés/seconde/1000) là carrés/seconde = 5 fixed_t rotSpeed = fmul(fix(frame_time), 0x83); //frame_time * fix(radians/seconde/1000) là radians/seconde = 2 fixed_t c_rotSpeed = fix(cos(f2float(rotSpeed))); fixed_t s_rotSpeed = fix(sin(f2float(rotSpeed))); - extern uint8_t map_test[map_w][map_h]; - extern fixed_t planeX; - extern fixed_t planeY; - extern fixed_t dirX; - extern fixed_t dirY; - extern fixed_t posX; - extern fixed_t posY; + uint8_t *map_test = game->current_map; + V2d *plane = &game->player.plane; + V2d *dir = &game->player.dir; + V2d *pos = &game->player.pos; fixed_t oldDirX; fixed_t oldPlaneX; @@ -47,132 +45,62 @@ void move() { int ytemp2; if (keydown(KEY_UP)) { - xtemp1 = f2int(posX + fmul(dirX, moveSpeed) - 0xF); - ytemp1 = f2int(posY); - xtemp2 = f2int(posX); - ytemp2 = f2int(posY + fmul(dirY, moveSpeed) - 0xF); + xtemp1 = ffloor(pos->x + fmul(dir->x, moveSpeed)); + ytemp1 = ffloor(pos->y); + xtemp2 = ffloor(pos->x); + ytemp2 = ffloor(pos->y + fmul(dir->y, moveSpeed)); - if(map_test[xtemp1][ytemp1] == 0) posX += fmul(dirX, moveSpeed); - if(map_test[xtemp2][ytemp2] == 0) posY += fmul(dirY, moveSpeed); + if(!map_test[xtemp1*map_w+ytemp1]) + pos->x += fmul(dir->x, moveSpeed); + if(!map_test[xtemp2*map_w+ytemp2]) + pos->y += fmul(dir->y, moveSpeed); } //move backwards if no wall behind you if (keydown(KEY_DOWN)) { - xtemp1 = f2int(posX - fmul(dirX, moveSpeed) + 0xF); - ytemp1 = f2int(posY); - xtemp2 = f2int(posX); - ytemp2 = f2int(posY - fmul(dirY, moveSpeed) + 0xF); + xtemp1 = ffloor(pos->x - fmul(dir->x, moveSpeed)); + ytemp1 = ffloor(pos->y); + xtemp2 = ffloor(pos->x); + ytemp2 = ffloor(pos->y - fmul(dir->y, moveSpeed)); - if(map_test[xtemp1][ytemp1] == 0) posX -= fmul(dirX, moveSpeed); - if(map_test[xtemp2][ytemp2] == 0) posY -= fmul(dirY, moveSpeed); + if(!map_test[xtemp1*map_w+ytemp1]) + pos->x -= fmul(dir->x, moveSpeed); + if(!map_test[xtemp2*map_w+ytemp2]) + pos->y -= fmul(dir->y, moveSpeed); } - //rotate to the rightdouble sin_rotspeed; + //rotate to the right if (keydown(KEY_RIGHT)) { //both camera direction and camera plane must be rotated - oldDirX = dirX; - dirX = (fmul(dirX, c_rotSpeed)+1) - (fmul(dirY, -s_rotSpeed)+1); - dirY = (fmul(oldDirX, -s_rotSpeed)+1) + (fmul(dirY, c_rotSpeed)+1); - oldPlaneX = planeX; - planeX = (fmul(planeX, c_rotSpeed)+1) - (fmul(planeY, -s_rotSpeed)+1); - planeY = (fmul(oldPlaneX, -s_rotSpeed)+1) + (fmul(planeY, c_rotSpeed)+1); + oldDirX = dir->x; + dir->x = (fmul(dir->x, c_rotSpeed)+1) - (fmul(dir->y, -s_rotSpeed)+1); + dir->y = (fmul(oldDirX, -s_rotSpeed)+1) + (fmul(dir->y, c_rotSpeed)+1); + oldPlaneX = plane->x; + plane->x = (fmul(plane->x, c_rotSpeed)+1) - (fmul(plane->y, -s_rotSpeed)+1); + plane->y = (fmul(oldPlaneX, -s_rotSpeed)+1) + (fmul(plane->y, c_rotSpeed)+1); } //rotate to the left if (keydown(KEY_LEFT)) { //both camera direction and camera plane must be rotated - oldDirX = dirX; - dirX = (fmul(dirX, c_rotSpeed)-1) - (fmul(dirY, s_rotSpeed)-1); - dirY = (fmul(oldDirX, s_rotSpeed)+1) + (fmul(dirY, c_rotSpeed)+1); - oldPlaneX = planeX; - planeX = (fmul(planeX, c_rotSpeed)-1) - (fmul(planeY, s_rotSpeed) - 1); - planeY = (fmul(oldPlaneX, s_rotSpeed)+1) + (fmul(planeY, c_rotSpeed) + 1); + oldDirX = dir->x; + dir->x = (fmul(dir->x, c_rotSpeed)-1) - (fmul(dir->y, s_rotSpeed)-1); + dir->y = (fmul(oldDirX, s_rotSpeed)+1) + (fmul(dir->y, c_rotSpeed)+1); + oldPlaneX = plane->x; + plane->x = (fmul(plane->x, c_rotSpeed)-1) - (fmul(plane->y, s_rotSpeed) - 1); + plane->y = (fmul(oldPlaneX, s_rotSpeed)+1) + (fmul(plane->y, c_rotSpeed) + 1); } - if (dirX > 0xFFFF) dirX = 0xFFFF; - if (dirY > 0xFFFF) dirY = 0xFFFF; - if (dirX < -0xFFFF) dirX = -0xFFFF; - if (dirY < -0xFFFF) dirY = -0xFFFF; -} - -void spawn_gen(){ - extern fixed_t posX; - extern fixed_t posY; - extern fixed_t dirX; - extern fixed_t dirY; - extern fixed_t planeX; - extern fixed_t planeY; - extern uint8_t map_test[map_w][map_h]; - - int seed, seed_x, seed_y; - int SeedSeed; - - time_t timeN; - time(&timeN); - SeedSeed = timeN; - - srand(SeedSeed); - - while(1){ - seed = rand() * 0.5 + rand() * 0.5; - seed_x = seed & 0b1111111; - seed_y = (seed >> 8) & 0b111111; - if (seed_x < map_w && seed_y < map_h && (seed_x < 111 && seed_y > 15)){ - if(map_test[seed_x][seed_y] == 0){ - break; - } - } - } - fixed_t start_dirX, start_dirY; - - int dirSeed = (seed >> 9) & 0b11 ; - switch(dirSeed){ - case 0: { - start_dirX = 0x0; - start_dirY = 0xFFFF; - planeX = fix(0.66); - planeY = 0; - break; - } - case 1: { - start_dirX = 0xFFFF; - start_dirY = 0x0; - planeX = 0; - planeY = fix(-0.66); - break; - } - case 2: { - start_dirX = -0xFFFF; - start_dirY = 0x0; - planeX = 0; - planeY = fix(0.66); - break; - } - case 3: { - start_dirX = 0x0; - start_dirY = -0xFFFF; - planeX = fix(-0.66); - planeY = 0; - break; - } - default : { - start_dirX = 0xFFFF; - start_dirY = 0x0; - planeX = 0; - planeY = fix(-0.66); - break; - } - } - - posX = fix(seed_x) + 0x7FFF; - posY = fix(seed_y) + 0x7FFF; //x and y start position - dirX = start_dirX; - dirY = start_dirY; //initial direction vector + if (dir->x > 0xFFFF) dir->x = 0xFFFF; + if (dir->y > 0xFFFF) dir->y = 0xFFFF; + if (dir->x < -0xFFFF) dir->x = -0xFFFF; + if (dir->y < -0xFFFF) dir->y = -0xFFFF; } void load_map(){ - spawn_gen(); + } #if asm_opti #else +#endif //Fonction fournie par le grand Lephé :D inline int __attribute__((always_inline)) get_pixel(bopti_image_t const *img, int u, int v) @@ -190,6 +118,23 @@ inline int __attribute__((always_inline)) return (dark << 1) + light; } +inline int __attribute__((always_inline)) + get_pixel_alpha(bopti_image_t const *img, int u, int v) +{ + int layers = image_layer_count(img->profile); + int columns = (img->width + 31) >> 5; + + uint32_t const *layer_ptr = (void *)img->data; + layer_ptr += (v * columns + (u >> 5)) * layers; + + uint32_t mask = 0x80000000u >> (u & 31); + + int light = (layer_ptr[0] & mask) != 0; + int dark = (layer_ptr[1] & mask) != 0; + int alpha = (layer_ptr[2] & mask) != 0; + return (alpha << 2) + (dark << 1) + light; +} + uint32_t *light, *dark; //Version simplifiée de gpixel dans gint/src/gray/gpixel.c @@ -244,7 +189,7 @@ void dscale_bopti(bopti_image_t *tex, fixed_t scale_x, fixed_t scale_y, int x, i if(screen_x < 0 || screen_x+scale_x > viewport_w) return; do{ - cust_dpixel(ffloor(oldx), ffloor(oldy), tpix); + dpixel(ffloor(oldx), ffloor(oldy), tpix); oldx += 0xFFFF; } while(oldx < screen_x+scale_x); screen_x += scale_x; @@ -274,21 +219,21 @@ inline void __attribute__((always_inline)) screenPos += texSize; } } -#endif void draw_walls( #if debug - EngineTimers *timers + EngineTimers *timers, #endif + RcGame *game ){ - extern fixed_t posX; - extern fixed_t posY; - extern fixed_t dirX; - extern fixed_t dirY; - extern fixed_t planeX; - extern fixed_t planeY; + fixed_t posX = game->player.pos.x; + fixed_t posY = game->player.pos.y; + fixed_t dirX = game->player.dir.x; + fixed_t dirY = game->player.dir.y; + fixed_t planeX = game->player.plane.x; + fixed_t planeY = game->player.plane.y; extern bopti_image_t *tex_index[TINDEX_S]; - extern uint8_t map_test[map_w][map_h]; + uint8_t *map_test = game->current_map; fixed_t cameraX; fixed_t rayDirX; @@ -331,8 +276,8 @@ void draw_walls( //which box of the map we're in - mapX = f2int(posX); - mapY = f2int(posY); + mapX = ffloor(posX); + mapY = ffloor(posY); // length of ray from one x or y-side to next x or y-side // these are derived as: @@ -378,11 +323,11 @@ void draw_walls( //perform DDA while(true) { //Check if the ray is out of range/bounds - if (sideDistX >= max_dist || sideDistY >= max_dist || mapX < 0 || mapY < 0 || mapX >= map_w || mapY >= map_h) { + if (sideDistX >= max_dist || sideDistY >= max_dist || mapX < 0 || mapY < 0 || mapY >= map_w || mapX >= map_h) { break; } //Otherwise check if ray has hit a wall - if (map_test[mapX][mapY] > 0) { + if (map_test[mapX*map_w+mapY] > 0) { break; } //jump to next map square, either in x-direction, or in y-direction @@ -418,7 +363,7 @@ void draw_walls( fixed_t wallX; //where exactly the wall was hit if (side == 0) wallX = posY + fmul(perpWallDist, rayDirY); else wallX = posX + fmul(perpWallDist, rayDirX); - wallX -= fix(floor(f2int(wallX))); + wallX -= fix(ffloor(wallX)); //x coordinate on the texture texX = fmul(wallX, TSIZE); @@ -427,7 +372,7 @@ void draw_walls( if(side == 0 && rayDirX > 0) texX = 64 - texX - 1; if(side == 1 && rayDirY < 0) texX = 64 - texX - 1; - lineHeight = f2int(fdiv(fix(viewport_h), perpWallDist)); //Taille en px de la ligne + lineHeight = ffloor(fdiv(fix(viewport_h), perpWallDist)); //Taille en px de la ligne if (lineHeight < 1) lineHeight = 1; //if (lineHeight > viewport_h) lineHeight = viewport_h - 1; @@ -444,7 +389,7 @@ void draw_walls( int linePos = viewport_h/2 - lineHeight/2; - bopti_image_t *tex = tex_index[map_test[mapX][mapY]]; + bopti_image_t *tex = tex_index[map_test[mapX*map_w+mapY]]; draw_stripe(tex, texSampleY, linePos, texSize, texX, x); diff --git a/src/moteur.h b/src/moteur.h index e02d1c2..9d85339 100644 --- a/src/moteur.h +++ b/src/moteur.h @@ -6,21 +6,8 @@ #include #include #include "fixed.h" - -//param. graphiques -#define screen_w 128 -#define screen_h 64 -#define viewport_w 128 -#define viewport_h 64 -#define max_dist fix(32) - -#define TSIZE 32 - -#define TINDEX_S 16 - -#define debug 0 //pour afficher les infos de debug - -#define asm_opti 0 +#include "game.h" +#include "config.h" typedef struct { @@ -36,12 +23,12 @@ void draw_stripe(bopti_image_t *tex, int texSampleY, int linePos, fixed_t texSize, int texX, int x); void load_map(); -void end_screen(); void draw_walls( #if debug - EngineTimers *timers + EngineTimers *timers, #endif + RcGame *game ); -void move(); +void move(RcGame *game); #endif /* moteur */ diff --git a/src/sprites.c b/src/sprites.c new file mode 100644 index 0000000..6dc7649 --- /dev/null +++ b/src/sprites.c @@ -0,0 +1 @@ +// Voir README.md pour license précise, par Fcalva 2023-2024 et est sous GPLv3 diff --git a/src/sprites.h b/src/sprites.h new file mode 100644 index 0000000..16fe029 --- /dev/null +++ b/src/sprites.h @@ -0,0 +1,12 @@ +// Voir README.md pour license précise, par Fcalva 2023-2024 et est sous GPLv3 + +#ifndef sprites__h +#define sprites__h + +#include "utils.h" + +#include + +//void project_sprite(bopti_image_t *tex, V2d pos, ) + +#endif diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..ebf556e --- /dev/null +++ b/src/utils.h @@ -0,0 +1,9 @@ +// Voir README.md pour license précise, par Fcalva 2023-2024 et est sous GPLv3 + +#ifndef utils__h +#define utils__h + +#include + + +#endif