#include "libs.h" #include "gui_image.h" #include "l3d.h" namespace Gui { Image::~Image(void) { #pragma message("Warning: Leaking GL textures..") } Image::Image(const char* filename) : Widget() { SDL_Surface* is = IMG_Load(filename); if(!is) { fprintf(stderr, "Could not load %s\n", filename); L3D::Quit(); } m_imgw = is->w; m_imgh = is->h; SetSize(m_imgw, m_imgh); /* GL textures must be POT, dim > 64. */ int texw, texh; { int nbit = 0; int sz = m_imgw; while(sz) { sz >>= 1; nbit++; } texw = MAX(64, 1<>= 1; nbit++; } texh = MAX(64, 1<pixels); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glDisable(GL_TEXTURE_2D); SDL_FreeSurface(s); } void Image::GetSizeRequested(float size[2]) { size[0] = m_imgw; size[1] = m_imgh; } void Image::Draw(void) { float allocSize[2]; GetSize(allocSize); glEnable(GL_BLEND); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, m_tex); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glBegin(GL_QUADS); float w = m_imgw * m_invtexw; float h = m_imgh * m_invtexh; glTexCoord2f(0, h); glVertex2f(0, 0); glTexCoord2f(w, h); glVertex2f(allocSize[0],0); glTexCoord2f(w, 0); glVertex2f(allocSize[0], allocSize[1]); glTexCoord2f(0, 0); glVertex2f(0, allocSize[1]); glEnd(); glDisable(GL_TEXTURE_2D); glDisable(GL_BLEND); } }