greenfork / nimraylib_now

The Ultimate Raylib gaming library wrapper for Nim
MIT License
149 stars 16 forks source link

Windows progress #1

Closed zetashift closed 3 years ago

zetashift commented 3 years ago

First off, thank you for your wonderful work! I've been tinkering on Windows with this, and anything that requires raylib.dll works, atleast the samples I tried. I'll be playing with this more!

  1. Download the Windows release from: https://github.com/raysan5/raylib/releases, but don't choose the installer, choose the other one, like for the 3.5.0 release you download: raylib-3.5.0_win64_mingw-w64.zip if you are on a 64bits system or raylib-3.5.0_win32_mingw-w64.zip if you are on a 32bits system.
  2. In the downloaded archive there is a /lib folder in there you'll find raylib.dll, put this dll in the same folder as your resulting .exe file.

So say we want to run this https://github.com/greenfork/nimraylib_now/blob/master/examples/shapes/bouncing_ball.nim example. cd yourself into the folder, run nim c -r .\bouncing_ball.nim and make sure the dll is in the same folder, and you'll find yourself seeing this:

screenshot_20210110_143702

Current issues

Testing doesn't work

Running nimble testExamples gives the following error

PS P:\dev\gamedev\nimraylib_now> nimble testExamples
  Executing task testExamples in P:\dev\gamedev\nimraylib_now\nimraylib_now.nimble
no tests were found for pattern: 'tests/examples/**/*.nim'

Maybe this is has to do with how paths are handled differently on Windows and Linux?

Raygui

I downloaded raygui.dll from your release page, put it next to raylib.dll but the example still fails with: could not load: raygui.dll Running buildRaygui gives me the following error:

  Executing task buildRaygui in P:\dev\gamedev\nimraylib_now\nimraylib_now.nimble
stack trace: (most recent call last)
C:\Users\Rishi\AppData\Local\Temp\nimblecache-0\nimscriptapi_2903036856.nim(187, 16)
P:\dev\gamedev\nimraylib_now\nimraylib_now.nimble(26, 8) buildRayguiTask
C:\Users\Rishi\.choosenim\toolchains\nim-1.4.2\lib\system\nimscript.nim(186, 19) exec
C:\Users\Rishi\.choosenim\toolchains\nim-1.4.2\lib\system\nimscript.nim(186, 19) Error: unhandled exception: The system cannot find the file specified.
 [OSError]
     Error: Exception raised during nimble script execution

This might have to do with me not having raygui.o anywhere. I'll try to troubleshoot this tonight or tomorrow.

greenfork commented 3 years ago

Thanks very much for the docs, this will definitely go to readme!

I fixed the issue with / so that it should be \ on Windows, this should solve testing.

With raygui it is a bit harder, the exact commands in nimble file are:

  let sourceFile = "raygui"/"src"/"raygui.h"
  exec "cc -x c -fPIC -c -o raygui.o -DRAYGUI_IMPLEMENTATION -DRAYGUI_SUPPORT_ICONS " & sourceFile
  exec "cc -shared -o libraygui.so -lraylib raygui.o"

On windows at least -o libraygui.so should be -o raygui.dll (as per conventions) and God knows what else. I searched for "how to compile a dynamic library for Windows" and got:

The commands should be similar though. And raygui.h requires raylib.h to be in path, you can just download it from raylib if it's not loaded by default https://github.com/raysan5/raylib/blob/master/src/raylib.h

greenfork commented 3 years ago

Could you test raygui usage with -d:nimDebugDlOpen, it should give more info on what's actually wrong with this dll. I personally took it from here: https://github.com/Guevara-chan/Raylib-Forever/releases

nim r -d:nimDebugDlOpen examples\original\basic.nim
greenfork commented 3 years ago

@zetashift could you update the bindings from latest master and try again. It should just work. Only raylib.dll should be present in the same directory

zetashift commented 3 years ago

Running the shapes/bouncing_ball and the shapes/draw_ring example gives me on the latest master currently this error:

C:/Users/Rishi/.choosenim/toolchains/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/6.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lraylib
collect2.exe: error: ld returned 1 exit status
Error: execution of an external program failed: 'gcc.exe   -o P:\dev\gamedev\nimraylib_now\examples\shapes\bouncing_ball.exe  C:\Users\Rishi\nimcache\bouncing_ball_d\stdlib_io.nim.c.o C:\Users\Rishi\nimcache\bouncing_ball_d\stdlib_system.nim.c.o C:\Users\Rishi\nimcache\bouncing_ball_d\stdlib_dynlib.nim.c.o C:\Users\Rishi\nimcache\bouncing_ball_d\stdlib_winlean.nim.c.o C:\Users\Rishi\nimcache\bouncing_ball_d\stdlib_times.nim.c.o C:\Users\Rishi\nimcache\bouncing_ball_d\stdlib_os.nim.c.o C:\Users\Rishi\nimcache\bouncing_ball_d\@m..@s..@ssrc@snimraylib_now@sraylib.nim.c.o C:\Users\Rishi\nimcache\bouncing_ball_d\@mbouncing_ball.nim.c.o  -lraylib   '

I think I have to pass in the raylib headers somewhere then?

greenfork commented 3 years ago

Yes, here is the line:

{.passL:"-lraylib".}

Possible problem is that mingw (gcc.exe) doesn't see the raylib.dll file. Possible solution: -L flag

nim r --passL:"-L<path/to/directory>" -d:nimDebugDlOpen examples\original\basic.nim

Another solution is to change {.passL:"-lraylib".} to something else, but I'm not sure what exactly

Searches on the topic, nothing seems to have the exact answer but for reference:

Mingw website is currently down for some reason, would be good to browse their wiki once it's up e.g. this one http://www.mingw.org/wiki/LibraryPathHOWTO

greenfork commented 3 years ago

According to these answers:

Seems that libraylibdll.a is also needed for linking

Raylib ships with 3 binaries:

raylib-3.5.0_win64_mingw-w64/lib/libraylib.a
raylib-3.5.0_win64_mingw-w64/lib/libraylibdll.a
raylib-3.5.0_win64_mingw-w64/lib/raylib.dll

and I assume that you need both libraylibdll.a and raylib.dll present in the same directory as exe

Alternatively we might want to pass it like this "--passL:libraylibdll.a" as @enthus1ast did it here https://github.com/enthus1ast/raylib/blob/master/src/draylib.nim#L4

greenfork commented 3 years ago

I removed this passL pragma from master so you have easier time tinkering with it. For me it now works like this:

nim r --passL:-lraylib examples/original/crown.nim

My current understanding is that one of these options should work for Windows if you have libray.dll and libraydll.a files in the same directory as exe:

nim r --passL:"-lraylib" examples\original\crown.nim
nim r --passL:"-L/path/to/exe/folder -lraylib" examples\original\crown.nim
nim r --passL:"libraylibdll.a" examples\original\crown.nim
nim r --passL:"-L/path/to/exe/folder libraylibdll.a" examples\original\crown.nim

Please let me know if one of these commands work or you find anything else. I think I will be able to automate path finding after we find the correct command and flags

zetashift commented 3 years ago

nim r --passL:"libraylibdll.a" examples\original\crown.nim works! As long as libraylibdll.a is in the same folder. nim r --passL:"-L/path/to/exe/folder libraylibdll.a" examples\original\crown.nim works too!

nim r --passL:"-lraylib" examples\original\crown.nim
nim r --passL:"-L/path/to/exe/folder -lraylib" examples\original\crown.nim
nim r --passL:"libraylibdll.a" examples\original\crown.nim
nim r --passL:"-L/path/to/exe/folder libraylibdll.a" examples\original\crown.nim
  1. The first one errored out with: ld.exe: cannot find -lraylib collect2.exe: error: ld returned 1 exit status
  2. Errors out with the same as above.
  3. Works whoop whoop!
  4. Works too!

The raygui example I tried worked too.

zetashift commented 3 years ago

nimble testExamples still gives me the error:

PS P:\dev\gamedev\nimraylib_now> nimble testExamples
  Executing task testExamples in P:\dev\gamedev\nimraylib_now\nimraylib_now.nimble
no tests were found for pattern: 'tests\examples\**\*.nim'
zetashift commented 3 years ago

Running a single test example using testament r I get this monstrosity: https://play.nim-lang.org/#ix=2LLJ

greenfork commented 3 years ago

Thank you very much for the feedback, I'm glad we finally found the right flag!

Current master fixes problem with passL flags like this in raylib.nim:

when defined(windows):
  when defined(vcc):
    # Should it be `link` instead of passL?
    {.passL:"raylibdll.lib".}
  else:
    {.passL:"libraylibdll.a".}
else:
  {.passL:"-lraylib".}

And "no tests were found for pattern" problem by replacing "*" glob to just single "" asterisk since double asterisk is not present in power shell, so the testament command is fixed to be like this:

task testExamples, "checks that all examples are correctly compiled":
  let pattern = "'tests"/"examples"/"*"/"*.nim'"
  exec "testament pattern " & pattern

In short, everything should work in regards to running and compiling any code if dll files are in the same directory as exe.

I don't know how to solve the problem with testament output though.

Let me know if the latest master works for you and I will release a new version and update readme accordingly.

zetashift commented 3 years ago

Samples I have tried, like crown/draw_ring/bouncing_ball work great on latest master by simply running: nim c -r foo.nim ofcourse having the .dll and .a file in the same folder as resulting .exe.

Thank you!!

greenfork commented 3 years ago

Great, thanks! A new version will be released soon

zetashift commented 3 years ago

I can finally try this on my Linux laptop :P. I'll close this, the examples might be a whole other issue.

greenfork commented 3 years ago

Mission completed indeed. Thank you for your time!