Add polygon fills

This commit is contained in:
Edgaru089 2024-04-22 04:50:06 +08:00
parent 7c75e10d62
commit 4acd6c0710
4 changed files with 49 additions and 4 deletions

View File

@ -188,6 +188,25 @@ static void _app_LevelCommand(App *app, char *cmd) {
e->render->fillbox = box; e->render->fillbox = box;
e->render->fillcolor = color; 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 { else {
WARN("unknown command \"%s\"", cmd); WARN("unknown command \"%s\"", cmd);

View File

@ -10,6 +10,7 @@
#include "util/tree.h" #include "util/tree.h"
#include "types.h" #include "types.h"
#include "render_util.h" #include "render_util.h"
#include "util/vector.h"
#include <math.h> #include <math.h>
#include <graphics.h> #include <graphics.h>
@ -27,7 +28,7 @@ void app_Render(App *app) {
setbkcolor(app->clear_color); setbkcolor(app->clear_color);
cleardevice(); cleardevice();
// Draw fill boxes first // Draw fill polys & boxes first
for (tree_Node *i = tree_FirstNode(app->entity->entities); for (tree_Node *i = tree_FirstNode(app->entity->entities);
i != NULL; i != NULL;
i = tree_Node_Next(i)) { i = tree_Node_Next(i)) {
@ -39,9 +40,29 @@ void app_Render(App *app) {
Vec2 pos = {.x = 0.0, .y = 0.0}; Vec2 pos = {.x = 0.0, .y = 0.0};
if (e->position) if (e->position)
pos = e->position->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 // Has fillbox
if (r->fillbox.size.x > EPS) { 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); setfillcolor(r->fillcolor);
solidrectangle( solidrectangle(
(int)round(cam.lefttop.x), (int)round(cam.lefttop.x),

View File

@ -3,6 +3,7 @@
#include "app.h" #include "app.h"
#include "render_bundle.h" #include "render_bundle.h"
#include "types.h" #include "types.h"
#include "util/vector.h"
Component_Render *render_NewComponent(Entity *super, const char *bundle_name) { 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) { void render_DeleteComponent(Component_Render *r) {
if (r->fillpoly)
vector_Destroy(r->fillpoly);
free(r); free(r);
} }

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "util/vector.h"
#include "render_bundle.h" #include "render_bundle.h"
@ -22,6 +23,7 @@ typedef struct {
void *custom_data; // User data for the callback void *custom_data; // User data for the callback
Box2 fillbox; // Fill box Box2 fillbox; // Fill box
vector_Vector *fillpoly; // Fill polygon, vector of Vec2
uint32_t fillcolor; // Fill color uint32_t fillcolor; // Fill color
} Component_Render; } Component_Render;