X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fvideo%2Fsurface.cpp;h=23ff693897742dcd1ebb6fb27604f265d7b58fe4;hb=394afb555fd97f21a5d7bb473c749535e01df085;hp=3121291193440a16f5b16c34e98fde6e59c3a8cb;hpb=5069d8fa90db33fd5f68540eda111f1fcf5f2d25;p=supertux.git diff --git a/src/video/surface.cpp b/src/video/surface.cpp index 312129119..23ff69389 100644 --- a/src/video/surface.cpp +++ b/src/video/surface.cpp @@ -1,7 +1,7 @@ -// $Id: surface.cpp 2175 2004-11-24 19:02:49Z sik0fewl $ +// $Id$ // // SuperTux -// Copyright (C) 2004 Tobias Glaesser +// Copyright (C) 2006 Matthias Braun // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License @@ -15,8 +15,8 @@ // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA -// 02111-1307, USA. +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + #include #include @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -31,753 +32,200 @@ #include "gameconfig.hpp" #include "physfs/physfs_sdl.hpp" #include "video/surface.hpp" -#include "video/screen.hpp" - -Surface::Surfaces Surface::surfaces; - -extern SDL_Surface* screen; - -SurfaceData::SurfaceData(SDL_Surface* temp, bool use_alpha_) - : type(SURFACE), surface(0), use_alpha(use_alpha_), - x(0), y(0), w(0), h(0) -{ - // Copy the given surface and make sure that it is not stored in - // video memory - surface = SDL_CreateRGBSurface(temp->flags & (~SDL_HWSURFACE), - temp->w, temp->h, - temp->format->BitsPerPixel, - temp->format->Rmask, - temp->format->Gmask, - temp->format->Bmask, - temp->format->Amask); - if(!surface) - throw std::runtime_error("No memory left for surface"); - - SDL_SetAlpha(temp,0,0); - SDL_BlitSurface(temp, NULL, surface, NULL); -} - -SurfaceData::SurfaceData(const std::string& file_, bool use_alpha_) - : type(LOAD), surface(0), file(file_), use_alpha(use_alpha_) -{} - -SurfaceData::SurfaceData(const std::string& file_, int x_, int y_, - int w_, int h_, bool use_alpha_) - : type(LOAD_PART), surface(0), file(file_), use_alpha(use_alpha_), - x(x_), y(y_), w(w_), h(h_) -{} - -SurfaceData::SurfaceData(Color top_gradient_, Color bottom_gradient_, - int w_, int h_) - : type(GRADIENT), surface(0), use_alpha(false), w(w_), h(h_) -{ - top_gradient = top_gradient_; - bottom_gradient = bottom_gradient_; -} - +#include "video/drawing_context.hpp" +#include "video/color.hpp" +#include "image_texture.hpp" +#include "texture_manager.hpp" -SurfaceData::~SurfaceData() +Surface::Surface(const std::string& file) { - SDL_FreeSurface(surface); -} + texture = texture_manager->get(file); + texture->ref(); + uv_left = 0; + uv_top = 0; + uv_right = texture->get_uv_right(); + uv_bottom = texture->get_uv_bottom(); -SurfaceImpl* -SurfaceData::create() -{ - return create_SurfaceOpenGL(); -} - -SurfaceOpenGL* -SurfaceData::create_SurfaceOpenGL() -{ - switch(type) - { - case LOAD: - return new SurfaceOpenGL(file); - case LOAD_PART: - return new SurfaceOpenGL(file, x, y, w, h); - case SURFACE: - return new SurfaceOpenGL(surface); - case GRADIENT: - return new SurfaceOpenGL(top_gradient, bottom_gradient, w, h); - default: - assert(false); - } + width = texture->get_image_width(); + height = texture->get_image_height(); } -/* Quick utility function for texture creation */ -static int power_of_two(int input) +Surface::Surface(const std::string& file, int x, int y, int w, int h) { - int value = 1; + texture = texture_manager->get(file); + texture->ref(); - while ( value < input ) - { - value <<= 1; - } - return value; -} + float tex_w = static_cast (texture->get_width()); + float tex_h = static_cast (texture->get_height()); + uv_left = static_cast(x) / tex_w; + uv_top = static_cast(y) / tex_h; + uv_right = static_cast(x+w) / tex_w; + uv_bottom = static_cast(y+h) / tex_h; -Surface::Surface(SDL_Surface* surf, bool use_alpha) - : impl(0), data(surf, use_alpha), w(0), h(0) -{ - impl = data.create(); - if (impl) - { - w = impl->w; - h = impl->h; - } - surfaces.push_back(this); + width = w; + height = h; } -Surface::Surface(const std::string& file, bool use_alpha) - : impl(0), data(file, use_alpha), w(0), h(0) +Surface::Surface(const Surface& other) { - impl = data.create(); - if (impl) - { - w = impl->w; - h = impl->h; - } - surfaces.push_back(this); -} - -Surface::Surface(const std::string& file, int x, int y, int w_, int h_, bool use_alpha) - : impl(0), data(file, x, y, w_, h_, use_alpha), w(0), h(0) -{ - impl = data.create(); - if (impl) - { - w = impl->w; - h = impl->h; - } - surfaces.push_back(this); -} + texture = other.texture; + texture->ref(); -Surface::Surface(Color top_background, Color bottom_background, int w_, int h_) - : impl(0), data(top_background, bottom_background, w_, h_), w(0), h(0) -{ - impl = data.create(); - if (impl) - { - w = impl->w; - h = impl->h; - } - surfaces.push_back(this); + uv_left = other.uv_left; + uv_top = other.uv_top; + uv_right = other.uv_right; + uv_bottom = other.uv_bottom; + width = other.width; + height = other.height; } -void -Surface::reload() +const Surface& +Surface::operator= (const Surface& other) { - delete impl; - impl = data.create(); - if (impl) - { - w = impl->w; - h = impl->h; - for(std::vector::iterator i = - data.applied_filters.begin(); i != data.applied_filters.end(); - i++) - impl->apply_filter(i->type, i->color); - } -} + other.texture->ref(); + texture->unref(); + texture = other.texture; -void Surface::apply_filter(int filter, Color color) -{ - impl->apply_filter(filter, color); + uv_left = other.uv_left; + uv_top = other.uv_top; + uv_right = other.uv_right; + uv_bottom = other.uv_bottom; + width = other.width; + height = other.height; - SurfaceData::Filter apply_filter; - apply_filter.type = filter; - apply_filter.color = color; - data.applied_filters.push_back(apply_filter); + return *this; } Surface::~Surface() { -#ifdef DEBUG - bool found = false; - for(std::list::iterator i = surfaces.begin(); i != surfaces.end(); - ++i) - { - if(*i == this) - { - found = true; break; - } - } - if(!found) - printf("Error: Surface freed twice!!!\n"); -#endif - surfaces.remove(this); - delete impl; -} - -void -Surface::reload_all() -{ - for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i) - { - (*i)->reload(); - } -} - -void -Surface::debug_check() -{ - for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i) - { - printf("Surface not freed: T:%d F:%s.\n", (*i)->data.type, - (*i)->data.file.c_str()); - } + texture->unref(); } void -apply_filter_to_surface(SDL_Surface* surface, int filter, Color color) +Surface::hflip() { - if(filter == HORIZONTAL_FLIP_FILTER) { - SDL_Surface* sur_copy = sdl_surface_from_sdl_surface(surface); - SDL_BlitSurface(surface, NULL, sur_copy, NULL); - SDL_SetAlpha(sur_copy,0,0); - - SDL_Rect src, dst; - src.y = dst.y = 0; - src.w = dst.w = 1; - src.h = dst.h = sur_copy->h; - for(int x = 0; x < sur_copy->w; x++) - { - src.x = x; dst.x = sur_copy->w-1 - x; - SDL_BlitSurface(sur_copy, &src, surface, &dst); - } - - SDL_FreeSurface(sur_copy); - } else if(filter == MASK_FILTER) { - SDL_Surface* sur_copy = sdl_surface_from_sdl_surface(surface); - - Uint8 r,g,b,a; - - SDL_LockSurface(sur_copy); - for(int x = 0; x < sur_copy->w; x++) - for(int y = 0; y < sur_copy->h; y++) { - SDL_GetRGBA(getpixel(sur_copy,x,y), sur_copy->format, &r,&g,&b,&a); - if(a != 0) { - putpixel(sur_copy, x,y, color.map_rgba(sur_copy)); - } - } - SDL_UnlockSurface(sur_copy); - - SDL_BlitSurface(sur_copy, NULL, surface, NULL); - SDL_FreeSurface(sur_copy); - } -} - -SDL_Surface* -sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h) -{ - SDL_Rect src; - SDL_Surface * sdl_surface; - SDL_Surface * temp; - SDL_Surface * conv; - - temp = IMG_Load_RW(get_physfs_SDLRWops(file), true); - if (temp == 0) { - std::stringstream msg; - msg << "Couldn't load '" << file << "': " << SDL_GetError(); - throw std::runtime_error(msg.str()); - } - - /* Set source rectangle for conv: */ - - src.x = x; - src.y = y; - src.w = w; - src.h = h; - - conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, temp->format->BitsPerPixel, - temp->format->Rmask, - temp->format->Gmask, - temp->format->Bmask, - temp->format->Amask); - - SDL_SetAlpha(temp,0,0); - - SDL_BlitSurface(temp, &src, conv, NULL); - sdl_surface = SDL_DisplayFormatAlpha(conv); - - if (sdl_surface == NULL) { - std::stringstream msg; - msg << "Can't convert file '" << file << "' to display format."; - throw std::runtime_error(msg.str()); - } - - SDL_FreeSurface(temp); - SDL_FreeSurface(conv); - - return sdl_surface; + std::swap(uv_left, uv_right); } -SDL_Surface* -sdl_surface_from_file(const std::string& file) +static inline void intern_draw(float left, float top, float right, float bottom, float uv_left, float uv_top, + float uv_right, float uv_bottom, + DrawingEffect effect) { - SDL_Surface* sdl_surface; - SDL_Surface* temp; - - temp = IMG_Load_RW(get_physfs_SDLRWops(file), true); - if (temp == 0) { - std::stringstream msg; - msg << "Couldn't load file '" << file << "': " << SDL_GetError(); - throw std::runtime_error(msg.str()); + if(effect & HORIZONTAL_FLIP) + std::swap(uv_left, uv_right); + if(effect & VERTICAL_FLIP) { + std::swap(uv_top, uv_bottom); } - sdl_surface = SDL_DisplayFormatAlpha(temp); - - if (sdl_surface == NULL) { - std::stringstream msg; - msg << "Couldn't convert file '" << file << "' to display format"; - throw std::runtime_error(msg.str()); - } - - SDL_FreeSurface(temp); + glBegin(GL_QUADS); + glTexCoord2f(uv_left, uv_top); + glVertex2f(left, top); - return sdl_surface; -} + glTexCoord2f(uv_right, uv_top); + glVertex2f(right, top); -SDL_Surface* -sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf) -{ - SDL_Surface* sdl_surface = SDL_DisplayFormatAlpha(sdl_surf); - if (sdl_surface == 0) { - std::stringstream msg; - msg << "Can't convert surface to display format."; - throw std::runtime_error(msg.str()); - } + glTexCoord2f(uv_right, uv_bottom); + glVertex2f(right, bottom); - return sdl_surface; + glTexCoord2f(uv_left, uv_bottom); + glVertex2f(left, bottom); + glEnd(); } -SDL_Surface* -sdl_surface_from_gradient(Color top, Color bottom, int w, int h) +static inline void intern_draw2(float left, float top, float right, float bottom, + float uv_left, float uv_top, + float uv_right, float uv_bottom, + float angle, + const Color& color, + const Blend& blend, + DrawingEffect effect) { - SDL_Surface* sdl_surface - = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, - screen->format->BitsPerPixel, screen->format->Rmask, - screen->format->Gmask, screen->format->Bmask, 0); - - if(sdl_surface == 0) - throw std::runtime_error("Can't create gradient surface"); - - if(top == bottom) { - SDL_FillRect(sdl_surface, NULL, SDL_MapRGB(sdl_surface->format, - top.red, top.green, top.blue)); - } else { - float redstep = (float(bottom.red)-float(top.red)) / float(h); - float greenstep = (float(bottom.green)-float(top.green)) / float(h); - float bluestep = (float(bottom.blue) - float(top.blue)) / float(h); - - SDL_Rect rect; - rect.x = 0; - rect.w = w; - rect.h = 1; - for(float y = 0; y < h; y++) { - rect.y = (int)y; - SDL_FillRect(sdl_surface, &rect, SDL_MapRGB(sdl_surface->format, - int(float(top.red) + redstep * y), - int(float(top.green) + greenstep * y), - int(float(top.blue) + bluestep * y))); - } + if(effect & HORIZONTAL_FLIP) + std::swap(uv_left, uv_right); + if(effect & VERTICAL_FLIP) { + std::swap(uv_top, uv_bottom); } - return sdl_surface; -} - -//--------------------------------------------------------------------------- - -SurfaceImpl::SurfaceImpl() - : sdl_surface(0) -{} - -SurfaceImpl::~SurfaceImpl() -{ - if(sdl_surface != 0) - SDL_FreeSurface(sdl_surface); -} + float center_x = (left + right) / 2; + float center_y = (top + bottom) / 2; -SDL_Surface* SurfaceImpl::get_sdl_surface() const -{ - return sdl_surface; -} + float sa = sinf(angle/180.0f*M_PI); + float ca = cosf(angle/180.0f*M_PI); -SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf) -{ - sdl_surface = sdl_surface_from_sdl_surface(surf); - create_gl(sdl_surface,&gl_texture); + left -= center_x; + right -= center_x; - w = sdl_surface->w; - h = sdl_surface->h; -} + top -= center_y; + bottom -= center_y; -SurfaceOpenGL::SurfaceOpenGL(const std::string& file) -{ - sdl_surface = sdl_surface_from_file(file); - create_gl(sdl_surface,&gl_texture); + glBlendFunc(blend.sfactor, blend.dfactor); + glColor4f(color.red, color.green, color.blue, color.alpha); + glBegin(GL_QUADS); + glTexCoord2f(uv_left, uv_top); + glVertex2f(left*ca - top*sa + center_x, + left*sa + top*ca + center_y); - w = sdl_surface->w; - h = sdl_surface->h; -} + glTexCoord2f(uv_right, uv_top); + glVertex2f(right*ca - top*sa + center_x, + right*sa + top*ca + center_y); -SurfaceOpenGL::SurfaceOpenGL(const std::string& file_, int x_, int y_, - int w_, int h_) -{ - sdl_surface = sdl_surface_part_from_file(file_, x_, y_, w_, h_); - - create_gl(sdl_surface, &gl_texture); - w = sdl_surface->w; - h = sdl_surface->h; -} + glTexCoord2f(uv_right, uv_bottom); + glVertex2f(right*ca - bottom*sa + center_x, + right*sa + bottom*ca + center_y); -SurfaceOpenGL::SurfaceOpenGL(Color top_gradient, Color bottom_gradient, - int _w, int _h) -{ - sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient,_w,_h); - create_gl(sdl_surface, &gl_texture); - w = sdl_surface->w; - h = sdl_surface->h; -} + glTexCoord2f(uv_left, uv_bottom); + glVertex2f(left*ca - bottom*sa + center_x, + left*sa + bottom*ca + center_y); + glEnd(); -SurfaceOpenGL::~SurfaceOpenGL() -{ - glDeleteTextures(1, &gl_texture); + // FIXME: find a better way to restore the blend mode + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } void -SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex) -{ - Uint32 saved_flags; - Uint8 saved_alpha; - int w, h; - SDL_Surface *conv; - - w = power_of_two(surf->w); - h = power_of_two(surf->h), - -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel, - 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff); -#else - conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel, - 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000); -#endif - - /* Save the alpha blending attributes */ - saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); - saved_alpha = surf->format->alpha; - if ( (saved_flags & SDL_SRCALPHA) - == SDL_SRCALPHA ) - { - SDL_SetAlpha(surf, 0, 0); - } - - SDL_BlitSurface(surf, 0, conv, 0); - - /* Restore the alpha blending attributes */ - if ( (saved_flags & SDL_SRCALPHA) - == SDL_SRCALPHA ) - { - SDL_SetAlpha(surf, saved_flags, saved_alpha); - } - - // We check all the pixels of the surface to figure out which - // internal format OpenGL should use for storing it, ie. if no alpha - // is present store in RGB instead of RGBA, this saves a few bytes - // of memory, but much more importantly it makes the game look - // *much* better in 16bit color mode - int internal_format = GL_RGBA; - bool has_alpha = false; - - unsigned char* buf = static_cast(conv->pixels); - for (int y = 0; y < surf->h; ++y) - for (int x = 0; x < surf->w; ++x) - { - if (buf[(conv->pitch*y + x*4) + 3] != 255) - { - has_alpha = true; - break; - } - } - - if (!has_alpha) - { - internal_format = GL_RGB; - } - - glGenTextures(1, &*tex); - glBindTexture(GL_TEXTURE_2D , *tex); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel); - glTexImage2D(GL_TEXTURE_2D, 0, internal_format, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels); - glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); - - SDL_FreeSurface(conv); -} - -int -SurfaceOpenGL::draw(float x, float y, Uint8 alpha, Uint32 effect) +Surface::draw(float x, float y, float alpha, float angle, const Color& color, const Blend& blend, DrawingEffect effect) const { - float pw = power_of_two(w); - float ph = power_of_two(h); - - if(effect & SEMI_TRANSPARENT) - alpha = 128; - - glEnable(GL_TEXTURE_2D); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - glColor4ub(alpha, alpha, alpha, alpha); - - glBindTexture(GL_TEXTURE_2D, gl_texture); + glColor4f(1.0f, 1.0f, 1.0f, alpha); + glBindTexture(GL_TEXTURE_2D, texture->get_handle()); - glBegin(GL_QUADS); - - if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP) - { - glTexCoord2f(0, 0); - glVertex2f((float)w+x, (float)h+y); - - glTexCoord2f((float)w / pw, 0); - glVertex2f(x, (float)h+y); - - glTexCoord2f((float)w / pw, (float)h / ph); - glVertex2f(x, y); - - glTexCoord2f(0, (float)h / ph); - glVertex2f((float)w+x, y); - } - else if(effect & VERTICAL_FLIP) - { - glTexCoord2f(0, 0); - glVertex2f(x, (float)h+y); - - glTexCoord2f((float)w / pw, 0); - glVertex2f((float)w+x, (float)h+y); - - glTexCoord2f((float)w / pw, (float)h / ph); - glVertex2f((float)w+x, y); - - glTexCoord2f(0, (float)h / ph); - glVertex2f(x, y); - } - else if(effect & HORIZONTAL_FLIP) - { - glTexCoord2f(0, 0); - glVertex2f((float)w+x, y); - - glTexCoord2f((float)w / pw, 0); - glVertex2f(x, y); - - glTexCoord2f((float)w / pw, (float)h / ph); - glVertex2f(x, (float)h+y); - - glTexCoord2f(0, (float)h / ph); - glVertex2f((float)w+x, (float)h+y); - } - else - { - glTexCoord2f(0, 0); - glVertex2f(x, y); - - glTexCoord2f((float)w / pw, 0); - glVertex2f((float)w+x, y); - - glTexCoord2f((float)w / pw, (float)h / ph); - glVertex2f((float)w+x, (float)h+y); - - glTexCoord2f(0, (float)h / ph); - glVertex2f(x, (float)h+y); - } - glEnd(); - - glDisable(GL_TEXTURE_2D); - glDisable(GL_BLEND); - - return 0; + intern_draw2(x, y, + x + width, y + height, + uv_left, uv_top, uv_right, uv_bottom, + angle, + color, + blend, + effect); } -int -SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect) +void +Surface::draw(float x, float y, float alpha, DrawingEffect effect) const { - float pw = power_of_two(int(this->w)); - float ph = power_of_two(int(this->h)); - - if(effect & SEMI_TRANSPARENT) - alpha = 128; - - glBindTexture(GL_TEXTURE_2D, gl_texture); - - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - glColor4ub(alpha, alpha, alpha, alpha); - - glEnable(GL_TEXTURE_2D); - - - glBegin(GL_QUADS); - - if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP) - { - glTexCoord2f(sx / pw, (float)(sy+h) / ph); - glVertex2f((float)w+x, (float)h+y); - - glTexCoord2f((sx+w) / pw, (sy+h) / ph); - glVertex2f(x, (float)h+y); - - glTexCoord2f((float)(sx + w) / pw, sy / ph); - glVertex2f(x, y); - - glTexCoord2f(sx / pw, sy / ph); - glVertex2f((float)w+x, y); - } - else if(effect & VERTICAL_FLIP) - { - glTexCoord2f(sx / pw, sy / ph); - glVertex2f(x, y); - - glTexCoord2f((float)(sx + w) / pw, sy / ph); - glVertex2f(w+x, y); - - glTexCoord2f((sx+w) / pw, (sy+h) / ph); - glVertex2f(w +x, h+y); - - glTexCoord2f(sx / pw, (float)(sy+h) / ph); - glVertex2f(x, h+y); - } - else if(effect & HORIZONTAL_FLIP) - { - glTexCoord2f(sx / pw, sy / ph); - glVertex2f((float)w+x, y); - - glTexCoord2f((float)(sx + w) / pw, sy / ph); - glVertex2f(x, y); - - glTexCoord2f((sx+w) / pw, (sy+h) / ph); - glVertex2f(x, (float)h+y); - - glTexCoord2f(sx / pw, (float)(sy+h) / ph); - glVertex2f((float)w+x, (float)h+y); - } - else - { - glTexCoord2f(sx / pw, (float)(sy+h) / ph); - glVertex2f(x, h+y); - - glTexCoord2f((sx+w) / pw, (sy+h) / ph); - glVertex2f(w +x, h+y); - - glTexCoord2f((float)(sx + w) / pw, sy / ph); - glVertex2f(w+x, y); + glColor4f(1.0f, 1.0f, 1.0f, alpha); + glBindTexture(GL_TEXTURE_2D, texture->get_handle()); - glTexCoord2f(sx / pw, sy / ph); - glVertex2f(x, y); - } - - glEnd(); - - glDisable(GL_TEXTURE_2D); - glDisable(GL_BLEND); - - return 0; + intern_draw(x, y, + x + width, y + height, + uv_left, uv_top, uv_right, uv_bottom, effect); } -int -SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uint32 effect) +void +Surface::draw_part(float src_x, float src_y, float dst_x, float dst_y, + float width, float height, float alpha, + DrawingEffect effect) const { - float pw = power_of_two(sw); - float ph = power_of_two(sh); - - if(effect & SEMI_TRANSPARENT) - alpha = 128; - - glEnable(GL_TEXTURE_2D); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - glColor4ub(alpha, alpha, alpha, alpha); + float uv_width = uv_right - uv_left; + float uv_height = uv_bottom - uv_top; - glBindTexture(GL_TEXTURE_2D, gl_texture); + float uv_left = this->uv_left + (uv_width * src_x) / this->width; + float uv_top = this->uv_top + (uv_height * src_y) / this->height; + float uv_right = this->uv_left + (uv_width * (src_x + width)) / this->width; + float uv_bottom = this->uv_top + (uv_height * (src_y + height)) / this->height; - glBegin(GL_QUADS); - - if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP) - { - glTexCoord2f(0, 0); - glVertex2f((float)sw+x, (float)sh+y); - - glTexCoord2f((float)w / pw, 0); - glVertex2f(x, (float)sh+y); - - glTexCoord2f((float)w / pw, (float)h / ph); - glVertex2f(x, y); - - glTexCoord2f(0, (float)h / ph); - glVertex2f((float)sw+x, y); - } - else if(effect & VERTICAL_FLIP) - { - glTexCoord2f(0, 0); - glVertex2f(x, (float)sh+y); - - glTexCoord2f((float)w / pw, 0); - glVertex2f((float)sw+x, (float)sh+y); - - glTexCoord2f((float)w / pw, (float)h / ph); - glVertex2f((float)sw+x, y); - - glTexCoord2f(0, (float)h / ph); - glVertex2f(x, y); - } - else if(effect & HORIZONTAL_FLIP) - { - glTexCoord2f(0, 0); - glVertex2f((float)sw+x, y); - - glTexCoord2f((float)w / pw, 0); - glVertex2f(x, y); - - glTexCoord2f((float)w / pw, (float)h / ph); - glVertex2f(x, (float)sh+y); - - glTexCoord2f(0, (float)h / ph); - glVertex2f((float)sw+x, (float)sh+y); - } - else - { - glTexCoord2f(0, 0); - glVertex2f(x, y); - - glTexCoord2f((float)w / pw, 0); - glVertex2f((float)sw+x, y); - - glTexCoord2f((float)w / pw, (float)h / ph); - glVertex2f((float)sw+x, (float)sh+y); - - glTexCoord2f(0, (float)h / ph); - glVertex2f(x, (float)sh+y); - } - glEnd(); - - glDisable(GL_TEXTURE_2D); - glDisable(GL_BLEND); - - return 0; -} - -void -SurfaceOpenGL::apply_filter(int filter, Color color) -{ - ::apply_filter_to_surface(sdl_surface, filter, color); - create_gl(sdl_surface,&gl_texture); + glColor4f(1.0f, 1.0f, 1.0f, alpha); + glBindTexture(GL_TEXTURE_2D, texture->get_handle()); - w = sdl_surface->w; - h = sdl_surface->h; + intern_draw(dst_x, dst_y, + dst_x + width, dst_y + height, + uv_left, uv_top, uv_right, uv_bottom, effect); }