summaryrefslogtreecommitdiff
path: root/graphics.c
diff options
context:
space:
mode:
Diffstat (limited to 'graphics.c')
-rwxr-xr-xgraphics.c185
1 files changed, 92 insertions, 93 deletions
diff --git a/graphics.c b/graphics.c
index c89e746..4fa8a49 100755
--- a/graphics.c
+++ b/graphics.c
@@ -12,23 +12,25 @@
#/ Self-testing shell script
#/
#/ ================================================================
+
SRC=${0##*./}
BIN=${SRC%.*}
-gcc \
- -Wall -Wextra -Werror -pedantic \
- -Wno-missing-braces \
- -Wno-old-style-declaration \
- -Wno-overlength-strings \
- -O3 -D NDEBUG \
- -D NO_WAYLAND=1 \
- -D GRAPHICS_TEST_SUITE \
- -lX11 -lm -lasound \
- -o $BIN $SRC && \
+gcc \
+ -Wall -Wextra -Werror -pedantic \
+ -Wno-missing-braces \
+ -Wno-old-style-declaration \
+ -Wno-overlength-strings \
+ -O3 -D NDEBUG \
+ -D ENABLE_WAYLAND=0 \
+ -D GRAPHICS_TEST_SUITE \
+ -lX11 -lm -lasound \
+ -o $BIN $SRC && \
./$BIN $@
STATUS=$?
rm -f $BIN
-exit $? # */
+exit $STATUS # */
#endif
+
// ================================================================
#ifndef GRAPHICS_HEADER_GUARD_
@@ -107,25 +109,25 @@ vec3_f32 rgb_f32_from_u32(u32 color);
u32 rgba_u32_from_f32(vec4_f32 color);
vec4_f32 rgba_f32_from_u32(u32 color);
-b8 rectangle_contains(f64 x0, f64 y0, f64 width, f64 height, f64 px, f64 py);
-b8 triangle_contains (f64 x0, f64 y0, f64 x1, f64 y1, f64 x2, f64 y2, f64 px, f64 py);
-b8 quad_contains (f64 x0, f64 y0, f64 x1, f64 y1, f64 x2, f64 y2, f64 x3, f64 y3, f64 px, f64 py);
-b8 ellipse_contains (f64 x0, f64 y0, f64 width, f64 height, f64 px, f64 py);
-b8 line_contains (f64 x0, f64 y0, f64 x1, f64 y1, f64 width, f64 px, f64 py);
+b8 hit_rectangle(f64 x0, f64 y0, f64 width, f64 height, f64 px, f64 py);
+b8 hit_triangle (f64 x0, f64 y0, f64 x1, f64 y1, f64 x2, f64 y2, f64 px, f64 py);
+b8 hit_quad (f64 x0, f64 y0, f64 x1, f64 y1, f64 x2, f64 y2, f64 x3, f64 y3, f64 px, f64 py);
+b8 hit_ellipse (f64 x0, f64 y0, f64 width, f64 height, f64 px, f64 py);
+b8 hit_line (f64 x0, f64 y0, f64 x1, f64 y1, f64 width, f64 px, f64 py);
// ================================================================
enum {
- OP_NONE = 0,
- OP_REQUESTS,
- OP_PIXELS,
- OP_RECTANGLE,
- OP_TRIANGLE,
- OP_QUAD,
- OP_ELLIPSE,
- OP_LINE,
- OP_TEXT_AREA,
- OP_TEXT_CURSOR,
+ GRAPHICS_OP_NONE = 0,
+ GRAPHICS_OP_REQUESTS,
+ GRAPHICS_OP_PIXELS,
+ GRAPHICS_OP_RECTANGLE,
+ GRAPHICS_OP_TRIANGLE,
+ GRAPHICS_OP_QUAD,
+ GRAPHICS_OP_ELLIPSE,
+ GRAPHICS_OP_LINE,
+ GRAPHICS_OP_TEXT_AREA,
+ GRAPHICS_OP_TEXT_CURSOR,
};
typedef struct {
@@ -135,12 +137,12 @@ typedef struct {
f64 height;
} Box;
-typedef struct gx_request {
+typedef struct graphics_request_ {
u16 op;
union {
struct {
- i64 num;
- struct gx_request *values;
+ i64 num;
+ struct graphics_request_ *values;
} requests;
struct {
Box area;
@@ -184,21 +186,16 @@ typedef struct gx_request {
c32 * text;
} text_cursor;
};
-} Gx_Request;
-
-typedef struct {
- i64 x;
- i64 y;
- Pixel_Buffer buffer;
-} Gx_Baked;
+} Graphics_Request;
typedef struct {
- i64 sketch_size;
- vec4_f32 *sketch;
- vec2 scale;
-} Gx_Context;
+ i64 sketch_size;
+ vec4_f32 * sketch;
+ vec2 scale;
+ Pixel_Buffer dst;
+} Graphics_Context;
-void gx_render(Gx_Context context, Pixel_Buffer dst, Gx_Request req);
+void perform_graphics_reqeust(Graphics_Context context, Graphics_Request req);
#endif // GRAPHICS_HEADER_GUARD_
@@ -437,13 +434,13 @@ void fill_triangle_raw(Pixel_Buffer dst, vec4_f32 color, f64 x0, f64 y0, f64 x1,
i64 right = i0;
for (i64 i = i0; i <= i1; ++i)
- if (triangle_contains(x0, y0, x1, y1, x2, y2, (f64) i, (f64) j)) {
+ if (hit_triangle(x0, y0, x1, y1, x2, y2, (f64) i, (f64) j)) {
left = i;
break;
}
for (i64 i = i1; i >= i0; --i)
- if (triangle_contains(x0, y0, x1, y1, x2, y2, (f64) i, (f64) j)) {
+ if (hit_triangle(x0, y0, x1, y1, x2, y2, (f64) i, (f64) j)) {
right = i;
break;
}
@@ -471,13 +468,13 @@ void fill_quad_raw(Pixel_Buffer dst, vec4_f32 color, f64 x0, f64 y0, f64 x1, f64
i64 right = i0;
for (i64 i = i0; i <= i1; ++i)
- if (quad_contains(x0, y0, x1, y1, x2, y2, x3, y3, (f64) i, (f64) j)) {
+ if (hit_quad(x0, y0, x1, y1, x2, y2, x3, y3, (f64) i, (f64) j)) {
left = i;
break;
}
for (i64 i = i1; i >= i0; --i)
- if (quad_contains(x0, y0, x1, y1, x2, y2, x3, y3, (f64) i, (f64) j)) {
+ if (hit_quad(x0, y0, x1, y1, x2, y2, x3, y3, (f64) i, (f64) j)) {
right = i;
break;
}
@@ -1089,11 +1086,11 @@ vec4_f32 rgba_f32_from_u32(u32 color) {
};
}
-b8 rectangle_contains(f64 x0, f64 y0, f64 width, f64 height, f64 px, f64 py) {
+b8 hit_rectangle(f64 x0, f64 y0, f64 width, f64 height, f64 px, f64 py) {
return px >= x0 && px < x0 + width && py >= y0 && py < y0 + height;
}
-b8 triangle_contains(f64 x0, f64 y0, f64 x1, f64 y1, f64 x2, f64 y2, f64 px, f64 py) {
+b8 hit_triangle(f64 x0, f64 y0, f64 x1, f64 y1, f64 x2, f64 y2, f64 px, f64 py) {
// Z-components of cross-products
//
@@ -1118,12 +1115,12 @@ b8 triangle_contains(f64 x0, f64 y0, f64 x1, f64 y1, f64 x2, f64 y2, f64 px, f64
&& same_sign_(z2, pz2);
}
-b8 quad_contains(f64 x0, f64 y0, f64 x1, f64 y1, f64 x2, f64 y2, f64 x3, f64 y3, f64 px, f64 py) {
- return triangle_contains(x0, y0, x1, y1, x2, y2, px, py)
- || triangle_contains(x0, y0, x2, y2, x3, y3, px, py);
+b8 hit_quad(f64 x0, f64 y0, f64 x1, f64 y1, f64 x2, f64 y2, f64 x3, f64 y3, f64 px, f64 py) {
+ return hit_triangle(x0, y0, x1, y1, x2, y2, px, py)
+ || hit_triangle(x0, y0, x2, y2, x3, y3, px, py);
}
-b8 ellipse_contains(f64 x0, f64 y0, f64 width, f64 height, f64 px, f64 py) {
+b8 hit_ellipse(f64 x0, f64 y0, f64 width, f64 height, f64 px, f64 py) {
f64 dw = width / 2;
f64 dh = height / 2;
@@ -1141,7 +1138,7 @@ b8 ellipse_contains(f64 x0, f64 y0, f64 width, f64 height, f64 px, f64 py) {
return dx * dx + dy * dy - 1.0 < EPSILON;
}
-b8 line_contains(f64 x0, f64 y0, f64 x1, f64 y1, f64 width, f64 px, f64 py) {
+b8 hit_line(f64 x0, f64 y0, f64 x1, f64 y1, f64 width, f64 px, f64 py) {
f64 dx = x1 - x0;
f64 dy = y1 - y0;
@@ -1157,21 +1154,21 @@ b8 line_contains(f64 x0, f64 y0, f64 x1, f64 y1, f64 width, f64 px, f64 py) {
tx *= width * .5;
ty *= width * .5;
- return quad_contains(x0 - tx, y0 - ty, x0 + tx, y0 + ty, x1 + tx, y1 + ty, x1 - tx, y1 - ty, px, py);
+ return hit_quad(x0 - tx, y0 - ty, x0 + tx, y0 + ty, x1 + tx, y1 + ty, x1 - tx, y1 - ty, px, py);
}
// ================================================================
-void gx_render_raw_(Gx_Context context, Pixel_Buffer dst, Gx_Request req) {
+void perform_raw_graphics_request_(Graphics_Context context, Graphics_Request req) {
switch (req.op) {
- case OP_REQUESTS:
+ case GRAPHICS_OP_REQUESTS:
for (i64 i = 0; i < req.requests.num; ++i)
- gx_render_raw_(context, dst, req.requests.values[i]);
+ perform_raw_graphics_request_(context, req.requests.values[i]);
break;
- case OP_PIXELS:
+ case GRAPHICS_OP_PIXELS:
draw_pixels_raw(
- dst,
+ context.dst,
req.pixels.area.x,
req.pixels.area.y,
req.pixels.area.width,
@@ -1180,9 +1177,9 @@ void gx_render_raw_(Gx_Context context, Pixel_Buffer dst, Gx_Request req) {
);
break;
- case OP_RECTANGLE:
+ case GRAPHICS_OP_RECTANGLE:
fill_rectangle_raw(
- dst,
+ context.dst,
req.rectangle.color,
req.rectangle.area.x,
req.rectangle.area.y,
@@ -1191,9 +1188,9 @@ void gx_render_raw_(Gx_Context context, Pixel_Buffer dst, Gx_Request req) {
);
break;
- case OP_TRIANGLE:
+ case GRAPHICS_OP_TRIANGLE:
fill_triangle_raw(
- dst,
+ context.dst,
req.triangle.color,
req.triangle.vertices[0].x,
req.triangle.vertices[0].y,
@@ -1204,9 +1201,9 @@ void gx_render_raw_(Gx_Context context, Pixel_Buffer dst, Gx_Request req) {
);
break;
- case OP_QUAD:
+ case GRAPHICS_OP_QUAD:
fill_quad_raw(
- dst,
+ context.dst,
req.quad.color,
req.quad.vertices[0].x,
req.quad.vertices[0].y,
@@ -1219,9 +1216,9 @@ void gx_render_raw_(Gx_Context context, Pixel_Buffer dst, Gx_Request req) {
);
break;
- case OP_ELLIPSE:
+ case GRAPHICS_OP_ELLIPSE:
fill_ellipse_raw(
- dst,
+ context.dst,
req.ellipse.color,
req.ellipse.area.x,
req.ellipse.area.y,
@@ -1230,9 +1227,9 @@ void gx_render_raw_(Gx_Context context, Pixel_Buffer dst, Gx_Request req) {
);
break;
- case OP_LINE:
+ case GRAPHICS_OP_LINE:
fill_line_raw(
- dst,
+ context.dst,
req.line.color,
req.line.vertices[0].x,
req.line.vertices[0].y,
@@ -1242,9 +1239,9 @@ void gx_render_raw_(Gx_Context context, Pixel_Buffer dst, Gx_Request req) {
);
break;
- case OP_TEXT_AREA:
+ case GRAPHICS_OP_TEXT_AREA:
draw_text_area_raw(
- dst,
+ context.dst,
req.text_area.color,
req.text_area.area.x,
req.text_area.area.y,
@@ -1257,9 +1254,9 @@ void gx_render_raw_(Gx_Context context, Pixel_Buffer dst, Gx_Request req) {
);
break;
- case OP_TEXT_CURSOR:
+ case GRAPHICS_OP_TEXT_CURSOR:
draw_text_cursor_raw(
- dst,
+ context.dst,
req.text_cursor.color,
req.text_cursor.area.x,
req.text_cursor.area.y,
@@ -1278,30 +1275,30 @@ void gx_render_raw_(Gx_Context context, Pixel_Buffer dst, Gx_Request req) {
}
}
-Gx_Request gx_scaled_(Gx_Request req, vec2 scale) {
- Gx_Request scaled = req;
+Graphics_Request graphics_request_scaled_(Graphics_Request req, vec2 scale) {
+ Graphics_Request scaled = req;
switch (req.op) {
- case OP_REQUESTS:
+ case GRAPHICS_OP_REQUESTS:
for (i64 i = 0; i < req.requests.num; ++i)
- scaled.requests.values[i] = gx_scaled_(req.requests.values[i], scale);
+ scaled.requests.values[i] = graphics_request_scaled_(req.requests.values[i], scale);
break;
- case OP_PIXELS:
+ case GRAPHICS_OP_PIXELS:
scaled.pixels.area.x *= scale.x;
scaled.pixels.area.y *= scale.y;
scaled.pixels.area.width *= scale.x;
scaled.pixels.area.height *= scale.y;
break;
- case OP_RECTANGLE:
+ case GRAPHICS_OP_RECTANGLE:
scaled.rectangle.area.x *= scale.x;
scaled.rectangle.area.y *= scale.y;
scaled.rectangle.area.width *= scale.x;
scaled.rectangle.area.height *= scale.y;
break;
- case OP_TRIANGLE:
+ case GRAPHICS_OP_TRIANGLE:
scaled.triangle.vertices[0].x *= scale.x;
scaled.triangle.vertices[0].y *= scale.y;
scaled.triangle.vertices[1].x *= scale.x;
@@ -1310,7 +1307,7 @@ Gx_Request gx_scaled_(Gx_Request req, vec2 scale) {
scaled.triangle.vertices[2].y *= scale.y;
break;
- case OP_QUAD:
+ case GRAPHICS_OP_QUAD:
scaled.quad.vertices[0].x *= scale.x;
scaled.quad.vertices[0].y *= scale.y;
scaled.quad.vertices[1].x *= scale.x;
@@ -1321,14 +1318,14 @@ Gx_Request gx_scaled_(Gx_Request req, vec2 scale) {
scaled.quad.vertices[3].y *= scale.y;
break;
- case OP_ELLIPSE:
+ case GRAPHICS_OP_ELLIPSE:
scaled.ellipse.area.x *= scale.x;
scaled.ellipse.area.y *= scale.y;
scaled.ellipse.area.width *= scale.x;
scaled.ellipse.area.height *= scale.y;
break;
- case OP_LINE:
+ case GRAPHICS_OP_LINE:
scaled.line.vertices[0].x *= scale.x;
scaled.line.vertices[0].y *= scale.y;
scaled.line.vertices[1].x *= scale.x;
@@ -1351,7 +1348,7 @@ Gx_Request gx_scaled_(Gx_Request req, vec2 scale) {
break;
- case OP_TEXT_AREA:
+ case GRAPHICS_OP_TEXT_AREA:
scaled.text_area.area.x *= scale.x;
scaled.text_area.area.y *= scale.y;
scaled.text_area.area.width *= scale.x;
@@ -1360,7 +1357,7 @@ Gx_Request gx_scaled_(Gx_Request req, vec2 scale) {
scaled.text_area.max_size.y *= scale.y;
break;
- case OP_TEXT_CURSOR:
+ case GRAPHICS_OP_TEXT_CURSOR:
scaled.text_cursor.area.x *= scale.x;
scaled.text_cursor.area.y *= scale.y;
scaled.text_cursor.area.width *= scale.x;
@@ -1375,21 +1372,23 @@ Gx_Request gx_scaled_(Gx_Request req, vec2 scale) {
return scaled;
}
-void gx_render_(Gx_Context context, Pixel_Buffer dst, Gx_Request req) {
- gx_render_raw_(context, dst, gx_scaled_(req, context.scale));
+void scale_and_perform_graphics_request_(Graphics_Context context, Graphics_Request req) {
+ perform_raw_graphics_request_(context, graphics_request_scaled_(req, context.scale));
}
-void gx_render(Gx_Context context, Pixel_Buffer dst, Gx_Request req) {
+// ================================================================
+
+void perform_graphics_request(Graphics_Context context, Graphics_Request req) {
if (context.sketch == NULL) {
context.sketch_size = MAX_NUM_PIXELS;
context.sketch = g_platform.sketch;
}
- if (dst.pixels == NULL) {
- dst.width = g_platform.frame_width;
- dst.height = g_platform.frame_height;
- dst.stride = g_platform.frame_width;
- dst.pixels = g_platform.pixels;
+ if (context.dst.pixels == NULL) {
+ context.dst.width = g_platform.frame_width;
+ context.dst.height = g_platform.frame_height;
+ context.dst.stride = g_platform.frame_width;
+ context.dst.pixels = g_platform.pixels;
if (context.scale.x == 0. && context.scale.y == 0.)
context.scale = (vec2) {
@@ -1398,7 +1397,7 @@ void gx_render(Gx_Context context, Pixel_Buffer dst, Gx_Request req) {
};
}
- gx_render_(context, dst, req);
+ scale_and_perform_graphics_request_(context, req);
}
// ================================================================