From 7834a4e2fc967e88bebb168ad63c8a28eb5e44eb Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 29 Jul 2015 21:43:30 +0200 Subject: Replaced old mail by twitter user --- src/textures.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/textures.c') diff --git a/src/textures.c b/src/textures.c index b7ab1f7e..8011ce3d 100644 --- a/src/textures.c +++ b/src/textures.c @@ -8,7 +8,7 @@ * stb_image - Multiple formats image loading (JPEG, PNG, BMP, TGA, PSD, GIF, PIC) * NOTE: stb_image has been slightly modified, original library: https://github.com/nothings/stb * -* Copyright (c) 2014 Ramon Santamaria (Ray San - raysan@raysanweb.com) +* Copyright (c) 2014 Ramon Santamaria (@raysan5) * * This software is provided "as-is", without any express or implied warranty. In no event * will the authors be held liable for any damages arising from the use of this software. -- cgit v1.2.3 From a42bfa77942565b7806294c40a4d970f9c3d27df Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 5 Aug 2015 19:17:56 +0200 Subject: Added trace log for data unloading --- src/audio.c | 4 ++++ src/models.c | 2 ++ src/text.c | 4 ++++ src/textures.c | 4 ++++ 4 files changed, 14 insertions(+) (limited to 'src/textures.c') diff --git a/src/audio.c b/src/audio.c index 4b6db257..b8025ad6 100644 --- a/src/audio.c +++ b/src/audio.c @@ -449,6 +449,8 @@ void UnloadSound(Sound sound) { alDeleteSources(1, &sound.source); alDeleteBuffers(1, &sound.buffer); + + TraceLog(INFO, "Unloaded sound data"); } // Play a sound @@ -922,6 +924,8 @@ static Wave LoadOGG(char *fileName) static void UnloadWave(Wave wave) { free(wave.data); + + TraceLog(INFO, "Unloaded wave data"); } // Some required functions for audio standalone module version diff --git a/src/models.c b/src/models.c index 81f3db68..054febcf 100644 --- a/src/models.c +++ b/src/models.c @@ -1116,6 +1116,8 @@ void UnloadModel(Model model) rlDeleteBuffers(model.mesh.vboId[2]); rlDeleteVertexArrays(model.mesh.vaoId); + + TraceLog(INFO, "Unloaded model data"); } // Link a texture to a model diff --git a/src/text.c b/src/text.c index b7c78ce1..65d52f26 100644 --- a/src/text.c +++ b/src/text.c @@ -216,6 +216,8 @@ extern void UnloadDefaultFont(void) { UnloadTexture(defaultFont.texture); free(defaultFont.charSet); + + TraceLog(INFO, "Unloaded default font data"); } // Get the default font, useful to be used with extended parameters @@ -266,6 +268,8 @@ void UnloadSpriteFont(SpriteFont spriteFont) { UnloadTexture(spriteFont.texture); free(spriteFont.charSet); + + TraceLog(INFO, "Unloaded sprite font data"); } // Draw text (using default font) diff --git a/src/textures.c b/src/textures.c index 8011ce3d..d96c48f8 100644 --- a/src/textures.c +++ b/src/textures.c @@ -390,12 +390,16 @@ Texture2D LoadTextureFromImage(Image image) void UnloadImage(Image image) { free(image.data); + + TraceLog(INFO, "Unloaded image data"); } // Unload texture from GPU memory void UnloadTexture(Texture2D texture) { rlDeleteTextures(texture.id); + + TraceLog(INFO, "[TEX ID %i] Unloaded texture data", texture.id); } // Get pixel data from image in the form of Color struct array -- cgit v1.2.3 From 6da175fccbd5881535258b79a746eab3558f5473 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Fri, 7 Aug 2015 17:23:53 +0200 Subject: Reviewed GetTextureData() --- src/textures.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'src/textures.c') diff --git a/src/textures.c b/src/textures.c index d96c48f8..1f774254 100644 --- a/src/textures.c +++ b/src/textures.c @@ -490,20 +490,28 @@ Color *GetImageData(Image image) } // Get pixel data from GPU texture and return an Image +// NOTE: Compressed texture formats not supported Image GetTextureData(Texture2D texture) { Image image; + image.data = NULL; - image.data = rlglReadTexturePixels(texture.id, texture.format); - - if (image.data != NULL) + if (texture.format < 8) { - image.width = texture.width; - image.height = texture.height; - image.format = texture.format; - image.mipmaps = 1; + image.data = rlglReadTexturePixels(texture.id, texture.format); + + if (image.data != NULL) + { + image.width = texture.width; + image.height = texture.height; + image.format = texture.format; + image.mipmaps = 1; + + TraceLog(INFO, "Texture pixel data obtained successfully"); + } + else TraceLog(WARNING, "Texture pixel data could not be obtained"); } - else TraceLog(WARNING, "Texture pixel data could not be obtained"); + else TraceLog(WARNING, "Compressed texture data could not be obtained"); return image; } -- cgit v1.2.3 From 07858c3a1fc9d73ec9a218d0b61beee73bbaff4c Mon Sep 17 00:00:00 2001 From: raysan5 Date: Fri, 7 Aug 2015 18:00:28 +0200 Subject: Reviewed ImageConvertToPOT() --- src/textures.c | 251 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 129 insertions(+), 122 deletions(-) (limited to 'src/textures.c') diff --git a/src/textures.c b/src/textures.c index 1f774254..69456701 100644 --- a/src/textures.c +++ b/src/textures.c @@ -330,7 +330,6 @@ Texture2D LoadTexture(const char *fileName) else { TraceLog(WARNING, "Texture could not be created"); - texture.id = 0; } @@ -391,7 +390,8 @@ void UnloadImage(Image image) { free(image.data); - TraceLog(INFO, "Unloaded image data"); + // NOTE: It becomes anoying every time a texture is loaded + //TraceLog(INFO, "Unloaded image data"); } // Unload texture from GPU memory @@ -519,136 +519,139 @@ Image GetTextureData(Texture2D texture) // Convert image data to desired format void ImageConvertFormat(Image *image, int newFormat) { - if ((image->format != newFormat) && (image->format < 8) && (newFormat < 8)) + if (image->format != newFormat) { - Color *pixels = GetImageData(*image); - - free(image->data); - - image->format = newFormat; - - int k = 0; - - switch (image->format) + if ((image->format < 8) && (newFormat < 8)) { - case UNCOMPRESSED_GRAYSCALE: + Color *pixels = GetImageData(*image); + + free(image->data); + + image->format = newFormat; + + int k = 0; + + switch (image->format) { - image->data = (unsigned char *)malloc(image->width*image->height*sizeof(unsigned char)); - - for (int i = 0; i < image->width*image->height; i++) + case UNCOMPRESSED_GRAYSCALE: { - ((unsigned char *)image->data)[i] = (unsigned char)((float)pixels[k].r*0.299f + (float)pixels[k].g*0.587f + (float)pixels[k].b*0.114f); - k++; - } - - } break; - case UNCOMPRESSED_GRAY_ALPHA: - { - image->data = (unsigned char *)malloc(image->width*image->height*2*sizeof(unsigned char)); - - for (int i = 0; i < image->width*image->height*2; i += 2) + image->data = (unsigned char *)malloc(image->width*image->height*sizeof(unsigned char)); + + for (int i = 0; i < image->width*image->height; i++) + { + ((unsigned char *)image->data)[i] = (unsigned char)((float)pixels[k].r*0.299f + (float)pixels[k].g*0.587f + (float)pixels[k].b*0.114f); + k++; + } + + } break; + case UNCOMPRESSED_GRAY_ALPHA: { - ((unsigned char *)image->data)[i] = (unsigned char)((float)pixels[k].r*0.299f + (float)pixels[k].g*0.587f + (float)pixels[k].b*0.114f); - ((unsigned char *)image->data)[i + 1] = pixels[k].a; - k++; - } + image->data = (unsigned char *)malloc(image->width*image->height*2*sizeof(unsigned char)); + + for (int i = 0; i < image->width*image->height*2; i += 2) + { + ((unsigned char *)image->data)[i] = (unsigned char)((float)pixels[k].r*0.299f + (float)pixels[k].g*0.587f + (float)pixels[k].b*0.114f); + ((unsigned char *)image->data)[i + 1] = pixels[k].a; + k++; + } - } break; - case UNCOMPRESSED_R5G6B5: - { - image->data = (unsigned short *)malloc(image->width*image->height*sizeof(unsigned short)); - - unsigned char r; - unsigned char g; - unsigned char b; - - for (int i = 0; i < image->width*image->height; i++) + } break; + case UNCOMPRESSED_R5G6B5: { - r = (unsigned char)(round((float)pixels[k].r*31/255)); - g = (unsigned char)(round((float)pixels[k].g*63/255)); - b = (unsigned char)(round((float)pixels[k].b*31/255)); + image->data = (unsigned short *)malloc(image->width*image->height*sizeof(unsigned short)); + + unsigned char r; + unsigned char g; + unsigned char b; - ((unsigned short *)image->data)[i] = (unsigned short)r << 11 | (unsigned short)g << 5 | (unsigned short)b; + for (int i = 0; i < image->width*image->height; i++) + { + r = (unsigned char)(round((float)pixels[k].r*31/255)); + g = (unsigned char)(round((float)pixels[k].g*63/255)); + b = (unsigned char)(round((float)pixels[k].b*31/255)); + + ((unsigned short *)image->data)[i] = (unsigned short)r << 11 | (unsigned short)g << 5 | (unsigned short)b; - k++; - } + k++; + } - } break; - case UNCOMPRESSED_R8G8B8: - { - image->data = (unsigned char *)malloc(image->width*image->height*3*sizeof(unsigned char)); - - for (int i = 0; i < image->width*image->height*3; i += 3) + } break; + case UNCOMPRESSED_R8G8B8: { - ((unsigned char *)image->data)[i] = pixels[k].r; - ((unsigned char *)image->data)[i + 1] = pixels[k].g; - ((unsigned char *)image->data)[i + 2] = pixels[k].b; - k++; - } - } break; - case UNCOMPRESSED_R5G5B5A1: - { - image->data = (unsigned short *)malloc(image->width*image->height*sizeof(unsigned short)); - - unsigned char r; - unsigned char g; - unsigned char b; - unsigned char a = 1; - - for (int i = 0; i < image->width*image->height; i++) + image->data = (unsigned char *)malloc(image->width*image->height*3*sizeof(unsigned char)); + + for (int i = 0; i < image->width*image->height*3; i += 3) + { + ((unsigned char *)image->data)[i] = pixels[k].r; + ((unsigned char *)image->data)[i + 1] = pixels[k].g; + ((unsigned char *)image->data)[i + 2] = pixels[k].b; + k++; + } + } break; + case UNCOMPRESSED_R5G5B5A1: { - r = (unsigned char)(round((float)pixels[k].r*31/255)); - g = (unsigned char)(round((float)pixels[k].g*31/255)); - b = (unsigned char)(round((float)pixels[k].b*31/255)); - a = (pixels[k].a > 50) ? 1 : 0; + image->data = (unsigned short *)malloc(image->width*image->height*sizeof(unsigned short)); + + unsigned char r; + unsigned char g; + unsigned char b; + unsigned char a = 1; - ((unsigned short *)image->data)[i] = (unsigned short)r << 11 | (unsigned short)g << 6 | (unsigned short)b << 1| (unsigned short)a; + for (int i = 0; i < image->width*image->height; i++) + { + r = (unsigned char)(round((float)pixels[k].r*31/255)); + g = (unsigned char)(round((float)pixels[k].g*31/255)); + b = (unsigned char)(round((float)pixels[k].b*31/255)); + a = (pixels[k].a > 50) ? 1 : 0; + + ((unsigned short *)image->data)[i] = (unsigned short)r << 11 | (unsigned short)g << 6 | (unsigned short)b << 1| (unsigned short)a; - k++; - } + k++; + } - } break; - case UNCOMPRESSED_R4G4B4A4: - { - image->data = (unsigned short *)malloc(image->width*image->height*sizeof(unsigned short)); - - unsigned char r; - unsigned char g; - unsigned char b; - unsigned char a; - - for (int i = 0; i < image->width*image->height; i++) + } break; + case UNCOMPRESSED_R4G4B4A4: { - r = (unsigned char)(round((float)pixels[k].r*15/255)); - g = (unsigned char)(round((float)pixels[k].g*15/255)); - b = (unsigned char)(round((float)pixels[k].b*15/255)); - a = (unsigned char)(round((float)pixels[k].a*15/255)); + image->data = (unsigned short *)malloc(image->width*image->height*sizeof(unsigned short)); + + unsigned char r; + unsigned char g; + unsigned char b; + unsigned char a; - ((unsigned short *)image->data)[i] = (unsigned short)r << 12 | (unsigned short)g << 8| (unsigned short)b << 4| (unsigned short)a; + for (int i = 0; i < image->width*image->height; i++) + { + r = (unsigned char)(round((float)pixels[k].r*15/255)); + g = (unsigned char)(round((float)pixels[k].g*15/255)); + b = (unsigned char)(round((float)pixels[k].b*15/255)); + a = (unsigned char)(round((float)pixels[k].a*15/255)); + + ((unsigned short *)image->data)[i] = (unsigned short)r << 12 | (unsigned short)g << 8| (unsigned short)b << 4| (unsigned short)a; - k++; - } - - } break; - case UNCOMPRESSED_R8G8B8A8: - { - image->data = (unsigned char *)malloc(image->width*image->height*4*sizeof(unsigned char)); - - for (int i = 0; i < image->width*image->height*4; i += 4) + k++; + } + + } break; + case UNCOMPRESSED_R8G8B8A8: { - ((unsigned char *)image->data)[i] = pixels[k].r; - ((unsigned char *)image->data)[i + 1] = pixels[k].g; - ((unsigned char *)image->data)[i + 2] = pixels[k].b; - ((unsigned char *)image->data)[i + 3] = pixels[k].a; - k++; - } - } break; - default: break; + image->data = (unsigned char *)malloc(image->width*image->height*4*sizeof(unsigned char)); + + for (int i = 0; i < image->width*image->height*4; i += 4) + { + ((unsigned char *)image->data)[i] = pixels[k].r; + ((unsigned char *)image->data)[i + 1] = pixels[k].g; + ((unsigned char *)image->data)[i + 2] = pixels[k].b; + ((unsigned char *)image->data)[i + 3] = pixels[k].a; + k++; + } + } break; + default: break; + } + + free(pixels); } - - free(pixels); + else TraceLog(WARNING, "Image data format is compressed, can not be converted"); } - else TraceLog(WARNING, "Image data format is compressed, can not be converted"); } @@ -656,8 +659,8 @@ void ImageConvertFormat(Image *image, int newFormat) // NOTE: Requirement on OpenGL ES 2.0 (RPI, HTML5) void ImageConvertToPOT(Image *image, Color fillColor) { - // TODO: Review for new image struct - /* + Color *pixels = GetImageData(*image); // Get pixels data + // Just add the required amount of pixels at the right and bottom sides of image... int potWidth = GetNextPOT(image->width); int potHeight = GetNextPOT(image->height); @@ -665,29 +668,33 @@ void ImageConvertToPOT(Image *image, Color fillColor) // Check if POT texture generation is required (if texture is not already POT) if ((potWidth != image->width) || (potHeight != image->height)) { - Color *imgDataPixelPOT = NULL; + Color *pixelsPOT = NULL; // Generate POT array from NPOT data - imgDataPixelPOT = (Color *)malloc(potWidth * potHeight * sizeof(Color)); + pixelsPOT = (Color *)malloc(potWidth * potHeight * sizeof(Color)); for (int j = 0; j < potHeight; j++) { for (int i = 0; i < potWidth; i++) { - if ((j < image->height) && (i < image->width)) imgDataPixelPOT[j*potWidth + i] = image->data[j*image->width + i]; - else imgDataPixelPOT[j*potWidth + i] = fillColor; + if ((j < image->height) && (i < image->width)) pixelsPOT[j*potWidth + i] = pixels[j*image->width + i]; + else pixelsPOT[j*potWidth + i] = fillColor; } } TraceLog(WARNING, "Image converted to POT: (%ix%i) -> (%ix%i)", image->width, image->height, potWidth, potHeight); - free(image->pixels); + free(pixels); // Free pixels data + free(image->data); // Free old image data + + int format = image->format; // Store image data format to reconvert later + + *image = LoadImageEx(pixelsPOT, potWidth, potHeight); + + free(pixelsPOT); // Free POT pixels data - image->pixels = imgDataPixelPOT; - image->width = potWidth; - image->height = potHeight; + ImageConvertFormat(image, format); // Reconvert image to previous format } - */ } // Copy an image to a new image -- cgit v1.2.3