From ac3ce97b80f139f2b06be59de872ad7765d56c76 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Fri, 3 Mar 2023 19:07:59 +0000 Subject: [PATCH] Add color options to draw box --- source/engine/debug/debugdraw.c | 8 ++++---- source/engine/debug/debugdraw.h | 3 ++- source/engine/ffi.c | 33 +++++++++++++++++++++++---------- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/source/engine/debug/debugdraw.c b/source/engine/debug/debugdraw.c index 10861f75..68da9bc1 100644 --- a/source/engine/debug/debugdraw.c +++ b/source/engine/debug/debugdraw.c @@ -7,7 +7,7 @@ #include #include "debug.h" #include "window.h" - +#include "2dphysics.h" #include "stb_ds.h" static uint32_t circleVBO; @@ -230,10 +230,10 @@ void draw_rect(int x, int y, int w, int h, float *color) draw_poly(verts, 4, color); } -void draw_box(struct cpVect c, struct cpVect wh) +void draw_box(struct cpVect c, struct cpVect wh, struct color color) { - float white[3] = {1, 1, 1}; - draw_rect(c.x, c.y, wh.x, wh.y, white); + float col[3] = {(float)color.r/255, (float)color.g/255, (float)color.b/255}; + draw_rect(c.x, c.y, wh.x, wh.y, col); } void draw_grid(int width, int span) diff --git a/source/engine/debug/debugdraw.h b/source/engine/debug/debugdraw.h index 5c37b87d..f30dbd02 100644 --- a/source/engine/debug/debugdraw.h +++ b/source/engine/debug/debugdraw.h @@ -2,6 +2,7 @@ #define DEBUGDRAW_H struct cpVect; +struct color; void debugdraw_init(); void draw_line(int x1, int y1, int x2, int y2, float *color); @@ -10,7 +11,7 @@ void draw_points(struct cpVect *points, int n, float size, float *color); void draw_circle(int x, int y, float radius, int pixels, float *color, int fill); void draw_grid(int width, int span); void draw_rect(int x, int y, int w, int h, float *color); -void draw_box(struct cpVect c, struct cpVect wh); +void draw_box(struct cpVect c, struct cpVect wh, struct color color); void draw_point(int x, int y, float r, float *color); void draw_cppoint(struct cpVect point, float r); void draw_poly(float *points, int n, float *color); diff --git a/source/engine/ffi.c b/source/engine/ffi.c index 65818900..227c551b 100644 --- a/source/engine/ffi.c +++ b/source/engine/ffi.c @@ -47,11 +47,11 @@ struct color duk2color(duk_context *duk, int p) struct color color; duk_get_prop_index(duk, p, 0); - color.r = duk_to_number(duk, -1); - duk_get_prop_index(duk, p, 0); - color.b = duk_to_number(duk, -1); - duk_get_prop_index(duk, p, 0); - color.g = duk_to_number(duk, -1); + color.r = duk_to_int(duk, -1); + duk_get_prop_index(duk, p, 1); + color.g = duk_to_int(duk, -1); + duk_get_prop_index(duk, p, 2); + color.b = duk_to_int(duk, -1); return color; } @@ -182,6 +182,19 @@ duk_ret_t duk_gui_text(duk_context *duk) { return 0; } +duk_ret_t duk_ui_text(duk_context *duk) +{ + const char *s = duk_to_string(duk, 0); + cpVect pos = duk2vec2(duk, 1); + + float size = duk_to_number(duk, 2); + struct color c = duk2color(duk,3); + const float col[3] = {(float)c.r/255, (float)c.g/255, (float)c.b/255}; + renderText(s, &pos, size, col, 500); + return 0; + +} + duk_ret_t duk_gui_img(duk_context *duk) { const char *img = duk_to_string(duk, 0); cpVect pos = duk2vec2(duk, 1); @@ -680,7 +693,7 @@ duk_ret_t duk_cmd(duk_context *duk) { return 1; case 53: - draw_box(duk2vec2(duk, 1), duk2vec2(duk, 2)); + draw_box(duk2vec2(duk, 1), duk2vec2(duk, 2), duk2color(duk,3)); return 0; case 54: @@ -961,11 +974,11 @@ duk_ret_t duk_set_body(duk_context *duk) { case 8: cpBodySetAngularVelocity(go->body, duk_to_number(duk, 2)); - break; + return 0; case 9: cpBodySetVelocity(go->body, duk2vec2(duk, 2)); - break; + return 0; case 10: go->e = fmax(duk_to_number(duk,2), 0); @@ -974,7 +987,6 @@ duk_ret_t duk_set_body(duk_context *duk) { case 11: go->f = fmax(duk_to_number(duk,2),0); break; - } cpSpaceReindexShapesForBody(space, go->body); @@ -1287,7 +1299,8 @@ void ffi_load() DUK_FUNC(register, 3); DUK_FUNC(register_collide, DUK_VARARGS); - DUK_FUNC(gui_text, 3); + DUK_FUNC(gui_text, DUK_VARARGS); + DUK_FUNC(ui_text, 4); DUK_FUNC(gui_img, 2);