1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | Running = true; while(Running) { MSG Message; while(PeekMessage(&Message, 0, 0, 0, PM_REMOVE)) { switch(Message.message) { case WM_ACTIVATEAPP: { } break; case WM_QUIT: { Running = false; } break; case WM_SYSKEYDOWN: case WM_SYSKEYUP: case WM_KEYDOWN: case WM_KEYUP: { u32 VKCode = (u32)Message.wParam; b32 WasDown = ((Message.lParam & (1 << 30)) != 0); b32 IsDown = ((Message.lParam & (1 << 31)) == 0); b32 AltKeyWasDown = (Message.lParam & (1 << 29)); if ((VKCode == VK_F4) && AltKeyWasDown) { Running = false; } if(WasDown != IsDown) { if(VKCode == 'W') {} else if(VKCode == 'S') {} else if(VKCode == 'A') {} else if(VKCode == 'D') {} else if(VKCode == 'Q') {} else if(VKCode == 'E') {} else if(VKCode == VK_ESCAPE) { Running = false; } } }break; default: { TranslateMessage(&Message); DispatchMessageA(&Message); } break; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | internal LRESULT CALLBACK MainWindowCallback(HWND Window, UINT Message, WPARAM wParam, LPARAM lParam) { //TODO should handle actual message dispatches? LRESULT Result = 0; switch (Message) { case WM_QUIT: { Running = false; }break; case WM_CLOSE: { // TODO Handle this with a message to the user? Running = false; } break; case WM_ACTIVATEAPP: { } break; case WM_DESTROY: { // TODO Handle this as an error - recreate window? Running = false; } break; case WM_SYSKEYDOWN: case WM_SYSKEYUP: case WM_KEYDOWN: case WM_KEYUP: { Assert(!"Keyboard input came in through a non-dispatch message!"); } break; case WM_PAINT: { } break; default: { Result = DefWindowProcA(Window, Message, wParam, lParam); } break; } return Result; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | #include "jesse_platform.h" #include <windows.h> #include <stdio.h> #include <gl/gl.h> #include "jesse.h" #include "jesse.cpp" internal void Win32FreeFileMemory(void *Memory) { if(Memory) { VirtualFree(Memory, 0, MEM_RELEASE); } } global b32 Running; internal LRESULT CALLBACK MainWindowCallback(HWND Window, UINT Message, WPARAM wParam, LPARAM lParam) { //TODO should handle actual message dispatches? LRESULT Result = 0; switch (Message) { case WM_QUIT: { Running = false; }break; case WM_CLOSE: { // TODO Handle this with a message to the user? Running = false; } break; case WM_ACTIVATEAPP: { } break; case WM_DESTROY: { // TODO Handle this as an error - recreate window? Running = false; } break; case WM_SYSKEYDOWN: case WM_SYSKEYUP: case WM_KEYDOWN: case WM_KEYUP: { Assert(!"Keyboard input came in through a non-dispatch message!"); } break; case WM_PAINT: { } break; default: { Result = DefWindowProcA(Window, Message, wParam, lParam); } break; } return Result; } int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { LARGE_INTEGER PerformanceFrequencyResult; QueryPerformanceFrequency(&PerformanceFrequencyResult); u64 PerformanceFrequency = PerformanceFrequencyResult.QuadPart; WNDCLASSA WindowClass = {}; WindowClass.style = CS_HREDRAW|CS_VREDRAW|CS_OWNDC; WindowClass.hInstance = hInstance; //WindowClass.hIcon; WindowClass.lpfnWndProc = MainWindowCallback; WindowClass.lpszClassName = "JesseClass"; RegisterClassA(&WindowClass); HWND WindowHandle = CreateWindowExA(0, WindowClass.lpszClassName, "Jesse", WS_OVERLAPPEDWINDOW|WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hInstance, 0); Assert(WindowHandle); HDC DeviceContext = GetDC(WindowHandle); Assert(DeviceContext); #if INTERNAL LPVOID BaseAddress = (LPVOID)Terabytes((u64)2); #else LPVOID BaseAddress = 0; #endif game_memory GameMemory = {}; GameMemory.PermStorageSize = Megabytes(64); GameMemory.TempStorageSize = Gigabytes((u64)4); u64 TotalMemSize = GameMemory.PermStorageSize + GameMemory.TempStorageSize; //TODO handle various memory footprints GameMemory.PermStorage = VirtualAlloc(BaseAddress, (size_t)TotalMemSize, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); GameMemory.TempStorage = ((u8 *)GameMemory.PermStorage + GameMemory.TempStorageSize); Assert(GameMemory.PermStorage && GameMemory.TempStorage); LARGE_INTEGER LastCounter; QueryPerformanceCounter(&LastCounter); u64 LastCycleCount = __rdtsc(); Running = true; while(Running) { MSG Message; while(PeekMessage(&Message, 0, 0, 0, PM_REMOVE)) { switch(Message.message) { case WM_ACTIVATEAPP: { } break; case WM_QUIT: { Running = false; } break; case WM_SYSKEYDOWN: case WM_SYSKEYUP: case WM_KEYDOWN: case WM_KEYUP: { u32 VKCode = (u32)Message.wParam; b32 WasDown = ((Message.lParam & (1 << 30)) != 0); b32 IsDown = ((Message.lParam & (1 << 31)) == 0); b32 AltKeyWasDown = (Message.lParam & (1 << 29)); if ((VKCode == VK_F4) && AltKeyWasDown) { Running = false; } if(WasDown != IsDown) { if(VKCode == 'W') {} else if(VKCode == 'S') {} else if(VKCode == 'A') {} else if(VKCode == 'D') {} else if(VKCode == 'Q') {} else if(VKCode == 'E') {} else if(VKCode == VK_ESCAPE) { Running = false; } } }break; default: { TranslateMessage(&Message); DispatchMessageA(&Message); } break; } } GameUpdateAndRender(&GameMemory); LARGE_INTEGER EndCounter; QueryPerformanceCounter(&EndCounter); u32 MillisecondsElasped = (u32)((1000*(EndCounter.QuadPart - LastCounter.QuadPart))/PerformanceFrequency); u64 EndCycleCount = __rdtsc(); u64 CycleCount = EndCycleCount - LastCycleCount; LastCounter = EndCounter; LastCycleCount = EndCycleCount; } return(0); } |
1 2 3 4 5 6 | case WM_PAINT: { PAINTSTRUCT ps; HDC dc = BeginPaint(Window, &ps); // do the drawing with dc here EndPaint(Window, &ps); } |