diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2025-03-05 23:44:57 +0100 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2025-03-05 23:44:57 +0100 |
commit | 504fc8344f4c83391ea7755908030413ed04d772 (patch) | |
tree | dfc3208e804c10dd0ca31fdd3ff529e8a6b171ba | |
parent | dc4e55de19fc8ed144100cdc99105dcd904e0c65 (diff) | |
download | reduced_system_layer-504fc8344f4c83391ea7755908030413ed04d772.zip |
Update comments; Typedefs
-rwxr-xr-x | reduced_system_layer.c | 142 | ||||
-rw-r--r-- | test.c | 29 |
2 files changed, 86 insertions, 85 deletions
diff --git a/reduced_system_layer.c b/reduced_system_layer.c index 3d2996e..f50bf45 100755 --- a/reduced_system_layer.c +++ b/reduced_system_layer.c @@ -44,7 +44,6 @@ #/ - Clipboard #/ - Images - BMP, PPM #/ - Sound - WAV -#/ - Logging #/ - Graphics perf - request cache #/ Requires: #/ + [ ] Graphics tests @@ -60,8 +59,10 @@ #/ - Labyrinth #/ - Chat #/ - Graphics +#/ - Custom fonts - integrate stb_truetype.h #/ - UI #/ - Icons +#/ - Folder widget #/ - Cross-platform networking - UDP + TCP + WebSocket #/ Requires: #/ - [ ] Sockets - UDP, TCP @@ -77,11 +78,14 @@ #/ - Improve microbenchmarks library #/ - Parsing #/ - Printing +#/ - Logging +#/ - Terminal colors #/ - Big integer #/ - Mersenne Twister 64 #/ - Arithmetic coding #/ - Graphics #/ - Vector math +#/ - Bezier curves #/ - CMYK color #/ - Textures #/ - Vulkan boilerplate @@ -198,18 +202,21 @@ typedef unsigned char b8; typedef float f32; typedef double f64; -typedef struct { f64 x, y; } vec2; -typedef struct { f64 x, y, z; } vec3; -typedef struct { f64 x, y, z, w; } vec4; -typedef struct { f32 x, y; } vec2_f32; -typedef struct { f32 x, y, z; } vec3_f32; -typedef struct { f32 x, y, z, w; } vec4_f32; -typedef struct { i64 x, y; } vec2_i64; -typedef struct { i64 x, y, z; } vec3_i64; -typedef struct { i64 x, y, z, w; } vec4_i64; -typedef struct { f64 v[ 4]; } mat2; -typedef struct { f64 v[ 9]; } mat3; -typedef struct { f64 v[16]; } mat4; +typedef union { struct { f64 v[ 2]; }; struct { f64 x, y; }; } vec2; +typedef union { struct { f64 v[ 3]; }; struct { f64 x, y, z; }; } vec3; +typedef union { struct { f64 v[ 4]; }; struct { f64 x, y, z, w; }; } vec4; +typedef union { struct { f32 v[ 2]; }; struct { f32 x, y; }; } vec2_f32; +typedef union { struct { f32 v[ 3]; }; struct { f32 x, y, z; }; } vec3_f32; +typedef union { struct { f32 v[ 4]; }; struct { f32 x, y, z, w; }; } vec4_f32; +typedef union { struct { i64 v[ 2]; }; struct { i64 x, y; }; } vec2_i64; +typedef union { struct { i64 v[ 3]; }; struct { i64 x, y, z; }; } vec3_i64; +typedef union { struct { i64 v[ 4]; }; struct { i64 x, y, z, w; }; } vec4_i64; +typedef union { struct { f64 v[ 4]; }; struct { f64 m[2][2]; }; } mat2; +typedef union { struct { f64 v[ 9]; }; struct { f64 m[3][3]; }; } mat3; +typedef union { struct { f64 v[16]; }; struct { f64 m[4][4]; }; } mat4; +typedef union { struct { f32 v[ 4]; }; struct { f32 m[2][2]; }; } mat2_f32; +typedef union { struct { f32 v[ 9]; }; struct { f32 m[3][3]; }; } mat3_f32; +typedef union { struct { f32 v[16]; }; struct { f32 m[4][4]; }; } mat4_f32; #endif // TYPES_HEADER_GUARD_ @@ -320,6 +327,7 @@ i32 main(i32 argc, c8 **argv); #define PRIMARY_SOUND_SAMPLE_RATE 44100 #endif +// FIXME: Implement dynamic primary sound buffer. #ifndef MAX_NUM_PRIMARY_SOUND_FRAMES #define MAX_NUM_PRIMARY_SOUND_FRAMES (10 * PRIMARY_SOUND_SAMPLE_RATE * NUM_PRIMARY_SOUND_CHANNELS) #endif @@ -611,14 +619,17 @@ typedef struct { i32 frame_height; f64 pixel_size; i32 antialiasing_scale; - b8 exact_resolution : 1; - b8 graceful_shutdown : 1; - - b8 done : 1; - b8 has_focus : 1; - b8 has_cursor : 1; - b8 files_dragged : 1; - b8 files_dropped : 1; + b8 exact_resolution : 1; + b8 graceful_shutdown : 1; + b8 enable_clipboard_text : 1; + b8 enable_clipboard_image : 1; + b8 enable_clipboard_sound : 1; + + b8 done : 1; + b8 has_focus : 1; + b8 has_cursor : 1; + b8 files_dragged : 1; + b8 files_dropped : 1; i32 real_width; i32 real_height; i32 cursor_x; @@ -688,6 +699,7 @@ void handle_primary_sound(void); void queue_primary_sound(i64 delay_in_samples, i64 num_samples, f32 *frames); // Networking +b8 network_open(u16 slot, Network_Address address, u16 *local_port); i64 network_recv(u16 slot, Network_Address address, i64 size, u8 *data, u16 *local_port, Network_Address *remote_address); i64 network_send(u16 slot, Network_Address address, i64 size, u8 *data, u16 *local_port); @@ -1438,7 +1450,7 @@ static i64 average_frame_duration_(i64 duration) { for (i64 i = 0; i < NUM_FRAMES_AVERAGED; ++i) durs[i] = _frame_duration[i]; - // FIXME: Use better sorting algorithm, e.g. merge sort. + // FIXME, PERF: Use better sorting algorithm, e.g. merge sort. for (i64 i = 0; i < (i64) NUM_FRAMES_AVERAGED; ++i) for (i64 j = i + 1; j < NUM_FRAMES_AVERAGED; ++j) if (durs[i] < durs[j]) { @@ -1591,6 +1603,10 @@ static void convert_pixels_for_window_(void) { } // Downscale pixels from vec4_f32 pixel buffer to internal u32 pixel buffer. + // + // FIXME, PERF: - Read all pixels sequentially, store the intermediate + // sum for each row in a temporary buffer. + // - Check if the scale is 1:1. { i64 dst_width = g_platform.frame_width / aa_scale; i64 dst_height = g_platform.frame_height / aa_scale; @@ -1814,7 +1830,7 @@ static void network_cleanup_(void) { resize_dynamic_array_exact(&_num_sockets, (void **) &_sockets, sizeof *_sockets, 0); } -static b8 network_open_(u16 slot, Network_Address address, u16 *local_port) { +b8 network_open(u16 slot, Network_Address address, u16 *local_port) { network_init_(slot); b8 change_address = @@ -1893,11 +1909,11 @@ static b8 network_open_(u16 slot, Network_Address address, u16 *local_port) { i64 network_recv(u16 slot, Network_Address address, i64 size, u8 *data, u16 *local_port, Network_Address *remote_address) { if (address.protocol != IPv4_UDP && address.protocol != IPv6_UDP) { LOG_ERROR("Invalid address protocol: %d", (i32) (u32) address.protocol); - return 0; + return -1; } - if (!network_open_(slot, address, local_port)) - return 0; + if (!network_open(slot, address, local_port)) + return -1; if (size <= 0) return 0; @@ -1929,7 +1945,7 @@ i64 network_recv(u16 slot, Network_Address address, i64 size, u8 *data, u16 *loc return 0; LOG_ERROR("recvfrom failed (errno %d).", errno); - return 0; + return -1; } if (remote_address != NULL) { @@ -1951,14 +1967,14 @@ i64 network_recv(u16 slot, Network_Address address, i64 size, u8 *data, u16 *loc i64 network_send(u16 slot, Network_Address address, i64 size, u8 *data, u16 *local_port) { if (address.protocol != IPv4_UDP && address.protocol != IPv6_UDP) { LOG_ERROR("Invalid address protocol: %d", (i32) (u32) address.protocol); - return 0; + return -1; } Network_Address local_address = address; local_address.port = 0; - if (!network_open_(slot, local_address, local_port)) - return 0; + if (!network_open(slot, local_address, local_port)) + return -1; if (size <= 0) return 0; @@ -1998,7 +2014,7 @@ i64 network_send(u16 slot, Network_Address address, i64 size, u8 *data, u16 *loc return 0; LOG_ERROR("sendto failed (errno %d).", errno); - return 0; + return -1; } return sent; @@ -3057,6 +3073,16 @@ void shutdown_all_systems(void) { resize_dynamic_array_exact(&g_platform.clipboard_text_len, (void **) &g_platform.clipboard_text, 1, 0); } +void request_clipboard_(void) { + if (_requested_clipboard) + return; + if (!g_platform.enable_clipboard_text && !g_platform.enable_clipboard_image && !g_platform.enable_clipboard_sound) + return; + + XConvertSelection(_display, _clipboard, _targets, _clipboard, _window, CurrentTime); + _requested_clipboard = 1; +} + i32 handle_main_window_events(void) { if (_display == NULL) return 0; @@ -3120,10 +3146,6 @@ i32 handle_main_window_events(void) { case Button5 + 2: g_platform.wheel_dx += MOUSE_WHEEL_FACTOR; break; default:; } - if (!_requested_clipboard) { - XConvertSelection(_display, _clipboard, _targets, _clipboard, _window, CurrentTime); - _requested_clipboard = 1; - } break; case ButtonRelease: @@ -3187,11 +3209,6 @@ i32 handle_main_window_events(void) { }; } } - - if (!_requested_clipboard) { - XConvertSelection(_display, _clipboard, _targets, _clipboard, _window, CurrentTime); - _requested_clipboard = 1; - } } break; case KeyRelease: { @@ -3390,33 +3407,41 @@ i32 handle_main_window_events(void) { break; } - if (target_text != None) + if (g_platform.enable_clipboard_text && target_text != None) XConvertSelection(_display, _clipboard, target_text, _clipboard, _window, CurrentTime); - if (target_image != None) + if (g_platform.enable_clipboard_image && target_image != None) XConvertSelection(_display, _clipboard, target_image, _clipboard, _window, CurrentTime); - if (target_sound != None) + if (g_platform.enable_clipboard_sound && target_sound != None) XConvertSelection(_display, _clipboard, target_sound, _clipboard, _window, CurrentTime); if (target_text == None && target_image == None && target_sound == None) _requested_clipboard = 0; } else if (ev.xselection.target == XA_STRING || ev.xselection.target == _text_plain || ev.xselection.target == _utf8_string) { - resize_dynamic_array_exact(&g_platform.clipboard_text_len, (void **) &g_platform.clipboard_text, 1, len + 1); - mem_cpy_(g_platform.clipboard_text, data, len); - g_platform.clipboard_text[len] = '\0'; + if (g_platform.enable_clipboard_text) { + resize_dynamic_array_exact(&g_platform.clipboard_text_len, (void **) &g_platform.clipboard_text, 1, len + 1); + mem_cpy_(g_platform.clipboard_text, data, len); + g_platform.clipboard_text[len] = '\0'; + } _requested_clipboard = 0; } else if (ev.xselection.target == _image_bmp) { - // TODO - LOG_ERROR("Receive BMP image - not implemented."); + if (g_platform.enable_clipboard_image) { + // TODO + LOG_ERROR("Receive BMP image - not implemented."); + } _requested_clipboard = 0; } else if (ev.xselection.target == _image_ppm) { - // TODO - LOG_ERROR("Receive PPM image - not implemented."); + if (g_platform.enable_clipboard_image) { + // TODO + LOG_ERROR("Receive PPM image - not implemented."); + } _requested_clipboard = 0; } else if (ev.xselection.target == _audio_wav) { - // TODO - LOG_ERROR("Receive WAV audio - not implemented."); + if (g_platform.enable_clipboard_sound) { + // TODO + LOG_ERROR("Receive WAV audio - not implemented."); + } _requested_clipboard = 0; } @@ -3432,10 +3457,7 @@ i32 handle_main_window_events(void) { case FocusIn: g_platform.has_focus = 1; - if (!_requested_clipboard) { - XConvertSelection(_display, _clipboard, _targets, _clipboard, _window, CurrentTime); - _requested_clipboard = 1; - } + request_clipboard_(); break; case MappingNotify: @@ -3601,6 +3623,7 @@ void render_main_window_frame(void) { if (!_mapped) { XMapWindow(_display, _window); + request_clipboard_(); _mapped = 1; } @@ -3860,6 +3883,11 @@ void shutdown_all_systems(void) { g_platform.done = 1; } +b8 network_open(u16 slot, Network_Address address, u16 *local_port) { + LOG_ERROR("Web sockets are not implemented."); + return 0; +} + i64 network_recv(u16 slot, Network_Address address, i64 size, u8 *data, u16 *local_port, Network_Address *remote_address) { LOG_ERROR("Web sockets are not implemented."); return 0; @@ -4141,7 +4169,7 @@ __attribute__((export_name("js_clipboard_text_len"))) i32 js_clipboard_text_len( } __attribute__((export_name("js_clipboard_text"))) void *js_clipboard_text(i32 len) { - if (len < 0) + if (!g_platform.enable_clipboard_text && len < 0) len = 0; resize_dynamic_array_exact(&g_platform.clipboard_text_len, (void **) &g_platform.clipboard_text, 1, len); return g_platform.clipboard_text; @@ -49,38 +49,13 @@ #if !defined(__wasm__) -#ifndef TYPES_HEADER_GUARD_ -#define TYPES_HEADER_GUARD_ - -typedef signed char i8; -typedef signed short i16; typedef signed i32; typedef signed long long i64; -typedef unsigned char u8; -typedef unsigned short u16; typedef unsigned u32; typedef unsigned long long u64; typedef char c8; -typedef int c32; -typedef unsigned char b8; -typedef float f32; typedef double f64; -typedef struct { f64 x, y; } vec2; -typedef struct { f64 x, y, z; } vec3; -typedef struct { f64 x, y, z, w; } vec4; -typedef struct { f32 x, y; } vec2_f32; -typedef struct { f32 x, y, z; } vec3_f32; -typedef struct { f32 x, y, z, w; } vec4_f32; -typedef struct { i64 x, y; } vec2_i64; -typedef struct { i64 x, y, z; } vec3_i64; -typedef struct { i64 x, y, z, w; } vec4_i64; -typedef struct { f64 v[ 4]; } mat2; -typedef struct { f64 v[ 9]; } mat3; -typedef struct { f64 v[16]; } mat4; - -#endif // TYPES_HEADER_GUARD_ - // ================================================================ #ifndef TEST_HEADER_GUARD_ @@ -259,9 +234,7 @@ void bench_register(c8 const *name, c8 const *file, bench_run_fn fn); #define BENCHMARK_END \ } bench_end_(bench_index_); } -// FIXME -// Does this work reliably? -// +// FIXME: Does this work reliably? #define DO_NOT_OPTIMIZE(x) \ do { \ volatile void *bench_ptr_ = &(x); \ |