From 8800de59fa704cfe60ac980db079b44042639684 Mon Sep 17 00:00:00 2001 From: jussi Date: Tue, 22 Feb 2022 02:49:15 +0200 Subject: Web build. --- CMakeLists.txt | 78 ++++++++++++++++++++++++++++++-------------------------- README.md | 64 +++++++++++++++++++++++++++++++++++++++++++++- build/.gitignore | 7 ++++- devnotes | 6 ++--- include/main.h | 8 ++---- src/lua_core.c | 6 ++++- 6 files changed, 120 insertions(+), 49 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c0645bf..be2d7f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,51 +1,57 @@ cmake_minimum_required( VERSION 3.9 ) project( ReiLua ) +# To make web build +# cmake .. -DCMAKE_TOOLCHAIN_FILE=/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake -DPLATFORM=Web + set( CMAKE_C_STANDARD 99 ) # Requires C99 standard -if( UNIX ) - set( CMAKE_C_COMPILER "gcc" ) -elseif( APPLE ) - set( CMAKE_C_COMPILER "clang" ) -# elseif( WIN32 ) -# set( CMAKE_C_COMPILER "mingw32" ) +if( DEBUG ) + set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -ggdb -std=c99 -Wall -pedantic -fno-common" ) +else() + set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -std=c99 -Wall -pedantic -fno-common" ) endif() -# set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -ggdb -std=c99 -Wall -pedantic -fno-common" ) -set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -std=c99 -Wall -pedantic -fno-common" ) - include_directories( include ) file( GLOB SOURCES "src/*.c" ) add_executable( ${PROJECT_NAME} ${SOURCES} ) -if( SHARED ) - message( Shared ) - find_package( raylib 4.0 REQUIRED ) # Requires at least version 4.0 - target_link_libraries( ${PROJECT_NAME} raylib ) - target_link_libraries( ${PROJECT_NAME} lua ) -else() - message( Static ) - target_link_libraries( ${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/libraylib.a ) - target_link_libraries( ${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/liblua.a ) - # target_link_libraries( ${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/libluajit.a ) -endif() - -if( UNIX ) - if( DRM ) # Mainly for Raspberry pi - # target_link_libraries( ${PROJECT_NAME} GLESv2 EGL drm gbm rt bcm_host m dl pthread ) - target_link_libraries( ${PROJECT_NAME} GLESv2 EGL drm gbm rt m dl pthread ) +if( EMSCRIPTEN ) # Web + target_link_libraries( ${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/web/libraylib.a ) + target_link_libraries( ${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/web/liblua.a ) + + set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY" ) + set( CMAKE_EXECUTABLE_SUFFIX ".html" ) # This line is used to set your executable to build with the emscripten html template so taht you can directly open it. + set( resources_dir "resources" ) + set_target_properties( ${PROJECT_NAME} PROPERTIES LINK_FLAGS "--preload-file ${resources_dir}" ) +else() # Desktop + if( SHARED ) + message( Shared ) + find_package( raylib 4.0 REQUIRED ) # Requires at least version 4.0 + target_link_libraries( ${PROJECT_NAME} raylib ) + target_link_libraries( ${PROJECT_NAME} lua ) else() - target_link_libraries( ${PROJECT_NAME} m dl pthread ) + message( Static ) + target_link_libraries( ${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/libraylib.a ) + target_link_libraries( ${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/liblua.a ) endif() -endif() -if( WIN32 ) - target_link_libraries( ${PROJECT_NAME} mingw32 opengl32 gdi32 winmm ) -endif() - -# Checks if OSX and links appropriate frameworks (Only required on MacOS) -if( APPLE ) - target_link_libraries( ${PROJECT_NAME} "-framework IOKit" ) - target_link_libraries( ${PROJECT_NAME} "-framework Cocoa" ) - target_link_libraries( ${PROJECT_NAME} "-framework OpenGL" ) + if( UNIX ) + set( CMAKE_C_COMPILER "gcc" ) + + if( DRM ) # Mainly for Raspberry pi + # target_link_libraries( ${PROJECT_NAME} GLESv2 EGL drm gbm rt bcm_host m dl pthread ) + target_link_libraries( ${PROJECT_NAME} GLESv2 EGL drm gbm rt m dl pthread ) + else() + target_link_libraries( ${PROJECT_NAME} m dl pthread ) + endif() + elseif( APPLE ) + set( CMAKE_C_COMPILER "clang" ) + + target_link_libraries( ${PROJECT_NAME} "-framework IOKit" ) + target_link_libraries( ${PROJECT_NAME} "-framework Cocoa" ) + target_link_libraries( ${PROJECT_NAME} "-framework OpenGL" ) + elseif( WIN32 ) + target_link_libraries( ${PROJECT_NAME} mingw32 opengl32 gdi32 winmm ) + endif() endif() diff --git a/README.md b/README.md index 585174f..950872e 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Reilua means fair in finnish. ## Status -ReiLua is currently in arbitrary version 0.1 and some planned raylib functionality is still missing. Also the build system is still lacking of Web build and Mac is not tested. +ReiLua is currently in arbitrary version 0.1 and some planned raylib functionality is still missing. ## Usage @@ -136,3 +136,65 @@ Not tested, but I guess it should work similarly to Linux. ### Raspberry Pi Works best when compiled using PLATFORM=DRM, but Raylib seems to have some problems with the input handling. + +### Web + +Compile Raylib for web by following it's instructions. https://github.com/raysan5/raylib/wiki/Working-for-Web-(HTML5)#1-install-emscripten-toolchain + +Lua can be compiled by making few changes to it's makefile. +``` +MYCFLAGS= $(LOCAL) -std=c99 -DLUA_USE_LINUX -DLUA_USE_READLINE +# to +MYCFLAGS= $(LOCAL) -std=c99 + +MYLIBS= -ldl -lreadline +# to +MYLIBS= -ldl + +CC= gcc +# to +CC= emcc + +AR= ar rc +# to +AR= emar + +# And a little bit more down. + $(AR) $@ $? +# to + $(AR) rcs $@ $? +``` + +* If your enviromental variables for "emsdk" are correct, you should be able to compile with make. +* You should now have "libraylib.a" and "liblua.a" librarys. +* Put them into "ReiLua/lib/web/". +* Navigate into "ReiLua/build/". + +Emscripten builds the resources like lua files and images to "ReiLua.data" file so we will create folder called "resources" that should include all that. "resources" should also be included in all resource paths. "main.lua" should be located in the root of that folder. So we should now have. + +``` +ReiLua + \build + |\resources + ||\main.lua +``` + +We can now build the game. You can use the command it top of the "CMakeLists.txt" to use emsdk toolchain with cmake. Remember to replace \ with correct path. + +``` +cmake .. -DCMAKE_TOOLCHAIN_FILE=/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake -DPLATFORM=Web + +make +``` + +You should now have "ReiLua.html", "ReiLua.js", "ReiLua.wasm" and "ReiLua.data". You can test the game by creating localhost with Python. + +``` +python -m http.server 8080 +``` + +You should now be able to access the webpage from browser. + +``` +localhost:8080/ReiLua.html +``` diff --git a/build/.gitignore b/build/.gitignore index f954020..7a0b42a 100644 --- a/build/.gitignore +++ b/build/.gitignore @@ -2,4 +2,9 @@ CMakeCache.txt cmake_install.cmake Makefile CMakeFiles -ReiLua \ No newline at end of file +ReiLua +resources +ReiLua.data +ReiLua.html +ReiLua.js +ReiLua.wasm \ No newline at end of file diff --git a/devnotes b/devnotes index 9d4b6cc..3aa880a 100644 --- a/devnotes +++ b/devnotes @@ -1,6 +1,4 @@ Backlog { - * Compilation - * Web * More and better examples * Raygui @@ -13,9 +11,9 @@ Backlog { * Fonts * More draw functions * Codepoints - * String management. At least TextSplit + * String management. At least TextSplit. * Audio - * Wave? + * Wave * Gestures * Raymath * Quaternions diff --git a/include/main.h b/include/main.h index def4e15..dd359ee 100644 --- a/include/main.h +++ b/include/main.h @@ -3,12 +3,9 @@ #define STRING_LEN 1024 #define VERSION_MAJOR 0 -#define VERSION_MINOR 1 +#define VERSION_MINOR 2 #define VERSION_PATCH 0 -#include -#include - #include #include #include @@ -18,5 +15,4 @@ #include #include #include - -// #include +#include diff --git a/src/lua_core.c b/src/lua_core.c index 1ee21de..54be454 100644 --- a/src/lua_core.c +++ b/src/lua_core.c @@ -213,12 +213,16 @@ bool luaCallMain() { char path[ STRING_LEN ] = { '\0' }; +/* If web, set path to resources folder. */ +#ifdef EMSCRIPTEN + sprintf( path, "resources/main.lua" ); +#else sprintf( path, "%smain.lua", state->exePath ); - /* Alternatively look for main. Could be precompiled binary file. */ if ( !FileExists( path ) ) { sprintf( path, "%smain", state->exePath ); } +#endif luaL_dofile( L, path ); -- cgit v1.2.3