diff options
Diffstat (limited to 'prge_texture.c')
-rw-r--r-- | prge_texture.c | 50 |
1 files changed, 20 insertions, 30 deletions
diff --git a/prge_texture.c b/prge_texture.c index cce3374..e6c0820 100644 --- a/prge_texture.c +++ b/prge_texture.c @@ -1,31 +1,19 @@ -Texture load_texture(Arena *arena, char *filename, B32 gamma_correction) +Texture load_texture(Arena *arena, const char *fname, B32 gamma) { - U8 *data; + Texture texture; + U8 *data; + S32 nchannels; + GLenum internal_format; + GLenum data_format; + S32 size; - S32 nchannels; - GLenum internal_format; - GLenum data_format; - - Texture texture; - - Arena *temparena; - - char *cfilename; - Str8 str; - - Assert(arena); - - MemoryZeroStruct(&texture); - - temparena = arena_alloc(0); - - cfilename = str8tocstr(arena, filename); + MEM0STRUCT(&texture); stbi_set_flip_vertically_on_load(1); - data = stbi_load(cfilename, &texture.width, &texture.height, &nchannels, 0); + data = stbi_load(fname, &texture.w, &texture.h, &nchannels, 0); if (!data) { - str = str8pushf(temparena, "[ERROR] : Texture : %s : Failed to load\n", cfilename); + sys_printf("[ERROR] : Texture : %s : Failed to load\n", fname); goto end; } @@ -36,22 +24,27 @@ Texture load_texture(Arena *arena, char *filename, B32 gamma_correction) break; case 3: texture.type = TextureType_RGB; - internal_format = (gamma_correction) ? GL_SRGB : GL_RGB; + internal_format = (gamma) ? GL_SRGB : GL_RGB; data_format = GL_RGB; break; case 4: texture.type = TextureType_RGBA; - internal_format = (gamma_correction) ? GL_SRGB_ALPHA : GL_RGBA; + internal_format = (gamma) ? GL_SRGB_ALPHA : GL_RGBA; data_format = GL_RGBA; break; default: - str = str8pushf(temparena, "[ERROR] : Texture : %s : Unsupported type\n", cfilename); + sys_printf("[ERROR] : Texture : %s : Unsupported type\n", fname); goto end; } + size = texture.type*texture.w*texture.h; + texture.data = push_arena(arena, size); + MEMCPY(texture.data, data, size); + glGenTextures(1, &texture.id); glBindTexture(GL_TEXTURE_2D, texture.id); - glTexImage2D(GL_TEXTURE_2D, 0, internal_format, texture.width, texture.height, 0, data_format, GL_UNSIGNED_BYTE, data); + glTexImage2D(GL_TEXTURE_2D, 0, internal_format, texture.w, texture.h, + 0, data_format, GL_UNSIGNED_BYTE, texture.data); glGenerateMipmap(GL_TEXTURE_2D); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); @@ -59,11 +52,8 @@ Texture load_texture(Arena *arena, char *filename, B32 gamma_correction) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glBindTexture(GL_TEXTURE_2D, 0); - str = str8pushf(temparena, "[INFO] : Texture : %s : Loaded successfully\n", cfilename); - + sys_printf("[INFO] : Texture : %s : Loaded successfully\n", fname); end: - str8print(str); stbi_image_free(data); - arena_release(temparena); return texture; } |