#include "TZR_resource.h" #include "TZR_globals.h" #include "sdl_error.h" #include #include #include #include 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) { perror("TZR_DirectResourceLoad"); return -1; } memcpy(res->raw.data, data, size); break; case TZR_RES_IMAGE: { 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); SDL_Texture *const tex = SDL_CreateTextureFromSurface(___tzr_renderer, surf); SDL_FreeSurface(surf); if (tex == NULL) return sdl_error(-1); 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) { SDL_Log("audio mixer disabled, skip loading"); return -1; } 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; } break; default: fprintf(stderr, "invalid type\n"); return -1; } return 0; }