Copy3DEngine/eng/sprites.c
2024-10-10 17:53:09 +02:00

122 lines
3.2 KiB
C

// Voir README.md pour license précise, par Fcalva 2023-2024 et est sous GPLv3
#include <gint/display.h>
#include <gint/keyboard.h>
#include <stdlib.h>
#include <string.h>
#include "fixed.h"
#include "game.h"
#include "sprites.h"
#include "moteur.h"
#include "config.h"
#include "utils.h"
extern fixed_t zbuf[viewport_w];
int spritec;
Sprite *sprite_index[SINDEX_S];
void add_sprite(Sprite *sprite){
if(spritec >= SINDEX_S)
return;
sprite_index[spritec++] = sprite;
}
void clear_sprites(){
spritec = 0;
}
void remove_sprite(GUNUSED Sprite *sprite){
//TODO
}
//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++){
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;
draw_stripe(tex_index[4], 0, sprite_pos_y, sprite_size, tex_x, x);
}
}
struct SpriteDist{
fixed_t dist;
int id;
};
int sprite_cmpfnc(const void *p1, const void *p2){
return ((struct SpriteDist*)p2)->dist - ((struct SpriteDist*)p1)->dist;
}
void draw_sprites(bopti_image_t *tex_index[], RcActor *player){
struct SpriteDist dists[SINDEX_S];
memset(dists, 0, sizeof(struct SpriteDist)*SINDEX_S);
for(int i = 0; i < spritec; i++){
Sprite *spr = sprite_index[i];
fixed_t d = player->pos.x - spr->pos.x;
d = fmul(d,d);
d += fmul(player->pos.y - spr->pos.y,player->pos.y - spr->pos.y);
dists[i].dist = d;
dists[i].id = i;
}
qsort(dists, SINDEX_S, sizeof(struct SpriteDist), &sprite_cmpfnc);
int msprite = min(spritec,max_sprites);
for(int i = msprite; i > 0; i--){
struct SpriteDist *sd = &dists[i];
Sprite *spr = sprite_index[sd->id];
if(sd->dist > max_dist)
continue;
project_sprite(tex_index, spr, player);
}
}