-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreen.cpp
65 lines (53 loc) · 1.05 KB
/
Screen.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
#include "StdAfx.h"
#include "Screen.h"
#include <Windows.h>
#include <stdio.h>
void Screen::UpdateSize()
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
int w = csbi.srWindow.Right - csbi.srWindow.Left + 1;
int h = csbi.srWindow.Bottom - csbi.srWindow.Top - 1;
if (width == w && height == h)
return;
width = w;
height = h;
if (buf)
delete[] buf;
buf = new char[width * height + 1];
buf[width * height] = 0;
CleanBuf();
}
void Screen::CleanBuf()
{
memset(buf, ' ', width * height);
}
Screen::Screen()
{
width = 0;
height = 0;
buf = NULL;
}
Screen::~Screen()
{
if (buf)
delete[] buf;
}
void Screen::Prepare()
{
UpdateSize();
CleanBuf();
}
void Screen::Write(int x, int y, char c)
{
if (x < 0 || y < 0 || x >= width || y >= height)
return;
buf[y * width + x] = c;
}
void Screen::Draw()
{
COORD pos = { 0, 0 };
HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(output, pos);
puts(buf);
}