pragma region
This commit is contained in:
129
include/tim.h
129
include/tim.h
@@ -1,4 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#pragma region include
|
||||||
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
@@ -8,24 +9,6 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
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
|
#ifdef _WIN32
|
||||||
// windows
|
// windows
|
||||||
#define TIM_WINDOWS
|
#define TIM_WINDOWS
|
||||||
@@ -50,11 +33,31 @@ typedef const char* cstr;
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#endif
|
#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
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* constants ******************************************************************/
|
#pragma endregion include
|
||||||
|
|
||||||
|
#pragma region constants
|
||||||
|
|
||||||
#define TIM_ENABLE_DBUF 1 // double buffering
|
#define TIM_ENABLE_DBUF 1 // double buffering
|
||||||
#define TIM_MAX_SCOPE 20 // max scope nesting
|
#define TIM_MAX_SCOPE 20 // max scope nesting
|
||||||
@@ -89,7 +92,9 @@ enum {
|
|||||||
};
|
};
|
||||||
typedef i32 TimKey;
|
typedef i32 TimKey;
|
||||||
|
|
||||||
/* types **********************************************************************/
|
#pragma endregion constants
|
||||||
|
|
||||||
|
#pragma region types
|
||||||
|
|
||||||
typedef struct TimCell_t {
|
typedef struct TimCell_t {
|
||||||
u8 fg; // foreground color
|
u8 fg; // foreground color
|
||||||
@@ -162,14 +167,17 @@ typedef struct TimState_t {
|
|||||||
#endif
|
#endif
|
||||||
} TimState_t;
|
} TimState_t;
|
||||||
|
|
||||||
/* macros *********************************************************************/
|
#pragma endregion types
|
||||||
|
|
||||||
|
#pragma region macros
|
||||||
|
|
||||||
#define MAX(a, b) ((a) > (b) ? (a) : (b)) //
|
#define MAX(a, b) ((a) > (b) ? (a) : (b)) //
|
||||||
#define MIN(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 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
|
#define S(s) ("" s), (sizeof(s) - 1) // expand to s, sizeof(s) - 1
|
||||||
|
|
||||||
/* global variables ***********************************************************/
|
#pragma endregion macros
|
||||||
|
|
||||||
|
|
||||||
// TODO: remove global variables
|
// TODO: remove global variables
|
||||||
|
|
||||||
@@ -183,7 +191,8 @@ TimState_t tim = {
|
|||||||
.buf = tim_buf,
|
.buf = tim_buf,
|
||||||
};
|
};
|
||||||
|
|
||||||
/* string *********************************************************************/
|
|
||||||
|
#pragma region string
|
||||||
|
|
||||||
// like strlen, returns 0 on NULL or int overflow
|
// like strlen, returns 0 on NULL or int overflow
|
||||||
static inline int ztrlen(const char* s) {
|
static inline int ztrlen(const char* s) {
|
||||||
@@ -299,7 +308,9 @@ static bool is_wide_perhaps(const u8* s, int n) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* unix ***********************************************************************/
|
#pragma endregion string
|
||||||
|
|
||||||
|
#pragma region unix
|
||||||
|
|
||||||
// Unix-like terminal IO. Osx is missing ppoll and __unix__. Come on, fix it!
|
// 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
|
#endif // TIM_UNIX
|
||||||
|
|
||||||
/* windows ********************************************************************/
|
#pragma endregion unix
|
||||||
|
|
||||||
|
#pragma region windows
|
||||||
|
|
||||||
// Windows terminal IO. Win32 is actually not that horrible as many say. Quirky
|
// Windows terminal IO. Win32 is actually not that horrible as many say. Quirky
|
||||||
// but well documented.
|
// but well documented.
|
||||||
@@ -641,7 +654,9 @@ static inline i64 time_us(void) {
|
|||||||
|
|
||||||
#endif // TIM_WINDOWS
|
#endif // TIM_WINDOWS
|
||||||
|
|
||||||
/* events *********************************************************************/
|
#pragma endregion windows
|
||||||
|
|
||||||
|
#pragma region events
|
||||||
|
|
||||||
// returns true if event was of type and key
|
// returns true if event was of type and key
|
||||||
static inline bool is_event_key(TimEventType type, TimKey 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);
|
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
|
// create cell from utf8 code point with fg and bg colors
|
||||||
static inline TimCell_t cell(const char* s, u8 fg, u8 bg) {
|
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
|
// enter layout scope
|
||||||
#define scope(x, y, w, h) \
|
#define scope(x, y, w, h) \
|
||||||
@@ -819,7 +838,9 @@ static inline int exit_scope(void) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* frame **********************************************************************/
|
#pragma endregion scope
|
||||||
|
|
||||||
|
#pragma region frame
|
||||||
|
|
||||||
// frame
|
// frame
|
||||||
// color: background, 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
|
// text label
|
||||||
// str : text - supports multiple lines
|
// 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
|
// button - returns true on click
|
||||||
// color: frame, background, text
|
// 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);
|
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) {
|
static void edit_insert(TimEdit_t* e, const char* s) {
|
||||||
int dst_size = ztrlen(e->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;
|
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
|
/// text edit - value in state
|
||||||
/// @param e persistent edit state, use edit_init() to create new state
|
/// @param e persistent edit state, use edit_init() to create new state
|
||||||
/// @param color frame, background, text
|
/// @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);
|
return edit_event(e, r);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* check **********************************************************************/
|
#pragma endregion
|
||||||
|
|
||||||
|
#pragma region check
|
||||||
|
|
||||||
// check box - returns true when clicked
|
// check box - returns true when clicked
|
||||||
// txt : text label
|
// txt : text label
|
||||||
@@ -1000,7 +1029,9 @@ static inline bool check(const char* txt, int* state, int x, int y, int w,
|
|||||||
return click;
|
return click;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* radio **********************************************************************/
|
#pragma endregion
|
||||||
|
|
||||||
|
#pragma region radio
|
||||||
|
|
||||||
// radio button - return true when clicked
|
// radio button - return true when clicked
|
||||||
// txt : text label
|
// txt : text label
|
||||||
@@ -1024,7 +1055,9 @@ static inline bool radio(const char* txt, int* state, int v, int x, int y,
|
|||||||
return click;
|
return click;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* rendering ******************************************************************/
|
#pragma endregion
|
||||||
|
|
||||||
|
#pragma region rendering
|
||||||
|
|
||||||
// write character to output buffer
|
// write character to output buffer
|
||||||
static inline void put_chr(char c) {
|
static inline void put_chr(char c) {
|
||||||
@@ -1139,7 +1172,9 @@ static void render(void) {
|
|||||||
tim.cells = old_cells; // swap buffer
|
tim.cells = old_cells; // swap buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
/* event loop *****************************************************************/
|
#pragma endregion
|
||||||
|
|
||||||
|
#pragma region event loop
|
||||||
|
|
||||||
static bool tim_run(f32 fps) {
|
static bool tim_run(f32 fps) {
|
||||||
int timeout = (fps > 0) ? (int)(1000 / fps) : 0;
|
int timeout = (fps > 0) ? (int)(1000 / fps) : 0;
|
||||||
@@ -1182,6 +1217,8 @@ static bool tim_run(f32 fps) {
|
|||||||
} // while
|
} // while
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#pragma endregion
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user