1 // $Id: surface.cpp 2175 2004-11-24 19:02:49Z sik0fewl $
4 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
29 #include <SDL_image.h>
31 #include "gameconfig.hpp"
32 #include "physfs/physfs_sdl.hpp"
33 #include "video/surface.hpp"
34 #include "video/screen.hpp"
36 Surface::Surfaces Surface::surfaces;
38 extern SDL_Surface* screen;
40 SurfaceData::SurfaceData(SDL_Surface* temp, bool use_alpha_)
41 : type(SURFACE), surface(0), use_alpha(use_alpha_),
42 x(0), y(0), w(0), h(0)
44 // Copy the given surface and make sure that it is not stored in
46 surface = SDL_CreateRGBSurface(temp->flags & (~SDL_HWSURFACE),
48 temp->format->BitsPerPixel,
54 throw std::runtime_error("No memory left for surface");
56 SDL_SetAlpha(temp,0,0);
57 SDL_BlitSurface(temp, NULL, surface, NULL);
60 SurfaceData::SurfaceData(const std::string& file_, bool use_alpha_)
61 : type(LOAD), surface(0), file(file_), use_alpha(use_alpha_)
64 SurfaceData::SurfaceData(const std::string& file_, int x_, int y_,
65 int w_, int h_, bool use_alpha_)
66 : type(LOAD_PART), surface(0), file(file_), use_alpha(use_alpha_),
67 x(x_), y(y_), w(w_), h(h_)
70 SurfaceData::SurfaceData(Color top_gradient_, Color bottom_gradient_,
72 : type(GRADIENT), surface(0), use_alpha(false), w(w_), h(h_)
74 top_gradient = top_gradient_;
75 bottom_gradient = bottom_gradient_;
79 SurfaceData::~SurfaceData()
81 SDL_FreeSurface(surface);
87 return create_SurfaceOpenGL();
91 SurfaceData::create_SurfaceOpenGL()
96 return new SurfaceOpenGL(file);
98 return new SurfaceOpenGL(file, x, y, w, h);
100 return new SurfaceOpenGL(surface);
102 return new SurfaceOpenGL(top_gradient, bottom_gradient, w, h);
108 /* Quick utility function for texture creation */
109 static int power_of_two(int input)
113 while ( value < input )
120 Surface::Surface(SDL_Surface* surf, bool use_alpha)
121 : impl(0), data(surf, use_alpha), w(0), h(0)
123 impl = data.create();
129 surfaces.push_back(this);
132 Surface::Surface(const std::string& file, bool use_alpha)
133 : impl(0), data(file, use_alpha), w(0), h(0)
135 impl = data.create();
141 surfaces.push_back(this);
144 Surface::Surface(const std::string& file, int x, int y, int w_, int h_, bool use_alpha)
145 : impl(0), data(file, x, y, w_, h_, use_alpha), w(0), h(0)
147 impl = data.create();
153 surfaces.push_back(this);
156 Surface::Surface(Color top_background, Color bottom_background, int w_, int h_)
157 : impl(0), data(top_background, bottom_background, w_, h_), w(0), h(0)
159 impl = data.create();
165 surfaces.push_back(this);
172 impl = data.create();
177 for(std::vector<SurfaceData::Filter>::iterator i =
178 data.applied_filters.begin(); i != data.applied_filters.end();
180 impl->apply_filter(i->type, i->color);
184 void Surface::apply_filter(int filter, Color color)
186 impl->apply_filter(filter, color);
188 SurfaceData::Filter apply_filter;
189 apply_filter.type = filter;
190 apply_filter.color = color;
191 data.applied_filters.push_back(apply_filter);
198 for(std::list<Surface*>::iterator i = surfaces.begin(); i != surfaces.end();
207 printf("Error: Surface freed twice!!!\n");
209 surfaces.remove(this);
214 Surface::reload_all()
216 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
223 Surface::debug_check()
225 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
227 printf("Surface not freed: T:%d F:%s.\n", (*i)->data.type,
228 (*i)->data.file.c_str());
233 apply_filter_to_surface(SDL_Surface* surface, int filter, Color color)
235 if(filter == HORIZONTAL_FLIP_FILTER) {
236 SDL_Surface* sur_copy = sdl_surface_from_sdl_surface(surface);
237 SDL_BlitSurface(surface, NULL, sur_copy, NULL);
238 SDL_SetAlpha(sur_copy,0,0);
243 src.h = dst.h = sur_copy->h;
244 for(int x = 0; x < sur_copy->w; x++)
246 src.x = x; dst.x = sur_copy->w-1 - x;
247 SDL_BlitSurface(sur_copy, &src, surface, &dst);
250 SDL_FreeSurface(sur_copy);
251 } else if(filter == MASK_FILTER) {
252 SDL_Surface* sur_copy = sdl_surface_from_sdl_surface(surface);
256 SDL_LockSurface(sur_copy);
257 for(int x = 0; x < sur_copy->w; x++)
258 for(int y = 0; y < sur_copy->h; y++) {
259 SDL_GetRGBA(getpixel(sur_copy,x,y), sur_copy->format, &r,&g,&b,&a);
261 putpixel(sur_copy, x,y, color.map_rgba(sur_copy));
264 SDL_UnlockSurface(sur_copy);
266 SDL_BlitSurface(sur_copy, NULL, surface, NULL);
267 SDL_FreeSurface(sur_copy);
272 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h)
275 SDL_Surface * sdl_surface;
279 temp = IMG_Load_RW(get_physfs_SDLRWops(file), true);
281 std::stringstream msg;
282 msg << "Couldn't load '" << file << "': " << SDL_GetError();
283 throw std::runtime_error(msg.str());
286 /* Set source rectangle for conv: */
293 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, temp->format->BitsPerPixel,
297 temp->format->Amask);
299 SDL_SetAlpha(temp,0,0);
301 SDL_BlitSurface(temp, &src, conv, NULL);
302 sdl_surface = SDL_DisplayFormatAlpha(conv);
304 if (sdl_surface == NULL) {
305 std::stringstream msg;
306 msg << "Can't convert file '" << file << "' to display format.";
307 throw std::runtime_error(msg.str());
310 SDL_FreeSurface(temp);
311 SDL_FreeSurface(conv);
317 sdl_surface_from_file(const std::string& file)
319 SDL_Surface* sdl_surface;
322 temp = IMG_Load_RW(get_physfs_SDLRWops(file), true);
324 std::stringstream msg;
325 msg << "Couldn't load file '" << file << "': " << SDL_GetError();
326 throw std::runtime_error(msg.str());
329 sdl_surface = SDL_DisplayFormatAlpha(temp);
331 if (sdl_surface == NULL) {
332 std::stringstream msg;
333 msg << "Couldn't convert file '" << file << "' to display format";
334 throw std::runtime_error(msg.str());
337 SDL_FreeSurface(temp);
343 sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf)
345 SDL_Surface* sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
346 if (sdl_surface == 0) {
347 std::stringstream msg;
348 msg << "Can't convert surface to display format.";
349 throw std::runtime_error(msg.str());
356 sdl_surface_from_gradient(Color top, Color bottom, int w, int h)
358 SDL_Surface* sdl_surface
359 = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h,
360 screen->format->BitsPerPixel, screen->format->Rmask,
361 screen->format->Gmask, screen->format->Bmask, 0);
364 throw std::runtime_error("Can't create gradient surface");
367 SDL_FillRect(sdl_surface, NULL, SDL_MapRGB(sdl_surface->format,
368 top.red, top.green, top.blue));
370 float redstep = (float(bottom.red)-float(top.red)) / float(h);
371 float greenstep = (float(bottom.green)-float(top.green)) / float(h);
372 float bluestep = (float(bottom.blue) - float(top.blue)) / float(h);
378 for(float y = 0; y < h; y++) {
380 SDL_FillRect(sdl_surface, &rect, SDL_MapRGB(sdl_surface->format,
381 int(float(top.red) + redstep * y),
382 int(float(top.green) + greenstep * y),
383 int(float(top.blue) + bluestep * y)));
390 //---------------------------------------------------------------------------
392 SurfaceImpl::SurfaceImpl()
396 SurfaceImpl::~SurfaceImpl()
399 SDL_FreeSurface(sdl_surface);
402 SDL_Surface* SurfaceImpl::get_sdl_surface() const
407 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf)
409 sdl_surface = sdl_surface_from_sdl_surface(surf);
410 create_gl(sdl_surface,&gl_texture);
416 SurfaceOpenGL::SurfaceOpenGL(const std::string& file)
418 sdl_surface = sdl_surface_from_file(file);
419 create_gl(sdl_surface,&gl_texture);
425 SurfaceOpenGL::SurfaceOpenGL(const std::string& file_, int x_, int y_,
428 sdl_surface = sdl_surface_part_from_file(file_, x_, y_, w_, h_);
430 create_gl(sdl_surface, &gl_texture);
435 SurfaceOpenGL::SurfaceOpenGL(Color top_gradient, Color bottom_gradient,
438 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient,_w,_h);
439 create_gl(sdl_surface, &gl_texture);
444 SurfaceOpenGL::~SurfaceOpenGL()
446 glDeleteTextures(1, &gl_texture);
450 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
457 w = power_of_two(surf->w);
458 h = power_of_two(surf->h),
460 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
461 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
462 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
464 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
465 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
468 /* Save the alpha blending attributes */
469 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
470 saved_alpha = surf->format->alpha;
471 if ( (saved_flags & SDL_SRCALPHA)
474 SDL_SetAlpha(surf, 0, 0);
477 SDL_BlitSurface(surf, 0, conv, 0);
479 /* Restore the alpha blending attributes */
480 if ( (saved_flags & SDL_SRCALPHA)
483 SDL_SetAlpha(surf, saved_flags, saved_alpha);
486 // We check all the pixels of the surface to figure out which
487 // internal format OpenGL should use for storing it, ie. if no alpha
488 // is present store in RGB instead of RGBA, this saves a few bytes
489 // of memory, but much more importantly it makes the game look
490 // *much* better in 16bit color mode
491 int internal_format = GL_RGBA;
492 bool has_alpha = false;
494 unsigned char* buf = static_cast<unsigned char*>(conv->pixels);
495 for (int y = 0; y < surf->h; ++y)
496 for (int x = 0; x < surf->w; ++x)
498 if (buf[(conv->pitch*y + x*4) + 3] != 255)
507 internal_format = GL_RGB;
510 glGenTextures(1, &*tex);
511 glBindTexture(GL_TEXTURE_2D , *tex);
512 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
513 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
514 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
515 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
516 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
517 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
518 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
520 SDL_FreeSurface(conv);
524 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, Uint32 effect)
526 float pw = power_of_two(w);
527 float ph = power_of_two(h);
529 if(effect & SEMI_TRANSPARENT)
532 glEnable(GL_TEXTURE_2D);
534 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
536 glColor4ub(alpha, alpha, alpha, alpha);
538 glBindTexture(GL_TEXTURE_2D, gl_texture);
542 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
545 glVertex2f((float)w+x, (float)h+y);
547 glTexCoord2f((float)w / pw, 0);
548 glVertex2f(x, (float)h+y);
550 glTexCoord2f((float)w / pw, (float)h / ph);
553 glTexCoord2f(0, (float)h / ph);
554 glVertex2f((float)w+x, y);
556 else if(effect & VERTICAL_FLIP)
559 glVertex2f(x, (float)h+y);
561 glTexCoord2f((float)w / pw, 0);
562 glVertex2f((float)w+x, (float)h+y);
564 glTexCoord2f((float)w / pw, (float)h / ph);
565 glVertex2f((float)w+x, y);
567 glTexCoord2f(0, (float)h / ph);
570 else if(effect & HORIZONTAL_FLIP)
573 glVertex2f((float)w+x, y);
575 glTexCoord2f((float)w / pw, 0);
578 glTexCoord2f((float)w / pw, (float)h / ph);
579 glVertex2f(x, (float)h+y);
581 glTexCoord2f(0, (float)h / ph);
582 glVertex2f((float)w+x, (float)h+y);
589 glTexCoord2f((float)w / pw, 0);
590 glVertex2f((float)w+x, y);
592 glTexCoord2f((float)w / pw, (float)h / ph);
593 glVertex2f((float)w+x, (float)h+y);
595 glTexCoord2f(0, (float)h / ph);
596 glVertex2f(x, (float)h+y);
600 glDisable(GL_TEXTURE_2D);
607 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
609 float pw = power_of_two(int(this->w));
610 float ph = power_of_two(int(this->h));
612 if(effect & SEMI_TRANSPARENT)
615 glBindTexture(GL_TEXTURE_2D, gl_texture);
618 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
620 glColor4ub(alpha, alpha, alpha, alpha);
622 glEnable(GL_TEXTURE_2D);
627 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
629 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
630 glVertex2f((float)w+x, (float)h+y);
632 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
633 glVertex2f(x, (float)h+y);
635 glTexCoord2f((float)(sx + w) / pw, sy / ph);
638 glTexCoord2f(sx / pw, sy / ph);
639 glVertex2f((float)w+x, y);
641 else if(effect & VERTICAL_FLIP)
643 glTexCoord2f(sx / pw, sy / ph);
646 glTexCoord2f((float)(sx + w) / pw, sy / ph);
649 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
650 glVertex2f(w +x, h+y);
652 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
655 else if(effect & HORIZONTAL_FLIP)
657 glTexCoord2f(sx / pw, sy / ph);
658 glVertex2f((float)w+x, y);
660 glTexCoord2f((float)(sx + w) / pw, sy / ph);
663 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
664 glVertex2f(x, (float)h+y);
666 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
667 glVertex2f((float)w+x, (float)h+y);
671 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
674 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
675 glVertex2f(w +x, h+y);
677 glTexCoord2f((float)(sx + w) / pw, sy / ph);
680 glTexCoord2f(sx / pw, sy / ph);
686 glDisable(GL_TEXTURE_2D);
693 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uint32 effect)
695 float pw = power_of_two(sw);
696 float ph = power_of_two(sh);
698 if(effect & SEMI_TRANSPARENT)
701 glEnable(GL_TEXTURE_2D);
703 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
705 glColor4ub(alpha, alpha, alpha, alpha);
707 glBindTexture(GL_TEXTURE_2D, gl_texture);
711 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
714 glVertex2f((float)sw+x, (float)sh+y);
716 glTexCoord2f((float)w / pw, 0);
717 glVertex2f(x, (float)sh+y);
719 glTexCoord2f((float)w / pw, (float)h / ph);
722 glTexCoord2f(0, (float)h / ph);
723 glVertex2f((float)sw+x, y);
725 else if(effect & VERTICAL_FLIP)
728 glVertex2f(x, (float)sh+y);
730 glTexCoord2f((float)w / pw, 0);
731 glVertex2f((float)sw+x, (float)sh+y);
733 glTexCoord2f((float)w / pw, (float)h / ph);
734 glVertex2f((float)sw+x, y);
736 glTexCoord2f(0, (float)h / ph);
739 else if(effect & HORIZONTAL_FLIP)
742 glVertex2f((float)sw+x, y);
744 glTexCoord2f((float)w / pw, 0);
747 glTexCoord2f((float)w / pw, (float)h / ph);
748 glVertex2f(x, (float)sh+y);
750 glTexCoord2f(0, (float)h / ph);
751 glVertex2f((float)sw+x, (float)sh+y);
758 glTexCoord2f((float)w / pw, 0);
759 glVertex2f((float)sw+x, y);
761 glTexCoord2f((float)w / pw, (float)h / ph);
762 glVertex2f((float)sw+x, (float)sh+y);
764 glTexCoord2f(0, (float)h / ph);
765 glVertex2f(x, (float)sh+y);
769 glDisable(GL_TEXTURE_2D);
776 SurfaceOpenGL::apply_filter(int filter, Color color)
778 ::apply_filter_to_surface(sdl_surface, filter, color);
779 create_gl(sdl_surface,&gl_texture);