Platform web.

This commit is contained in:
jussi
2023-11-29 19:52:47 +02:00
parent 4ff1b1dcb9
commit 8882d2ff2c
9 changed files with 79 additions and 33 deletions

View File

@@ -13,7 +13,7 @@ set( CMAKE_C_STANDARD 99 ) # Requires C99 standard
option( SHARED "Build using dynamic libraries." off ) option( SHARED "Build using dynamic libraries." off )
option( LUAJIT "Use LuaJIT." off ) option( LUAJIT "Use LuaJIT." off )
enum_option( PLATFORM "Desktop;Desktop_SDL" "Platform to build for." ) enum_option( PLATFORM "Desktop;Desktop_SDL;Web" "Platform to build for." )
if( NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES ) if( NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES )
set( CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE ) set( CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE )
@@ -29,16 +29,19 @@ if( PLATFORM STREQUAL "Desktop" )
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DPLATFORM_DESKTOP" ) set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DPLATFORM_DESKTOP" )
elseif( PLATFORM STREQUAL "Desktop_SDL" ) elseif( PLATFORM STREQUAL "Desktop_SDL" )
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DPLATFORM_DESKTOP_SDL" ) set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DPLATFORM_DESKTOP_SDL" )
elseif( PLATFORM STREQUAL "Web" )
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DPLATFORM_WEB" )
endif() endif()
if( EMSCRIPTEN ) # Web if( PLATFORM STREQUAL "Web" )
target_link_libraries( ${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/web/libraylib.a ) target_link_libraries( ${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/web/libraylib.a )
target_link_libraries( ${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/web/liblua.a ) target_link_libraries( ${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/web/liblua.a )
# Try "-s USE_PTHREADS" if not getting pixel perfect rendering. # Try "-s USE_PTHREADS" if not getting pixel perfect rendering.
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY" ) set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY" )
# set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY -s FORCE_FILESYSTEM=1" )
set( CMAKE_EXECUTABLE_SUFFIX ".html" ) # This line is used to set your executable to build with the emscripten html template so that you can directly open it. set( CMAKE_EXECUTABLE_SUFFIX ".html" ) # This line is used to set your executable to build with the emscripten html template so that you can directly open it.
set( resources_dir "resources" ) set( resources_dir "resources@/" ) # Sets resources as root for the virtual file system.
set_target_properties( ${PROJECT_NAME} PROPERTIES LINK_FLAGS "--preload-file ${resources_dir}" ) set_target_properties( ${PROJECT_NAME} PROPERTIES LINK_FLAGS "--preload-file ${resources_dir}" )
else() # Desktop else() # Desktop
if( SHARED ) if( SHARED )

View File

@@ -15,6 +15,7 @@ KEY CHANGES:
- ADDED: SDL Events. - ADDED: SDL Events.
- ADDED: Experimental GLFW Pen Touch events. Needs glfw PR https://github.com/glfw/glfw/pull/1445. - ADDED: Experimental GLFW Pen Touch events. Needs glfw PR https://github.com/glfw/glfw/pull/1445.
- ADDED: Platform specific API documentation generation. - ADDED: Platform specific API documentation generation.
- ADDED: Platform web.
DETAILED CHANGES: DETAILED CHANGES:
- REMOVED: DrawLineBezierQuad, DrawLineBezierCubic. - REMOVED: DrawLineBezierQuad, DrawLineBezierCubic.

View File

@@ -31,6 +31,8 @@ void luaCallProcess();
void luaCallDraw(); void luaCallDraw();
void luaCallExit(); void luaCallExit();
void luaRegister(); void luaRegister();
void platformDefineGlobals();
void luaPlatformRegister();
/* Lua get types. */ /* Lua get types. */
bool uluaGetBoolean( lua_State *L, int index ); bool uluaGetBoolean( lua_State *L, int index );
Color uluaGetColor( lua_State *L, int index ); Color uluaGetColor( lua_State *L, int index );

View File

@@ -14,8 +14,8 @@ static int getBufferElementSize( Buffer *buffer ) {
case BUFFER_INT: return sizeof( int ); case BUFFER_INT: return sizeof( int );
case BUFFER_FLOAT: return sizeof( float ); case BUFFER_FLOAT: return sizeof( float );
case BUFFER_DOUBLE: return sizeof( double ); case BUFFER_DOUBLE: return sizeof( double );
default: 1;
} }
return 1;
} }
void unloadBuffer( Buffer *buffer ) { void unloadBuffer( Buffer *buffer ) {

View File

@@ -15,6 +15,7 @@ Copy a block of pixels from one framebuffer object to another.
Use -1 RenderTexture for window framebuffer Use -1 RenderTexture for window framebuffer
*/ */
int lglBlitFramebuffer( lua_State *L ) { int lglBlitFramebuffer( lua_State *L ) {
#if defined( PLATFORM_DESKTOP ) || defined( PLATFORM_DESKTOP_SDL )
if ( !( lua_isuserdata( L, 1 ) || lua_isnil( L, 1 ) ) || !( lua_isuserdata( L, 2 ) || lua_isnil( L, 2 ) ) ) { if ( !( lua_isuserdata( L, 1 ) || lua_isnil( L, 1 ) ) || !( lua_isuserdata( L, 2 ) || lua_isnil( L, 2 ) ) ) {
TraceLog( state->logLevelInvalid, "%s", "Argument needs to be RenderTexture or nil" ); TraceLog( state->logLevelInvalid, "%s", "Argument needs to be RenderTexture or nil" );
lua_pushnil( L ); lua_pushnil( L );
@@ -52,4 +53,5 @@ int lglBlitFramebuffer( lua_State *L ) {
glBindFramebuffer( GL_DRAW_FRAMEBUFFER, 0 ); glBindFramebuffer( GL_DRAW_FRAMEBUFFER, 0 );
return 1; return 1;
#endif
} }

View File

@@ -18,6 +18,8 @@
#include "platforms/core_desktop.c" #include "platforms/core_desktop.c"
#elif PLATFORM_DESKTOP_SDL #elif PLATFORM_DESKTOP_SDL
#include "platforms/core_desktop_sdl.c" #include "platforms/core_desktop_sdl.c"
#elif PLATFORM_WEB
#include "platforms/core_web.c"
#endif #endif
/* Define types. */ /* Define types. */
@@ -28,6 +30,7 @@ static int gcBuffer( lua_State *L ) {
Buffer *buffer = luaL_checkudata( L, 1, "Buffer" ); Buffer *buffer = luaL_checkudata( L, 1, "Buffer" );
unloadBuffer( buffer ); unloadBuffer( buffer );
} }
return 0;
} }
static void defineBuffer() { static void defineBuffer() {
@@ -46,6 +49,7 @@ static int gcImage( lua_State *L ) {
Image *image = luaL_checkudata( L, 1, "Image" ); Image *image = luaL_checkudata( L, 1, "Image" );
UnloadImage( *image ); UnloadImage( *image );
} }
return 0;
} }
static void defineImage() { static void defineImage() {
@@ -64,6 +68,7 @@ static int gcTexture( lua_State *L ) {
Texture *texture = luaL_checkudata( L, 1, "Texture" ); Texture *texture = luaL_checkudata( L, 1, "Texture" );
UnloadTexture( *texture ); UnloadTexture( *texture );
} }
return 0;
} }
static void defineTexture() { static void defineTexture() {
@@ -83,6 +88,7 @@ static int gcRenderTexture( lua_State *L ) {
RenderTexture *renderTexture = luaL_checkudata( L, 1, "RenderTexture" ); RenderTexture *renderTexture = luaL_checkudata( L, 1, "RenderTexture" );
UnloadRenderTexture( *renderTexture ); UnloadRenderTexture( *renderTexture );
} }
return 0;
} }
static void defineRenderTexture() { static void defineRenderTexture() {
@@ -119,6 +125,7 @@ static int gcShader( lua_State *L ) {
Shader *shader = luaL_checkudata( L, 1, "Shader" ); Shader *shader = luaL_checkudata( L, 1, "Shader" );
UnloadShader( *shader ); UnloadShader( *shader );
} }
return 0;
} }
static void defineShader() { static void defineShader() {
@@ -137,6 +144,7 @@ static int gcFont( lua_State *L ) {
Font *font = luaL_checkudata( L, 1, "Font" ); Font *font = luaL_checkudata( L, 1, "Font" );
UnloadFont( *font ); UnloadFont( *font );
} }
return 0;
} }
static void defineFont() { static void defineFont() {
@@ -155,6 +163,7 @@ static int gcWave( lua_State *L ) {
Wave *wave = luaL_checkudata( L, 1, "Wave" ); Wave *wave = luaL_checkudata( L, 1, "Wave" );
UnloadWave( *wave ); UnloadWave( *wave );
} }
return 0;
} }
static void defineWave() { static void defineWave() {
@@ -173,6 +182,7 @@ static int gcSound( lua_State *L ) {
Sound *sound = luaL_checkudata( L, 1, "Sound" ); Sound *sound = luaL_checkudata( L, 1, "Sound" );
UnloadSound( *sound ); UnloadSound( *sound );
} }
return 0;
} }
static void defineSound() { static void defineSound() {
@@ -191,6 +201,7 @@ static int gcMusic( lua_State *L ) {
Music *music = luaL_checkudata( L, 1, "Music" ); Music *music = luaL_checkudata( L, 1, "Music" );
UnloadMusicStream( *music ); UnloadMusicStream( *music );
} }
return 0;
} }
static void defineMusic() { static void defineMusic() {
@@ -219,6 +230,7 @@ static int gcMaterial( lua_State *L ) {
/* Custom UnloadMaterial since we don't want to free Shaders or Textures. */ /* Custom UnloadMaterial since we don't want to free Shaders or Textures. */
unloadMaterial( material ); unloadMaterial( material );
} }
return 0;
} }
static void defineMaterial() { static void defineMaterial() {
@@ -237,6 +249,7 @@ static int gcMesh( lua_State *L ) {
Mesh *mesh = luaL_checkudata( L, 1, "Mesh" ); Mesh *mesh = luaL_checkudata( L, 1, "Mesh" );
UnloadMesh( *mesh ); UnloadMesh( *mesh );
} }
return 0;
} }
static void defineMesh() { static void defineMesh() {
@@ -256,6 +269,7 @@ static int gcModel( lua_State *L ) {
UnloadModel( *model ); UnloadModel( *model );
// UnloadModelKeepMeshes( *model ); // UnloadModelKeepMeshes( *model );
} }
return 0;
} }
static void defineModel() { static void defineModel() {
@@ -274,6 +288,7 @@ static int gcModelAnimation( lua_State *L ) {
ModelAnimation *modelAnimation = luaL_checkudata( L, 1, "ModelAnimation" ); ModelAnimation *modelAnimation = luaL_checkudata( L, 1, "ModelAnimation" );
UnloadModelAnimation( *modelAnimation ); UnloadModelAnimation( *modelAnimation );
} }
return 0;
} }
static void defineModelAnimation() { static void defineModelAnimation() {
@@ -1030,11 +1045,11 @@ bool luaCallMain() {
char path[ STRING_LEN ] = { '\0' }; char path[ STRING_LEN ] = { '\0' };
/* If web, set path to resources folder. */ /* If web, set path to resources folder. */
#ifdef EMSCRIPTEN #ifdef PLATFORM_WEB
snprintf( path, STRING_LEN, "resources/main.lua" ); snprintf( path, STRING_LEN, "main.lua" );
/* Alternatively look for main. Could be precompiled binary file. */ /* Alternatively look for main. Could be precompiled binary file. */
if ( !FileExists( path ) ) { if ( !FileExists( path ) ) {
snprintf( path, STRING_LEN, "resources/main" ); snprintf( path, STRING_LEN, "main" );
} }
#else #else
snprintf( path, STRING_LEN, "%smain.lua", state->exePath ); snprintf( path, STRING_LEN, "%smain.lua", state->exePath );
@@ -1055,8 +1070,6 @@ bool luaCallMain() {
/* Apply custom callback here. */ /* Apply custom callback here. */
SetTraceLogCallback( logCustom ); SetTraceLogCallback( logCustom );
platformRegisterEvents();
lua_getglobal( L, "RL" ); lua_getglobal( L, "RL" );
lua_getfield( L, -1, "init" ); lua_getfield( L, -1, "init" );
@@ -1077,6 +1090,7 @@ bool luaCallMain() {
} }
void luaCallProcess() { void luaCallProcess() {
#ifdef PLATFORM_DESKTOP_SDL #ifdef PLATFORM_DESKTOP_SDL
platformSendEvents(); platformSendEvents();
#endif #endif

View File

@@ -3,7 +3,7 @@
#include "core.h" #include "core.h"
#include "platforms/core_desktop.h" #include "platforms/core_desktop.h"
static void platformDefineGlobals() { void platformDefineGlobals() {
lua_State *L = state->luaState; lua_State *L = state->luaState;
lua_getglobal( L, "RL" ); lua_getglobal( L, "RL" );
@@ -87,17 +87,6 @@ int lcoreGetKeyScancode( lua_State *L ) {
return 1; return 1;
} }
static void luaPlatformRegister() {
lua_State *L = state->luaState;
lua_getglobal( L, "RL" );
/* Input-related functions: keyboard. */
assingGlobalFunction( "GetKeyName", lcoreGetKeyName );
assingGlobalFunction( "GetKeyScancode", lcoreGetKeyScancode );
lua_pop( L, -1 );
}
/* Events. */ /* Events. */
/* /*
@@ -649,3 +638,16 @@ static void platformRegisterEvents() {
// state->glfwtabletCursorCallback = glfwSetPenTabletCursorCallback( penTabletCursorEvent ); // state->glfwtabletCursorCallback = glfwSetPenTabletCursorCallback( penTabletCursorEvent );
// state->glfwtabletProximityCallback = glfwSetPenTabletProximityCallback( penTabletProximityEvent ); // state->glfwtabletProximityCallback = glfwSetPenTabletProximityCallback( penTabletProximityEvent );
} }
void luaPlatformRegister() {
lua_State *L = state->luaState;
lua_getglobal( L, "RL" );
/* Input-related functions: keyboard. */
assingGlobalFunction( "GetKeyName", lcoreGetKeyName );
assingGlobalFunction( "GetKeyScancode", lcoreGetKeyScancode );
lua_pop( L, -1 );
platformRegisterEvents();
}

View File

@@ -3,7 +3,7 @@
#include "core.h" #include "core.h"
#include "platforms/core_desktop_sdl.h" #include "platforms/core_desktop_sdl.h"
static void platformDefineGlobals() { void platformDefineGlobals() {
lua_State *L = state->luaState; lua_State *L = state->luaState;
lua_getglobal( L, "RL" ); lua_getglobal( L, "RL" );
@@ -111,17 +111,6 @@ int lcoreGetScancodeFromKey( lua_State *L ) {
return 1; return 1;
} }
static void luaPlatformRegister() {
lua_State *L = state->luaState;
lua_getglobal( L, "RL" );
/* Input-related functions: keyboard. */
assingGlobalFunction( "GetKeyName", lcoreGetKeyName );
assingGlobalFunction( "GetScancodeFromKey", lcoreGetScancodeFromKey );
lua_pop( L, -1 );
}
/* Events. */ /* Events. */
/* /*
@@ -555,3 +544,16 @@ Event occurs an event of type SDL_DOLLARGESTURE or SDL_DOLLARRECORD is reported.
} }
state->SDL_eventQueueLen = 0; state->SDL_eventQueueLen = 0;
} }
void luaPlatformRegister() {
lua_State *L = state->luaState;
lua_getglobal( L, "RL" );
/* Input-related functions: keyboard. */
assingGlobalFunction( "GetKeyName", lcoreGetKeyName );
assingGlobalFunction( "GetScancodeFromKey", lcoreGetScancodeFromKey );
lua_pop( L, -1 );
platformRegisterEvents()
}

20
src/platforms/core_web.c Normal file
View File

@@ -0,0 +1,20 @@
#include "main.h"
#include "lua_core.h"
#include "core.h"
void platformDefineGlobals() {
lua_State *L = state->luaState;
lua_getglobal( L, "RL" );
/*DOC_DEFINES_START*/
/*DOC_DEFINES_END*/
lua_pop( L, -1 );
}
/* Functions. */
/* Events. */
void luaPlatformRegister() {
return;
}