Skip to content

Instantly share code, notes, and snippets.

@vaiorabbit
Last active October 16, 2023 13:23
Show Gist options
  • Save vaiorabbit/91725d4b76bb37fa0fa60e08e0848434 to your computer and use it in GitHub Desktop.
Save vaiorabbit/91725d4b76bb37fa0fa60e08e0848434 to your computer and use it in GitHub Desktop.
/*
[Usage]
$ clang -I`brew --prefix mruby`/include -Iraylib/include raylib_mrb.c `brew --prefix mruby`/lib/libmruby.a raylib/lib/libraylib.a -lm -framework IOKit -framework Cocoa -framework OpenGL -o raylib_mrb
$ ./raylib_mrb
*/
#include <mruby.h>
#include <mruby/class.h>
#include <mruby/compile.h>
#include <mruby/data.h>
#include <mruby/string.h>
#include <stdio.h>
#include <raylib.h>
struct RClass* mRaylib;
struct RClass* cRaylibColor;
static const struct mrb_data_type mrb_raylib_struct_Color = {
"Color", mrb_free
};
static mrb_value mrb_raylib_Color_initialize(mrb_state* mrb, mrb_value self) {
Color* instance = (Color*)mrb_malloc(mrb, sizeof(Color));
mrb_int r = 0;
mrb_int g = 0;
mrb_int b = 0;
mrb_int a = 0;
mrb_get_args(mrb, "iiii", &r, &g, &b, &a);
instance->r = r;
instance->g = g;
instance->b = b;
instance->a = a;
mrb_data_init(self, instance, &mrb_raylib_struct_Color);
return self;
}
static mrb_value mrb_raylib_InitWindow(mrb_state* mrb, mrb_value self)
{
mrb_int width = 0;
mrb_int height = 0;
mrb_value title;
mrb_get_args(mrb, "iiS", &width, &height, &title);
InitWindow(width, height, RSTRING_PTR(title));
return mrb_nil_value();
}
static mrb_value mrb_raylib_CloseWindow(mrb_state* mrb, mrb_value self)
{
CloseWindow();
return mrb_nil_value();
}
static mrb_value mrb_raylib_WindowShouldClose(mrb_state* mrb, mrb_value self)
{
bool ret = WindowShouldClose();
return mrb_bool_value(ret);
}
static mrb_value mrb_raylib_SetTargetFPS(mrb_state* mrb, mrb_value self)
{
mrb_int fps = 0;
mrb_get_args(mrb, "i", &fps);
SetTargetFPS(fps);
return mrb_nil_value();
}
static mrb_value mrb_raylib_BeginDrawing(mrb_state* mrb, mrb_value self)
{
BeginDrawing();
return mrb_nil_value();
}
static mrb_value mrb_raylib_EndDrawing(mrb_state* mrb, mrb_value self)
{
EndDrawing();
return mrb_nil_value();
}
static mrb_value mrb_raylib_ClearBackground(mrb_state* mrb, mrb_value self)
{
mrb_value color;
mrb_get_args(mrb, "o", &color);
Color* instance = DATA_GET_PTR(mrb, color, &mrb_raylib_struct_Color, Color);
ClearBackground(*instance);
return mrb_nil_value();
}
static mrb_value mrb_raylib_DrawFPS(mrb_state* mrb, mrb_value self)
{
mrb_int posX = 0;
mrb_int posY = 0;
mrb_get_args(mrb, "ii", &posX, &posY);
DrawFPS(posX, posY);
return self;
}
static void mrb_raylib_module_init(mrb_state* mrb)
{
mRaylib = mrb_define_module(mrb, "Raylib");
mrb_define_module_function(mrb, mRaylib, "InitWindow", mrb_raylib_InitWindow, MRB_ARGS_REQ(3));
mrb_define_module_function(mrb, mRaylib, "CloseWindow", mrb_raylib_CloseWindow, MRB_ARGS_NONE());
mrb_define_module_function(mrb, mRaylib, "ClearBackground", mrb_raylib_ClearBackground, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, mRaylib, "DrawFPS", mrb_raylib_DrawFPS, MRB_ARGS_REQ(2));
mrb_define_module_function(mrb, mRaylib, "SetTargetFPS", mrb_raylib_SetTargetFPS, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, mRaylib, "WindowShouldClose", mrb_raylib_WindowShouldClose, MRB_ARGS_NONE());
mrb_define_module_function(mrb, mRaylib, "BeginDrawing", mrb_raylib_BeginDrawing, MRB_ARGS_NONE());
mrb_define_module_function(mrb, mRaylib, "EndDrawing", mrb_raylib_EndDrawing, MRB_ARGS_NONE());
cRaylibColor = mrb_define_class_under(mrb, mRaylib, "Color", mrb->object_class);
MRB_SET_INSTANCE_TT(cRaylibColor, MRB_TT_DATA);
mrb_define_method(mrb, cRaylibColor, "initialize", mrb_raylib_Color_initialize, MRB_ARGS_REQ(4));
}
int main(void)
{
mrb_state* mrb = mrb_open();
mrb_raylib_module_init(mrb);
char ruby_code[] =
"c = 0\n"
"Raylib.InitWindow(720, 405, 'raylib/mruby')\n"
"Raylib.SetTargetFPS(60)\n"
"until Raylib.WindowShouldClose()\n"
" Raylib.BeginDrawing()\n"
" bg = Raylib::Color.new(c,c,c,255)\n"
" Raylib.ClearBackground(bg)\n"
" Raylib.DrawFPS(720 - 100, 16)\n"
" Raylib.EndDrawing()\n"
" c = (c + 1) % 255\n"
"end\n"
"Raylib.CloseWindow()\n"
;
mrb_load_string(mrb, ruby_code);
mrb_print_error(mrb);
mrb_close(mrb);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment