Working map diplaying.

This commit is contained in:
Ulysse Cura 2026-08-20 01:06:26 +02:00
parent 49c158e463
commit 8d021e517a
3 changed files with 67 additions and 123 deletions

View File

@ -24,7 +24,6 @@ typedef struct game_t {
asset_manager_t asset_manager;
event_bus_t event_bus;
entity_manager_t entity_manager;
tilemap_display_t tilemap_display;
inputs_t inputs;
map_t *map;

View File

@ -5,6 +5,8 @@
#include "vector2d.h"
#include "texture.h"
#define TILE_SIZE 16
typedef struct map_entity_component_def_t {
uint32_t type;
void *attributes;
@ -22,6 +24,7 @@ typedef struct map_t {
uint16_t *backgroundlayer2;
uint16_t *backgroundlayer3;
uint16_t *foregroundlayer;
texture_t *tileset;
uint8_t *hitboxes;
uint32_t entities_nb;
map_entity_def_t *entities_defs;
@ -35,24 +38,8 @@ int map_load_entities(map_t *map);
int map_remove_entities(map_t *map);
#define TILE_SIZE 16
int map_display_background(void);
typedef struct tilemap_display_t {
frect_t view_rect;
texture_t *tileset;
texture_t *background;
texture_t *foreground;
texture_t *buffer;
} tilemap_display_t;
int tilemap_display_init(void);
int tilemap_display_update(void);
int tilemap_display_background(void);
int tilemap_display_foreground(void);
void tilemap_display_exit(void);
int map_display_foreground(void);
#endif // MAP_H

166
src/map.c
View File

@ -397,122 +397,80 @@ int map_remove_entities(map_t *map)
return EXIT_SUCCESS;
}
int tilemap_display_init(void)
static inline int draw_tile_with_id(uint32_t tile_x, uint32_t tile_y, uint16_t tile_id)
{
game.tilemap_display.view_rect = (frect_t) {
.x = 0.0f,
.y = 0.0f,
.w = 0.0f,
.h = 0.0f
rect_t src_frect = {
.x = tile_id * TILE_SIZE % game.map->tileset->w,
.y = tile_id * TILE_SIZE / game.map->tileset->w * TILE_SIZE,
.w = TILE_SIZE,
.h = TILE_SIZE
};
game.tilemap_display.background = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_TARGET, DISPLAY_WIDTH, DISPLAY_HEIGHT);
if(!game.tilemap_display.background)
{
error_printf("Failed to create texture : %s", SDL_GetError());
return EXIT_FAILURE;
}
game.tilemap_display.foreground = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_TARGET, DISPLAY_WIDTH, DISPLAY_HEIGHT);
if(!game.tilemap_display.foreground)
{
error_printf("Failed to create texture : %s", SDL_GetError());
return EXIT_FAILURE;
}
game.tilemap_display.buffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_TARGET, DISPLAY_WIDTH, DISPLAY_HEIGHT);
if(!game.tilemap_display.buffer)
{
error_printf("Failed to create texture : %s", SDL_GetError());
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int tilemap_display_update(void)
{
frect_t buffer_view_rect = {
.x = game.tilemap_display.view_rect.x - game.camera.view_rect.x,
.y = game.tilemap_display.view_rect.y - game.camera.view_rect.y,
.w = game.tilemap_display.view_rect.w - game.camera.view_rect.w,
.h = game.tilemap_display.view_rect.h - game.camera.view_rect.h
rect_t dst_frect = {
.x = tile_x * TILE_SIZE,
.y = tile_y * TILE_SIZE,
.w = TILE_SIZE,
.h = TILE_SIZE
};
if(game.tilemap_display.view_rect.x != game.camera.view_rect.x
|| game.tilemap_display.view_rect.y != game.camera.view_rect.y
|| game.tilemap_display.view_rect.w != game.camera.view_rect.w
|| game.tilemap_display.view_rect.h != game.camera.view_rect.h)
if(display_draw_texture(game.map->tileset, &src_frect, &dst_frect, false)) return_failure_int;
return EXIT_SUCCESS;
}
int map_display_background(void)
{
uint32_t left_tile = 0;
if(game.camera.view_rect.x / TILE_SIZE > 0)
left_tile = game.camera.view_rect.x / TILE_SIZE;
uint32_t right_tile = game.map->width;
if((game.camera.view_rect.x + game.camera.view_rect.w) / TILE_SIZE + 1 < game.map->width)
right_tile = (game.camera.view_rect.x + game.camera.view_rect.w) / TILE_SIZE + 1;
uint32_t top_tile = 0;
if(game.camera.view_rect.y / TILE_SIZE > 0)
top_tile = game.camera.view_rect.y / TILE_SIZE;
uint32_t bottom_tile = game.map->height;
if((game.camera.view_rect.y + game.camera.view_rect.h) / TILE_SIZE + 1 < game.map->height)
bottom_tile = (game.camera.view_rect.y + game.camera.view_rect.h) / TILE_SIZE + 1;
for(uint32_t tile_y = top_tile; tile_y < bottom_tile; tile_y++)
for(uint32_t tile_x = left_tile; tile_x < right_tile; tile_x++)
{
// Update background
if(!SDL_SetRenderTarget(renderer, game.tilemap_display.buffer))
{
error_printf("Failed to set texture as render target : %s", SDL_GetError());
return EXIT_FAILURE;
}
if(!SDL_SetRenderDrawColor(renderer, C_WHITE, 255))
{
error_printf("Failed to change renderer draw color : %s", SDL_GetError());
return EXIT_FAILURE;
}
if(!SDL_RenderClear(renderer))
{
error_printf("Failed to clear renderer : %s", SDL_GetError());
return EXIT_FAILURE;
}
if(!SDL_RenderTexture(renderer, game.tilemap_display.background, &game.tilemap_display.view_rect, &buffer_view_rect))
{
error_printf("Failed to render texture : %s", SDL_GetError());
return EXIT_FAILURE;
}
if(display_draw_frect(&game.tilemap_display.view_rect, C_BLACK, 255)) return_failure_int;
// Rewrite buffer to background texture
if(!SDL_SetRenderTarget(renderer, game.tilemap_display.background))
{
error_printf("Failed to reset render target : %s", SDL_GetError());
return EXIT_FAILURE;
}
if(!SDL_RenderTexture(renderer, game.tilemap_display.buffer, NULL, NULL))
{
error_printf("Failed to render texture : %s", SDL_GetError());
return EXIT_FAILURE;
}
if(!SDL_SetRenderTarget(renderer, NULL))
{
error_printf("Failed to reset render target : %s", SDL_GetError());
return EXIT_FAILURE;
}
draw_tile_with_id(tile_x, tile_y, game.map->backgroundlayer1[tile_y * game.map->width + tile_x]);
draw_tile_with_id(tile_x, tile_y, game.map->backgroundlayer2[tile_y * game.map->width + tile_x]);
draw_tile_with_id(tile_x, tile_y, game.map->backgroundlayer3[tile_y * game.map->width + tile_x]);
}
game.tilemap_display.view_rect = game.camera.view_rect;
return EXIT_SUCCESS;
}
int tilemap_display_background(void)
int map_display_foreground(void)
{
if(display_draw_texture(game.tilemap_display.background, NULL, NULL, false)) return_failure_int;
uint32_t left_tile = 0;
if(game.camera.view_rect.x / TILE_SIZE > 0)
left_tile = game.camera.view_rect.x / TILE_SIZE;
uint32_t right_tile = game.map->width;
if((game.camera.view_rect.x + game.camera.view_rect.w) / TILE_SIZE + 1 < (int)game.map->width)
right_tile = (game.camera.view_rect.x + game.camera.view_rect.w) / TILE_SIZE + 1;
uint32_t top_tile = 0;
if(game.camera.view_rect.y / TILE_SIZE > 0)
top_tile = game.camera.view_rect.y / TILE_SIZE;
uint32_t bottom_tile = game.map->height;
if((game.camera.view_rect.y + game.camera.view_rect.h) / TILE_SIZE + 1 < (int)game.map->height)
bottom_tile = (game.camera.view_rect.y + game.camera.view_rect.h) / TILE_SIZE + 1;
for(uint32_t tile_y = top_tile; tile_y < bottom_tile; tile_y++)
for(uint32_t tile_x = left_tile; tile_x < right_tile; tile_x++)
{
draw_tile_with_id(tile_x, tile_y, game.map->foregroundlayer[tile_y * game.map->width + tile_x]);
}
return EXIT_SUCCESS;
}
int tilemap_display_foreground(void)
{
// if(display_draw_texture(game.tilemap_display.foreground, NULL, NULL, false)) return_failure_int;
return EXIT_SUCCESS;
}
void tilemap_display_exit(void)
{
SDL_DestroyTexture(game.tilemap_display.background);
SDL_DestroyTexture(game.tilemap_display.foreground);
SDL_DestroyTexture(game.tilemap_display.buffer);
}