diff --git a/silly.c b/silly.c new file mode 100644 index 0000000..6af0d3b --- /dev/null +++ b/silly.c @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include +#include +#include + +size_t f(float x, int n) +{ + bool is_negative = x < 0; + x = fabsf(x); + + float pow10 = 1; + float factor = n < 0 ? 0.1 : 10; + + size_t decs = 0; + size_t zeroes = 0; + for (int i = 0; decs < FLT_DIG and i < abs(n); i++) { + float next_pow = pow10 * factor; + float mod = fmodf(x * next_pow, 10); + + if (mod == 0) + break; + + pow10 = next_pow; + if (decs != 0 or (int)mod != 0) + decs += 1; + else + zeroes += 1; + } + decs += n * (n < 0); + + uint_fast64_t x_integ = roundf(x * pow10); + + size_t ints = 0; + for (int y = x_integ / pow10; y > 0; y /= 10) + ints += 1; + + size_t len = (is_negative) + (ints == 0) + ints + (decs != 0) + zeroes + decs; + char buf[len + 1]; + buf[len] = 0; + + for (int i = 0; i < decs; i++) { + buf[--len] = x_integ % 10 + 48; + x_integ /= 10; + } + + for (int i = 0; i < zeroes; i++) { + buf[--len] = '0'; + } + + if (decs != 0) + buf[--len] = '.'; + + if (n < 0) for (int i = 0; i < -n; i++) { + buf[--len] = '0'; + } + + if (ints == 0) { + buf[--len] = '0'; + } else for (int i = 0; i < ints; i++) { + buf[--len] = x_integ % 10 + 48; + x_integ /= 10; + } + + if (is_negative) + buf[--len] = '-'; + + printf("%s\n", buf); + printf("\t%f : decs:%llu, ints:%u, zeroes:%u\n", pow10, decs, ints, zeroes); +} + +int main() +{ + f(39, 3); // "39" + f(39.39, 9); // "39.39" + f(0.3939, 3); // "0.394" + f(39e-9, 9); // "0.000000039" + + f(3939, -2); // "3900" + + f(39.99, 0); // 39.99 expected: 40 + f(39.99, 1); // 39. expected: 40 + f(39.99, 2); // 39. expected: 39.99 + + f(-39.3939, 3); // -39.4 expected: -39.394 + + return 0; +} \ No newline at end of file diff --git a/silly.exe b/silly.exe new file mode 100644 index 0000000..a02981f Binary files /dev/null and b/silly.exe differ diff --git a/source/io/ctrl.c b/source/io/ctrl.c index f5617da..f85ed17 100644 --- a/source/io/ctrl.c +++ b/source/io/ctrl.c @@ -12,7 +12,7 @@ #define IO_BUF_SIZE 16 -enum InputType { +enum CtrlType { KEY, AXIS, JOYSTICK, @@ -20,7 +20,7 @@ enum InputType { ESCAPE }; -struct InputRecord { +struct CtrlRecord { u16 id; u8 type; @@ -40,15 +40,15 @@ struct InputRecord { struct timespec timestamp; }; -struct InputBuffer { - struct InputRecord records[IO_BUF_SIZE]; +struct RecordBuffer { + struct CtrlRecord records[IO_BUF_SIZE]; size_t count; pthread_mutex_t mutex; }; -struct InputBuffer NewInputBuffer() +struct RecordBuffer NewInputBuffer() { - struct InputBuffer buf; + struct RecordBuffer buf; buf.count = 0; buf.mutex = PTHREAD_MUTEX_INITIALIZER; return buf; @@ -74,27 +74,6 @@ struct Axis { struct timespec last_released; }; -enum KeyCode { - LEFT, - RIGHT, - SOFT_DROP, - HARD_DROP, - ROTATE_CCW, - ROTATE_CW, - ROTATE_180, - SWAP, - ESC -}; - -enum AxisCode { - VSCROLL, - HSCROLL -}; - -enum JoystickCode { - MOUSE -}; - typedef u32 hashtype; hashtype Hash(void *item, size_t size) { @@ -273,7 +252,7 @@ struct Axis *CtrlGet(Ctrl *ctrl, u16f code, u8f type) return code_bkt->axis; } -void update_key(struct Axis *axis, struct InputRecord *record) +void update_key(struct Axis *axis, struct CtrlRecord *record) { if (record->data.key.is_down) axis->last_pressed = record->timestamp; @@ -283,26 +262,26 @@ void update_key(struct Axis *axis, struct InputRecord *record) axis->data.key.is_down = record->data.key.is_down; } -void update_axis(struct Axis *axis, struct InputRecord *record) +void update_axis(struct Axis *axis, struct CtrlRecord *record) { axis->data.axis.value = record->data.axis.value; axis->last_pressed = record->timestamp; } -void update_joystick(struct Axis *axis, struct InputRecord *record) +void update_joystick(struct Axis *axis, struct CtrlRecord *record) { axis->data.joystick.x = record->data.joystick.x; axis->data.joystick.y = record->data.joystick.y; axis->last_pressed = record->timestamp; } -bool CtrlPoll(Ctrl *ctrl, struct InputBuffer *buf) +bool CtrlPoll(Ctrl *ctrl, struct RecordBuffer *buf) { pthread_mutex_lock(&buf->mutex); pthread_mutex_lock(&ctrl->mutex); for (size_t i = 0; i < buf->count; i++) { - struct InputRecord *rec = &buf->records[i]; + struct CtrlRecord *rec = &buf->records[i]; struct Axis *axis = find_axis(&ctrl->binds, rec->id, rec->type); if (axis == nullptr) diff --git a/source/io/ctrl.h b/source/io/ctrl.h index 0ace76e..e363640 100644 --- a/source/io/ctrl.h +++ b/source/io/ctrl.h @@ -12,7 +12,7 @@ #define IO_BUF_SIZE 16 -enum InputType { +enum CtrlType { KEY, AXIS, JOYSTICK, @@ -20,7 +20,7 @@ enum InputType { ESCAPE }; -struct InputRecord { +struct CtrlRecord { u16 id; u8 type; @@ -40,13 +40,13 @@ struct InputRecord { struct timespec timestamp; }; -struct InputBuffer { - struct InputRecord records[IO_BUF_SIZE]; +struct RecordBuffer { + struct CtrlRecord records[IO_BUF_SIZE]; size_t count; pthread_mutex_t mutex; }; -struct InputBuffer NewInputBuffer(); +struct RecordBuffer NewInputBuffer(); struct Axis { union { @@ -68,7 +68,7 @@ struct Axis { struct timespec last_released; }; -enum KeyCode { +enum CtrlCode { LEFT, RIGHT, SOFT_DROP, @@ -77,15 +77,9 @@ enum KeyCode { ROTATE_CW, ROTATE_180, SWAP, - ESC -}; - -enum AxisCode { + ESC, VSCROLL, - HSCROLL -}; - -enum JoystickCode { + HSCROLL, MOUSE }; @@ -129,4 +123,4 @@ bool CtrlMap(Ctrl *ctrl, u16f code, u16f bind, u8f type); struct Axis *CtrlGet(Ctrl *ctrl, u16f code, u8f type); -bool CtrlPoll(Ctrl *ctrl, struct InputBuffer *buf); \ No newline at end of file +bool CtrlPoll(Ctrl *ctrl, struct RecordBuffer *buf); \ No newline at end of file diff --git a/source/io/input.c b/source/io/input.c index 8713d82..c9b5f59 100644 --- a/source/io/input.c +++ b/source/io/input.c @@ -14,27 +14,23 @@ struct input_args { Ctrl *ctrl; - struct InputBuffer *buf; - pthread_mutex_t mutex; + struct RecordBuffer *buf; }; void *block_input(void *args_ptr) { struct input_args *args = args_ptr; Ctrl *ctrl = args->ctrl; - struct InputBuffer *buf = args->buf; - printf("RECIEVED BUF: %u, %u\n", args->ctrl, args->buf); + struct RecordBuffer *buf = args->buf; + free(args_ptr); while (true) { - printf("\tIN LOOP\n"); bool success; #ifdef _WIN32 success = WindowsBlockInput(buf); #endif - - printf("%llu\n", buf->records[0].timestamp.tv_sec); - + if (!success) { printf("winfail"); exit(1); @@ -44,17 +40,13 @@ void *block_input(void *args_ptr) return nullptr; } -void StartInput(Ctrl *ctrl, struct InputBuffer *buf) +void StartInput(Ctrl *ctrl, struct RecordBuffer *buf) { - printf("START INPUT:%u\n", buf); - struct input_args args = { - .ctrl = ctrl, - .buf = buf, - .mutex = PTHREAD_MUTEX_INITIALIZER - }; - pthread_mutex_lock(&args.mutex); - pthread_create(&ctrl->thread, nullptr, block_input, &args); - pthread_mutex_destroy(); + struct input_args *args = malloc(sizeof(struct input_args)); + args->ctrl = ctrl; + args->buf = buf; + + pthread_create(&ctrl->thread, nullptr, block_input, args); } void JoinInput(Ctrl *ctrl) diff --git a/source/io/input.h b/source/io/input.h index 3253074..08c43eb 100644 --- a/source/io/input.h +++ b/source/io/input.h @@ -8,6 +8,6 @@ #include "ctrl.h" #include "fumotris.h" -void StartInput(Ctrl *ctrl, struct InputBuffer *buf); +void StartInput(Ctrl *ctrl, struct RecordBuffer *buf); void JoinInput(Ctrl *ctrl); \ No newline at end of file diff --git a/source/io/platforms/win.c b/source/io/platforms/win.c index f006dd6..29e9f96 100644 --- a/source/io/platforms/win.c +++ b/source/io/platforms/win.c @@ -21,7 +21,7 @@ bool WindowsInit() return true; } -bool WindowsBlockInput(struct InputBuffer *buf) +bool WindowsBlockInput(struct RecordBuffer *buf) { return WinBlockInput(buf); } diff --git a/source/io/platforms/win.h b/source/io/platforms/win.h index b539fdd..57eee81 100644 --- a/source/io/platforms/win.h +++ b/source/io/platforms/win.h @@ -10,6 +10,6 @@ bool WindowsInit(); -bool WindowsBlockInput(struct InputBuffer *buf); +bool WindowsBlockInput(struct RecordBuffer *buf); bool WindowsWait(double seconds); \ No newline at end of file diff --git a/source/io/platforms/winhandler.c b/source/io/platforms/winhandler.c index b3b901c..43f996e 100644 --- a/source/io/platforms/winhandler.c +++ b/source/io/platforms/winhandler.c @@ -43,7 +43,7 @@ bool WinInitConsole() return SetConsoleMode(windows.input_handle, mode); } -void set_key_record(struct InputRecord *record, KEY_EVENT_RECORD win_key) +void set_key_record(struct CtrlRecord *record, KEY_EVENT_RECORD win_key) { record->type = KEY; record->id = win_key.wVirtualKeyCode; @@ -53,7 +53,7 @@ void set_key_record(struct InputRecord *record, KEY_EVENT_RECORD win_key) record->type = ESCAPE; } -bool set_mouse_record(struct InputRecord *record, MOUSE_EVENT_RECORD win_mouse) +bool set_mouse_record(struct CtrlRecord *record, MOUSE_EVENT_RECORD win_mouse) { switch (win_mouse.dwEventFlags) { case MOUSE_WHEELED: @@ -78,14 +78,14 @@ bool set_mouse_record(struct InputRecord *record, MOUSE_EVENT_RECORD win_mouse) return true; } -void set_window_record(struct InputRecord *record, WINDOW_BUFFER_SIZE_RECORD win_resize) +void set_window_record(struct CtrlRecord *record, WINDOW_BUFFER_SIZE_RECORD win_resize) { record->type = WINDOW; record->data.joystick.x = win_resize.dwSize.X; record->data.joystick.y = win_resize.dwSize.Y; } -bool dispatch_record(struct InputRecord *record, INPUT_RECORD win_record) +bool dispatch_record(struct CtrlRecord *record, INPUT_RECORD win_record) { switch (win_record.EventType) { case KEY_EVENT: @@ -102,18 +102,14 @@ bool dispatch_record(struct InputRecord *record, INPUT_RECORD win_record) return true; } -bool WinBlockInput(struct InputBuffer *buf) +bool WinBlockInput(struct RecordBuffer *buf) { - printf("\twin blocked: %u\n", buf); size_t win_size = IO_BUF_SIZE - buf->count; - printf("\twhar\n"); INPUT_RECORD win_buf[win_size]; DWORD count; - printf("\treading in..\n"); if (!ReadConsoleInput(windows.input_handle, win_buf, win_size, &count)) return false; - printf("\read done\n"); struct timespec now; timespec_get(&now, TIME_UTC); @@ -121,7 +117,7 @@ bool WinBlockInput(struct InputBuffer *buf) pthread_mutex_lock(&buf->mutex); for (size_t i = 0; i < count; i++) { - struct InputRecord record; + struct CtrlRecord record; record.timestamp = now; bool include = dispatch_record(&record, win_buf[i]); @@ -143,9 +139,7 @@ bool WinWait(double seconds) if (!SetWaitableTimer(windows.timer, &duration, 0, NULL, NULL, FALSE)) return false; - printf("about to wait..\n"); DWORD result = WaitForSingleObject(windows.timer, INFINITE); - printf("waitforsingle\n"); if (result != WAIT_OBJECT_0) return false; diff --git a/source/io/platforms/winhandler.h b/source/io/platforms/winhandler.h index 7bb4172..09b75ef 100644 --- a/source/io/platforms/winhandler.h +++ b/source/io/platforms/winhandler.h @@ -13,6 +13,6 @@ bool WinInitTimer(); bool WinInitConsole(); -bool WinBlockInput(struct InputBuffer *buf); +bool WinBlockInput(struct RecordBuffer *buf); bool WinWait(double seconds); \ No newline at end of file diff --git a/source/io/term.c b/source/io/term.c index d920236..2871b56 100644 --- a/source/io/term.c +++ b/source/io/term.c @@ -71,8 +71,7 @@ size_t TermBufToChars(struct TermBuf *term, char *buf, size_t max_chars) u8f last_bg = 0; u8f last_fg = 0; - //size_t filled = snprintf(buf, max_chars, "\x1b[H\x1b[0m"); - size_t filled = snprintf(buf, max_chars, "\x1b[0m"); + size_t filled = snprintf(buf, max_chars, "\x1b[H\x1b[0m"); for(size_t y = 0; y < term->hgt; y++) { for(size_t x = 0; x < term->wid; x++) { diff --git a/source/main.c b/source/main.c index 7541a47..956e572 100644 --- a/source/main.c +++ b/source/main.c @@ -15,7 +15,9 @@ #include "win.h" #endif -const enum KeyCode key_codes[] = { +const size_t code_count = 12; + +const enum CtrlCode codes[12] = { LEFT, RIGHT, SOFT_DROP, @@ -24,9 +26,15 @@ const enum KeyCode key_codes[] = { ROTATE_CW, ROTATE_180, SWAP, - ESC + ESC, + + VSCROLL, + HSCROLL, + + MOUSE }; -const u16f key_binds[] = { + +const u16f binds[12] = { 0x25, 0x27, 0x28, @@ -35,14 +43,13 @@ const u16f key_binds[] = { 'X', 'A', 'C', - 0x1B -}; + 0x1B, -const enum AxisCode axis_codes[] = { - VSCROLL, - HSCROLL + 0, + 1, + + 0 }; -const u16f axis_binds[] = { 0, 1 }; u8 I[16] = { 0, 0, 0, 0, @@ -51,7 +58,7 @@ u8 I[16] = { 0, 0, 0, 0 }; -void Loop(Ctrl *ctrl, struct InputBuffer *in_buf) +void Loop(Ctrl *ctrl, struct RecordBuffer *in_buf) { struct TermBuf term = NewTermBuf(20, 20); struct CharBlk4 term_blks[term.area]; @@ -74,15 +81,14 @@ void Loop(Ctrl *ctrl, struct InputBuffer *in_buf) for (int i = 0; i < 7779997; i++) { CtrlPoll(ctrl, in_buf); - printf("polled\n"); TetrMapToTermBuf(&board, &term); TetrMapToTermBuf(&falling, &term); TermBufToChars(&term, out, out_max); - //puts(out); + puts(out); - printf("%u\n", WindowsWait(0.01)); + WindowsWait(0.5); } } @@ -93,7 +99,7 @@ int main() Ctrl ctrl; NEW_CTRL(ctrl, 13, 13); - for (size_t i = 0; i < 9; i++) { + for (size_t i = 0; i < code_count; i++) { CtrlMap(&ctrl, key_binds[i], key_codes[i], KEY); } for (size_t i = 0; i < 2; i++) { @@ -104,7 +110,7 @@ int main() printf("set controls\n"); - struct InputBuffer in_buf = { + struct RecordBuffer in_buf = { .count = 0, .mutex = PTHREAD_MUTEX_INITIALIZER }; diff --git a/test.exe b/test.exe index 5c5a00c..48bd297 100644 Binary files a/test.exe and b/test.exe differ