DEADSOFTWARE

ports: ported to emscripten
[flatwaifu.git] / src / sdl2 / main.c
index dd5b8d43b2d16947a79291f66e1dc57d9fa9c38f..1375a29c6a32a9050d427f807d37476153792f3b 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
@@ -22,6 +26,7 @@
 
 #define TITLE_STR "DooM 2D (SDL2)"
 
+static Uint32 ticks;
 static int quit = 0;
 static SDL_Window *window;
 static SDL_GLContext context;
@@ -66,95 +71,113 @@ void ERR_fatal (char *s, ...) {
 }
 
 void ERR_quit (void) {
-  puts("Спасибо за то, что вы играли в Операцию \"Смятка\"!");
-  //F_loadres(F_getresid("ENDOOM"),p,0,4000);
   quit = 1;
 }
 
 /* --- system.h --- */
 
-static int Y_set_videomode_opengl (int w, int h, int fullscreen) {
+static int Y_resize_window (int w, int h, int fullscreen) {
   assert(w > 0);
   assert(h > 0);
-  Uint32 flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL;
-  flags |= fullscreen ? SDL_WINDOW_FULLSCREEN : 0;
-  // TODO set context version and type
-  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
-  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
-  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
-  int x = SDL_WINDOWPOS_CENTERED;
-  int y = SDL_WINDOWPOS_CENTERED;
-  SDL_Window *win = SDL_CreateWindow(TITLE_STR, x, y, w, h, flags);
-  if (win != NULL) {
-    SDL_GLContext ctx = SDL_GL_CreateContext(win);
-    if (ctx != NULL) {
-      Y_unset_videomode();
-      window = win;
-      context = ctx;
-      SDL_GL_MakeCurrent(window, context);
-    } else {
-      SDL_DestroyWindow(win);
-      win = NULL;
+  assert(window != NULL);
+  if (surf != NULL) {
+    if (surf->w != w || surf->h != h) {
+      SDL_Surface *s = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0);
+      if (s != NULL) {
+        SDL_SetPaletteColors(s->format->palette, surf->format->palette->colors, 0, surf->format->palette->ncolors);
+        SDL_FreeSurface(surf);
+        surf = s;
+      }
     }
   }
-  return win != NULL;
+  SDL_SetWindowSize(window, w, h);
+  Y_set_fullscreen(fullscreen);
+  return 1;
 }
 
-static int Y_set_videomode_software (int w, int h, int fullscreen) {
+int Y_set_videomode_opengl (int w, int h, int fullscreen) {
   assert(w > 0);
   assert(h > 0);
-  int x = SDL_WINDOWPOS_CENTERED;
-  int y = SDL_WINDOWPOS_CENTERED;
-  Uint32 flags = SDL_WINDOW_RESIZABLE;
-  flags |= fullscreen ? SDL_WINDOW_FULLSCREEN : 0;
-  SDL_Window *win = SDL_CreateWindow(TITLE_STR, x, y, w, h, flags);
-  if (win != NULL) {
-    SDL_Surface *s = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0);
-    if (s != NULL) {
-      Y_unset_videomode();
-      window = win;
-      surf = s;
-    } else {
-      SDL_DestroyWindow(win);
-      win = NULL;
+  Uint32 flags;
+  SDL_Window *win;
+  SDL_GLContext ctx;
+  if (window != NULL && context != NULL) {
+    Y_resize_window(w, h, fullscreen);
+    win = window;
+  } else {
+    flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL;
+    if (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) {
+      ctx = SDL_GL_CreateContext(win);
+      if (ctx != NULL) {
+        Y_unset_videomode();
+        window = win;
+        context = ctx;
+        SDL_GL_MakeCurrent(window, context);
+      } else {
+        SDL_DestroyWindow(win);
+        win = NULL;
+      }
+    }
+  }
+  if (win == NULL) {
+    logo("Y_set_videomode_opengl: error: %s\n", SDL_GetError());
   }
   return win != NULL;
 }
 
-static int Y_resize_window (int w, int h, int fullscreen) {
+int Y_set_videomode_software (int w, int h, int fullscreen) {
   assert(w > 0);
   assert(h > 0);
-  assert(window != NULL);
-  if (surf != NULL) {
-    SDL_Surface *s = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0);
-    if (s != NULL) {
-      SDL_SetPaletteColors(s->format->palette, surf->format->palette->colors, 0, surf->format->palette->ncolors);
-      SDL_FreeSurface(surf);
-      surf = s;
+  Uint32 flags;
+  SDL_Surface *s;
+  SDL_Window *win;
+  if (window != NULL && surf != NULL) {
+    Y_resize_window(w, h, fullscreen);
+    win = window;
+  } else {
+    flags = SDL_WINDOW_RESIZABLE;
+    if (fullscreen) {
+      flags = flags | SDL_WINDOW_FULLSCREEN;
+    }
+    win = SDL_CreateWindow(TITLE_STR, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags);
+    if (win != NULL) {
+      s = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0);
+      if (s != NULL) {
+        Y_unset_videomode();
+        window = win;
+        surf = s;
+      } else {
+        SDL_DestroyWindow(win);
+        win = NULL;
+      }
     }
   }
-  SDL_SetWindowSize(window, w, h);
-  Y_set_fullscreen(fullscreen);
-  return 1;
+  if (win == NULL) {
+    logo("Y_set_videomode_software: error: %s\n", SDL_GetError());
+  }
+  return win != NULL;
 }
 
-int Y_set_videomode (int w, int h, int flags) {
-  assert(w > 0);
-  assert(h > 0);
-  int fullscreen = (flags & SYSTEM_USE_FULLSCREEN) != 0;
-  if (flags & SYSTEM_USE_OPENGL) {
-    if (window != NULL && context != NULL) {
-      return Y_resize_window(w, h, fullscreen);
-    } else {
-      return Y_set_videomode_opengl(w, h, fullscreen);
-    }
+void Y_get_videomode (int *w, int *h) {
+  if (window != NULL) {
+    SDL_GetWindowSize(window, w, h);
   } else {
-    if (window != NULL && surf != NULL) {
-      return Y_resize_window(w, h, fullscreen);
-    } else {
-      return Y_set_videomode_software(w, h, fullscreen);
-    }
+    *w = 0;
+    *h = 0;
   }
 }
 
@@ -178,11 +201,10 @@ void Y_unset_videomode (void) {
   }
 }
 
-int Y_set_fullscreen (int yes) {
+void Y_set_fullscreen (int yes) {
   if (window != NULL) {
     SDL_SetWindowFullscreen(window, yes ? SDL_WINDOW_FULLSCREEN : 0);
   }
-  return yes;
 }
 
 int Y_get_fullscreen (void) {
@@ -354,9 +376,7 @@ static int sdl_to_key (int code) {
 static void window_event_handler (SDL_WindowEvent *ev) {
   switch (ev->event) {
     case SDL_WINDOWEVENT_RESIZED:
-      if (window != NULL && ev->windowID == SDL_GetWindowID(window)) {
-        R_set_videomode(ev->data1, ev->data2, Y_get_fullscreen());
-      }
+      R_set_videomode(ev->data1, ev->data2, Y_get_fullscreen());
       break;
     case SDL_WINDOWEVENT_CLOSE:
       ERR_quit();
@@ -373,7 +393,9 @@ static void poll_events (void (*h)(int key, int down)) {
         ERR_quit();
         break;
       case SDL_WINDOWEVENT:
-        window_event_handler(&ev.window);
+        if (ev.window.windowID == SDL_GetWindowID(window)) {
+          window_event_handler(&ev.window);
+        }
         break;
       case SDL_KEYDOWN:
       case SDL_KEYUP:
@@ -387,15 +409,24 @@ static void poll_events (void (*h)(int key, int down)) {
   }
 }
 
+static void step (void) {
+  poll_events(&G_keyf);
+  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;
@@ -438,16 +469,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();