Skip to content

Commit 3c33e1e

Browse files
committed
EMCC: Emscripten version wip
- fix crash after showing the menu
1 parent 1b0735c commit 3c33e1e

File tree

5 files changed

+58
-23
lines changed

5 files changed

+58
-23
lines changed

src/platform/emcc/Makefile.am

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ sbasic_html_SOURCES = \
3737
../../ui/textedit.cpp \
3838
../../ui/image.cpp \
3939
../../ui/strlib.cpp \
40-
../../ui/audio.cpp \
4140
runtime.h runtime.cpp \
4241
canvas.h canvas.cpp \
4342
main.cpp

src/platform/emcc/canvas.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ Font::Font(int size, bool bold, bool italic) :
173173
_face.append(size).append("px monospace");
174174
}
175175

176+
Font::~Font() {
177+
if (this == font) {
178+
font = nullptr;
179+
}
180+
}
181+
176182
Canvas::Canvas() :
177183
_clip(nullptr),
178184
_id(-1),
@@ -190,10 +196,15 @@ Canvas::Canvas(int width, int height) :
190196
Canvas::~Canvas() {
191197
if (_id != -1) {
192198
EM_ASM_({
193-
var element = document.getElementById($0);
199+
var element = document.getElementById($0 == -1 ? "canvas" : "canvas_" + $0);
194200
document.body.removeChild(element);
195201
}, _id);
196202
}
203+
if (this == screen) {
204+
screen = nullptr;
205+
} else if (this == drawTarget) {
206+
drawTarget = nullptr;
207+
}
197208
}
198209

199210
bool Canvas::create(int width, int height) {
@@ -359,11 +370,8 @@ void maGetImageData(MAHandle maHandle, void *dst, const MARect *srcRect, int str
359370
MAHandle maFontLoadDefault(int type, int style, int size) {
360371
bool italic = (style & FONT_STYLE_ITALIC);
361372
bool bold = (style & FONT_STYLE_BOLD);
362-
if (font) {
363-
delete font;
364-
}
365-
font = new Font(size, bold, italic);
366-
return (MAHandle)font;
373+
Font *newFont = new Font(size, bold, italic);
374+
return (MAHandle)newFont;
367375
}
368376

369377
MAHandle maFontSetCurrent(MAHandle maHandle) {
@@ -374,9 +382,6 @@ MAHandle maFontSetCurrent(MAHandle maHandle) {
374382
int maFontDelete(MAHandle maHandle) {
375383
if (maHandle != -1) {
376384
Font *handleFont = (Font *)maHandle;
377-
if (font == handleFont) {
378-
font = nullptr;
379-
}
380385
delete handleFont;
381386
}
382387
return RES_FONT_OK;

src/platform/emcc/canvas.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ struct Canvas {
2424

2525
struct Font {
2626
Font(int size, bool bold, bool italic);
27+
virtual ~Font();
2728

2829
int _size;
2930
bool _bold;
3031
bool _italic;
3132

32-
strlib::String _face;
33+
strlib::String _face;
3334
};

src/platform/emcc/runtime.cpp

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
#include "common/smbas.h"
1414
#include "lib/maapi.h"
1515
#include "ui/utils.h"
16-
#include "ui/audio.h"
1716
#include "ui/theme.h"
1817
#include "platform/emcc/runtime.h"
1918
#include "platform/emcc/main_bas.h"
2019

2120
#define MAIN_BAS "__main_bas__"
2221
#define WAIT_INTERVAL 10
22+
#define EVENT_TYPE_RESTART 101
23+
#define EVENT_TYPE_SHOW_MENU 102
2324

2425
Runtime *runtime;
2526

@@ -54,6 +55,7 @@ Runtime::Runtime() :
5455
logEntered();
5556
runtime = this;
5657

58+
// note: cannot call emscripten_sleep from inside the callbacks
5759
emscripten_set_mousedown_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, this, true, mouse_callback);
5860
emscripten_set_mouseup_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, this, true, mouse_callback);
5961
emscripten_set_mousemove_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, this, true, mouse_callback);
@@ -124,9 +126,7 @@ void Runtime::handleMouse(int eventType, const EmscriptenMouseEvent *e) {
124126
switch (eventType) {
125127
case EMSCRIPTEN_EVENT_MOUSEDOWN:
126128
if (e->button == 2) {
127-
_menuX = e->clientX;
128-
_menuY = e->clientY;
129-
showMenu();
129+
pushEvent(getMotionEvent(EVENT_TYPE_SHOW_MENU, e));
130130
} else {
131131
pushEvent(getMotionEvent(EVENT_TYPE_POINTER_PRESSED, e));
132132
}
@@ -144,7 +144,7 @@ void Runtime::pause(int timeout) {
144144
if (timeout == -1) {
145145
if (hasEvent()) {
146146
MAEvent *event = popEvent();
147-
handleEvent(*event);
147+
processEvent(*event);
148148
delete event;
149149
}
150150
} else {
@@ -154,7 +154,7 @@ void Runtime::pause(int timeout) {
154154
break;
155155
} else if (hasEvent()) {
156156
MAEvent *event = popEvent();
157-
handleEvent(*event);
157+
processEvent(*event);
158158
delete event;
159159
}
160160
emscripten_sleep(WAIT_INTERVAL);
@@ -164,7 +164,6 @@ void Runtime::pause(int timeout) {
164164
}
165165
}
166166
}
167-
168167
}
169168

170169
MAEvent Runtime::processEvents(int waitFlag) {
@@ -187,7 +186,7 @@ MAEvent Runtime::processEvents(int waitFlag) {
187186
MAEvent event;
188187
if (hasEvent()) {
189188
MAEvent *nextEvent = popEvent();
190-
handleEvent(*nextEvent);
189+
processEvent(*nextEvent);
191190
event = *nextEvent;
192191
delete nextEvent;
193192
} else {
@@ -196,14 +195,30 @@ MAEvent Runtime::processEvents(int waitFlag) {
196195
return event;
197196
}
198197

198+
void Runtime::processEvent(MAEvent &event) {
199+
switch (event.type) {
200+
case EVENT_TYPE_KEY_PRESSED:
201+
//handleKeyEvent(event);
202+
handleEvent(event);
203+
break;
204+
case EVENT_TYPE_RESTART:
205+
setRestart();
206+
break;
207+
case EVENT_TYPE_SHOW_MENU:
208+
_menuX = event.point.x;
209+
_menuY = event.point.y;
210+
showMenu();
211+
break;
212+
default:
213+
handleEvent(event);
214+
break;
215+
}
216+
}
217+
199218
void Runtime::runShell() {
200219
logEntered();
201-
202-
audio_open();
203220
runMain(MAIN_BAS);
204-
audio_close();
205221
_state = kDoneState;
206-
logLeaving();
207222
}
208223

209224
void Runtime::setClipboardText(const char *text) {
@@ -271,3 +286,17 @@ int osd_devrestore(void) {
271286
runtime->setRunning(false);
272287
return 0;
273288
}
289+
290+
291+
void osd_audio(const char *path) {
292+
}
293+
294+
void osd_beep() {
295+
}
296+
297+
void osd_clear_sound_queue() {
298+
}
299+
300+
void osd_sound(int frequency, int millis, int volume, int background) {
301+
}
302+

src/platform/emcc/runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct Runtime : public System {
3333
void pushEvent(MAEvent *event) { _eventQueue->push(event); }
3434
MAEvent *popEvent() { return _eventQueue->pop(); }
3535
MAEvent processEvents(int waitFlag);
36+
void processEvent(MAEvent &event);
3637
bool run(const char *bas) { return execute(bas); }
3738
void runShell();
3839
void resize(int w, int h);

0 commit comments

Comments
 (0)