aboutsummaryrefslogtreecommitdiff
path: root/sources/TZR_DirectResourceLoad.c
blob: 4b010e179c55d8473c1dd060a062ead4199b2216 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "TZR_resource.h"
#include "TZR_globals.h"
#include "sdl_error.h"
#include <SDL2/SDL_opengl.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_rwops.h>
#include <errno.h>
#include <string.h>

#ifdef TZR_STB_IMAGE
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

SDL_Surface *
stbi_load_surface(void *src, int size)
{
	int width, height, chans;
	void *const data = stbi_load_from_memory(src, size, &width, &height,
	                                         &chans, 4);
	const int pitch = (width * 4 + 3) & ~3;

	if (data == NULL) {
		TZR_Log("stbi: %s", stbi_failure_reason());
		return NULL;
	}

	if (chans != 4) {
		TZR_Log("TZR only supports RGBA images");
		free(data);
		return NULL;
	}

#if SDL_BYTEORDER == SDL_LIL_ENDIAN
	const int rmask = 0x000000ff;
	const int gmask = 0x0000ff00;
	const int bmask = 0x00ff0000;
	const int amask = 0xff000000 * (chans == 4);
#else
	const int s = 8 * (chans != 4);
	const int rmask = 0xff000000 >> s;
	const int gmask = 0x00ff0000 >> s;
	const int bmask = 0x0000ff00 >> s;
	const int amask = 0x000000ff >> s;
#endif

	SDL_Surface *const surf = SDL_CreateRGBSurfaceFrom(data, width, height, chans * 8, pitch, rmask, gmask, bmask, amask);
	if (surf == NULL) {
		sdl_error(0);
		free(data);
		return NULL;
	}

	return surf;
}
#endif

int
TZR_DirectResourceLoad(TZR_Resource *res, const void *data, int size)
{
	switch (res->type) {
	case TZR_RES_RAW:
		res->raw.size = size;
		res->raw.data = malloc(size);
		if (res->raw.data == NULL) {
			TZR_Log("%s", strerror(errno));
			return -1;
		}
		memcpy(res->raw.data, data, size);
		break;
	case TZR_RES_IMAGE: {
#ifdef TZR_STB_IMAGE
		SDL_Surface *const surf = stbi_load_surface((void*)data, size);
		if (surf == NULL)
			return -1;
#else
		SDL_RWops *const rw = SDL_RWFromConstMem(data, size);
		if (rw == NULL)
			return sdl_error(-1);
		SDL_Surface *const surf = SDL_LoadBMP_RW(rw, 1);
		if (surf == NULL)
			return sdl_error(-1);
#endif /* TZR_STB_IMAGE */
		SDL_Texture *const tex =
		    SDL_CreateTextureFromSurface(___tzr_renderer, surf);
		SDL_FreeSurface(surf);
		if (tex == NULL)
			return sdl_error(-1);
#ifdef TZR_OPENGL
		if (___tzr_config.mipmaps) {
			TZR_Log("generating mipmaps");
			SDL_GL_BindTexture(tex, NULL, NULL);
			glGenerateMipmap(GL_TEXTURE_2D);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
			                GL_LINEAR_MIPMAP_LINEAR);
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS,
			                -.5f);
			SDL_GL_UnbindTexture(tex);
		}
#endif /* TZR_OPENGL */
		if (SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND) < 0) {
			SDL_DestroyTexture(tex);
			return sdl_error(-1);
		}
		if (SDL_QueryTexture(tex, NULL, NULL,
		                     &res->image.width, &res->image.height)) {
			SDL_DestroyTexture(tex);
			return sdl_error(-1);
		}
		res->image.ptr = tex;
	} break;
	case TZR_RES_SOUND: {
		if (!___tzr_config.mixer) {
			TZR_Log("audio mixer disabled, skip loading");
			return -1;
		}
#ifdef TZR_SOLOUD
		Wav *wav = Wav_create();
		if (wav == NULL) {
			TZR_Log("Wav_create failed");
			return -1;
		}
		if (Wav_loadMemEx(wav, data, size, true, false)) {
			TZR_Log("Wav_loadMem failed");
			Wav_destroy(wav);
			return -1;
		}
		res->sound.ptr = wav;
#else /* TZR_SOLOUD */
		SDL_RWops *const rw = SDL_RWFromConstMem(data, size);
		if (rw == NULL)
			return sdl_error(-1);
		Mix_Chunk *const chunk = Mix_LoadWAV_RW(rw, 0);
		SDL_RWclose(rw);
		if (chunk == NULL)
			return sdl_error(-1);
		res->sound.ptr = chunk;
#endif
	} break;
	default:
		TZR_Log("invalid type");
		return -1;
	}
	return 0;
}