Bundle rendering fixes & first bundle

This commit is contained in:
Edgaru089 2024-03-30 18:55:43 +08:00
parent 367a3b1ccb
commit 93ffbfd66c
3 changed files with 45 additions and 8 deletions

View File

@ -4,6 +4,7 @@
#include "particle.h"
#include "physics.h"
#include "easyx.h"
#include "render_bundle.h"
#include "util/tree.h"
#include "types.h"
#include "render_util.h"
@ -48,7 +49,13 @@ void app_Render(App *app) {
render_SetModes(mode_rotate, since);
render_FillCircleW(app, vec2(200, 100), 20);
render_DrawBundleW(app, render_FindBundle("info_plate"), vec2(600, 550));
// Draw particles
setfillcolor(RGB(255, 255, 255));
setbkcolor(RGB(0, 0, 0));
particle_Render(app->particle);
}
}

View File

@ -2,8 +2,8 @@
#include "camera.h"
#include "render_bundle.h"
#include "app.h"
#include "render_util.h"
#include "util/vector.h"
#include "util/assert.h"
#include <stdio.h>
#include <math.h>
#include <easyx.h>
@ -41,20 +41,25 @@ extern "C" void render_DrawPrimitiveW(App *app, render_Primitive *p, Vec2 offset
setlinecolor(p->fg);
setfillcolor(p->fg);
setbkcolor(p->bg);
render_SetModes(p->mode, time_Now());
// 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) {
WARN("render_Lines drawed odd numbers of points", 0);
for (int i = 0; i < vector_Size(buff) - 1; 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));
for (int i = 0; i < vector_Size(buff) - 1; i++) {
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_Polygon:
fillpolygon((POINT *)vector_Data(buff), vector_Size(buff));

View File

@ -1,11 +1,15 @@
#include "render_bundle.h"
#include "render_util.h"
#include "util/vector.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define RGB(r, g, b) ((r) | ((g) << 8) | ((b) << 16))
vector_Vector *render_Bundles;
static render_Bundle *_tmpb;
static render_Primitive *_tmpp;
@ -49,14 +53,15 @@ static void _render_BundleCommand(char *cmd) {
_tmpp->points = vector_Create(sizeof(Vec2));
_tmpp->fg = 0xffffff;
_tmpp->bg = 0;
_tmpp->mode = render_ModeDefault;
// parse the type
char *type = strtok(NULL, " ");
if (strcmp(type, "LINES"))
if (strcmp(type, "LINES") == 0)
_tmpp->type = render_Lines;
else if (strcmp(type, "LINESTRIP"))
else if (strcmp(type, "LINESTRIP") == 0)
_tmpp->type = render_LineStrip;
else if (strcmp(type, "POLY"))
else if (strcmp(type, "POLY") == 0)
_tmpp->type = render_Polygon;
@ -74,10 +79,30 @@ static void _render_BundleCommand(char *cmd) {
} else if (CMD("P")) {
// Add a Vec2
if (_tmpp) {
Vec2 v = vec2(strtod(strtok(NULL, " "), NULL), strtod(strtok(NULL, " "), NULL));
double x = strtod(strtok(NULL, " "), NULL);
double y = strtod(strtok(NULL, " "), NULL);
Vec2 v = vec2(x, y);
vector_Push(_tmpp->points, &v);
} else
WARN("P without PRIM first", 0);
} else if (CMD("FG")) {
// Set Foreground color
if (_tmpp) {
int r = atoi(strtok(NULL, " "));
int g = atoi(strtok(NULL, " "));
int b = atoi(strtok(NULL, " "));
_tmpp->fg = RGB(r, g, b);
} else
WARN("FG without PRIM first", 0);
} else if (CMD("BG")) {
// Set Background color
if (_tmpp) {
int r = atoi(strtok(NULL, " "));
int g = atoi(strtok(NULL, " "));
int b = atoi(strtok(NULL, " "));
_tmpp->bg = RGB(r, g, b);
} else
WARN("BG without PRIM first", 0);
} else {
WARN("unknown command %s", cmd);
}