Playing with Crystal on Windows

I’m a hobbyist level hack programmer but wanted to share the steps I followed to get Crystal working on Windows. I have MS Visual Studio 2022 installed with the C++ build tools.
BLUF: crystal runs, shards runs, libs get linked, things compile and run, woot.
The steps I followed were:

  1. install crystal windows preview via scoop
  2. compile and install Simple Directmedia Layer (SDL)–inclduing SDLMain–(Simple Directmedia Layer · GitHub) using Visual Studio 2022
  3. copy the SDL.lib and SDLmain.lib files to the crystal lib folder within scoop
  4. open Powershell and run crystal init app sdl_app
  5. cd sdl_app
  6. udpate shard.yml with
    dependencies:
    sdl:
    github: ysbaddaden/sdl.cr
  7. run shards install
  8. on error, open adminstrative powershell, and run shards udpate, close adminstrative powershell
  9. open windows environment variables within Windows Settings and add entry for CRYSTAL_PATH pointing to your scoop/apps/crystal/current/src;lib
  10. add require “SDL” to the source_file.cr
  11. run crystal build ./src/source_file should succeed
    Note 1: sdl example files from the sdl shard should now be able to run once copied into the source_file.cr using crystal run ./src/source_file.cr
    Note 2: to distribute the exe, it will require the SDL2.dll be in the same folder

crystal env shows the following
CRYSTAL_CACHE_DIR=C:\Users\me\AppData\Local\crystal\cache
CRYSTAL_PATH=lib;C:\Users\me\scoop\apps\crystal\current\src
CRYSTAL_VERSION=1.5.0
CRYSTAL_LIBRARY_PATH=C:\Users\me\scoop\apps\crystal\current\lib
CRYSTAL_OPTS=“”

***Updated based on Hadeweka’s suggestions.

8 Likes

Small note: If you set CRYSTAL_PATH to lib as well, it should suffice to simply do require "sdl", to make the code a bit more portable :slight_smile:

If I may ask: Do you have something specific in mind you want to do with Crystal and SDL?

Thanks for the suggestion, I’ve added the lib to the CRYSTAL_PATH and it doesn’t change the behavior of the require. Still needs the expanded line. My CRYSTAL_PATH is now “CRYSTAL_PATH=C:\Users\me\scoop\apps\crystal\current\src;CRYSTAL_PATH=C:\Users\me\scoop\apps\crystal\current\lib”. I’ll keep playing with it.
As to what I am doing, for years I’ve had a mental block regarding gui’s and I’ve decided now is the time to break that and get an understanding. Came across “SDL Game Development” and started going through the C++ code. From my time doing Ruby, I really hate the format and verbosity of C++, and am going to do the book exercises in both C++ and Crystal. Eventually, I’d like to do some modelling and sim using Crystal.

Ah, I see the problem, you seem to define CRYSTAL_PATH twice. Also, you just need to put lib into the variable, not the absolute path. This way, Crystal will always check for the local lib directory, regardless of your actual project.

Use the command
set CRYSTAL_PATH=C:\Users\me\scoop\apps\crystal\current\src;lib
or directly set your environment variable to
C:\Users\me\scoop\apps\crystal\current\src;lib
and it should work.

Also it definitely sounds interesting!
At one point I tried to build a game with C++, added Ruby scripting and then somehow ended up at Crystal instead.
I’m not fully settled on a media library yet (though it will probably be SDL in some way), but it’s already so much less frustrating than C++.

3 Likes

Thanks, appreciate the hint on lib;. Now works as I’d expect and I’ve updated the original post.

Thanks, this worked for me too

1 Like