Documentation and better cmakelist.

This commit is contained in:
jussi
2022-02-20 20:37:39 +02:00
parent 6e4fdd3b3a
commit 8182a5f1b6
9 changed files with 229 additions and 39 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
.vscode
Toolchain-mingw32.cmake
releases

3
API.md
View File

@@ -2,8 +2,7 @@
## Usage ## Usage
Application needs 'main.lua' file as entry point. ReiLua executable will first look it from same directory Application needs 'main.lua' file as entry point. ReiLua executable will first look it from same directory. Alternatively, path to the folder where "main.lua" is located can be given as argument. There are three global Lua functions that the engine will call, 'init', 'process' and 'draw'.
or it's path can be given by argument. There are three global functions that the engine will call, 'init', 'process' and 'draw'.
--- ---
> function init() > function init()

View File

@@ -1,39 +1,36 @@
cmake_minimum_required( VERSION 3.15 ) cmake_minimum_required( VERSION 3.15 )
project( ReiLua ) project( ReiLua )
# find_package( raylib 3.7 REQUIRED ) # Requires at least version 3.7 set( CMAKE_C_STANDARD 99 ) # Requires C99 standard
set( CMAKE_C_STANDARD 11 ) # Requires C11 standard
if( UNIX ) if( UNIX )
set( CMAKE_C_COMPILER "gcc" ) set( CMAKE_C_COMPILER "gcc" )
elseif( APPLE ) elseif( APPLE )
set( CMAKE_C_COMPILER "clang" ) set( CMAKE_C_COMPILER "clang" )
# elseif( WIN32 )
# set( CMAKE_C_COMPILER "mingw32" )
endif() endif()
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -ggdb -std=c11 -Wall -pedantic -fno-common" ) # set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -ggdb -std=c99 -Wall -pedantic -fno-common" )
# set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -std=c11 -Wall -pedantic -fno-common" ) set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -std=c99 -Wall -pedantic -fno-common" )
option( STATIC ON )
option( DRM OFF )
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( STATIC ) 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 ) 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 )
else()
find_package( raylib 4.0 REQUIRED ) # Requires at least version 4.0
message( Shared )
target_link_libraries( ${PROJECT_NAME} raylib )
target_link_libraries( ${PROJECT_NAME} lua )
endif() endif()
if( UNIX ) if( UNIX )
if( DRM ) # 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()
@@ -41,6 +38,10 @@ if( UNIX )
endif() endif()
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) # Checks if OSX and links appropriate frameworks (Only required on MacOS)
if( APPLE ) if( APPLE )
target_link_libraries( ${PROJECT_NAME} "-framework IOKit" ) target_link_libraries( ${PROJECT_NAME} "-framework IOKit" )

133
README.md Normal file
View File

@@ -0,0 +1,133 @@
## About
Idea of this project was to bring the power and simplicity of raylib to easy beginner friendly language like Lua in a very straight forward manner. It is not a straight binding to Raylib, some functions are not included and some are added. The idea of pointing "main.lua" file and access functions "init", "process" and "draw" are borrowed from Löve game engine.
Need for boilerplate code is minimal and in true Lua fashion (in better and worse) you don't need to worry about types and typecasts since all Raylib types are just lua tables and object id's. Also what Lua cannot handle, the engine is simple enough to be fairly easily extended with new functionality or by using Lua C-libraries.
## Status
Engine is currently in arbitrary version 0.1 and some functionality is still missing. Also the build system is still lacking of Web build and Mac is not tested.
## Usage
Application needs 'main.lua' file as entry point. ReiLua executable will first look it from same directory. Alternatively, path to the folder where "main.lua" is located can be given as argument. There are three global Lua functions that the engine will call, 'init', 'process' and 'draw'.
Example of basic "main.lua" file that will show basic windows with text.
```
local textColor = BLACK
local textPos = { 192, 200 }
function init()
RL_SetWindowTitle( "First window" )
end
function process( delta )
if RL_IsKeyPressed( KEY_ENTER ) then
textColor = BLUE
textPos = { 230, 230 }
end
if RL_IsKeyPressed( KEY_SPACE ) then
textColor = BLACK
textPos = { 192, 200 }
end
end
function draw()
RL_ClearBackground( RAYWHITE );
RL_DrawText( 0, "Congrats! You created your first window!", textPos, 20, 2, textColor );
end
```
Application folder structure should be...
```
GameFolder
\ReiLua.exe
\main.lua
```
Application should now start successfully from executable. All functionality can be found in "API".
## Building
I think the simplest way would be to statically link Raylib and Lua to the same executable. Specially on Linux this would simplify distribution of games since different distros tend to use different versions of librarys. Of course if you plan to only experiment with it, this isn't so important. At the time of writing ReiLua uses latest Raylib version from master branch. If you use Raylib v4.0.0 release, you will get a lot of undefined reference errors.
//TODO In future these instructions should be set on fixed release versions of Raylib.
https://github.com/raysan5/raylib
https://github.com/lua/lua
### Linux
Compile Raylib and lua by following their instructions. They will compile to libraylib.a and liblua.a by default.
You need build essential and cmake. If you compiled Raylib you should already have these.
```
sudo apt install build-essential
sudo apt install cmake
```
If compiling statically, move libraylib.a and liblua.a to "ReiLua/lib" folder. From "ReiLua" folder...
```
cd build
cmake ..
make
```
Run example.
```
./ReiLua ../examples/snake/
```
If you now see extremely low res snake racing off the window then you are successfull. Congratulations! You can reset the game by pressing enter.
### Windows
I don't have much experience on compiling on Windows, but I got the following method to work.
* Download "w64devkit" from https://github.com/skeeto/w64devkit and "CMake" from https://cmake.org/download/. Install CMake with path environment variables set.
* Download Raylib source.
* Run "w64devkit.exe" and navigate( ls == dir ) to "raylib-master/src" folder and run...
```
mingw32-make
```
* You should now have "libraylib.a" file in that folder.
* Copy that to "ReiLua/lib" folder.
* I haven't got Lua to compile on Windows so we will download it's binarys from http://luabinaries.sourceforge.net/download.html. Take the one with "Windows x64 DLL and Includes (MingW-w64 6 Built)".
* Copy "liblua54.a" to "ReiLua/lib" folder.
* Change it's name to "liblua.a" or change the part in "CMakeLists.txt" where it links to "/lib/liblua.a" to "/lib/liblua54.a".
* Navigate to "ReiLua/build" folder with "w64devkit" and run...
```
cmake -G "MinGW Makefiles" ..
# Cmake uses NMake Makefiles by default so we will set the Generator to MinGW with -G
mingw32-make
```
* You should now have "ReiLua.exe".
* From Lua folder, copy "lua54.dll" to same folder with "ReiLua.exe". Don't change the name of it!
Run example.
```
./ReiLua.exe ../examples/snake/
```
If you now see extremely low res snake racing off the window then you are successfull. Congratulations! You can reset the game by pressing enter.
### MacOS
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.

View File

@@ -1,8 +1,9 @@
Backlog { Backlog {
* Compilation * Compilation
* Windows * Windows
Mingw cross compilation causes name clashes with raylib https://github.com/raysan5/raylib/issues/1217
* Web * Web
* Could be better in general * Better CMakeList
* More and better examples * More and better examples
* Raygui * Raygui

View File

@@ -22,8 +22,7 @@ apiFile:write( "# ReiLua API\n" )
-- Usage. -- Usage.
apiFile:write( "\n## Usage\n" ) apiFile:write( "\n## Usage\n" )
apiFile:write( "\nApplication needs 'main.lua' file as entry point. ReiLua executable will first look it from same directory\ apiFile:write( "\nApplication needs 'main.lua' file as entry point. ReiLua executable will first look it from same directory. Alternatively, path to the folder where \"main.lua\" is located can be given as argument. There are three global Lua functions that the engine will call, 'init', 'process' and 'draw'.\n" )
or it's path can be given by argument. There are three global functions that the engine will call, 'init', 'process' and 'draw'.\n" )
apiFile:write( "\n---\n> function init()\n\ apiFile:write( "\n---\n> function init()\n\
This function will be called first when 'main.lua' is found\n\n---\n" ) This function will be called first when 'main.lua' is found\n\n---\n" )

23
examples/window/main.lua Normal file
View File

@@ -0,0 +1,23 @@
local textColor = BLACK
local textPos = { 192, 200 }
function init()
RL_SetWindowTitle( "First window" )
end
function process( delta )
if RL_IsKeyPressed( KEY_ENTER ) then
textColor = BLUE
textPos = { 230, 230 }
end
if RL_IsKeyPressed( KEY_SPACE ) then
textColor = BLACK
textPos = { 192, 200 }
end
end
function draw()
RL_ClearBackground( RAYWHITE );
RL_DrawText( 0, "Congrats! You created your first window!", textPos, 20, 2, textColor );
end

View File

@@ -5,6 +5,24 @@
#define VERSION_MAJOR 0 #define VERSION_MAJOR 0
#define VERSION_MINOR 1 #define VERSION_MINOR 1
// #define WIN32_LEAN_AND_MEAN
// #if defined(_WIN32)
// #define WIN32
// #endif
// #if defined(_WIN64)
// #define WIN64
// #define _AMD64_
// #undef _X86_
// #else
// #undef _AMD64_
// #define _X86_
// #endif
// #include <minwindef.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -14,3 +32,13 @@
#include <lua.h> #include <lua.h>
#include <lualib.h> #include <lualib.h>
#include <lauxlib.h> #include <lauxlib.h>
#include <stdint.h>
// #ifdef _WIN32
// // #include <windows.h>
// #include <windef.h>
// #include <mmsystem.h>
// // #include <winbase.h>
// // #define APIENTRY WINAPI
// #endif

View File

@@ -889,14 +889,15 @@ int lmathMatrixFrustum( lua_State *L ) {
lua_pushboolean( L, false ); lua_pushboolean( L, false );
return 1; return 1;
} }
float far = lua_tonumber( L, -1 ); // float far = lua_tonumber( L, -1 );
float near = lua_tonumber( L, -2 ); // float near = lua_tonumber( L, -2 );
float top = lua_tonumber( L, -3 ); // float top = lua_tonumber( L, -3 );
float bottom = lua_tonumber( L, -4 ); // float bottom = lua_tonumber( L, -4 );
float right = lua_tonumber( L, -5 ); // float right = lua_tonumber( L, -5 );
float left = lua_tonumber( L, -6 ); // float left = lua_tonumber( L, -6 );
uluaPushMatrix( L, MatrixFrustum( left, right, bottom, top, near, far ) ); // uluaPushMatrix( L, MatrixFrustum( left, right, bottom, top, near, far ) );
uluaPushMatrix( L, MatrixFrustum( lua_tonumber( L, -6 ), lua_tonumber( L, -5 ), lua_tonumber( L, -4 ), lua_tonumber( L, -3 ), lua_tonumber( L, -2 ), lua_tonumber( L, -1 ) ) );
return 1; return 1;
} }
@@ -915,12 +916,13 @@ int lmathMatrixPerspective( lua_State *L ) {
lua_pushboolean( L, false ); lua_pushboolean( L, false );
return 1; return 1;
} }
float far = lua_tonumber( L, -1 ); // float far = lua_tonumber( L, -1 );
float near = lua_tonumber( L, -2 ); // float near = lua_tonumber( L, -2 );
float aspect = lua_tonumber( L, -3 ); // float aspect = lua_tonumber( L, -3 );
float fovy = lua_tonumber( L, -4 ); // float fovy = lua_tonumber( L, -4 );
uluaPushMatrix( L, MatrixPerspective( fovy, aspect, near, far ) ); // uluaPushMatrix( L, MatrixPerspective( fovy, aspect, near, far ) );
uluaPushMatrix( L, MatrixPerspective( lua_tonumber( L, -4 ), lua_tonumber( L, -3 ), lua_tonumber( L, -2 ), lua_tonumber( L, -1 ) ) );
return 1; return 1;
} }
@@ -940,14 +942,15 @@ int lmathMatrixOrtho( lua_State *L ) {
lua_pushboolean( L, false ); lua_pushboolean( L, false );
return 1; return 1;
} }
float far = lua_tonumber( L, -1 ); // float far = lua_tonumber( L, -1 );
float near = lua_tonumber( L, -2 ); // float near = lua_tonumber( L, -2 );
float top = lua_tonumber( L, -3 ); // float top = lua_tonumber( L, -3 );
float bottom = lua_tonumber( L, -4 ); // float bottom = lua_tonumber( L, -4 );
float right = lua_tonumber( L, -5 ); // float right = lua_tonumber( L, -5 );
float left = lua_tonumber( L, -6 ); // float left = lua_tonumber( L, -6 );
uluaPushMatrix( L, MatrixOrtho( left, right, bottom, top, near, far ) ); // uluaPushMatrix( L, MatrixOrtho( left, right, bottom, top, near, far ) );
uluaPushMatrix( L, MatrixOrtho( lua_tonumber( L, -6 ), lua_tonumber( L, -5 ), lua_tonumber( L, -4 ), lua_tonumber( L, -3 ), lua_tonumber( L, -2 ), lua_tonumber( L, -1 ) ) );
return 1; return 1;
} }