Refactorisation

This commit is contained in:
attilavs2 2024-10-05 00:56:13 +02:00
parent e117e19744
commit 8f2be6b456
13 changed files with 187 additions and 174 deletions

View file

@ -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)

View file

@ -0,0 +1,3 @@
sprite.png:
type: bopti-image
name: sprite_tst

BIN
assets-fx/sprite.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 699 B

16
src/config.h Normal file
View file

@ -0,0 +1,16 @@
#pragma once
//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 1 //pour afficher les infos de debug
#define asm_opti 0

View file

@ -4,8 +4,11 @@
//vient de https://gitea.planet-casio.com/Slyvtt/OutRun/src/branch/master/src
//
#pragma once
#include <stdint.h>
#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);
}

25
src/game.h Normal file
View file

@ -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

View file

@ -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);

View file

@ -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 */

View file

@ -8,7 +8,6 @@
#include <gint/display.h>
#include <gint/keyboard.h>
#include "render-fx.h"
#include <gint/gray.h>
#include <libprof.h>
@ -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);

View file

@ -6,21 +6,8 @@
#include <libprof.h>
#include <gint/display.h>
#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 */

1
src/sprites.c Normal file
View file

@ -0,0 +1 @@
// Voir README.md pour license précise, par Fcalva 2023-2024 et est sous GPLv3

12
src/sprites.h Normal file
View file

@ -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 <gint/display.h>
//void project_sprite(bopti_image_t *tex, V2d pos, )
#endif

9
src/utils.h Normal file
View file

@ -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 <stdint.h>
#endif