-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.cpp
101 lines (84 loc) · 2.06 KB
/
index.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
playarea1 is:
1024x768
starting pixel at(top left corner is):
X=61
Y=183
end pixel at(bottom right corner is):
X=1084
Y=950
----------
playarea2 is:
720x480
starting pixel at(top left corner is):
X=1137
Y=75
end pixel at(bottom right corner is):
X=1856
Y=554
-s TOTAL_MEMORY=30015488
*/
#include <stdio.h>
#include <stdlib.h>
#include <emscripten/emscripten.h>
#include <emscripten/html5.h>
#include "src/Game.h"
Game * game;
//Callbacks
extern "C"
{
EM_BOOL static handleKeyboard(int eventType, const EmscriptenKeyboardEvent *e, void *userData)
{
// printf("%s | %d\n", e->code, eventType);
game->getPlayerController()->handleKeyboard(eventType, e, userData);
return true;
}
EM_BOOL handleMouse(int eventType, const EmscriptenMouseEvent *e, void *userData)
{
//printf("%s | %d\n", mouseEvent->code, eventType);
game->getPlayerController()->handleMouse(eventType, e, userData);
return true;
}
EMSCRIPTEN_KEEPALIVE uint8_t * getMemoryOffset()
{
return game->getScreen()->getPixels();
}
EMSCRIPTEN_KEEPALIVE int getScreenWidth()
{
return game->getScreen()->getWidth();
}
EMSCRIPTEN_KEEPALIVE int getScreenHeight()
{
return game->getScreen()->getHeight();
}
EMSCRIPTEN_KEEPALIVE void addImage(int pointerValue, int width, int height,int id)
{
game->loadImage(pointerValue,width,height,id);
}
EMSCRIPTEN_KEEPALIVE void addMap(int pointerValue)
{
game->loadMap(pointerValue);
}
EM_JS(void, render, (), {
Module.renderImageFromMemory();
});
EM_JS(void, renderSetup, (), {
Module.renderSetup();
});
}
void updateLoop()
{
game->update();
render();
}
int main(void)
{
game = new Game();
renderSetup();
//Callback setup
emscripten_set_keydown_callback(0, 0, 1, handleKeyboard);
emscripten_set_keyup_callback(0, 0, 1, handleKeyboard);
emscripten_set_click_callback(0,0,0,handleMouse);
//Main loop handler
emscripten_set_main_loop(updateLoop, -1, 1);
}