mirror of
https://github.com/NoelFB/blah.git
synced 2024-11-25 16:18:57 +08:00
Emscripten build
This commit is contained in:
parent
63466dc2ea
commit
6ade1624f1
|
@ -89,13 +89,16 @@ endif()
|
||||||
# Link and create SDL2 Definition if we're using it
|
# Link and create SDL2 Definition if we're using it
|
||||||
if (SDL2_ENABLED)
|
if (SDL2_ENABLED)
|
||||||
add_compile_definitions(BLAH_USE_SDL2)
|
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)
|
if (NOT DEFINED SDL2_INCLUDE_DIRS OR NOT DEFINED SDL2_LIBRARIES)
|
||||||
find_package(SDL2 REQUIRED)
|
find_package(SDL2 REQUIRED)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
target_include_directories(blah PUBLIC "$<BUILD_INTERFACE:${SDL2_INCLUDE_DIRS}>")
|
target_include_directories(blah PUBLIC "$<BUILD_INTERFACE:${SDL2_INCLUDE_DIRS}>")
|
||||||
set(LIBS ${LIBS} ${SDL2_LIBRARIES})
|
set(LIBS ${LIBS} ${SDL2_LIBRARIES})
|
||||||
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
target_link_libraries(blah PUBLIC ${LIBS})
|
target_link_libraries(blah PUBLIC ${LIBS})
|
||||||
|
|
105
src/core/app.cpp
105
src/core/app.cpp
|
@ -7,13 +7,20 @@
|
||||||
#include "../internal/graphics_backend.h"
|
#include "../internal/graphics_backend.h"
|
||||||
#include "../internal/input_backend.h"
|
#include "../internal/input_backend.h"
|
||||||
|
|
||||||
|
#ifdef __EMSCRIPTEN__
|
||||||
|
#include <emscripten.h>
|
||||||
|
#include <emscripten/html5.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
using namespace Blah;
|
using namespace Blah;
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
Config app_config;
|
static Config app_config;
|
||||||
bool app_is_running = false;
|
static bool app_is_running = false;
|
||||||
bool app_is_exiting = false;
|
static bool app_is_exiting = false;
|
||||||
|
static uint64_t time_last;
|
||||||
|
static uint64_t time_accumulator = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Config::Config()
|
Config::Config()
|
||||||
|
@ -34,48 +41,9 @@ Config::Config()
|
||||||
on_error = nullptr;
|
on_error = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool App::run(const Config* c)
|
namespace {
|
||||||
{
|
|
||||||
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;
|
void loop_iteration() {
|
||||||
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)
|
|
||||||
{
|
|
||||||
// poll system events
|
// poll system events
|
||||||
PlatformBackend::frame();
|
PlatformBackend::frame();
|
||||||
|
|
||||||
|
@ -143,9 +111,58 @@ bool App::run(const Config* c)
|
||||||
GraphicsBackend::after_render();
|
GraphicsBackend::after_render();
|
||||||
PlatformBackend::present();
|
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
|
// shutdown
|
||||||
if (app_config.on_shutdown != nullptr)
|
if (app_config.on_shutdown != nullptr)
|
||||||
app_config.on_shutdown();
|
app_config.on_shutdown();
|
||||||
|
|
|
@ -19,7 +19,11 @@ namespace
|
||||||
|
|
||||||
const ShaderData shader_data = {
|
const ShaderData shader_data = {
|
||||||
// vertex shader
|
// vertex shader
|
||||||
|
#ifdef __EMSCRIPTEN__
|
||||||
|
"#version 300 es\n"
|
||||||
|
#else
|
||||||
"#version 330\n"
|
"#version 330\n"
|
||||||
|
#endif
|
||||||
"uniform mat4 u_matrix;\n"
|
"uniform mat4 u_matrix;\n"
|
||||||
"layout(location=0) in vec2 a_position;\n"
|
"layout(location=0) in vec2 a_position;\n"
|
||||||
"layout(location=1) in vec2 a_tex;\n"
|
"layout(location=1) in vec2 a_tex;\n"
|
||||||
|
@ -37,7 +41,12 @@ namespace
|
||||||
"}",
|
"}",
|
||||||
|
|
||||||
// fragment shader
|
// fragment shader
|
||||||
|
#ifdef __EMSCRIPTEN__
|
||||||
|
"#version 300 es\n"
|
||||||
|
"precision mediump float;\n"
|
||||||
|
#else
|
||||||
"#version 330\n"
|
"#version 330\n"
|
||||||
|
#endif
|
||||||
"uniform sampler2D u_texture;\n"
|
"uniform sampler2D u_texture;\n"
|
||||||
"in vec2 v_tex;\n"
|
"in vec2 v_tex;\n"
|
||||||
"in vec4 v_col;\n"
|
"in vec4 v_col;\n"
|
||||||
|
|
|
@ -81,6 +81,10 @@ bool PlatformBackend::init(const Config* config)
|
||||||
{
|
{
|
||||||
flags |= SDL_WINDOW_OPENGL;
|
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_MAJOR_VERSION, 3);
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
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_STENCIL_SIZE, 8);
|
||||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
|
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
|
||||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
|
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
// enable DirectX
|
// enable DirectX
|
||||||
else if (App::renderer() == Renderer::D3D11)
|
else if (App::renderer() == Renderer::D3D11)
|
||||||
|
@ -140,9 +145,11 @@ bool PlatformBackend::init(const Config* config)
|
||||||
|
|
||||||
void PlatformBackend::ready()
|
void PlatformBackend::ready()
|
||||||
{
|
{
|
||||||
|
#ifndef __EMSCRIPTEN__
|
||||||
// enable V-Sync
|
// enable V-Sync
|
||||||
if (App::renderer() == Renderer::OpenGL)
|
if (App::renderer() == Renderer::OpenGL)
|
||||||
SDL_GL_SetSwapInterval(1);
|
SDL_GL_SetSwapInterval(1);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlatformBackend::shutdown()
|
void PlatformBackend::shutdown()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user