Some guidance in setting up Racket as a game scripting language.

182 views
Skip to first unread message

Hans Sjunnesson

unread,
Oct 2, 2019, 9:31:09 AM10/2/19
to Racket Users
This is a hobby project of mine - a game in C, using SDL. I'd like to only use C for the rendering part - rather use a scripting language for all of the gameplay code. I want to try to use Racket for this - I've been doing enough Clojure that I'm familiar with a lisp. My first approach was to following along "Embedding into a Program" section of the Racket guide. But I'm running into road blocks - and I can't find any good examples of projects which embed Racket in a C program that calls functions in a Racket environment. My initial plan was to define a Racket module that provided an "init", an "update", and a "draw" function, then call those from the C program. The embedding guide points me to using "raco" to generate a C version of "racket/base", and that works - I can eval basic stuff, but I'm getting an error when I (require "test.rkt"). So I'm guessing there's more to be done with setting up the environment.

My "test.rkt" file is this:

#lang racket/base

(printf "Hello World!\n")

And this is the pertinent part of the main.c file, which links libracket.

#include "scheme.h"
#include "base.c"

static int run(Scheme_Env* e, int argc, char* argv[]) {
scheme_env = e;

declare_modules(e);

scheme_namespace_require(scheme_intern_symbol("racket/base"));

Scheme_Config* config = scheme_current_config();
Scheme_Object *curout = scheme_get_param(config, MZCONFIG_OUTPUT_PORT);
Scheme_Object* v = scheme_eval_string("(+ 1 2)", scheme_env);
scheme_display(v, curout);
scheme_display(scheme_make_char('\n'), curout);

scheme_eval_string("(require \"test.rkt\")", scheme_env);
  
  return 0;
}

int main(int argc, char* argv[]) {
scheme_main_setup(1, run, argc, argv);
return 0;
}

The output is:

3
standard-module-name-resolver: collection not found
  for module path: racket/base/lang/reader
  collection: "racket/base/lang"
  in collection directories:
  context...:
   show-collection-err_0
   standard-module-name-resolver
   1/module-path-index-resolve
   do-dynamic-require5_0
   read-syntax3_0
   default-load-handler
   standard-module-name-resolver
   1/module-path-index-resolve
   perform-require!78.1
   for-loop_0
   expand-capturing-lifts
   temp118_0
   temp91_0
   compile15_0
   temp85_0

I'm thinking that the environment isn't correctly setup here, the "in collection directories" lists is empty.

Am I going about this the wrong way though? Would I be better served in setting up all of the game loops, etc. in Racket, then use FFI just for rendering with SDL?

The game I'm making is a roguelike, so performance isn't as critical.

Alex Harsanyi

unread,
Oct 2, 2019, 6:59:58 PM10/2/19
to Racket Users

Unless your objective is to learn C and SDL, I would recommend writing the game entirely in Racket and using the Racket graphics facilities -- you will find that the performance will be more than enough for a roguelike game that you want to develop. Also, there are more people that can help you with Racket graphics questions, should you have any.

There is also a sdl2 package on pkgs.racket-lang.org, if you want to use SDL from Racket.

Alex.

Hans Sjunnesson

unread,
Oct 5, 2019, 8:51:19 AM10/5/19
to Racket Users
I get that it's probably not the best way of doing it, like I described it. But this is mostly a project of brushing up on C and more low level programming.

Jens Axel Søgaard

unread,
Oct 5, 2019, 9:57:09 AM10/5/19
to Hans Sjunnesson, Racket Users
Den ons. 2. okt. 2019 kl. 15.31 skrev Hans Sjunnesson <hans.sj...@gmail.com>:
This is a hobby project of mine - a game in C, using SDL. I'd like to only use C for the rendering part - rather use a scripting language for all of the gameplay code. I want to try to use Racket for this - I've been doing enough Clojure that I'm familiar with a lisp. My first approach was to following along "Embedding into a Program" section of the Racket guide. But I'm running into road blocks - and I can't find any good examples of projects which embed Racket in a C program that calls functions in a Racket environment. My initial plan was to define a Racket module that provided an "init", an "update", and a "draw" function, then call those from the C program. The embedding guide points me to using "raco" to generate a C version of "racket/base", and that works - I can eval basic stuff, but I'm getting an error when I (require "test.rkt"). So I'm guessing there's more to be done with setting up the environment.

How do I compile the two test files?
In the reddit thread you said, I should start with:
raco ctool --c-mods base.c ++lib racket/base

But what are the options for gcc and the linker?

/Jens Axel

Hans Sjunnesson

unread,
Oct 7, 2019, 2:15:06 PM10/7/19
to Racket Users
I'm sorry Jens - I'm using Visual Studio on Windows to compile this project.
I've initially built libracket, and SDL, which I'm linking against.
I can zip the entire solution and upload it somewhere for you to have a look, but you'll need Visual Studio on Windows for that to work.

Jens Axel Søgaard

unread,
Oct 7, 2019, 3:48:01 PM10/7/19
to Hans Sjunnesson, Racket Users
Den man. 7. okt. 2019 kl. 20.15 skrev Hans Sjunnesson <hans.sj...@gmail.com>:
I'm sorry Jens - I'm using Visual Studio on Windows to compile this project.
I've initially built libracket, and SDL, which I'm linking against.
I can zip the entire solution and upload it somewhere for you to have a look, but you'll need Visual Studio on Windows for that to work.

I don't have a windows machine - but your test example only have two files. Is there a way to see
what options are applied (in a log perhaps)?

/Jens Axel

 

George Neuner

unread,
Oct 7, 2019, 4:30:56 PM10/7/19
to Jens Axel Søgaard, Hans Sjunnesson, Racket Users
Visual Studio's compile and link options are output in log files in the   <project_name>.tlog  sub-directory under the build (debug or release) directory.  See  CL.command.?.log  and  link.command.?.log  where '?' is some number.   [I don't know what that number represents - on my system I have only ever seen '1'.]

George

George Neuner

unread,
Oct 7, 2019, 4:50:35 PM10/7/19
to Jens Axel Søgaard, Hans Sjunnesson, Racket Users

On 10/7/2019 4:30 PM, George Neuner wrote:

Visual Studio's compile and link options are output in log files in the   <project_name>.tlog  sub-directory under the build (debug or release) directory.  See  CL.command.?.log  and  link.command.?.log  where '?' is some number.   [I don't know what that number represents - on my system I have only ever seen '1'.]

Sorry, should have been a little more explicit.

By default the logs and build information are in the source tree in sub-directories that are named corresponding to the desired build:  debug, release, x64/debug, x64/release.  The  <project>.tlog directory and the logs for each build that has happened will be under those.

If the project is customized for different output locations, then YMMV.

George

Hans Sjunnesson

unread,
Oct 8, 2019, 3:15:42 AM10/8/19
to Racket Users
Thanks, that's helpful.
I've zipped the entire visual studio project and put it up here https://www.dropbox.com/s/v0taqj2zr0qdv8p/roguelike.zip?dl=0
There's some SDL init stuff that pops a window with a texture on it. But in main.c, the program will throw an exception when trying this:

scheme_namespace_require(scheme_make_utf8_string("test.rkt"));

If you comment that, it works - and you can evaluate racket code like this:

scheme_eval_string("(+ 1 2)", scheme_env);
Reply all
Reply to author
Forward
0 new messages