[ion] Initial emscripten support

Change-Id: Id76a45c5c723fae11387e20458bc9555689b9a48
This commit is contained in:
Romain Goyet
2016-11-06 12:06:02 +01:00
parent 3702af0f11
commit ade7f0480b
4 changed files with 93 additions and 0 deletions

8
Makefile.emscripten Normal file
View File

@@ -0,0 +1,8 @@
CC=emcc
CXX=emcc
LD=emcc
SIZE=size
DEBUG=0
USE_LIBA=0
EXE=html

View File

@@ -1,5 +1,8 @@
#include <escher/container.h>
#include <ion/display.h>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
Container::Container() :
m_activeApp(nullptr)
@@ -23,9 +26,13 @@ void Container::run() {
m_window.setFrame(KDRect(0, 0, Ion::Display::Width, Ion::Display::Height));
m_window.redraw();
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop_arg([](void * ctx){ ((Container *)ctx)->step(); }, this, 0, 1);
#else
while (true) {
step();
}
#endif
}
void Container::step() {

View File

@@ -0,0 +1,3 @@
objs += $(addprefix ion/src/emscripten/, \
main.o\
)

View File

@@ -0,0 +1,75 @@
#include <ion.h>
extern "C" {
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <SDL/SDL.h>
}
SDL_Surface * screen = nullptr;
int main(int argc, char * argv[]) {
SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(Ion::Display::Width, Ion::Display::Height, 32, SDL_HWSURFACE);
ion_app();
return 0;
}
void Ion::Display::pushRect(KDRect r, const KDColor * pixels) {
if (SDL_MUSTLOCK(screen)) {
SDL_LockSurface(screen);
}
int pixelNumber = 0;
for (int j=r.top(); j<r.bottom(); j++) {
for (int i=r.left(); i<r.right(); i++) {
KDColor c = pixels[pixelNumber++];
*((Uint32*)screen->pixels + j * Ion::Display::Width + i) = SDL_MapRGB(screen->format, c.red(), c.green(), c.blue());
}
}
if (SDL_MUSTLOCK(screen)) {
SDL_UnlockSurface(screen);
}
SDL_UpdateRect(screen, r.x(), r.y(), r.width(), r.height());
}
void Ion::Display::pushRectUniform(KDRect r, KDColor c) {
Uint32 sdlColor = SDL_MapRGB(screen->format, c.red(), c.green(), c.blue());
SDL_Rect sdlRect = { r.x(), r.y(), r.width(), r.height() };
SDL_FillRect(screen, &sdlRect, sdlColor);
SDL_UpdateRect(screen, r.x(), r.y(), r.width(), r.height());
}
void Ion::Display::pullRect(KDRect r, KDColor * pixels) {
}
Ion::Events::Event Ion::Events::getEvent() {
SDL_Event event;
if (SDL_PollEvent(&event)) {
if (event.type == SDL_KEYDOWN) {
switch(event.key.keysym.sym) {
case SDLK_UP:
return Ion::Events::Event::UP_ARROW;
case SDLK_DOWN:
return Ion::Events::Event::DOWN_ARROW;
case SDLK_LEFT:
return Ion::Events::Event::LEFT_ARROW;
case SDLK_RIGHT:
return Ion::Events::Event::RIGHT_ARROW;
case SDLK_RETURN:
return Ion::Events::Event::ENTER;
case SDLK_ESCAPE:
return Ion::Events::Event::ESC;
}
}
}
return Ion::Events::Event::SHIFT;
}
bool Ion::Keyboard::keyDown(Ion::Keyboard::Key key) {
//SDL_Quit();
return false;
}
void Ion::msleep(long ms) {
usleep(1000*ms);
}