diff --git a/src/sdl2/main.c b/src/sdl2/main.c
index c1b872ca71a417cf29817d220d91b251dd07e10b..ae80fe9d6dd8c62f8993994620a0204fe846943d 100644 (file)
--- a/src/sdl2/main.c
+++ b/src/sdl2/main.c
-#include <SDL.h>
+#ifdef __EMSCRIPTEN__
+# include <emscripten.h>
+#endif
+
+#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h> // srand exit
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h> // srand exit
#include "system.h"
#include "input.h"
#include "system.h"
#include "input.h"
+#include "cp866.h"
+
#include "my.h" // fexists
#include "player.h" // pl1 pl2
#include "menu.h" // G_keyf
#include "error.h" // logo
#include "my.h" // fexists
#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
#include "files.h" // F_startup F_addwad F_initwads F_allocres
#include "config.h" // CFG_args CFG_load CFG_save
#include "music.h" // S_initmusic S_updatemusic S_donemusic
#include "render.h" // R_init R_draw R_done
#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;
static SDL_Surface *surf;
static int quit = 0;
static SDL_Window *window;
static SDL_GLContext context;
static SDL_Surface *surf;
+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 --- */
/* --- error.h --- */
}
void ERR_quit (void) {
}
void ERR_quit (void) {
- puts("Спасибо за то, что вы играли в Операцию \"Смятка\"!");
- //F_loadres(F_getresid("ENDOOM"),p,0,4000);
quit = 1;
}
/* --- system.h --- */
quit = 1;
}
/* --- system.h --- */
-static int Y_set_videomode_opengl (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;
- }
- }
- return win != NULL;
-}
-
-static int Y_set_videomode_software (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;
- }
- }
- return win != NULL;
-}
-
static int Y_resize_window (int w, int h, int fullscreen) {
assert(w > 0);
assert(h > 0);
assert(window != NULL);
if (surf != NULL) {
static int Y_resize_window (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;
+ 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;
+ }
}
}
SDL_SetWindowSize(window, w, h);
}
}
SDL_SetWindowSize(window, w, h);
return 1;
}
return 1;
}
-int Y_set_videomode (int w, int h, int flags) {
+int Y_set_videomode_opengl (int w, int h, int fullscreen) {
assert(w > 0);
assert(h > 0);
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);
+ 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;
+}
+
+int Y_set_videomode_software (int w, int h, int fullscreen) {
+ assert(w > 0);
+ assert(h > 0);
+ Uint32 flags;
+ SDL_Surface *s;
+ SDL_Window *win;
+ if (window != NULL && surf != NULL) {
+ Y_resize_window(w, h, fullscreen);
+ win = window;
} else {
} else {
- if (window != NULL && surf != NULL) {
- return Y_resize_window(w, h, fullscreen);
- } else {
- return Y_set_videomode_software(w, h, fullscreen);
+ 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;
+ }
}
}
}
}
+ if (win == NULL) {
+ logo("Y_set_videomode_software: error: %s\n", SDL_GetError());
+ }
+ return win != NULL;
}
void Y_get_videomode (int *w, int *h) {
}
void Y_get_videomode (int *w, int *h) {
}
}
}
}
-int Y_set_fullscreen (int yes) {
+static void init_videomode_list (void) {
+ int i, j, k;
+ SDL_DisplayMode m;
+ int n = SDL_GetNumDisplayModes(0);
+ if (vlist.modes != NULL) {
+ free(vlist.modes);
+ vlist.modes = NULL;
+ vlist.n = 0;
+ }
+ if (n > 0) {
+ vlist.modes = malloc(n * sizeof(videomode_size_t));
+ if (vlist.modes != NULL) {
+ j = 0;
+ for (i = 0; i < n; i++) {
+ SDL_GetDisplayMode(0, i, &m);
+ k = 0;
+ while (k < j && (m.w != vlist.modes[k].w || m.h != vlist.modes[k].h)) {
+ k++;
+ }
+ if (k >= j) {
+ vlist.modes[j] = (videomode_size_t) {
+ .w = m.w,
+ .h = m.h
+ };
+ j++;
+ }
+ }
+ vlist.n = j;
+ }
+ }
+}
+
+const videomode_t *Y_get_videomode_list_opengl (int fullscreen) {
+ init_videomode_list();
+ return &vlist;
+}
+
+const videomode_t *Y_get_videomode_list_software (int fullscreen) {
+ init_videomode_list();
+ return &vlist;
+}
+
+void Y_set_fullscreen (int yes) {
if (window != NULL) {
SDL_SetWindowFullscreen(window, yes ? SDL_WINDOW_FULLSCREEN : 0);
}
if (window != NULL) {
SDL_SetWindowFullscreen(window, yes ? SDL_WINDOW_FULLSCREEN : 0);
}
- return yes;
}
int Y_get_fullscreen (void) {
}
int Y_get_fullscreen (void) {
Y_repaint_rect(0, 0, surf->w, surf->h);
}
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) {
/* --- main --- */
static int sdl_to_key (int code) {
static void window_event_handler (SDL_WindowEvent *ev) {
switch (ev->event) {
case SDL_WINDOWEVENT_RESIZED:
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();
break;
case SDL_WINDOWEVENT_CLOSE:
ERR_quit();
}
}
}
}
-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 void poll_events (void) {
+ int key, down, uch, ch;
SDL_Event ev;
while (SDL_PollEvent(&ev)) {
switch (ev.type) {
SDL_Event ev;
while (SDL_PollEvent(&ev)) {
switch (ev.type) {
ERR_quit();
break;
case SDL_WINDOWEVENT:
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:
break;
case SDL_KEYDOWN:
case SDL_KEYUP:
+ down = ev.type == SDL_KEYDOWN;
key = sdl_to_key(ev.key.keysym.scancode);
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 = cp866_utoc(uch);
+ if (ch >= 0) {
+ GM_input(ch);
}
break;
}
}
}
}
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;
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;
}
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;
// Player 1 defaults
pl1.ku = KEY_KP_8;
pl1.kd = KEY_KP_5;
R_init();
G_init();
ticks = SDL_GetTicks();
R_init();
G_init();
ticks = SDL_GetTicks();
+#ifdef __EMSCRIPTEN__
+ emscripten_set_main_loop(step, 0, 1);
+#else
while (!quit) {
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();
CFG_save();
R_done();
S_donemusic();