GithubHelp home page GithubHelp logo

tsnake41 / raylib-lua Goto Github PK

View Code? Open in Web Editor NEW
143.0 7.0 11.0 3.52 MB

A modern LuaJIT binding for Raylib (also available at https://gitlab.com/TSnake41/raylib-lua)

License: ISC License

Makefile 0.25% C 41.09% Lua 58.62% Shell 0.03%
lua luajit raylib-lua raylib game-development raylib-binding luajit-ffi-bindings physac rlgl easings

raylib-lua's Introduction

raylib-lua logo

release downloads

raylib-lua

LuaJIT-based binding for raylib, a simple and easy-to-use library to learn video game programming.

This binding is partially based on raylib-wren/wray.

Usage of raylua_s

raylua_s is the script-mode binary of raylib-lua. Without any argument, raylua_s gets you into the REPL (the read-eval-print loop, i.e. the interactive interpretor) which gives you a minimal Lua shell that allows you to run Lua code from a terminal (aka command prompt, console, or shell).

You can give the name of a Lua file as an argument to raylua_s to run the specified Lua file.

Usage of raylua_e

raylua_e is the embedding-mode binary of raylib-lua.

This binary allows you to build standalone raylib applications from Lua code. This is useful both for testing and for packaging and distribution.

There are 3 ways to use it :

  • zip mode : If you specify a zip file as an argument to raylua_e, this zip file's contents will be used to build the application. raylua_e expects the zip file to contain a main.lua file, which is the entry point of the application (i.e. main.lua is where you want your program to start running).

     # Windows
     raylua_e someGame.zip
    
     # Unix
     ./raylua_e someGame.zip
    
  • directory mode : This mode is similar to zip mode, except that it automatically builds the zip file payload from the specified directory and then uses that zip file to build the application.

     # project/ is a directory
    
     # Windows
     raylua_e project
    
     # Unix
     ./raylua_e project
    
  • Lua mode : Alternatively, if your program is contained within a single Lua file, then you can build the executable directly from a single Lua file.

     # Windows
     raylua_e core_basic_window.lua
    
     # Unix
     ./raylua_e core_basic_window.lua
    

Using require in embedded mode works as expected but dofile and loadfile may not work as expected as these functions load from an external file rather than from package loaders.

Usage of raylua_r (Windows only)

On Windows systems, you may notice that there is an additional executable (named raylua_r) that is included with the pre-built Windows release download of this raylib-lua bindings library (or which you can build yourself). The purpose of raylua_r is to remove the extra terminal window (aka command prompt, console, or shell) that would normally open alongside the graphical raylib window whenever you run it. So, if you don't want that additional text window to show then you can use raylua_r instead of raylua_e to build your project without it.

Other operating systems may not consistently support this mechanism, and so there is currently no corresponding raylua_r for them.

For more information on the origin of raylua_r, see this old pull request where this feature was originally added.

Building / Updating raylib / Contribution

To build raylib-lua from source, you need to take care that submodules are imported. Otherwise, if submodules haven't been imported or if you are unsure:

git submodule init
git submodule update

This may take some time depending on network bandwidth. Afterwards, raylib-lua should build as expected using the make tool with a working C compiler.

If you are unfamliar with make, see the wikipedia for Make. It is a widely used build automation tool that is most often used on Unix-like systems (such as Linux and BSD).

Support for make on Windows can sometimes be poor though, so remember that there is a download button at the top of this page for if you just want ready-to-use copies of the raylua_s, raylua_e, and raylua_r executable files for Windows. That may be the easiest way to get started for many users. (It can be easy to overlook the download buttons on GitHub pages if you aren't paying close enough attention, so heads-up on that.)

A working Lua interpreter is needed. By default, the luajit interpreter built along with libluajit.a is used. In case of cross-compiling, you may want to change which Lua interpreter is used to one your system supports. You can specify the interpreter with the LUA variable.

If you need to update the raylib binding, there are a few tasks you will need to do:

  • Update the tools/api.h function signatures. Keep the file clean, with exactly one function per line.
  • Update the struct definitions in src/raylib.lua.

Loading embedded ressources

Currently, raylib-lua supports loading resources from payloads using the raylib API. You can also arbitrarily load files from payloads using raylua.loadfile, which returns a boolean indicating success or failure and the file's content.

Making structs

To make raylib structs, you need to use the LuaJIT FFI.

local ffi = require "ffi"

After importing the necessary package (as above), use ffi.new to make a struct, e.g. ffi.new("Color", r, g, b, a).

However, many functions in raylib won't require the use of such structs, so there's no need to worry until you actually need this.

Note concerning pointers

You can use the rl.ref function to build a pointer from a cdata struct. The rl.ref function only works with cdata structs. In the case of primitive (non-struct) cdata in contrast, you will need to make an array and pass it directly.

e.g :

local int_ptr = ffi.new "int [1]"
local data = tostring(rl.LoadFileData("test.txt", int_ptr))
local count = tonumber(int_ptr[0])

Simple example to test if your raylib-lua is working

rl.SetConfigFlags(rl.FLAG_VSYNC_HINT)

rl.InitWindow(800, 450, "raylib [core] example - basic window")

while not rl.WindowShouldClose() do
	rl.BeginDrawing()

	rl.ClearBackground(rl.RAYWHITE)
	rl.DrawText("Congrats! You created your first window!", 190, 200, 20, rl.LIGHTGRAY)

	rl.EndDrawing()
end

rl.CloseWindow()

Compatibility

raylib-lua (raylua) currently uses raylib 4.5 API. See compat.lua for more info.

physac and rlgl modules are also built-in by default. raygui is supported, but is minimally tested.

Please report any issues you have with raygui with raylib-lua (raylua) on GitHub or on the #raylib-lua subchannel of the raylib Discord server. To find the #raylib-lua page, join the raylib Discord server by clicking the above link, then scroll down on the left side of Discord's list of channels until you find #raylib-lua and click it. You can chat and ask questions there.

Global API

You can make raylib-lua (raylua) partially compatible with original raylib-lua or raylib-lua-sol with the global API by adding setmetatable(_G, { __index = rl }) on the first line.

This will allow direct use of the raylib binding through globals instead of through the rl table.

You have an example of this in lua_global_api.lua.

Editor support

There is limited autocompletion support for VSCode and other EmmyLua frontends using this definition file.

Check this page for more information.

It may also be possible to get auto-complete working in ZeroBrane Studio (a popular IDE exclusively designed for Lua) according to the information on this page written by a different Lua raylib binding author, which mentions that their own autocomplete implementation (raylua) coincedentally has some compatibility with this binding (raylib-lua) too.

Debugging

You can use Local Lua Debugger for Visual Studio Code to provide debugging support with Visual Studio Code. You need to add this at the beginning of your code to use it :

do local f = getmetatable(rl).__index;rawset(rl, "__index", function (_, k) return select(2, pcall(f, _, k)) end) end
package.path = package.path .. os.getenv "LUA_PATH"
local lldebugger = require "lldebugger"; lldebugger.start()

You also need to setup a launch configuration in Visual Studio Code to run raylua_s with the debugger attached, e.g.

{
    "type": "lua-local",
    "request": "launch",
    "name": "(Lua) Launch",
    "cwd": "${workspaceFolder}",
    "program": { "command": "PATH TO raylua_s" },
    "args": [ "main.lua OR ${file} OR WHATEVER" ]
}

This debugger doesn't support pausing at arbitrary unspecified points. You will need to place a breakpoint before executing your code to get an actual steppable debug working. Otherwise, an error will need to be thrown in the application to get the debugging to trigger.

This debugger has significant overhead, expect a potentially large performance loss in intensive projects if you use it.

Other bindings

raylib-lua (raylua) is not the only Lua binding for raylib.

There are some other bindings, which may or may not be up to date.

RobLoach/raylib-lua-sol

raysan5/raylib-lua

HDPLocust/raylib-luamore

alexander-matz ffi binding

darltrash/raylib-luajit

Rabios/raylua

Licence

Copyright (C) 2023 Astie Teddy

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

raylib-lua's People

Contributors

joseph-montanez avatar sneusse avatar tsnake41 avatar wraithglade avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

raylib-lua's Issues

raylib-lua is slow on Apple M1

I'm on a MacBook Air (M1, 2020) running macOS 12.4.

After cloning the repo and subrepos and running make, trying to run any of the raylua_* binaries results in:

zsh: killed     ./raylua_e

(…or raylua_s or raylua_r) and an exit code of 137.

Searching for "zsh: killed" seems to mostly result in pages relating to running Homebrew or tools installed with it on M1 Macs. I don't think this is Homebrew-related since I'm a boomer who uses MacPorts (if it ain't broke…). Nonetheless I wonder if the issue might still be related to the relatively new M1 hardware and I wonder if anyone else is seeing this issue happen on M1 Macs, or alternatley is running it successfully.

Unfortunately I no longer have easy access to Intel-based Mac hardware to see if the issue is happening there too.

Unable to use DrawTextEx

As the title says, I can't use it, I've tried everything. I even made sure that the font was in the path.

`varargs` and the `arg` hidden parameter don't work correctly

Hi, I think I've found an issue when trying to define a varargs function. Here's an example:

function util.pyprint(...)
    res = ""
    for k, v in ipairs(arg) do
        print("k = " .. tostring(k) .. ", v = " .. tostring(v))
        res = res .. tostring(v)
    end
    print(res)
end

This function is supposed to print variable argument and is intentionally similar to python's print. I added another print call for debugging purposes. When I run this function, with any kind and number of arguments, I get this output:

k = 1, v = test.lua
test.lua

(test.lua being the input file for raylua_s).

Part of the `README.md` lists "3.5" as a version, but elsewhere the library is claimed to be "4.5". Which is correct?

The README.md currently has the following in it:

raylib-lua (raylua) currently uses raylib 3.5 API.

However, elsewhere on the repository and also on the big raylib bindings list this library's version is listed as being 4.5.

Is this bindings library still using 3.5 for some parts of its code or has that text simply not been updated yet?

Should that version number be changed to 4.5?

Anyway, thank your for your time and for your work on this bindings library. I look forward to trying it out. 😎

Audio doesnt work in embedded mode

sorry i am no c programmer i have no ideia why it doesnt work but it doesnt work
the following example works fine when running in scritpt mode but not when i build the exe

local width, height = 800, 450

rl.SetConfigFlags(rl.FLAG_VSYNC_HINT)
rl.InitWindow(800, 450, "raylib")
rl.InitAudioDevice()

local logo = rl.LoadTexture "assets/logo.png"
local music = rl.LoadMusicStream "assets/music/music.ogg"

rl.PlayMusicStream(music)

while not rl.WindowShouldClose() do
  rl.UpdateMusicStream(music)

	rl.BeginDrawing()
  rl.ClearBackground(rl.RAYWHITE)

  rl.DrawTexture(logo, width/2 - logo.width/2, height/2 - logo.height/2, rl.WHITE)
  rl.DrawText("this is a texture!", 350, 370, 10, rl.GRAY)

  rl.EndDrawing()
end

rl.UnloadMusicStream(music)

rl.CloseAudioDevice()
rl.CloseWindow()

the raylib output indicates that files were loaded succesfully, and if I remove the rl.UpdateMusicStream it seems to work fine (although the music wont play)

Explain the purpose of `raylua_r` in `README.md`

When I downloaded the compiled binaries for the raylib Lua bindings, I noticed that it had an extra EXE file in the zip file which was not mentioned anywhere on the README.md page.

What is it intended for?

I think some comment about what it is should probably be added to the README.md to avoid confusion.

My collision system no longer works

I updated the binary (its the only change I made to the code) and now the program ignores collisions

function collision()
	 local collision = false
	 for k,v in pairs(o) do
			if rl.CheckCollisionBoxes(
				 {{ pl.pos.x - pl.size.x/2, pl.pos.y - pl.size.y/2, pl.pos.z - pl.size.z/2 },
						{ pl.pos.x + pl.size.x/2, pl.pos.y + pl.size.y/2, pl.pos.z + pl.size.z/2 }},
				 {{ o[k][1][1] - o[k][2][1]/2, o[k][1][2] - o[k][2][2]/2, o[k][1][3] - o[k][2][3]/2},
						{ o[k][1][1] + o[k][2][1]/2,	o[k][1][2] + o[k][2][2]/2, o[k][1][3] + o[k][2][3]/2 }}) == false  then
				 collision = false
			else
				 collision = true
				 break
			end


			return collision
	 end
end

the function was taken from a raylib example and as I said it was totally working and shiet but now it no longer works 😢

Request, or instructions, to create a custom version of raylua

This may seem strange, but I would like to know if there is a possibility to have the executables without the built-in raylib.

The reason for this is that I have been looking for an efficient way to get all the files from a program made in lua for a while, and put them inside an executable / binary

The problem is that none is 100% perfect, but testing with raylua, I can get it to work with any other library and files I want, I did a test with sdl.

The only problem is that it already has raylib built in, giving it a hefty size of 2.5mb, and I would like to have a compiler option without raylib built in.

I see huge potential for this to become the best "compiler" for lua.

I also accept instructions on how to compile the program without raylib. Thanks in advance for the answer.

How is Texture2D handled

-- you'll see I moved the images into the folder with the file and with luaJit.exe and the libaries
Here's the original C source
https://github.com/raysan5/raylib/blob/master/examples/textures/textures_background_scrolling.c
Where background, scrollingMid, scrollingFore are there's Texture2D statement ahead of them
but how do we do that in Lua?
All I get is crazy flashing window.

------- textures_background_scrolling.lua

local rl = require("raylib")
-- Initialization
local screenWidth = 800
local screenHeight = 450

rl.InitWindow(screenWidth, screenHeight, "raylib [textures] example - background scrolling")
rl.SetTargetFPS(60) -- Set our game to run at 60 frames-per-second

 -- NOTE: Be careful, background width must be equal or bigger than screen width
 -- if not, texture should be draw more than two times for scrolling effect
  background = rl.LoadTexture("cyberpunk_street_background.png")
  midground = rl.LoadTexture("cyberpunk_street_midground.png")
  foreground = rl.LoadTexture("cyberpunk_street_foreground.png")

 scrollingBack = 0.0
 scrollingMid = 0.0
 scrollingFore = 0.0


while not rl.WindowShouldClose() do   -- Detect window close button or ESC key
 -- Update
    ------------------------------------------------------------------------------------
    scrollingBack = scrollingBack- 0.1
    scrollingMid = scrollingMid- 0.5
    scrollingFore = scrollingFore- 1.0
  
    -- NOTE: Texture is scaled twice its size, so it sould be considered on scrolling
    if (scrollingBack <= -background.width*2) then scrollingBack = 0 end
    if (scrollingMid <= -midground.width*2) then scrollingMid = 0 end
    if (scrollingFore <= -foreground.width*2) then scrollingFore = 0 end
    -- Draw
    -----------------------------------------------------------------------------------
    rl.BeginDrawing()   
    
    
     --ClearBackground(GetColor(0x052c46ff))
      rl.ClearBackground(rl.RAYWHITE)
        -- Draw background image twice
        -- NOTE: Texture is scaled twice its size
      rl.DrawTextureEx(background, (Vector2){ scrollingBack, 20 }, 0.0, 2.0, WHITE)
      rl.DrawTextureEx(background, (Vector2){ background.width*2 + scrollingBack, 20 }, 0.0, 2.0,  rl.WHITE)

        --Draw midground image twice
      rl.DrawTextureEx(midground, (Vector2){ scrollingMid, 20 }, 0.0, 2.0, WHITE)
       rl.DrawTextureEx(midground, (Vector2){ midground.width*2 + scrollingMid, 20 }, 0.0, 2.0,  rl.WHITE)

        -- Draw foreground image twice
      rl.DrawTextureEx(foreground, (Vector2){ scrollingFore, 70 }, 0.0, 2.0, WHITE)
       rl.DrawTextureEx(foreground, (Vector2){ foreground.width*2 + scrollingFore, 70 }, 0.0, 2.0, rl.WHITE)

        rl.DrawText("BACKGROUND SCROLLING & PARALLAX", 10, 10, 20, RED)
        rl.DrawText("(c) Cyberpunk Street Environment by Luis Zuno (@ansimuz)", screenWidth - 330, screenHeight - 20, 10, RAYWHITE)

    EndDrawing()


rl.DrawText("Congrats! You created your first window!", 190, 200, 20, rl.LIGHTGRAY)

rl.EndDrawing()
end

-- De-Initialization
----------------------------------------------------------------------------------------
rl.UnloadTexture(background)  -- Unload background texture
rl.UnloadTexture(midground)   -- Unload midground texture
rl.UnloadTexture(foreground)  -- Unload foreground texture

rl.CloseWindow()              -- Close window and OpenGL context
----------------------------------------------------------------------------------------

Error trying to initialize a window

Hi, first of all, thanks for the amazing work with raylib-lua, this is definitely the best raylib port for lua that I could find, it meets all my requirements. However, I am having a small problem and would like instructions to fix it, as I have no experience with c.

When I run raylua_s, it runs the REPL normally, however, when I try to start a window, with the following code, it returns the following error:

Code:

local screenWidth = 800
local screenHeight = 450

rl.InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window")
rl.SetTargetFPS(60)

while not rl.WindowShouldClose() do
  rl.BeginDrawing()
  rl.ClearBackground(rl.RAYWHITE)
  rl.DrawText("Congrats! You created your first window!", 190, 200, 20, rl.LIGHTGRAY)
  rl.DrawFPS(10, 10)
  rl.EndDrawing()
end

rl.CloseWindow()

Error:

$ ./raylua_s main.lua
RAYLUA: Raylua boot script
RAYLUA: Lua Version: Lua 5.1
RAYLUA: Lua JIT: LuaJIT 2.1.0-beta3
RAYLUA: Loading FFI binding entries.
RAYLUA: Loaded 823 FFI entries.
INFO: Initializing raylib 4.0
WARNING: GLFW: Error: 65543 Description: GLX: Failed to create context: GLXBadFBConfig
WARNING: GLFW: Failed to initialize Window
FATAL: Failed to initialize Graphic Device
$

I researched, and I didn't find any solution regarding raylib.

I would be very grateful if you could help me resolve this error.

Grateful for the answer.

error while using local raylib = require("raylib")

when i execute the local raylib = require("raylib") it gives this error

lua: error loading module 'raylib' from file '.\raylib.lua':
C stack overflow
stack traceback:
[C]: in ?
[C]: in function 'require'
.\raylib.lua:1: in main chunk
[C]: in function 'require'
.\raylib.lua:1: in main chunk
[C]: in function 'require'
.\raylib.lua:1: in main chunk
[C]: in function 'require'
.\raylib.lua:1: in main chunk
[C]: in function 'require'
... (skipping 370 levels)
[C]: in function 'require'
.\raylib.lua:1: in main chunk
[C]: in function 'require'
.\raylib.lua:1: in main chunk
[C]: in function 'require'
.\raylib.lua:1: in main chunk
[C]: in function 'require'
.\raylib.lua:1: in main chunk
[C]: in function 'require'
raylib.lua:1: in main chunk
[C]: in ?
stack traceback:
[C]: in ?
[C]: in function 'require'
.\raylib.lua:1: in main chunk
[C]: in function 'require'
.\raylib.lua:1: in main chunk
[C]: in function 'require'
.\raylib.lua:1: in main chunk
[C]: in function 'require'
.\raylib.lua:1: in main chunk
[C]: in function 'require'
... (skipping 370 levels)
[C]: in function 'require'
.\raylib.lua:1: in main chunk
[C]: in function 'require'
.\raylib.lua:1: in main chunk
[C]: in function 'require'
.\raylib.lua:1: in main chunk
[C]: in function 'require'
.\raylib.lua:1: in main chunk
[C]: in function 'require'
raylib.lua:1: in main chunk
[C]: in ?

what i do??

How does this system work

I would like to ask how this binder works and how to correctly use it. I never used binders and the only thing i understand now is how to run the project for linux but what if i want to build it for windows and etc.. Is there some info on how to do stuff like this?

Building on Windows

I noticed there was no info on building for windows in the readme, it just says to download the prebuilt executables. Wanted to try the 5.0 branch, which isn't prebuilt yet, so I tried finding a reliable way to build it on Windows and I think that MSYS2 with the mingw64 environment active appears to be very reliable. I tested with the 4.5a source and the updated 5.0 source code on two different Windows machines and the builds worked as expected. Possibly worth a quick update to the readme?

Machines Testing (using both raylua_s.exe and raylua_e.exe):

Machine 1:
OS: Windows 11 Home 23H2
CPU: 11th Gen Intel Core i7-11800H @ 2.30GHz 2.30 GHz
GPU: RTX 3060

Machine 2:
OS: Windows 10 Home 22H2
CPU: AMD Ryzen 7-5700U @ 4.3GHz
GPU: Integrated AMD Radeon

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.