Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web app with emscripten #95

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ script:
- ./build/voidstar --list
- mv -v build/voidstar "build/voidstar-$(git describe --abbrev --dirty --always --tags)-$TRAVIS_OS_NAME-$(basename $CC)"
- if [[ "$TRAVIS_OS_NAME" = 'linux' ]]; then cppcheck --error-exitcode=1 --enable=all -Isrc/include/ -Isrc/common/tdogl/ src/; fi
- |
if [[ "$TRAVIS_OS_NAME" = 'linux' ]] && [[ "$TRAVIS_BRANCH" != 'master' ]] && [[ "$CC" = 'gcc' ]]; then
docker run --rm -it -v "$PWD":/src trzeci/emscripten:sdk-tag-1.37.18-64bit bash -c 'rm -rf build && mkdir build && cd build && cp -vr ../data . && apt update && apt install -y libglm-dev && emcmake cmake --DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_CXX_COMPILER=em++ -DCMAKE_BUILD_TYPE=Release -DCMAKE_MAKE_PROGRAM=emmake .. && emmake make'
fi

after_script:
- set +e
Expand Down
76 changes: 50 additions & 26 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 2.8)
cmake_minimum_required(VERSION 3.0)

# set cmake module path, to search in cmake/modules first
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules")
Expand All @@ -11,7 +11,12 @@ include(static_analysis)
#-----------------------------------------------------------------------------
# GENERAL CONFIGURATION
#-----------------------------------------------------------------------------
project(ProjectName CXX)
project(voidstar CXX)

## These are cmake >= 3.1
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS ON)
#-----------------------------------------------------------------------------
# DEPENDENCIES
#-----------------------------------------------------------------------------
Expand All @@ -20,31 +25,44 @@ project(ProjectName CXX)
# ==> and cmake --help-module FindBoost to known which variables are set.
# find_package(Boost COMPONENTS filesystem threads)
# include_directories(${Boost_INCLUDE_DIRS})
find_package(OpenGL REQUIRED)
include_directories(${OpenGL_INCLUDE_DIR})
find_package(GLEW REQUIRED)
include_directories(${GLEW_INCLUDE_DIRS})
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
include_directories(${GLFW_INCLUDE_DIRS})

if(EMSCRIPTEN)
message(STATUS "here!")
include_directories( /usr/include )

set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH)
message(STATUS "Cmake prefix path: ${CMAKE_PREFIX_PATH}")
endif(EMSCRIPTEN)

if(NOT EMSCRIPTEN)
message(STATUS "not here!")
find_package(OpenGL REQUIRED)
include_directories(${OpenGL_INCLUDE_DIR})
find_package(GLEW REQUIRED)
include_directories(${GLEW_INCLUDE_DIRS})
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
include_directories(${GLFW_INCLUDE_DIRS})
endif()

#-----------------------------------------------------------------------------
# BUILD TYPES & FLAGS
#-----------------------------------------------------------------------------

include(sanitizers)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g3 -O0")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Werror -O2")
#set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Werror -O2")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Werror -O0")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -std=c++1y ${CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}}")
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.9")
else(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.9")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdiagnostics-color")
endif()

# strip binary in release mode
if(CMAKE_BUILD_TYPE MATCHES "RELEASE")
set(CMAKE_EXE_LINKER_FLAGS "-s")
endif()
# # strip binary in release mode
# if(CMAKE_BUILD_TYPE MATCHES "RELEASE")
# set(CMAKE_EXE_LINKER_FLAGS "-s")
# endif()

#-----------------------------------------------------------------------------
# SOURCES
Expand Down Expand Up @@ -101,23 +119,29 @@ if(APPLE)
endif(APPLE)

# create your executable with
add_executable(voidstar ${GUI_TYPE} ${SOURCES})
add_executable(${PROJECT_NAME} ${GUI_TYPE} ${SOURCES})

add_dependencies(voidstar shaders_out)
add_dependencies(${PROJECT_NAME} shaders_out)

# create a library with
# add_library(my_lib SHARED ${SOURCES})

# you can link both with
target_link_libraries(voidstar
m
${OPENGL_gl_LIBRARY}
${GLEW_LIBRARIES}
${GLFW_STATIC_LIBRARIES} # This needs manual addition of ${GLFW_STATIC_LDFLAGS}
${EXTRA_LIBS}
)
if (NOT EMSCRIPTEN)
target_link_libraries(${PROJECT_NAME}
m
${OPENGL_gl_LIBRARY}
${GLEW_LIBRARIES}
${GLFW_STATIC_LIBRARIES} # This needs manual addition of ${GLFW_STATIC_LDFLAGS}
${EXTRA_LIBS}
)
string(REPLACE ";" " " glfw_static_ldflags "${GLFW_STATIC_LDFLAGS}")
set_property(TARGET voidstar APPEND_STRING PROPERTY LINK_FLAGS "${glfw_static_ldflags}")
set_property(TARGET ${PROJECT_NAME} APPEND_STRING PROPERTY LINK_FLAGS "${glfw_static_ldflags}")
endif()

if(EMSCRIPTEN)
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX ".html")
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-s USE_GLFW=3 -s USE_WEBGL2=1 -s FULL_ES3=1 -s DISABLE_EXCEPTION_CATCHING=2 --preload-file data/rgb555.xraw -s ALLOW_MEMORY_GROWTH=1")
endif()

#-----------------------------------------------------------------------------
# PACKAGING
Expand All @@ -134,7 +158,7 @@ set_property(TARGET voidstar APPEND_STRING PROPERTY LINK_FLAGS "${glfw_static_ld
# OR
# install(PROGRAMS myexecutable DESTINATION <dir>)

install(TARGETS voidstar RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX})
install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX})

include(uninstall)
# add_subdirectory("${CMAKE_SOURCE_DIR}/cmake/cpack") # enable packaging with CPack
Expand Down
48 changes: 48 additions & 0 deletions emscDisplayMgr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once
//------------------------------------------------------------------------------
/**
@class Oryol::_priv::emscDisplayMgr
@ingroup _priv
@brief display manager class for emscripten platform
*/
#include "Gfx/private/displayMgrBase.h"
#include <emscripten/html5.h>

namespace Oryol {

// emscripten-specific soft-fullscreen control functions
extern "C" {
extern void enter_soft_fullscreen();
extern void leave_soft_fullscreen();
extern bool is_soft_fullscreen_active();
}

namespace _priv {

class emscDisplayMgr : public displayMgrBase {
public:
/// constructor
emscDisplayMgr();
/// destructor
~emscDisplayMgr();

/// setup the display system, must happen before rendering
void SetupDisplay(const GfxSetup& gfxSetup, const gfxPointers& ptrs);
/// discard the display, rendering cannot happen after
void DiscardDisplay();

/// bind the default frame buffer
void glBindDefaultFramebuffer();
/// emscripten callback when canvas size has changed (for soft-fullscreen)
static EM_BOOL emscCanvasSizeChanged(int eventType, const void* reserved, void* userData);
/// emscripten callback when window size has changed (for HTMLUseCanvasSize)
static EM_BOOL emscWindowSizeChanged(int eventType, const EmscriptenUiEvent* uiEvent, void* userData);

static emscDisplayMgr* self;
int storedCanvasWidth;
int storedCanvasHeight;
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx;
};

} // namespace _priv
} // namespace Oryol
73 changes: 73 additions & 0 deletions emscInputMgr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#pragma once
//------------------------------------------------------------------------------
/**
@class Oryol::_priv::emscInputMgr
@ingroup _priv
@brief provide input on emscripten platform
*/
#include "Input/private/inputMgrBase.h"
#include "Core/RunLoop.h"
#include <emscripten/html5.h>

namespace Oryol {
namespace _priv {

class emscInputMgr : public inputMgrBase {
public:
/// constructor
emscInputMgr();
/// destructor
~emscInputMgr();

/// setup the input manager
void setup(const InputSetup& setup);
/// discard the input manager
void discard();

/// setup game pad mappings for known gamepads
void setupGamepadMappings();
/// polling for the gamepad
void updateGamepads();
/// setup the key mapping table
void setupKeyTable();
/// setup emscripten input handling callbacks
void setupCallbacks();
/// detach emscripten callbacks
void discardCallbacks();
/// map HTML5 key code to Oryol keycode
Key::Code mapKey(unsigned long html5KeyCode) const;
/// map HTML5 mouse button code to Oryol mouse button
MouseButton::Code mapMouseButton(unsigned short html5Btn) const;

/// update mouse pointer lock state
static bool updatePointerLockMode(PointerLockMode::Code lockMode);
/// key down callback
static EM_BOOL emscKeyDown(int eventType, const EmscriptenKeyboardEvent* e, void* userData);
/// key up callback
static EM_BOOL emscKeyUp(int eventType, const EmscriptenKeyboardEvent* e, void* userData);
/// key press callback
static EM_BOOL emscKeyPress(int eventType, const EmscriptenKeyboardEvent* e, void* userData);
/// mouse-button-down callback
static EM_BOOL emscMouseDown(int eventType, const EmscriptenMouseEvent* e, void* userData);
/// mouse-button-up callback
static EM_BOOL emscMouseUp(int eventType, const EmscriptenMouseEvent* e, void* userData);
/// mouse-move callback
static EM_BOOL emscMouseMove(int eventType, const EmscriptenMouseEvent* e, void* userData);
/// wheel callback
static EM_BOOL emscWheel(int eventType, const EmscriptenWheelEvent* e, void* userData);
/// touch event callback (same callback for touchstart, touchmove, touchend, touchcancel)
static EM_BOOL emscTouch(int eventType, const EmscriptenTouchEvent* e, void* userData);
/// device motion callback
static EM_BOOL emscDeviceMotion(int eventType, const EmscriptenDeviceMotionEvent* e, void* userData);
/// device orientation callback
static EM_BOOL emscDeviceOrientation(int eventType, const EmscriptenDeviceOrientationEvent* e, void* userData);

static const int MaxNumKeys = 256;
RunLoop::Id runLoopId;
RunLoop::Id updateGamepadsRunLoopId;
bool pointerLockActive;
Key::Code keyTable[MaxNumKeys];
};

} // namespace _priv
} // namespace Oryol
2 changes: 2 additions & 0 deletions src/GlfwManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ GlfwManager::init() {
glfwSetKeyCallback(window_, onKeyEvent);

// GLFW settings
#ifndef __EMSCRIPTEN__
glfwSetInputMode(window_, GLFW_STICKY_KEYS, GL_TRUE);
#endif
glfwSetCursorPos(window_, 0, 0);
glfwSetScrollCallback(window_, onScroll);
glfwMakeContextCurrent(window_);
Expand Down
1 change: 1 addition & 0 deletions src/common/tdogl/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#pragma once

#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>


Expand Down
1 change: 1 addition & 0 deletions src/common/tdogl/Program.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "Shader.h"
#include <vector>
#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>

namespace tdogl {
Expand Down
15 changes: 9 additions & 6 deletions src/include/Arguments.hh
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,29 @@ struct Arguments {
manager("glfw"),
width(800), height(600),
range_begin(0), range_end(0),
fullscreen(false),
keep_chrome(false),
sliding_window_length(2*1024), sliding_step(1024), move_window(false),
spin_shape(false)
spin_shape(false),
fullscreen(false),
keep_chrome(false)
#ifdef __EMSCRIPTEN__
,input({"../data/rgb555.xraw"})
#endif
{}

std::vector<std::string> input;
std::string name;
std::string algo;
std::string manager;
size_t width;
size_t height;
size_t range_begin;
size_t range_end;
bool fullscreen;
bool keep_chrome;
size_t sliding_window_length;
size_t sliding_step;
bool move_window;
bool spin_shape;
bool fullscreen;
bool keep_chrome;
std::vector<std::string> input;
};

std::shared_ptr<Arguments> parseArgs(int argc, char *argv[]);
Loading