void GfxGlEngine::drawPicture(const Picture &picture, const int dx, const int dy, Rect* clipRect) { GLuint aTextureID = picture.getGlTextureID(); float x0 = (float)( dx+picture.getOffset().getX()); float x1 = x0+picture.getWidth(); float y0 = (float)(dy-picture.getOffset().getY()); float y1 = y0+picture.getHeight(); // Bind the texture to which subsequent calls refer to glBindTexture( GL_TEXTURE_2D, aTextureID); glBegin( GL_QUADS ); //Bottom-left vertex (corner) glTexCoord2i( 0, 0 ); glVertex2f( x0, y0 ); //Bottom-right vertex (corner) glTexCoord2i( 1, 0 ); glVertex2f( x1, y0 ); //Top-right vertex (corner) glTexCoord2i( 1, 1 ); glVertex2f( x1, y1 ); //Top-left vertex (corner) glTexCoord2i( 0, 1 ); glVertex2f( x0, y1 ); glEnd(); }
void GfxGlEngine::unloadPicture(Picture &ioPicture) { const GLuint& texture = (GLuint)ioPicture.getGlTextureID(); glDeleteTextures(1, &texture ); SDL_FreeSurface(ioPicture.getSurface()); ioPicture = Picture(); }
void GfxGlEngine::loadPicture(Picture& ioPicture) { GLuint& texture(ioPicture.getGlTextureID()); SDL_Surface *surface = ioPicture.getSurface(); GLenum texture_format; GLint nOfColors; // SDL_Surface *surface2 // get the number of channels in the SDL surface nOfColors = surface->format->BytesPerPixel; if (nOfColors == 4) // contains an alpha channel { if (surface->format->Rmask == 0x000000ff) texture_format = GL_RGBA; else texture_format = GL_BGRA; } else if (nOfColors == 3) // no alpha channel { if (surface->format->Rmask == 0x000000ff) texture_format = GL_RGB; else texture_format = GL_BGR; } else { THROW("Invalid image format"); } if (texture == 0) { // the picture has no texture ID! // generate a texture ID glGenTextures( 1, &texture ); } // Bind the texture object glBindTexture( GL_TEXTURE_2D, texture ); // Set the texture's stretching properties glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); // Edit the texture object's image data using the information SDL_Surface gives us ioPicture.lock(); glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels ); ioPicture.unlock(); }