]> git.bts.cx Git - benzene.git/blob - src/bz/gfx/drawing.c
Initial version
[benzene.git] / src / bz / gfx / drawing.c
1 #include <bz/gfx/drawing.h>
2
3 #include <bz/math/math.h>
4 #include <bz/gfx/gfx.h>
5 #include <bz/gfx/tilemap.h>
6 #include <string.h>
7
8 #define DRAW_DATA(name, members, ...) \
9 struct data##name { members; }; typedef struct data##name data##name; \
10 static void dispatch##name(void *userParam) { data##name *data = (data##name *)userParam; name(__VA_ARGS__); } \
11
12 #define DRAW_DATA_DISPATCH(drawQueue, name, ...) \
13 if (drawQueue != NULL) { \
14 data##name *data = (data##name *)bzGfxDrawQueueEnqueue(drawQueue, sortIdx, dispatch##name, sizeof(data##name)); \
15 *data = (data##name){ __VA_ARGS__ }; \
16 } else { \
17 name(__VA_ARGS__); \
18 }
19
20 DRAW_DATA(bzPSet, float x; float y; int c, data->x, data->y, data->c)
21 DRAW_DATA(bzCls, uint8_t c, data->c)
22 DRAW_DATA(bzCamera, float x; float y, data->x, data->y)
23 DRAW_DATA(bzCirc, int x; int y; int r; int c, data->x, data->y, data->r, data->c)
24 DRAW_DATA(bzCircFill, int x; int y; int r; int c, data->x, data->y, data->r, data->c)
25 DRAW_DATA(bzLine, int x0; int y0; int x1; int y1; int c, data->x0, data->y0, data->x1, data->y1, data->c)
26 DRAW_DATA(bzRect, int x0; int y0; int x1; int y1; int c, data->x0, data->y0, data->x1, data->y1, data->c)
27 DRAW_DATA(bzRectFill, int x0; int y0; int x1; int y1; int c, data->x0, data->y0, data->x1, data->y1, data->c)
28 DRAW_DATA(bzTriangle, int x0; int y0; int x1; int y1; int x2; int y2; int c, data->x0, data->y0, data->x1, data->y1, data->x2, data->y2, data->c)
29 DRAW_DATA(bzSpr, int n; int x; int y, data->n, data->x, data->y)
30 DRAW_DATA(bzSprExt, int n; int x; int y; int w; int h; bool flipX; bool flipY, data->n, data->x, data->y, data->w, data->h, data->flipX, data->flipY)
31 DRAW_DATA(bzSSpr, int sx; int sy; int sw; int sh; int dx; int dy, data->sx, data->sy, data->sw, data->sh, data->dx, data->dy)
32 DRAW_DATA(bzSSprExt, int sx; int sy; int sw; int sh; int dx; int dy; int dw; int dh; bool flipX; bool flipY, data->sx, data->sy, data->sw, data->sh, data->dx, data->dy, data->dw, data->dh, data->flipX, data->flipY)
33 DRAW_DATA(bzPrint, int x; int y; int c; char string[], data->x, data->y, data->c, data->string)
34 DRAW_DATA(bzFillP, uint16_t p, data->p)
35 DRAW_DATA(bzSetPaletteColor, int palette; int colorIdx; int color, data->palette, data->colorIdx, data->color)
36 DRAW_DATA(bzSetOutputBuffer, int idx, data->idx)
37 DRAW_DATA(bzGfxMap, BZTilemapID tilemap; int tx; int ty; int x; int y; int mw; int mh; BZIdentifierHash tag, data->tilemap, data->tx, data->ty, data->x, data->y, data->mw, data->mh, data->tag)
38
39 void bzDrawPSet(BZDrawQueueID drawQueue, uint8_t sortIdx, const BZMatrix *mtx, float x, float y, int c) {
40 BZVector pos = bzVectorMake(x, y);
41 bzMatrixTransformVector(&pos, &pos, mtx);
42 DRAW_DATA_DISPATCH(drawQueue, bzPSet, pos.x, pos.y, c)
43 }
44
45 void bzDrawCls(BZDrawQueueID drawQueue, uint8_t sortIdx, const BZMatrix *mtx, uint8_t c) {
46 DRAW_DATA_DISPATCH(drawQueue, bzCls, c)
47 }
48
49 void bzDrawCamera(BZDrawQueueID drawQueue, uint8_t sortIdx, const BZMatrix *mtx, float x, float y) {
50 DRAW_DATA_DISPATCH(drawQueue, bzCamera, x, y)
51 }
52
53 void bzDrawCirc(BZDrawQueueID drawQueue, uint8_t sortIdx, const BZMatrix *mtx, int x, int y, int r, int c) {
54 BZVector pos = bzVectorMake(x, y);
55 BZVector radius = bzVectorMake(r, 0);
56 bzMatrixTransformVector(&pos, &pos, mtx);
57 bzMatrixScaleRotateVector(&radius, &radius, mtx);
58 DRAW_DATA_DISPATCH(drawQueue, bzCirc, pos.x, pos.y, bzVectorMagnitude(&radius), c) // FIXME, RADIUS
59 }
60
61 void bzDrawCircFill(BZDrawQueueID drawQueue, uint8_t sortIdx, const BZMatrix *mtx, int x, int y, int r, int c) {
62 BZVector pos = bzVectorMake(x, y);
63 BZVector radius = bzVectorMake(r, 0);
64 bzMatrixTransformVector(&pos, &pos, mtx);
65 bzMatrixScaleRotateVector(&radius, &radius, mtx);
66 DRAW_DATA_DISPATCH(drawQueue, bzCircFill, pos.x, pos.y, bzVectorMagnitude(&radius), c) // FIXME, RADIUS
67 }
68
69 void bzDrawLine(BZDrawQueueID drawQueue, uint8_t sortIdx, const BZMatrix *mtx, int x0, int y0, int x1, int y1, int c) {
70 BZVector pos0 = bzVectorMake(x0, y0);
71 BZVector pos1 = bzVectorMake(x1, y1);
72 bzMatrixTransformVector(&pos0, &pos0, mtx);
73 bzMatrixTransformVector(&pos1, &pos1, mtx);
74 DRAW_DATA_DISPATCH(drawQueue, bzLine, pos0.x, pos0.y, pos1.x, pos1.y, c)
75 }
76
77 void bzDrawRect(BZDrawQueueID drawQueue, uint8_t sortIdx, const BZMatrix *mtx, int x0, int y0, int x1, int y1, int c) {
78 BZVector pos0 = bzVectorMake(x0, y0);
79 BZVector pos1 = bzVectorMake(x1, y1);
80 bzMatrixTransformVector(&pos0, &pos0, mtx);
81 bzMatrixTransformVector(&pos1, &pos1, mtx);
82 DRAW_DATA_DISPATCH(drawQueue, bzRect, pos0.x, pos0.y, pos1.x, pos1.y, c) // FIXME, rotation??
83 }
84
85 void bzDrawRectFill(BZDrawQueueID drawQueue, uint8_t sortIdx, const BZMatrix *mtx, int x0, int y0, int x1, int y1, int c) {
86 BZVector pos0 = bzVectorMake(x0, y0);
87 BZVector pos1 = bzVectorMake(x1, y1);
88 bzMatrixTransformVector(&pos0, &pos0, mtx);
89 bzMatrixTransformVector(&pos1, &pos1, mtx);
90 DRAW_DATA_DISPATCH(drawQueue, bzRectFill, pos0.x, pos0.y, pos1.x, pos1.y, c) // FIXME, rotation??
91 }
92
93 void bzDrawTriangle(BZDrawQueueID drawQueue, uint8_t sortIdx, const BZMatrix *mtx, int x0, int y0, int x1, int y1, int x2, int y2, int c) {
94 BZVector pos0 = bzVectorMake(x0, y0);
95 BZVector pos1 = bzVectorMake(x1, y1);
96 BZVector pos2 = bzVectorMake(x2, y2);
97 bzMatrixTransformVector(&pos0, &pos0, mtx);
98 bzMatrixTransformVector(&pos1, &pos1, mtx);
99 bzMatrixTransformVector(&pos2, &pos2, mtx);
100 DRAW_DATA_DISPATCH(drawQueue, bzTriangle, pos0.x, pos0.y, pos1.x, pos1.y, pos2.x, pos2.y, c)
101 }
102
103 void bzDrawSpr(BZDrawQueueID drawQueue, uint8_t sortIdx, const BZMatrix *mtx, int n, int x, int y) {
104 DRAW_DATA_DISPATCH(drawQueue, bzSpr, n, x, y)
105 }
106
107 void bzDrawSprExt(BZDrawQueueID drawQueue, uint8_t sortIdx, const BZMatrix *mtx, int n, int x, int y, int w, int h, bool flipX, bool flipY) {
108 DRAW_DATA_DISPATCH(drawQueue, bzSprExt, n, x, y, w, h, flipX, flipY)
109 }
110
111 void bzDrawSSpr(BZDrawQueueID drawQueue, uint8_t sortIdx, const BZMatrix *mtx, int sx, int sy, int sw, int sh, int dx, int dy) {
112 DRAW_DATA_DISPATCH(drawQueue, bzSSpr, sx, sy, sw, sh, dx, dy)
113 }
114
115 void bzDrawSSprExt(BZDrawQueueID drawQueue, uint8_t sortIdx, const BZMatrix *mtx, int sx, int sy, int sw, int sh, int dx, int dy, int dw, int dh, bool flipX, bool flipY) {
116 DRAW_DATA_DISPATCH(drawQueue, bzSSprExt, sx, sy, sw, sh, dx, dy, dw, dh, flipX, flipY)
117 }
118
119 void bzDrawPrint(BZDrawQueueID drawQueue, uint8_t sortIdx, const BZMatrix *mtx, int x, int y, int color, const char *text) {
120 BZVector pos = bzVectorMake(x, y);
121 bzMatrixTransformVector(&pos, &pos, mtx);
122 if (drawQueue != NULL) {
123 size_t dataSize = sizeof(databzPrint) + strlen(text);
124 databzPrint *data = (databzPrint *)bzGfxDrawQueueEnqueue(drawQueue, sortIdx, dispatchbzPrint, dataSize);
125 data->x = pos.x;
126 data->y = pos.y;
127 data->c = color;
128 strcpy(data->string, text);
129 } else {
130 bzPrint(pos.x, pos.y, color, text);
131 }
132 }
133
134 void bzDrawFillP(BZDrawQueueID drawQueue, uint8_t sortIdx, const BZMatrix *mtx, uint16_t p) {
135 DRAW_DATA_DISPATCH(drawQueue, bzFillP, p)
136 }
137
138 void bzDrawSetPaletteColor(BZDrawQueueID drawQueue, uint8_t sortIdx, const BZMatrix *mtx, int palette, int colorIdx, int color) {
139 DRAW_DATA_DISPATCH(drawQueue, bzSetPaletteColor, palette, colorIdx, color)
140 }
141
142 void bzDrawSetOutputBuffer(BZDrawQueueID drawQueue, uint8_t sortIdx, const BZMatrix *mtx, int idx) {
143 DRAW_DATA_DISPATCH(drawQueue, bzSetOutputBuffer, idx)
144 }
145
146 void bzDrawMap(BZDrawQueueID drawQueue, uint8_t sortIdx, const BZMatrix *mtx, BZTilemapID tilemap, int tx, int ty, int x, int y, int mw, int mh, BZIdentifierHash tag) {
147 DRAW_DATA_DISPATCH(drawQueue, bzGfxMap, tilemap, tx, ty, x, y, mw, mh, tag)
148 }