diff options
Diffstat (limited to 'sources')
-rw-r--r-- | sources/core.h | 11 | ||||
-rw-r--r-- | sources/main.c | 6 | ||||
-rw-r--r-- | sources/shapes.h | 20 | ||||
-rw-r--r-- | sources/text.h | 12 | ||||
-rw-r--r-- | sources/types.h | 103 |
5 files changed, 147 insertions, 5 deletions
diff --git a/sources/core.h b/sources/core.h index 7e387de..70a7d81 100644 --- a/sources/core.h +++ b/sources/core.h @@ -3,6 +3,14 @@ #include <stdio.h> #include <stdlib.h> +static s7_pointer rl_get_mouse_position(s7_scheme *s7, s7_pointer args) { + Vector2 mouse_pos = GetMousePosition(); + s7_pointer vec = s7_make_float_vector(s7, 2, 1, NULL); + s7_vector_set(s7, vec, 0, s7_make_real(s7, mouse_pos.x)); + s7_vector_set(s7, vec, 1, s7_make_real(s7, mouse_pos.y)); + return vec; +} + static s7_pointer rl_is_key_down(s7_scheme *s7, s7_pointer args) { int key = s7_integer(s7_car(args)); bool isKey = IsKeyDown(key); @@ -10,5 +18,6 @@ static s7_pointer rl_is_key_down(s7_scheme *s7, s7_pointer args) { } static void rl_core_define_methods(s7_scheme *s7) { - s7_define_function(s7, "rl-is-key-down", rl_is_key_down, 1, 0, false, "test"); + s7_define_function(s7, "rl-is-key-down", rl_is_key_down, 1, 0, false, "(rl-is-key-down KEY)"); + s7_define_function(s7, "rl-get-mouse-position", rl_get_mouse_position, 0, 0, false, "(rl-get-mouse-position)"); } diff --git a/sources/main.c b/sources/main.c index 5086080..5556460 100644 --- a/sources/main.c +++ b/sources/main.c @@ -5,9 +5,11 @@ #include "raylib.h" #include "s7.h" +#include "types.h" #include "text.h" #include "texture.h" #include "core.h" +#include "shapes.h" #include <math.h> #include <stdio.h> @@ -41,10 +43,12 @@ void main_loop(){ int main(int argc, char* argv[]) { s7 = s7_init(); - + + rl_register_types(s7); rl_text_define_methods(s7); rl_texture_define_methods(s7); rl_core_define_methods(s7); + rl_shapes_define_methods(s7); const int screen_width = 800; const int screen_height = 600; diff --git a/sources/shapes.h b/sources/shapes.h new file mode 100644 index 0000000..dc73011 --- /dev/null +++ b/sources/shapes.h @@ -0,0 +1,20 @@ +#include "raylib.h" +#include "s7.h" +#include <stdio.h> +#include <stdlib.h> + +static s7_pointer rl_draw_rectangle(s7_scheme *s7, s7_pointer args) { + Color *c = (Color *)s7_c_object_value(s7_car(s7_cdr(s7_cdr(s7_cdr(s7_cdr(args)))))); + + DrawRectangle(s7_integer(s7_car(args)), + s7_integer(s7_car(s7_cdr(args))), + s7_integer(s7_car(s7_cdr(s7_cdr(args)))), + s7_integer(s7_car(s7_cdr(s7_cdr(s7_cdr(args))))), + *c); + + return(NULL); +} + +static void rl_shapes_define_methods(s7_scheme *s7) { + s7_define_function(s7, "rl-draw-rectangle", rl_draw_rectangle, 5, 0, false, "test"); +} diff --git a/sources/text.h b/sources/text.h index a0e3497..0759d2f 100644 --- a/sources/text.h +++ b/sources/text.h @@ -4,11 +4,17 @@ #include <stdlib.h> static s7_pointer rl_draw_text(s7_scheme *s7, s7_pointer args) { - printf("%s\n", s7_string(s7_car(args))); - DrawText(s7_string(s7_car(args)), 200, 80, 20, RED); + Color *c = (Color *)s7_c_object_value(s7_car(s7_cddddr(args))); + + DrawText(s7_string(s7_car(args)), + s7_integer(s7_cadr(args)), + s7_integer(s7_caddr(args)), + s7_integer(s7_cadddr(args)), + *c); + return(NULL); } static void rl_text_define_methods(s7_scheme *s7) { - s7_define_function(s7, "rl-draw-text", rl_draw_text, 1, 0, false, "test"); + s7_define_function(s7, "rl-draw-text", rl_draw_text, 5, 0, false, "test"); } diff --git a/sources/types.h b/sources/types.h new file mode 100644 index 0000000..7f8acc4 --- /dev/null +++ b/sources/types.h @@ -0,0 +1,103 @@ +#include "raylib.h" +#include "s7.h" +#include <stdio.h> +#include <stdlib.h> + +// ====================================== +// Color + +static int color_type_tag = 0; + +static s7_pointer free_color(s7_scheme *sc, s7_pointer obj) +{ + free(s7_c_object_value(obj)); + return(NULL); +} + +static s7_pointer make_color(s7_scheme *sc, s7_pointer args) +{ + Color *c = (Color *)malloc(sizeof(Color)); + + c->r = s7_integer(s7_car(args)); + c->g = s7_integer(s7_cadr(args)); + c->b = s7_integer(s7_caddr(args)); + c->a = 255; + + return(s7_make_c_object(sc, color_type_tag, (void *)c)); +} + +static s7_pointer color_to_string(s7_scheme *sc, s7_pointer args) +{ + s7_pointer result; + Color *c = (Color *)s7_c_object_value(s7_car(args)); + char *str = (char *)calloc(32, sizeof(char)); + snprintf(str, 32, "<color %d %d %d>", c->r, c->g, c->b); + result = s7_make_string(sc, str); + free(str); + return(result); +} + +static s7_pointer is_color(s7_scheme *sc, s7_pointer args) +{ + return(s7_make_boolean(sc, + s7_is_c_object(s7_car(args)) && + s7_c_object_type(s7_car(args)) == color_type_tag)); +} + +static s7_pointer color_r(s7_scheme *sc, s7_pointer args) +{ + Color *c = (Color *)s7_c_object_value(s7_car(args)); + return(s7_make_integer(sc, c->r)); +} + +static s7_pointer set_color_r(s7_scheme *sc, s7_pointer args) +{ + Color *c = (Color *)s7_c_object_value(s7_car(args)); + c->r = s7_integer(s7_cadr(args)); + return(s7_cadr(args)); +} + +static s7_pointer color_g(s7_scheme *sc, s7_pointer args) +{ + Color *c = (Color *)s7_c_object_value(s7_car(args)); + return(s7_make_integer(sc, c->g)); +} + +static s7_pointer set_color_g(s7_scheme *sc, s7_pointer args) +{ + Color *c = (Color *)s7_c_object_value(s7_car(args)); + c->g = s7_integer(s7_cadr(args)); + return(s7_cadr(args)); +} + +static s7_pointer color_b(s7_scheme *sc, s7_pointer args) +{ + Color *c = (Color *)s7_c_object_value(s7_car(args)); + return(s7_make_integer(sc, c->b)); +} + +static s7_pointer set_color_b(s7_scheme *sc, s7_pointer args) +{ + Color *c = (Color *)s7_c_object_value(s7_car(args)); + c->b = s7_integer(s7_cadr(args)); + return(s7_cadr(args)); +} + + + +// ====================================== + +static void rl_register_types(s7_scheme *s7) { + color_type_tag = s7_make_c_type(s7, "color"); + s7_c_type_set_gc_free(s7, color_type_tag, free_color); + s7_c_type_set_to_string(s7, color_type_tag, color_to_string); + + s7_define_function(s7, "make-color", make_color, 3, 0, false, "(make-color r g b) makes a new color"); + + s7_define_function(s7, "color?", is_color, 1, 0, false, "(color? anything) returns #t if its argument is a color object"); + + s7_define_variable(s7, "color-r", s7_dilambda(s7, "color-r", color_r, 1, 0, set_color_r, 2, 0, "color r field")); + s7_define_variable(s7, "color-g", s7_dilambda(s7, "color-g", color_g, 1, 0, set_color_g, 2, 0, "color g field")); + s7_define_variable(s7, "color-b", s7_dilambda(s7, "color-b", color_b, 1, 0, set_color_b, 2, 0, "color b field")); +} + |