Reviewed ImageConvertToPOT()

This commit is contained in:
raysan5 2015-08-07 18:00:28 +02:00
parent fd2b0c7c38
commit 07858c3a1f

View file

@ -330,7 +330,6 @@ Texture2D LoadTexture(const char *fileName)
else else
{ {
TraceLog(WARNING, "Texture could not be created"); TraceLog(WARNING, "Texture could not be created");
texture.id = 0; texture.id = 0;
} }
@ -391,7 +390,8 @@ void UnloadImage(Image image)
{ {
free(image.data); 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 // Unload texture from GPU memory
@ -519,7 +519,9 @@ Image GetTextureData(Texture2D texture)
// Convert image data to desired format // Convert image data to desired format
void ImageConvertFormat(Image *image, int newFormat) void ImageConvertFormat(Image *image, int newFormat)
{ {
if ((image->format != newFormat) && (image->format < 8) && (newFormat < 8)) if (image->format != newFormat)
{
if ((image->format < 8) && (newFormat < 8))
{ {
Color *pixels = GetImageData(*image); Color *pixels = GetImageData(*image);
@ -650,14 +652,15 @@ void ImageConvertFormat(Image *image, int newFormat)
} }
else TraceLog(WARNING, "Image data format is compressed, can not be converted"); else TraceLog(WARNING, "Image data format is compressed, can not be converted");
} }
}
// Convert image to POT (power-of-two) // Convert image to POT (power-of-two)
// NOTE: Requirement on OpenGL ES 2.0 (RPI, HTML5) // NOTE: Requirement on OpenGL ES 2.0 (RPI, HTML5)
void ImageConvertToPOT(Image *image, Color fillColor) 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... // Just add the required amount of pixels at the right and bottom sides of image...
int potWidth = GetNextPOT(image->width); int potWidth = GetNextPOT(image->width);
int potHeight = GetNextPOT(image->height); 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) // Check if POT texture generation is required (if texture is not already POT)
if ((potWidth != image->width) || (potHeight != image->height)) if ((potWidth != image->width) || (potHeight != image->height))
{ {
Color *imgDataPixelPOT = NULL; Color *pixelsPOT = NULL;
// Generate POT array from NPOT data // 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 j = 0; j < potHeight; j++)
{ {
for (int i = 0; i < potWidth; i++) for (int i = 0; i < potWidth; i++)
{ {
if ((j < image->height) && (i < image->width)) imgDataPixelPOT[j*potWidth + i] = image->data[j*image->width + i]; if ((j < image->height) && (i < image->width)) pixelsPOT[j*potWidth + i] = pixels[j*image->width + i];
else imgDataPixelPOT[j*potWidth + i] = fillColor; else pixelsPOT[j*potWidth + i] = fillColor;
} }
} }
TraceLog(WARNING, "Image converted to POT: (%ix%i) -> (%ix%i)", image->width, image->height, potWidth, potHeight); 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
image->pixels = imgDataPixelPOT; int format = image->format; // Store image data format to reconvert later
image->width = potWidth;
image->height = potHeight; *image = LoadImageEx(pixelsPOT, potWidth, potHeight);
free(pixelsPOT); // Free POT pixels data
ImageConvertFormat(image, format); // Reconvert image to previous format
} }
*/
} }
// Copy an image to a new image // Copy an image to a new image