Permalink
Browse files

Made the SDL port compile under emscripten.

  • Loading branch information...
allefant committed Jul 21, 2015
1 parent 3cb1be6 commit 2351d027f82548787220ea6034740146ab121245
@@ -34,6 +34,7 @@ if(WANT_ANDROID)
set(ANDROID_TARGET "android-12" CACHE STRING "What Android target to compile for.")
endif(WANT_ANDROID)
option(ALLEGRO_SDL "Build using the SDL backend (experimental)" OFF)
option(ALLEGRO_EMSCRIPTEN "Build using SDL-emscripten (experimental)" OFF)
option(WANT_STATIC_RUNTIME "Whether or not to link the C and C++ runtimes statically (currently only implemented for MSVC)" OFF)
# Set the project name.
@@ -435,14 +436,23 @@ include(FindPkgConfig)
include(TestBigEndian)
if(NOT IPHONE)
if((NOT IPHONE) AND (NOT ALLEGRO_EMSCRIPTEN))
test_big_endian(ALLEGRO_BIG_ENDIAN)
endif(NOT IPHONE)
endif()
if(NOT ALLEGRO_BIG_ENDIAN)
set(ALLEGRO_LITTLE_ENDIAN 1)
endif(NOT ALLEGRO_BIG_ENDIAN)
if(ALLEGRO_EMSCRIPTEN)
set(SUPPORT_OPENGL 1)
set(ALLEGRO_CFG_OPENGL 1)
set(ALLEGRO_CFG_OPENGLES 1)
set(ALLEGRO_CFG_OPENGLES2 1)
set(ALLEGRO_CFG_OPENGL_PROGRAMMABLE_PIPELINE 1)
set(CMAKE_EXECUTABLE_SUFFIX ".bc")
endif()
check_include_files(dirent.h ALLEGRO_HAVE_DIRENT_H)
check_include_files(inttypes.h ALLEGRO_HAVE_INTTYPES_H)
check_include_files(linux/input.h ALLEGRO_HAVE_LINUX_INPUT_H)
@@ -51,6 +51,10 @@ endif(SUPPORT_AUDIO)
if(SUPPORT_FONT AND WANT_TTF)
find_package(Freetype)
if(ALLEGRO_EMSCRIPTEN)
set(FREETYPE_FOUND 1)
set(FREETYPE_LIBRARY "libfreetype.a" CACHE STRING "lib" FORCE PARENT_SCOPE)
endif()
if(FREETYPE_FOUND)
add_subdirectory(ttf)
set(SUPPORT_TTF 1 PARENT_SCOPE)
@@ -78,6 +78,14 @@ if(WANT_NATIVE_IMAGE_LOADER)
endif(IPHONE)
endif(WANT_NATIVE_IMAGE_LOADER)
if(ALLEGRO_EMSCRIPTEN)
set(ALLEGRO_CFG_IIO_HAVE_PNG 1)
set(ALLEGRO_CFG_IIO_SUPPORT_PNG 1)
list(APPEND IMAGE_SOURCES png.c)
set(CMAKE_C_FLAGS "-s USE_ZLIB=1 -s USE_LIBPNG=1")
endif()
# Now look for third party libraries to handle the unsupported formats
if(WANT_IMAGE_PNG AND NOT ALLEGRO_CFG_IIO_SUPPORT_PNG)
@@ -184,7 +184,7 @@ if(WANT_D3D AND D3DX9_FOUND)
example(ex_d3d ex_d3d.cpp ${D3DX9_LIBRARY})
endif(WANT_D3D AND D3DX9_FOUND)
if(SUPPORT_OPENGL AND NOT IPHONE AND NOT ANDROID AND NOT ALLEGRO_RASPBERRYPI)
if(SUPPORT_OPENGL AND NOT IPHONE AND NOT ANDROID AND NOT ALLEGRO_RASPBERRYPI AND NOT ALLEGRO_EMSCRIPTEN)
example(ex_gldepth ${FONT} ${IMAGE})
example(ex_glext)
example(ex_opengl)
@@ -6,6 +6,11 @@
#include "common.c"
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
#define FPS 60
#define MAX_SPRITES 1024
@@ -229,15 +234,22 @@ static void redraw(void)
}
#ifndef __EMSCRIPTEN__
int main(int argc, char **argv)
{
#endif
ALLEGRO_TIMER *timer;
ALLEGRO_EVENT_QUEUE *queue;
ALLEGRO_MONITOR_INFO info;
int w = 640, h = 480;
bool done = false;
bool need_redraw = true;
bool background = false;
#ifdef __EMSCRIPTEN__
int main(int argc, char **argv)
{
#endif
example.show_help = true;
example.hold_bitmap_drawing = false;
@@ -259,7 +271,7 @@ int main(int argc, char **argv)
al_get_monitor_info(0, &info);
#if defined ALLEGRO_CFG_OPENGLES
#if defined ALLEGRO_CFG_OPENGLES && !defined __EMSCRIPTEN__
al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW);
#endif
al_set_new_display_option(ALLEGRO_SUPPORTED_ORIENTATIONS,
@@ -312,12 +324,29 @@ int main(int argc, char **argv)
al_start_timer(timer);
while (!done) {
#ifdef __EMSCRIPTEN__
void single_frame(void);
emscripten_set_main_loop(single_frame, 0, true);
}
return 0;
}
void single_frame(void)
{
{
#endif
float x, y;
ALLEGRO_EVENT event;
w = al_get_display_width(example.display);
h = al_get_display_height(example.display);
#ifdef __EMSCRIPTEN__
if (!background && need_redraw) {
#else
if (!background && need_redraw && al_is_event_queue_empty(queue)) {
#endif
double t = -al_get_time();
add_time();
al_clear_to_color(al_map_rgb_f(0, 0, 0));
@@ -328,7 +357,11 @@ int main(int argc, char **argv)
need_redraw = false;
}
#ifdef __EMSCRIPTEN__
while (al_get_next_event(queue, &event)) {
#else
al_wait_for_event(queue, &event);
#endif
switch (event.type) {
case ALLEGRO_EVENT_KEY_CHAR: /* includes repeats */
if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
@@ -431,11 +464,16 @@ int main(int argc, char **argv)
break;
}
}
#ifdef __EMSCRIPTEN__
}
}
#else
}
al_destroy_bitmap(example.bitmap);
return 0;
#endif
}
/* vim: set sts=3 sw=3 et: */
@@ -58,7 +58,7 @@
#define GL_GLEXT_PROTOTYPES
#endif
#elif defined ALLEGRO_ANDROID || defined ALLEGRO_RASPBERRYPI
#elif defined ALLEGRO_ANDROID || defined ALLEGRO_RASPBERRYPI || defined __EMSCRIPTEN__
#include <GLES/gl.h>
#include <GLES/glext.h>
@@ -74,7 +74,7 @@
#define GL_RGBA8 GL_RGBA8_OES
#ifndef ALLEGRO_RASPBERRYPI
#if !defined ALLEGRO_RASPBERRYPI && !defined __EMSCRIPTEN__
#define GL_FRAMEBUFFER_BINDING_EXT GL_FRAMEBUFFER_BINDING_OES
#define GL_FRAMEBUFFER_EXT GL_FRAMEBUFFER_OES
#define glBlendEquation glBlendEquationOES
@@ -7,6 +7,7 @@
void _al_init_timers(void);
int _al_get_active_timers_count(void);
double _al_timer_thread_handle_tick(double interval);
#ifdef __cplusplus
}
@@ -10,6 +10,10 @@ typedef struct
{
ALLEGRO_SYSTEM system;
ALLEGRO_MUTEX *mutex;
#ifdef __EMSCRIPTEN__
double timer_time;
#endif
} ALLEGRO_SYSTEM_SDL;
typedef struct ALLEGRO_DISPLAY_SDL
@@ -104,7 +104,9 @@ ALLEGRO_DISPLAY *al_create_display(int w, int h)
al_destroy_display(display);
return NULL;
}
#ifdef __EMSCRIPTEN__
al_use_shader(display->default_shader);
#endif
}
/* Clear the screen */
@@ -28,6 +28,16 @@
#endif
#endif
#ifdef __EMSCRIPTEN__
typedef int errno_t;
static errno_t strerror_s(char *buffer, size_t numberOfElements,
int errnum )
{
return 0;
}
#endif
#include <stdio.h>
#include "allegro5/internal/aintern.h"
@@ -719,7 +719,7 @@ static void ogl_flip_blocks(ALLEGRO_LOCKED_REGION *lr, int wc, int hc)
static ALLEGRO_LOCKED_REGION *ogl_lock_compressed_region(ALLEGRO_BITMAP *bitmap,
int x, int y, int w, int h, int flags)
{
#if !defined ALLEGRO_ANDROID && !defined ALLEGRO_IPHONE
#if !defined ALLEGRO_ANDROID && !defined ALLEGRO_IPHONE && !defined __EMSCRIPTEN__
ALLEGRO_BITMAP_EXTRA_OPENGL *const ogl_bitmap = bitmap->extra;
ALLEGRO_DISPLAY *disp;
ALLEGRO_DISPLAY *old_disp = NULL;
@@ -833,7 +833,9 @@ static ALLEGRO_LOCKED_REGION *ogl_lock_compressed_region(ALLEGRO_BITMAP *bitmap,
}
}
#ifdef GL_CLIENT_PIXEL_STORE_BIT
glPopClientAttrib();
#endif
if (old_disp != NULL) {
_al_set_current_display_only(old_disp);
@@ -43,7 +43,7 @@
IS_ANDROID_AND(al_get_display_flags(dpy) & ALLEGRO_PROGRAMMABLE_PIPELINE)
/* XXX still hacky */
#if defined ALLEGRO_RASPBERRYPI
#if defined ALLEGRO_RASPBERRYPI || defined __EMSCRIPTEN__
#define GL_COLOR_ATTACHMENT0_EXT GL_COLOR_ATTACHMENT0
#define GL_FRAMEBUFFER_BINDING_EXT GL_FRAMEBUFFER_BINDING
#define GL_FRAMEBUFFER_COMPLETE_EXT GL_FRAMEBUFFER_COMPLETE
@@ -87,6 +87,11 @@ static ALLEGRO_DISPLAY *sdl_create_display_locked(int w, int h)
d->h = h;
d->flags = al_get_new_display_flags();
d->flags |= ALLEGRO_OPENGL;
#ifdef __EMSCRIPTEN__
d->flags |= ALLEGRO_PROGRAMMABLE_PIPELINE;
#endif
int flags = SDL_WINDOW_OPENGL;
if (d->flags & ALLEGRO_FULLSCREEN)
flags |= SDL_WINDOW_FULLSCREEN;
@@ -1,6 +1,7 @@
#include "allegro5/allegro.h"
#include "allegro5/internal/aintern_system.h"
#include "allegro5/platform/allegro_internal_sdl.h"
#include "allegro5/internal/aintern_timer.h"
ALLEGRO_DEBUG_CHANNEL("SDL")
@@ -101,6 +102,12 @@ static void sdl_heartbeat(void)
}
}
}
#ifdef __EMSCRIPTEN__
double t = al_get_time();
double interval = t - s->timer_time;
_al_timer_thread_handle_tick(interval);
s->timer_time = t;
#endif
al_unlock_mutex(s->mutex);
}
@@ -127,6 +134,10 @@ static void sdl_heartbeat_init(void)
* once the system was created.
*/
s->mutex = al_create_mutex();
#ifdef __EMSCRIPTEN__
s->timer_time = al_get_time();
#endif
}
static void sdl_shutdown_system(void)
@@ -34,7 +34,6 @@
/* forward declarations */
static double timer_thread_handle_tick(double interval);
static void timer_handle_tick(ALLEGRO_TIMER *timer);
@@ -109,7 +108,7 @@ static void timer_thread_proc(_AL_THREAD *self, void *unused)
old_time = new_time;
/* Handle a tick. */
interval = timer_thread_handle_tick(interval);
interval = _al_timer_thread_handle_tick(interval);
}
_al_mutex_unlock(&timers_mutex);
}
@@ -124,7 +123,7 @@ static void timer_thread_proc(_AL_THREAD *self, void *unused)
* returns the duration that the timer thread should try to sleep
* next time.
*/
static double timer_thread_handle_tick(double interval)
double _al_timer_thread_handle_tick(double interval)
{
double new_delay = 0.032768;
unsigned int i;

0 comments on commit 2351d02

Please sign in to comment.