DEADSOFTWARE

menu: change videomode at runtime
[flatwaifu.git] / src / sdl / main.c
index b3627fc2f92ef66cb54a83e39e228d16380aaac9..9227725c9876a90f48dd1c9380ab3695acda90ae 100644 (file)
@@ -29,6 +29,7 @@
 #include <stdarg.h>
 #include <stdlib.h> // srand exit
 #include <string.h> // strcasecmp
+#include <ctype.h>
 #include <assert.h>
 #include "system.h"
 #include "input.h"
@@ -54,6 +55,8 @@ static Uint32 ticks;
 static int quit = 0;
 static SDL_Surface *surf = NULL;
 static int mode = MODE_NONE;
+static int text_input;
+static videomode_t vlist;
 
 /* --- error.h --- */
 
@@ -149,6 +152,47 @@ int Y_set_videomode_software (int w, int h, int fullscreen) {
   return s != NULL;
 }
 
+static void init_videomode_list (Uint32 flags) {
+  int i, n;
+  SDL_Rect **r;
+  if (vlist.modes != NULL) {
+    free(vlist.modes);
+    vlist.modes = NULL;
+    vlist.n = 0;
+  }
+  r = SDL_ListModes(NULL, flags);
+  if (r == (SDL_Rect **)-1) {
+    if ((flags & SDL_FULLSCREEN) == 0) {
+      init_videomode_list(flags | SDL_FULLSCREEN);
+    }
+  } else if (r != (SDL_Rect**)0) {
+    n = 0;
+    while (r[n] != NULL) {
+      n++;
+    }
+    vlist.modes = malloc(n * sizeof(videomode_size_t));
+    if (vlist.modes != NULL) {
+      vlist.n = n;
+      for (i = 0; i < n; i++) {
+        vlist.modes[i] = (videomode_size_t) {
+          .w = r[i]->w,
+          .h = r[i]->h
+        };
+      }
+    }
+  }
+}
+
+const videomode_t *Y_get_videomode_list_opengl (int fullscreen) {
+  init_videomode_list(SDL_OPENGL | (fullscreen ? SDL_FULLSCREEN : 0));
+  return &vlist;
+}
+
+const videomode_t *Y_get_videomode_list_software (int fullscreen) {
+  init_videomode_list(SDL_SWSURFACE | SDL_HWPALETTE | (fullscreen ? SDL_FULLSCREEN : 0));
+  return &vlist;
+}
+
 void Y_get_videomode (int *w, int *h) {
   if (mode != MODE_NONE) {
     *w = surf->w;
@@ -232,6 +276,14 @@ void Y_repaint (void) {
   SDL_Flip(surf);
 }
 
+void Y_enable_text_input (void) {
+  text_input = 1;
+}
+
+void Y_disable_text_input (void) {
+  text_input = 0;
+}
+
 /* --- main --- */
 
 static int sdl_to_key (int code) {
@@ -344,8 +396,8 @@ static int sdl_to_key (int code) {
   }
 }
 
-static void poll_events (void (*h)(int key, int down)) {
-  int key;
+static void poll_events (void) {
+  int key, sym, down;
   SDL_Event ev;
   while (SDL_PollEvent(&ev)) {
     switch (ev.type) {
@@ -357,10 +409,14 @@ static void poll_events (void (*h)(int key, int down)) {
         break;
       case SDL_KEYDOWN:
       case SDL_KEYUP:
-        key = sdl_to_key(ev.key.keysym.sym);
-        I_press(key, ev.type == SDL_KEYDOWN);
-        if (h != NULL) {
-          (*h)(key, ev.type == SDL_KEYDOWN);
+        sym = ev.key.keysym.sym;
+        down = ev.type == SDL_KEYDOWN;
+        key = sdl_to_key(sym);
+        I_press(key, down);
+        GM_key(key, down);
+        if (down && text_input && sym >= 0x20 && sym <= 0x7e) {
+          // TODO fix this
+          GM_input(sym);
         }
         break;
     }
@@ -368,7 +424,7 @@ static void poll_events (void (*h)(int key, int down)) {
 }
 
 static void step (void) {
-  poll_events(&G_keyf);
+  poll_events();
   S_updatemusic();
   Uint32 t = SDL_GetTicks();
   if (t - ticks > DELAY) {
@@ -385,7 +441,7 @@ int main (int argc, char *argv[]) {
     logo("main: failed to init SDL: %s\n", SDL_GetError());
     return 1;
   }
-  SDL_WM_SetCaption("Doom 2D v1.351", "Doom 2D");
+  SDL_WM_SetCaption("Doom 2D (SDL)", "Doom 2D");
   // Player 1 defaults
   pl1.ku = KEY_KP_8;
   pl1.kd = KEY_KP_5;