Im using SDL 1.3 with opengl and everythings working intill I call my rotate function which takes a opengl texture does a glrotate then grabs the pixels and i create a SDL texture from that rotated surface. I know my method is way off because its going to be slow in real time but i dont want to do it in only opengl because im making a game pack where you can select from my retro arcade and
the rest of the games are created with sdl and sdl renderer. So my problem is i have a big memleak when rotating when i free my buffer, sdl surfaces and sdl texture then rotate again i can watch my memory build up each time in the task manager. Just hoping some of the pros can help me with this leak here some of my code where it is called.
Function where i rotate
SDL_Texture *loadText(GLuint texture,SDL_Renderer *renderer)
{
SDL_Surface *newsur;
GLfloat textureWidth, textureHeight;
Uint32 rmask, gmask, bmask, amask;
/* SDL interprets each pixel as a 32-bit number, so our masks must depend
on the endianness (byte order) of the machine */
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
glGetTexLevelParameterfv(GL_TEXTURE_2D, 1, GL_TEXTURE_WIDTH, &textureWidth);
glGetTexLevelParameterfv(GL_TEXTURE_2D, 1, GL_TEXTURE_HEIGHT, &textureHeight);
GLushort *buffer = (GLushort *)malloc(textureWidth*textureHeight*4);
glReadPixels(200,150,textureWidth,textureHeight,GL_RGBA, GL_UNSIGNED_BYTE,buffer);
newsur=SDL_CreateRGBSurface(SDL_ANYFORMAT,256,256,32,rmask,gmask,bmask,amask);
newsur->pixels = buffer;
text = SDL_CreateTextureFromSurface(renderer,newsur);
if(newsur)
{
SDL_FreeSurface(newsur);
newsur = NULL;
}
if(buffer)
{
free(buffer);
buffer = NULL;
}
return text;
}
And my main
int main(argc, argv[])
{
if ( SDL_Init(SDL_INIT_VIDEO) != 0 )
{
printf("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_Window *window;
/* Set 640x480 video mode */
window = SDL_CreateWindow("Test", 0, 250, 640, 480,
SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
SDL_GLContext glcontext= SDL_GL_CreateContext(window);
renderer = SDL_CreateRenderer(window, 0, 1);
SDL_GL_MakeCurrent(window, glcontext);
init();
glEnable( GL_TEXTURE_2D );
/* Enable swap on VSYNC */
SDL_GL_SetSwapInterval(1);
GLuint texture; //This is a handle to our texture object
texture = CreateTexture("sky.bmp",NORM);
SDL_Texture *background =0;
SDL_Rect dstbg, srcbg;
dstbg.x = 0; // Sets X and Y position for image (Foreground)
dstbg.y = 0;
srcbg.x = 0;
srcbg.y = 0;
dstbg.w = 256 ;
dstbg.h = 256 ;
done = false;
while (!done)
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
// check for messages
switch (event.type)
{
// exit if the window is closed
case SDL_QUIT:
done = true;
break;
// check for keypresses
case SDL_KEYDOWN:
// exit if ESCAPE is pressed
if (event.key.keysym.sym == SDLK_ESCAPE)
done = true;
if (event.key.keysym.sym == SDLK_LEFT)
{
angle -=.75;
background = loadText(texture,renderer);
text = NULL;
}
if (event.key.keysym.sym == SDLK_RIGHT)
{
angle +=.75;
background = loadText(texture,renderer);
text = NULL;
}
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
if(background)
SDL_RenderCopy(renderer, background, 0, &dstbg);
display();
SDL_RenderPresent(renderer);
}
return 0;
}
Please any help or comments are appreciated