Emscripten build

This commit is contained in:
kevinbchen 2021-01-04 00:58:24 -08:00
parent 63466dc2ea
commit 6ade1624f1
4 changed files with 115 additions and 79 deletions

View File

@ -89,13 +89,16 @@ endif()
# Link and create SDL2 Definition if we're using it
if (SDL2_ENABLED)
add_compile_definitions(BLAH_USE_SDL2)
if (${CMAKE_SYSTEM_NAME} MATCHES "Emscripten")
set_target_properties(blah PROPERTIES COMPILE_FLAGS "-s USE_SDL=2")
else()
if (NOT DEFINED SDL2_INCLUDE_DIRS OR NOT DEFINED SDL2_LIBRARIES)
find_package(SDL2 REQUIRED)
endif()
target_include_directories(blah PUBLIC "$<BUILD_INTERFACE:${SDL2_INCLUDE_DIRS}>")
set(LIBS ${LIBS} ${SDL2_LIBRARIES})
endif()
endif()
target_link_libraries(blah PUBLIC ${LIBS})

View File

@ -7,13 +7,20 @@
#include "../internal/graphics_backend.h"
#include "../internal/input_backend.h"
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <emscripten/html5.h>
#endif
using namespace Blah;
namespace
{
Config app_config;
bool app_is_running = false;
bool app_is_exiting = false;
static Config app_config;
static bool app_is_running = false;
static bool app_is_exiting = false;
static uint64_t time_last;
static uint64_t time_accumulator = 0;
}
Config::Config()
@ -34,48 +41,9 @@ Config::Config()
on_error = nullptr;
}
bool App::run(const Config* c)
{
BLAH_ASSERT(!app_is_running, "The Application is already running");
BLAH_ASSERT(c != nullptr, "The Application requires a valid Config");
BLAH_ASSERT(c->name != nullptr, "The Application Name cannot be null");
BLAH_ASSERT(c->width > 0 && c->height > 0, "The Width and Height must be larget than 0");
BLAH_ASSERT(c->max_updates > 0, "Max Updates must be >= 1");
BLAH_ASSERT(c->target_framerate > 0, "Target Framerate must be >= 1");
namespace {
app_config = *c;
app_is_running = true;
app_is_exiting = false;
// initialize the system
if (!PlatformBackend::init(&app_config))
{
Log::error("Failed to initialize Platform module");
return false;
}
// initialize graphics
if (!GraphicsBackend::init())
{
Log::error("Failed to initialize Graphics module");
return false;
}
// input
InputBackend::init();
// startup
if (app_config.on_startup != nullptr)
app_config.on_startup();
uint64_t time_last = PlatformBackend::time();
uint64_t time_accumulator = 0;
// display window
PlatformBackend::ready();
while (!app_is_exiting)
{
void loop_iteration() {
// poll system events
PlatformBackend::frame();
@ -143,9 +111,58 @@ bool App::run(const Config* c)
GraphicsBackend::after_render();
PlatformBackend::present();
}
}
} // namespace
bool App::run(const Config* c)
{
BLAH_ASSERT(!app_is_running, "The Application is already running");
BLAH_ASSERT(c != nullptr, "The Application requires a valid Config");
BLAH_ASSERT(c->name != nullptr, "The Application Name cannot be null");
BLAH_ASSERT(c->width > 0 && c->height > 0, "The Width and Height must be larget than 0");
BLAH_ASSERT(c->max_updates > 0, "Max Updates must be >= 1");
BLAH_ASSERT(c->target_framerate > 0, "Target Framerate must be >= 1");
app_config = *c;
app_is_running = true;
app_is_exiting = false;
// initialize the system
if (!PlatformBackend::init(&app_config))
{
Log::error("Failed to initialize Platform module");
return false;
}
// initialize graphics
if (!GraphicsBackend::init())
{
Log::error("Failed to initialize Graphics module");
return false;
}
// input
InputBackend::init();
// startup
if (app_config.on_startup != nullptr)
app_config.on_startup();
time_last = PlatformBackend::time();
time_accumulator = 0;
// display window
PlatformBackend::ready();
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(loop_iteration, 0, 1);
#else
while (!app_is_exiting)
{
loop_iteration();
}
#endif
// shutdown
if (app_config.on_shutdown != nullptr)
app_config.on_shutdown();

View File

@ -19,7 +19,11 @@ namespace
const ShaderData shader_data = {
// vertex shader
#ifdef __EMSCRIPTEN__
"#version 300 es\n"
#else
"#version 330\n"
#endif
"uniform mat4 u_matrix;\n"
"layout(location=0) in vec2 a_position;\n"
"layout(location=1) in vec2 a_tex;\n"
@ -37,7 +41,12 @@ namespace
"}",
// fragment shader
#ifdef __EMSCRIPTEN__
"#version 300 es\n"
"precision mediump float;\n"
#else
"#version 330\n"
#endif
"uniform sampler2D u_texture;\n"
"in vec2 v_tex;\n"
"in vec4 v_col;\n"

View File

@ -81,6 +81,10 @@ bool PlatformBackend::init(const Config* config)
{
flags |= SDL_WINDOW_OPENGL;
#ifdef __EMSCRIPTEN__
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#else
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
@ -93,6 +97,7 @@ bool PlatformBackend::init(const Config* config)
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
#endif
}
// enable DirectX
else if (App::renderer() == Renderer::D3D11)
@ -140,9 +145,11 @@ bool PlatformBackend::init(const Config* config)
void PlatformBackend::ready()
{
#ifndef __EMSCRIPTEN__
// enable V-Sync
if (App::renderer() == Renderer::OpenGL)
SDL_GL_SetSwapInterval(1);
#endif
}
void PlatformBackend::shutdown()