diff --git a/src/sdl/main.c b/src/sdl/main.c
index db74674a752b8fd421099ba56644469937c1ad5f..2df46b3fb96cdc76a92b6e799f72878b45493c2f 100644 (file)
--- a/src/sdl/main.c
+++ b/src/sdl/main.c
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#include "SDL.h"
+#ifdef __EMSCRIPTEN__
+# include <emscripten.h>
+#endif
+
+#include <SDL/SDL.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h> // srand exit
-#include <string.h> // strcasecmp
#include <assert.h>
#include "system.h"
#include "input.h"
#include "player.h" // pl1 pl2
#include "menu.h" // G_keyf
#include "error.h" // logo
+#include "monster.h" // nomon
#include "files.h" // F_startup F_addwad F_initwads F_allocres
#include "config.h" // CFG_args CFG_load CFG_save
#define MODE_OPENGL 1
#define MODE_SOFTWARE 2
+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;
+
+static const cfg_t arg[] = {
+ {"file", NULL, Y_FILES},
+ {"cheat", &cheat, Y_SW_ON},
+// {"vga", &shot_vga, Y_SW_ON},
+ {"sndvol", &snd_vol, Y_WORD},
+ {"musvol", &mus_vol, Y_WORD},
+// {"fullscr", &fullscreen, Y_SW_ON},
+// {"window", &fullscreen, Y_SW_OFF},
+ {"mon", &nomon, Y_SW_OFF},
+// {"gamma", &gammaa, Y_DWORD},
+ {"warp", &_warp, Y_BYTE},
+// {"width", &SCRW, Y_DWORD},
+// {"height", &SCRH, Y_DWORD},
+// {"config", NULL, cfg_file, Y_STRING},
+ {NULL, NULL, 0} // end
+};
+
+static const cfg_t cfg[] = {
+// {"screenshot", &shot_vga, Y_SW_ON},
+ {"sound_volume", &snd_vol, Y_WORD},
+ {"music_volume", &mus_vol, Y_WORD},
+// {"fullscreen", &fullscreen, Y_SW_ON},
+ {"sky", &w_horiz, Y_SW_ON},
+// {"gamma", &gammaa, Y_DWORD},
+// {"screen_width", &SCRW, Y_DWORD},
+// {"screen_height", &SCRH, Y_DWORD},
+ {"music_random", &music_random, Y_SW_ON},
+ {"music_time", &music_time, Y_DWORD},
+ {"music_fade", &music_fade, Y_DWORD},
+ {"pl1_left", &pl1.kl, Y_KEY},
+ {"pl1_right",&pl1.kr, Y_KEY},
+ {"pl1_up", &pl1.ku, Y_KEY},
+ {"pl1_down", &pl1.kd, Y_KEY},
+ {"pl1_jump", &pl1.kj, Y_KEY},
+ {"pl1_fire", &pl1.kf, Y_KEY},
+ {"pl1_next", &pl1.kwr, Y_KEY},
+ {"pl1_prev", &pl1.kwl, Y_KEY},
+ {"pl1_use", &pl1.kp, Y_KEY},
+ {"pl2_left", &pl2.kl, Y_KEY},
+ {"pl2_right", &pl2.kr, Y_KEY},
+ {"pl2_up", &pl2.ku, Y_KEY},
+ {"pl2_down", &pl2.kd, Y_KEY},
+ {"pl2_jump", &pl2.kj, Y_KEY},
+ {"pl2_fire", &pl2.kf, Y_KEY},
+ {"pl2_next", &pl2.kwr, Y_KEY},
+ {"pl2_prev", &pl2.kwl, Y_KEY},
+ {"pl2_use", &pl2.kp, Y_KEY},
+ {NULL, NULL, 0} // end
+};
+
+static void CFG_args (int argc, char **argv) {
+ int i;
+ for (i = 1; i < argc; i++) {
+ if (argv[i][0] == '-' && argv[i][1] != 0) {
+ if (i + 1 >= argc) {
+ ERR_failinit("CFG_args: not enough arguments for parameter %s\n", argv[i]);
+ } else {
+ if (CFG_update_key(&argv[i][1], argv[i + 1], arg) != 0) {
+ ERR_failinit("CFG_args: unknown parameter %s\n", argv[i]);
+ }
+ i += 1;
+ }
+ } else {
+ ERR_failinit("CFG_args: something wrong here: %s\n", argv[i]);
+ }
+ }
+}
+
+static void CFG_load (void) {
+ CFG_read_config("default.cfg", cfg);
+ CFG_read_config("doom2d.cfg", cfg);
+}
+
+static void CFG_save (void) {
+ CFG_update_config("doom2d.cfg", "doom2d.cfg", cfg, "generated by doom2d, do not modify");
+ //CFG_update_config("doom2d.cfg", "doom2d.tmp", cfg, "temporary file");
+ //CFG_update_config("doom2d.tmp", "doom2d.cfg", cfg, "generated by doom2d, do not modify");
+ //remove("doom2d.tmp");
+}
/* --- error.h --- */
if (s != NULL) {
mode = MODE_OPENGL;
surf = s;
+ } else {
+ logo("Y_set_videomode_opengl: error: %s\n", SDL_GetError());
}
}
return s != NULL;
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;
void Y_unset_videomode (void) {
surf = NULL;
mode = MODE_NONE;
+#ifndef __EMSCRIPTEN__
SDL_QuitSubSystem(SDL_INIT_VIDEO);
SDL_InitSubSystem(SDL_INIT_VIDEO);
+#endif
}
void Y_set_fullscreen (int fullscreen) {
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) {
}
}
-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) {
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;
}
}
}
+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("main: initialize SDL\n");
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
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;
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();