Compare commits
5 Commits
519ae78ada
...
2a0803d4c4
| Author | SHA1 | Date |
|---|---|---|
|
|
2a0803d4c4 | |
|
|
a2420cb277 | |
|
|
8d021e517a | |
|
|
49c158e463 | |
|
|
5eb065b1cf |
|
|
@ -6,6 +6,7 @@ CORE_SOURCES := \
|
|||
asset_manager.c \
|
||||
texture.c \
|
||||
map.c \
|
||||
camera.c \
|
||||
event_bus.c \
|
||||
ecs.c \
|
||||
display.c \
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ int destroy_asset(asset_t *asset)
|
|||
break;
|
||||
|
||||
case ASSET_MAP:
|
||||
destroy_map(asset->data);
|
||||
if(destroy_map(asset->data)) return_failure_int;
|
||||
break;
|
||||
|
||||
default:
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
#include "camera.h"
|
||||
|
||||
#include "game.h"
|
||||
#include "display.h"
|
||||
|
||||
void camera_init(void)
|
||||
{
|
||||
game.camera.view_rect = (rect_t) {
|
||||
.x = 0.0f,
|
||||
.y = 0.0f,
|
||||
.w = DISPLAY_WIDTH / 2,
|
||||
.h = DISPLAY_HEIGHT / 2
|
||||
};
|
||||
|
||||
game.camera.tracking_pos = NULL;
|
||||
game.camera.tracking_center_offset = (fvector2d_t) {
|
||||
.x = 0.0f,
|
||||
.y = 0.0f
|
||||
};
|
||||
}
|
||||
|
||||
void camera_update(void)
|
||||
{
|
||||
if(game.camera.tracking_pos)
|
||||
{
|
||||
game.camera.view_rect.x = game.camera.tracking_pos->x + game.camera.tracking_center_offset.x - game.camera.view_rect.w / 2;
|
||||
game.camera.view_rect.y = game.camera.tracking_pos->y + game.camera.tracking_center_offset.y - game.camera.view_rect.h / 2;
|
||||
}
|
||||
}
|
||||
|
|
@ -24,7 +24,7 @@ inline int display_init(void)
|
|||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
window = SDL_CreateWindow("2D_Engine", DISPLAY_WIDTH * RENDER_SCALE, DISPLAY_HEIGHT * RENDER_SCALE, 0);
|
||||
window = SDL_CreateWindow("2D_Engine", DISPLAY_WIDTH, DISPLAY_HEIGHT, 0);
|
||||
if(!window)
|
||||
{
|
||||
error_printf("Failed to create window : %s", SDL_GetError());
|
||||
|
|
@ -51,12 +51,6 @@ inline int display_init(void)
|
|||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if(!SDL_SetRenderScale(renderer, RENDER_SCALE, RENDER_SCALE))
|
||||
{
|
||||
error_printf("Failed to set renderer scale presentation : %s", SDL_GetError());
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if(!SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND))
|
||||
{
|
||||
error_printf("Failed to set renderer draw blend mode : %s", SDL_GetError());
|
||||
|
|
@ -148,12 +142,21 @@ inline int display_update(void)
|
|||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int display_draw_texture(texture_t *texture, const rect_t *src_rect, const frect_t *dst_frect, bool flip)
|
||||
int display_draw_texture(texture_t *texture, const rect_t *src_rect, const rect_t *dst_rect, bool flip)
|
||||
{
|
||||
frect_t src_frect;
|
||||
rect_to_frect(src_rect, &src_frect);
|
||||
if(src_rect) rect_to_frect(src_rect, &src_frect);
|
||||
|
||||
if(!SDL_RenderTextureRotated(renderer, texture, &src_frect, dst_frect, 0, NULL, flip))
|
||||
frect_t dst_frect;
|
||||
if(dst_rect)
|
||||
dst_frect = (frect_t) {
|
||||
.x = (dst_rect->x - game.camera.view_rect.x) * (DISPLAY_WIDTH / game.camera.view_rect.w),
|
||||
.y = (dst_rect->y - game.camera.view_rect.y) * (DISPLAY_HEIGHT / game.camera.view_rect.h),
|
||||
.w = dst_rect->w * (DISPLAY_WIDTH / game.camera.view_rect.w),
|
||||
.h = dst_rect->h * (DISPLAY_HEIGHT / game.camera.view_rect.h)
|
||||
};
|
||||
|
||||
if(!SDL_RenderTextureRotated(renderer, texture, src_rect ? &src_frect : NULL, dst_rect ? &dst_frect : NULL, 0, NULL, flip))
|
||||
{
|
||||
error_printf("Failed to copy texture to renderer : %s", SDL_GetError());
|
||||
return EXIT_FAILURE;
|
||||
|
|
@ -170,11 +173,18 @@ int display_draw_frect(const frect_t *frect, unsigned int r, unsigned int g, uns
|
|||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if(!SDL_RenderRect(renderer, frect))
|
||||
frect_t display_frect = {
|
||||
.x = (frect->x - game.camera.view_rect.x) * (DISPLAY_WIDTH / game.camera.view_rect.w),
|
||||
.y = (frect->y - game.camera.view_rect.y) * (DISPLAY_HEIGHT / game.camera.view_rect.h),
|
||||
.w = frect->w * (DISPLAY_WIDTH / game.camera.view_rect.w),
|
||||
.h = frect->h * (DISPLAY_HEIGHT / game.camera.view_rect.h)
|
||||
};
|
||||
|
||||
if(!SDL_RenderRect(renderer, &display_frect))
|
||||
{
|
||||
error_printf("Failed to draw rectangle to renderer : %s", SDL_GetError());
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
#ifndef CAMERA_H
|
||||
#define CAMERA_H
|
||||
|
||||
#include "vector2d.h"
|
||||
|
||||
typedef struct camera_t {
|
||||
rect_t view_rect;
|
||||
fvector2d_t *tracking_pos;
|
||||
fvector2d_t tracking_center_offset;
|
||||
} camera_t;
|
||||
|
||||
void camera_init(void);
|
||||
|
||||
void camera_update(void);
|
||||
|
||||
#endif // CAMERA_H
|
||||
|
|
@ -12,9 +12,8 @@
|
|||
#define C_BLUE 0, 0, 255
|
||||
#define C_WHITE 255, 255, 255
|
||||
|
||||
#define DISPLAY_WIDTH 396
|
||||
#define DISPLAY_HEIGHT 224
|
||||
#define RENDER_SCALE 2
|
||||
#define DISPLAY_WIDTH 720
|
||||
#define DISPLAY_HEIGHT 480
|
||||
|
||||
//#define DRIVERS NULL
|
||||
#define DRIVERS "vulkan,gpu,software"
|
||||
|
|
@ -28,7 +27,7 @@ void display_exit(void);
|
|||
int display_clear(void);
|
||||
int display_update(void);
|
||||
|
||||
int display_draw_texture(texture_t *texture, const rect_t *src_rect, const frect_t *dst_frect, bool flip);
|
||||
int display_draw_texture(texture_t *texture, const rect_t *src_rect, const rect_t *dst_rect, bool flip);
|
||||
int display_draw_frect(const frect_t *frect, unsigned int r, unsigned int g, unsigned int b, unsigned int a);
|
||||
|
||||
#endif // DISPLAY_H
|
||||
|
|
@ -15,8 +15,23 @@
|
|||
fprintf(stderr, "\033[32mINFO\033[m: " __VA_ARGS__); \
|
||||
fputc('\n', stderr); \
|
||||
} while (0)
|
||||
|
||||
#define return_failure_int \
|
||||
do { \
|
||||
fprintf(stderr, "\tin %s:%d:%s()\n", __FILE__, __LINE__, __func__); \
|
||||
return EXIT_FAILURE; \
|
||||
} while (0);
|
||||
|
||||
#define return_failure_ptr \
|
||||
do { \
|
||||
fprintf(stderr, "\tin %s:%d:%s()\n", __FILE__, __LINE__, __func__); \
|
||||
return NULL; \
|
||||
} while (0);
|
||||
#else
|
||||
#define debug_printf(...)
|
||||
|
||||
#define return_failure_int return EXIT_FAILURE;
|
||||
#define return_failure_ptr return NULL;
|
||||
#endif
|
||||
|
||||
#define warning_printf(...) \
|
||||
|
|
@ -32,16 +47,4 @@
|
|||
fputc('\n', stderr); \
|
||||
} while (0)
|
||||
|
||||
#define return_failure_int \
|
||||
do { \
|
||||
fprintf(stderr, " in %s:%d:%s()\n", __FILE__, __LINE__, __func__); \
|
||||
return EXIT_FAILURE; \
|
||||
} while (0);
|
||||
|
||||
#define return_failure_ptr \
|
||||
do { \
|
||||
fprintf(stderr, " in %s:%d:%s()\n", __FILE__, __LINE__, __func__); \
|
||||
return NULL; \
|
||||
} while (0);
|
||||
|
||||
#endif // ERRORS_H
|
||||
|
|
@ -3,8 +3,10 @@
|
|||
|
||||
#include <stdbool.h>
|
||||
#include "asset_manager.h"
|
||||
#include "ecs.h"
|
||||
#include "camera.h"
|
||||
#include "event_bus.h"
|
||||
#include "ecs.h"
|
||||
#include "map.h"
|
||||
#include "inputs.h"
|
||||
|
||||
/**
|
||||
|
|
@ -18,10 +20,12 @@
|
|||
* @param delta_time_ms Frame time
|
||||
*/
|
||||
typedef struct game_t {
|
||||
camera_t camera;
|
||||
asset_manager_t asset_manager;
|
||||
entity_manager_t entity_manager;
|
||||
event_bus_t event_bus;
|
||||
entity_manager_t entity_manager;
|
||||
inputs_t inputs;
|
||||
map_t *map;
|
||||
|
||||
float delta_time_ms;
|
||||
bool is_running;
|
||||
|
|
|
|||
|
|
@ -2,35 +2,43 @@
|
|||
#define MAP_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "linked_list.h"
|
||||
#include "texture.h"
|
||||
|
||||
#define TILE_SIZE 16
|
||||
|
||||
typedef struct map_entity_component_def_t {
|
||||
uint32_t type;
|
||||
uint16_t type;
|
||||
void *attributes;
|
||||
} map_entity_component_def_t;
|
||||
|
||||
typedef struct map_entity_def_t {
|
||||
uint32_t id;
|
||||
uint32_t components_nb;
|
||||
uint16_t components_nb;
|
||||
map_entity_component_def_t *components_defs;
|
||||
} map_entity_def_t;
|
||||
|
||||
typedef struct map_t {
|
||||
uint32_t width, height;
|
||||
uint16_t *backgroundlayer1;
|
||||
uint16_t *backgroundlayer2;
|
||||
uint16_t *backgroundlayer3;
|
||||
uint16_t width, height;
|
||||
uint8_t backgroundlayernb;
|
||||
linked_list_t backgroundlayers;
|
||||
uint16_t *foregroundlayer;
|
||||
texture_t *tileset;
|
||||
uint8_t *hitboxes;
|
||||
uint32_t entities_nb;
|
||||
uint16_t entities_nb;
|
||||
map_entity_def_t *entities_defs;
|
||||
} map_t;
|
||||
|
||||
map_t *load_map(void);
|
||||
|
||||
void destroy_map(map_t *map);
|
||||
int destroy_map(map_t *map);
|
||||
|
||||
int map_load_entities(map_t *map);
|
||||
|
||||
int map_remove_entities(map_t *map);
|
||||
|
||||
int map_display_background(void);
|
||||
|
||||
int map_display_foreground(void);
|
||||
|
||||
#endif // MAP_H
|
||||
169
src/map.c
169
src/map.c
|
|
@ -5,6 +5,7 @@
|
|||
#include "ecs.h"
|
||||
#include "components.h"
|
||||
#include "vector2d.h"
|
||||
#include "display.h"
|
||||
#include "memory_alloc.h"
|
||||
#include "errors.h"
|
||||
|
||||
|
|
@ -14,54 +15,59 @@ map_t *load_map(void)
|
|||
|
||||
if(fread(&map->width, sizeof(map->width), 1, game.asset_manager.asset_file) != 1)
|
||||
{
|
||||
error_printf("Failed to read in asset file.");
|
||||
error_printf("Failed to read in asset file : %d", errno);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(fread(&map->height, sizeof(map->height), 1, game.asset_manager.asset_file) != 1)
|
||||
{
|
||||
error_printf("Failed to read in asset file.");
|
||||
error_printf("Failed to read in asset file : %d", errno);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
map->backgroundlayer1 = malloc(map->width * map->height * sizeof(uint16_t));
|
||||
if(fread(map->backgroundlayer1, sizeof(uint16_t), map->width * map->height, game.asset_manager.asset_file) != (size_t)map->width * (size_t)map->height)
|
||||
if(fread(&map->backgroundlayernb, sizeof(map->backgroundlayernb), 1, game.asset_manager.asset_file) != 1)
|
||||
{
|
||||
error_printf("Failed to read in asset file.");
|
||||
error_printf("Failed to read in asset file : %d", errno);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
map->backgroundlayer2 = malloc(map->width * map->height * sizeof(uint16_t));
|
||||
if(fread(map->backgroundlayer2, sizeof(uint16_t), map->width * map->height, game.asset_manager.asset_file) != (size_t)map->width * (size_t)map->height)
|
||||
linked_list_init(&map->backgroundlayers, map->width * map->height * sizeof(uint16_t), NULL, NULL);
|
||||
if(linked_list_reserve(&map->backgroundlayers, map->backgroundlayernb)) return_failure_ptr;
|
||||
|
||||
debug_printf("%d", map->backgroundlayernb);
|
||||
|
||||
for(uint8_t i = 0; i < map->backgroundlayernb; i++)
|
||||
{
|
||||
error_printf("Failed to read in asset file.");
|
||||
elem_t *backgroundlayer_elem = create_elem(&map->backgroundlayers);
|
||||
if(!backgroundlayer_elem) return_failure_ptr;
|
||||
|
||||
uint16_t *backgroundlayer = backgroundlayer_elem->data;
|
||||
if(fread(backgroundlayer, sizeof(*backgroundlayer), map->width * map->height, game.asset_manager.asset_file) != (size_t)map->width * (size_t)map->height)
|
||||
{
|
||||
error_printf("Failed to read in asset file : %d", errno);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
linked_list_push_back(&map->backgroundlayers, backgroundlayer_elem);
|
||||
}
|
||||
|
||||
map->foregroundlayer = malloc(map->width * map->height * sizeof(*map->foregroundlayer));
|
||||
if(fread(map->foregroundlayer, sizeof(*map->foregroundlayer), map->width * map->height, game.asset_manager.asset_file) != (size_t)map->width * (size_t)map->height)
|
||||
{
|
||||
error_printf("Failed to read in asset file : %d", errno);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
map->backgroundlayer3 = malloc(map->width * map->height * sizeof(uint16_t));
|
||||
if(fread(map->backgroundlayer3, sizeof(uint16_t), map->width * map->height, game.asset_manager.asset_file) != (size_t)map->width * (size_t)map->height)
|
||||
map->hitboxes = malloc(map->width * map->height * sizeof(*map->hitboxes));
|
||||
if(fread(map->hitboxes, sizeof(*map->hitboxes), map->width * map->height, game.asset_manager.asset_file) != (size_t)map->width * (size_t)map->height)
|
||||
{
|
||||
error_printf("Failed to read in asset file.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
map->foregroundlayer = malloc(map->width * map->height * sizeof(uint16_t));
|
||||
if(fread(map->foregroundlayer, sizeof(uint16_t), map->width * map->height, game.asset_manager.asset_file) != (size_t)map->width * (size_t)map->height)
|
||||
{
|
||||
error_printf("Failed to read in asset file.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
map->hitboxes = malloc(map->width * map->height * sizeof(uint8_t));
|
||||
if(fread(map->hitboxes, sizeof(uint8_t), map->width * map->height, game.asset_manager.asset_file) != (size_t)map->width * (size_t)map->height)
|
||||
{
|
||||
error_printf("Failed to read in asset file.");
|
||||
error_printf("Failed to read in asset file : %d", errno);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(fread(&map->entities_nb, sizeof(map->entities_nb), 1, game.asset_manager.asset_file) != 1)
|
||||
{
|
||||
error_printf("Failed to read in asset file.");
|
||||
error_printf("Failed to read in asset file : %d", errno);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
@ -73,13 +79,13 @@ map_t *load_map(void)
|
|||
|
||||
if(fread(&entity_def->id, sizeof(entity_def->id), 1, game.asset_manager.asset_file) != 1)
|
||||
{
|
||||
error_printf("Failed to read in asset file.");
|
||||
error_printf("Failed to read in asset file : %d", errno);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(fread(&entity_def->components_nb, sizeof(entity_def->components_nb), 1, game.asset_manager.asset_file) != 1)
|
||||
{
|
||||
error_printf("Failed to read in asset file.");
|
||||
error_printf("Failed to read in asset file : %d", errno);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
@ -91,7 +97,7 @@ map_t *load_map(void)
|
|||
|
||||
if(fread(&component_def->type, sizeof(component_def->type), 1, game.asset_manager.asset_file) != 1)
|
||||
{
|
||||
error_printf("Failed to read in asset file.");
|
||||
error_printf("Failed to read in asset file : %d", errno);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
@ -105,7 +111,7 @@ map_t *load_map(void)
|
|||
component_def->attributes = malloc(data_size);
|
||||
if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1)
|
||||
{
|
||||
error_printf("Failed to read in asset file.");
|
||||
error_printf("Failed to read in asset file : %d", errno);
|
||||
return NULL;
|
||||
}
|
||||
break;
|
||||
|
|
@ -115,7 +121,7 @@ map_t *load_map(void)
|
|||
size_t cap = 0;
|
||||
if(getdelim((char **)&component_def->attributes, &cap, '\0', game.asset_manager.asset_file) == -1)
|
||||
{
|
||||
error_printf("Failed to read in asset file.");
|
||||
error_printf("Failed to read in asset file : %d", errno);
|
||||
return NULL;
|
||||
}
|
||||
break;
|
||||
|
|
@ -126,7 +132,7 @@ map_t *load_map(void)
|
|||
component_def->attributes = malloc(data_size);
|
||||
if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1)
|
||||
{
|
||||
error_printf("Failed to read in asset file.");
|
||||
error_printf("Failed to read in asset file : %d", errno);
|
||||
return NULL;
|
||||
}
|
||||
break;
|
||||
|
|
@ -136,7 +142,7 @@ map_t *load_map(void)
|
|||
component_def->attributes = malloc(data_size);
|
||||
if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1)
|
||||
{
|
||||
error_printf("Failed to read in asset file.");
|
||||
error_printf("Failed to read in asset file : %d", errno);
|
||||
return NULL;
|
||||
}
|
||||
break;
|
||||
|
|
@ -146,7 +152,7 @@ map_t *load_map(void)
|
|||
component_def->attributes = malloc(data_size);
|
||||
if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1)
|
||||
{
|
||||
error_printf("Failed to read in asset file.");
|
||||
error_printf("Failed to read in asset file : %d", errno);
|
||||
return NULL;
|
||||
}
|
||||
break;
|
||||
|
|
@ -156,7 +162,7 @@ map_t *load_map(void)
|
|||
component_def->attributes = malloc(data_size);
|
||||
if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1)
|
||||
{
|
||||
error_printf("Failed to read in asset file.");
|
||||
error_printf("Failed to read in asset file : %d", errno);
|
||||
return NULL;
|
||||
}
|
||||
break;
|
||||
|
|
@ -171,7 +177,7 @@ map_t *load_map(void)
|
|||
return map;
|
||||
}
|
||||
|
||||
void destroy_map(map_t *map)
|
||||
int destroy_map(map_t *map)
|
||||
{
|
||||
if(map->entities_defs)
|
||||
{
|
||||
|
|
@ -198,11 +204,11 @@ void destroy_map(map_t *map)
|
|||
|
||||
if(map->foregroundlayer) free(map->foregroundlayer);
|
||||
|
||||
if(map->backgroundlayer3) free(map->backgroundlayer3);
|
||||
if(map->backgroundlayer2) free(map->backgroundlayer2);
|
||||
if(map->backgroundlayer1) free(map->backgroundlayer1);
|
||||
if(linked_list_exit(&map->backgroundlayers)) return_failure_int;
|
||||
|
||||
free(map);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int map_load_entities(map_t *map)
|
||||
|
|
@ -219,6 +225,7 @@ int map_load_entities(map_t *map)
|
|||
map_entity_component_def_t *component_def = &entity_def->components_defs[j];
|
||||
|
||||
component_t *component = entity_new_component(entity, component_def->type);
|
||||
if(!entity) return_failure_int;
|
||||
|
||||
switch(component_def->type)
|
||||
{
|
||||
|
|
@ -267,7 +274,7 @@ int map_load_entities(map_t *map)
|
|||
system_data->loop = data->loop;
|
||||
system_data->reverse = data->reverse;
|
||||
|
||||
animation_system_apply_changes(component);
|
||||
if(animation_system_apply_changes(component)) return_failure_int;
|
||||
|
||||
break;
|
||||
}
|
||||
|
|
@ -394,3 +401,85 @@ int map_remove_entities(map_t *map)
|
|||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
static inline int draw_tile(uint32_t tile_x, uint32_t tile_y, uint16_t tile_id)
|
||||
{
|
||||
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
|
||||
};
|
||||
|
||||
rect_t dst_frect = {
|
||||
.x = tile_x * TILE_SIZE,
|
||||
.y = tile_y * TILE_SIZE,
|
||||
.w = TILE_SIZE,
|
||||
.h = TILE_SIZE
|
||||
};
|
||||
|
||||
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++)
|
||||
{
|
||||
elem_t *backgroundlayer_elem = game.map->backgroundlayers.first;
|
||||
|
||||
while(backgroundlayer_elem)
|
||||
{
|
||||
if(draw_tile(tile_x, tile_y, ((uint16_t *)backgroundlayer_elem->data)[tile_y * game.map->width + tile_x])) return_failure_int;
|
||||
backgroundlayer_elem = backgroundlayer_elem->next;
|
||||
}
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int map_display_foreground(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 < (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++)
|
||||
{
|
||||
if(draw_tile(tile_x, tile_y, game.map->foregroundlayer[tile_y * game.map->width + tile_x])) return_failure_int;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
texture_t *load_texture(void)
|
||||
{
|
||||
uint32_t width, height;
|
||||
uint16_t width, height;
|
||||
|
||||
if(fread(&width, sizeof(width), 1, game.asset_manager.asset_file) != 1)
|
||||
{
|
||||
|
|
@ -35,7 +35,7 @@ texture_t *load_texture(void)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
if(fread(tmp_surface->pixels, sizeof(uint16_t), width * height, game.asset_manager.asset_file) != width * height)
|
||||
if(fread(tmp_surface->pixels, sizeof(uint16_t), width * height, game.asset_manager.asset_file) != (size_t)width * (size_t)height)
|
||||
{
|
||||
error_printf("Failed to read asset file.");
|
||||
SDL_DestroySurface(tmp_surface);
|
||||
|
|
|
|||
Loading…
Reference in New Issue