moved savelevel() code to level.h/c where it belongs to. :)
[supertux.git] / src / setup.c
index 0175fb8..d7b67f6 100644 (file)
@@ -7,7 +7,7 @@
   bill@newbreedsoftware.com
   http://www.newbreedsoftware.com/supertux/
   
-  April 11, 2000 - November 7, 2001
+  April 11, 2000 - February 1st, 2004
 */
 
 #include <stdio.h>
@@ -17,6 +17,9 @@
 #include <unistd.h>
 #include <SDL.h>
 #include <SDL_image.h>
+#ifndef NOOPENGL
+#include <SDL_opengl.h>
+#endif
 
 #ifdef LINUX
 #include <pwd.h>
 #include "globals.h"
 #include "setup.h"
 #include "screen.h"
-
+#include "texture.h"
 
 /* Local function prototypes: */
 
 void seticon(void);
 void usage(char * prog, int ret);
 
+/* Does the given file exist and is it accessible? */
+int faccessible(char *filename)
+{
+  struct stat filestat;
+  if (stat(filename, &filestat) == -1)
+    return NO;
+  else
+    return YES;
+}
+
 
 /* --- SETUP --- */
 
@@ -63,8 +76,17 @@ void st_directory_setup(void)
   strcat(st_save_dir,"/save");
 
   /* Create them. In the case they exist it won't destroy anything. */
+#ifdef LINUX
+
   mkdir(st_dir, 0755);
   mkdir(st_save_dir, 0755);
+#else
+  #ifdef WIN32
+
+  mkdir(st_dir);
+  mkdir(st_save_dir);
+#endif
+#endif
 }
 
 void st_general_setup(void)
@@ -72,33 +94,27 @@ void st_general_setup(void)
   /* Seed random number generator: */
 
   srand(SDL_GetTicks());
-  
-  /* Load global images: */
-
-  letters_black = load_image(DATA_PREFIX "/images/status/letters-black.png",
-                             USE_ALPHA);
-
-  letters_gold = load_image(DATA_PREFIX "/images/status/letters-gold.png",
-                            USE_ALPHA);/*
-  if (tux_x < 0)
-    tux_x = 0;*/
-
-  letters_blue = load_image(DATA_PREFIX "/images/status/letters-blue.png",
-                            USE_ALPHA);
-
-  letters_red = load_image(DATA_PREFIX "/images/status/letters-red.png",
-                           USE_ALPHA);
-
 
+  /* Load global images: */
+          
+ text_load(&black_text,DATA_PREFIX "/images/status/letters-black.png");
+ text_load(&gold_text,DATA_PREFIX "/images/status/letters-gold.png");
+ text_load(&blue_text,DATA_PREFIX "/images/status/letters-blue.png");
+ text_load(&red_text,DATA_PREFIX "/images/status/letters-red.png");
   /* Set icon image: */
 
   seticon();
+  SDL_EnableUNICODE(1);
 
 }
 
 void st_video_setup(void)
 {
 
+if(screen != NULL)
+   SDL_FreeSurface(screen); 
+
   /* Init SDL Video: */
 
   if (SDL_Init(SDL_INIT_VIDEO) < 0)
@@ -112,6 +128,21 @@ void st_video_setup(void)
 
   /* Open display: */
 
+  if(use_gl)
+  st_video_setup_gl();
+  else
+  st_video_setup_sdl();
+  
+  texture_setup();
+    
+  /* Set window manager stuff: */
+
+  SDL_WM_SetCaption("Super Tux", "Super Tux");
+
+}
+
+void st_video_setup_sdl(void)
+{
   if (use_fullscreen == YES)
     {
       screen = SDL_SetVideoMode(640, 480, 16, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
@@ -125,10 +156,9 @@ void st_video_setup(void)
           use_fullscreen = NO;
         }
     }
-
-  if (use_fullscreen == NO)
+    else
     {
-      screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE | SDL_DOUBLEBUF);
+      screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE | SDL_DOUBLEBUF );
 
       if (screen == NULL)
         {
@@ -139,11 +169,61 @@ void st_video_setup(void)
           exit(1);
         }
     }
+}
 
-  /* Set window manager stuff: */
+void st_video_setup_gl(void)
+{
+#ifndef NOOPENGL
 
-  SDL_WM_SetCaption("Super Tux", "Super Tux");
-    
+       SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
+       SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
+       SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
+       SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
+       SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
+
+  if (use_fullscreen == YES)
+    {
+      screen = SDL_SetVideoMode(640, 480, 32, SDL_FULLSCREEN | SDL_OPENGL ) ; /* | SDL_HWSURFACE); */
+      if (screen == NULL)
+        {
+          fprintf(stderr,
+                  "\nWarning: I could not set up fullscreen video for "
+                  "640x480 mode.\n"
+                  "The Simple DirectMedia error that occured was:\n"
+                  "%s\n\n", SDL_GetError());
+          use_fullscreen = NO;
+        }
+    }
+    else
+    {
+      screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_OPENGL | SDL_OPENGLBLIT  );
+
+      if (screen == NULL)
+        {
+          fprintf(stderr,
+                  "\nError: I could not set up video for 640x480 mode.\n"
+                  "The Simple DirectMedia error that occured was:\n"
+                  "%s\n\n", SDL_GetError());
+          exit(1);
+        }
+    }
+       
+       /*
+        * Set up OpenGL for 2D rendering.
+        */
+       glDisable(GL_DEPTH_TEST);
+       glDisable(GL_CULL_FACE);
+
+       glViewport(0, 0, screen->w, screen->h);
+       glMatrixMode(GL_PROJECTION);
+       glLoadIdentity();
+       glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
+
+       glMatrixMode(GL_MODELVIEW);
+       glLoadIdentity();
+       glTranslatef(0.0f, 0.0f, 0.0f);
+       
+#endif
 }
 
 void st_joystick_setup(void)
@@ -247,7 +327,7 @@ void st_audio_setup(void)
 
   if (audio_device == YES)
     {
-      if (open_audio(44100, AUDIO_S16, 2, 512) < 0)
+      if (open_audio(44100, AUDIO_S16, 2, 2048) < 0)
         {
           /* only print out message if sound or music
              was not disabled at command-line
@@ -335,11 +415,14 @@ void parseargs(int argc, char * argv[])
 {
   int i;
 
-
   /* Set defaults: */
 
+
   debug_mode = NO;
   use_fullscreen = NO;
+  show_fps = NO;
+  use_gl = NO;    
+
 #ifndef NOSOUND
 
   use_sound = YES;
@@ -363,6 +446,21 @@ void parseargs(int argc, char * argv[])
 
           use_fullscreen = YES;
         }
+      else if (strcmp(argv[i], "--show-fps") == 0)
+        {
+          /* Use full screen: */
+
+          show_fps = YES;
+        }
+      else if (strcmp(argv[i], "--opengl") == 0 ||
+          strcmp(argv[i], "-gl") == 0)
+        {
+       #ifndef NOOPENGL
+          /* Use OpengGL: */
+
+          use_gl = YES;
+       #endif
+        }
       else if (strcmp(argv[i], "--usage") == 0)
         {
           /* Show usage: */
@@ -383,8 +481,10 @@ void parseargs(int argc, char * argv[])
           printf("Sounds disabled \n");
           use_sound = NO;
 #else
+
           printf("Warning: Sounds feature is not compiled in \n");
 #endif
+
         }
       else if (strcmp(argv[i], "--disable-music") == 0)
         {
@@ -393,13 +493,15 @@ void parseargs(int argc, char * argv[])
           printf("Music disabled \n");
           use_music = NO;
 #else
+
           printf("Warning: Music feature is not compiled in \n");
 #endif
+
         }
       else if (strcmp(argv[i], "--debug-mode") == 0)
         {
           /* Enable the debug-mode */
-       debug_mode = YES;
+          debug_mode = YES;
 
         }
       else if (strcmp(argv[i], "--help") == 0)
@@ -409,6 +511,8 @@ void parseargs(int argc, char * argv[])
 
           printf("----------  Command-line options  ----------\n\n");
 
+          printf("  --opengl            - If opengl support was compiled in, this will enable the EXPERIMENTAL OpenGL mode.\n\n");
+         
           printf("  --disable-sound     - If sound support was compiled in,  this will\n                        disable sound for this session of the game.\n\n");
 
           printf("  --disable-music     - Like above, but this will disable music.\n\n");
@@ -416,7 +520,7 @@ void parseargs(int argc, char * argv[])
           printf("  --fullscreen        - Run in fullscreen mode.\n\n");
 
           printf("  --debug-mode        - Enables the debug-mode, which is useful for developers.\n\n");
-         
+
           printf("  --help              - Display a help message summarizing command-line\n                        options, license and game controls.\n\n");
 
           printf("  --usage             - Display a brief message summarizing command-line options.\n\n");
@@ -462,11 +566,12 @@ void usage(char * prog, int ret)
 
   /* Display the usage message: */
 
-  fprintf(fi, "Usage: %s [--fullscreen] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version]\n",
-           prog);
+  fprintf(fi, "Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version]\n",
+          prog);
 
 
   /* Quit! */
 
   exit(ret);
 }
+