Changed map and texture formats.
This commit is contained in:
parent
a2420cb277
commit
2a0803d4c4
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -2,37 +2,36 @@
|
|||
#define MAP_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "vector2d.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);
|
||||
|
||||
|
|
|
|||
97
src/map.c
97
src/map.c
|
|
@ -15,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;
|
||||
}
|
||||
|
||||
|
|
@ -74,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;
|
||||
}
|
||||
|
||||
|
|
@ -92,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;
|
||||
}
|
||||
|
||||
|
|
@ -106,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;
|
||||
|
|
@ -116,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;
|
||||
|
|
@ -127,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;
|
||||
|
|
@ -137,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;
|
||||
|
|
@ -147,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;
|
||||
|
|
@ -157,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;
|
||||
|
|
@ -172,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)
|
||||
{
|
||||
|
|
@ -199,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)
|
||||
|
|
@ -398,7 +403,7 @@ int map_remove_entities(map_t *map)
|
|||
}
|
||||
|
||||
|
||||
static inline int draw_tile_with_id(uint32_t tile_x, uint32_t tile_y, uint16_t tile_id)
|
||||
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,
|
||||
|
|
@ -440,9 +445,13 @@ int map_display_background(void)
|
|||
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->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]);
|
||||
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;
|
||||
|
|
@ -469,7 +478,7 @@ int map_display_foreground(void)
|
|||
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]);
|
||||
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