-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseApp.cpp
120 lines (96 loc) · 2.38 KB
/
BaseApp.cpp
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
#include <algorithm>
#include <time.h>
#include <conio.h>
#include <assert.h>
#include <strsafe.h>
#define MY_PERFORMENCE_COUNTER
#include "BaseApp.h"
#include "PerformanceCounter.h"
BaseApp::BaseApp(int xSize, int ySize) : X_SIZE(xSize), Y_SIZE(ySize)
{
SMALL_RECT windowSize = {0, 0, X_SIZE-1, Y_SIZE-1};
mConsole = GetStdHandle(STD_OUTPUT_HANDLE);
mConsoleIn = GetStdHandle(STD_INPUT_HANDLE);
if(!SetConsoleWindowInfo(mConsole, TRUE, &windowSize))
{
cout << X_SIZE << Y_SIZE << endl;
cout << "SetConsoleWindowInfo failed with error " << GetLastError() << endl;
}
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(mConsole, &cursorInfo);
cursorInfo.bVisible = FALSE;
cursorInfo.dwSize = 1;
SetConsoleCursorInfo(mConsole, &cursorInfo);
mChiBuffer = (CHAR_INFO*)malloc((X_SIZE+1)*(Y_SIZE+1)*sizeof(CHAR_INFO));
mDwBufferSize.X = X_SIZE + 1;
mDwBufferSize.Y = Y_SIZE + 1; // ðàçìåð áóôåðà äàííûõ
mDwBufferCoord.X = 0;
mDwBufferCoord.Y = 0; // êîîðäèíàòû ÿ÷åéêè
mLpWriteRegion.Left = 0;
mLpWriteRegion.Top = 0;
mLpWriteRegion.Right = X_SIZE + 1;
mLpWriteRegion.Bottom = Y_SIZE + 1; // ïðÿìîóãîëüíèê äëÿ ÷òåíèÿ
for (int x=0; x<X_SIZE+1; x++)
{
for (int y=0; y<Y_SIZE+1; y++)
{
SetChar(x, y, L' ');
}
}
}
BaseApp::~BaseApp()
{
free(mChiBuffer);
}
void BaseApp::SetChar(int x, int y, wchar_t c)
{
mChiBuffer[x + (X_SIZE+1)*y].Char.UnicodeChar = c;
mChiBuffer[x + (X_SIZE+1)*y].Attributes = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED;
}
wchar_t BaseApp::GetChar(int x, int y)
{
return mChiBuffer[x + (X_SIZE+1)*y].Char.AsciiChar;
}
void BaseApp::Render()
{
if (!WriteConsoleOutputA(mConsole, mChiBuffer, mDwBufferSize, mDwBufferCoord, &mLpWriteRegion))
{
printf("WriteConsoleOutput failed - (%d)\n", GetLastError());
}
}
void BaseApp::Run()
{
CStopwatch timer;
int sum = 0;
int counter = 0;
int deltaTime = 0;
while (1)
{
timer.Start();
if (kbhit())
{
KeyPressed (getch());
if (!FlushConsoleInputBuffer(mConsoleIn))
cout<<"FlushConsoleInputBuffer failed with error "<<GetLastError();
}
UpdateF((float)deltaTime / 1000.0f);
Render();
Sleep(1);
while (1)
{
deltaTime = timer.Now();
if (deltaTime > 20)
break;
}
sum += deltaTime;
counter++;
if (sum >= 1000)
{
TCHAR szbuff[255];
StringCchPrintf(szbuff, 255, TEXT("FPS: %d"), counter);
SetConsoleTitle(szbuff);
counter = 0;
sum = 0;
}
}
}