bool CSurface::LoadImage(string filename, bool load) { m_Filename = filename; if (m_Filename == ButtonPath("").c_str()) return true; // ignore disabled buttons if (load == false) { Register(false); return true; } SDL_Surface* loadedImage = 0; loadedImage = IMG_Load(filename.c_str()); // Load image if (loadedImage) { m_Surface = m_UseAlpha? // Convert image to screen BPP SDL_DisplayFormatAlpha(loadedImage) : // either with alpha SDL_DisplayFormat(loadedImage); // or without alpha SDL_FreeSurface(loadedImage); // Free old surface } else { g_LogFile.write("CSurface LoadImage loadedImage error with filename: " + m_Filename); g_LogFile.write(SDL_GetError()); g_LogFile.write(IMG_GetError()); return false; } if (!m_Surface) { g_LogFile.write("CSurface LoadImage !m_Surface error with filename: " + m_Filename); g_LogFile.write(SDL_GetError()); g_LogFile.write(IMG_GetError()); return false; } if (m_UseKey && !m_UseAlpha) SDL_SetColorKey(m_Surface, SDL_SRCCOLORKEY, m_ColorKey); if (!m_Cached) Register(true); return true; }
bool CSurface::DrawSurface(int x, int y, SDL_Surface* destination, SDL_Rect* clip, bool resize, bool maintainRatio) { double scaleX = 0; double scaleY = 0; if (m_Surface == 0) { if (m_ColoredSurface == true) { g_LogFile.ss() << "ERROR - Colored surface null: '" << m_Filename << "'"; g_LogFile.ssend(); return false; } if (m_Temp) SDL_FreeSurface(m_Temp); m_Temp = 0; if (!loaded) { if (m_Filename == ButtonPath("").c_str()) // fix disabled buttons m_Filename = ImagePath("blank.png").c_str(); if (!LoadImage(m_Filename)) { g_LogFile.ss() << "ERROR - Loading Image '" << m_Filename << "'"; g_LogFile.ssend(); return false; } if (m_Cached) { loaded = true; } } } m_TimeUsed = g_Graphics.GetTicks(); if (clip && resize) { if (m_Temp == 0) { if (maintainRatio == true) { if (clip->w != m_Surface->w && clip->h != m_Surface->h) { if (m_Surface->w > m_Surface->h) // if the width is larger so scale down based on the width but keep aspect ratio scaleX = scaleY = ((double)clip->w / (double)m_Surface->w); else // assume the height is larger scaleX = scaleY = ((double)clip->h / (double)m_Surface->h); m_Temp = zoomSurface(m_Surface, scaleX, scaleY, 1); } } else { if (clip->w != m_Surface->w || clip->h != m_Surface->h) { scaleX = ((double)clip->w / (double)m_Surface->w); scaleY = ((double)clip->h / (double)m_Surface->h); m_Temp = zoomSurface(m_Surface, scaleX, scaleY, 1); } } } else { if (maintainRatio == true) { if (m_Temp->w != clip->w && m_Temp->h != clip->h) { if (m_Temp) SDL_FreeSurface(m_Temp);// free old image m_Temp = 0; if (m_Surface->w > m_Surface->h) // if the width is larger so scale down based on the width but keep aspect ratio scaleX = scaleY = ((double)clip->w / (double)m_Surface->w); else // assume the height is larger scaleX = scaleY = ((double)clip->h / (double)m_Surface->h); m_Temp = zoomSurface(m_Surface, scaleX, scaleY, 1); } } else { if (m_Temp->w != clip->w || m_Temp->h != clip->h) { if (m_Temp) SDL_FreeSurface(m_Temp); // free old image m_Temp = 0; scaleX = ((double)clip->w / (double)m_Surface->w); scaleY = ((double)clip->h / (double)m_Surface->h); m_Temp = zoomSurface(m_Surface, scaleX, scaleY, 1); } } } } // create and setup a SDL offset rectangle SDL_Rect offset; offset.x = x; offset.y = y; // Draw the source surface onto the destination int error = 0; if (destination) { error = m_Temp ? SDL_BlitSurface(m_Temp, clip, destination, &offset) : SDL_BlitSurface(m_Surface, clip, destination, &offset); } else // blit to the screen { error = m_Temp ? SDL_BlitSurface(m_Temp, clip, g_Graphics.GetScreen(), &offset) : SDL_BlitSurface(m_Surface, clip, g_Graphics.GetScreen(), &offset); } if (error == -1) { g_LogFile.ss() << "Error Blitting surface (" << m_Filename << ") - " << SDL_GetError(); g_LogFile.ssend(); return false; } return true; }
ButtonSurface::ButtonSurface (std::string name, const char* pt, const char* ext ) : CSurface( ButtonPath((name + pt) + ext).c_str() ) { }
ButtonSurface::ButtonSurface( std::string name ) : CSurface( ButtonPath(name + ".png") ) { }