Merged -nogl update by gnomino
[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 4099. 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 @@ -120,6 +120,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 @@ -139,11 +147,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 @@ -26,7 +26,7 @@
140  Application supertux : $(sources) $(wrapper_objects) : linkerfile ;
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 squirrel ;
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 @@ -308,7 +307,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 @@ -338,24 +337,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,23 +253,55 @@
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 +  int width = 800;
354 +  int height = 600;
355 +  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);
356  
357    delete gradientrequest;
358  }
359 @@ -294,18 +327,13 @@
360    float w = fillrectrequest->size.x;
361    float h = fillrectrequest->size.y;
362  
363 -  glDisable(GL_TEXTURE_2D);
364 -  glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
365 -            fillrectrequest->color.blue, fillrectrequest->color.alpha);
366 -
367 -  glBegin(GL_QUADS);
368 -  glVertex2f(x, y);
369 -  glVertex2f(x+w, y);
370 -  glVertex2f(x+w, y+h);
371 -  glVertex2f(x, y+h);
372 -  glEnd();
373 -  glEnable(GL_TEXTURE_2D);
374 +  int r = static_cast<int>(fillrectrequest->color.red);
375 +  int g = static_cast<int>(fillrectrequest->color.green);
376 +  int b = static_cast<int>(fillrectrequest->color.blue);
377 +  int a = static_cast<int>(fillrectrequest->color.alpha);
378  
379 +  ::fillrect(screen, x, y, w, h, r, g, b, a);
380 +
381    delete fillrectrequest;
382  }
383  
384 @@ -319,67 +347,10 @@
385    transformstack.clear();
386    target_stack.clear();
387  
388 -  bool use_lightmap = lightmap_requests.size() != 0;
389 -
390 -  // PART1: create lightmap
391 -  if(use_lightmap) {
392 -    glViewport(0, screen->h - lightmap_height, lightmap_width, lightmap_height);
393 -    glMatrixMode(GL_PROJECTION);
394 -    glLoadIdentity();
395 -    glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
396 -    glMatrixMode(GL_MODELVIEW);
397 -    glLoadIdentity();
398 -
399 -    // FIXME: Add ambient light support here
400 -    glClearColor(0.3, 0.3, 0.4, 1);
401 -    glClear(GL_COLOR_BUFFER_BIT);
402 -    handle_drawing_requests(lightmap_requests);
403 -    lightmap_requests.clear();
404 -
405 -    glDisable(GL_BLEND);
406 -    glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
407 -    glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, screen->h - lightmap_height, lightmap_width, lightmap_height);
408 -
409 -    glViewport(0, 0, screen->w, screen->h);
410 -    glMatrixMode(GL_PROJECTION);
411 -    glLoadIdentity();
412 -    glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
413 -    glMatrixMode(GL_MODELVIEW);
414 -    glLoadIdentity();
415 -    glEnable(GL_BLEND);
416 -  }
417 -
418 -  //glClear(GL_COLOR_BUFFER_BIT);
419    handle_drawing_requests(drawing_requests);
420    drawing_requests.clear();
421  
422 -  if(use_lightmap) {
423 -    // multiple the lightmap with the framebuffer
424 -    glBlendFunc(GL_DST_COLOR, GL_ZERO);
425 -
426 -    glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
427 -    glBegin(GL_QUADS);
428 -
429 -    glTexCoord2f(0, lightmap_uv_bottom);
430 -    glVertex2f(0, 0);
431 -
432 -    glTexCoord2f(lightmap_uv_right, lightmap_uv_bottom);
433 -    glVertex2f(SCREEN_WIDTH, 0);
434 -
435 -    glTexCoord2f(lightmap_uv_right, 0);
436 -    glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
437 -
438 -    glTexCoord2f(0, 0);
439 -    glVertex2f(0, SCREEN_HEIGHT);
440 -
441 -    glEnd();
442 -
443 -    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
444 -  }
445 -
446 -  assert_gl("drawing");
447 -
448 -  SDL_GL_SwapBuffers();
449 +  SDL_Flip(screen);
450  }
451  
452  void
453 @@ -471,8 +442,4 @@
454  DrawingContext::set_target(Target target)
455  {
456    this->target = target;
457 -  if(target == LIGHTMAP)
458 -    requests = &lightmap_requests;
459 -  else
460 -    requests = &drawing_requests;
461  }
462 diff -aur supertux/src/video/drawing_context.hpp supertux-nogl/src/video/drawing_context.hpp
463 --- supertux/src/video/drawing_context.hpp      2006-07-03 02:00:48.000000000 +0200
464 +++ supertux-nogl/src/video/drawing_context.hpp 2006-07-03 02:00:29.000000000 +0200
465 @@ -24,7 +24,6 @@
466  #include <string>
467  #include <stdint.h>
468  
469 -#include <GL/gl.h>
470  #include <SDL.h>
471  #include <stdint.h>
472  #include <memory>
473 @@ -35,8 +34,9 @@
474  #include "font.hpp"
475  #include "color.hpp"
476  
477 +#include "glutil.hpp"
478 +
479  class Surface;
480 -class Texture;
481  
482  // some constants for predefined layer values
483  enum {
484 @@ -222,16 +222,12 @@
485    void draw_filled_rect(DrawingRequest& request);
486  
487    DrawingRequests drawing_requests;
488 -  DrawingRequests lightmap_requests;
489  
490    DrawingRequests* requests;
491  
492    SDL_Surface* screen;
493    Target target;
494    std::vector<Target> target_stack;
495 -  Texture* lightmap;
496 -  int lightmap_width, lightmap_height;
497 -  float lightmap_uv_right, lightmap_uv_bottom;
498  };
499  
500  #endif
501 diff -aur supertux/src/video/glutil.hpp supertux-nogl/src/video/glutil.hpp
502 --- supertux/src/video/glutil.hpp       2006-07-03 02:00:48.000000000 +0200
503 +++ supertux-nogl/src/video/glutil.hpp  2006-07-03 02:00:29.000000000 +0200
504 @@ -21,58 +21,12 @@
505  
506  #include <sstream>
507  #include <stdexcept>
508 -#include <GL/gl.h>
509  
510 -static inline void check_gl_error(const char* message)
511 -{
512 -#ifdef DEBUG
513 -  GLenum error = glGetError();
514 -  if(error != GL_NO_ERROR) {
515 -    std::ostringstream msg;
516 -    msg << "OpenGLError while '" << message << "': ";
517 -    switch(error) {
518 -      case GL_INVALID_ENUM:
519 -        msg << "INVALID_ENUM: An unacceptable value is specified for an "
520 -               "enumerated argument.";
521 -        break;
522 -      case GL_INVALID_VALUE:
523 -        msg << "INVALID_VALUE: A numeric argument is out of range.";
524 -        break;
525 -      case GL_INVALID_OPERATION:
526 -        msg << "INVALID_OPERATION: The specified operation is not allowed "
527 -               "in the current state.";
528 -        break;
529 -      case GL_STACK_OVERFLOW:
530 -        msg << "STACK_OVERFLOW: This command would cause a stack overflow.";
531 -        break;
532 -      case GL_STACK_UNDERFLOW:
533 -        msg << "STACK_UNDERFLOW: This command would cause a stack underflow.";
534 -        break;
535 -      case GL_OUT_OF_MEMORY:
536 -        msg << "OUT_OF_MEMORY: There is not enough memory left to execute the "
537 -               "command.";
538 -        break;
539 -#ifdef GL_TABLE_TOO_LARGE
540 -      case GL_TABLE_TOO_LARGE:
541 -        msg << "TABLE_TOO_LARGE: table is too large";
542 -        break;
543 -#endif
544 -      default:
545 -        msg << "Unknown error (code " << error << ")";
546 -    }
547 -
548 -    throw std::runtime_error(msg.str());
549 -  }
550 -#endif
551 -}
552 +#define GLenum int
553 +#define GLint int
554 +#define GL_SRC_ALPHA 0
555 +#define GL_ONE_MINUS_SRC_ALPHA 1
556 +#define GL_RGBA 2
557 +#define GL_ONE 3
558  
559 -static inline void assert_gl(const char* message)
560 -{
561 -#ifdef DEBUG
562 -  check_gl_error(message);
563 -#else
564 -  (void) message;
565  #endif
566 -}
567 -
568 -#endif
569 diff -aur supertux/src/video/surface.cpp supertux-nogl/src/video/surface.cpp
570 --- supertux/src/video/surface.cpp      2006-07-03 02:00:48.000000000 +0200
571 +++ supertux-nogl/src/video/surface.cpp 2006-07-03 02:00:29.000000000 +0200
572 @@ -28,6 +28,7 @@
573  
574  #include <SDL.h>
575  #include <SDL_image.h>
576 +#include <SDL_rotozoom.h>
577  
578  #include "gameconfig.hpp"
579  #include "physfs/physfs_sdl.hpp"
580 @@ -41,13 +42,13 @@
581  {
582    texture = texture_manager->get(file);
583    texture->ref();
584 -  uv_left = 0;
585 -  uv_top = 0;
586 -  uv_right = texture->get_uv_right();
587 -  uv_bottom = texture->get_uv_bottom();
588  
589 -  width = texture->get_image_width();
590 -  height = texture->get_image_height();
591 +  offsetx = 0;
592 +  offsety = 0;
593 +  width = static_cast<int>(texture->get_image_width());
594 +  height = static_cast<int>(texture->get_image_height());
595 +
596 +  flipx = false;
597  }
598  
599  Surface::Surface(const std::string& file, int x, int y, int w, int h)
600 @@ -55,15 +56,12 @@
601    texture = texture_manager->get(file);
602    texture->ref();
603  
604 -  float tex_w = static_cast<float> (texture->get_width());
605 -  float tex_h = static_cast<float> (texture->get_height());
606 -  uv_left = static_cast<float>(x) / tex_w;
607 -  uv_top = static_cast<float>(y) / tex_h;
608 -  uv_right = static_cast<float>(x+w) / tex_w;
609 -  uv_bottom = static_cast<float>(y+h) / tex_h;
610 -
611 +  offsetx = x;
612 +  offsety = y;
613    width = w;
614    height = h;
615 +
616 +  flipx = false;
617  }
618  
619  Surface::Surface(const Surface& other)
620 @@ -71,12 +69,12 @@
621    texture = other.texture;
622    texture->ref();
623  
624 -  uv_left = other.uv_left;
625 -  uv_top = other.uv_top;
626 -  uv_right = other.uv_right;
627 -  uv_bottom = other.uv_bottom;
628 +  offsetx = other.offsetx;
629 +  offsety = other.offsety;
630    width = other.width;
631    height = other.height;
632 +
633 +  flipx = other.flipx;
634  }
635  
636  const Surface&
637 @@ -86,52 +84,33 @@
638    texture->unref();
639    texture = other.texture;
640  
641 -  uv_left = other.uv_left;
642 -  uv_top = other.uv_top;
643 -  uv_right = other.uv_right;
644 -  uv_bottom = other.uv_bottom;
645 +  offsetx = other.offsetx;
646 +  offsety = other.offsety;
647    width = other.width;
648    height = other.height;
649  
650 +  flipx = other.flipx;
651 +
652    return *this;
653  }
654  
655  Surface::~Surface()
656  {
657    texture->unref();
658 +
659 +  for (std::list<TransformedSurface*>::iterator i = transformedSurfaces.begin(); i != transformedSurfaces.end(); i++) {
660 +    SDL_FreeSurface((*i)->surface);
661 +    delete (*i);
662 +  }
663  }
664  
665  void
666  Surface::hflip()
667  {
668 -  std::swap(uv_left, uv_right);
669 +  flipx = !flipx;
670  }
671  
672 -static inline void intern_draw(float left, float top, float right, float bottom,                               float uv_left, float uv_top,
673 -                               float uv_right, float uv_bottom,
674 -                               DrawingEffect effect)
675 -{
676 -  if(effect & HORIZONTAL_FLIP)
677 -    std::swap(uv_left, uv_right);
678 -  if(effect & VERTICAL_FLIP) {
679 -    std::swap(uv_top, uv_bottom);
680 -  }
681 -
682 -  glBegin(GL_QUADS);
683 -  glTexCoord2f(uv_left, uv_top);
684 -  glVertex2f(left, top);
685 -
686 -  glTexCoord2f(uv_right, uv_top);
687 -  glVertex2f(right, top);
688 -
689 -  glTexCoord2f(uv_right, uv_bottom);
690 -  glVertex2f(right, bottom);
691 -
692 -  glTexCoord2f(uv_left, uv_bottom);
693 -  glVertex2f(left, bottom);
694 -  glEnd();
695 -}
696 -
697 +/*
698  static inline void intern_draw2(float left, float top, float right, float bottom,
699                                  float uv_left, float uv_top,
700                                  float uv_right, float uv_bottom,
701 @@ -182,50 +161,76 @@
702    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
703    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
704  }
705 +*/
706  
707  void
708 -Surface::draw(float x, float y, float alpha, float angle, const Color& color, const Blend& blend, DrawingEffect effect) const
709 +Surface::draw(float x, float y, float alpha, float, const Color&, const Blend&, DrawingEffect effect) const
710  {
711 -  glColor4f(1.0f, 1.0f, 1.0f, alpha);
712 -  glBindTexture(GL_TEXTURE_2D, texture->get_handle());
713 -
714 -  intern_draw2(x, y,
715 -               x + width, y + height,
716 -               uv_left, uv_top, uv_right, uv_bottom,
717 -               angle,
718 -               color,
719 -               blend,
720 -               effect);
721 +  draw_part(0, 0, x, y, width, height, alpha, effect);
722  }
723  
724  void
725  Surface::draw(float x, float y, float alpha, DrawingEffect effect) const
726  {
727 -  glColor4f(1.0f, 1.0f, 1.0f, alpha);
728 -  glBindTexture(GL_TEXTURE_2D, texture->get_handle());
729 -
730 -  intern_draw(x, y,
731 -              x + width, y + height,
732 -              uv_left, uv_top, uv_right, uv_bottom, effect);
733 +  draw_part(0, 0, x, y, width, height, alpha, effect);
734  }
735  
736  void
737  Surface::draw_part(float src_x, float src_y, float dst_x, float dst_y,
738 -                   float width, float height, float alpha,
739 +                   float width, float height, float ,
740                     DrawingEffect effect) const
741  {
742 -  float uv_width = uv_right - uv_left;
743 -  float uv_height = uv_bottom - uv_top;
744 +  //FIXME: support parameter "alpha"
745 +  SDL_Surface* surface = texture->getSurface();
746  
747 -  float uv_left = this->uv_left + (uv_width * src_x) / this->width;
748 -  float uv_top = this->uv_top + (uv_height * src_y) / this->height;
749 -  float uv_right = this->uv_left + (uv_width * (src_x + width)) / this->width;
750 -  float uv_bottom = this->uv_top + (uv_height * (src_y + height)) / this->height;
751 +  // get and check SDL_Surface
752 +  if (surface == 0) {
753 +    std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
754 +    return;
755 +  }    
756  
757 -  glColor4f(1.0f, 1.0f, 1.0f, alpha);
758 -  glBindTexture(GL_TEXTURE_2D, texture->get_handle());
759 +  SDL_Surface* transformedSurface = surface;
760  
761 -  intern_draw(dst_x, dst_y,
762 -              dst_x + width, dst_y + height,
763 -              uv_left, uv_top, uv_right, uv_bottom, effect);
764 +  if (flipx) effect = HORIZONTAL_FLIP;
765 +
766 +  if (effect != NO_EFFECT) {
767 +    transformedSurface = 0;
768 +
769 +    // check if we have this effect buffered
770 +    for (std::list<TransformedSurface*>::const_iterator i = transformedSurfaces.begin(); i != transformedSurfaces.end(); i++) {
771 +      if ((*i)->effect == effect) transformedSurface = (*i)->surface;
772 +    }
773 +
774 +    // if not: transform and buffer
775 +    if (!transformedSurface) {
776 +      if (effect == HORIZONTAL_FLIP) transformedSurface = zoomSurface(surface, -1, 1, 0);
777 +      if (effect == VERTICAL_FLIP) transformedSurface = zoomSurface(surface, 1, -1, 0);
778 +      if (transformedSurface == 0) {
779 +        std::cerr << "Warning: No known transformation applies to surface, skipped draw" << std::endl;
780 +        return;
781 +      }        
782 +      TransformedSurface* su = new TransformedSurface();
783 +      su->surface = transformedSurface;
784 +      su->effect = effect;
785 +
786 +      transformedSurfaces.push_front(su);
787 +    }
788 +  }
789 +
790 +  int ox = offsetx; if (effect == HORIZONTAL_FLIP) ox = static_cast<int>(surface->w) - (ox+static_cast<int>(width));
791 +  int oy = offsety; if (effect == VERTICAL_FLIP) oy = static_cast<int>(surface->h) - (oy+static_cast<int>(height));
792 +  // draw surface to screen
793 +  SDL_Surface* screen = SDL_GetVideoSurface();
794 +
795 +  SDL_Rect srcRect;
796 +  srcRect.x = static_cast<int>(ox+src_x);
797 +  srcRect.y = static_cast<int>(oy+src_y);
798 +  srcRect.w = static_cast<int>(width);
799 +  srcRect.h = static_cast<int>(height);
800 +
801 +  SDL_Rect dstRect;
802 +  dstRect.x = static_cast<int>(dst_x);
803 +  dstRect.y = static_cast<int>(dst_y);
804 +
805 +  SDL_BlitSurface(transformedSurface, &srcRect, screen, &dstRect);
806  }
807 diff -aur supertux/src/video/surface.hpp supertux-nogl/src/video/surface.hpp
808 --- supertux/src/video/surface.hpp      2006-07-03 02:00:48.000000000 +0200
809 +++ supertux-nogl/src/video/surface.hpp 2006-07-03 02:00:29.000000000 +0200
810 @@ -20,7 +20,9 @@
811  #ifndef __SURFACE_HPP__
812  #define __SURFACE_HPP__
813  
814 +#include <SDL_image.h>
815  #include <string>
816 +#include <list>
817  
818  class Color;
819  class Blend;
820 @@ -37,6 +39,15 @@
821  };
822  
823  /**
824 + * Helper class to buffer a pre-transformed SDL_Surface
825 + */
826 +class TransformedSurface {
827 +    public:
828 +       SDL_Surface* surface;
829 +       DrawingEffect effect;
830 +};
831 +
832 +/**
833   * A rectangular image.
834   * The class basically holds a reference to a texture with additional UV
835   * coordinates that specify a rectangular area on this texture
836 @@ -48,19 +59,26 @@
837    friend class Font;
838    ImageTexture* texture;
839  
840 -  float uv_left;
841 -  float uv_top;
842 -  float uv_right;
843 -  float uv_bottom;
844 +  bool flipx;
845  
846 +  /** draw the surface on the screen, applying a ::DrawingEffect on-the-fly. Transformed Surfaces will be cached in ::transformedSurfaces */
847    void draw(float x, float y, float alpha, float angle, const Color& color, const Blend& blend, DrawingEffect effect) const;
848 +  
849 +  /** draw the surface on the screen, applying a ::DrawingEffect on-the-fly. Transformed Surfaces will be cached in ::transformedSurfaces */
850    void draw(float x, float y, float alpha, DrawingEffect effect) const;
851 +  
852 +  /** draw the surface on the screen, applying a ::DrawingEffect on-the-fly. Transformed Surfaces will be cached in ::transformedSurfaces */
853    void draw_part(float src_x, float src_y, float dst_x, float dst_y,
854                   float width, float height,
855                   float alpha, DrawingEffect effect) const;
856  
857 -  float width;
858 -  float height;
859 +  int offsetx; /**< Region in ::surface to be used for blitting */
860 +  int offsety; /**< Region in ::surface to be used for blitting */
861 +  int width;   /**< Region in ::surface to be used for blitting */
862 +  int height;  /**< Region in ::surface to be used for blitting */
863 +
864 +  mutable std::list<TransformedSurface*> transformedSurfaces; /**< Cache for pre-transformed surfaces */
865 +
866  public:
867    Surface(const std::string& file);
868    Surface(const std::string& file, int x, int y, int w, int h);
869 diff -aur supertux/src/video/texture.cpp supertux-nogl/src/video/texture.cpp
870 --- supertux/src/video/texture.cpp      2006-07-03 02:00:48.000000000 +0200
871 +++ supertux-nogl/src/video/texture.cpp 2006-07-03 02:00:29.000000000 +0200
872 @@ -21,7 +21,6 @@
873  
874  #include "texture.hpp"
875  
876 -#include <GL/gl.h>
877  #include <assert.h>
878  #include "glutil.hpp"
879  
880 @@ -30,7 +29,7 @@
881    return (v & (v-1)) == 0;
882  }
883  
884 -Texture::Texture(unsigned int w, unsigned int h, GLenum glformat)
885 +Texture::Texture(unsigned int w, unsigned int h, GLenum)
886  {
887    assert(is_power_of_2(w));
888    assert(is_power_of_2(h));
889 @@ -38,23 +37,10 @@
890    this->width = w;
891    this->height = h;
892  
893 -  assert_gl("before creating texture");
894 -  glGenTextures(1, &handle);
895 -
896 -  try {
897 -    glBindTexture(GL_TEXTURE_2D, handle);
898 -
899 -    glTexImage2D(GL_TEXTURE_2D, 0, glformat, width, height, 0, GL_RGBA,
900 -                 GL_UNSIGNED_BYTE, 0);
901 -
902 -    set_texture_params();
903 -  } catch(...) {
904 -    glDeleteTextures(1, &handle);
905 -    throw;
906 -  }
907 +  surface = 0;
908  }
909  
910 -Texture::Texture(SDL_Surface* image, GLenum glformat)
911 +Texture::Texture(SDL_Surface* image, GLenum)
912  {
913    const SDL_PixelFormat* format = image->format;
914    if(!is_power_of_2(image->w) || !is_power_of_2(image->h))
915 @@ -65,45 +51,14 @@
916    this->width = image->w;
917    this->height = image->h;
918  
919 -  assert_gl("before creating texture");
920 -  glGenTextures(1, &handle);
921 -
922 -  try {
923 -    GLenum sdl_format;
924 -    if(format->BytesPerPixel == 3)
925 -      sdl_format = GL_RGB;
926 -    else if(format->BytesPerPixel == 4)
927 -      sdl_format = GL_RGBA;
928 -    else
929 -      assert(false);
930 -
931 -    glBindTexture(GL_TEXTURE_2D, handle);
932 -    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
933 -    glPixelStorei(GL_UNPACK_ROW_LENGTH, image->pitch/format->BytesPerPixel);
934 -    glTexImage2D(GL_TEXTURE_2D, 0, glformat, width, height, 0, sdl_format,
935 -            GL_UNSIGNED_BYTE, image->pixels);
936 -
937 -    assert_gl("creating texture");
938 -
939 -    set_texture_params();
940 -  } catch(...) {
941 -    glDeleteTextures(1, &handle);
942 -    throw;
943 -  }
944 +  surface = SDL_DisplayFormatAlpha(image);
945  }
946  
947  Texture::~Texture()
948  {
949 -  glDeleteTextures(1, &handle);
950  }
951  
952  void
953  Texture::set_texture_params()
954  {
955 -  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
956 -  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
957 -  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
958 -  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
959 -
960 -  assert_gl("set texture params");
961  }
962 diff -aur supertux/src/video/texture.hpp supertux-nogl/src/video/texture.hpp
963 --- supertux/src/video/texture.hpp      2006-07-03 02:00:48.000000000 +0200
964 +++ supertux-nogl/src/video/texture.hpp 2006-07-03 02:00:29.000000000 +0200
965 @@ -21,7 +21,7 @@
966  #define __TEXTURE_HPP__
967  
968  #include <SDL.h>
969 -#include <GL/gl.h>
970 +#include "glutil.hpp"
971  
972  /**
973   * This class is a wrapper around a texture handle. It stores the texture width
974 @@ -31,8 +31,9 @@
975  class Texture
976  {
977  protected:
978 +  SDL_Surface* surface; /**< non-GL branch stores optimized surface */
979 +
980    friend class TextureManager;
981 -  GLuint handle;
982    unsigned int width;
983    unsigned int height;
984  
985 @@ -41,11 +42,6 @@
986    Texture(SDL_Surface* surface, GLenum glformat);
987    virtual ~Texture();
988  
989 -  GLuint get_handle() const
990 -  {
991 -    return handle;
992 -  }
993 -
994    unsigned int get_width() const
995    {
996      return width;
997 @@ -56,6 +52,14 @@
998      return height;
999    }
1000  
1001 +  SDL_Surface* getSurface() {
1002 +    return surface;
1003 +  }
1004 +
1005 +  void setSurface(SDL_Surface* surface) {
1006 +    this->surface = surface;
1007 +  }
1008 +
1009  private:
1010    void set_texture_params();
1011  };
1012 diff -aur supertux/src/video/texture_manager.cpp supertux-nogl/src/video/texture_manager.cpp
1013 --- supertux/src/video/texture_manager.cpp      2006-07-03 02:00:48.000000000 +0200
1014 +++ supertux-nogl/src/video/texture_manager.cpp 2006-07-03 02:00:29.000000000 +0200
1015 @@ -24,8 +24,6 @@
1016  #include <assert.h>
1017  #include <SDL.h>
1018  #include <SDL_image.h>
1019 -#include <GL/gl.h>
1020 -#include <GL/glext.h>
1021  #include <iostream>
1022  #include <sstream>
1023  #include <stdexcept>
1024 @@ -145,12 +143,6 @@
1025  void
1026  TextureManager::save_textures()
1027  {
1028 -  glPixelStorei(GL_PACK_ROW_LENGTH, 0);
1029 -  glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
1030 -  glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
1031 -  glPixelStorei(GL_PACK_SKIP_ROWS, 0);
1032 -  glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
1033 -  glPixelStorei(GL_PACK_ALIGNMENT, 1);
1034    for(Textures::iterator i = textures.begin(); i != textures.end(); ++i) {
1035      save_texture(*i);
1036    }
1037 @@ -165,74 +157,15 @@
1038  {
1039    SavedTexture saved_texture;
1040    saved_texture.texture = texture;
1041 -  glBindTexture(GL_TEXTURE_2D, texture->get_handle());
1042 -  glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
1043 -                           &saved_texture.width);
1044 -  glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
1045 -                           &saved_texture.height);
1046 -  glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER,
1047 -                           &saved_texture.border);
1048 -  glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
1049 -                      &saved_texture.min_filter);
1050 -  glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
1051 -                      &saved_texture.mag_filter);
1052 -  glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
1053 -                      &saved_texture.wrap_s);
1054 -  glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
1055 -                      &saved_texture.wrap_t);
1056  
1057    size_t pixelssize = saved_texture.width * saved_texture.height * 4;
1058    saved_texture.pixels = new char[pixelssize];
1059 -
1060 -  glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,
1061 -                saved_texture.pixels);
1062  
1063    saved_textures.push_back(saved_texture);
1064 -
1065 -  glDeleteTextures(1, &(texture->handle));
1066 -  texture->handle = 0;
1067 -
1068 -  assert_gl("retrieving texture for save");
1069  }
1070  
1071  void
1072  TextureManager::reload_textures()
1073  {
1074 -  glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1075 -  glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
1076 -  glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
1077 -  glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
1078 -  glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
1079 -  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
1080 -
1081 -  for(std::vector<SavedTexture>::iterator i = saved_textures.begin();
1082 -      i != saved_textures.end(); ++i) {
1083 -    SavedTexture& saved_texture = *i;
1084 -
1085 -    GLuint handle;
1086 -    glGenTextures(1, &handle);
1087 -    assert_gl("creating texture handle");
1088 -
1089 -    glBindTexture(GL_TEXTURE_2D, handle);
1090 -    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
1091 -                 saved_texture.width, saved_texture.height,
1092 -                 saved_texture.border, GL_RGBA,
1093 -                 GL_UNSIGNED_BYTE, saved_texture.pixels);
1094 -    delete[] saved_texture.pixels;
1095 -    assert_gl("uploading texture pixel data");
1096 -
1097 -    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
1098 -                    saved_texture.min_filter);
1099 -    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
1100 -                    saved_texture.mag_filter);
1101 -    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
1102 -                    saved_texture.wrap_s);
1103 -    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
1104 -                    saved_texture.wrap_t);
1105 -
1106 -    assert_gl("setting texture_params");
1107 -    saved_texture.texture->handle = handle;
1108 -  }
1109 -
1110    saved_textures.clear();
1111  }
1112 diff -aur supertux/src/video/texture_manager.hpp supertux-nogl/src/video/texture_manager.hpp
1113 --- supertux/src/video/texture_manager.hpp      2006-07-03 02:00:48.000000000 +0200
1114 +++ supertux-nogl/src/video/texture_manager.hpp 2006-07-03 02:00:29.000000000 +0200
1115 @@ -20,7 +20,7 @@
1116  #ifndef __IMAGE_TEXTURE_MANAGER_HPP__
1117  #define __IMAGE_TEXTURE_MANAGER_HPP__
1118  
1119 -#include <GL/gl.h>
1120 +#include "video/glutil.hpp"
1121  #include <string>
1122  #include <vector>
1123  #include <map>