DEADSOFTWARE

menu: rewrite menu code (software render are broken)
[flatwaifu.git] / src / sdl2 / main.c
index 3f49369d326d61e8707ec99626b9edb779abd1bd..a6b98d6813f6020130572574a6ea010cc6025694 100644 (file)
@@ -1,4 +1,8 @@
-#include "SDL.h"
+#ifdef __EMSCRIPTEN__
+#  include <emscripten.h>
+#endif
+
+#include <SDL2/SDL.h>
 #include <stdio.h>
 #include <stdarg.h>
 #include <stdlib.h> // srand exit
@@ -20,8 +24,9 @@
 #include "music.h" // S_initmusic S_updatemusic S_donemusic
 #include "render.h" // R_init R_draw R_done
 
-#define TITLE_STR "DooM 2D (SDL2)"
+#define TITLE_STR "Doom 2D (SDL2)"
 
+static Uint32 ticks;
 static int quit = 0;
 static SDL_Window *window;
 static SDL_GLContext context;
@@ -66,8 +71,6 @@ void ERR_fatal (char *s, ...) {
 }
 
 void ERR_quit (void) {
-  puts("Спасибо за то, что вы играли в Операцию \"Смятка\"!");
-  //F_loadres(F_getresid("ENDOOM"),p,0,4000);
   quit = 1;
 }
 
@@ -107,8 +110,14 @@ int Y_set_videomode_opengl (int w, int h, int fullscreen) {
       flags = flags | SDL_WINDOW_FULLSCREEN;
     }
     // TODO set context version and type
+#ifdef __EMSCRIPTEN__
+    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
+    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
+    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
+#else
     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
+#endif
     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
     win = SDL_CreateWindow(TITLE_STR, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags);
     if (win != NULL) {
@@ -124,6 +133,9 @@ int Y_set_videomode_opengl (int w, int h, int fullscreen) {
       }
     }
   }
+  if (win == NULL) {
+    logo("Y_set_videomode_opengl: error: %s\n", SDL_GetError());
+  }
   return win != NULL;
 }
 
@@ -154,6 +166,9 @@ int Y_set_videomode_software (int w, int h, int fullscreen) {
       }
     }
   }
+  if (win == NULL) {
+    logo("Y_set_videomode_software: error: %s\n", SDL_GetError());
+  }
   return win != NULL;
 }
 
@@ -246,6 +261,14 @@ void Y_repaint (void) {
   Y_repaint_rect(0, 0, surf->w, surf->h);
 }
 
+void Y_enable_text_input (void) {
+  SDL_StartTextInput();
+}
+
+void Y_disable_text_input (void) {
+  SDL_StopTextInput();
+}
+
 /* --- main --- */
 
 static int sdl_to_key (int code) {
@@ -369,8 +392,56 @@ static void window_event_handler (SDL_WindowEvent *ev) {
   }
 }
 
-static void poll_events (void (*h)(int key, int down)) {
-  int key;
+static int utf8_to_wchar (char *x) {
+  int i = 0;
+  byte *s = (byte*)x;
+  if (s[0] < 0x80) {
+    return s[0];
+  } else if (s[0] < 0xE0) {
+    if (s[0] - 192 >= 0 && s[1] >= 0x80 && s[1] < 0xE0) {
+      i = (s[0] - 192) * 64 + s[1] - 128;
+    }
+  } else if (s[0] < 0xF0) {
+    if (s[1] >= 0x80 && s[1] < 0xE0 && s[2] >= 0x80 && s[2] < 0xE0) {
+      i = ((s[0] - 224) * 64 + s[1] - 128) * 64 + s[2] - 128;
+    }
+  }
+  return i;
+}
+
+static int wchar_to_cp866 (int uch) {
+  if (uch <= 0x7f) {
+    return uch;
+  } else if (uch >= 0x410 && uch <= 0x43f) {
+    return uch - 0x410 + 0x80;
+  } else if (uch >= 0x440 && uch <= 0x44f) {
+    return uch - 0x440 + 0xe0;
+  } else {
+    switch (uch) {
+      // TODO graphics from 0xb0..0xdf
+      case 0x401: return 0xf0;
+      case 0x451: return 0xf1;
+      case 0x404: return 0xf2;
+      case 0x454: return 0xf3;
+      case 0x407: return 0xf4;
+      case 0x457: return 0xf5;
+      case 0x40e: return 0xf6;
+      case 0x45e: return 0xf7;
+      case 0xb0: return 0xf8;
+      case 0x2219: return 0xf9;
+      case 0xb7: return 0xfa;
+      case 0x221a: return 0xfb;
+      case 0x2116: return 0xfc;
+      case 0xa4: return 0xfd;
+      case 0x25a0: return 0xfe;
+      case 0xa0: return 0xff;
+      default: return 0; // unknown
+    }
+  }
+}
+
+static void poll_events (void) {
+  int key, down, uch, ch;
   SDL_Event ev;
   while (SDL_PollEvent(&ev)) {
     switch (ev.type) {
@@ -384,25 +455,38 @@ static void poll_events (void (*h)(int key, int down)) {
         break;
       case SDL_KEYDOWN:
       case SDL_KEYUP:
+        down = ev.type == SDL_KEYDOWN;
         key = sdl_to_key(ev.key.keysym.scancode);
-        I_press(key, ev.type == SDL_KEYDOWN);
-        if (h != NULL) {
-          (*h)(key, ev.type == SDL_KEYDOWN);
-        }
+        I_press(key, down);
+        GM_key(key, down);
+        break;
+      case SDL_TEXTINPUT:
+        uch = utf8_to_wchar(ev.text.text);
+        ch = wchar_to_cp866(uch);
+        GM_input(ch);
         break;
     }
   }
 }
 
+static void step (void) {
+  poll_events();
+  S_updatemusic();
+  Uint32 t = SDL_GetTicks();
+  if (t - ticks > DELAY) {
+    ticks = t;
+    G_act();
+  }
+  R_draw();
+}
+
 int main (int argc, char **argv) {
   char *pw;
-  Uint32 t, ticks;
   logo("system: initialize SDL2\n");
   if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_EVENTS) == -1) {
     logo("system: failed to init SDL2: %s\n", SDL_GetError());
     return 1;
   }
-  //SDL_WM_SetCaption("Doom 2D v1.351", "Doom 2D");
   // Player 1 defaults
   pl1.ku = KEY_KP_8;
   pl1.kd = KEY_KP_5;
@@ -445,16 +529,13 @@ int main (int argc, char **argv) {
   R_init();
   G_init();
   ticks = SDL_GetTicks();
+#ifdef __EMSCRIPTEN__
+  emscripten_set_main_loop(step, 0, 1);
+#else
   while (!quit) {
-    poll_events(&G_keyf);
-    S_updatemusic();
-    t = SDL_GetTicks();
-    if (t - ticks > DELAY) {
-      ticks = t;
-      G_act();
-    }
-    R_draw();
+    step();
   }
+#endif
   CFG_save();
   R_done();
   S_donemusic();