-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleGame.cpp
93 lines (76 loc) · 2.35 KB
/
ConsoleGame.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
# include <stdexcept>
# include <chrono>
#include <string>
using namespace std;
# include <Windows.h>
# include "ConsoleGame.h"
ConsoleGame::ConsoleGame(short screen_width, short screen_height)
: m_screen_width(screen_width),
m_screen_height(screen_height)
{
m_screen = new CHAR_INFO[(int) m_screen_width * m_screen_height];
memset(m_screen, 0, sizeof(CHAR_INFO) * m_screen_width * m_screen_height);
m_buffer = CreateConsoleScreenBuffer(
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
CONSOLE_TEXTMODE_BUFFER,
NULL
);
}
void ConsoleGame::updateFrame(float dt)
{
wchar_t title[256];
swprintf_s(title, 256, L"Console Game: FPS: %3.2f", 1.0f / dt);
SetConsoleTitle(title);
WriteConsoleOutput(
m_buffer,
m_screen,
{ m_screen_width, m_screen_height },
{ 0, 0 },
& m_rect_window
);
}
void ConsoleGame::createWindow(int font_width, int font_height)
{
COORD screen_coords = { m_screen_width, m_screen_height };
if (!SetConsoleScreenBufferSize(m_buffer, screen_coords))
throw std::runtime_error("Unable to set Console Screen Buffer size.");
if (!SetConsoleActiveScreenBuffer(m_buffer))
throw std::runtime_error("Unable to set Console Screen Buffer as Active.");
CONSOLE_FONT_INFOEX font_info;
font_info.cbSize = sizeof(font_info);
font_info.nFont = 0;
font_info.dwFontSize.X = font_width;
font_info.dwFontSize.Y = font_height;
font_info.FontFamily = FF_DONTCARE;
font_info.FontWeight = FW_NORMAL;
wcscpy_s(font_info.FaceName, L"Consolas");
if (!SetCurrentConsoleFontEx(m_buffer, false, &font_info))
throw std::runtime_error("Unable to set Console font.");
m_rect_window = { 0, 0, m_screen_width - 1, m_screen_height - 1 };
if (!SetConsoleWindowInfo(m_buffer, TRUE, &m_rect_window))
throw std::runtime_error("Unable to resize Console Window.");
}
void ConsoleGame::start()
{
auto time_naught = chrono::system_clock::now();
auto time_final = chrono::system_clock::now();
while (TRUE)
{
time_final = chrono::system_clock::now();
chrono::duration<float> chrono_dt = time_final - time_naught;
time_naught = time_final;
float dt = chrono_dt.count();
onTick(dt);
updateFrame(dt);
}
}
void ConsoleGame::drawPixel(short x, short y, short ch, short col)
{
if (x >= 0 && x < m_screen_width && y >= 0 && y < m_screen_height)
{
m_screen[y * m_screen_width + x].Char.UnicodeChar = ch;
m_screen[y * m_screen_width + x].Attributes = col;
}
}