From 4f8d4ab8bd84bfbe7329a1afae3034ec2937bbb5 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Mon, 1 Oct 2012 18:45:20 +0200 Subject: [PATCH] Split image loading from texture generation. --- main.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/main.cpp b/main.cpp index 4054636..256b29e 100644 --- a/main.cpp +++ b/main.cpp @@ -138,7 +138,7 @@ void mouse(int x, int y) update_hsv(); } -void load_texture(const char *filename) +unsigned char *load_image(const char *filename, unsigned *w, unsigned *h) { SDL_Surface *img = IMG_Load(filename); if (img == NULL) { @@ -177,17 +177,30 @@ void load_texture(const char *filename) } SDL_UnlockSurface(img); + *w = img->w; + *h = img->h; + + SDL_FreeSurface(img); + + return dst_pixels; +} + +void load_texture(const char *filename) +{ + unsigned w, h; + unsigned char *pixels = load_image(filename, &w, &h); + #if 1 // we will convert to sRGB in the shader - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_BYTE, dst_pixels); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels); check_error(); #else // implicit sRGB conversion in hardware - glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_BYTE, dst_pixels); + glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels); check_error(); #endif - free(dst_pixels); - SDL_FreeSurface(img); + + free(pixels); } void write_ppm(const char *filename, unsigned char *screenbuf) -- 2.39.2