SDL trim function causes an error [solved]

Started by
2 comments, last by andrew_480 18 years, 1 month ago
Hi. I've been trying to make a wrapper for SDL, but my trim function causes an error. When I call the function it works, but when I close the application it comes up with an error message saying "SDL_Wrapper.exe has encountered a problem and needs to close. We are sorry for the inconvenience... blah blah blah". Heres the function's code:

// Trim off some of the image.
void Image::trim(Uint16 left, Uint16 top, Uint16 right, Uint16 bottom) 
throw (SDL_Exception)
{
  // Check that the image isn't empty.
  if (m_surface == NULL)
  {
    throw SDL_Exception("Can't trim an empty image.");
  }
  
  // Create an empty image of the correct size.
  SDL_Surface* trimmed = SDL_CreateRGBSurface(m_surface->flags, 
  right - left, bottom - top, m_surface->format->BitsPerPixel, 
  m_surface->format->Rmask, m_surface->format->Gmask, m_surface->format->Bmask, 
  m_surface->format->Amask);
  
  if (trimmed == NULL)
  {
    throw SDL_Exception("Couldn't create empty surface.");
  }
  
  Uint32 flags;
  Uint8 r, g, b, a;
  
  // Get the color key and alpha.
  getColorKey(r, g, b);
  a = getAlpha();
  flags = m_surface->flags;
  
  // Clear color key and alpha.
  clearColorKey();
  clearAlpha();
  
  // Find the area to save.
  SDL_Rect srcRect;
  srcRect.x = left;
  srcRect.y = top;
  srcRect.w = trimmed->w;
  srcRect.h = trimmed->h;
  
  // Draw the area to the trimmed surface.
  SDL_BlitSurface(m_surface, &srcRect, trimmed, NULL);
  
  // Set the alpha and color key of the trimmed image.
  SDL_SetAlpha(trimmed, flags, a);
  SDL_SetColorKey(trimmed, flags, SDL_MapRGB(trimmed->format, r, g, b));
  
  // Set the image to the trimmed one.
  SDL_FreeSurface(m_surface);
  m_surface = trimmed;
}



If I remove the line of code that calls this function the error message doesn't appear. [Edited by - andrew_480 on March 16, 2006 8:43:28 PM]
Advertisement
Is this code OK? I'll put my code in full up when I get home.
Maybe it's something else.
If you free the video surface that you got with SDL_Init(...) before calling SDL_Quit() will this cause an error?
Quote:Original post by andrew_480
If you free the video surface that you got with SDL_Init(...) before calling SDL_Quit() will this cause an error?


Perhaps, but that is something you should not definitly do! The first thing you need to do is find out where the error is happening at. The error you are seeing could be a result from a 'throw' or an illegal operation. So once you find that you, you can narrow down the problem. Your code looks ok (kinda tired, so if anyone sees anything point it out [wink]) other than that, so first see if you can determine where it is crashing at.
I found what was wrong. I was doing the opposite of what I thought. I called SDL_FreeSurface() inside a destructor after calling SDL_Quit().

This topic is closed to new replies.

Advertisement