   [SDL] Cropping Images goes wrong [Solved]
  Hi guys,I didn't knew where to place this topic, since it can be either general programming, game programming or Alternative Game Libraries. But since I am using SDL as the subsystem for the application, i'll place it here.Im trying to make a simple Tetris like game, called Bloxx. Therefore I made some nice classes for my program: Application Class, State Manager class, Image class. Everything works well.Now for the menu I have some images in tga a file called "menu.tga". The strings "New Game", "Highscores" and "Quit" are stored in this image two times. One time for the inactive display (not selected) and one time active (selected).To load the images I have a CropPart function added to my Image class. This function is very simple://------------------------------------------------------// CropPart://  Returns a small piece of the image//------------------------------------------------------SDL_Surface* AImage::CropPart(int nX, int nY, int nWidth, int nHeight){  SDL_Surface  *pCroppedPart;  SDL_Rectpart;  SDL_Rectdest;    part.x = nX;  part.y = nY;  part.w = nWidth;  part.h = nHeight;    dest.x = 0;  dest.y = 0;    // I dont know if there is a better way to do this?  pCroppedPart = SDL_CreateRGBSurface(m_pImage->flags, nWidth, nHeight,    m_pImage->format->BitsPerPixel,    m_pImage->format->Rmask,   m_pImage->format->Gmask,   m_pImage->format->Bmask,   m_pImage->format->Amask );   SDL_BlitSurface(m_pImage, &#8706;, pCroppedPart, &dest);    return pCroppedPart;}The code works. Since the images can either be active or inactive i'll store them into an array:  AImage *m_pImgNewGame[1];  AImage *m_pImgHighscores[1];  AImage *m_pImgQuit[1];And i'll load the like this:bool MainMenu::LoadGraphics(){  m_pImgBlox  = new AImage();  m_pImgMenu  = new AImage();    m_pImgBlox->Load("data/bloxx.tga");  m_pImgMenu->Load("data/menu.tga");    // Disabled Menu Choices  m_pImgNewGame[0]    = new AImage(m_pImgMenu->CropPart(0, 0, 171, 15));   m_pImgHighscores[0] = new AImage(m_pImgMenu->CropPart(0, 18, 200, 15));  m_pImgQuit[0]  = new AImage(m_pImgMenu->CropPart(0, 35, 67, 17));  // Enabled Menu Choices  m_pImgNewGame[1]    = new AImage(m_pImgMenu->CropPart(209, 0, 171, 15));   m_pImgHighscores[1] = new AImage(m_pImgMenu->CropPart(209, 18, 200, 15));  m_pImgQuit[1]  = new AImage(m_pImgMenu->CropPart(208, 35, 67, 15)); return true;}And I view them like this:void MainMenu::Draw(){  int nWindowCenterX = GetApp()->GetWindowWidth() / 2;  int nWindowCenterY = GetApp()->GetWindowHeight() / 2;    m_pImgBlox->Draw(GetApp()->GetMainSurface(), (nWindowCenterX - (m_pImgBlox->GetWidth() / 2)), 25);    // Menu Items  m_pImgNewGame[0]->Draw(GetApp()->GetMainSurface(), (nWindowCenterX - (m_pImgNewGame[0]->GetWidth() / 2)), 200);    m_pImgHighscores[0]->Draw(GetApp()->GetMainSurface(), (nWindowCenterX - (m_pImgHighscores[0]->GetWidth() / 2)), 225);   m_pImgQuit[0]->Draw(GetApp()->GetMainSurface(), (nWindowCenterX - (m_pImgQuit[0]->GetWidth() / 2)), 250); }The AImage::Draw function looks like this:void AImage::Draw(SDL_Surface *pSurface, int nX, int nY){ SDL_Rect dest;  dest.x = nX; dest.y = nY;  // GetSurface returns m_pImage, which is an SDL_Surface SDL_BlitSurface(GetSurface(), NULL, pSurface, &dest);}Now, the problem is: instead of drawing the inactive menu items it draws the images like this:New Game [0]New Game [1]Highscores [1]Where 0 means that the inactive version is drawn, and 1 means the active version is drawn. Of course, this has to be this:New Game[0]Highscores[0]Quit[0]Any Idea what I am doing wrong? :s I've searched for an answer until 4:00AM last night... :([Edited by - AuC on December 15, 2005 8:34:00 AM]
  shouldnt the size of these arrays be 2?  AImage *m_pImgNewGame[1];  AImage *m_pImgHighscores[1];  AImage *m_pImgQuit[1];so this will happenm_pImgNewGame[0]    = new AImage(m_pImgMenu->CropPart(0, 0, 171, 15));   m_pImgHighscores[0] = new AImage(m_pImgMenu->CropPart(0, 18, 200, 15));  m_pImgQuit[0]  = new AImage(m_pImgMenu->CropPart(0, 35, 67, 17));  // Enabled Menu Choices  m_pImgNewGame[1]    = new AImage(m_pImgMenu->CropPart(209, 0, 171, 15));   m_pImgHighscores[1] = new AImage(m_pImgMenu->CropPart(209, 18, 200, 15));  m_pImgQuit[1]  = new AImage(m_pImgMenu->CropPart(208, 35, 67, 15));m_pImgNewGame[1] = ...will overwrite m_pImgHighscores[0]and m_pImgHighscores[1]...will overwrite m_pImgQuit[0]you are unlucky enough that you are not writing out of bounds, so that a segfault never alerted you to this...also there is this functionQuote:SDL_Surface *SDL_CreateRGBSurfaceFrom(void *pixels, int width, int height, int depth, int pitch, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask);it can do what you're trying to do without copying the data multiple times.pitch is something like surface->pitch or surface->format->pitch...;  Ooh, stupid... -_- Have made that mistake many times :sThank you! I'll try that function too ^.^   