Compare commits

..

No commits in common. "2a0803d4c4e15048fced7d3c402b76fc782e0d85" and "519ae78ada36b922c64de8d37571db8d0ba0c944" have entirely different histories.

11 changed files with 81 additions and 240 deletions

View File

@ -6,7 +6,6 @@ CORE_SOURCES := \
asset_manager.c \
texture.c \
map.c \
camera.c \
event_bus.c \
ecs.c \
display.c \

View File

@ -39,7 +39,7 @@ int destroy_asset(asset_t *asset)
break;
case ASSET_MAP:
if(destroy_map(asset->data)) return_failure_int;
destroy_map(asset->data);
break;
default:

View File

@ -1,29 +0,0 @@
#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;
}
}

View File

@ -24,7 +24,7 @@ inline int display_init(void)
return EXIT_FAILURE;
}
window = SDL_CreateWindow("2D_Engine", DISPLAY_WIDTH, DISPLAY_HEIGHT, 0);
window = SDL_CreateWindow("2D_Engine", DISPLAY_WIDTH * RENDER_SCALE, DISPLAY_HEIGHT * RENDER_SCALE, 0);
if(!window)
{
error_printf("Failed to create window : %s", SDL_GetError());
@ -51,6 +51,12 @@ 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());
@ -142,21 +148,12 @@ inline int display_update(void)
return EXIT_SUCCESS;
}
int display_draw_texture(texture_t *texture, const rect_t *src_rect, const rect_t *dst_rect, bool flip)
int display_draw_texture(texture_t *texture, const rect_t *src_rect, const frect_t *dst_frect, bool flip)
{
frect_t src_frect;
if(src_rect) rect_to_frect(src_rect, &src_frect);
rect_to_frect(src_rect, &src_frect);
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))
if(!SDL_RenderTextureRotated(renderer, texture, &src_frect, dst_frect, 0, NULL, flip))
{
error_printf("Failed to copy texture to renderer : %s", SDL_GetError());
return EXIT_FAILURE;
@ -173,18 +170,11 @@ int display_draw_frect(const frect_t *frect, unsigned int r, unsigned int g, uns
return EXIT_FAILURE;
}
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))
if(!SDL_RenderRect(renderer, frect))
{
error_printf("Failed to draw rectangle to renderer : %s", SDL_GetError());
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
}

View File

@ -1,16 +0,0 @@
#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

View File

@ -12,8 +12,9 @@
#define C_BLUE 0, 0, 255
#define C_WHITE 255, 255, 255
#define DISPLAY_WIDTH 720
#define DISPLAY_HEIGHT 480
#define DISPLAY_WIDTH 396
#define DISPLAY_HEIGHT 224
#define RENDER_SCALE 2
//#define DRIVERS NULL
#define DRIVERS "vulkan,gpu,software"
@ -27,7 +28,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 rect_t *dst_rect, bool flip);
int display_draw_texture(texture_t *texture, const rect_t *src_rect, const frect_t *dst_frect, 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

View File

@ -15,23 +15,8 @@
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(...) \
@ -47,4 +32,16 @@
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

View File

@ -3,10 +3,8 @@
#include <stdbool.h>
#include "asset_manager.h"
#include "camera.h"
#include "event_bus.h"
#include "ecs.h"
#include "map.h"
#include "event_bus.h"
#include "inputs.h"
/**
@ -20,12 +18,10 @@
* @param delta_time_ms Frame time
*/
typedef struct game_t {
camera_t camera;
asset_manager_t asset_manager;
event_bus_t event_bus;
entity_manager_t entity_manager;
event_bus_t event_bus;
inputs_t inputs;
map_t *map;
float delta_time_ms;
bool is_running;

View File

@ -2,43 +2,35 @@
#define MAP_H
#include <stdint.h>
#include "linked_list.h"
#include "texture.h"
#define TILE_SIZE 16
typedef struct map_entity_component_def_t {
uint16_t type;
uint32_t type;
void *attributes;
} map_entity_component_def_t;
typedef struct map_entity_def_t {
uint32_t id;
uint16_t components_nb;
uint32_t components_nb;
map_entity_component_def_t *components_defs;
} map_entity_def_t;
typedef struct map_t {
uint16_t width, height;
uint8_t backgroundlayernb;
linked_list_t backgroundlayers;
uint32_t width, height;
uint16_t *backgroundlayer1;
uint16_t *backgroundlayer2;
uint16_t *backgroundlayer3;
uint16_t *foregroundlayer;
texture_t *tileset;
uint8_t *hitboxes;
uint16_t entities_nb;
uint32_t entities_nb;
map_entity_def_t *entities_defs;
} map_t;
map_t *load_map(void);
int destroy_map(map_t *map);
void 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
View File

@ -5,7 +5,6 @@
#include "ecs.h"
#include "components.h"
#include "vector2d.h"
#include "display.h"
#include "memory_alloc.h"
#include "errors.h"
@ -15,59 +14,54 @@ 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 : %d", errno);
error_printf("Failed to read in asset file.");
return NULL;
}
if(fread(&map->height, sizeof(map->height), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", errno);
error_printf("Failed to read in asset file.");
return NULL;
}
if(fread(&map->backgroundlayernb, sizeof(map->backgroundlayernb), 1, game.asset_manager.asset_file) != 1)
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)
{
error_printf("Failed to read in asset file : %d", errno);
error_printf("Failed to read in asset file.");
return NULL;
}
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++)
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)
{
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);
error_printf("Failed to read in asset file.");
return NULL;
}
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)
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)
{
error_printf("Failed to read in asset file : %d", errno);
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.");
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 : %d", errno);
error_printf("Failed to read in asset file.");
return NULL;
}
@ -79,13 +73,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 : %d", errno);
error_printf("Failed to read in asset file.");
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 : %d", errno);
error_printf("Failed to read in asset file.");
return NULL;
}
@ -97,7 +91,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 : %d", errno);
error_printf("Failed to read in asset file.");
return NULL;
}
@ -111,7 +105,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 : %d", errno);
error_printf("Failed to read in asset file.");
return NULL;
}
break;
@ -121,7 +115,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 : %d", errno);
error_printf("Failed to read in asset file.");
return NULL;
}
break;
@ -132,7 +126,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 : %d", errno);
error_printf("Failed to read in asset file.");
return NULL;
}
break;
@ -142,7 +136,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 : %d", errno);
error_printf("Failed to read in asset file.");
return NULL;
}
break;
@ -152,7 +146,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 : %d", errno);
error_printf("Failed to read in asset file.");
return NULL;
}
break;
@ -162,7 +156,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 : %d", errno);
error_printf("Failed to read in asset file.");
return NULL;
}
break;
@ -177,7 +171,7 @@ map_t *load_map(void)
return map;
}
int destroy_map(map_t *map)
void destroy_map(map_t *map)
{
if(map->entities_defs)
{
@ -204,11 +198,11 @@ int destroy_map(map_t *map)
if(map->foregroundlayer) free(map->foregroundlayer);
if(linked_list_exit(&map->backgroundlayers)) return_failure_int;
if(map->backgroundlayer3) free(map->backgroundlayer3);
if(map->backgroundlayer2) free(map->backgroundlayer2);
if(map->backgroundlayer1) free(map->backgroundlayer1);
free(map);
return EXIT_SUCCESS;
}
int map_load_entities(map_t *map)
@ -225,7 +219,6 @@ 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)
{
@ -274,7 +267,7 @@ int map_load_entities(map_t *map)
system_data->loop = data->loop;
system_data->reverse = data->reverse;
if(animation_system_apply_changes(component)) return_failure_int;
animation_system_apply_changes(component);
break;
}
@ -401,85 +394,3 @@ 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;
}

View File

@ -7,7 +7,7 @@
texture_t *load_texture(void)
{
uint16_t width, height;
uint32_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) != (size_t)width * (size_t)height)
if(fread(tmp_surface->pixels, sizeof(uint16_t), width * height, game.asset_manager.asset_file) != width * height)
{
error_printf("Failed to read asset file.");
SDL_DestroySurface(tmp_surface);