diff --git a/src/base/base_core.h b/src/base/base_core.h index 346ddc9..440766d 100644 --- a/src/base/base_core.h +++ b/src/base/base_core.h @@ -103,6 +103,141 @@ union U128 #define Million(n) ((n)*1000000) #define Billion(n) ((n)*1000000000) +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +//~ + +global const U32 bitmask1 = 0x00000001; +global const U32 bitmask2 = 0x00000003; +global const U32 bitmask3 = 0x00000007; +global const U32 bitmask4 = 0x0000000f; +global const U32 bitmask5 = 0x0000001f; +global const U32 bitmask6 = 0x0000003f; +global const U32 bitmask7 = 0x0000007f; +global const U32 bitmask8 = 0x000000ff; +global const U32 bitmask9 = 0x000001ff; +global const U32 bitmask10 = 0x000003ff; +global const U32 bitmask11 = 0x000007ff; +global const U32 bitmask12 = 0x00000fff; +global const U32 bitmask13 = 0x00001fff; +global const U32 bitmask14 = 0x00003fff; +global const U32 bitmask15 = 0x00007fff; +global const U32 bitmask16 = 0x0000ffff; +global const U32 bitmask17 = 0x0001ffff; +global const U32 bitmask18 = 0x0003ffff; +global const U32 bitmask19 = 0x0007ffff; +global const U32 bitmask20 = 0x000fffff; +global const U32 bitmask21 = 0x001fffff; +global const U32 bitmask22 = 0x003fffff; +global const U32 bitmask23 = 0x007fffff; +global const U32 bitmask24 = 0x00ffffff; +global const U32 bitmask25 = 0x01ffffff; +global const U32 bitmask26 = 0x03ffffff; +global const U32 bitmask27 = 0x07ffffff; +global const U32 bitmask28 = 0x0fffffff; +global const U32 bitmask29 = 0x1fffffff; +global const U32 bitmask30 = 0x3fffffff; +global const U32 bitmask31 = 0x7fffffff; +global const U32 bitmask32 = 0xffffffff; + +global const U64 bitmask33 = 0x00000001ffffffffull; +global const U64 bitmask34 = 0x00000003ffffffffull; +global const U64 bitmask35 = 0x00000007ffffffffull; +global const U64 bitmask36 = 0x0000000fffffffffull; +global const U64 bitmask37 = 0x0000001fffffffffull; +global const U64 bitmask38 = 0x0000003fffffffffull; +global const U64 bitmask39 = 0x0000007fffffffffull; +global const U64 bitmask40 = 0x000000ffffffffffull; +global const U64 bitmask41 = 0x000001ffffffffffull; +global const U64 bitmask42 = 0x000003ffffffffffull; +global const U64 bitmask43 = 0x000007ffffffffffull; +global const U64 bitmask44 = 0x00000fffffffffffull; +global const U64 bitmask45 = 0x00001fffffffffffull; +global const U64 bitmask46 = 0x00003fffffffffffull; +global const U64 bitmask47 = 0x00007fffffffffffull; +global const U64 bitmask48 = 0x0000ffffffffffffull; +global const U64 bitmask49 = 0x0001ffffffffffffull; +global const U64 bitmask50 = 0x0003ffffffffffffull; +global const U64 bitmask51 = 0x0007ffffffffffffull; +global const U64 bitmask52 = 0x000fffffffffffffull; +global const U64 bitmask53 = 0x001fffffffffffffull; +global const U64 bitmask54 = 0x003fffffffffffffull; +global const U64 bitmask55 = 0x007fffffffffffffull; +global const U64 bitmask56 = 0x00ffffffffffffffull; +global const U64 bitmask57 = 0x01ffffffffffffffull; +global const U64 bitmask58 = 0x03ffffffffffffffull; +global const U64 bitmask59 = 0x07ffffffffffffffull; +global const U64 bitmask60 = 0x0fffffffffffffffull; +global const U64 bitmask61 = 0x1fffffffffffffffull; +global const U64 bitmask62 = 0x3fffffffffffffffull; +global const U64 bitmask63 = 0x7fffffffffffffffull; +global const U64 bitmask64 = 0xffffffffffffffffull; + +global const U32 bit1 = (1<<0); +global const U32 bit2 = (1<<1); +global const U32 bit3 = (1<<2); +global const U32 bit4 = (1<<3); +global const U32 bit5 = (1<<4); +global const U32 bit6 = (1<<5); +global const U32 bit7 = (1<<6); +global const U32 bit8 = (1<<7); +global const U32 bit9 = (1<<8); +global const U32 bit10 = (1<<9); +global const U32 bit11 = (1<<10); +global const U32 bit12 = (1<<11); +global const U32 bit13 = (1<<12); +global const U32 bit14 = (1<<13); +global const U32 bit15 = (1<<14); +global const U32 bit16 = (1<<15); +global const U32 bit17 = (1<<16); +global const U32 bit18 = (1<<17); +global const U32 bit19 = (1<<18); +global const U32 bit20 = (1<<19); +global const U32 bit21 = (1<<20); +global const U32 bit22 = (1<<21); +global const U32 bit23 = (1<<22); +global const U32 bit24 = (1<<23); +global const U32 bit25 = (1<<24); +global const U32 bit26 = (1<<25); +global const U32 bit27 = (1<<26); +global const U32 bit28 = (1<<27); +global const U32 bit29 = (1<<28); +global const U32 bit30 = (1<<29); +global const U32 bit31 = (1<<30); +global const U32 bit32 = (1<<31); + +global const U64 bit33 = (1ull<<32); +global const U64 bit34 = (1ull<<33); +global const U64 bit35 = (1ull<<34); +global const U64 bit36 = (1ull<<35); +global const U64 bit37 = (1ull<<36); +global const U64 bit38 = (1ull<<37); +global const U64 bit39 = (1ull<<38); +global const U64 bit40 = (1ull<<39); +global const U64 bit41 = (1ull<<40); +global const U64 bit42 = (1ull<<41); +global const U64 bit43 = (1ull<<42); +global const U64 bit44 = (1ull<<43); +global const U64 bit45 = (1ull<<44); +global const U64 bit46 = (1ull<<45); +global const U64 bit47 = (1ull<<46); +global const U64 bit48 = (1ull<<47); +global const U64 bit49 = (1ull<<48); +global const U64 bit50 = (1ull<<49); +global const U64 bit51 = (1ull<<50); +global const U64 bit52 = (1ull<<51); +global const U64 bit53 = (1ull<<52); +global const U64 bit54 = (1ull<<53); +global const U64 bit55 = (1ull<<54); +global const U64 bit56 = (1ull<<55); +global const U64 bit57 = (1ull<<56); +global const U64 bit58 = (1ull<<57); +global const U64 bit59 = (1ull<<58); +global const U64 bit60 = (1ull<<59); +global const U64 bit61 = (1ull<<60); +global const U64 bit62 = (1ull<<61); +global const U64 bit63 = (1ull<<62); +global const U64 bit64 = (1ull<<63); + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //~ Helper macros #define ArrayCount(a) (sizeof(a) / sizeof((a)[0])) @@ -113,7 +248,7 @@ union U128 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //~ -#define LOG OS_W32_log_debug +#define LOG os_w32_log_debug #endif /* BASE_CORE_H */ diff --git a/src/base/base_inc.h b/src/base/base_inc.h index f37f047..5dee226 100644 --- a/src/base/base_inc.h +++ b/src/base/base_inc.h @@ -5,7 +5,7 @@ //~ #include "base_core.h" #include "base_arena.h" -// #include "base_math.h" +#include "base_math.h" // #include "base_entry_point.h" #endif /* BASE_INC_H */ \ No newline at end of file diff --git a/src/base/base_math.h b/src/base/base_math.h new file mode 100644 index 0000000..928f143 --- /dev/null +++ b/src/base/base_math.h @@ -0,0 +1,18 @@ +#ifndef BASE_MATH_H +#define BASE_MATH_H + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +//~ + +typedef union Vec3F32 +{ + struct + { + F32 x; + F32 y; + }; +} +Vec3F32; + + +#endif /* BASE_MATH_H */ \ No newline at end of file diff --git a/src/main.c b/src/main.c index c24c866..b3e1fba 100644 --- a/src/main.c +++ b/src/main.c @@ -34,6 +34,7 @@ entry_point() // Init systems Arena *arena = arena_alloc(); + os_console_init(); os_event_init(); r_init(); //test_arena(arena); @@ -60,16 +61,16 @@ entry_point() OS_Event *event = &os_events.list[i]; if(event->kind != OS_EventKind_Null) { - LOG("%s ", os_key_display_string[event->key]); + os_console_log_fmt("%s ", os_key_display_string[event->key]); if(event->kind == OS_EventKind_Release) { - LOG(" RELEASE"); + os_console_log(" RELEASE"); } if(event->kind == OS_EventKind_Press) { - LOG(" PRESS"); + os_console_log(" PRESS"); } - LOG("\n"); + os_console_log("\n"); } } diff --git a/src/os/os_core.h b/src/os/os_core.h index 869b1b1..b86b8ed 100644 --- a/src/os/os_core.h +++ b/src/os/os_core.h @@ -31,6 +31,8 @@ struct OS_Event OS_Handle window; OS_EventKind kind; OS_Key key; + Vec3F32 pos; + B32 is_repeat; }; @@ -53,6 +55,12 @@ internal B32 os_commit(void *ptr, U64 size); internal void os_decommit(void *ptr, U64 size); internal void os_release(void *ptr, U64 size); + +internal void os_console_init(); +internal void os_console_log(const char *msg); +internal void os_console_log_fmt(const char *msg, ...); +internal void os_clear_console(); + internal U64 os_events_idx(); internal void os_event_init(); internal U64 os_events_received_on_frame(); diff --git a/src/os/win32/os_core_win32.c b/src/os/win32/os_core_win32.c index 5ddc8f5..8e67b14 100644 --- a/src/os/win32/os_core_win32.c +++ b/src/os/win32/os_core_win32.c @@ -5,7 +5,8 @@ #pragma comment(lib, "user32.lib") #pragma comment(lib, "gdi32.lib") - +global U32 os_console_line_count; +#define MAX_CONSOLE_LINES 64 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //~ @@ -38,10 +39,106 @@ os_release(void *ptr, U64 size) } +internal void +os_console_init() +{ + os_console_line_count = 0; + BOOL initd; + if(!AllocConsole()) + { + initd = FALSE; + } + else + { + FILE *dummy; + freopen_s(&dummy, "CONOUT$", "w", stdout); + initd = TRUE; + } + + if(!initd) + { + LOG("Failed to allocate console!"); + Trap(); + } +} + +internal void +os_console_log(const char *msg) +{ + os_w32_console_clear_current_line(); + printf(msg); + os_console_line_count += 1; + if(os_console_line_count > MAX_CONSOLE_LINES) + { + os_w32_reset_console_to_top(); + os_console_line_count = 0; + } +} + +internal void +os_console_log_fmt(const char *fmt_string, ...) +{ + os_w32_console_clear_current_line(); + char buffer[256]; + va_list args; + va_start(args, fmt_string); + _vsnprintf(buffer, sizeof(buffer) / sizeof(char), fmt_string, args); + va_end(args); + + printf(buffer); + os_console_line_count += 1; + if(os_console_line_count > MAX_CONSOLE_LINES) + { + os_w32_reset_console_to_top(); + os_console_line_count = 0; + } +} + +internal void +os_clear_console() +{ + HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + CONSOLE_SCREEN_BUFFER_INFO csbi; + DWORD written; + COORD topLeft = {0, 0}; + + // Get the console buffer info + GetConsoleScreenBufferInfo(hConsole, &csbi); + DWORD bufferSize = csbi.dwSize.X * csbi.dwSize.Y; + + // Fill the console with spaces to clear it + FillConsoleOutputCharacter(hConsole, ' ', bufferSize, topLeft, &written); + FillConsoleOutputAttribute(hConsole, csbi.wAttributes, bufferSize, topLeft, &written); + + // Move the cursor to the top-left + SetConsoleCursorPosition(hConsole, topLeft); +} + +internal void +os_w32_reset_console_to_top() +{ + HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + COORD topLeft = {0, 0}; + SetConsoleCursorPosition(hConsole, topLeft); +} + +internal void +os_w32_console_clear_current_line() +{ + HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + CONSOLE_SCREEN_BUFFER_INFO csbi; + GetConsoleScreenBufferInfo(hConsole, &csbi); + DWORD written; + COORD cursorPos = csbi.dwCursorPosition; + DWORD lineLength = csbi.dwSize.X - cursorPos.X; // Remaining width of the line + FillConsoleOutputCharacter(hConsole, ' ', lineLength, cursorPos, &written); + FillConsoleOutputAttribute(hConsole, csbi.wAttributes, lineLength, cursorPos, &written); + SetConsoleCursorPosition(hConsole, cursorPos); // Restore cursor positio +} internal void -OS_W32_log_debug(const char *fmt_string, ...) +os_w32_log_debug(const char *fmt_string, ...) { char buffer[1024]; va_list args; @@ -50,3 +147,5 @@ OS_W32_log_debug(const char *fmt_string, ...) va_end(args); OutputDebugStringA(buffer); } + + diff --git a/src/os/win32/os_core_win32.h b/src/os/win32/os_core_win32.h index 59fa991..fa223b0 100644 --- a/src/os/win32/os_core_win32.h +++ b/src/os/win32/os_core_win32.h @@ -7,6 +7,9 @@ #define NOMINMAX #include -internal void OS_W32_log_debug(const char *fmt_string, ...); + +internal void os_w32_console_clear_current_line(); +internal void os_w32_reset_console_to_top(); +internal void os_w32_log_debug(const char *fmt_string, ...); #endif /* OS_CORE_WIN32_H */ \ No newline at end of file diff --git a/src/os/win32/os_gfx_win32.c b/src/os/win32/os_gfx_win32.c index 09b19e6..3d6ab82 100644 --- a/src/os/win32/os_gfx_win32.c +++ b/src/os/win32/os_gfx_win32.c @@ -51,6 +51,10 @@ os_event_reset() { os_events_free_idx = 0; os_events_reached_max = 0; + for (U32 i = 0; i < OS_MAX_EVENTS; i += 1) + { + os_events.list[i].kind = OS_EventKind_Null; + } } @@ -79,15 +83,10 @@ DisplayWndProc(HWND Window, UINT Message, WPARAM WParam, LPARAM LParam) case WM_QUIT: case WM_DESTROY: case WM_CLOSE: - { - g_do_exit = 1; - } break; - // NOTE(casey): Anything you want the application to handle, forward to the main thread - // here. - case WM_MOUSEMOVE: { - + g_do_exit = 1; } break; + case WM_LBUTTONUP: case WM_RBUTTONUP: case WM_MBUTTONUP: @@ -114,7 +113,44 @@ DisplayWndProc(HWND Window, UINT Message, WPARAM WParam, LPARAM LParam) event->key = OS_Key_MiddleMouseButton; } break; } + event->pos.x = (F32)(S16)LOWORD(LParam); + event->pos.y = (F32)(S16)HIWORD(LParam); } break; + + case WM_MOUSEMOVE: + { + OS_Event *event = os_event_push(OS_EventKind_MouseMove, window); + event->pos.x = (F32)(S16)LOWORD(LParam); + event->pos.y = (F32)(S16)HIWORD(LParam); + } break; + + case WM_SYSKEYDOWN: case WM_SYSKEYUP: + { + if(WParam != VK_MENU && (WParam < VK_F1 || VK_F24 < WParam || WParam == VK_F4)) + { + result = DefWindowProcW(Window, Message, WParam, LParam); + } + } // Fallthrough + case WM_KEYDOWN: case WM_KEYUP: + { + B32 was_down = (LParam & bit31); + B32 is_down = !(LParam & bit32); + + B32 is_repeat = 0; + if(!is_down) + { + release = 1; + } + else if(was_down) + { + is_repeat = 1; + } + + OS_Event *event = os_event_push(release ? OS_EventKind_Release : OS_EventKind_Press, window); + event->key = os_win32_os_key_from_vkey(WParam); + event->is_repeat = is_repeat; + } break; + default: { result = DefWindowProcW(Window, Message, WParam, LParam); @@ -123,6 +159,49 @@ DisplayWndProc(HWND Window, UINT Message, WPARAM WParam, LPARAM LParam) return result; } +internal OS_Key +os_win32_os_key_from_vkey(WPARAM vkey) +{ + local_persist B32 first = 1; + local_persist OS_Key key_table[256]; + if (first) + { + first = 0; + MemoryZeroArray(key_table); + + key_table[(unsigned int)'A'] = OS_Key_A; + key_table[(unsigned int)'B'] = OS_Key_B; + key_table[(unsigned int)'C'] = OS_Key_C; + key_table[(unsigned int)'D'] = OS_Key_D; + key_table[(unsigned int)'E'] = OS_Key_E; + key_table[(unsigned int)'F'] = OS_Key_F; + key_table[(unsigned int)'G'] = OS_Key_G; + key_table[(unsigned int)'H'] = OS_Key_H; + key_table[(unsigned int)'I'] = OS_Key_I; + key_table[(unsigned int)'J'] = OS_Key_J; + key_table[(unsigned int)'K'] = OS_Key_K; + key_table[(unsigned int)'L'] = OS_Key_L; + key_table[(unsigned int)'M'] = OS_Key_M; + key_table[(unsigned int)'N'] = OS_Key_N; + key_table[(unsigned int)'O'] = OS_Key_O; + key_table[(unsigned int)'P'] = OS_Key_P; + key_table[(unsigned int)'Q'] = OS_Key_Q; + key_table[(unsigned int)'R'] = OS_Key_R; + key_table[(unsigned int)'S'] = OS_Key_S; + key_table[(unsigned int)'T'] = OS_Key_T; + key_table[(unsigned int)'U'] = OS_Key_U; + key_table[(unsigned int)'V'] = OS_Key_V; + key_table[(unsigned int)'W'] = OS_Key_W; + key_table[(unsigned int)'X'] = OS_Key_X; + key_table[(unsigned int)'Y'] = OS_Key_Y; + key_table[(unsigned int)'Z'] = OS_Key_Z; + } + + OS_Key key = key_table[vkey & bitmask8]; + + return key; +} + internal DWORD WINAPI MainThreadEntryPoint() { diff --git a/src/os/win32/os_gfx_win32.h b/src/os/win32/os_gfx_win32.h index a633127..b51bacc 100644 --- a/src/os/win32/os_gfx_win32.h +++ b/src/os/win32/os_gfx_win32.h @@ -23,5 +23,5 @@ struct OS_W32_WndParam LPVOID lpParam; }; - +internal OS_Key os_win32_os_key_from_vkey(WPARAM vkey); #endif /* OS_GFX_WIN32_H */ \ No newline at end of file