diff --git a/src/config.h b/src/config.h index c97a8f5..7ffaef0 100644 --- a/src/config.h +++ b/src/config.h @@ -9,7 +9,10 @@ #define TSIZE 32 +//Tex index #define TINDEX_S 16 +//Sprite index +#define SINDEX_S 64 #define debug 1 //pour afficher les infos de debug diff --git a/src/fixed.h b/src/fixed.h index bf8dc83..360d74a 100644 --- a/src/fixed.h +++ b/src/fixed.h @@ -89,7 +89,7 @@ static inline fixed_t fease(fixed_t x) } static inline fixed_t fixabs(fixed_t x){ - return x & 0x80000000; + return x & 0x7FFFFFFF; } static inline fixed_t fV2d_dotp(V2d u, V2d v){ diff --git a/src/main.c b/src/main.c index 3549b44..12bdf16 100644 --- a/src/main.c +++ b/src/main.c @@ -19,9 +19,10 @@ #include "moteur.h" #include "map.h" #include "config.h" +#include "sprites.h" //====== Copy3DEngine ===== -// Git du moteur : https://gitea.planet-casio.com/Fcalva/Copy3DEngine +// Git du moteur : https://git.planet-casio.com/Fcalva/Copy3DEngine // Git du jeu : [Rajoutez le vôtre ici] // // Page du jeu : [La vôtre ici] @@ -98,11 +99,13 @@ int main(){ tex_index[1] = &buisson0; tex_index[2] = &briques0; - //Vos textures générées procéduralement + //Vos textures générées procéduralement (TODO) tex_index[0] = &briques0; tex_index[3] = &briques0; + tex_index[4] = &sprite_tst; + prof_init(); if(dgray(DGRAY_ON)) @@ -116,6 +119,11 @@ int main(){ //gdb_start_on_exception(); #endif + Sprite tsprite = { + .pos = {200000, 300000}, + .tex = 4 + }; + while (!exit_game) { prof_t frame = prof_make(); prof_enter(frame); @@ -135,15 +143,13 @@ int main(){ &game ); + project_sprite(tex_index, &tsprite, &game.player); + 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, 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, 27, C_BLACK, "pX %d", game.player.pos.x); + dprint( 1, 36, C_BLACK, "pY %d", game.player.pos.y); 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(); diff --git a/src/moteur.c b/src/moteur.c index c3db427..396f15f 100644 --- a/src/moteur.c +++ b/src/moteur.c @@ -25,6 +25,9 @@ // // +//1D zbuf for sprites +fixed_t zbuf[viewport_w]; + 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 @@ -131,8 +134,7 @@ inline int __attribute__((always_inline)) 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; + return (dark << 1) + light; } uint32_t *light, *dark; @@ -142,7 +144,7 @@ uint32_t *light, *dark; inline void __attribute__((always_inline)) cust_dpixel(int x, int y, color_t color){ - if(y >= viewport_h) + if(y >= viewport_h || y < 0) return; int offset = (y << 2) + (x >> 5); uint32_t mask = 1 << (~x & 31); @@ -189,7 +191,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{ - dpixel(ffloor(oldx), ffloor(oldy), tpix); + cust_dpixel(ffloor(oldx), ffloor(oldy), tpix); oldx += 0xFFFF; } while(oldx < screen_x+scale_x); screen_x += scale_x; @@ -274,7 +276,6 @@ void draw_walls( rayDirX = dirX + fmul(planeX, cameraX); rayDirY = dirY + fmul(planeY, cameraX); - //which box of the map we're in mapX = ffloor(posX); mapY = ffloor(posY); @@ -297,6 +298,8 @@ void draw_walls( deltaDistX = abs(fdiv(0xFFFF, rayDirX)); deltaDistY = abs(fdiv(0xFFFF, rayDirY)); + if(deltaDistX > max_dist) deltaDistX = max_dist; + if(deltaDistY > max_dist) deltaDistY = max_dist; //calculate step and initial sideDist if (rayDirX < 0) { @@ -352,6 +355,8 @@ void draw_walls( if (side == 0) perpWallDist = (sideDistX - deltaDistX); else perpWallDist = (sideDistY - deltaDistY); + zbuf[x] = perpWallDist; + #if debug prof_leave(timers->raycast_time); prof_enter(timers->draw_time); @@ -391,6 +396,8 @@ void draw_walls( bopti_image_t *tex = tex_index[map_test[mapX*map_w+mapY]]; + //dline(x, linePos, x, linePos + lineHeight, 1); + draw_stripe(tex, texSampleY, linePos, texSize, texX, x); diff --git a/src/sprites.c b/src/sprites.c index 6dc7649..b1c6349 100644 --- a/src/sprites.c +++ b/src/sprites.c @@ -1 +1,86 @@ // Voir README.md pour license précise, par Fcalva 2023-2024 et est sous GPLv3 + +#include +#include + +#include "game.h" +#include "sprites.h" +#include "moteur.h" +#include "config.h" +#include "fixed.h" + +extern fixed_t zbuf[viewport_w]; + +int spritec; + +Sprite *sprite_index[SINDEX_S]; + +void add_sprite(Sprite *sprite){ + +} + +void remove_sprite(Sprite *sprite){ + +} + +void draw_sprites(){ + +} + +//Lodev's sprite projection, translated + +void project_sprite(bopti_image_t *tex_index[], Sprite *sprite, RcActor *player){ + + V2d relpos = sprite->pos; + relpos.x -= player->pos.x; + relpos.y -= player->pos.y; + + fixed_t idet = fmul(player->plane.x, player->dir.y) - + fmul(player->dir.x, player->plane.y); + idet = idet ? fdiv(fix(1), idet):0; + + fixed_t tx = fmul(idet, fmul(player->dir.y, relpos.x) - + fmul(player->dir.x, relpos.y)); + fixed_t ty = fmul(idet, fmul(-player->plane.y, relpos.x) + + fmul(player->plane.x, relpos.y)); + fixed_t txtydiv = ty ? fdiv(tx, ty):0; + int screen_x = ffloor(fmul(fix(viewport_w/2),(fix(1) + txtydiv))); + + int sprite_h = ffloor(fixabs(fdiv(fix(viewport_h),ty))); + + int sprite_pos_y = -sprite_h / 2 + viewport_h / 2; + + if(sprite_pos_y < 0) + sprite_pos_y = 0; + + int sprite_pos_x = -sprite_h / 2 + screen_x; + if(sprite_pos_x < 0) + sprite_pos_x = 0; + int sprite_end_x = sprite_h / 2 + screen_x; + if(sprite_end_x > viewport_w) + sprite_end_x = viewport_w; + + int sprite_w = sprite_h; + + if(sprite_pos_x + sprite_h > viewport_w) + sprite_h = viewport_w - sprite_pos_x; + if(sprite_pos_y + sprite_w > viewport_h) + sprite_w = viewport_h - sprite_pos_y; + if(sprite_pos_x + sprite_h < 0 || sprite_pos_x > viewport_w) + return; + + fixed_t sprite_size = fdiv(fix(sprite_w), fix(TSIZE)); + + for(int x = sprite_pos_x; x < sprite_end_x; x++){ + //TODO : Zbuffer + if(ty < 0 || x < 0 || ty > zbuf[x]) + continue; + if(x > viewport_w) + break; + int tex_x = ffloor((x-sprite_pos_x) * fdiv(fix(TSIZE), fix(sprite_w))); + tex_x %= TSIZE; + + //dline(x, sprite_pos_y, x, sprite_pos_y+sprite_w,2); + draw_stripe(tex_index[4], 0, sprite_pos_y, sprite_size, tex_x, x); + } +} diff --git a/src/sprites.h b/src/sprites.h index 16fe029..44e9e56 100644 --- a/src/sprites.h +++ b/src/sprites.h @@ -4,9 +4,16 @@ #define sprites__h #include "utils.h" +#include "fixed.h" +#include "game.h" #include -//void project_sprite(bopti_image_t *tex, V2d pos, ) +typedef struct { + V2d pos; + int tex; +} Sprite; + +void project_sprite(bopti_image_t *tex_index[], Sprite *sprite, RcActor *player); #endif