summaryrefslogtreecommitdiffhomepage
path: root/src/raylib.c
blob: b53261fb3b6e029c437e943c1b17199e09e07340 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <raylib.h>
#include <mruby.h>
#include <mruby/array.h>
#include <mruby/data.h>
#include <mruby/class.h>
#include <stdlib.h>
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif

#if defined(PLATFORM_WEB)
void execute_emscripten_block(void*);
#endif

static const struct mrb_data_type Color_type = {
	"Color", mrb_free
};

static mrb_value
mrb_Color_initialize(mrb_state* state, mrb_value self) {
	mrb_int r = 255;
	mrb_int g = 0;
	mrb_int b = 0;
	mrb_int a = 255;
	mrb_get_args(state, "|iiii", &r, &g, &b, &a);

	struct Color *color = (struct Color *)DATA_PTR(self);
	if(color) { mrb_free(state, color); }
	mrb_data_init(self, NULL, &Color_type);
	color = (struct Color *)mrb_malloc(state, sizeof(struct Color));

	color->r = r;
	color->g = g;
	color->b = b;
	color->a = a;

	mrb_data_init(self, color, &Color_type);
	return self;
}

static mrb_value
mrb_Color_red(mrb_state* state, mrb_value self) {
	struct Color *color = NULL;
	Data_Get_Struct(mrb, self, &Color_type, color);
	return mrb_fixnum_value(color->r);
}

static mrb_value
mrb_init_window(mrb_state *mrb, mrb_value self) {
	printf("1\n");
	mrb_int screenWidth = 800;
	printf("2\n");
	mrb_int screenHeight = 600;
	printf("3\n");
	char* title = "Hello World from FelFlame!";
	printf("4\n");
	mrb_get_args(mrb, "|iiz", &screenWidth, &screenHeight, &title);
	printf("5\n");

	InitWindow(screenWidth, screenHeight, title);
	printf("6\n");

	return mrb_nil_value();
}

static mrb_value
mrb_platform(mrb_state *mrb, mrb_value self) {
#if defined(PLATFORM_WEB)
	return mrb_str_new_lit(mrb, "web");
#else
	return mrb_str_new_lit(mrb, "desktop");
#endif
}

//void DrawText(const char *text, int posX, int posY, int fontSize, Color color);
static mrb_value
mrb_draw_text(mrb_state *mrb, mrb_value self) {
	char* text = "Default Text";
	mrb_int x = 0;
	mrb_int y = 0;
	mrb_int fontSize = 16;
	mrb_value color_obj;

	struct Color *color = NULL;

	mrb_get_args(mrb, "|ziiio", &text, &x, &y, &fontSize, &color_obj);
	Data_Get_Struct(mrb, color_obj, &Color_type, color);
	DrawText(text, x, y, fontSize, *color);
	return mrb_nil_value();
}

static mrb_value
mrb_begin_drawing(mrb_state *mrb, mrb_value self) {
	BeginDrawing();
	return mrb_nil_value();
}

static mrb_value
mrb_end_drawing(mrb_state *mrb, mrb_value self) {
	EndDrawing();
	return mrb_nil_value();
}

static mrb_value
mrb_clear_background(mrb_state *mrb, mrb_value self) {
	ClearBackground(RAYWHITE);
	return mrb_nil_value();
}

static mrb_value 
mrb_call_main_loop(mrb_state *mrb, mrb_value self) {
	struct RClass *c = mrb_module_get(mrb, "Raylib");
	mrb_value ml = mrb_funcall(mrb, mrb_obj_value(c), "main_loop", 0);
	return mrb_funcall(mrb, ml, "call", 0);
}

static mrb_value 
mrb_window_should_close(mrb_state *mrb, mrb_value self) {
	return mrb_obj_value(WindowShouldClose());
}

#if defined(PLATFORM_WEB)
static mrb_value 
mrb_emscripten_set_main_loop(mrb_state *mrb, mrb_value self) {
	emscripten_set_main_loop_arg(execute_emscripten_block, mrb, 0, 1);
	return mrb_nil_value();
}

void
execute_emscripten_block(void *mrb) {
	struct RClass *c = mrb_module_get(mrb, "Raylib");
	mrb_value ml = mrb_funcall(mrb, mrb_obj_value(c), "main_loop", 0);
	mrb_funcall(mrb, ml, "call", 0);
}
#endif

static mrb_value
mrb_target_fps(mrb_state *mrb, mrb_value self) {
	mrb_int fps = 60;
	mrb_get_args(mrb, "i", &fps);
	SetTargetFPS(fps);
	return mrb_nil_value();
}

static mrb_value
mrb_fps(mrb_state *mrb, mrb_value self) {
	return mrb_fixnum_value(GetFPS());
}

static mrb_value
mrb_frame_time(mrb_state *mrb, mrb_value self) {
	return mrb_float_value(mrb, GetFrameTime());
}

static mrb_value
mrb_time(mrb_state *mrb, mrb_value self) {
	return mrb_float_value(mrb, GetTime());
}

void
mrb_mruby_raylib_gem_init(mrb_state* mrb) {
	struct RClass *raylib = mrb_define_module(mrb, "Raylib");
	//struct RClass *color_class = mrb_define_class(mrb, "Color", mrb->object_class);
	struct RClass *color_class = mrb_define_class_under(mrb, raylib, "Color", mrb->object_class);
	MRB_SET_INSTANCE_TT(color_class, MRB_TT_DATA);
	mrb_define_class_method(mrb, raylib, "init_window", mrb_init_window, MRB_ARGS_REQ(3));
	mrb_define_class_method(mrb, raylib, "platform", mrb_platform, MRB_ARGS_NONE());
	mrb_define_class_method(mrb, raylib, "_draw_text", mrb_draw_text, MRB_ARGS_REQ(5));
	mrb_define_class_method(mrb, raylib, "begin_drawing", mrb_begin_drawing, MRB_ARGS_NONE());
	mrb_define_class_method(mrb, raylib, "end_drawing", mrb_end_drawing, MRB_ARGS_NONE());
	mrb_define_class_method(mrb, raylib, "clear_background", mrb_clear_background, MRB_ARGS_NONE());
	mrb_define_class_method(mrb, raylib, "call_main_loop", mrb_call_main_loop, MRB_ARGS_NONE());
	mrb_define_class_method(mrb, raylib, "window_should_close?", mrb_window_should_close, MRB_ARGS_NONE());
	mrb_define_class_method(mrb, raylib, "target_fps=", mrb_target_fps, MRB_ARGS_REQ(1));
	mrb_define_class_method(mrb, raylib, "fps", mrb_fps, MRB_ARGS_NONE());
	mrb_define_class_method(mrb, raylib, "frame_time", mrb_frame_time, MRB_ARGS_NONE());
	mrb_define_class_method(mrb, raylib, "time", mrb_time, MRB_ARGS_NONE());
	mrb_define_method(mrb, color_class, "initialize", mrb_Color_initialize, MRB_ARGS_REQ(4));
#if defined(PLATFORM_WEB)
	mrb_define_class_method(mrb, raylib, "emscripten_set_main_loop", mrb_emscripten_set_main_loop, MRB_ARGS_NONE());
#endif
}

void
mrb_mruby_raylib_gem_final(mrb_state* mrb) {
	/* finalizer */
}