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 | LARGE_INTEGER perfCountFrequencyResult;
QueryPerformanceCounter(&perfCountFrequencyResult);
int64_t perfCountFrequency = perfCountFrequencyResult.QuadPart;
// Open window (1280x720)
LARGE_INTEGER lastCounter;
QueryPerformanceCounter(&lastCounter);
int64_t lastCycleCount = __rdtsc();
while (app.isRunning)
{
// Draw around 5 bitmaps (256x256) and a couple of triangles to the screen using OpenGL
int64_t endCycleCount = __rdtsc();
LARGE_INTEGER endCounter;
QueryPerformanceCounter(&endCounter);
int64_t cycleElapsed = endCycleCount - lastCycleCount;
int64_t counterElapsed = endCounter.QuadPart - lastCounter.QuadPart;
float msPerframe = ((1000.0 * (float)counterElapsed) / (float)perfCountFrequency);
float fps = (float)perfCountFrequency / (float)counterElapsed;
float mcpf = ((float)cycleElapsed / 1000000.0);
printf("%fms/f / %fFPS / %fMC/F\n", msPerframe, fps, mcpf); // A sample: (0.000018ms/f / 56302504.000000FPS / 22.228949MC/F)
lastCounter = endCounter;
lastCycleCount = endCycleCount;
}
|
1 2 3 | if (WasKeyPressed[KEY_A]) { printf("You pressed key A\n"); }
if (WasKeyReleased[KEY_A]) { printf("You released key A\n"); }
if (IsKeyHeldDown[KEY_A]) { printf("You are holding key A down\n"); } else { /* key is up */ }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | static bool WasKeyPressed[512], WasKeyReleased[512], IsKeyHeldDown[512];
// ... in your WindowProc
switch (message) {
case WM_KEYUP:
case WM_KEYDOWN: {
bool wasUp = !!((lparam >> 30) & 1);
bool isDown = message == WM_KEYDOWN;
DWORD key = wparam;
WasKeyPressed[key] = wasUp & isDown;
WasKeyReleased[key] = !wasUp & !isDown;
IsKeyHeldDown[key] = isDown;
return 0;
}
// .. in your main game loop
// if you need to do action exactly once when key is being pressed down, like throw one grenade
if (WasKeyPressed[VK_A]) throwGrenade();
// if you need to do action continuously while key is kept pressed down, like move forward
if (IsKeyHeldDown[VK_UP]) moveForward();
|
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 | bool IsKeyPressed(int key)
{
return (vkCode == key) && isDown && wasDown;
}
bool IsKeyDown(int key)
{
return (vkCode == key) && isDown && !wasDown;
}
bool IsKeyUp(int key)
{
return (vkCode == key) && !isDown && wasDown;
}
while (/* game loop */)
{
MSG message;
while (PeekMessage(&message, 0, 0, 0, PM_REMOVE))
{
switch (message.message)
{
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
case WM_KEYDOWN:
case WM_KEYUP:
{
vkCode = message.wParam;
wasDown = ((message.lParam & (1 << 30)) != 0);
isDown = ((message.lParam & (1 << 31)) == 0);
// Handle wasPressed, isPressed, isReleased
}
} break;
default:
{
TranslateMessage(&message);
DispatchMessage(&message);
} break;
}
}
// Do other stuff
}
|