-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnsg.c
73 lines (63 loc) · 1.79 KB
/
cnsg.c
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
#include<windows.h>
#include "./include/cnsg.h"
static LARGE_INTEGER frequency;
static Image screenImage;
static WindowManager rootManager;
static int initialized;
static int sleep;
static int WINAPI ctrlCHandler(DWORD dwCtrlType) {
deinitCNSG();
return TRUE;
}
WindowManager* initCNSG(int argc, char *argv[], unsigned int width, unsigned int height) {
initSound(argc, argv);
initInput();
initScreen(width, height);
initColorImages();
SetConsoleCtrlHandler(ctrlCHandler, TRUE);
rootManager = initWindowManager(NULL);
screenImage = initImage(width, height, BLACK, NULL_COLOR);
initialized = TRUE;
return &rootManager;
}
void deinitCNSG(void) {
deinitInput();
deinitSound();
initialized = FALSE;
}
float elapsedTime(LARGE_INTEGER start) {
LARGE_INTEGER current;
LARGE_INTEGER elapsed;
if(frequency.QuadPart == 0) QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(¤t);
elapsed.QuadPart = current.QuadPart - start.QuadPart;
return (float)elapsed.QuadPart / frequency.QuadPart;
}
void getScreenShot(Image *image) {
copyImage(image, &screenImage);
}
int getSleepFlag(void) {
return sleep;
}
void gameLoop(unsigned int fps) {
LARGE_INTEGER previousClock;
float delay = 1.0F / fps;
QueryPerformanceCounter(&previousClock);
while(initialized) {
float elapsed = elapsedTime(previousClock);
sleep = FALSE;
if(elapsed > 1.0F) {
elapsed = 1.0F;
sleep = TRUE;
}
QueryPerformanceCounter(&previousClock);
clearImage(&screenImage, BLACK);
drawWindowManager(&rootManager, &screenImage, elapsed);
updateWindowManagerController(&rootManager);
updateWindowManager(&rootManager, elapsed);
flushBuffer(&screenImage);
while(elapsedTime(previousClock) < delay);
}
if(screenImage.width != 0) freeImage(&screenImage);
deinitSound();
}