Skip to content

Commit

Permalink
More responsive graphics approach
Browse files Browse the repository at this point in the history
  • Loading branch information
makuke1234 committed Jan 3, 2022
1 parent f3e2c54 commit 8f64c6e
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# PongD2D

![Release version](https://img.shields.io/badge/release-v1.0.2-green.svg)
![Release version](https://img.shields.io/badge/release-v1.1.0-green.svg)
![C version](https://img.shields.io/badge/version-C20-blue.svg)
![C++ version](https://img.shields.io/badge/version-C++20-blue.svg)

Expand Down
4 changes: 2 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ ASMC=nasm
CDEFFLAGS=-std=c2x -Wall -Wextra -Wpedantic -Wconversion -m64 -D UNICODE -D _UNICODE
CXXDEFFLAGS=-std=c++20 -Wall -Wextra -Wpedantic -Wconversion -m64 -D UNICODE -D _UNICODE
CDEBFLAGS=-g -O0 -D _DEBUG
CFLAGS=-O3 -Wl,--strip-all,--build-id=none,--gc-sections -fno-ident -D NDEBUG
LIB=-municode -lgdi32 -ld2d1 -ldwrite -luuid -mwindows
CFLAGS=-O3 -Wl,--strip-all,--build-id=none,--gc-sections -fno-ident -D NDEBUG -mwindows
LIB=-municode -lgdi32 -ld2d1 -ldwrite -luuid

SRC=src
SRC_SINGLE=srcSingle
Expand Down
61 changes: 35 additions & 26 deletions src/logic.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "resource.h"
#include "pongerr.h"

#include <time.h>
#define _USE_MATH_DEFINES
#include <math.h>

Expand All @@ -12,29 +11,31 @@ DWORD WINAPI PongLogic_thread(LPVOID param)

while (logic->killThreadFlag == false)
{
// Wait for time to come over
EnterCriticalSection(&logic->timing.crit);

SleepConditionVariableCS(&logic->timing.cv, &logic->timing.crit, INFINITE);
float delta = logic->timing.frameTime;

LeaveCriticalSection(&logic->timing.crit);


if (logic->requestReset)
{
logic->requestReset = false;
// Spin-loop waiting
while (!logic->requestReset);
EnterCriticalSection(&logic->timing.critR);

logic->requestReset = false;
WakeConditionVariable(&logic->timing.cvR);
SleepConditionVariableCS(&logic->timing.cvR, &logic->timing.critR, INFINITE);

LeaveCriticalSection(&logic->timing.critR);
}

while (!logic->scoring.notPaused && !logic->requestReset)
if (!logic->scoring.notPaused)
{
Sleep(20);
if (logic->killThreadFlag)
{
goto PongLogic_thread_finish;
}
continue;
}

clock_t start = clock();
Sleep(16);
float delta = (float)(clock() - start) / (float)CLOCKS_PER_SEC;


switch (logic->scoring.mode)
{
case GameMode_normal:
Expand Down Expand Up @@ -243,18 +244,12 @@ DWORD WINAPI PongLogic_thread(LPVOID param)
break;
}
case GameMode_gameOver:
{

break;
}
}

// Update screen
InvalidateRect(logic->pong->hwnd, NULL, FALSE);
// Screen will be updated automatically
}

PongLogic_thread_finish: ;

logic->logicThread = NULL;
return 0;
}
Expand All @@ -271,6 +266,13 @@ bool PongLogic_create(PongLogic_t * restrict logic, PongWnd_t * pong)

PongLogic_reset(logic);

// Create critical section and condition variable
InitializeCriticalSection(&logic->timing.crit);
InitializeConditionVariable(&logic->timing.cv);

InitializeCriticalSection(&logic->timing.critR);
InitializeConditionVariable(&logic->timing.cvR);

// try to create thread
logic->logicThread = CreateThread(
NULL,
Expand Down Expand Up @@ -299,9 +301,14 @@ void PongLogic_free(PongLogic_t * restrict logic)
if (logic->logicThread != NULL)
{
logic->killThreadFlag = true;
WakeConditionVariable(&logic->timing.cv);
WaitForSingleObject(logic->logicThread, INFINITE);
}

// Delete critical section
DeleteCriticalSection(&logic->timing.crit);
DeleteCriticalSection(&logic->timing.critR);

PongLogic_freeAssets(logic);
}

Expand Down Expand Up @@ -445,9 +452,11 @@ void PongLogic_reset(PongLogic_t * logic)

if (logic->logicThread != NULL)
{
EnterCriticalSection(&logic->timing.critR);
logic->requestReset = true;
// Do nothing smart
while (logic->requestReset);
WakeConditionVariable(&logic->timing.cv);

SleepConditionVariableCS(&logic->timing.cvR, &logic->timing.critR, INFINITE);
}

// Reset scoring
Expand All @@ -460,8 +469,8 @@ void PongLogic_reset(PongLogic_t * logic)

if (logic->logicThread != NULL)
{
logic->requestReset = true;
while (logic->requestReset);
LeaveCriticalSection(&logic->timing.critR);
WakeConditionVariable(&logic->timing.cvR);
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/logic.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ typedef struct Scoring

} Scoring_t;

typedef struct Timing
{
float frameTime;
CRITICAL_SECTION crit;
CONDITION_VARIABLE cv;

CRITICAL_SECTION critR;
CONDITION_VARIABLE cvR;

} Timing_t;

struct PongWnd;

typedef struct PongLogic
Expand All @@ -43,6 +54,7 @@ typedef struct PongLogic

Scoring_t scoring;
volatile bool requestReset;
Timing_t timing;

HANDLE logicThread;
bool killThreadFlag;
Expand Down
8 changes: 4 additions & 4 deletions src/resource.rc
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ IDI_APPLICATION ICON "assets/icon.ico"
// Version info
#include <winver.h>
VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,0,2
PRODUCTVERSION 1,0,2
FILEVERSION 1,1,0
PRODUCTVERSION 1,1,0
FILEFLAGSMASK VS_FFI_FILEFLAGSMASK
#ifdef _DEBUG
FILEFLAGS VS_FF_DEBUG | VS_FF_PRERELEASE
Expand All @@ -30,12 +30,12 @@ BEGIN
BEGIN
VALUE "CompanyName", "Maku Maku"
VALUE "FileDescription", "A version of Pong written in C and using Direct2D"
VALUE "FileVersion", "1.0.2"
VALUE "FileVersion", "1.1.0"
VALUE "InternalName", "Win32App"
VALUE "LegalCopyright", "\xA92022 Maku Maku"
VALUE "OriginalFileName", "PongD2D.exe"
VALUE "ProductName", "PongD2D"
VALUE "ProductVersion", "1.0.2"
VALUE "ProductVersion", "1.1.0"
END
END
BLOCK "VarFileInfo"
Expand Down
13 changes: 13 additions & 0 deletions src/winapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,16 @@ bool w32_regClass(

return RegisterClassExW(&wc) != 0;
}

LARGE_INTEGER w32_getTime()
{
LARGE_INTEGER time;
if (QueryPerformanceCounter(&time) == FALSE)
{
return (LARGE_INTEGER){ 0 };
}
else
{
return time;
}
}
7 changes: 7 additions & 0 deletions src/winapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,12 @@ bool w32_regClass(
HBRUSH backBrush
);

/**
* @brief Get time in microsecond accuracy
*
* @return LARGE_INTEGER Win32 time structure
*/
LARGE_INTEGER w32_getTime();


#endif
52 changes: 37 additions & 15 deletions src/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,19 +362,37 @@ const wchar_t * PongWnd_getTitle(const PongWnd_t * restrict pong)
return pong->wndTitle;
}
}
int PongWnd_msgLoop(const PongWnd_t * pong)
int PongWnd_msgLoop(PongWnd_t * pong)
{
MSG msg;
BOOL br;
while ((br = GetMessageW(&msg, NULL, 0, 0)) != 0)
MSG msg = { 0 };
while (msg.message != WM_QUIT)
{
if (br == -1)
BOOL br;
if ((br = PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) != 0)
{
pongErr(pong, PongErr_unknown);
return -1;
if (br == -1)
{
pongErr(pong, PongErr_unknown);
return -1;
}
//TranslateMessage(&msg);
DispatchMessageW(&msg);
}
else
{
// render
static LARGE_INTEGER start = { 0 };
PongWnd_onRender(pong);

LARGE_INTEGER stop;
QueryPerformanceCounter(&stop);
pong->logic.timing.frameTime = (float)(stop.QuadPart - start.QuadPart) / (float)10000000;

// Notify game logic thread to calculate next frame's movement in time
WakeConditionVariable(&pong->logic.timing.cv);

start = stop;
}
TranslateMessage(&msg);
DispatchMessageW(&msg);
}

return (int)msg.wParam;
Expand Down Expand Up @@ -451,8 +469,18 @@ LRESULT PongWnd_winProc(PongWnd_t * pong, HWND hwnd, UINT msg, WPARAM wp, LPARAM
switch (msg)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
consciousUnused(hdc);

// Direct2D rendering
PongWnd_onRender(pong);


EndPaint(hwnd, &ps);
break;
}
case WM_KEYDOWN:
PongWnd_onKeyPress(pong, wp, lp);
break;
Expand Down Expand Up @@ -492,10 +520,6 @@ void PongWnd_onRender(PongWnd_t * restrict pong)
return;
}

PAINTSTRUCT ps;
HDC hdc = BeginPaint(pong->hwnd, &ps);
consciousUnused(hdc);

dxRTBeginDraw((ID2D1RenderTarget *)pong->dx.pRT);
dxRTSetTransform((ID2D1RenderTarget *)pong->dx.pRT, dxD2D1Matrix3x2FIdentity());
// Draw black background
Expand Down Expand Up @@ -726,8 +750,6 @@ void PongWnd_onRender(PongWnd_t * restrict pong)
{
PongWnd_freeAssets(pong);
}

EndPaint(pong->hwnd, &ps);
}
void PongWnd_onSize(PongWnd_t * restrict pong, LPARAM lp)
{
Expand Down
2 changes: 1 addition & 1 deletion src/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const wchar_t * PongWnd_getTitle(const PongWnd_t * restrict pong);
* @param pong Pointer to PongWnd_t structure
* @return int wParam of last message
*/
int PongWnd_msgLoop(const PongWnd_t * pong);
int PongWnd_msgLoop(PongWnd_t * pong);
/**
* @brief Frees resources owned by PongWnd_t object
*
Expand Down

0 comments on commit 8f64c6e

Please sign in to comment.