Add color options to draw box

This commit is contained in:
John Alanbrook 2023-03-03 19:07:59 +00:00
parent 0832441c91
commit ac3ce97b80
3 changed files with 29 additions and 15 deletions

View file

@ -7,7 +7,7 @@
#include <assert.h> #include <assert.h>
#include "debug.h" #include "debug.h"
#include "window.h" #include "window.h"
#include "2dphysics.h"
#include "stb_ds.h" #include "stb_ds.h"
static uint32_t circleVBO; 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); 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}; 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, white); draw_rect(c.x, c.y, wh.x, wh.y, col);
} }
void draw_grid(int width, int span) void draw_grid(int width, int span)

View file

@ -2,6 +2,7 @@
#define DEBUGDRAW_H #define DEBUGDRAW_H
struct cpVect; struct cpVect;
struct color;
void debugdraw_init(); void debugdraw_init();
void draw_line(int x1, int y1, int x2, int y2, float *color); 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_circle(int x, int y, float radius, int pixels, float *color, int fill);
void draw_grid(int width, int span); void draw_grid(int width, int span);
void draw_rect(int x, int y, int w, int h, float *color); 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_point(int x, int y, float r, float *color);
void draw_cppoint(struct cpVect point, float r); void draw_cppoint(struct cpVect point, float r);
void draw_poly(float *points, int n, float *color); void draw_poly(float *points, int n, float *color);

View file

@ -47,11 +47,11 @@ struct color duk2color(duk_context *duk, int p)
struct color color; struct color color;
duk_get_prop_index(duk, p, 0); duk_get_prop_index(duk, p, 0);
color.r = duk_to_number(duk, -1); color.r = duk_to_int(duk, -1);
duk_get_prop_index(duk, p, 0); duk_get_prop_index(duk, p, 1);
color.b = duk_to_number(duk, -1); color.g = duk_to_int(duk, -1);
duk_get_prop_index(duk, p, 0); duk_get_prop_index(duk, p, 2);
color.g = duk_to_number(duk, -1); color.b = duk_to_int(duk, -1);
return color; return color;
} }
@ -182,6 +182,19 @@ duk_ret_t duk_gui_text(duk_context *duk) {
return 0; 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) { duk_ret_t duk_gui_img(duk_context *duk) {
const char *img = duk_to_string(duk, 0); const char *img = duk_to_string(duk, 0);
cpVect pos = duk2vec2(duk, 1); cpVect pos = duk2vec2(duk, 1);
@ -680,7 +693,7 @@ duk_ret_t duk_cmd(duk_context *duk) {
return 1; return 1;
case 53: case 53:
draw_box(duk2vec2(duk, 1), duk2vec2(duk, 2)); draw_box(duk2vec2(duk, 1), duk2vec2(duk, 2), duk2color(duk,3));
return 0; return 0;
case 54: case 54:
@ -961,11 +974,11 @@ duk_ret_t duk_set_body(duk_context *duk) {
case 8: case 8:
cpBodySetAngularVelocity(go->body, duk_to_number(duk, 2)); cpBodySetAngularVelocity(go->body, duk_to_number(duk, 2));
break; return 0;
case 9: case 9:
cpBodySetVelocity(go->body, duk2vec2(duk, 2)); cpBodySetVelocity(go->body, duk2vec2(duk, 2));
break; return 0;
case 10: case 10:
go->e = fmax(duk_to_number(duk,2), 0); go->e = fmax(duk_to_number(duk,2), 0);
@ -974,7 +987,6 @@ duk_ret_t duk_set_body(duk_context *duk) {
case 11: case 11:
go->f = fmax(duk_to_number(duk,2),0); go->f = fmax(duk_to_number(duk,2),0);
break; break;
} }
cpSpaceReindexShapesForBody(space, go->body); cpSpaceReindexShapesForBody(space, go->body);
@ -1287,7 +1299,8 @@ void ffi_load()
DUK_FUNC(register, 3); DUK_FUNC(register, 3);
DUK_FUNC(register_collide, DUK_VARARGS); 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); DUK_FUNC(gui_img, 2);