This repository has been archived by the owner on Feb 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
console_draw3.c
92 lines (78 loc) · 2.97 KB
/
console_draw3.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <windows.h>
#include <string.h>
#include "console_draw3.h"
COORD dwBufferSize = { SCREEN_WIDTH, SCREEN_HEIGHT };
COORD dwBufferCoord = { 0, 0 };
SMALL_RECT rcRegion = { 0, 0, SCREEN_WIDTH-1, SCREEN_HEIGHT-1 };
CHAR_INFO screen_buffer[SCREEN_HEIGHT][SCREEN_WIDTH];
CHAR_INFO background_buffer[SCREEN_HEIGHT][SCREEN_WIDTH];
CHAR_INFO clean_buffer[SCREEN_HEIGHT][SCREEN_WIDTH];
int Double_Buffer_Switch;
HANDLE hOutput[2];
int Touched_Global = 0;
int Initialized_Clean = 0;
void putASCII2( int x, int y, int ch, int fgcolor, int bgcolor )
{
if ( Touched_Global == 0) {
hOutput[0] = (HANDLE) GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleScreenBufferSize(hOutput[0], dwBufferSize);
hOutput[1] = CreateConsoleScreenBuffer(
GENERIC_READ | // read/write access
GENERIC_WRITE,
FILE_SHARE_READ |
FILE_SHARE_WRITE, // shared
NULL, // default security attributes
CONSOLE_TEXTMODE_BUFFER, // must be TEXTMODE
NULL); // reserved; must be NULL
SetConsoleScreenBufferSize(hOutput[1], dwBufferSize);
SetConsoleActiveScreenBuffer(hOutput[0]);
Double_Buffer_Switch = 1;
Touched_Global = 1;
}
if (x<SCREEN_WIDTH && x>=0 && y<SCREEN_HEIGHT && y>=0) {
screen_buffer[y][x].Char.AsciiChar = ch;
screen_buffer[y][x].Attributes = fgcolor | bgcolor<<4;
}
}
/* 在 (x, y) 座標的位置顯示字串 p 的內容 fg_color 是前景的顏色 bg_color 則是背景的顏色 */
void putString(int x, int y, char *p, int fg_color, int bg_color)
{
int i;
for(i=0; (unsigned int) i<strlen(p); i++) {
putASCII2(x+i, y, p[i], fg_color, bg_color);
}
}
void drawCmdWindow()
{
if (Touched_Global) {
WriteConsoleOutput( hOutput[Double_Buffer_Switch], (CHAR_INFO *)screen_buffer, dwBufferSize,
dwBufferCoord, &rcRegion );
SetConsoleActiveScreenBuffer(hOutput[Double_Buffer_Switch]);
SetConsoleCursorPosition( hOutput[Double_Buffer_Switch], dwBufferSize );
Double_Buffer_Switch = !Double_Buffer_Switch;
}
}
void clearScreen(void)
{
int i, j;
if (Initialized_Clean == 0) {
for (i=0; i<SCREEN_HEIGHT; i++) {
for (j=0; j<SCREEN_WIDTH; j++) {
screen_buffer[i][j].Char.AsciiChar = ' ';
screen_buffer[i][j].Attributes = 0;
}
}
memcpy(clean_buffer, screen_buffer, sizeof(CHAR_INFO)*SCREEN_HEIGHT*SCREEN_WIDTH);
Initialized_Clean = 1;
} else {
memcpy(screen_buffer, clean_buffer, sizeof(CHAR_INFO)*SCREEN_HEIGHT*SCREEN_WIDTH);
}
}
void saveScreen(void)
{
memcpy(background_buffer, screen_buffer, sizeof(CHAR_INFO)*SCREEN_HEIGHT*SCREEN_WIDTH);
}
void restoreScreen(void)
{
memcpy(screen_buffer, background_buffer, sizeof(CHAR_INFO)*SCREEN_HEIGHT*SCREEN_WIDTH);
}