aboutsummaryrefslogtreecommitdiff
path: root/sources/TZR_LoadResourceFromMemory.c
blob: 1df08244e3db3794c2dc1f6be2dc6ff7da3f9d20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "TZR_resource.h"
#include "TZR_globals.h"
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_rwops.h>
#include <stdio.h>
#include <stdlib.h>

static int
reserve_ressources(size_t size)
{
	if (size <= ___tzr_resources_capacity)
		return 0;
	size_t target = 16;
	while (target < size * sizeof(TZR_Resource))
		target *= 2;
	TZR_Resource *new_resources;
	if (___tzr_resources == NULL)
		new_resources = malloc(target);
	else
		new_resources = realloc(___tzr_resources, target);
	if (new_resources == NULL) {
		perror("TZR_LoadResourceFromMemory:reserve_ressources");
		return 1;
	}
	___tzr_resources = new_resources;
	return 0;
}

TZR_Uint
TZR_LoadResourceFromMemory(TZR_ResourceType type, const void *data, int size)
{
	if (reserve_ressources(___tzr_resources_size + 1)) {
		fprintf(stderr, "failed to reserve for new ressource\n");
		return 0;
	}
	TZR_Resource *const res = &___tzr_resources[___tzr_resources_size];
	res->type = type;
	res->path = NULL;
	if (TZR_DirectResourceLoad(res, data, size))
		return 0;
	___tzr_resources_size += 1;
	return ___tzr_resources_size;
}