1
0
Fork 0
mirror of https://github.com/bjornbytes/lovr.git synced 2024-07-22 05:33:35 +00:00
lovr/src/loaders/texture.c

72 lines
2 KiB
C
Raw Normal View History

2016-11-26 07:54:45 +00:00
#include "loaders/texture.h"
2017-03-11 10:19:33 +00:00
#include "lib/stb/stb_image.h"
2016-11-26 07:54:45 +00:00
#include <stdlib.h>
2017-01-22 01:29:20 +00:00
#include <string.h>
2016-11-26 07:54:45 +00:00
2017-06-19 00:28:15 +00:00
TextureFormatInfo lovrTextureFormats[] = {
2017-07-14 14:28:29 +00:00
{
.internalFormat = GL_RGB,
.format = GL_RGB,
2017-07-14 17:43:26 +00:00
.channels = 3
2017-07-14 14:28:29 +00:00
},
2017-06-19 00:28:15 +00:00
{
.internalFormat = GL_RGBA,
.format = GL_RGBA,
2017-07-14 17:43:26 +00:00
.channels = 4
2017-06-19 00:28:15 +00:00
}
};
2017-02-03 23:16:30 +00:00
TextureData* lovrTextureDataGetBlank(int width, int height, uint8_t value, TextureFormat format) {
2016-11-27 10:06:47 +00:00
TextureData* textureData = malloc(sizeof(TextureData));
if (!textureData) return NULL;
2017-06-19 00:28:15 +00:00
size_t size = width * height * lovrTextureFormats[format].channels * sizeof(uint8_t);
2017-01-09 05:29:16 +00:00
textureData->width = width;
textureData->height = height;
2017-02-03 23:16:30 +00:00
textureData->format = format;
2017-06-19 00:28:15 +00:00
textureData->data = malloc(size);
memset(textureData->data, value, size);
2016-11-27 10:06:47 +00:00
return textureData;
}
2017-02-06 04:30:17 +00:00
TextureData* lovrTextureDataGetEmpty(int width, int height, TextureFormat format) {
2017-01-09 06:51:43 +00:00
TextureData* textureData = malloc(sizeof(TextureData));
if (!textureData) return NULL;
textureData->width = width;
textureData->height = height;
2017-02-06 04:30:17 +00:00
textureData->format = format;
2017-06-19 00:28:15 +00:00
textureData->data = NULL;
2017-01-09 06:51:43 +00:00
return textureData;
}
2017-04-02 12:55:21 +00:00
TextureData* lovrTextureDataFromBlob(Blob* blob) {
2016-11-26 07:54:45 +00:00
TextureData* textureData = malloc(sizeof(TextureData));
if (!textureData) return NULL;
stbi_set_flip_vertically_on_load(0);
2017-06-19 00:28:15 +00:00
textureData->format = FORMAT_RGBA;
textureData->data = stbi_load_from_memory(blob->data, blob->size, &textureData->width, &textureData->height, NULL, 4);
2016-11-26 07:54:45 +00:00
2017-06-19 00:28:15 +00:00
if (!textureData->data) {
error("Could not load texture data from '%s'", blob->name);
free(textureData);
return NULL;
2016-11-26 07:54:45 +00:00
}
2016-12-01 04:32:14 +00:00
2017-06-19 00:28:15 +00:00
return textureData;
2016-11-26 07:54:45 +00:00
}
2017-02-06 04:30:17 +00:00
void lovrTextureDataResize(TextureData* textureData, int width, int height, uint8_t value) {
2017-06-19 00:28:15 +00:00
int size = sizeof(uint8_t) * width * height * lovrTextureFormats[textureData->format].channels;
2017-02-06 04:30:17 +00:00
textureData->width = width;
textureData->height = height;
textureData->data = realloc(textureData->data, size);
memset(textureData->data, value, size);
}
2017-02-19 09:54:58 +00:00
void lovrTextureDataDestroy(TextureData* textureData) {
free(textureData->data);
free(textureData);
}