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

94 lines
2.5 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-07-22 10:11:43 +00:00
const TextureFormat FORMAT_RGB = {
.glInternalFormat = GL_RGB,
.glFormat = GL_RGB,
.compressed = 0,
.blockBytes = 2
};
const TextureFormat FORMAT_RGBA = {
.glInternalFormat = GL_RGBA,
.glFormat = GL_RGBA,
.compressed = 0,
.blockBytes = 4
2017-06-19 00:28:15 +00:00
};
2017-07-22 10:14:36 +00:00
const TextureFormat FORMAT_DXT1 = {
.glInternalFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
.glFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
.compressed = 1,
.blockBytes = 8
};
const TextureFormat FORMAT_DXT3 = {
.glInternalFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,
.glFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,
.compressed = 1,
.blockBytes = 16
};
const TextureFormat FORMAT_DXT5 = {
.glInternalFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
.glFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
.compressed = 1,
.blockBytes = 16
};
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-07-22 10:11:43 +00:00
size_t size = width * height * format.blockBytes;
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-07-22 10:11:43 +00:00
int size = width * height * textureData->format.blockBytes;
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);
}