JacksEscape/render_bundle_draw.cpp

64 lines
1.6 KiB
C++
Raw Normal View History

2024-03-28 14:04:43 +08:00
#include "camera.h"
#include "render_bundle.h"
#include "app.h"
#include "util/vector.h"
#include "util/assert.h"
#include <stdio.h>
#include <math.h>
#include <easyx.h>
namespace {
vector_Vector *buff;
}
extern "C" void render_DrawPrimitiveW(App *app, render_Primitive *p, Vec2 offset) {
if (buff == NULL)
buff = vector_Create(sizeof(POINT));
vector_Clear(buff);
// Construct the points in screen space
for (int i = 0; i < vector_Size(p->points); i++) {
Vec2 realpos = vec2_Add(*((Vec2 *)vector_At(p->points, i)), offset);
Vec2 screenpos;
if (!app->camera) {
// Really weird
fprintf(stderr, "[WARN][render_DrawPrimitiveW] called without a Camera system\n");
screenpos = realpos;
} else
screenpos = camera_TransformVec2(app->camera, realpos);
// Round the screen position to ints
POINT rounded;
rounded.x = (LONG)round(screenpos.x);
rounded.y = (LONG)round(screenpos.y);
vector_Push(buff, &rounded);
}
// Set the colors
setlinecolor(p->fg);
setfillcolor(p->fg);
setbkcolor(p->bg);
// Draw the converted primitive
switch (p->type) {
case render_Lines:
if (vector_Size(buff) % 2 != 0)
fprintf(stderr, "[WARN][render_DrawPrimitiveW] render_Lines drawed odd numbers of points");
for (int i = 0; i < vector_Size(buff); i += 2) {
POINT p0 = *(POINT *)vector_At(buff, i);
POINT p1 = *(POINT *)vector_At(buff, i + 1);
line(p0.x, p0.y, p1.x, p1.y);
}
break;
case render_LineStrip:
polyline((POINT *)vector_Data(buff), vector_Size(buff));
break;
case render_Polygon:
fillpolygon((POINT *)vector_Data(buff), vector_Size(buff));
break;
}
}