Skip to content

Commit 09dfe62

Browse files
committed
Hardcode the default window size on Emscripten
Also minor cleanups to the main function and related things...
1 parent 55c32eb commit 09dfe62

File tree

2 files changed

+137
-130
lines changed

2 files changed

+137
-130
lines changed

src/main.cc

Lines changed: 78 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -363,105 +363,105 @@ init_framebuffers(void)
363363
void
364364
tproject_set_args(int argc, char **argv)
365365
{
366-
if (argc > 1) {
367-
368-
char *s = argv[1];
369-
int n;
370-
371-
do {
372-
if (strncmp(s, "principia://", 12) != 0)
373-
break;
366+
if (argc <= 1)
367+
return;
374368

375-
s+=12;
369+
char *s = argv[1];
370+
int n;
376371

377-
/* extract the host */
378-
_community_host[0] = '\0';
379-
for (n=0; n<511; n++) {
380-
if (*(s+n) == '/' || *(s+n) == '\0') {
381-
break;
382-
}
383-
}
372+
do {
373+
if (strncmp(s, "principia://", 12) != 0)
374+
break;
384375

385-
strncpy(_community_host, s, n);
386-
_community_host[n] = '\0';
387-
s += n;
376+
s+=12;
388377

389-
if (*s == '\0') {
378+
/* extract the host */
379+
_community_host[0] = '\0';
380+
for (n=0; n<511; n++) {
381+
if (*(s+n) == '/' || *(s+n) == '\0') {
390382
break;
391383
}
384+
}
392385

393-
s ++;
386+
strncpy(_community_host, s, n);
387+
_community_host[n] = '\0';
388+
s += n;
394389

395-
/* backwards compatibility, if the "host" equals any of the reserved words below we default to the
396-
* currently signed-in community host instead and backset the pointer */
397-
if (strcmp(_community_host, "play") == 0 ||
398-
strcmp(_community_host, "sandbox") == 0 ||
399-
strcmp(_community_host, "edit") == 0) {
400-
s -= strlen(_community_host) + 1;
401-
_community_host[0] = '\0';
402-
}
390+
if (*s == '\0') {
391+
break;
392+
}
403393

404-
if (strncmp(s, "play/", 5) == 0) {
405-
s+=5;
394+
s ++;
406395

407-
int action = ACTION_IGNORE;
396+
/* backwards compatibility, if the "host" equals any of the reserved words below we default to the
397+
* currently signed-in community host instead and backset the pointer */
398+
if (strcmp(_community_host, "play") == 0 ||
399+
strcmp(_community_host, "sandbox") == 0 ||
400+
strcmp(_community_host, "edit") == 0) {
401+
s -= strlen(_community_host) + 1;
402+
_community_host[0] = '\0';
403+
}
408404

409-
if (strncmp(s, "lvl/", 4) == 0)
410-
action = ACTION_OPEN_PLAY;
411-
else if (strncmp(s, "pkg/", 4) == 0)
412-
action = ACTION_PLAY_PKG;
405+
if (strncmp(s, "play/", 5) == 0) {
406+
s+=5;
413407

414-
s+=4;
408+
int action = ACTION_IGNORE;
415409

416-
if (strncmp(s, "local/", 6) == 0) {
417-
s+=6;
418-
_play_type = LEVEL_LOCAL;
419-
} else if (strncmp(s, "db/", 3) == 0) {
420-
s+=3;
421-
_play_type = LEVEL_DB;
422-
} else if (strncmp(s, "main/", 5) == 0) {
423-
s+=5;
424-
_play_type = LEVEL_MAIN;
425-
} else {
426-
break;
427-
}
410+
if (strncmp(s, "lvl/", 4) == 0)
411+
action = ACTION_OPEN_PLAY;
412+
else if (strncmp(s, "pkg/", 4) == 0)
413+
action = ACTION_PLAY_PKG;
428414

429-
_play_id = atoi(s);
430-
P.add_action(action, 0);
431-
} else if (strncmp(s, "sandbox/", 8) == 0) {
432-
s+=8;
433-
434-
int action = ACTION_IGNORE;
435-
436-
if (strncmp(s, "local/", 6) == 0) {
437-
s+=6;
438-
_play_type = LEVEL_LOCAL;
439-
action = ACTION_OPEN;
440-
} else if (strncmp(s, "db/", 3) == 0) {
441-
s+=3;
442-
_play_type = LEVEL_DB;
443-
action = ACTION_DERIVE;
444-
}
415+
s+=4;
445416

446-
_play_id = atoi(s);
447-
P.add_action(action, _play_id);
448-
} else if (strncmp(s, "edit/", 5) == 0) {
417+
if (strncmp(s, "local/", 6) == 0) {
418+
s+=6;
419+
_play_type = LEVEL_LOCAL;
420+
} else if (strncmp(s, "db/", 3) == 0) {
421+
s+=3;
422+
_play_type = LEVEL_DB;
423+
} else if (strncmp(s, "main/", 5) == 0) {
449424
s+=5;
425+
_play_type = LEVEL_MAIN;
426+
} else {
427+
break;
428+
}
429+
430+
_play_id = atoi(s);
431+
P.add_action(action, 0);
432+
} else if (strncmp(s, "sandbox/", 8) == 0) {
433+
s+=8;
434+
435+
int action = ACTION_IGNORE;
436+
437+
if (strncmp(s, "local/", 6) == 0) {
438+
s+=6;
439+
_play_type = LEVEL_LOCAL;
440+
action = ACTION_OPEN;
441+
} else if (strncmp(s, "db/", 3) == 0) {
442+
s+=3;
443+
_play_type = LEVEL_DB;
444+
action = ACTION_DERIVE;
445+
}
450446

451-
int action = ACTION_IGNORE;
447+
_play_id = atoi(s);
448+
P.add_action(action, _play_id);
449+
} else if (strncmp(s, "edit/", 5) == 0) {
450+
s+=5;
452451

453-
if (strncmp(s, "db/", 3) == 0) {
454-
s+=3;
455-
_play_type = LEVEL_DB;
456-
action = ACTION_EDIT;
457-
}
452+
int action = ACTION_IGNORE;
458453

459-
_play_id = atoi(s);
460-
P.add_action(action, _play_id);
454+
if (strncmp(s, "db/", 3) == 0) {
455+
s+=3;
456+
_play_type = LEVEL_DB;
457+
action = ACTION_EDIT;
461458
}
462459

463-
} while (0);
464-
}
460+
_play_id = atoi(s);
461+
P.add_action(action, _play_id);
462+
}
463+
464+
} while (0);
465465
}
466466

467467
void

src/tms/backend/main.cc

Lines changed: 59 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include <pwd.h>
2828
#endif
2929

30-
#ifdef __EMSCRIPTEN__
30+
#ifdef TMS_BACKEND_EMSCRIPTEN
3131
#include <emscripten.h>
3232
#endif
3333

@@ -66,6 +66,21 @@ to reproduce it, if possible.
6666
exit(1);
6767
}
6868

69+
void redirect_log_output() {
70+
#if !defined(DEBUG) && !defined(TMS_BACKEND_EMSCRIPTEN)
71+
char logfile[1024];
72+
snprintf(logfile, 1023, "%s/run.log", tms_storage_path());
73+
74+
tms_infof("Redirecting log output to %s", logfile);
75+
FILE *log = fopen(logfile, "w+");
76+
if (log) {
77+
_f_out = log;
78+
} else {
79+
tms_errorf("Could not open log file for writing! Nevermind.");
80+
}
81+
#endif
82+
}
83+
6984
void print_log_header() {
7085
tms_printf( \
7186
" _ _ _ \n"
@@ -77,11 +92,35 @@ void print_log_header() {
7792
"Version %s, commit %s\n", principia_version_string(), principia_version_hash());
7893
}
7994

95+
static void find_data_dir() {
96+
#ifndef TMS_BACKEND_ANDROID
97+
// Check if we're in the right place
98+
struct stat st{};
99+
if (stat("data", &st) != 0) {
100+
// We're in the build dir, go up
101+
tms_infof("chdirring to ../");
102+
chdir("../");
103+
104+
// How about now?
105+
if (stat("data", &st) != 0) {
106+
// If that doesn't work we're assuming a system install.
107+
tms_infof("chdirring to ./share/principia/");
108+
chdir("./share/principia/");
109+
110+
if (stat("data", &st) != 0) {
111+
// We're doomed, better just fail.
112+
tms_fatalf("Could not find data directories.");
113+
}
114+
}
115+
}
116+
#endif
117+
}
118+
80119
int main(int argc, char **argv)
81120
{
82121
int done = 0;
83122

84-
#ifndef __ANDROID__
123+
#ifndef TMS_BACKEND_ANDROID
85124
signal(SIGSEGV, _catch_signal);
86125

87126
#ifdef TMS_BACKEND_WINDOWS
@@ -93,6 +132,7 @@ int main(int argc, char **argv)
93132
char* exedir = SDL_GetBasePath();
94133
tms_infof("chdirring to %s", exedir);
95134
chdir(exedir);
135+
#endif
96136

97137
// Switch to portable if ./portable.txt exists next to binary
98138
if (access("portable.txt", F_OK) == 0) {
@@ -102,44 +142,15 @@ int main(int argc, char **argv)
102142

103143
tms_storage_create_dirs();
104144

105-
#if defined(SDL_HINT_APP_NAME)
145+
#ifdef SDL_HINT_APP_NAME
106146
SDL_SetHint(SDL_HINT_APP_NAME, "Principia");
107147
#endif
108148

109-
#if !defined(DEBUG) && !defined(__EMSCRIPTEN__)
110-
char logfile[1024];
111-
snprintf(logfile, 1023, "%s/run.log", tms_storage_path());
112-
113-
tms_infof("Redirecting log output to %s", logfile);
114-
FILE *log = fopen(logfile, "w+");
115-
if (log) {
116-
_f_out = log;
117-
} else {
118-
tms_errorf("Could not open log file for writing! Nevermind.");
119-
}
120-
#endif
149+
redirect_log_output();
121150

122151
print_log_header();
123152

124-
// Check if we're in the right place
125-
struct stat st{};
126-
if (stat("data", &st) != 0) {
127-
// We're in the build dir, go up
128-
tms_infof("chdirring to ../");
129-
chdir("../");
130-
131-
// How about now?
132-
if (stat("data", &st) != 0) {
133-
// If that doesn't work we're assuming a system install.
134-
tms_infof("chdirring to ./share/principia/");
135-
chdir("./share/principia/");
136-
137-
if (stat("data", &st) != 0) {
138-
// We're doomed, better just fail.
139-
tms_fatalf("Could not find data directories.");
140-
}
141-
}
142-
}
153+
find_data_dir();
143154

144155
SDL_version compiled;
145156
SDL_VERSION(&compiled);
@@ -153,9 +164,14 @@ int main(int argc, char **argv)
153164

154165
tms_infof("Initializing SDL...");
155166
SDL_Init(SDL_INIT_VIDEO);
167+
168+
#ifdef TMS_BACKEND_EMSCRIPTEN
169+
_tms.window_width = 1280;
170+
_tms.window_height = 720;
171+
172+
#elif !defined(TMS_BACKEND_ANDROID)
156173
SDL_DisplayMode mode;
157174
SDL_GetCurrentDisplayMode(0, &mode);
158-
159175
_tms.window_width = 1280;
160176

161177
if (mode.w <= 1280)
@@ -166,22 +182,16 @@ int main(int argc, char **argv)
166182
_tms.window_height = (int)((double)_tms.window_width * .5625);
167183

168184
tms_infof("set initial res to %dx%d", _tms.window_width, _tms.window_height);
185+
#endif
169186

170187
tproject_set_args(argc, argv);
171188

172-
#else // ANDROID
173-
174-
tms_storage_create_dirs();
175-
SDL_Init(SDL_INIT_VIDEO);
176-
177-
#endif
178-
179189
tms_init();
180190

181191
if (_tms.screen == 0)
182192
tms_fatalf("Context has no initial screen!");
183193

184-
#ifdef __EMSCRIPTEN__
194+
#ifdef TMS_BACKEND_EMSCRIPTEN
185195
emscripten_set_main_loop(mainloop, 0, 1);
186196
#else
187197
do {
@@ -292,17 +302,17 @@ static void mainloop()
292302
int
293303
tbackend_init_surface()
294304
{
295-
#ifndef __ANDROID__
305+
uint32_t flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
306+
307+
#ifdef TMS_BACKEND_ANDROID
308+
flags |= SDL_WINDOW_FULLSCREEN;
309+
#else
296310
_tms.window_width = settings["window_width"]->v.i;
297311
_tms.window_height = settings["window_height"]->v.i;
298312

299313
_tms.xppcm = 108.f/2.54f * 1.5f;
300314
_tms.yppcm = 107.f/2.54f * 1.5f;
301-
#endif
302315

303-
uint32_t flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
304-
305-
#ifndef __ANDROID__
306316
if (settings["window_maximized"]->v.b)
307317
flags |= SDL_WINDOW_MAXIMIZED;
308318

@@ -311,8 +321,6 @@ tbackend_init_surface()
311321

312322
if (settings["window_resizable"]->v.b)
313323
flags |= SDL_WINDOW_RESIZABLE;
314-
#else
315-
flags |= SDL_WINDOW_FULLSCREEN;
316324
#endif
317325

318326
tms_infof("Creating window...");
@@ -326,10 +334,9 @@ tbackend_init_surface()
326334

327335
_tms._window = _window;
328336

329-
#ifdef __ANDROID__
337+
#ifdef TMS_BACKEND_ANDROID
330338
SDL_GL_GetDrawableSize(_window, &_tms.window_width, &_tms.window_height);
331339

332-
_tms._window = _window;
333340
float density_x, density_y;
334341
SDL_GetDisplayDPI(0, NULL, &density_x, &density_y);
335342
_tms.xppcm = density_x / 2.54f;

0 commit comments

Comments
 (0)