From 41abfb196c524cf16107675b8e5b356827a04cd0 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Thu, 16 Jan 2025 04:57:15 +0100 Subject: Refactor --- graphics.c | 168 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 84 insertions(+), 84 deletions(-) (limited to 'graphics.c') diff --git a/graphics.c b/graphics.c index 6f786c5..40052ad 100644 --- a/graphics.c +++ b/graphics.c @@ -83,7 +83,7 @@ void draw_selection_cursor(Brush brush, f64 x, f64 y, f64 width, f64 height, f64 #ifndef GRAPHICS_IMPL_GUARD_ #define GRAPHICS_IMPL_GUARD_ -static f64 min3(f64 a, f64 b, f64 c) { +static f64 min3_(f64 a, f64 b, f64 c) { if (a < b && a < c) return a; if (b < c) @@ -91,7 +91,7 @@ static f64 min3(f64 a, f64 b, f64 c) { return c; } -static f64 max3(f64 a, f64 b, f64 c) { +static f64 max3_(f64 a, f64 b, f64 c) { if (a > b && a > c) return a; if (b > c) @@ -99,7 +99,7 @@ static f64 max3(f64 a, f64 b, f64 c) { return c; } -static b8 same_sign(f64 a, f64 b) { +static b8 same_sign_(f64 a, f64 b) { if (a >= EPSILON && b <= -EPSILON) return 0; if (a <= -EPSILON && b >= EPSILON) return 0; return 1; @@ -109,18 +109,18 @@ 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, }; -#define CHAR_NUM_BITS_X 6 -#define CHAR_NUM_BITS_Y 7 -#define CHAR_NUM_BITS (CHAR_NUM_BITS_X * CHAR_NUM_BITS_Y) +#define CHAR_NUM_BITS_X_ 6 +#define CHAR_NUM_BITS_Y_ 7 +#define CHAR_NUM_BITS_ (CHAR_NUM_BITS_X_ * CHAR_NUM_BITS_Y_) -static i64 char_column_offset(c32 c, i64 column_index) { - if (column_index < 0 || column_index >= CHAR_NUM_BITS_X) +static i64 char_column_offset_(c32 c, i64 column_index) { + if (column_index < 0 || column_index >= CHAR_NUM_BITS_X_) return -1; - return (c - 32) * CHAR_NUM_BITS + column_index * CHAR_NUM_BITS_Y; + return (c - 32) * CHAR_NUM_BITS_ + column_index * CHAR_NUM_BITS_Y_; } -static b8 char_bit(i64 column_offset, i64 row_index) { - if (column_offset < 0 || row_index < 0 || row_index >= CHAR_NUM_BITS_Y) +static b8 char_bit_(i64 column_offset, i64 row_index) { + if (column_offset < 0 || row_index < 0 || row_index >= CHAR_NUM_BITS_Y_) return 0; i64 bit_index = column_offset + row_index; @@ -131,34 +131,34 @@ static b8 char_bit(i64 column_offset, i64 row_index) { return !!(_bitfont[qword_index] & mask); } -static u64 char_column_convolved(c32 c, i64 column_index) { - if (column_index < 0 || column_index >= CHAR_NUM_BITS_X) +static u64 char_column_convolved_(c32 c, i64 column_index) { + if (column_index < 0 || column_index >= CHAR_NUM_BITS_X_) return 0; u64 column = 0; - i64 offset = char_column_offset(c, column_index); + i64 offset = char_column_offset_(c, column_index); - for (i64 y = 0; y < CHAR_NUM_BITS_Y; ++y) - if (char_bit(offset, y)) + for (i64 y = 0; y < CHAR_NUM_BITS_Y_; ++y) + if (char_bit_(offset, y)) column |= 3ull << y; return column; } -static b8 char_column_empty(c32 c, i64 column_index) { - if (column_index < 0 || column_index >= CHAR_NUM_BITS_X) +static b8 char_column_empty_(c32 c, i64 column_index) { + if (column_index < 0 || column_index >= CHAR_NUM_BITS_X_) return 1; - i64 offset = char_column_offset(c, column_index); + i64 offset = char_column_offset_(c, column_index); - for (i64 y = 0; y < CHAR_NUM_BITS_Y; ++y) - if (char_bit(offset, y)) + for (i64 y = 0; y < CHAR_NUM_BITS_Y_; ++y) + if (char_bit_(offset, y)) return 0; return 1; } -static i64 char_width(c32 c) { +static i64 char_width_(c32 c) { if (c < 32) return 0; if (c == ' ' || c > 127) @@ -166,22 +166,22 @@ static i64 char_width(c32 c) { i64 width = 0; - for (; width < CHAR_NUM_BITS_X; ++width) - if (char_column_empty(c, width) && char_column_empty(c, width + 1)) + for (; width < CHAR_NUM_BITS_X_; ++width) + if (char_column_empty_(c, width) && char_column_empty_(c, width + 1)) break; return width; } -static i64 char_spacing(i64 num_chars, c32 *text, i64 index) { +static i64 char_spacing_(i64 num_chars, c32 *text, i64 index) { if (text == NULL) return 0; if (index < 0 || index + 1 >= num_chars) return 0; - u64 a = char_column_convolved(text[index], char_width(text[index]) - 1); - u64 b = char_column_convolved(text[index + 1], 0); + u64 a = char_column_convolved_(text[index], char_width_(text[index]) - 1); + u64 b = char_column_convolved_(text[index + 1], 0); if (!!(a & b)) return 1; @@ -189,7 +189,7 @@ static i64 char_spacing(i64 num_chars, c32 *text, i64 index) { return 0; } -static i64 text_cursor(i64 num_chars, c32 *text) { +static i64 text_cursor_(i64 num_chars, c32 *text) { if (text == NULL) return 0; @@ -200,20 +200,20 @@ static i64 text_cursor(i64 num_chars, c32 *text) { if (text[i] == '\n') cursor = 0; else if (text[i] == '\b' && i > 0) - cursor -= char_width(text[i - 1]) + char_spacing(num_chars, text, i - 1); + cursor -= char_width_(text[i - 1]) + char_spacing_(num_chars, text, i - 1); else if (text[i] == '\r') cursor = 0; else - cursor += char_width(' ') + char_spacing(num_chars, text, i); + cursor += char_width_(' ') + char_spacing_(num_chars, text, i); continue; } - cursor += char_width(text[i]) + char_spacing(num_chars, text, i); + cursor += char_width_(text[i]) + char_spacing_(num_chars, text, i); } return cursor; } -static i64 enum_text_columns(i64 num_chars, c32 *text) { +static i64 enum_text_columns_(i64 num_chars, c32 *text) { if (text == NULL) return 0; @@ -229,16 +229,16 @@ static i64 enum_text_columns(i64 num_chars, c32 *text) { } else if (text[i] == '\b' && i > 0) { if (cols < n) cols = n; - n -= char_width(text[i - 1]) + char_spacing(num_chars, text, i - 1); + n -= char_width_(text[i - 1]) + char_spacing_(num_chars, text, i - 1); } else if (text[i] == '\r') { if (cols < n) cols = n; n = 0; } else - n += char_width(' ') + char_spacing(num_chars, text, i); + n += char_width_(' ') + char_spacing_(num_chars, text, i); continue; } - n += char_width(text[i]) + char_spacing(num_chars, text, i); + n += char_width_(text[i]) + char_spacing_(num_chars, text, i); } if (cols < n) @@ -247,7 +247,7 @@ static i64 enum_text_columns(i64 num_chars, c32 *text) { return cols; } -static i64 enum_text_rows(i64 num_chars, c32 *text) { +static i64 enum_text_rows_(i64 num_chars, c32 *text) { if (text == NULL) return 0; @@ -257,13 +257,13 @@ static i64 enum_text_rows(i64 num_chars, c32 *text) { if (i == num_chars || text[i] == '\n') { if (rows > 0) ++rows; - rows += CHAR_NUM_BITS_Y; + rows += CHAR_NUM_BITS_Y_; } return rows; } -static void brush_defaults(Brush *b) { +static void brush_defaults_(Brush *b) { if (g_platform.frame_width > 0) { if (b->scale.x == 0.) b->scale.x = ((f64) g_platform.frame_width) / g_platform.real_width; if (b->scale.y == 0.) b->scale.y = ((f64) g_platform.frame_height) / g_platform.real_height; @@ -276,11 +276,11 @@ static void brush_defaults(Brush *b) { } } -static void draw_text(Brush brush, f64 x_, f64 y_, f64 scale_x, f64 scale_y, i64 num_chars, c32 *text) { +static void draw_text_(Brush brush, f64 x_, f64 y_, f64 scale_x, f64 scale_y, i64 num_chars, c32 *text) { if (text == NULL) return; - brush_defaults(&brush); + brush_defaults_(&brush); x_ = brush.position.x + x_ * brush.scale.x; y_ = brush.position.y + y_ * brush.scale.y; @@ -294,24 +294,24 @@ static void draw_text(Brush brush, f64 x_, f64 y_, f64 scale_x, f64 scale_y, i64 f64 y = y_; f64 kx = scale_x; - f64 h = scale_y * CHAR_NUM_BITS_Y; + f64 h = scale_y * CHAR_NUM_BITS_Y_; for (i64 n = 0; n < num_chars; ++n) { if (text[n] <= ' ') { if (text[n] == '\n') { x = x_; - y += scale_y * (CHAR_NUM_BITS_Y + 1); + y += scale_y * (CHAR_NUM_BITS_Y_ + 1); } else if (text[n] == '\b' && n > 0) - x -= kx * (char_width(text[n - 1]) + char_spacing(num_chars, text, n - 1)); + x -= kx * (char_width_(text[n - 1]) + char_spacing_(num_chars, text, n - 1)); else if (text[n] == '\r') x = x_; else - x += kx * (char_width(' ') + char_spacing(num_chars, text, n)); + x += kx * (char_width_(' ') + char_spacing_(num_chars, text, n)); continue; } - i64 num_cols = char_width(text[n]); + i64 num_cols = char_width_(text[n]); f64 w = num_cols * kx; i64 i0 = (i64) floor(x); @@ -324,20 +324,20 @@ static void draw_text(Brush brush, f64 x_, f64 y_, f64 scale_x, f64 scale_y, i64 if (i >= g_platform.frame_width) break; i64 column = ((i - i0) * num_cols) / (i1 - i0); - i64 offset = char_column_offset(text[n], column); + i64 offset = char_column_offset_(text[n], column); for (i64 j = j0; j < j1; ++j) { if (j < 0) continue; if (j >= g_platform.frame_height) break; - i64 row = ((j - j0) * CHAR_NUM_BITS_Y) / (j1 - j0); + i64 row = ((j - j0) * CHAR_NUM_BITS_Y_) / (j1 - j0); - if (char_bit(offset, row)) + if (char_bit_(offset, row)) put_pixel(brush, i, j); } } - x += kx * (num_cols + char_spacing(num_chars, text, n)); + x += kx * (num_cols + char_spacing_(num_chars, text, n)); } } @@ -355,7 +355,7 @@ static f64 gamma_re_(f64 x) { // ================================================================ -vec3_f32 gamma_apply (vec3_f32 rgb) { +vec3_f32 gamma_apply(vec3_f32 rgb) { return (vec3_f32) { .x = (f32) gamma_(rgb.x), .y = (f32) gamma_(rgb.y), @@ -507,9 +507,9 @@ b8 triangle_contains(f64 x0, f64 y0, f64 x1, f64 y1, f64 x2, f64 y2, f64 px, f64 f64 pz2 = (px - x2) * (y1 - y2) - (x1 - x2) * (py - y2); return - same_sign(z0, pz0) - && same_sign(z1, pz1) - && same_sign(z2, pz2); + same_sign_(z0, pz0) + && same_sign_(z1, pz1) + && same_sign_(z2, pz2); } b8 ellipse_contains(f64 x0, f64 y0, f64 width, f64 height, f64 px, f64 py) { @@ -552,7 +552,7 @@ b8 line_contains(f64 x0, f64 y0, f64 x1, f64 y1, f64 width, f64 px, f64 py) { } void put_pixel(Brush brush, i64 x, i64 y) { - brush_defaults(&brush); + brush_defaults_(&brush); if (x < 0 || x >= brush.buffer.width || y < 0 || y >= brush.buffer.height) return; @@ -597,7 +597,7 @@ void put_pixel(Brush brush, i64 x, i64 y) { void draw_pixels(Brush brush, f64 x, f64 y, f64 width, f64 height, Pixel_Buffer src) { // FIXME PERF - brush_defaults(&brush); + brush_defaults_(&brush); f64 x0, y0, x1, y1; @@ -633,7 +633,7 @@ void draw_pixels(Brush brush, f64 x, f64 y, f64 width, f64 height, Pixel_Buffer } void fill_rectangle(Brush brush, f64 x, f64 y, f64 width, f64 height) { - brush_defaults(&brush); + brush_defaults_(&brush); f64 x0, y0, x1, y1; @@ -724,7 +724,7 @@ void fill_triangle(Brush brush, f64 x0, f64 y0, f64 x1, f64 y1, f64 x2, f64 y2) // FIXME PERF: // Implement better algorithm. - brush_defaults(&brush); + brush_defaults_(&brush); x0 = brush.position.x + x0 * brush.scale.x; y0 = brush.position.y + y0 * brush.scale.y; @@ -735,10 +735,10 @@ void fill_triangle(Brush brush, f64 x0, f64 y0, f64 x1, f64 y1, f64 x2, f64 y2) x2 = brush.position.x + x2 * brush.scale.x; y2 = brush.position.y + y2 * brush.scale.y; - i64 min_x = (i64) floor(min3(x0, x1, x2)); - i64 min_y = (i64) floor(min3(y0, y1, y2)); - i64 max_x = (i64) ceil (max3(x0, x1, x2)); - i64 max_y = (i64) ceil (max3(y0, y1, y2)); + i64 min_x = (i64) floor(min3_(x0, x1, x2)); + i64 min_y = (i64) floor(min3_(y0, y1, y2)); + i64 max_x = (i64) ceil (max3_(x0, x1, x2)); + i64 max_y = (i64) ceil (max3_(y0, y1, y2)); for (i64 j = min_y; j <= max_y; ++j) for (i64 i = min_x; i <= max_x; ++i) @@ -750,7 +750,7 @@ void fill_ellipse(Brush brush, f64 x, f64 y, f64 width, f64 height) { // FIXME PERF: // Implement better algorithm. - brush_defaults(&brush); + brush_defaults_(&brush); f64 x0, y0, x1, y1; @@ -806,8 +806,8 @@ void draw_text_area(Brush brush, f64 x, f64 y, f64 width, f64 height, f64 max_sc if (max_scale_x < EPSILON || max_scale_y < EPSILON) return; - i64 num_columns = enum_text_columns(num_chars, text); - i64 num_rows = enum_text_rows(num_chars, text); + i64 num_columns = enum_text_columns_(num_chars, text); + i64 num_rows = enum_text_rows_(num_chars, text); f64 scale_x = width / num_columns; f64 scale_y = height / num_rows; @@ -820,17 +820,17 @@ void draw_text_area(Brush brush, f64 x, f64 y, f64 width, f64 height, f64 max_sc kx = k * max_scale_x; ky = k * max_scale_y; - draw_text(brush, x, y, kx, ky, num_chars, text); + draw_text_(brush, x, y, kx, ky, num_chars, text); } void draw_selection_cursor(Brush brush, f64 x, f64 y, 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 < EPSILON || max_scale_y < EPSILON) return; - i64 num_columns = enum_text_columns(num_chars, text); - i64 num_rows = enum_text_rows(num_chars, text); - i64 cursor_x = text_cursor(cursor, text); - i64 cursor_y = enum_text_rows(cursor, text); + i64 num_columns = enum_text_columns_(num_chars, text); + i64 num_rows = enum_text_rows_(num_chars, text); + i64 cursor_x = text_cursor_(cursor, text); + i64 cursor_y = enum_text_rows_(cursor, text); f64 scale_x = width / num_columns; f64 scale_y = height / num_rows; @@ -847,54 +847,54 @@ void draw_selection_cursor(Brush brush, f64 x, f64 y, f64 width, f64 height, f64 i64 selection_x, selection_y; if (selection > 0) { - selection_x = text_cursor(cursor + selection, text); - selection_y = enum_text_rows(cursor + selection, text); + selection_x = text_cursor_(cursor + selection, text); + selection_y = enum_text_rows_(cursor + selection, text); } else { selection_x = cursor_x; selection_y = cursor_y; - cursor_x = text_cursor(cursor + selection, text); - cursor_y = enum_text_rows(cursor + selection, text); + cursor_x = text_cursor_(cursor + selection, text); + cursor_y = enum_text_rows_(cursor + selection, text); } if (cursor_y == selection_y) fill_rectangle( brush, x + kx * cursor_x, - y + ky * cursor_y - ky * (CHAR_NUM_BITS_Y + 1), + y + ky * cursor_y - ky * (CHAR_NUM_BITS_Y_ + 1), kx * (selection_x - cursor_x), - ky * (CHAR_NUM_BITS_Y + 1) + ky * (CHAR_NUM_BITS_Y_ + 1) ); else { fill_rectangle( brush, x + kx * cursor_x, - y + ky * cursor_y - ky * (CHAR_NUM_BITS_Y + 1), + y + ky * cursor_y - ky * (CHAR_NUM_BITS_Y_ + 1), kx * (num_columns - cursor_x), - ky * (CHAR_NUM_BITS_Y + 1) + ky * (CHAR_NUM_BITS_Y_ + 1) ); - for (i64 j = cursor_y + CHAR_NUM_BITS_Y + 1; j < selection_y; j += CHAR_NUM_BITS_Y + 1) + for (i64 j = cursor_y + CHAR_NUM_BITS_Y_ + 1; j < selection_y; j += CHAR_NUM_BITS_Y_ + 1) fill_rectangle( brush, x, - y + ky * j - ky * (CHAR_NUM_BITS_Y + 1), + y + ky * j - ky * (CHAR_NUM_BITS_Y_ + 1), kx * num_columns, - ky * (CHAR_NUM_BITS_Y + 1) + ky * (CHAR_NUM_BITS_Y_ + 1) ); fill_rectangle( brush, x, - y + ky * selection_y - ky * (CHAR_NUM_BITS_Y + 1), + y + ky * selection_y - ky * (CHAR_NUM_BITS_Y_ + 1), kx * selection_x, - ky * (CHAR_NUM_BITS_Y + 1) + ky * (CHAR_NUM_BITS_Y_ + 1) ); } } else fill_rectangle( brush, x + kx * cursor_x, - y + ky * cursor_y - ky * CHAR_NUM_BITS_Y, + y + ky * cursor_y - ky * CHAR_NUM_BITS_Y_, kx * .5, - ky * (CHAR_NUM_BITS_Y - 1) + ky * (CHAR_NUM_BITS_Y_ - 1) ); } -- cgit v1.2.3