Web build.

This commit is contained in:
jussi
2022-02-22 02:49:15 +02:00
parent 612ede6da4
commit 8800de59fa
6 changed files with 120 additions and 49 deletions

View File

@@ -1,23 +1,30 @@
cmake_minimum_required( VERSION 3.9 ) cmake_minimum_required( VERSION 3.9 )
project( ReiLua ) project( ReiLua )
# To make web build
# cmake .. -DCMAKE_TOOLCHAIN_FILE=<YOUR PATH HERE>/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake -DPLATFORM=Web
set( CMAKE_C_STANDARD 99 ) # Requires C99 standard set( CMAKE_C_STANDARD 99 ) # Requires C99 standard
if( UNIX ) if( DEBUG )
set( CMAKE_C_COMPILER "gcc" ) set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -ggdb -std=c99 -Wall -pedantic -fno-common" )
elseif( APPLE ) else()
set( CMAKE_C_COMPILER "clang" )
# elseif( WIN32 )
# set( CMAKE_C_COMPILER "mingw32" )
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" ) set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -std=c99 -Wall -pedantic -fno-common" )
endif()
include_directories( include ) include_directories( include )
file( GLOB SOURCES "src/*.c" ) file( GLOB SOURCES "src/*.c" )
add_executable( ${PROJECT_NAME} ${SOURCES} ) add_executable( ${PROJECT_NAME} ${SOURCES} )
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 ) if( SHARED )
message( Shared ) message( Shared )
find_package( raylib 4.0 REQUIRED ) # Requires at least version 4.0 find_package( raylib 4.0 REQUIRED ) # Requires at least version 4.0
@@ -27,25 +34,24 @@ else()
message( Static ) message( Static )
target_link_libraries( ${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/libraylib.a ) 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/liblua.a )
# target_link_libraries( ${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/libluajit.a )
endif() endif()
if( UNIX ) if( UNIX )
set( CMAKE_C_COMPILER "gcc" )
if( DRM ) # Mainly for Raspberry pi 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 bcm_host m dl pthread )
target_link_libraries( ${PROJECT_NAME} GLESv2 EGL drm gbm rt m dl pthread ) target_link_libraries( ${PROJECT_NAME} GLESv2 EGL drm gbm rt m dl pthread )
else() else()
target_link_libraries( ${PROJECT_NAME} m dl pthread ) target_link_libraries( ${PROJECT_NAME} m dl pthread )
endif() endif()
endif() elseif( APPLE )
set( CMAKE_C_COMPILER "clang" )
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 IOKit" )
target_link_libraries( ${PROJECT_NAME} "-framework Cocoa" ) target_link_libraries( ${PROJECT_NAME} "-framework Cocoa" )
target_link_libraries( ${PROJECT_NAME} "-framework OpenGL" ) target_link_libraries( ${PROJECT_NAME} "-framework OpenGL" )
elseif( WIN32 )
target_link_libraries( ${PROJECT_NAME} mingw32 opengl32 gdi32 winmm )
endif()
endif() endif()

View File

@@ -10,7 +10,7 @@ Reilua means fair in finnish.
## Status ## 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 ## Usage
@@ -136,3 +136,65 @@ Not tested, but I guess it should work similarly to Linux.
### Raspberry Pi ### Raspberry Pi
Works best when compiled using PLATFORM=DRM, but Raylib seems to have some problems with the input handling. 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 \<YOUR PATH HERE> with correct path.
```
cmake .. -DCMAKE_TOOLCHAIN_FILE=<YOUR PATH HERE>/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
```

5
build/.gitignore vendored
View File

@@ -3,3 +3,8 @@ cmake_install.cmake
Makefile Makefile
CMakeFiles CMakeFiles
ReiLua ReiLua
resources
ReiLua.data
ReiLua.html
ReiLua.js
ReiLua.wasm

View File

@@ -1,6 +1,4 @@
Backlog { Backlog {
* Compilation
* Web
* More and better examples * More and better examples
* Raygui * Raygui
@@ -13,9 +11,9 @@ Backlog {
* Fonts * Fonts
* More draw functions * More draw functions
* Codepoints * Codepoints
* String management. At least TextSplit * String management. At least TextSplit.
* Audio * Audio
* Wave? * Wave
* Gestures * Gestures
* Raymath * Raymath
* Quaternions * Quaternions

View File

@@ -3,12 +3,9 @@
#define STRING_LEN 1024 #define STRING_LEN 1024
#define VERSION_MAJOR 0 #define VERSION_MAJOR 0
#define VERSION_MINOR 1 #define VERSION_MINOR 2
#define VERSION_PATCH 0 #define VERSION_PATCH 0
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -18,5 +15,4 @@
#include <lua.h> #include <lua.h>
#include <lualib.h> #include <lualib.h>
#include <lauxlib.h> #include <lauxlib.h>
#include <stdint.h>
// #include <stdint.h>

View File

@@ -213,12 +213,16 @@ bool luaCallMain() {
char path[ STRING_LEN ] = { '\0' }; 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 ); sprintf( path, "%smain.lua", state->exePath );
/* Alternatively look for main. Could be precompiled binary file. */ /* Alternatively look for main. Could be precompiled binary file. */
if ( !FileExists( path ) ) { if ( !FileExists( path ) ) {
sprintf( path, "%smain", state->exePath ); sprintf( path, "%smain", state->exePath );
} }
#endif
luaL_dofile( L, path ); luaL_dofile( L, path );