I'm creating a program in which I would like to have the ability to scale textures. I created a system which can successfully do so but it ended up being quite large and so I refactored it into a function of its own. This way I can, at the beginning of run time, load the scaled image which I need and save it to use throughout the duration of the program rather than doing the process every frame. The one issue is well put simply it doesn't work. Whenever I run the program it crashes. I'm just a hobbyist and communication between functions isn't something I'm super well-versed in, I have a feeling that's where the problem lies.
// in the place where I call the scaling function in order to load it into a variable
Uint32* textureBuffer = scaleImage(texNum, textureWidth, textureHeight);
// in the scaling function
Uint32* scaleImage(int texNum, int newWidth, int newHeight) {
Uint32* scaledTextureBuffer[projectionWidth * projectionHeight];
/* other variables & scaling the image */
scaledTextureBuffer[(projectionWidth * y) + x] = texelColor;
return scaledTextureBuffer;
}
Perhaps there's something else fatally wrong with my program but I belive the exchanging of that Uint32 scaledTextureBuffer might be the issue but I'm not sure what I'm doing wrong.
Thanks for the help.
Always enable your compiler's warnings! It would have warned you that the array has the wrong type.
Fixed:
Uint32* scaleImage(int texNum, int newWidth, int newHeight) {
Uint32 scaledTextureBuffer[projectionWidth * projectionHeight];
// ...
scaledTextureBuffer[(projectionWidth * y) + x] = texelColor;
// ...
return scaledTextureBuffer;
}
But there's a bigger second problem: You're returning a pointer to an automatic object. scaledTextureBuffer
ceases to exist when scaleImage
returns, so returning a pointer to (an element of) scaledTextureBuffer
makes no sense.
To fix this, you need to allocate the array in a way that it won't be deleted automatically.
Uint32* scaleImage(int texNum, int newWidth, int newHeight) {
Uint32 *scaledTextureBuffer =
malloc( projectionWidth * projectionHeight * sizeof( Uint32 ) );
// ...
scaledTextureBuffer[(projectionWidth * y) + x] = texelColor;
// ...
return scaledTextureBuffer;
}
Uint32* scaledTextureBuffer = scaleImage(texNum, textureWidth, textureHeight);
// ...
free(scaledTextureBuffer);