diff options
-rwxr-xr-x | build_all.sh | 18 | ||||
-rw-r--r-- | examples/game_of_life.c | 4 | ||||
-rw-r--r-- | examples/graph.c | 26 | ||||
-rw-r--r-- | examples/julia_set.c | 6 | ||||
-rw-r--r-- | examples/labyrinth.c | 4 | ||||
-rw-r--r-- | examples/particles.c | 9 | ||||
-rw-r--r-- | examples/pixels.c | 2 | ||||
-rw-r--r-- | examples/sinewave.c | 18 | ||||
-rw-r--r-- | examples/ui.c | 50 | ||||
-rw-r--r-- | graphics.c | 8 | ||||
-rwxr-xr-x | reduced_system_layer.c | 19 |
11 files changed, 85 insertions, 79 deletions
diff --git a/build_all.sh b/build_all.sh index 8fae799..dd6da5e 100755 --- a/build_all.sh +++ b/build_all.sh @@ -14,13 +14,13 @@ if [ ! -d ./bin ]; then mkdir ./bin fi -# gcc $FLAGS -o ./bin/graph ./examples/graph.c -# gcc $FLAGS -o ./bin/particles ./examples/particles.c -# gcc $FLAGS -o ./bin/julia_set ./examples/julia_set.c -# gcc $FLAGS -o ./bin/game_of_life ./examples/game_of_life.c -# gcc $FLAGS -o ./bin/labyrinth ./examples/labyrinth.c -# gcc $FLAGS -o ./bin/sinewave ./examples/sinewave.c +gcc $FLAGS -o ./bin/graph ./examples/graph.c +gcc $FLAGS -o ./bin/particles ./examples/particles.c +gcc $FLAGS -o ./bin/julia_set ./examples/julia_set.c +gcc $FLAGS -o ./bin/game_of_life ./examples/game_of_life.c +gcc $FLAGS -o ./bin/labyrinth ./examples/labyrinth.c +gcc $FLAGS -o ./bin/sinewave ./examples/sinewave.c gcc $FLAGS -o ./bin/pixels ./examples/pixels.c -# gcc $FLAGS -o ./bin/ui ./examples/ui.c -# gcc $FLAGS -o ./bin/echo ./examples/echo.c -# gcc $FLAGS -o ./bin/proto ./examples/proto.c +gcc $FLAGS -o ./bin/ui ./examples/ui.c +gcc $FLAGS -o ./bin/echo ./examples/echo.c +gcc $FLAGS -o ./bin/proto ./examples/proto.c diff --git a/examples/game_of_life.c b/examples/game_of_life.c index fdd3d54..d95fb9e 100644 --- a/examples/game_of_life.c +++ b/examples/game_of_life.c @@ -11,9 +11,7 @@ i32 main(i32 argc, c8 **argv) { (void) argv; g_platform = (Platform) { - .title = "Conway's Game of Life", - .frame_width = 960, - .frame_height = 720, + .title = "Conway's Game of Life", }; p_event_loop(); diff --git a/examples/graph.c b/examples/graph.c index f03cc93..99f916d 100644 --- a/examples/graph.c +++ b/examples/graph.c @@ -53,16 +53,15 @@ void draw_node(i64 node_index) { Node n = world.nodes[node_index]; - vec3_f32 color = {0}; // black color + vec4_f32 color = { 0.f, 0.f, 0.f, 1.f }; // black color if (n.highlight) - color = (vec3_f32) { 1.f, .0f, 1.f }; // pink color + color = (vec4_f32) { 1.f, .0f, 1.f, 1.f }; // pink color if (n.hover) - color = (vec3_f32) { 0.f, .5f, 0.f }; // green color + color = (vec4_f32) { 0.f, .5f, 0.f, 1.f }; // green color fill_ellipse( - OP_SET, // set pixels - color, + (Brush) { .color = color }, n.x - n.radius, n.y - n.radius, n.radius * 2, @@ -78,16 +77,15 @@ void draw_edge(i64 edge_index) { Node n0 = world.nodes[e.src]; Node n1 = world.nodes[e.dst]; - vec3_f32 color = { .5f, .5f, .5f }; // grey color + vec4_f32 color = { .5f, .5f, .5f, 1.f }; // grey color if (e.highlight) - color = (vec3_f32) { 1.f, .0f, 1.f }; // pink color + color = (vec4_f32) { 1.f, .0f, 1.f, 1.f }; // pink color if (e.hover) - color = (vec3_f32) { 0.f, .5f, 0.f }; // green color + color = (vec4_f32) { 0.f, .5f, 0.f, 1.f }; // green color fill_line( - OP_SET, // set pixels - color, + (Brush) { .color = color }, n0.x, n0.y, n1.x, @@ -504,7 +502,7 @@ void update_and_render_frame(void) { // Render - fill_rectangle(OP_SET, (vec3_f32) { .7f, .8f, .9f }, 0, 0, g_platform.frame_width, g_platform.frame_height); + fill_rectangle((Brush) { .color = { .7f, .8f, .9f, 1.f } }, 0, 0, g_platform.frame_width, g_platform.frame_height); if (adding_edge) { f64 x0 = world.nodes[adding_src].x; @@ -517,7 +515,7 @@ void update_and_render_frame(void) { y1 = world.nodes[adding_dst].y; } - fill_line(OP_SET, (vec3_f32) { .5f, 0.f, .5f }, x0, y0, x1, y1, 30); + fill_line((Brush) { .color = { .5f, 0.f, .5f, 1.f } }, x0, y0, x1, y1, 30); } draw_graph(); @@ -530,9 +528,7 @@ i32 main(i32 argc, c8 **argv) { (void) argv; g_platform = (Platform) { - .title = "Graph", - .frame_width = 960, - .frame_height = 720, + .title = "Graph", }; add_node(100, 100); diff --git a/examples/julia_set.c b/examples/julia_set.c index 90e4b45..e50a246 100644 --- a/examples/julia_set.c +++ b/examples/julia_set.c @@ -1,4 +1,4 @@ -#include "../reduced_system_layer.c" +#include "../graphics.c" i64 p = 4; f64 view_x = 0.; @@ -71,7 +71,7 @@ void update_and_render_frame(void) { for (i32 jj = 0; jj < p; ++jj) for (i32 ii = 0; ii < p; ++ii) - g_platform.pixels[(j + jj) * g_platform.frame_width + (i + ii)] = rgb_f32_from_u32(c); + g_platform.pixels[(j + jj) * g_platform.frame_width + (i + ii)] = with_alpha(rgb_f32_from_u32(c), 1.f); } p_render_frame(); @@ -83,8 +83,6 @@ i32 main(i32 argc, c8 **argv) { g_platform = (Platform) { .title = "Julia Set", - .frame_width = 960, - .frame_height = 720, .exact_resolution = 1, }; diff --git a/examples/labyrinth.c b/examples/labyrinth.c index 54bed19..01af7c6 100644 --- a/examples/labyrinth.c +++ b/examples/labyrinth.c @@ -11,9 +11,7 @@ i32 main(i32 argc, c8 **argv) { (void) argv; g_platform = (Platform) { - .title = "Labyrinth", - .frame_width = 960, - .frame_height = 720, + .title = "Labyrinth", }; p_event_loop(); diff --git a/examples/particles.c b/examples/particles.c index 37a77a5..03c51f7 100644 --- a/examples/particles.c +++ b/examples/particles.c @@ -14,7 +14,7 @@ typedef struct { } World; World world = {0}; -vec3_f32 background = { .0f, .0f, .08f }; +vec4_f32 background = { .0f, .0f, .08f, 1.f }; i64 time_0; void update_and_render_frame(void) { @@ -100,7 +100,7 @@ void update_and_render_frame(void) { if (i < 0 || i >= g_platform.frame_width) continue; if ((i - x) * (i - x) + (j - y) * (j - y) > 100) continue; f64 v = (e->vx * e->vx + e->vy * e->vy) * 8.; - g_platform.pixels[j * g_platform.frame_width + i] = (vec3_f32) { -.2 + v * 2., .1 + v * .7, 1. - v }; + g_platform.pixels[j * g_platform.frame_width + i] = (vec4_f32) { -.2 + v * 2., .1 + v * .7, 1. - v, 1.f }; } } } @@ -113,9 +113,8 @@ i32 main(i32 argc, c8 **argv) { (void) argv; g_platform = (Platform) { - .title = "Gravity", - .frame_width = 960, - .frame_height = 720, + .title = "Particles", + .exact_resolution = 1, }; time_0 = p_time(); diff --git a/examples/pixels.c b/examples/pixels.c index c47ac35..0579361 100644 --- a/examples/pixels.c +++ b/examples/pixels.c @@ -19,7 +19,7 @@ void update_and_render_frame(void) { Brush white = RGB(1.f, 1.f, 1.f); Brush black = RGB(0.f, 0.f, 0.f); - black.antialiasing = 1; + white.quick = 1; fill_rectangle(white, 0., 0., g_platform.real_width, g_platform.real_height); fill_rectangle(black, x, y, 40., 40.); diff --git a/examples/sinewave.c b/examples/sinewave.c index 3a127ae..4f8bbe0 100644 --- a/examples/sinewave.c +++ b/examples/sinewave.c @@ -3,7 +3,7 @@ i64 time_0 = 0; i64 audio_samples = 0; -f32 frames[AUDIO_SAMPLE_RATE * AUDIO_NUM_CHANNELS] = {0}; +f32 frames[SOUND_SAMPLE_RATE * NUM_SOUND_CHANNELS] = {0}; b8 ui_button(f64 x, f64 y, f64 width, f64 height) { b8 has_cursor = g_platform.cursor_x >= x && g_platform.cursor_x < x + width && @@ -12,11 +12,11 @@ b8 ui_button(f64 x, f64 y, f64 width, f64 height) { b8 is_pressed = has_cursor && g_platform.key_down[BUTTON_LEFT]; if (is_pressed) - fill_rectangle(OP_SET, (vec3_f32) { 1.f, 1.f, 1.f }, x, y, width, height); + fill_rectangle(RGB(1.f, 1.f, 1.f), x, y, width, height); else if (has_cursor) - fill_rectangle(OP_SET, (vec3_f32) { .8f, .8f, 0.f }, x, y, width, height); + fill_rectangle(RGB(.8f, .8f, 0.f), x, y, width, height); else - fill_rectangle(OP_SET, (vec3_f32) { .8f, .8f, .2f }, x, y, width, height); + fill_rectangle(RGB(.8f, .8f, .2f), x, y, width, height); return has_cursor && g_platform.key_pressed[BUTTON_LEFT]; } @@ -25,13 +25,13 @@ void update_and_render_frame(void) { i32 num_events = p_handle_events(); if (num_events > 0) { - fill_rectangle(OP_SET, (vec3_f32) { .1f, .1f, .1f }, 0, 0, g_platform.frame_width, g_platform.frame_height); + fill_rectangle(RGB(.1f, .1f, .1f), 0, 0, g_platform.frame_width, g_platform.frame_height); if (ui_button(100, 100, 200, 200)) - p_queue_sound(0, AUDIO_SAMPLE_RATE, frames); + p_queue_sound(0, SOUND_SAMPLE_RATE, frames); } - i64 samples_elapsed = ((p_time() - time_0) * AUDIO_SAMPLE_RATE) / 1000 - audio_samples; + i64 samples_elapsed = ((p_time() - time_0) * SOUND_SAMPLE_RATE) / 1000 - audio_samples; audio_samples += samples_elapsed; p_handle_audio(samples_elapsed); @@ -50,8 +50,8 @@ i32 main(i32 argc, c8 **argv) { f64 frequency = 440.; - for (i64 i = 0; i < AUDIO_SAMPLE_RATE; ++i) { - f64 t = ((f64) i) / AUDIO_SAMPLE_RATE; + for (i64 i = 0; i < SOUND_SAMPLE_RATE; ++i) { + f64 t = ((f64) i) / SOUND_SAMPLE_RATE; f64 x = sin(t * (M_PI * 2.) * frequency); if (t < .005) x *= t / .005; diff --git a/examples/ui.c b/examples/ui.c index 389e2b4..8f1294a 100644 --- a/examples/ui.c +++ b/examples/ui.c @@ -1,3 +1,4 @@ +#define MAX_FRAME_DURATION 100 #include "../graphics.c" b8 button_0_down = 0; @@ -13,19 +14,27 @@ i64 selection = 0; void update_and_render_frame(void) { p_wait_events(); - for (i32 j = 0; j < g_platform.frame_height; ++j) - for (i32 i = 0; i < g_platform.frame_width; ++i) - g_platform.pixels[j * g_platform.frame_width + i] = (vec3_f32) { .2f, .1f, 0.f }; + Brush background = RGB(.2f, .1f, 0.f); + background.quick = 1; + + Brush white = RGB(1.f, 1.f, 1.f); + Brush green = RGB(0.f, 1.f, 0.f); + Brush dark_green = RGB(.1f, .5f, .1f); + Brush red = RGB(1.f, 0.f, 0.f); + Brush light_red = RGB(1.f, .5f, .5f); + Brush light_green = RGB(.5f, 1.f, .5f); + + fill_rectangle(background, 0, 0, g_platform.real_width, g_platform.real_height); if (g_platform.cursor_x >= 40 && g_platform.cursor_x < 100 && g_platform.cursor_y >= 40 && g_platform.cursor_y < 100) { button_0_down = g_platform.key_down[BUTTON_LEFT]; if (button_0_down) - fill_rectangle(OP_SET, (vec3_f32) { 1.f, 1.f, 1.f }, 40, 40, 60, 60); + fill_rectangle(white, 40, 40, 60, 60); else - fill_rectangle(OP_SET, (vec3_f32) { 0.f, 1.f, 0.f }, 40, 40, 60, 60); + fill_rectangle(green, 40, 40, 60, 60); } else { button_0_down = 0; - fill_rectangle(OP_SET, (vec3_f32) { .1f, .5f, .1f }, 40, 40, 60, 60); + fill_rectangle(dark_green, 40, 40, 60, 60); } if (g_platform.cursor_x >= 40 && g_platform.cursor_x < 100 && g_platform.cursor_y >= 120 && g_platform.cursor_y < 180) { @@ -33,23 +42,23 @@ void update_and_render_frame(void) { if (g_platform.key_pressed[BUTTON_LEFT]) button_1_checked = !button_1_checked; if (button_1_down) - fill_rectangle(OP_SET, (vec3_f32) { 1.f, 1.f, 1.f }, 40, 120, 60, 60); + fill_rectangle(white, 40, 120, 60, 60); else if (button_1_checked) - fill_rectangle(OP_SET, (vec3_f32) { 1.f, .5f, .5f }, 40, 120, 60, 60); + fill_rectangle(light_red, 40, 120, 60, 60); else - fill_rectangle(OP_SET, (vec3_f32) { .5f, 1.f, .5f }, 40, 120, 60, 60); + fill_rectangle(light_green, 40, 120, 60, 60); } else { button_1_down = 0; if (button_1_checked) - fill_rectangle(OP_SET, (vec3_f32) { 1.f, 0.f, 0.f }, 40, 120, 60, 60); + fill_rectangle(red, 40, 120, 60, 60); else - fill_rectangle(OP_SET, (vec3_f32) { 0.f, 1.f, 0.f }, 40, 120, 60, 60); + fill_rectangle(green, 40, 120, 60, 60); } - i64 w = g_platform.frame_width / 2; - i64 h = g_platform.frame_height / 2; - i64 x0 = w / 2; - i64 y0 = h / 2; + f64 w = g_platform.real_width / 2.0; + f64 h = g_platform.real_height / 2.0; + f64 x0 = w / 2.0; + f64 y0 = h / 2.0; vec3_f32 color = { 1.f, .5f, .5f }; @@ -204,9 +213,9 @@ void update_and_render_frame(void) { } } - draw_text_area((vec3_f32) {0}, x0 + 8, y0 - 8, w, h, 10., 10., text_len, text); - draw_text_area(color, x0, y0, w, h, 10., 10., text_len, text); - draw_selection_cursor((vec3_f32) { 1.f, 1.f, 1.f }, x0, y0, w, h, 10., 10., cursor, selection, text_len, text); + draw_text_area(RGB(0.f, 0.f, 0.f), x0 + 8, y0 - 8, w, h, 10., 10., text_len, text); + draw_text_area((Brush) { .color = with_alpha(color, 1.f) }, x0, y0, w, h, 10., 10., text_len, text); + draw_selection_cursor((Brush) { .xor_color = 1, .color = { 1.f, 1.f, 1.f, 1.f } }, x0, y0, w, h, 10., 10., cursor, selection, text_len, text); p_render_frame(); } @@ -216,10 +225,7 @@ i32 main(i32 argc, c8 **argv) { (void) argv; g_platform = (Platform) { - .title = "UI", - .frame_width = 960, - .frame_height = 720, - .exact_resolution = 1, + .title = "UI", }; p_event_loop(); @@ -33,9 +33,9 @@ typedef struct { Pixel_Buffer buffer; vec2 position; vec2 scale; - b8 antialiasing : 1; - b8 alpha : 1; - b8 xor_color : 1; + b8 quick : 1; + b8 alpha : 1; + b8 xor_color : 1; vec4_f32 color; } Brush; @@ -605,7 +605,7 @@ void fill_rectangle(Brush brush, f64 x, f64 y, f64 width, f64 height) { y1 = y0 + height * brush.scale.y; } - if (brush.antialiasing) { + if (!brush.quick) { i64 i0 = (i64) ceil (x0); i64 i1 = (i64) floor(x1); i64 j0 = (i64) ceil (y0); diff --git a/reduced_system_layer.c b/reduced_system_layer.c index 3d020bd..522d672 100755 --- a/reduced_system_layer.c +++ b/reduced_system_layer.c @@ -1254,10 +1254,21 @@ void p_init(void) { i32 display_width = DisplayWidth (_display, screen); i32 display_height = DisplayHeight(_display, screen); - if (g_platform.frame_width <= 0) - g_platform.frame_width = 400; - if (g_platform.frame_height <= 0) - g_platform.frame_height = 300; + if (g_platform.frame_width <= 0 || g_platform.frame_height <= 0) { + if (display_width >= 1680 && display_height >= 1020) { + g_platform.frame_width = 1280; + g_platform.frame_height = 720; + } else if (display_width >= 800 && display_height >= 600) { + g_platform.frame_width = display_width - 400; + g_platform.frame_height = display_height - 300; + } else if (display_width >= 400 && display_height >= 300) { + g_platform.frame_width = 400; + g_platform.frame_height = 300; + } else { + g_platform.frame_width = display_width; + g_platform.frame_height = display_height; + } + } i32 x = (display_width - g_platform.frame_width) / 2; i32 y = (display_height - g_platform.frame_height) / 2; |