version 0.2
[zeRace] / sdl.c
diff --git a/sdl.c b/sdl.c
index 0661854..16bd1c6 100644 (file)
--- a/sdl.c
+++ b/sdl.c
@@ -1,14 +1,16 @@
+/* some useful functions for zeRace game */
+
 #include "sdl.h"
 
 SDL_Surface *font=NULL;
 
+/* prints a message using the bitmap font */
 void print(SDL_Surface *dst,int x,int y,unsigned char *text)
 {
   SDL_Rect srcpos,dstpos;
+  /* on the first call, load the font picture */
   if (!font) if ((font=IMG_Load("sprites/font.png"))==NULL)
-  {
-               fprintf(stderr,"could not load font file\n",SDL_GetError());
-  }
+    fprintf(stderr,"could not load font file\n");
   while (*text!='\0')
   {
     if (*text>=' ' && *text<' '+16*10)
@@ -24,6 +26,51 @@ void print(SDL_Surface *dst,int x,int y,unsigned char *text)
   }
 }
 
+/* reads a string into the "text" variable */
+void readstring(SDL_Surface *dst,int x,int y,unsigned char *text,int limit)
+{
+  SDL_Event event;
+  int shift=0;
+
+  SDL_EnableUNICODE(1);
+  for (;;)
+  {
+    while (SDL_PollEvent(&event)) switch (event.type)
+    {
+      case SDL_KEYDOWN:
+        switch (event.key.keysym.sym)
+        {
+          case SDLK_ESCAPE:
+          case SDLK_RETURN:
+            return;
+          case SDLK_LSHIFT:
+          case SDLK_RSHIFT:
+            shift=1;
+            break;
+          case SDLK_BACKSPACE:
+            if (strlen(text))
+            {
+              text[strlen(text)-1]='\0';
+              print(dst,x+strlen(text)*10,y,"  ");
+            }
+            break;
+          default:
+            if ((event.key.keysym.unicode & 0xff80)==0 && strlen(text)<limit)
+            {
+              text[strlen(text)+1]='\0';
+                    text[strlen(text)]=event.key.keysym.unicode & 0x7f;
+            }
+            break;
+        }
+        break;
+    }
+    print(dst,x,y,text);
+    print(dst,x+strlen(text)*10,y,"_");
+    SDL_Flip(dst);
+    SDL_Delay(10);
+  }
+}
+
 /*
  * Return the pixel value at (x, y)
  * NOTE: The surface must be locked before calling this!
@@ -91,61 +138,3 @@ void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
         break;
     }
 }
-
-/* Set pixel function */
-void setpixel(SDL_Surface *screen, int x, int y, Uint8 r, Uint8 g, Uint8 b)
-{
-       Uint8 *ubuff8;
-       Uint16 *ubuff16; 
-       Uint32 *ubuff32;
-       Uint32 color;
-       
-       /* Lock the screen, if needed */
-       if(SDL_MUSTLOCK(screen)) {
-               if(SDL_LockSurface(screen) < 0) 
-                       return;
-       }
-       /* Get the color */
-       color = SDL_MapRGB( screen->format, r, g, b );
-       
-       /* How we draw the pixel depends on the bitdepth */
-       switch(screen->format->BytesPerPixel) {
-               case 1: 
-                       ubuff8 = (Uint8*) screen->pixels;
-                       ubuff8 += (y * screen->pitch) + x; 
-                       *ubuff8 = (Uint8) color;
-                       break;
-               case 2:
-                       ubuff16 = (Uint16*) screen->pixels;
-                       ubuff16 += ((y * screen->pitch)>>2) + x;
-                       *ubuff16 = (Uint16) color; 
-                       break;  
-               case 3:
-                       ubuff8 = (Uint8*) screen->pixels;
-                       ubuff8 += (y * screen->pitch) + x;
-                                       
-                       r = (color>>screen->format->Rshift)&0xFF;
-                       g = (color>>screen->format->Gshift)&0xFF;
-                       b = (color>>screen->format->Bshift)&0xFF;
-
-                       ubuff8[0] = r;
-                       ubuff8[1] = g;
-                       ubuff8[2] = b;
-                       break;
-                               
-               case 4:
-                       ubuff32 = (Uint32*) screen->pixels;
-                       ubuff32 += ((y*screen->pitch)>>2) + x;
-                       *ubuff32 = color;
-                       break;
-       
-               default:
-                       fprintf(stderr, "Error: Unknown bitdepth!\n");
-       }
-
-       /* Unlock the screen if needed */
-       if(SDL_MUSTLOCK(screen)) {
-               SDL_UnlockSurface(screen);
-       }
-}