Skip to content

Commit

Permalink
sprites seem to work...
Browse files Browse the repository at this point in the history
  • Loading branch information
ys-zm committed Jan 29, 2024
1 parent 713d1f2 commit 7c4c0f0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/game/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: yzaim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/08 15:26:51 by yzaim #+# #+# */
/* Updated: 2024/01/25 16:01:28 by jboeve ######## odam.nl */
/* Updated: 2024/01/29 12:51:57 by yesimzaim ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -112,4 +112,5 @@ void game_loop(void* param)
timer_start(&meta->update_timer);
render_minimap(&meta->minimap, &meta->map, &meta->player);
render_viewport(meta->image, &meta->player);
sprite_calculate(&meta->player);
}
49 changes: 35 additions & 14 deletions src/game/sprite.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
/* By: jboeve <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/01/25 16:01:20 by jboeve #+# #+# */
/* Updated: 2024/01/26 17:22:12 by yzaim ######## odam.nl */
/* Updated: 2024/01/29 13:00:59 by yesimzaim ######## odam.nl */
/* */
/* ************************************************************************** */

#include "meta.h"
#include "vector.h"
//#include <algorithm>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
Expand Down Expand Up @@ -39,16 +40,20 @@ void init_sprites(t_sprite *sprites)
UNIMPLEMENTED("mlx_load_png failed");
}

// static void sprite_sort(int32_t *order, double *dist, uint32_t count)
// {
// size_t i = 0;
// while (i < count)
// {
// s[i] =
// i++;
// }

// }
bool if_black(uint32_t color)
{
// Extract individual color channels
uint8_t r = (color >> 24) & 0xFF;
uint8_t g = (color >> 16) & 0xFF;
uint8_t b = (color >> 8) & 0xFF;

// Check if all color channels are 0 (black)
if (r == 0 && g == 0 && b == 0) {
return true; // Black
} else {
return false; // Not black
}
}

void sprite_calculate(t_player *p)
{
Expand Down Expand Up @@ -101,14 +106,30 @@ void sprite_calculate(t_player *p)
if (draw_end.x >= (int32_t) p->meta->image->width)
draw_end.x = p->meta->image->width - 1;


// loop through every vertical stripe of the sprite on the screen
int stripe = draw_start.x;
while (stripe < draw_end.x)
{

int tex_x;
tex_x = (int)(256 * (stripe - (-sprite_width / 2 + sprite_screen_x)) * p->meta->attributes.sprites[i].tex->width / sprite_width) / 256;
if (transform.y > 0 && stripe > 0 && stripe < p->meta->image->width && transform.y < p->z_buffer[stripe])
{
int y;
y = draw_start.y;
while (y < draw_end.y)
{
int d = (y) * 256 - p->meta->image->height * 128 + sprite_height * 128; //256 and 128 factors to avoid floats
int tex_y = ((d * p->meta->attributes.sprites[i].tex->height) / sprite_height) / 256;
uint32_t color = pixel_picker(p->meta->attributes.sprites[i].tex, tex_x, tex_y);
if (!if_black(color))
{
mlx_put_pixel(p->meta->image, stripe, y, color);
}
y++;
}
}
stripe++;
}

i++;
}

Expand Down
7 changes: 1 addition & 6 deletions src/game/sprite_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jboeve <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/01/25 16:01:20 by jboeve #+# #+# */
/* Updated: 2024/01/26 17:24:54 by yzaim ######## odam.nl */
/* Updated: 2024/01/29 12:50:12 by yesimzaim ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -68,10 +68,5 @@ void quick_sort(double *sprite_dist, int32_t *sprite_order, int32_t low, int32_t

void sprite_sort(double *sprite_dist, int32_t *sprite_order)
{
print_double_array("SPRITE_DIST", sprite_dist, SPRITE_COUNT);
print_ints_array("SPRITE_ORDER", sprite_order, SPRITE_COUNT);
printf("SPRITECOUNT: %u\n", SPRITE_COUNT);
quick_sort(sprite_dist, sprite_order, 0, SPRITE_COUNT - 1);
print_double_array("SPRITE_DIST", sprite_dist, SPRITE_COUNT);
print_ints_array("SPRITE_ORDER", sprite_order, SPRITE_COUNT);
}

0 comments on commit 7c4c0f0

Please sign in to comment.