diff --git a/app_file.c b/app_file.c index 9b1fad4..3585639 100644 --- a/app_file.c +++ b/app_file.c @@ -188,6 +188,25 @@ static void _app_LevelCommand(App *app, char *cmd) { e->render->fillbox = box; e->render->fillcolor = color; } + CMD("FILLPOLY") { + uint32_t color = readcolor(); + int n = TOKEN_INT; + + // Allocate a Vector + vector_Vector *vec = vector_Create(sizeof(Vec2)); + vector_Reserve(vec, n); + + // Read N points + for (int i = 0; i < n; i++) { + Vec2 point = readvec2(); + vector_Push(vec, &point); + } + + Entity *e = entity_Create(app->entity, cmd); + ADD_COMPONENT(e, render); + e->render->fillpoly = vec; // Deallocated in render_DeleteComponent + e->render->fillcolor = color; + } else { WARN("unknown command \"%s\"", cmd); diff --git a/app_render.cpp b/app_render.cpp index 2dd4991..fc9907b 100644 --- a/app_render.cpp +++ b/app_render.cpp @@ -10,6 +10,7 @@ #include "util/tree.h" #include "types.h" #include "render_util.h" +#include "util/vector.h" #include #include @@ -27,7 +28,7 @@ void app_Render(App *app) { setbkcolor(app->clear_color); cleardevice(); - // Draw fill boxes first + // Draw fill polys & boxes first for (tree_Node *i = tree_FirstNode(app->entity->entities); i != NULL; i = tree_Node_Next(i)) { @@ -39,9 +40,29 @@ void app_Render(App *app) { Vec2 pos = {.x = 0.0, .y = 0.0}; if (e->position) pos = e->position->position; + + // Has fill polygon + if (r->fillpoly) { + static vector_Vector *buf = vector_Create(2 * sizeof(int)); + vector_Clear(buf); + + // Transform every point in the polygon + for (int i = 0; i < vector_Size(r->fillpoly); i++) { + Vec2 screen_point = camera_TransformVec2(app->camera, vec2_Add(*(Vec2 *)vector_At(r->fillpoly, i), pos)); + int screen_point_int[2] = { + (int)round(screen_point.x), + (int)round(screen_point.y)}; + vector_Push(buf, screen_point_int); + } + + // Draw + setfillcolor(r->fillcolor); + solidpolygon((POINT *)vector_Data(buf), vector_Size(r->fillpoly)); + } + // Has fillbox if (r->fillbox.size.x > EPS) { - Box2 cam = camera_TransformBox2(app->camera, r->fillbox); + Box2 cam = camera_TransformBox2(app->camera, box2_Offset(r->fillbox, pos)); setfillcolor(r->fillcolor); solidrectangle( (int)round(cam.lefttop.x), diff --git a/render_component.c b/render_component.c index 8d97d3c..e66ac03 100644 --- a/render_component.c +++ b/render_component.c @@ -3,6 +3,7 @@ #include "app.h" #include "render_bundle.h" #include "types.h" +#include "util/vector.h" Component_Render *render_NewComponent(Entity *super, const char *bundle_name) { @@ -25,5 +26,7 @@ Component_Render *render_NewComponentFunc(Entity *super, render_CustomFunc func, } void render_DeleteComponent(Component_Render *r) { + if (r->fillpoly) + vector_Destroy(r->fillpoly); free(r); } diff --git a/render_component.h b/render_component.h index 0a452df..933a817 100644 --- a/render_component.h +++ b/render_component.h @@ -1,5 +1,6 @@ #pragma once +#include "util/vector.h" #include "render_bundle.h" @@ -21,8 +22,9 @@ typedef struct { render_CustomFunc custom; // Custom rendering function void *custom_data; // User data for the callback - Box2 fillbox; // Fill box - uint32_t fillcolor; // Fill color + Box2 fillbox; // Fill box + vector_Vector *fillpoly; // Fill polygon, vector of Vec2 + uint32_t fillcolor; // Fill color } Component_Render;