From 512b96acc2ecdb46b38d2c46300ecbab64825843 Mon Sep 17 00:00:00 2001 From: Daniel Blagy Date: Wed, 13 Apr 2022 17:24:44 +0300 Subject: [PATCH] ep6 using delta --- dewcin_yt/src/main.cpp | 48 +++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/dewcin_yt/src/main.cpp b/dewcin_yt/src/main.cpp index ef24222..c92de0b 100644 --- a/dewcin_yt/src/main.cpp +++ b/dewcin_yt/src/main.cpp @@ -4,34 +4,44 @@ dewcin_app_entry_point { // game init code - int x = 250, y = 250; + float x = 250.0f, y = 250.0f; dewcin::Game::setGameUpdate([&](float delta) { wchar_t charBuffer[256]; - swprintf(charBuffer, 256, L"delta: %f\n", delta); - OutputDebugString(charBuffer); + //swprintf(charBuffer, 256, L"delta: %f\n", delta); + //OutputDebugString(charBuffer); - dewcin::Renderer::SetPixel(10, 10, { 0, 0, 255 }); - - dewcin::Renderer::FillRectangle({ x, y, 320, 180 }, { 0, 255, 0 }); + static int frames = 0; + static float timePassed = 0.0f; - if (dewcin::Input::isKeyPressed(DC_UP)) - y -= 10; - else if (dewcin::Input::isKeyPressed(DC_DOWN)) - y += 10; + frames++; + timePassed += delta; - if (dewcin::Input::isKeyPressed(DC_LEFT)) - x -= 10; - else if (dewcin::Input::isKeyPressed(DC_RIGHT)) - x += 10; + if (timePassed >= 1.0f) + { + swprintf(charBuffer, 256, L"FPS: %d\n", frames); + OutputDebugString(charBuffer); - if (dewcin::Input::isMouseButtonPressed(DC_MOUSE_MIDDLE)) - OutputDebugString(L"mouse middle button pressed!\n"); + timePassed -= 1.0f; + frames = 0; + } - dewcin::Input::Position mousePosition = dewcin::Input::getMousePosition(); - swprintf(charBuffer, 256, L"%d, %d\n", mousePosition.x, mousePosition.y); - OutputDebugString(charBuffer); + dewcin::Renderer::SetPixel(10, 10, { 0, 0, 255 }); + + // x = 3.4 = 3 x = 3.4 + 0.5 = 3.9 -> 3 + // x = 3.8 = 3 x = 3.8 ) 0.5 = 4.3 -> 4 + dewcin::Renderer::FillRectangle({ int(x + 0.5f), int(y + 0.5f), 320, 180 }, { 0, 255, 0 }); + + if (dewcin::Input::isKeyPressed(DC_W)) + y -= 100.0f * delta; + else if (dewcin::Input::isKeyPressed(DC_S)) + y += 100.0f * delta; + + if (dewcin::Input::isKeyPressed(DC_A)) + x -= 100.0f * delta; + else if (dewcin::Input::isKeyPressed(DC_D)) + x += 100.0f * delta; } );