diff --git a/include/tim.h b/include/tim.h index e5a38c8..5035a74 100644 --- a/include/tim.h +++ b/include/tim.h @@ -1,4 +1,5 @@ #pragma once +#pragma region include #include #include @@ -8,24 +9,6 @@ #include #include -typedef int8_t i8; -typedef int16_t i16; -typedef int32_t i32; -typedef int64_t i64; -typedef uint8_t u8; -typedef uint16_t u16; -typedef uint32_t u32; -typedef uint64_t u64; -typedef float f32; -typedef double f64; -typedef const char* cstr; - -#if !defined(__cplusplus) && !defined(bool) - # define bool u8 - # define false 0 - # define true 1 -#endif - #ifdef _WIN32 // windows #define TIM_WINDOWS @@ -50,11 +33,31 @@ typedef const char* cstr; #include #endif +typedef int8_t i8; +typedef int16_t i16; +typedef int32_t i32; +typedef int64_t i64; +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; +typedef float f32; +typedef double f64; +typedef const char* cstr; + +#if !defined(__cplusplus) && !defined(bool) + # define bool u8 + # define false 0 + # define true 1 +#endif + #ifdef __cplusplus extern "C" { #endif -/* constants ******************************************************************/ +#pragma endregion include + +#pragma region constants #define TIM_ENABLE_DBUF 1 // double buffering #define TIM_MAX_SCOPE 20 // max scope nesting @@ -89,7 +92,9 @@ enum { }; typedef i32 TimKey; -/* types **********************************************************************/ +#pragma endregion constants + +#pragma region types typedef struct TimCell_t { u8 fg; // foreground color @@ -162,14 +167,17 @@ typedef struct TimState_t { #endif } TimState_t; -/* macros *********************************************************************/ +#pragma endregion types + +#pragma region macros #define MAX(a, b) ((a) > (b) ? (a) : (b)) // #define MIN(a, b) ((a) < (b) ? (a) : (b)) // #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) // number of items in array #define S(s) ("" s), (sizeof(s) - 1) // expand to s, sizeof(s) - 1 -/* global variables ***********************************************************/ +#pragma endregion macros + // TODO: remove global variables @@ -183,7 +191,8 @@ TimState_t tim = { .buf = tim_buf, }; -/* string *********************************************************************/ + +#pragma region string // like strlen, returns 0 on NULL or int overflow static inline int ztrlen(const char* s) { @@ -299,7 +308,9 @@ static bool is_wide_perhaps(const u8* s, int n) { return true; } -/* unix ***********************************************************************/ +#pragma endregion string + +#pragma region unix // Unix-like terminal IO. Osx is missing ppoll and __unix__. Come on, fix it! @@ -467,7 +478,9 @@ static inline i64 time_us(void) { #endif // TIM_UNIX -/* windows ********************************************************************/ +#pragma endregion unix + +#pragma region windows // Windows terminal IO. Win32 is actually not that horrible as many say. Quirky // but well documented. @@ -641,7 +654,9 @@ static inline i64 time_us(void) { #endif // TIM_WINDOWS -/* events *********************************************************************/ +#pragma endregion windows + +#pragma region events // returns true if event was of type and key static inline bool is_event_key(TimEventType type, TimKey key) { @@ -665,7 +680,9 @@ static inline bool is_click_over(TimRect_t r) { return is_event_key(TimEvent_Mouse, TimKey_MouseButtonLeft) && is_mouse_over(r); } -/* drawing ********************************************************************/ +#pragma endregion events + +#pragma region drawing // create cell from utf8 code point with fg and bg colors static inline TimCell_t cell(const char* s, u8 fg, u8 bg) { @@ -761,7 +778,9 @@ static void draw_invert(int x, int y, int w) { } } -/* scope **********************************************************************/ +#pragma endregion drawing + +#pragma region scope // enter layout scope #define scope(x, y, w, h) \ @@ -819,7 +838,9 @@ static inline int exit_scope(void) { return 0; } -/* frame **********************************************************************/ +#pragma endregion scope + +#pragma region frame // frame // color: background, frame @@ -830,7 +851,9 @@ static inline void frame(int x, int y, int w, int h, u64 color) { } } -/* label **********************************************************************/ +#pragma endregion frame + +#pragma region label // text label // str : text - supports multiple lines @@ -851,7 +874,9 @@ static inline void label(const char* s, int x, int y, int w, int h, } } -/* button *********************************************************************/ +#pragma endregion label + +#pragma region button // button - returns true on click // color: frame, background, text @@ -869,7 +894,19 @@ static inline bool button(const char* txt, int x, int y, int w, int h, return is_click_over(r); } -/* edit ***********************************************************************/ +#pragma endregion button + +#pragma region edit + +static inline void edit_init(TimEdit_t* e, int capacity, const char* initial_content){ + e->length = utflen(initial_content); + e->cursor = utflen(initial_content); + e->capacity = capacity; + e->s = (char*)malloc(capacity + 1); + int byte_len = strlen(initial_content); + memcpy(e->s, initial_content, byte_len); + e->s[byte_len] = 0; +} static void edit_insert(TimEdit_t* e, const char* s) { int dst_size = ztrlen(e->s); @@ -945,16 +982,6 @@ static int edit_event(TimEdit_t* e, TimRect_t r) { return tim.event.key; } -static inline void edit_init(TimEdit_t* e, int capacity, const char* initial_content){ - e->length = utflen(initial_content); - e->cursor = utflen(initial_content); - e->capacity = capacity; - e->s = (char*)malloc(capacity + 1); - int byte_len = strlen(initial_content); - memcpy(e->s, initial_content, byte_len); - e->s[byte_len] = 0; -} - /// text edit - value in state /// @param e persistent edit state, use edit_init() to create new state /// @param color frame, background, text @@ -977,7 +1004,9 @@ static inline int edit(TimEdit_t* e, int x, int y, int w, u64 color) { return edit_event(e, r); } -/* check **********************************************************************/ +#pragma endregion + +#pragma region check // check box - returns true when clicked // txt : text label @@ -1000,7 +1029,9 @@ static inline bool check(const char* txt, int* state, int x, int y, int w, return click; } -/* radio **********************************************************************/ +#pragma endregion + +#pragma region radio // radio button - return true when clicked // txt : text label @@ -1024,7 +1055,9 @@ static inline bool radio(const char* txt, int* state, int v, int x, int y, return click; } -/* rendering ******************************************************************/ +#pragma endregion + +#pragma region rendering // write character to output buffer static inline void put_chr(char c) { @@ -1139,7 +1172,9 @@ static void render(void) { tim.cells = old_cells; // swap buffer } -/* event loop *****************************************************************/ +#pragma endregion + +#pragma region event loop static bool tim_run(f32 fps) { int timeout = (fps > 0) ? (int)(1000 / fps) : 0; @@ -1182,6 +1217,8 @@ static bool tim_run(f32 fps) { } // while } +#pragma endregion + #ifdef __cplusplus } #endif