anut-py / h-raylib Goto Github PK
View Code? Open in Web Editor NEWHaskell bindings for Raylib
Home Page: https://hackage.haskell.org/package/h-raylib
License: Apache License 2.0
Haskell bindings for Raylib
Home Page: https://hackage.haskell.org/package/h-raylib
License: Apache License 2.0
I tried drawing text using drawTextEx and it mostly won't work, and sometimes it shows for a frame or 2 then nothing again. Tried using this code with two different fonts I know are working from another project:
mainFontPath :: String
mainFontPath = "/path/to/font"
main :: IO ()
main = do
initWindow screenWidth screenHeight "Font Test"
setTargetFPS 75
mainFont <- loadFont mainFontPath
gameLoop mainFont
gameLoop :: Font -> IO ()
gameLoop mainFont = do
beginDrawing
clearBackground rayWhite
drawTextEx mainFont "Testing drawTextEx" (Vector2 640.0 12.0) 20.0 1.0 black
endDrawing
shouldClose <- windowShouldClose
unless shouldClose $ gameLoop mainFont
And the logs show the font loaded successfully
sscan
is not implemented.
I can fix it if you want but I wanted to report it here before I intrude.
Hi Anut, attached is the output for the mac build, running cabal run
Let me know if I can help any further with info or tests!
Best!
The types in Raylib.Types feature many nested records. It would be nice to be able to access them using lenses. If there is interest, I can try to do it myself.
import Control.Monad.Catch (MonadMask, bracket_)
drawing :: (MonadIO m, MonadMask m) => m () -> m ()
drawing = bracket_ (liftIO beginDrawing) (liftIO endDrawing)
mode3D :: (MonadIO m, MonadMask m) => RL.Camera3D -> m () -> m ()
mode3D c = bracket_ (liftIO $ beginMode3D c) (liftIO endMode3D)
mode2D :: (MonadIO m, MonadMask m) => RL.Camera2D -> m () -> m ()
mode2D c = bracket_ (liftIO $ beginMode2D c) (liftIO endMode2D)
devtools.js -u
devtools.js -p
Basically title.
My ghc version is ghc 9.2.8
.
Other functions from Raylib.Util
are there.
Template Haskell is enabled.
The cabal file is this:
common warnings
ghc-options: -Wall
executable pokiclone
-- Import common warning flags.
import: warnings
-- .hs or .lhs file containing the Main module.
main-is: Main.hs
-- Modules included in this executable, other than Main.
-- other-modules:
-- LANGUAGE extensions used by modules in this package.
other-extensions: MultiWayIf
-- Other library packages from which modules are imported.
build-depends: base ^>=4.16.4.0
, h-raylib
, lens
-- Directories containing source files.
hs-source-dirs: app
-- Base language which the package is written in.
default-language: GHC2021
After unpacking the hackage sdist for 4.5.1.0 raylib/src/external
contains only:
cgltf.h dr_mp3.h glad.h jar_xm.h msf_gif.h qoi.h sinfl.h stb_image_write.h stb_truetype.h
dirent.h dr_wav.h glfw m3d.h par_shapes.h rl_gputex.h stb_image.h stb_perlin.h tinyobj_loader_c.h
dr_flac.h glad_gles2.h jar_mod.h miniaudio.h qoa.h sdefl.h stb_image_resize.h stb_rect_pack.h vox_loader.h
Hello,
When I run the following program with cabal run
under Windows nothing happens.
hello-raylib> cabal run
hello-raylib>
hello-raylib.cabal
cabal-version: 3.4
name: hello-raylib
version: 0.1.0.0
common warnings
ghc-options: -Wall
executable hello-raylib
import: warnings
main-is: Main.hs
build-depends: base ^>=4.16.0.0
, h-raylib
hs-source-dirs: app
default-language: Haskell2010
app/Main.hs
module Main where
import Control.Monad (unless)
import qualified Raylib.Core as Raylib
main :: IO ()
main = do
w <- Raylib.initWindow 480 320 "Hello, Raylib!"
let loop = do
shouldClose <- Raylib.windowShouldClose
unless shouldClose loop
loop
Raylib.closeWindow w
I tried updating my NVidia drivers. No change.
I installed raylib and successfully ran one of their examples in C so I suspect there's something wrong with this package or something I've done or missed in the code posted above.
Thank you and kind regards
Björn
When using loadModel
or unloadModel
, WindowResources
has to be passed as final parameter. Because of that, pasting code around could be annoying, for some codes using win
while the other using w
or wr
.
Here's solution using monad reader
inWindow :: (MonadIO m, MonadMask m) => Int -> Int -> String -> Int -> ReaderT WindowResources m b -> m b
inWindow w h title fps = bracket (liftIO $ initWindow w h title <* setTargetFPS fps) (liftIO . closeWindow) . runReaderT
loadModelFile :: (MonadIO m, MonadMask m, MonadReader WindowResources m) => FilePath -> m Model
loadModelFile file = ask >>= liftIO . loadModel file
and also some bracket functions for unload
withModel :: (MonadIO m, MonadMask m, MonadReader WindowResources m) => FilePath -> (RL.Model -> m b) -> m b
withModel file = bracket (ask >>= liftIO . loadModel file) (\m -> ask >>= liftIO . unloadModel m)
withModels :: (MonadIO m, MonadMask m, MonadReader WindowResources m) => [FilePath] -> ([RL.Model] -> m b) -> m b
withModels [] f = f []
withModels (file:files) f = withModel file $ \m -> withModels files $ \ms -> f (m:ms)
a side effect of using monad reader is that the abstraction require using lift
for simplest program
mainGame :: IO ()
mainGame =
inWindow 1920 1080 "MyApp" 90 $ do
lift $ -- init something ...
withModels ["./res/ship1.obj", "./res/ship2.obj"] $ \[m1, m2] -> lift $ do
-- main loop with some real drawing code ...
Hello and thank you for creating these bindings!
I wanted to report that the defaultWindowResources
function defined here isn't visible in the documentation. I had to click on Source for WindowResources
and that's how I found it.
On a related note, I'm trying to get loadTexture
to work which requires WindowResources
. Would you happen to have an example of using loadTexture
? Thanks!
I used the window example as base and started playing around with inputs, just printing text to see if they work. isKeyPressed and isKeyReleased are working fine, but isKeyDown and isKeyUp just return True. Heres the code i used:
gameLoop :: IO ()
gameLoop = do
wDown <- isKeyDown key'w
wUp <- isKeyUp key'w
if wDown then print "W down" else print "W not down"
if wUp then print "W up" else print "W not up"
beginDrawing
clearBackground rayWhite
drawText "Basic raylib window" 30 40 18 lightGray
endDrawing
shouldClose <- windowShouldClose
unless shouldClose gameLoop
The output prints both "W down" and "W up" no matter the input
When I've included this package into Stack build project, I've got error : * Missing (or bad) C libraries: gcc_eh
Can we please include into documentation these steps to resolve it?
This errors occurs when haskell stack can't find gcc libs. To fix this follow these steps:
stack exec -- pacman -Syu
, stack exec -- pacman -S gcc
Hello, and first of all thank you for these bindings - I've been enjoying using them!
However, I think I've found a bug. I am currently working on a project and for some reason I'm getting wildly different behaviours when getRayCollisionXXX
is involved. Here's a snippet (which you can find here)
checkBoard :: PlayerAimComponent -> (BoardComponent, PositionComponent) -> System World BoardComponent
checkBoard (Aim ray) (_, p) = do
hit <- liftIO $ isHit ray p
if hit then
pure $ Board Filled Filled Filled Filled Filled Filled Filled Filled Filled
else
pure $ Board Empty Empty Empty Empty Empty Empty Empty Empty Empty
isHit :: RL.Ray -> PositionComponent -> IO Bool
isHit ray (Position p) = do
let hitInfo = RL.getRayCollisionBox ray $ RL.BoundingBox from to
-- liftIO $ print "Hello" -- <- uncomment to make this work?
pure $ RL.raycollision'hit hitInfo > 0
where from = addVectors p $ Vector3 (-1.5) (-1.5) (-0.05)
to = addVectors p $ Vector3 1.5 1.5 0.05
So this function is WiP, but it tries to detect if the ray shooting from the camera is hitting one of the game boards, and if it does I'm just flagging every cell so that we can see it. Here's the weird thing though: This function only seems to work if you print
something inside it. I have no idea what this signifies, but after digging down into h-raylib
's source code I've found that getRayCollisionBox
along with lots of other physics functions are unsafe IO functions.
I have no experience in FFI or making bindings to C, but while this fact remains true I can't help but wonder if it's the cause. All the other raylib functions I've used I've had to do liftIO
to access them, and they've all worked really well with no issues this far.
Would it be difficult to make your collision functions also operate within IO
? At least then if this is still an issue we can conclude its not the bindings?
Alternatively, if you have any suggestions of what the issue could be, please let me know as I'm blocked by this.
Thanks again for the great library!
Here's some debugging work I've done:
Here's what it looks like if we don't print anything. The boards light up if hit
is true, which is true when raycollision'hit hitInfo > 0
. When we don't print anything, all of them are permanently lit even if we don't look at any of them.
Uncommenting the comment so that we print hello
in this function actually fixes it, all three boards are unlit until we look at them:
raycollision'hit hitInfo
This one is an exception - if I print this value, I get the following:
hitInfo
hit
value is 6749696 - a memory address maybe?hit
value is 1
if I look at it, and 0
otherwise1
if looked at and 0
otherwisegetFontDefault
seems to cause Raylib's text utilities to suddenly break measureText
. Additionally, I've found that invoking getFontDefault
twice will crash Haskell citing a double free error or memory corruption as a cause. I'm not to sure what the cause is, but my guess is the original font is somehow getting destroyed as it is passed to Haskell.
Minimum repoduction:
main :: IO ()
main = do
_ <- initWindow 1 1 ""
defaultFont <- getFontDefault
defaultFont <- getFontDefault
return ()
Set up h-raylib to work with emscripten and support building to WebAssembly.
Haskell-styled function should be good at combination.
e.g. clamp
clamp has to be used in this way
clamp Value_to_clamp Lower_bound Upper_bound
more haskellish args ord should be
clamp Lower_bound Upper_bound Value_to_clamp
if change the order of args the endo-function will change
from (\x -> clamp x (-40) 40)
to clamp (-40) 40
which makes this function combine easily with other Float -> Float
function
Same thing goes:
I saw that the Xext
dependency was removed recently 5a09c28
But when building 4.5.0.7
with macaroni.nix
, it fails due to that dependency not being declared:
Configuring library for h-raylib-4.5.0.7..
building
Preprocessing library for h-raylib-4.5.0.7..
Building library for h-raylib-4.5.0.7..
[1 of 4] Compiling Raylib.Types ( src/Raylib/Types.hs, dist/build/Raylib/Types.o, dist/build/Raylib/Types.dyn_o )
[2 of 4] Compiling Raylib.Colors ( src/Raylib/Colors.hs, dist/build/Raylib/Colors.o, dist/build/Raylib/Colors.dyn_o )
[3 of 4] Compiling Raylib.Util ( src/Raylib/Util.hs, dist/build/Raylib/Util.o, dist/build/Raylib/Util.dyn_o )
[4 of 4] Compiling Raylib ( src/Raylib.hs, dist/build/Raylib.o, dist/build/Raylib.dyn_o )
In file included from raylib/src/external/glfw/src/x11_platform.h:48,
from raylib/src/external/glfw/src/platform.h:62,
from raylib/src/external/glfw/src/internal.h:331,
from raylib/src/external/glfw/src/init.c:30,
from raylib/src/rglfw.c:61:0: error:
/nix/store/r0pzhgq3l9mfhc67gxc7aarv15pjshql-libXi-1.8-dev/include/X11/extensions/XInput2.h:32:10: error:
fatal error: X11/extensions/Xge.h: No such file or directory
32 | #include <X11/extensions/Xge.h>
| ^~~~~~~~~~~~~~~~~~~~~~
|
32 | #include <X11/extensions/Xge.h>
| ^
compilation terminated.
`cc' failed in phase `C Compiler'. (Exit code: 1)
error: builder for '/nix/store/w7yzb3cdb5lvkr57llyvy2i28ay8z1j0-h-raylib-lib-h-raylib-4.5.0.7.drv' failed with exit code 1;
last 10 log lines:
>
> /nix/store/r0pzhgq3l9mfhc67gxc7aarv15pjshql-libXi-1.8-dev/include/X11/extensions/XInput2.h:32:10: error:
> fatal error: X11/extensions/Xge.h: No such file or directory
> 32 | #include <X11/extensions/Xge.h>
> | ^~~~~~~~~~~~~~~~~~~~~~
> |
> 32 | #include <X11/extensions/Xge.h>
> | ^
> compilation terminated.
> `cc' failed in phase `C Compiler'. (Exit code: 1)
For full logs, run 'nix log /nix/store/w7yzb3cdb5lvkr57llyvy2i28ay8z1j0-h-raylib-lib-h-raylib-4.5.0.7.drv'.
error: 1 dependencies of derivation '/nix/store/q9mawhklkbnpi65h7r88va5672fdwgbd-h-raylib-minimal-exe-h-raylib-minimal-0.1.0.0.drv' failed to build
make: *** [Makefile:2: build] Error 1
For now, 4.5.0.3
seems to build fine.
It would be cool if raygui was also included in this package. Any chance that this might happen?
Trying to drag and drop files fails. Particularly using the function loadDroppedFiles
will seg fault while trying to free an invalid pointer to the file path:
free(): invalid pointer
Aborted (core dumped)
Or it will try to double free:
free(): double free detected in tcache 2
Aborted (core dumped)
It does that on the next frame or the frame after. I'm not sure if this is user error, because I don't know what I should or should not free manually and how. This is a basic version of what I tried:
mainloop :: AppState -> IO AppState
mainloop appState = do
fileIsDropped <- isFileDropped
when fileIsDropped $
print =<< loadDroppedFiles
endDrawing
return appState''
main = whileWindowOpen_ mainloop =<< initApp
for adding base-4.18 to .cabal
whileWindowOpen0 :: IO () -> IO ()
==>
whileWindowOpenM0 :: MonadIO m => m () -> m ()
h-raylib/src/Raylib/Core/Models.hs
Line 162 in 019c689
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.