summaryrefslogtreecommitdiff
path: root/graphics.c
diff options
context:
space:
mode:
Diffstat (limited to 'graphics.c')
-rw-r--r--graphics.c64
1 files changed, 24 insertions, 40 deletions
diff --git a/graphics.c b/graphics.c
index 44fee52..a853e39 100644
--- a/graphics.c
+++ b/graphics.c
@@ -29,13 +29,12 @@ b8 triangle_contains (f64 x0, f64 y0, f64 x1, f64 y1, f64 x2, f64 y2, f64 px, f6
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);
-u32 u32_from_rgb (f32 red, f32 green, f32 blue);
-void fill_rectangle (u32 op, u32 color, f64 x0, f64 y0, f64 width, f64 height);
-void fill_triangle (u32 op, u32 color, f64 x0, f64 y0, f64 x1, f64 y1, f64 x2, f64 y2);
-void fill_ellipse (u32 op, u32 color, f64 x0, f64 y0, f64 width, f64 height);
-void fill_line (u32 op, u32 color, f64 x0, f64 y0, f64 x1, f64 y1, f64 width);
-void draw_text_area (u32 color, f64 x0, f64 y0, f64 width, f64 height, f64 max_scale_x, f64 max_scale_y, i64 num_chars, c32 *text);
-void draw_selection_cursor(u32 color, f64 x0, f64 y0, f64 width, f64 height, f64 max_scale_x, f64 max_scale_y, i64 cursor, i64 selection, i64 num_chars, c32 *text);
+void fill_rectangle (u32 op, vec3_f32 color, f64 x0, f64 y0, f64 width, f64 height);
+void fill_triangle (u32 op, vec3_f32 color, f64 x0, f64 y0, f64 x1, f64 y1, f64 x2, f64 y2);
+void fill_ellipse (u32 op, vec3_f32 color, f64 x0, f64 y0, f64 width, f64 height);
+void fill_line (u32 op, vec3_f32 color, f64 x0, f64 y0, f64 x1, f64 y1, f64 width);
+void draw_text_area (vec3_f32 color, f64 x0, f64 y0, f64 width, f64 height, f64 max_scale_x, f64 max_scale_y, i64 num_chars, c32 *text);
+void draw_selection_cursor(vec3_f32 color, f64 x0, f64 y0, f64 width, f64 height, f64 max_scale_x, f64 max_scale_y, i64 cursor, i64 selection, i64 num_chars, c32 *text);
#endif // GRAPHICS_HEADER_GUARD_
@@ -67,21 +66,6 @@ b8 same_sign(f64 a, f64 b) {
return 1;
}
-u32 u32_from_rgb(f32 red, f32 green, f32 blue) {
- i32 r = (i32) floor(red * 255.f);
- i32 g = (i32) floor(green * 255.f);
- i32 b = (i32) floor(blue * 255.f);
-
- if (r < 0) r = 0;
- if (r > 255) r = 255;
- if (g < 0) g = 0;
- if (g > 255) g = 255;
- if (b < 0) b = 0;
- if (b > 255) b = 255;
-
- return (r << 16) | (g << 8) | b;
-}
-
static u64 _bitfont[] = {
0xbc0000000000, 0xc00300000, 0x5fd5040093f24fc9, 0xa00a2c2a1a280105, 0xc000415e6f, 0x400000020be0000, 0x1c38a8400000007d, 0x40002043e1020215, 0x408102000000010, 0x9800000000020002, 0xf913e00000033, 0x53200000207c8800, 0x3654880000099, 0x54b800000f840e00, 0xe953c000001a, 0x953e000000674080, 0x1e54b800000f, 0x490000000000240, 0x88a08000000, 0x20a220050a142850, 0x6520800000, 0x912f801eab260be, 0x800034952bf0001f, 0xc850bf0000921427, 0xf00010a54afc0003, 0xd29427800002142b, 0x840007e1023f0000, 0x7d09100000217e, 0x3f000188a08fc000, 0xc30c0cfc00000810, 0x27803f101013f00f, 0xc244bf0000f214, 0x4bf0002f21427800, 0xc254a480006c24, 0x407c00102fc08100, 0xf208080f0000fa0, 0x531007d81c607c0, 0xc208288c031141, 0x83fc00046954b10, 0x180e03000000, 0x41040000000ff04, 0x8102040810000404, 0x2a54600000000101, 0x309123e0000e, 0xc912180000a22447, 0x8000062a54700007, 0xe52a4300000029f0, 0xa0000602043e0001, 0x1d48000002074, 0x1f000003610f8000, 0x13e04f800000010, 0x470000780813e00f, 0x184893e0000e224, 0x23e0001f12243000, 0x82a54100000008, 0x40780000009f0200, 0xe208080e0001f20, 0xa22007981860780, 0x82082888022282, 0x16c200004ca95320, 0x7f000004, 0x408200000086d04, 0x8204,
};
@@ -240,7 +224,7 @@ i64 enum_text_rows(i64 num_chars, c32 *text) {
return rows;
}
-void draw_text(u32 color, f64 x0, f64 y0, f64 scale_x, f64 scale_y, i64 num_chars, c32 *text) {
+void draw_text(vec3_f32 color, f64 x0, f64 y0, f64 scale_x, f64 scale_y, i64 num_chars, c32 *text) {
if (text == NULL)
return;
@@ -275,19 +259,19 @@ void draw_text(u32 color, f64 x0, f64 y0, f64 scale_x, f64 scale_y, i64 num_char
for (i64 i = i0; i < i1; ++i) {
if (i < 0) continue;
- if (i >= platform.frame_width) break;
+ if (i >= g_platform.frame_width) break;
i64 column = ((i - i0) * num_cols) / (i1 - i0);
i64 offset = char_column_offset(text[n], column);
for (i64 j = j0; j < j1; ++j) {
if (j < 0) continue;
- if (j >= platform.frame_height) break;
+ if (j >= g_platform.frame_height) break;
i64 row = ((j - j0) * CHAR_NUM_BITS_Y) / (j1 - j0);
if (char_bit(offset, row))
- platform.pixels[j * platform.frame_width + i] = color;
+ g_platform.pixels[j * g_platform.frame_width + i] = color;
}
}
@@ -295,13 +279,13 @@ void draw_text(u32 color, f64 x0, f64 y0, f64 scale_x, f64 scale_y, i64 num_char
}
}
-void put_pixel(i64 i, i64 j, u32 op, u32 color) {
- if (i < 0 || i >= platform.frame_width || j < 0 || j >= platform.frame_height)
+void put_pixel(i64 i, i64 j, u32 op, vec3_f32 color) {
+ if (i < 0 || i >= g_platform.frame_width || j < 0 || j >= g_platform.frame_height)
return;
if (op == OP_XOR)
- platform.pixels[j * platform.frame_width + i] ^= color;
+ g_platform.pixels[j * g_platform.frame_width + i] = rgb_f32_from_u32(rgb_u32_from_f32(color) ^ rgb_u32_from_f32(g_platform.pixels[j * g_platform.frame_width + i]));
else
- platform.pixels[j * platform.frame_width + i] = color;
+ g_platform.pixels[j * g_platform.frame_width + i] = color;
}
b8 rectangle_contains(f64 x0, f64 y0, f64 width, f64 height, f64 px, f64 py) {
@@ -365,7 +349,7 @@ b8 line_contains(f64 x0, f64 y0, f64 x1, f64 y1, f64 width, f64 px, f64 py) {
|| triangle_contains(x0 - tx, y0 - ty, x1 + tx, y1 + ty, x1 - tx, y1 - ty, px, py);
}
-void fill_rectangle(u32 op, u32 color, f64 x0, f64 y0, f64 width, f64 height) {
+void fill_rectangle(u32 op, vec3_f32 color, f64 x0, f64 y0, f64 width, f64 height) {
i64 i0 = (i64) floor(x0 + .5);
i64 j0 = (i64) floor(y0 + .5);
i64 i1 = (i64) floor(x0 + width + .5);
@@ -373,15 +357,15 @@ void fill_rectangle(u32 op, u32 color, f64 x0, f64 y0, f64 width, f64 height) {
if (i0 < 0) i0 = 0;
if (j0 < 0) j0 = 0;
- if (i1 >= platform.frame_width) i1 = platform.frame_width - 1;
- if (j1 >= platform.frame_height) j1 = platform.frame_height - 1;
+ if (i1 >= g_platform.frame_width) i1 = g_platform.frame_width - 1;
+ if (j1 >= g_platform.frame_height) j1 = g_platform.frame_height - 1;
for (i64 j = j0; j < j1; ++j)
for (i64 i = i0; i < i1; ++i)
put_pixel(i, j, op, color);
}
-void fill_triangle(u32 op, u32 color, f64 x0, f64 y0, f64 x1, f64 y1, f64 x2, f64 y2) {
+void fill_triangle(u32 op, vec3_f32 color, f64 x0, f64 y0, f64 x1, f64 y1, f64 x2, f64 y2) {
// FIXME PERF:
// Implement better algorithm.
@@ -396,7 +380,7 @@ void fill_triangle(u32 op, u32 color, f64 x0, f64 y0, f64 x1, f64 y1, f64 x2, f6
put_pixel(i, j, op, color);
}
-void fill_ellipse(u32 op, u32 color, f64 x0, f64 y0, f64 width, f64 height) {
+void fill_ellipse(u32 op, vec3_f32 color, f64 x0, f64 y0, f64 width, f64 height) {
// FIXME PERF:
// Implement better algorithm.
@@ -406,10 +390,10 @@ void fill_ellipse(u32 op, u32 color, f64 x0, f64 y0, f64 width, f64 height) {
i64 j1 = (i64) floor(y0 + height + .5);
for (i64 j = j0; j < j1; ++j) {
- if (j < 0 || j >= platform.frame_height)
+ if (j < 0 || j >= g_platform.frame_height)
continue;
for (i64 i = i0; i < i1; ++i) {
- if (i < 0 || i >= platform.frame_width)
+ if (i < 0 || i >= g_platform.frame_width)
continue;
if (ellipse_contains(x0, y0, width, height, (f64) i, (f64) j))
put_pixel(i, j, op, color);
@@ -417,7 +401,7 @@ void fill_ellipse(u32 op, u32 color, f64 x0, f64 y0, f64 width, f64 height) {
}
}
-void fill_line(u32 op, u32 color, f64 x0, f64 y0, f64 x1, f64 y1, f64 width) {
+void fill_line(u32 op, vec3_f32 color, f64 x0, f64 y0, f64 x1, f64 y1, f64 width) {
f64 dx = x1 - x0;
f64 dy = y1 - y0;
@@ -437,7 +421,7 @@ void fill_line(u32 op, u32 color, f64 x0, f64 y0, f64 x1, f64 y1, f64 width) {
fill_triangle(op, color, x0 - tx, y0 - ty, x1 + tx, y1 + ty, x1 - tx, y1 - ty);
}
-void draw_text_area(u32 color, f64 x0, f64 y0, f64 width, f64 height, f64 max_scale_x, f64 max_scale_y, i64 num_chars, c32 *text) {
+void draw_text_area(vec3_f32 color, f64 x0, f64 y0, f64 width, f64 height, f64 max_scale_x, f64 max_scale_y, i64 num_chars, c32 *text) {
if (max_scale_x < 1e-6 || max_scale_y < 1e-6)
return;
@@ -458,7 +442,7 @@ void draw_text_area(u32 color, f64 x0, f64 y0, f64 width, f64 height, f64 max_sc
draw_text(color, x0, y0, kx, ky, num_chars, text);
}
-void draw_selection_cursor(u32 color, f64 x0, f64 y0, f64 width, f64 height, f64 max_scale_x, f64 max_scale_y, i64 cursor, i64 selection, i64 num_chars, c32 *text) {
+void draw_selection_cursor(vec3_f32 color, f64 x0, f64 y0, f64 width, f64 height, f64 max_scale_x, f64 max_scale_y, i64 cursor, i64 selection, i64 num_chars, c32 *text) {
if (max_scale_x < 1e-6 || max_scale_y < 1e-6)
return;