Skip to content

Commit 654fd71

Browse files
committed
Improve web version
1 parent 976889e commit 654fd71

File tree

2 files changed

+40
-40
lines changed

2 files changed

+40
-40
lines changed

html/index.html

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,25 @@
11
<!DOCTYPE html>
22
<html lang="en">
3-
<head>
3+
<head>
44
<meta charset="utf-8">
5-
<title>C port of Ken Thompson's Space Travel</title>
5+
<title>Space Travel</title>
66
<style>
7-
html, body {
8-
margin: 0;
9-
padding: 0;
10-
height: 100%;
11-
display: flex;
12-
justify-content: center;
13-
align-items: center;
14-
background-color: black;
15-
}
16-
canvas {
17-
display: block;
18-
max-width: calc(100% - 40px);
19-
max-height: calc(100% - 40px);
20-
object-fit: contain;
21-
margin: 20px;
22-
}
7+
body {
8+
background: black;
9+
}
10+
canvas {
11+
position: absolute;
12+
max-width: 100%;
13+
max-height: 100%;
14+
top: 50%;
15+
left: 50%;
16+
transform: translate(-50%, -50%);
17+
}
2318
</style>
24-
</head>
25-
<body>
26-
<canvas id="canvas" oncontextmenu="event.preventDefault()"></canvas>
27-
<script>
28-
var Module = {
29-
canvas: (function() { return document.getElementById('canvas'); })()
30-
};
31-
</script>
19+
</head>
20+
<body>
21+
<canvas id="canvas"></canvas>
22+
<script>Module = { canvas }</script>
3223
<script src="st.js"></script>
33-
</body>
24+
</body>
3425
</html>

st.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -913,10 +913,17 @@ void loop(void)
913913
cl = crflg ? "CL" : lanflg ? "L" : " ";
914914
}
915915

916-
void mainloop(void)
916+
void main_loop(void)
917917
{
918918
SDL_Event e;
919-
static unsigned secs = 0;
919+
static unsigned t0 = 0, t;
920+
921+
t = SDL_GetTicks64();
922+
923+
#ifdef __EMSCRIPTEN__
924+
if (t - t0 < 1000/60) return;
925+
#endif
926+
920927
if (!quit) {
921928
if (SDL_PollEvent(&e)) {
922929
if (e.type == SDL_QUIT) quit = true;
@@ -934,26 +941,25 @@ void mainloop(void)
934941
return;
935942
}
936943
contrl(NULL);
937-
if (SDL_GetTicks64() / 1000 != secs) show = !show;
938-
secs = SDL_GetTicks64() / 1000;
944+
if (t/1000 > t0/1000) show = !show;
945+
t0 = t;
939946
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
940947
SDL_RenderClear(renderer);
941948
displist();
942949
loop();
943950
SDL_RenderPresent(renderer);
944-
SDL_Delay(1000/60);
945951
return;
946952
}
947953

948954
SDL_DestroyRenderer(renderer);
949955
SDL_DestroyWindow(window);
950956
SDL_Quit();
951957

952-
#ifdef __EMSCRIPTEN__
958+
#ifdef __EMSCRIPTEN__
953959
emscripten_cancel_main_loop();
954-
#else
960+
#else
955961
exit(0);
956-
#endif
962+
#endif
957963
}
958964

959965
int main(void)
@@ -1012,11 +1018,14 @@ int main(void)
10121018

10131019
pbson = SDL_GetKeyboardState(NULL);
10141020

1015-
#ifdef __EMSCRIPTEN__
1016-
emscripten_set_main_loop(mainloop, 0, 1);
1017-
#else
1018-
while (1) mainloop();
1019-
#endif
1021+
#ifdef __EMSCRIPTEN__
1022+
emscripten_set_main_loop(main_loop, 0, true);
1023+
#else
1024+
while (1) {
1025+
main_loop();
1026+
SDL_Delay(1000/60);
1027+
}
1028+
#endif
10201029

10211030
return 0;
10221031
}

0 commit comments

Comments
 (0)