--- /dev/null
+#include "ems.h"
+
+#ifdef __EMSCRIPTEN__
+#include <emscripten.h>
+#include <emscripten/html5.h>
+
+EM_JS(void, js_set_ready, (), { Rune.actions.set_ready(); });
+
+EM_JS(void, js_set_choices,
+ (const char *first, const char *second, const char *third), {
+ Rune.actions.set_choices(UTF8ToString(first), UTF8ToString(second),
+ UTF8ToString(third));
+ });
+
+EM_JS(int, canvas_get_width, (),
+ { return document.getElementById("canvas").clientWidth; });
+
+EM_JS(int, canvas_get_height, (),
+ { return document.getElementById("canvas").clientHeight; });
+#else
+#include <iostream>
+#endif
+
+void call_js_set_ready() {
+#ifdef __EMSCRIPTEN__
+ js_set_ready();
+#else
+ std::clog << "WARNING: emscripten not enabled, cannot call js_set_ready()!"
+ << std::endl;
+#endif
+}
+
+void call_js_set_choices(const char *first, const char *second,
+ const char *third) {
+#ifdef __EMSCRIPTEN__
+ js_set_choices(first, second, third);
+#else
+ std::clog << "WARNING: emscripten not enabled, cannot call js_set_choices()!"
+ << std::endl;
+#endif
+}
+
+int call_js_get_canvas_width() {
+
+#ifdef __EMSCRIPTEN__
+ return canvas_get_width();
+#else
+ return 800;
+#endif
+}
+
+int call_js_get_canvas_height() {
+#ifdef __EMSCRIPTEN__
+ return canvas_get_height();
+#else
+ return 500;
+#endif
+}
--- /dev/null
+#ifndef ROCK_PAPER_SCISSORS_DUEL_EMSCRIPTEN_H_
+#define ROCK_PAPER_SCISSORS_DUEL_EMSCRIPTEN_H_
+
+extern void call_js_set_ready();
+extern void call_js_set_choices(const char *first, const char *second, const char *third);
+extern int call_js_get_canvas_width();
+extern int call_js_get_canvas_height();
+
+#endif
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <emscripten/html5.h>
+
+#include "ems.h"
#else
#include <random>
#endif
} // end em exposed functions
-EM_JS(void, js_set_ready, (), { Rune.actions.set_ready(); });
-
-EM_JS(void, js_set_choices,
- (const char *first, const char *second, const char *third), {
- Rune.actions.set_choices(UTF8ToString(first), UTF8ToString(second),
- UTF8ToString(third));
- });
+EM_BOOL resize_event_callback(int event_type, const EmscriptenUiEvent *event,
+ void *ud) {
+ if (event_type == EMSCRIPTEN_EVENT_RESIZE) {
+ SetWindowSize(call_js_get_canvas_width(), call_js_get_canvas_height());
+ }
+ return false;
+}
#endif
+void game_update(void *game_ptr) {
+ BeginDrawing();
+ ClearBackground(BLACK);
+ DrawText("Testing...", 100, 100, 30, RAYWHITE);
+ EndDrawing();
+}
+
int main() {
#ifdef __EMSCRIPTEN__
InitWindow(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT,
#endif
#ifdef __EMSCRIPTEN__
+ SetWindowSize(call_js_get_canvas_width(), call_js_get_canvas_height());
+
+ emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, nullptr, false,
+ resize_event_callback);
+
+ // TODO set game ptr
+ emscripten_set_main_loop_arg(game_update, nullptr, 0, 1);
#else
SetTargetFPS(60);
while (!WindowShouldClose()) {
- BeginDrawing();
- ClearBackground(BLACK);
- DrawText("Testing...", 100, 100, 30, RAYWHITE);
- EndDrawing();
+ game_update(nullptr);
}
CloseAudioDevice();
/rock_paper_scissors_duel.js
/rock_paper_scissors_duel.wasm
/rock_paper_scissors_duel.data
+/index.html
endif
SOURCES = \
- ../src/main.cc
+ ../src/main.cc \
+ ../src/ems.cc
HEADERS = \
../src/constants.h
CXX = source ${HOME}/git/emsdk/emsdk_env.sh && em++
-all: | format rock_paper_scissors_duel.html
+all: | format rock_paper_scissors_duel.html index.html
rock_paper_scissors_duel.html: ${SOURCES} ${HEADERS}
${CXX} -o rock_paper_scissors_duel.html \
${OTHER_FLAGS} \
${SOURCES}
-.PHONY: clean format
+.PHONY: clean format index.html
+
+index.html:
+ ln -sf rock_paper_scissors_duel.html index.html
clean:
rm -f rock_paper_scissors_duel.html
rm -f rock_paper_scissors_duel.js
rm -f rock_paper_scissors_duel.wasm
rm -f rock_paper_scissors_duel.data
+ rm -f index.html
format:
clang-format -i --style=file ${SOURCES} ${HEADERS}