update
[supertux.git] / src / screen.c
index b78058a..40d1e7b 100644 (file)
@@ -7,7 +7,7 @@
   bill@newbreedsoftware.com
   http://www.newbreedsoftware.com/supertux/
   
-  April 11, 2000 - April 22, 2000
+  April 11, 2000 - March 15, 2004
 */
 
 #include <stdio.h>
 #include "setup.h"
 #include "type.h"
 
-/* --- LOAD AND DISPLAY AN IMAGE --- */
-
-void load_and_display_image(char * file)
-{
-  SDL_Surface * img;
-  
-  img = load_image(file, IGNORE_ALPHA);
-  SDL_BlitSurface(img, NULL, screen, NULL);
-  SDL_FreeSurface(img);
-}
-
+/* Needed for line calculations */
+#define SGN(x) ((x)>0 ? 1 : ((x)==0 ? 0:(-1)))
+#define ABS(x) ((x)>0 ? (x) : (-x))
 
 /* --- CLEAR SCREEN --- */
 
-void clearscreen(float r, float g, float b)
+void clearscreen(int r, int g, int b)
 {
 #ifndef NOOPENGL
   if(use_gl)
-  {
-  glClearColor(r/256, g/256, b/256, 1.0);
-  glClear(GL_COLOR_BUFFER_BIT);
-  }
+    {
+      glClearColor(r/256, g/256, b/256, 1.0);
+      glClear(GL_COLOR_BUFFER_BIT);
+    }
   else
+  {
 #endif
-  SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
-}
-
 
-/* --- UPDATE SCREEN --- */
+    SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
+#ifndef NOOPENGL
 
-void updatescreen(void)
-{
-  if(use_gl)  /*clearscreen(0,0,0);*/
-  SDL_GL_SwapBuffers();
-  else
-    SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);
+    }
+#endif
 }
 
-void flipscreen(void)
+/* 'Stolen' from the SDL documentation.
+ * Set the pixel at (x, y) to the given value
+ * NOTE: The surface must be locked before calling this!
+ */
+void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
 {
-if(use_gl)
-SDL_GL_SwapBuffers();
-else
-SDL_Flip(screen);
-}
+  int bpp = surface->format->BytesPerPixel;
+  /* Here p is the address to the pixel we want to set */
+  Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
 
-/* --- LOAD AN IMAGE --- */
+  switch(bpp)
+    {
+    case 1:
+      *p = pixel;
+      break;
 
-SDL_Surface * load_image(char * file, int use_alpha)
-{
-/*
-if(!faccessible(file))
-{
-if(!faccessible(st_dir,
-*/
+    case 2:
+      *(Uint16 *)p = pixel;
+      break;
 
-  SDL_Surface * temp, * surf;
-  
-  temp = IMG_Load(file);
-
-  if (temp == NULL)
-    st_abort("Can't load", file);
-    
-  surf = SDL_DisplayFormatAlpha(temp);
-
-  if (surf == NULL)
-    st_abort("Can't covert to display format", file);
-    
-  if (use_alpha == IGNORE_ALPHA)
-    SDL_SetAlpha(surf, 0, 0);
-  
-  SDL_FreeSurface(temp);
+    case 3:
+      if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
+        {
+          p[0] = (pixel >> 16) & 0xff;
+          p[1] = (pixel >> 8) & 0xff;
+          p[2] = pixel & 0xff;
+        }
+      else
+        {
+          p[0] = pixel & 0xff;
+          p[1] = (pixel >> 8) & 0xff;
+          p[2] = (pixel >> 16) & 0xff;
+        }
+      break;
 
-  return(surf);
+    case 4:
+      *(Uint32 *)p = pixel;
+      break;
+    }
 }
 
-void update_rect(SDL_Surface *scr, Sint32 x, Sint32 y, Sint32 w, Sint32 h)
+/* Draw a single pixel on the screen. */
+void drawpixel(int x, int y, Uint32 pixel)
 {
-if(!use_gl)
-SDL_UpdateRect(scr, x, y, w, h);
+  /* Lock the screen for direct access to the pixels */
+  if ( SDL_MUSTLOCK(screen) )
+    {
+      if ( SDL_LockSurface(screen) < 0 )
+        {
+          fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
+          return;
+        }
+    }
+
+  if(!(x < 0 || y < 0 || x > screen->w || y > screen->h))
+    putpixel(screen, x, y, pixel);
+
+  if ( SDL_MUSTLOCK(screen) )
+    {
+      SDL_UnlockSurface(screen);
+    }
+  /* Update just the part of the display that we've changed */
+  SDL_UpdateRect(screen, x, y, 1, 1);
 }
 
-void drawtext(char * text, int x, int y, SDL_Surface * surf, int update, int shadowsize)
+void drawline(int x1, int y1, int x2, int y2, int r, int g, int b, int a)
 {
-       /* i - helps to keep tracking of the all string length
-               j - helps to keep track of the length of the current line */
-  int i, j;
-  char c;
-  SDL_Rect src, dest;
-  
-  /* For each letter in the string... */
-  
-  for (i = 0; i < strlen(text); i++)
+#ifndef NOOPENGL
+  if(use_gl)
     {
-      /* Set source rectangle: */
-      
-      c = text[i];
-      
-      if (c >= 'A' && c <= 'Z')
-       {
-         /* Capital letter - first row: */
-         
-         src.x = (c - 'A') * 16;
-         src.y = 0;
-       }
-      else if (c >= 'a' && c <= 'z')
-       {
-         /* Lowercase letter - first row: */
-         
-         src.x = (c - 'a') * 16;
-         src.y = 16;
-       }
-      else if (c >= '!' && c <= '9')
-       {
-         /* Punctuation (except '?') or number - third row: */
-         
-         src.x = (c - '!') * 16;
-         src.y = 32;
-       }
-      else if (c == '?')
+      glEnable(GL_BLEND);
+      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+      glColor4ub(r, g, b,a);
+
+      glBegin(GL_LINES);
+      glVertex2f(x1, y1);
+      glVertex2f(x2, y2);
+      glEnd();
+      glDisable(GL_BLEND);
+    }
+  else
+    {
+#endif
+
+      /* Basic unantialiased Bresenham line algorithm */
+      int lg_delta, sh_delta, cycle, lg_step, sh_step;
+      Uint32 color = SDL_MapRGBA(screen->format, r, g, b, a);
+
+      lg_delta = x2 - x1;
+      sh_delta = y2 - y1;
+      lg_step = SGN(lg_delta);
+      lg_delta = ABS(lg_delta);
+      sh_step = SGN(sh_delta);
+      sh_delta = ABS(sh_delta);
+      if (sh_delta < lg_delta)
+        {
+          cycle = lg_delta >> 1;
+          while (x1 != x2)
+            {
+              drawpixel(x1, y1, color);
+              cycle += sh_delta;
+              if (cycle > lg_delta)
+                {
+                  cycle -= lg_delta;
+                  y1 += sh_step;
+                }
+              x1 += lg_step;
+            }
+          drawpixel(x1, y1, color);
+        }
+      cycle = sh_delta >> 1;
+      while (y1 != y2)
+        {
+          drawpixel(x1, y1, color);
+          cycle += lg_delta;
+          if (cycle > sh_delta)
+            {
+              cycle -= sh_delta;
+              x1 += lg_step;
+            }
+          y1 += sh_step;
+        }
+      drawpixel(x1, y1, color);
+#ifndef NOOPENGL
+
+    }
+#endif
+}
+
+/* --- FILL A RECT --- */
+
+void fillrect(float x, float y, float w, float h, int r, int g, int b, int a)
+{
+if(w < 0)
        {
-         /* Question mark - third row, last character: */
-         
-         src.x = 400;
-         src.y = 24;
+       x += w;
+       w = -w;
        }
-       else if (c == '\n')             /* support for multi-lines */
+if(h < 0)
        {
-       j = i + 1;
-       y += 18;
-       continue;
+       y += h;
+       h = -h;
        }
-      else
-       src.x = -1;
-      
-      src.w = 16;
-      src.h = 16;
-      
-
-      /* Draw character: */
-      
-      if (src.x != -1)
-       {
-         /* Set destination rectangle for shadow: */
-         
-         dest.x = x + (i * 16) + shadowsize;
-         dest.y = y + shadowsize;
-         dest.w = src.w;
-         dest.h = src.h;
-         
-         
-         /* Shadow: */
-         
-         SDL_BlitSurface(letters_black, &src, screen, &dest);
-         
-         
-         /* Set destination rectangle for text: */
-         
-         dest.x = x + (i * 16);
-         dest.y = y;
-         dest.w = src.w;
-         dest.h = src.h;
-         
-         
-         /* Shadow: */
-         
-         SDL_BlitSurface(surf, &src, screen, &dest);
-         
-
-/* FIXME: Text doesn't work in OpenGL mode, below is experimental code */
-/*
 
-         dest.x = 0;
-         dest.y = 0; 
-         dest.w = src.w;
-         
-         dest.h = src.h;
-         
-         temp = SDL_CreateRGBSurface(SDL_SWSURFACE, dest.w, dest.h, 32,
-#if SDL_BYTEORDER == SDL_BIG_ENDIAN
-            0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
-#else
-            0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
-#endif
-         SDL_BlitSurface(letters_black, &src, temp, NULL);
-         texture_type xyz;
-         texture_from_sdl_surface(&xyz,temp,IGNORE_ALPHA);
-         texture_draw(&xyz,x + (i * 16) + shadowsize,y + shadowsize, update);
-         texture_free(&xyz);
-         / * Set destination rectangle for text: * /
-         
-         dest.x = x + (i * 16);
-         dest.y = y;
-         dest.w = src.w;
-         dest.h = src.h;         
-         
-         / * Text: * /
-         
-         SDL_BlitSurface(surf, &src, temp, NULL);
-         texture_from_sdl_surface(&xyz,temp,IGNORE_ALPHA);
-         SDL_FreeSurface(temp);
-         texture_draw(&xyz,x + (i * 16) + shadowsize,y + shadowsize, update);
-         texture_free(&xyz);*/
-       }
+#ifndef NOOPENGL
+  if(use_gl)
+    {
+      glEnable(GL_BLEND);
+      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+      glColor4ub(r, g, b,a);
+
+      glBegin(GL_POLYGON);
+      glVertex2f(x, y);
+      glVertex2f(x+w, y);
+      glVertex2f(x+w, y+h);
+      glVertex2f(x, y+h);
+      glEnd();
+      glDisable(GL_BLEND);
     }
-  
-  
-  /* Update */
-  
-  if (update == UPDATE)
+  else
     {
-      dest.w = strlen(text) * 16 + 1;
-      
-      if (dest.w > screen->w)
-       dest.w = screen->w;
-      
-      update_rect(screen, x, y, dest.w, 17);
+#endif
+      SDL_Rect src, rect;
+      SDL_Surface *temp = NULL;
+
+      rect.x = (int)x;
+      rect.y = (int)y;
+      rect.w = (int)w;
+      rect.h = (int)h;
+
+      if(a != 255)
+        {
+          temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
+                                      screen->format->Rmask,
+                                      screen->format->Gmask,
+                                      screen->format->Bmask,
+                                      screen->format->Amask);
+
+
+          src.x = 0;
+          src.y = 0;
+          src.w = rect.w;
+          src.h = rect.h;
+
+          SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, r, g, b));
+
+          SDL_SetAlpha(temp, SDL_SRCALPHA, a);
+
+          SDL_BlitSurface(temp,0,screen,&rect);
+
+          SDL_FreeSurface(temp);
+        }
+      else
+        SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, r, g, b));
+
+#ifndef NOOPENGL
+
     }
-    
+#endif
 }
 
 
-/* --- DRAW HORIZONTALLY-CENTERED TEXT: --- */
+/* --- UPDATE SCREEN --- */
 
-void drawcenteredtext(char * text, int y, SDL_Surface * surf, int update, int shadowsize)
+void updatescreen(void)
 {
-  drawtext(text, screen->w / 2 - (strlen(text) * 8), y, surf, update, shadowsize);
+  if(use_gl)  /*clearscreen(0,0,0);*/
+    SDL_GL_SwapBuffers();
+  else
+    SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);
 }
 
-/* --- ERASE TEXT: --- */
-
-void erasetext(char * text, int x, int y, SDL_Surface * surf, int update, int shadowsize)
+void flipscreen(void)
 {
-  SDL_Rect dest;
-  
-  
-  dest.x = x;
-  dest.y = y;
-  dest.w = strlen(text) * 16 + shadowsize;
-  dest.h = 17;
-  
-  if (dest.w > screen->w)
-    dest.w = screen->w;
-  
-  SDL_BlitSurface(surf, &dest, screen, &dest);
-  
-  if (update == UPDATE)
-    update_rect(screen, dest.x, dest.y, dest.w, dest.h);
+  if(use_gl)
+    SDL_GL_SwapBuffers();
+  else
+    SDL_Flip(screen);
 }
 
-
-/* --- ERASE CENTERED TEXT: --- */
-
-void erasecenteredtext(char * text, int y, SDL_Surface * surf, int update, int shadowsize)
+void update_rect(SDL_Surface *scr, Sint32 x, Sint32 y, Sint32 w, Sint32 h)
 {
-  erasetext(text, screen->w / 2 - (strlen(text) * 8), y, surf, update, shadowsize);
+  if(!use_gl)
+    SDL_UpdateRect(scr, x, y, w, h);
 }
+