- if (use_alpha == false && !config->use_gl)
- SDL_SetAlpha(sdl_surface, 0, 0);
-
- SDL_FreeSurface(temp);
- SDL_FreeSurface(conv);
-
- return sdl_surface;
-}
-
-SDL_Surface*
-sdl_surface_from_file(const std::string& file, bool use_alpha)
-{
- SDL_Surface* sdl_surface;
- SDL_Surface* temp;
-
- temp = IMG_Load(file.c_str());
- if (temp == 0) {
- std::stringstream msg;
- msg << "Couldn't load file '" << file << "': " << SDL_GetError();
- throw std::runtime_error(msg.str());
- }
-
- if(use_alpha == false && !config->use_gl)
- sdl_surface = SDL_DisplayFormat(temp);
- else
- 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());
- }
-
- if (use_alpha == false && !config->use_gl)
- SDL_SetAlpha(sdl_surface, 0, 0);
-
- SDL_FreeSurface(temp);
-
- return sdl_surface;
-}
-
-SDL_Surface*
-sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, bool use_alpha)
-{
- SDL_Surface* sdl_surface;
-#if 0
- Uint32 saved_flags;
- Uint8 saved_alpha;
-
- /* Save the alpha blending attributes */
- saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
- saved_alpha = sdl_surf->format->alpha;
- if ( (saved_flags & SDL_SRCALPHA)
- == SDL_SRCALPHA )
- {
- SDL_SetAlpha(sdl_surf, 0, 0);
- }
-#endif
-
- if(use_alpha == false && !config->use_gl)
- sdl_surface = SDL_DisplayFormat(sdl_surf);
- else
- sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
-
-#if 0
- /* Restore the alpha blending attributes */
- if ( (saved_flags & SDL_SRCALPHA)
- == SDL_SRCALPHA )
- {
- SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
- }
-#endif
-
- if (sdl_surface == NULL) {
- std::stringstream msg;
- msg << "Can't convert surface to display format.";
- throw std::runtime_error(msg.str());
- }
-
- if (use_alpha == false && !config->use_gl)
- SDL_SetAlpha(sdl_surface, 0, 0);
-
- return sdl_surface;
-}
-
-SDL_Surface*
-sdl_surface_from_gradient(Color top, Color bottom, int w, int h)
-{
- 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)));
- }
- }
-
- return sdl_surface;
-}
-
-//---------------------------------------------------------------------------
-
-SurfaceImpl::SurfaceImpl()
- : sdl_surface(0)
-{}
-
-SurfaceImpl::~SurfaceImpl()
-{
- if(sdl_surface != 0)
- SDL_FreeSurface(sdl_surface);
-}
-
-SDL_Surface* SurfaceImpl::get_sdl_surface() const
-{
- return sdl_surface;
-}
-
-SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, bool use_alpha)
-{
- sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
- create_gl(sdl_surface,&gl_texture);
-
- w = sdl_surface->w;
- h = sdl_surface->h;
-}
-
-SurfaceOpenGL::SurfaceOpenGL(const std::string& file, bool use_alpha)
-{
- sdl_surface = sdl_surface_from_file(file, use_alpha);
- create_gl(sdl_surface,&gl_texture);
-
- w = sdl_surface->w;
- h = sdl_surface->h;
-}
-
-SurfaceOpenGL::SurfaceOpenGL(const std::string& file_, int x_, int y_,
- int w_, int h_, bool use_alpha_)
-{
- sdl_surface = sdl_surface_part_from_file(file_,x_,y_,w_,h_,use_alpha_);
-
- create_gl(sdl_surface, &gl_texture);
- w = sdl_surface->w;
- h = sdl_surface->h;
-}
-
-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;
-}
-
-SurfaceOpenGL::~SurfaceOpenGL()
-{
- glDeleteTextures(1, &gl_texture);
-}
-
-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/*GL_RGB10_A2*/;
- bool has_alpha = false;
-
- unsigned char* buf = static_cast<unsigned char*>(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)
-{
- 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);
-