The Update(TM), Part 1
This commit is contained in:
1 parent
0439221e78
commit
fe8bdae9a8
45 files changed
+4450
-3564
No files matched your search
+11
-31
@@ -5,75 +5,55 @@
|
||||
#define STBI_NO_HDR
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>
|
||||
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
#include <stb_image_write.h>
|
||||
|
||||
// Generates GL texture and sets parameters
|
||||
void
|
||||
texture_gl_set(Texture* self, void* data)
|
||||
void texture_gl_set(Texture* self, void* data)
|
||||
{
|
||||
glGenTextures(1, &self->id);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, self->id);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, self->size.x, self->size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
// Initializes texture from path; returns true/false on success
|
||||
bool
|
||||
texture_from_path_init(Texture* self, const std::string& path)
|
||||
bool texture_from_path_init(Texture* self, const std::string& path)
|
||||
{
|
||||
void* data;
|
||||
|
||||
data = stbi_load(path.c_str(), &self->size.x, &self->size.y, &self->channels, 4);
|
||||
void* data = stbi_load(path.c_str(), &self->size.x, &self->size.y, &self->channels, TEXTURE_CHANNELS);
|
||||
|
||||
if (!data)
|
||||
{
|
||||
std::cout << STRING_ERROR_TEXTURE_INIT << path << std::endl;
|
||||
log_error(std::format(TEXTURE_INIT_ERROR, path));
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout << STRING_INFO_TEXTURE_INIT << path << std::endl;
|
||||
log_info(std::format(TEXTURE_INIT_INFO, path));
|
||||
|
||||
texture_gl_set(self, data);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Initializes texture from data; returns true/false on success
|
||||
bool
|
||||
texture_from_data_init(Texture* self, const u8* data, u32 length)
|
||||
bool texture_from_data_init(Texture* self, const u8* data, u32 length)
|
||||
{
|
||||
void* textureData;
|
||||
void* textureData = stbi_load_from_memory(data, length, &self->size.x, &self->size.y, &self->channels, TEXTURE_CHANNELS);
|
||||
|
||||
textureData = stbi_load_from_memory(data, length, &self->size.x, &self->size.y, &self->channels, 4);
|
||||
|
||||
if (!textureData)
|
||||
return false;
|
||||
if (!textureData) return false;
|
||||
|
||||
texture_gl_set(self, textureData);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Writes an image to the path from the data/size
|
||||
bool
|
||||
texture_from_data_write(const std::string& path, const u8* data, s32 width, s32 height)
|
||||
bool texture_from_data_write(const std::string& path, const u8* data, s32 width, s32 height)
|
||||
{
|
||||
return (bool)stbi_write_png(path.c_str(), width, height, 4, data, width * 4);
|
||||
return (bool)stbi_write_png(path.c_str(), width, height, TEXTURE_CHANNELS, data, width * TEXTURE_CHANNELS);
|
||||
}
|
||||
|
||||
// Frees texture
|
||||
void
|
||||
texture_free(Texture* self)
|
||||
void texture_free(Texture* self)
|
||||
{
|
||||
glDeleteTextures(1, &self->id);
|
||||
*self = Texture{};
|
||||
|
||||
Reference in new issue
Block a user