From 19f0a5d022e4862f8e5bcbaac80b18502d7d16ff Mon Sep 17 00:00:00 2001 From: attilavs2 Date: Tue, 8 Oct 2024 13:16:05 +0200 Subject: [PATCH] fx : ajout de sprites --- assets-fx/fxconv-metadata.txt | 1 + assets-fx/sprite.png | Bin 699 -> 642 bytes src/config.h | 2 ++ src/main.c | 8 ++++-- src/moteur.c | 29 ++++++-------------- src/moteur.h | 2 ++ src/sprites.c | 50 +++++++++++++++++++++++++++++----- src/sprites.h | 8 +++++- src/utils.h | 1 + 9 files changed, 71 insertions(+), 30 deletions(-) diff --git a/assets-fx/fxconv-metadata.txt b/assets-fx/fxconv-metadata.txt index d6d7d33..2766769 100644 --- a/assets-fx/fxconv-metadata.txt +++ b/assets-fx/fxconv-metadata.txt @@ -1,3 +1,4 @@ sprite.png: type: bopti-image name: sprite_tst + profile: gray_alpha diff --git a/assets-fx/sprite.png b/assets-fx/sprite.png index 3dbb8a7aeea434b1f24cfd59aeb0180a201ec32a..d934f8895ee6839da02ce6a0796414e40e03488d 100644 GIT binary patch delta 173 zcmdnZ+QhnH7o#Bq2>f*3T@R!<3p^r=85s1GL71^(seKtxkiEpy*OmPR7Y8?|iR+Wa zGLv63YUD?Gx;TbJ9DX}3kc&Zq!#Q-<|EX_T94!*3+^&B&akI(fB`=E}XVq)ZI;B%<&5c{p~5dGj5JBtC|gZ;G$%pMB_ Vly{e(I{>tr!PC{xWt~$(695esJNf_s delta 231 zcmVK~z}7?Ub<&z#s@juf)y&|4;@ugIyeqNt+g_4CN9h z6MevOKqwT_tM(%Rc=zW#fuMtIt^N+`u}BF}tSTYHuH@Z55CCm3qMHeqJYe #include +#include #include #include #include #include "fixed.h" -#include "gint/display-cg.h" -#include "gint/display-fx.h" #include "moteur.h" #include "map.h" #include "game.h" @@ -23,7 +22,6 @@ // moteur.c : // ici se trouvent tout ce qui concerne les graphismes, mouvement et collisions // -// //1D zbuf for sprites fixed_t zbuf[viewport_w]; @@ -107,22 +105,6 @@ void load_map(){ //Fonction fournie par le grand Lephé :D inline int __attribute__((always_inline)) get_pixel(bopti_image_t const *img, int u, int v) -{ - int layers = 2; - 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; - 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; @@ -134,7 +116,11 @@ inline int __attribute__((always_inline)) int light = (layer_ptr[0] & mask) != 0; int dark = (layer_ptr[1] & mask) != 0; - return (dark << 1) + light; + int alpha = 0; + if(layers > 2){ + alpha = !(layer_ptr[2] & mask); + } + return (alpha << 2) + (dark << 1) + light; } uint32_t *light, *dark; @@ -169,6 +155,9 @@ inline void __attribute__((always_inline)) light[offset] |= mask; dark [offset] |= mask; break; + case C_NONE: + break; + default: break; } } diff --git a/src/moteur.h b/src/moteur.h index 9d85339..093a582 100644 --- a/src/moteur.h +++ b/src/moteur.h @@ -16,6 +16,8 @@ typedef struct { } EngineTimers; +int get_pixel(bopti_image_t const *tex, int u, int v); + void dscale_bopti(bopti_image_t *tex, fixed_t scale_x, fixed_t scale_y, int x, int y); diff --git a/src/sprites.c b/src/sprites.c index b1c6349..6c8b157 100644 --- a/src/sprites.c +++ b/src/sprites.c @@ -2,12 +2,15 @@ #include #include +#include +#include #include "game.h" #include "sprites.h" #include "moteur.h" #include "config.h" #include "fixed.h" +#include "utils.h" extern fixed_t zbuf[viewport_w]; @@ -16,15 +19,17 @@ int spritec; Sprite *sprite_index[SINDEX_S]; void add_sprite(Sprite *sprite){ - + if(spritec >= SINDEX_S) + return; + sprite_index[spritec++] = sprite; } -void remove_sprite(Sprite *sprite){ - +void clear_sprites(){ + spritec = 0; } -void draw_sprites(){ - +void remove_sprite(GUNUSED Sprite *sprite){ + //TODO } //Lodev's sprite projection, translated @@ -72,7 +77,6 @@ void project_sprite(bopti_image_t *tex_index[], Sprite *sprite, RcActor *player) 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) @@ -80,7 +84,39 @@ void project_sprite(bopti_image_t *tex_index[], Sprite *sprite, RcActor *player) 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); } } + +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); + } +} diff --git a/src/sprites.h b/src/sprites.h index 44e9e56..aed5543 100644 --- a/src/sprites.h +++ b/src/sprites.h @@ -14,6 +14,12 @@ typedef struct { int tex; } Sprite; -void project_sprite(bopti_image_t *tex_index[], Sprite *sprite, RcActor *player); +//Adds the sprite reference to the internal sprite index +// /!\ This sprite reference may be used at every call of draw_sprites +void add_sprite(Sprite *sprite); + +void clear_sprites(); + +void draw_sprites(bopti_image_t *tex_index[], RcActor *player); #endif diff --git a/src/utils.h b/src/utils.h index ebf556e..e17cfd8 100644 --- a/src/utils.h +++ b/src/utils.h @@ -5,5 +5,6 @@ #include +#define min(x, xmin) (x > xmin ? xmin:x) #endif