Copy3DEngine/src/sprites.c

87 lines
2.2 KiB
C
Raw Normal View History

2024-10-05 00:56:13 +02:00
// Voir README.md pour license précise, par Fcalva 2023-2024 et est sous GPLv3
2024-10-05 21:48:42 +02:00
#include <gint/display.h>
#include <gint/keyboard.h>
#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);
}
}