63 lines
1.6 KiB
C
63 lines
1.6 KiB
C
#include "texture.h"
|
|
|
|
#include "asset_manager.h"
|
|
#include "display.h"
|
|
#include "errors.h"
|
|
|
|
texture_t *texture_load(FILE *asset_file)
|
|
{
|
|
size_t width, height;
|
|
|
|
if(fread(&width, sizeof(width), 1, asset_file) != 1)
|
|
{
|
|
error_printf("Failed to read in asset file.");
|
|
return NULL;
|
|
}
|
|
|
|
if(fread(&height, sizeof(height), 1, asset_file) != 1)
|
|
{
|
|
error_printf("Failed to read in asset file.");
|
|
return NULL;
|
|
}
|
|
|
|
SDL_Surface *tmp_surface = SDL_CreateSurface(width, height, SDL_PIXELFORMAT_RGB565);
|
|
if(!tmp_surface)
|
|
{
|
|
error_printf("Failed to create temporary surface : %s", SDL_GetError());
|
|
return NULL;
|
|
}
|
|
|
|
if(!SDL_LockSurface(tmp_surface))
|
|
{
|
|
error_printf("Failed to lock temporary surface : %s", SDL_GetError());
|
|
SDL_DestroySurface(tmp_surface);
|
|
return NULL;
|
|
}
|
|
|
|
if(fread(tmp_surface->pixels, sizeof(uint16_t), width * height, asset_file) != width * height)
|
|
{
|
|
error_printf("Failed to read asset file.");
|
|
SDL_DestroySurface(tmp_surface);
|
|
return NULL;
|
|
}
|
|
|
|
if(!SDL_SetSurfaceColorKey(tmp_surface, true, 0xF81F))
|
|
{
|
|
error_printf("Failed to set color key of temporary surface : %s", SDL_GetError());
|
|
SDL_DestroySurface(tmp_surface);
|
|
return NULL;
|
|
}
|
|
|
|
texture_t *texture = SDL_CreateTextureFromSurface(renderer, tmp_surface);
|
|
if(!texture)
|
|
{
|
|
error_printf("Failed to create texture from temporary surface : %s", SDL_GetError());
|
|
SDL_DestroySurface(tmp_surface);
|
|
return NULL;
|
|
}
|
|
|
|
SDL_DestroySurface(tmp_surface);
|
|
|
|
return texture;
|
|
}
|