Yet another try
[supertux.git] / contrib / supertux-nogl.diff
1 #
2 #  SuperTux -nogl patch
3 #  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
4 #
5 #  This program is free software; you can redistribute it and/or
6 #  modify it under the terms of the GNU General Public License
7 #  as published by the Free Software Foundation; either version 2
8 #  of the License, or (at your option) any later version.
9 #
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU General Public License for more details.
14 #
15 #  You should have received a copy of the GNU General Public License
16 #  along with this program; if not, write to the Free Software
17 #  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 #
19 # -----------------------------------------------------------------------------
20 #
21 #  This patch allows running the game on systems without OpenGL support. 
22 #
23 #  It modifies the video portion of the SuperTux engine to render all graphics
24 #  with SDL functions only. Many features are removed from the video engine,
25 #  so don't expect much.
26 #
27 #  Note that the patched sources will need an additional library, SDL_gfx, to 
28 #  compile.
29 #
30 #  Installing the patch should be pretty straightforward. Simply run the
31 #  following command prior to running autogen.sh and configure:
32 #
33 #  patch -p1 < contrib/supertux-nogl.diff
34 #
35 #  This patch works for revision 3858. It may break for later revisions.
36 #
37 # -----------------------------------------------------------------------------
38 diff -Naur supertux/INSTALL supertux-nogl/INSTALL
39 --- supertux/INSTALL    2006-03-03 21:49:07.000000000 +0100
40 +++ supertux-nogl/INSTALL       2006-04-07 04:13:00.000000000 +0200
41 @@ -1,7 +1,7 @@
42  - Install instructions for SuperTux -
43  http://supertux.berlios.de/
44  
45 -Last update: October 11, 2005 by Ondra Hosek
46 +Last update: March 4, 2006 by Christoph Sommer
47  
48  BINARIES
49  --------
50 @@ -34,17 +34,15 @@
51      Download: ftp://ftp.perforce.com/pub/jam
52      Homepage: http://www.perforce.com/jam/jam.html
53  
54 -* OpenGL headers and libraries
55 -    opengl libraries and headers are specific to your graphics card. Make sure
56 -    that you have hardware accelerated opengl drivers installed. Software
57 -    renderers like Mesa will make supertux unplayable slow.
58 -
59  * SDL 1.2.5 or later (1.2.8 is recommended on MacOS/X)
60      http://www.libsdl.org
61  
62  * SDL_image (any version)
63      http://www.libsdl.org/projects/SDL_image
64  
65 +* SDL_gfx (2.0.13 or later)
66 +    http://www.ferzkopp.net/Software/SDL_gfx-2.0/
67 +
68  * PhysicsFS (1.0.0, the development branch 1.1.x is buggy and does not work,
69               1.2.0 and later should work when it is released)
70      http://www.icculus.org/physfs
71 diff -Naur supertux/README supertux-nogl/README
72 --- supertux/README     2006-03-03 20:43:38.000000000 +0100
73 +++ supertux-nogl/README        2006-04-07 04:13:00.000000000 +0200
74 @@ -2,7 +2,7 @@
75  - An introduction for SuperTux -
76  http://supertux.berlios.de/
77  
78 -Last update: October 13, 2005
79 +Last update: March 4, 2006 by Christoph Sommer
80  
81  DESCRIPTION
82  -----------
83 @@ -75,10 +75,9 @@
84    Also, notice that SuperTux saves the options, so it's often enough to
85    specify them once.
86  
87 -  The game uses OpenGL to render the graphics. You will either need a CPU
88 -  with about 10 GHz or an accelerated video card with the vendor's drivers.
89 -  (On Linux, the team recommends using cards from NVidia with the proprietary
90 -  drivers, but ATI or another verndor should do.)
91 +  The game uses SDL to render the graphics. You will either need a CPU
92 +  with about 1 GHz or go get the original version of SuperTux which uses
93 +  OpenGL.
94  
95  
96  PLAYING THE GAME
97 diff -Naur supertux/configure.ac supertux-nogl/configure.ac
98 --- supertux/configure.ac       2006-03-03 20:43:38.000000000 +0100
99 +++ supertux-nogl/configure.ac  2006-04-07 04:13:00.000000000 +0200
100 @@ -11,7 +11,7 @@
101  
102  dnl Process this file with autoconf to produce a configure script.
103  AC_PREREQ([2.54])
104 -AC_INIT(supertux, 0.2-svn)
105 +AC_INIT(supertux, 0.2-nogl-svn)
106  AC_CONFIG_SRCDIR([src/main.cpp])
107  AC_CONFIG_AUX_DIR([mk/autoconf])
108  AC_CANONICAL_TARGET
109 @@ -112,6 +105,14 @@
110          [AC_MSG_ERROR([Please install SDLImage >= 1.2.1])],
111          [$SDL_CFLAGS], [$SDL_LIBS])
112  
113 +dnl FIXME: This is far from perfect
114 +NP_FINDLIB([SDLGFX], [SDL_gfx], [SDL_gfx >= 2.0.13],
115 +        NP_LANG_PROGRAM([#include <SDL_rotozoom.h>], [0;]),
116 +        [], [-lSDL_gfx],
117 +        [],
118 +        [AC_MSG_ERROR([Please install SDL_gfx >= 2.0.13])],
119 +        [$SDL_CFLAGS], [$SDL_LIBS])
120 +
121  NP_FINDLIB([PHYSFS], [physfs], [physfs >= 1.0.0],
122          NP_LANG_PROGRAM([
123  #include <stdio.h>
124 @@ -131,11 +132,6 @@
125           [AC_MSG_ERROR([Please intall OpenAL])],
126           [], [])
127  
128 -AX_CHECK_GL
129 -if test "$no_gl" = "yes"; then
130 -  AC_MSG_ERROR([Please install opengl libraries and headers])
131 -fi
132 -
133  dnl Checks for library functions.
134  AC_CHECK_FUNCS(mkdir strdup strstr)
135  
136 diff -Naur supertux/src/Jamfile supertux-nogl/src/Jamfile
137 --- supertux/src/Jamfile        2006-03-03 20:34:49.000000000 +0100
138 +++ supertux-nogl/src/Jamfile   2006-04-07 04:11:50.000000000 +0200
139 @@ -24,7 +24,7 @@
140  Application supertux : $(sources) $(wrapper_objects) ;
141  C++Flags supertux : -DAPPDATADIR='\"$(appdatadir)\"' ;
142  LinkWith supertux : squirrel ;
143 -ExternalLibs supertux : SDL SDLIMAGE GL OPENAL VORBIS VORBISFILE OGG ICONV PHYSFS BINRELOC ;
144 +ExternalLibs supertux : SDL SDLIMAGE SDLGFX OPENAL VORBIS VORBISFILE OGG ICONV PHYSFS BINRELOC ;
145  Help supertux : "Build the supertux executable" ;
146  IncludeDir supertux : squirrel/include ;
147  
148 diff -Naur supertux/src/main.cpp supertux-nogl/src/main.cpp
149 --- supertux/src/main.cpp       2006-04-07 03:32:14.000000000 +0200
150 +++ supertux-nogl/src/main.cpp  2006-04-07 04:11:50.000000000 +0200
151 @@ -35,7 +35,6 @@
152  #include <physfs.h>
153  #include <SDL.h>
154  #include <SDL_image.h>
155 -#include <GL/gl.h>
156  
157  #include "gameconfig.hpp"
158  #include "resources.hpp"
159 @@ -317,7 +275,7 @@
160    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
161    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
162    
163 -  int flags = SDL_OPENGL;
164 +  int flags = SDL_SWSURFACE;
165    if(config->use_fullscreen)
166      flags |= SDL_FULLSCREEN;
167    int width = config->screenwidth;
168 @@ -347,24 +305,6 @@
169    }
170  #endif
171  
172 -  // setup opengl state and transform
173 -  glDisable(GL_DEPTH_TEST);
174 -  glDisable(GL_CULL_FACE);
175 -  glEnable(GL_TEXTURE_2D);
176 -  glEnable(GL_BLEND);
177 -  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
178 -
179 -  glViewport(0, 0, screen->w, screen->h);
180 -  glMatrixMode(GL_PROJECTION);
181 -  glLoadIdentity();
182 -  // logical resolution here not real monitor resolution
183 -  glOrtho(0, 800, 600, 0, -1.0, 1.0);
184 -  glMatrixMode(GL_MODELVIEW);
185 -  glLoadIdentity();
186 -  glTranslatef(0, 0, 0);
187 -
188 -  check_gl_error("Setting up view matrices");
189 -
190    if(texture_manager != NULL)
191      texture_manager->reload_textures();
192    else
193 diff -aur supertux/src/video/drawing_context.cpp supertux-nogl/src/video/drawing_context.cpp
194 --- supertux/src/video/drawing_context.cpp      2006-07-03 02:00:48.000000000 +0200
195 +++ supertux-nogl/src/video/drawing_context.cpp 2006-07-03 02:00:29.000000000 +0200
196 @@ -23,7 +23,6 @@
197  #include <cassert>
198  #include <iostream>
199  #include <SDL_image.h>
200 -#include <GL/gl.h>
201  
202  #include "drawing_context.hpp"
203  #include "surface.hpp"
204 @@ -49,24 +48,12 @@
205  {
206    screen = SDL_GetVideoSurface();
207  
208 -  lightmap_width = screen->w / LIGHTMAP_DIV;
209 -  lightmap_height = screen->h / LIGHTMAP_DIV;
210 -  unsigned int width = next_po2(lightmap_width);
211 -  unsigned int height = next_po2(lightmap_height);
212 -
213 -  lightmap = new Texture(width, height, GL_RGB);
214 -
215 -  lightmap_uv_right = static_cast<float>(lightmap_width) / static_cast<float>(width);
216 -  lightmap_uv_bottom = static_cast<float>(lightmap_height) / static_cast<float>(height);
217 -  texture_manager->register_texture(lightmap);
218 -
219 +  target = NORMAL;
220    requests = &drawing_requests;
221  }
222  
223  DrawingContext::~DrawingContext()
224  {
225 -  texture_manager->remove_texture(lightmap);
226 -  delete lightmap;
227  }
228  
229  void
230 @@ -74,6 +61,8 @@
231                               float angle, const Color& color, const Blend& blend,
232                               int layer)
233  {
234 +  if(target != NORMAL) return;
235 +
236    assert(surface != 0);
237    
238    DrawingRequest request;
239 @@ -109,6 +98,8 @@
240  DrawingContext::draw_surface_part(const Surface* surface, const Vector& source,
241      const Vector& size, const Vector& dest, int layer)
242  {
243 +  if(target != NORMAL) return;
244 +
245    assert(surface != 0);
246  
247    DrawingRequest request;
248 @@ -148,6 +139,8 @@
249  DrawingContext::draw_text(const Font* font, const std::string& text,
250      const Vector& position, FontAlignment alignment, int layer)
251  {
252 +  if(target != NORMAL) return;
253 +
254    DrawingRequest request;
255  
256    request.type = TEXT;
257 @@ -169,6 +162,8 @@
258  DrawingContext::draw_center_text(const Font* font, const std::string& text,
259      const Vector& position, int layer)
260  {
261 +  if(target != NORMAL) return;
262 +
263    draw_text(font, text, Vector(position.x + SCREEN_WIDTH/2, position.y),
264        CENTER_ALLIGN, layer);
265  }
266 @@ -176,6 +171,8 @@
267  void
268  DrawingContext::draw_gradient(const Color& top, const Color& bottom, int layer)
269  {
270 +  if(target != NORMAL) return;
271 +
272    DrawingRequest request;
273  
274    request.type = GRADIENT;
275 @@ -197,6 +194,8 @@
276  DrawingContext::draw_filled_rect(const Vector& topleft, const Vector& size,
277                                   const Color& color, int layer)
278  {
279 +  if(target != NORMAL) return;
280 +
281    DrawingRequest request;
282  
283    request.type = FILLRECT;
284 @@ -219,6 +218,8 @@
285  DrawingContext::draw_filled_rect(const Rect& rect, const Color& color,
286                                   int layer)
287  {
288 +  if(target != NORMAL) return;
289 +
290    DrawingRequest request;
291  
292    request.type = FILLRECT;
293 @@ -252,24 +253,56 @@
294    delete surfacepartrequest;
295  }
296  
297 +namespace
298 +{
299 +  void fillrect(SDL_Surface* screen, float x, float y, float w, float h, int r, int g, int b, int a)
300 +  {
301 +    if(w < 0) {
302 +      x += w;
303 +      w = -w;
304 +    }
305 +    if(h < 0) {
306 +      y += h;
307 +      h = -h;
308 +    }
309 +
310 +    SDL_Rect src, rect;
311 +    SDL_Surface *temp = NULL;
312 +
313 +    rect.x = (int)x;
314 +    rect.y = (int)y;
315 +    rect.w = (int)w;
316 +    rect.h = (int)h;
317 +
318 +    if(a != 255) {
319 +      temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
320 +
321 +      src.x = 0;
322 +      src.y = 0;
323 +      src.w = rect.w;
324 +      src.h = rect.h;
325 +
326 +      SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, r, g, b));
327 +      SDL_SetAlpha(temp, SDL_SRCALPHA, a);
328 +      SDL_BlitSurface(temp,0,screen,&rect);
329 +      SDL_FreeSurface(temp);
330 +    } else {
331 +      SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, r, g, b));
332 +    }
333 +  }
334 +}
335 +
336  void
337  DrawingContext::draw_gradient(DrawingRequest& request)
338  {
339    GradientRequest* gradientrequest = (GradientRequest*) request.request_data;
340    const Color& top = gradientrequest->top;
341    const Color& bottom = gradientrequest->bottom;
342 -  
343 -  glDisable(GL_TEXTURE_2D);
344 -  glBegin(GL_QUADS);
345 -  glColor4f(top.red, top.green, top.blue, top.alpha);
346 -  glVertex2f(0, 0);
347 -  glVertex2f(SCREEN_WIDTH, 0);
348 -  glColor4f(bottom.red, bottom.green, bottom.blue, bottom.alpha);
349 -  glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
350 -  glVertex2f(0, SCREEN_HEIGHT);
351 -  glEnd();
352 -  glEnable(GL_TEXTURE_2D);
353  
354 +  int width = 800;
355 +  int height = 600;
356 +  for(float y = 0; y < height; y += 2) ::fillrect(screen, 0, (int)y, width, 2, (int)(((float)(top.red-bottom.red)/(0-height)) * y + top.red), (int)(((float)(top.green-bottom.green)/(0-height)) * y + top.green), (int)(((float)(top.blue-bottom.blue)/(0-height)) * y + top.blue), 255);
357 +
358    delete gradientrequest;
359  }
360  
361 @@ -294,18 +327,13 @@
362    float w = fillrectrequest->size.x;
363    float h = fillrectrequest->size.y;
364  
365 -  glDisable(GL_TEXTURE_2D);
366 -  glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
367 -            fillrectrequest->color.blue, fillrectrequest->color.alpha);
368
369 -  glBegin(GL_QUADS);
370 -  glVertex2f(x, y);
371 -  glVertex2f(x+w, y);
372 -  glVertex2f(x+w, y+h);
373 -  glVertex2f(x, y+h);
374 -  glEnd();
375 -  glEnable(GL_TEXTURE_2D);
376 +  int r = static_cast<int>(fillrectrequest->color.red);
377 +  int g = static_cast<int>(fillrectrequest->color.green);
378 +  int b = static_cast<int>(fillrectrequest->color.blue);
379 +  int a = static_cast<int>(fillrectrequest->color.alpha);
380  
381 +  ::fillrect(screen, x, y, w, h, r, g, b, a);
382 +
383    delete fillrectrequest;
384  }
385  
386 @@ -319,67 +347,10 @@
387    transformstack.clear();
388    target_stack.clear();
389  
390 -  bool use_lightmap = lightmap_requests.size() != 0;
391 -  
392 -  // PART1: create lightmap
393 -  if(use_lightmap) {
394 -    glViewport(0, screen->h - lightmap_height, lightmap_width, lightmap_height);
395 -    glMatrixMode(GL_PROJECTION);
396 -    glLoadIdentity();               
397 -    glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
398 -    glMatrixMode(GL_MODELVIEW);
399 -    glLoadIdentity();
400 -
401 -    // FIXME: Add ambient light support here
402 -    glClearColor(0.3, 0.3, 0.4, 1);
403 -    glClear(GL_COLOR_BUFFER_BIT);
404 -    handle_drawing_requests(lightmap_requests);
405 -    lightmap_requests.clear();
406 -  
407 -    glDisable(GL_BLEND);
408 -    glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
409 -    glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, screen->h - lightmap_height, lightmap_width, lightmap_height);
410 -
411 -    glViewport(0, 0, screen->w, screen->h);
412 -    glMatrixMode(GL_PROJECTION);
413 -    glLoadIdentity();               
414 -    glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
415 -    glMatrixMode(GL_MODELVIEW);    
416 -    glLoadIdentity();
417 -    glEnable(GL_BLEND);
418 -  }
419 -
420 -  //glClear(GL_COLOR_BUFFER_BIT);
421    handle_drawing_requests(drawing_requests);
422    drawing_requests.clear();
423  
424 -  if(use_lightmap) {
425 -    // multiple the lightmap with the framebuffer
426 -    glBlendFunc(GL_DST_COLOR, GL_ZERO);
427 -     
428 -    glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
429 -    glBegin(GL_QUADS);
430 -
431 -    glTexCoord2f(0, lightmap_uv_bottom);
432 -    glVertex2f(0, 0);
433 -
434 -    glTexCoord2f(lightmap_uv_right, lightmap_uv_bottom);
435 -    glVertex2f(SCREEN_WIDTH, 0);
436 -
437 -    glTexCoord2f(lightmap_uv_right, 0);
438 -    glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
439 -
440 -    glTexCoord2f(0, 0);
441 -    glVertex2f(0, SCREEN_HEIGHT);
442 -    
443 -    glEnd();
444 -
445 -    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
446 -  }
447 -
448 -  assert_gl("drawing");
449 -
450 -  SDL_GL_SwapBuffers();
451 +  SDL_Flip(screen);
452  }
453  
454  void
455 @@ -471,9 +442,5 @@
456  DrawingContext::set_target(Target target)
457  {
458    this->target = target;
459 -  if(target == LIGHTMAP)
460 -    requests = &lightmap_requests;
461 -  else
462 -    requests = &drawing_requests;
463  }
464  
465 diff -aur supertux/src/video/drawing_context.hpp supertux-nogl/src/video/drawing_context.hpp
466 --- supertux/src/video/drawing_context.hpp      2006-07-03 02:00:48.000000000 +0200
467 +++ supertux-nogl/src/video/drawing_context.hpp 2006-07-03 02:00:29.000000000 +0200
468 @@ -24,7 +24,6 @@
469  #include <string>
470  #include <stdint.h>
471  
472 -#include <GL/gl.h>
473  #include <SDL.h>
474  #include <stdint.h>
475  #include <memory>
476 @@ -35,8 +34,9 @@
477  #include "font.hpp"
478  #include "color.hpp"
479  
480 +#include "glutil.hpp"
481 +
482  class Surface;
483 -class Texture;
484  
485  // some constants for predefined layer values
486  enum {
487 @@ -214,16 +214,12 @@
488    void draw_filled_rect(DrawingRequest& request);
489    
490    DrawingRequests drawing_requests;
491 -  DrawingRequests lightmap_requests;
492  
493    DrawingRequests* requests;
494  
495    SDL_Surface* screen;
496    Target target;
497    std::vector<Target> target_stack;
498 -  Texture* lightmap;
499 -  int lightmap_width, lightmap_height;
500 -  float lightmap_uv_right, lightmap_uv_bottom;
501  };
502  
503  #endif
504 diff -aur supertux/src/video/glutil.hpp supertux-nogl/src/video/glutil.hpp
505 --- supertux/src/video/glutil.hpp       2006-07-03 02:00:48.000000000 +0200
506 +++ supertux-nogl/src/video/glutil.hpp  2006-07-03 02:00:29.000000000 +0200
507 @@ -21,58 +21,12 @@
508  
509  #include <sstream>
510  #include <stdexcept>
511 -#include <GL/gl.h>
512  
513 -static inline void check_gl_error(const char* message)
514 -{
515 -#ifdef DEBUG
516 -  GLenum error = glGetError();
517 -  if(error != GL_NO_ERROR) {
518 -    std::ostringstream msg;
519 -    msg << "OpenGLError while '" << message << "': ";
520 -    switch(error) {
521 -      case GL_INVALID_ENUM:
522 -        msg << "INVALID_ENUM: An unacceptable value is specified for an "
523 -               "enumerated argument.";
524 -        break;
525 -      case GL_INVALID_VALUE:
526 -        msg << "INVALID_VALUE: A numeric argument is out of range.";
527 -        break;
528 -      case GL_INVALID_OPERATION:
529 -        msg << "INVALID_OPERATION: The specified operation is not allowed "
530 -               "in the current state.";
531 -        break;
532 -      case GL_STACK_OVERFLOW:
533 -        msg << "STACK_OVERFLOW: This command would cause a stack overflow.";
534 -        break;
535 -      case GL_STACK_UNDERFLOW:
536 -        msg << "STACK_UNDERFLOW: This command would cause a stack underflow.";
537 -        break;
538 -      case GL_OUT_OF_MEMORY:
539 -        msg << "OUT_OF_MEMORY: There is not enough memory left to execute the "
540 -               "command.";
541 -        break;
542 -#ifdef GL_TABLE_TOO_LARGE
543 -      case GL_TABLE_TOO_LARGE:
544 -        msg << "TABLE_TOO_LARGE: table is too large";
545 -        break;
546 -#endif                        
547 -      default:
548 -        msg << "Unknown error (code " << error << ")";
549 -    }
550 -        
551 -    throw std::runtime_error(msg.str());
552 -  }
553 -#endif
554 -}
555 +#define GLenum int
556 +#define GLint int
557 +#define GL_SRC_ALPHA 0
558 +#define GL_ONE_MINUS_SRC_ALPHA 1
559 +#define GL_RGBA 2
560 +#define GL_ONE 3
561  
562 -static inline void assert_gl(const char* message)
563 -{
564 -#ifdef DEBUG
565 -  check_gl_error(message);
566 -#else
567 -  (void) message;
568  #endif
569 -}
570 -
571 -#endif
572 diff -aur supertux/src/video/surface.cpp supertux-nogl/src/video/surface.cpp
573 --- supertux/src/video/surface.cpp      2006-07-03 02:00:48.000000000 +0200
574 +++ supertux-nogl/src/video/surface.cpp 2006-07-03 02:00:29.000000000 +0200
575 @@ -28,6 +28,7 @@
576  
577  #include <SDL.h>
578  #include <SDL_image.h>
579 +#include <SDL_rotozoom.h>
580  
581  #include "gameconfig.hpp"
582  #include "physfs/physfs_sdl.hpp"
583 @@ -41,13 +42,13 @@
584  {
585    texture = texture_manager->get(file);
586    texture->ref();
587 -  uv_left = 0;
588 -  uv_top = 0;
589 -  uv_right = texture->get_uv_right();
590 -  uv_bottom = texture->get_uv_bottom();
591  
592 -  width = texture->get_image_width();
593 -  height = texture->get_image_height();
594 +  offsetx = 0;
595 +  offsety = 0;
596 +  width = static_cast<int>(texture->get_image_width());
597 +  height = static_cast<int>(texture->get_image_height());
598 +
599 +  flipx = false;
600  }
601  
602  Surface::Surface(const std::string& file, int x, int y, int w, int h)
603 @@ -55,15 +56,12 @@
604    texture = texture_manager->get(file);
605    texture->ref();
606  
607 -  float tex_w = static_cast<float> (texture->get_width());
608 -  float tex_h = static_cast<float> (texture->get_height());
609 -  uv_left = static_cast<float>(x) / tex_w;
610 -  uv_top = static_cast<float>(y) / tex_h;
611 -  uv_right = static_cast<float>(x+w) / tex_w;
612 -  uv_bottom = static_cast<float>(y+h) / tex_h;
613 -
614 +  offsetx = x;
615 +  offsety = y;
616    width = w;
617    height = h;
618 +
619 +  flipx = false;
620  }
621  
622  Surface::Surface(const Surface& other)
623 @@ -71,12 +69,12 @@
624    texture = other.texture;
625    texture->ref();
626  
627 -  uv_left = other.uv_left;
628 -  uv_top = other.uv_top;
629 -  uv_right = other.uv_right;
630 -  uv_bottom = other.uv_bottom;
631 +  offsetx = other.offsetx;
632 +  offsety = other.offsety;
633    width = other.width;
634    height = other.height;
635 +
636 +  flipx = other.flipx;
637  }
638  
639  const Surface&
640 @@ -86,52 +84,33 @@
641    texture->unref();
642    texture = other.texture;
643  
644 -  uv_left = other.uv_left;
645 -  uv_top = other.uv_top;
646 -  uv_right = other.uv_right;
647 -  uv_bottom = other.uv_bottom;
648 +  offsetx = other.offsetx;
649 +  offsety = other.offsety;
650    width = other.width;
651    height = other.height;
652  
653 +  flipx = other.flipx;
654 +
655    return *this;
656  }
657  
658  Surface::~Surface()
659  {
660    texture->unref();
661 +
662 +  for (std::list<TransformedSurface*>::iterator i = transformedSurfaces.begin(); i != transformedSurfaces.end(); i++) {
663 +    SDL_FreeSurface((*i)->surface);
664 +    delete (*i);
665 +  }
666  }
667  
668  void
669  Surface::hflip()
670  {
671 -  std::swap(uv_left, uv_right);
672 +  flipx = !flipx;
673  }
674  
675 -static inline void intern_draw(float left, float top, float right, float bottom,                               float uv_left, float uv_top,
676 -                               float uv_right, float uv_bottom,
677 -                               DrawingEffect effect)
678 -{
679 -  if(effect & HORIZONTAL_FLIP)
680 -    std::swap(uv_left, uv_right);
681 -  if(effect & VERTICAL_FLIP) {
682 -    std::swap(uv_top, uv_bottom);
683 -  }
684 -  
685 -  glBegin(GL_QUADS);
686 -  glTexCoord2f(uv_left, uv_top);
687 -  glVertex2f(left, top);
688 -  
689 -  glTexCoord2f(uv_right, uv_top);
690 -  glVertex2f(right, top);
691 -
692 -  glTexCoord2f(uv_right, uv_bottom);
693 -  glVertex2f(right, bottom);
694 -
695 -  glTexCoord2f(uv_left, uv_bottom);
696 -  glVertex2f(left, bottom);
697 -  glEnd();
698 -}
699 -
700 +/*
701  static inline void intern_draw2(float left, float top, float right, float bottom,
702                                  float uv_left, float uv_top,
703                                  float uv_right, float uv_bottom,
704 @@ -182,51 +161,77 @@
705    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
706    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
707  }
708 +*/
709  
710  void
711 -Surface::draw(float x, float y, float alpha, float angle, const Color& color, const Blend& blend, DrawingEffect effect) const
712 +Surface::draw(float x, float y, float alpha, float, const Color&, const Blend&, DrawingEffect effect) const
713  {
714 -  glColor4f(1.0f, 1.0f, 1.0f, alpha);
715 -  glBindTexture(GL_TEXTURE_2D, texture->get_handle());
716 -
717 -  intern_draw2(x, y,
718 -               x + width, y + height,
719 -               uv_left, uv_top, uv_right, uv_bottom, 
720 -               angle,
721 -               color,
722 -               blend, 
723 -               effect);
724 +  draw_part(0, 0, x, y, width, height, alpha, effect);
725  }
726  
727  void
728  Surface::draw(float x, float y, float alpha, DrawingEffect effect) const
729  {
730 -  glColor4f(1.0f, 1.0f, 1.0f, alpha);
731 -  glBindTexture(GL_TEXTURE_2D, texture->get_handle());
732 -
733 -  intern_draw(x, y,
734 -              x + width, y + height,
735 -              uv_left, uv_top, uv_right, uv_bottom, effect);
736 +  draw_part(0, 0, x, y, width, height, alpha, effect);
737  }
738  
739  void
740  Surface::draw_part(float src_x, float src_y, float dst_x, float dst_y,
741 -                   float width, float height, float alpha,
742 +                   float width, float height, float,
743                     DrawingEffect effect) const
744  {
745 -  float uv_width = uv_right - uv_left;
746 -  float uv_height = uv_bottom - uv_top;
747 -  
748 -  float uv_left = this->uv_left + (uv_width * src_x) / this->width;
749 -  float uv_top = this->uv_top + (uv_height * src_y) / this->height;
750 -  float uv_right = this->uv_left + (uv_width * (src_x + width)) / this->width;
751 -  float uv_bottom = this->uv_top + (uv_height * (src_y + height)) / this->height;
752 -  
753 -  glColor4f(1.0f, 1.0f, 1.0f, alpha);
754 -  glBindTexture(GL_TEXTURE_2D, texture->get_handle());  
755 -  
756 -  intern_draw(dst_x, dst_y,
757 -              dst_x + width, dst_y + height,
758 -              uv_left, uv_top, uv_right, uv_bottom, effect);
759 +  //FIXME: support parameter "alpha"
760 +  SDL_Surface* surface = texture->getSurface();
761 +
762 +  // get and check SDL_Surface
763 +  if (surface == 0) {
764 +    std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
765 +    return;
766 +  }    
767 +
768 +  SDL_Surface* transformedSurface = surface;
769 +
770 +  if (flipx) effect = HORIZONTAL_FLIP;
771 +
772 +  if (effect != NO_EFFECT) {
773 +    transformedSurface = 0;
774 +
775 +    // check if we have this effect buffered
776 +    for (std::list<TransformedSurface*>::const_iterator i = transformedSurfaces.begin(); i != transformedSurfaces.end(); i++) {
777 +      if ((*i)->effect == effect) transformedSurface = (*i)->surface;
778 +    }
779 +
780 +    // if not: transform and buffer
781 +    if (!transformedSurface) {
782 +      if (effect == HORIZONTAL_FLIP) transformedSurface = zoomSurface(surface, -1, 1, 0);
783 +      if (effect == VERTICAL_FLIP) transformedSurface = zoomSurface(surface, 1, -1, 0);
784 +      if (transformedSurface == 0) {
785 +        std::cerr << "Warning: No known transformation applies to surface, skipped draw" << std::endl;
786 +        return;
787 +      }        
788 +      TransformedSurface* su = new TransformedSurface();
789 +      su->surface = transformedSurface;
790 +      su->effect = effect;
791 +
792 +      transformedSurfaces.push_front(su);
793 +    }
794 +  }
795 +
796 +  int ox = offsetx; if (effect == HORIZONTAL_FLIP) ox = static_cast<int>(surface->w) - (ox+static_cast<int>(width));
797 +  int oy = offsety; if (effect == VERTICAL_FLIP) oy = static_cast<int>(surface->h) - (oy+static_cast<int>(height));
798 +  // draw surface to screen
799 +  SDL_Surface* screen = SDL_GetVideoSurface();
800 +
801 +  SDL_Rect srcRect;
802 +  srcRect.x = static_cast<int>(ox+src_x);
803 +  srcRect.y = static_cast<int>(oy+src_y);
804 +  srcRect.w = static_cast<int>(width);
805 +  srcRect.h = static_cast<int>(height);
806 +
807 +  SDL_Rect dstRect;
808 +  dstRect.x = static_cast<int>(dst_x);
809 +  dstRect.y = static_cast<int>(dst_y);
810 +
811 +  SDL_BlitSurface(transformedSurface, &srcRect, screen, &dstRect);
812  }
813  
814 diff -aur supertux/src/video/surface.hpp supertux-nogl/src/video/surface.hpp
815 --- supertux/src/video/surface.hpp      2006-07-03 02:00:48.000000000 +0200
816 +++ supertux-nogl/src/video/surface.hpp 2006-07-03 02:00:29.000000000 +0200
817 @@ -20,7 +20,9 @@
818  #ifndef __SURFACE_HPP__
819  #define __SURFACE_HPP__
820  
821 +#include <SDL_image.h>
822  #include <string>
823 +#include <list>
824  
825  class Color;
826  class Blend;
827 @@ -37,6 +39,15 @@
828  };
829  
830  /**
831 + * Helper class to buffer a pre-transformed SDL_Surface
832 + */
833 +class TransformedSurface {
834 +    public:
835 +       SDL_Surface* surface;
836 +       DrawingEffect effect;
837 +};
838 +
839 +/**
840   * A rectangular image.
841   * The class basically holds a reference to a texture with additional UV
842   * coordinates that specify a rectangular area on this texture
843 @@ -48,19 +59,26 @@
844    friend class Font;
845    ImageTexture* texture;
846  
847 -  float uv_left;
848 -  float uv_top;
849 -  float uv_right;
850 -  float uv_bottom;
851 +  bool flipx;
852  
853 +  /** draw the surface on the screen, applying a ::DrawingEffect on-the-fly. Transformed Surfaces will be cached in ::transformedSurfaces */
854    void draw(float x, float y, float alpha, float angle, const Color& color, const Blend& blend, DrawingEffect effect) const;
855 +
856 +  /** draw the surface on the screen, applying a ::DrawingEffect on-the-fly. Transformed Surfaces will be cached in ::transformedSurfaces */
857    void draw(float x, float y, float alpha, DrawingEffect effect) const;
858 +
859 +  /** draw the surface on the screen, applying a ::DrawingEffect on-the-fly. Transformed Surfaces will be cached in ::transformedSurfaces */
860    void draw_part(float src_x, float src_y, float dst_x, float dst_y,
861                   float width, float height,
862                   float alpha, DrawingEffect effect) const;
863  
864 -  float width;
865 -  float height;
866 +  int offsetx; /**< Region in ::surface to be used for blitting */
867 +  int offsety; /**< Region in ::surface to be used for blitting */
868 +  int width;   /**< Region in ::surface to be used for blitting */
869 +  int height;  /**< Region in ::surface to be used for blitting */
870 +
871 +  mutable std::list<TransformedSurface*> transformedSurfaces; /**< Cache for pre-transformed surfaces */
872 +
873  public:
874    Surface(const std::string& file);
875    Surface(const std::string& file, int x, int y, int w, int h);
876 diff -aur supertux/src/video/texture.cpp supertux-nogl/src/video/texture.cpp
877 --- supertux/src/video/texture.cpp      2006-07-03 02:00:48.000000000 +0200
878 +++ supertux-nogl/src/video/texture.cpp 2006-07-03 02:00:29.000000000 +0200
879 @@ -21,7 +21,6 @@
880  
881  #include "texture.hpp"
882  
883 -#include <GL/gl.h>
884  #include <assert.h>
885  #include "glutil.hpp"
886  
887 @@ -30,81 +29,37 @@
888    return (v & (v-1)) == 0;
889  }
890  
891 -Texture::Texture(unsigned int w, unsigned int h, GLenum glformat)
892 +Texture::Texture(unsigned int w, unsigned int h, GLenum)
893  {
894    assert(is_power_of_2(w));
895    assert(is_power_of_2(h));
896  
897    this->width = w;
898    this->height = h;
899 -  
900 -  assert_gl("before creating texture");
901 -  glGenTextures(1, &handle);
902 -  
903 -  try {
904 -    glBindTexture(GL_TEXTURE_2D, handle);
905 -
906 -    glTexImage2D(GL_TEXTURE_2D, 0, glformat, width, height, 0, GL_RGBA,
907 -                 GL_UNSIGNED_BYTE, 0);
908 -
909 -    set_texture_params();
910 -  } catch(...) {
911 -    glDeleteTextures(1, &handle);
912 -    throw;
913 -  }
914 +
915 +  surface = 0;
916  }
917  
918 -Texture::Texture(SDL_Surface* image, GLenum glformat)
919 +Texture::Texture(SDL_Surface* image, GLenum)
920  {
921    const SDL_PixelFormat* format = image->format;
922    if(!is_power_of_2(image->w) || !is_power_of_2(image->h))
923      throw std::runtime_error("image has no power of 2 size");
924    if(format->BitsPerPixel != 24 && format->BitsPerPixel != 32)
925      throw std::runtime_error("image has no 24 or 32 bit color depth");
926 -  
927 +
928    this->width = image->w;
929    this->height = image->h;
930  
931 -  assert_gl("before creating texture");
932 -  glGenTextures(1, &handle);
933 -  
934 -  try {
935 -    GLenum sdl_format;
936 -    if(format->BytesPerPixel == 3)
937 -      sdl_format = GL_RGB;
938 -    else if(format->BytesPerPixel == 4)
939 -      sdl_format = GL_RGBA;
940 -    else
941 -      assert(false);
942 -
943 -    glBindTexture(GL_TEXTURE_2D, handle);
944 -    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
945 -    glPixelStorei(GL_UNPACK_ROW_LENGTH, image->pitch/format->BytesPerPixel);
946 -    glTexImage2D(GL_TEXTURE_2D, 0, glformat, width, height, 0, sdl_format,
947 -            GL_UNSIGNED_BYTE, image->pixels);
948 -
949 -    assert_gl("creating texture");
950 -
951 -    set_texture_params();    
952 -  } catch(...) {
953 -    glDeleteTextures(1, &handle);
954 -    throw;
955 -  }
956 +  surface = SDL_DisplayFormatAlpha(image);
957  }
958  
959  Texture::~Texture()
960  {
961 -  glDeleteTextures(1, &handle);
962  }
963  
964  void
965  Texture::set_texture_params()
966  {
967 -  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
968 -  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
969 -  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
970 -  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
971 -
972 -  assert_gl("set texture params");
973  }
974  
975 diff -aur supertux/src/video/texture.hpp supertux-nogl/src/video/texture.hpp
976 --- supertux/src/video/texture.hpp      2006-07-03 02:00:48.000000000 +0200
977 +++ supertux-nogl/src/video/texture.hpp 2006-07-03 02:00:29.000000000 +0200
978 @@ -21,7 +21,7 @@
979  #define __TEXTURE_HPP__
980  
981  #include <SDL.h>
982 -#include <GL/gl.h>
983 +#include "glutil.hpp"
984  
985  /**
986   * This class is a wrapper around a texture handle. It stores the texture width
987 @@ -31,8 +31,9 @@
988  class Texture
989  {
990  protected:
991 +  SDL_Surface* surface; /**< non-GL branch stores optimized surface */
992 +
993    friend class TextureManager;
994 -  GLuint handle;
995    unsigned int width;
996    unsigned int height;
997  
998 @@ -41,11 +42,6 @@
999    Texture(SDL_Surface* surface, GLenum glformat);
1000    virtual ~Texture();
1001    
1002 -  GLuint get_handle() const
1003 -  {
1004 -    return handle;
1005 -  }
1006 -
1007    unsigned int get_width() const
1008    {
1009      return width;
1010 @@ -56,6 +52,14 @@
1011      return height;
1012    }
1013  
1014 +  SDL_Surface* getSurface() {
1015 +    return surface;
1016 +  }
1017 +
1018 +  void setSurface(SDL_Surface* surface) {
1019 +    this->surface = surface;
1020 +  }
1021 +
1022  private:
1023    void set_texture_params();
1024  };
1025 diff -aur supertux/src/video/texture_manager.cpp supertux-nogl/src/video/texture_manager.cpp
1026 --- supertux/src/video/texture_manager.cpp      2006-07-03 02:00:48.000000000 +0200
1027 +++ supertux-nogl/src/video/texture_manager.cpp 2006-07-03 02:00:29.000000000 +0200
1028 @@ -24,8 +24,6 @@
1029  #include <assert.h>
1030  #include <SDL.h>
1031  #include <SDL_image.h>
1032 -#include <GL/gl.h>
1033 -#include <GL/glext.h>
1034  #include <iostream>
1035  #include <sstream>
1036  #include <stdexcept>
1037 @@ -145,12 +143,6 @@
1038  void
1039  TextureManager::save_textures()
1040  {
1041 -  glPixelStorei(GL_PACK_ROW_LENGTH, 0);
1042 -  glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
1043 -  glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
1044 -  glPixelStorei(GL_PACK_SKIP_ROWS, 0);
1045 -  glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
1046 -  glPixelStorei(GL_PACK_ALIGNMENT, 1);
1047    for(Textures::iterator i = textures.begin(); i != textures.end(); ++i) {
1048      save_texture(*i);
1049    }
1050 @@ -165,75 +157,16 @@
1051  {
1052    SavedTexture saved_texture;
1053    saved_texture.texture = texture;
1054 -  glBindTexture(GL_TEXTURE_2D, texture->get_handle());
1055 -  glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
1056 -                           &saved_texture.width);
1057 -  glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
1058 -                           &saved_texture.height);
1059 -  glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER,
1060 -                           &saved_texture.border);
1061 -  glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
1062 -                      &saved_texture.min_filter);
1063 -  glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
1064 -                      &saved_texture.mag_filter);
1065 -  glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
1066 -                      &saved_texture.wrap_s);
1067 -  glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
1068 -                      &saved_texture.wrap_t);
1069  
1070    size_t pixelssize = saved_texture.width * saved_texture.height * 4;
1071    saved_texture.pixels = new char[pixelssize];
1072 -  
1073 -  glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,
1074 -                saved_texture.pixels);
1075  
1076    saved_textures.push_back(saved_texture);
1077 -
1078 -  glDeleteTextures(1, &(texture->handle));
1079 -  texture->handle = 0;
1080 -
1081 -  assert_gl("retrieving texture for save");
1082  }
1083  
1084  void
1085  TextureManager::reload_textures()
1086  {
1087 -  glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1088 -  glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
1089 -  glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
1090 -  glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
1091 -  glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
1092 -  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
1093 -  
1094 -  for(std::vector<SavedTexture>::iterator i = saved_textures.begin();
1095 -      i != saved_textures.end(); ++i) {
1096 -    SavedTexture& saved_texture = *i;
1097 -    
1098 -    GLuint handle;
1099 -    glGenTextures(1, &handle);
1100 -    assert_gl("creating texture handle");
1101 -
1102 -    glBindTexture(GL_TEXTURE_2D, handle);
1103 -    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
1104 -                 saved_texture.width, saved_texture.height,
1105 -                 saved_texture.border, GL_RGBA,
1106 -                 GL_UNSIGNED_BYTE, saved_texture.pixels);
1107 -    delete[] saved_texture.pixels;
1108 -    assert_gl("uploading texture pixel data");
1109 -
1110 -    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
1111 -                    saved_texture.min_filter);
1112 -    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
1113 -                    saved_texture.mag_filter);
1114 -    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
1115 -                    saved_texture.wrap_s);
1116 -    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
1117 -                    saved_texture.wrap_t);
1118 -
1119 -    assert_gl("setting texture_params");
1120 -    saved_texture.texture->handle = handle;
1121 -  }
1122 -
1123    saved_textures.clear();
1124  }
1125  
1126 diff -aur supertux/src/video/texture_manager.hpp supertux-nogl/src/video/texture_manager.hpp
1127 --- supertux/src/video/texture_manager.hpp      2006-07-03 02:00:48.000000000 +0200
1128 +++ supertux-nogl/src/video/texture_manager.hpp 2006-07-03 02:00:29.000000000 +0200
1129 @@ -20,7 +20,7 @@
1130  #ifndef __IMAGE_TEXTURE_MANAGER_HPP__
1131  #define __IMAGE_TEXTURE_MANAGER_HPP__
1132  
1133 -#include <GL/gl.h>
1134 +#include "video/glutil.hpp"
1135  #include <string>
1136  #include <vector>
1137  #include <map>