目录
- 简介
- 函数介绍
- 1、init(int x, int fz)
- 2、fill(concol color)
- 3、update()
- 4、getmousepos(pos & p)
- 5、gt(int x, int y)
- 6、HideCursor()
- 7、settextcolor(concol color)
- 8、setbackcolor(concol color)
- 9、rect(int sx, int sy, int ex, int ey, concol color)
- 10、line(int sx, int sy, int ex, int ey, concol color)
- 11、dot(int x, int y, concol color)
- 示例代码
- 示例图片
- 头文件代码
- 总结
简介
这个头文件是用来在C++控制台绘图的,借鉴了 pygame 的一些思想,提供了一些函数用来绘图,可以有效的避免多次绘制重复图像时屏闪等问题。
函数介绍
1、init(int x, int fz)
初始化屏幕, 将屏幕大小设定为 x , 字体大小为 fz 。
2、fill(concol color)
用来将整个屏幕填充为一定的颜色。
3、update()
注意!绘画完后的图像并不会立刻出现在控制台上,使用这个函数来更新。
4、getmousepos(pos & p)
获取鼠标坐标并存到 p 中
5、gt(int x, int y)
将控制台光标移到(x,y)的位置,x,y 从1开始
6、HideCursor()
隐藏控制台光标
7、settextcolor(concol color)
设置文字颜色
8、setbackcolor(concol color)
设置背景颜色
9、rect(int sx, int sy, int ex, int ey, concol color)
以(sx, sy)为左上端点、(ex, ey)为右下端点,绘制颜色为color的矩形。
10、line(int sx, int sy, int ex, int ey, concol color)
绘制一条从(sx, sy)到(ex, ey)的线。
11、dot(int x, int y, concol color)
在(x,y)的位置画一个点
先介绍到这里
里面更多的功能、函数会在以后的文章后详细解释。
示例代码
我这里将头文件命名为 Drawer.h ,如果用了别的名字保存头文件,这里也要改过来。
功能是从(1,1)到鼠标的位置画线。
#include "Drawer.h"
using namespace std;
Pos mp; // Mouse_Pos
void game()
{
while()
{
getmousepos(mp); // Get Mouse Pos
fill(black); // Let Screen be black
line(, 1, mp.x, mp.y, green); // from (1, 1) to (mp.x, mp.y) draw Line
// ^
// |
// You can change this to red, blue, white or more
update(); // Update Screen
}
}
int main(){
system("mode con cols= lines=52");
init(, 16);// 50x50 Screen and font size is 16
game();
}
示例图片
截屏截不到鼠标。。。
头文件代码
#include <windows.h>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#ifndef KEY_DOWN
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) &x8000) ? 1:0)
#endif
#define cleanx2
#define boxx4
#define fullx1
using namespace std;
enum concol {
black =,
dark_blue =,
dark_green =,
dark_aqua =, dark_cyan = 3,
dark_red =,
dark_purple =, dark_pink = 5, dark_magenta = 5,
dark_yellow =,
dark_white =,
gray =,
blue =,
green =,
cyan =,
red =,
purple =, pink = 13,
yellow =,
white =
};
struct pixel {
concol color;
};
struct Pos {
int x, y;
};
struct Posd
{
int x, y, z ;
};
CONSOLE_CURSOR_INFO CursorInfo;
COORD _GoToPos;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
HWND hwnd = GetForegroundWindow();
int backcol, textcol;
pixel _LastScreen[][1000];
pixel _NewScreen[][1000];
int _ScreenSideLength;
int Pydx = 0, Py3dy = 0 ;
int xzlen =, yzlen = 1 ;
int fontsize =;
inline void getmousepos(Pos &p) {
POINT pt;
GetCursorPos(&pt);
ScreenToClient(hwnd, &pt);
p.y = (pt.y / fontsize +.5);
p.x = (pt.x / fontsize +.5);
}
inline void gt(short x, short y) {
--x;
--y;
_GoToPos = {x * xzlen, y * yzlen};
SetConsoleCursorPosition(hOut, _GoToPos);
}
inline void HideCursor()
{
GetConsoleCursorInfo(hOut, &CursorInfo);
CursorInfo.bVisible = false;
SetConsoleCursorInfo(hOut, &CursorInfo);
}
inline void settextcolor(concol textcolor) {
textcol = textcolor;
unsigned short wAttributes = ((unsigned int)backcol <<) | (unsigned int)textcol;
SetConsoleTextAttribute(hOut, wAttributes);
}
inline void setbackcolor(concol backcolor) {
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
backcol = backcolor;
unsigned short wAttributes = ((unsigned int)backcol <<) | (unsigned int)textcol;
SetConsoleTextAttribute(hOut, wAttributes);
}
struct Button {
Pos mp;
const char* name;
int len, qx, px, py;
void rename(const char* ne) {
name = ne;
len = strlen(ne);
}
void setpos(int x, int y) {
py = y;
qx = (x - len /);
px = (x + len /);
gt(qx, y);
printf(name);
}
bool check() {
getmousepos(mp);
if (mp.x <= px and mp.x >= qx and mp.y <= py and mp.y >= py -) {
gt(qx -, py);
printf(">>");
gt(px, py);
printf("<<");
if (KEY_DOWN(MOUSE_MOVED)) return true;
} else {
gt(qx -, py);
printf(" ");
gt(px, py);
printf(" ");
}
return false;
}
};
inline void init(int x, int fz) {
fontsize = fz;
GetConsoleCursorInfo(hOut, &CursorInfo);
CursorInfo.bVisible = false;
SetConsoleCursorInfo(hOut, &CursorInfo);
_ScreenSideLength = x;
}
inline void update() {
for (int i =; i <= _ScreenSideLength ; ++i) {
for (int j =; j <= _ScreenSideLength ; ++j) {
if (_LastScreen[j][i].color != _NewScreen[j][i].color) {
gt(j, i);
setbackcolor(_NewScreen[j][i].color);
for(int i=;i<=yzlen;++i)
{
for(int j=;j<=xzlen;++j)
putchar(' ');
puts("\n");
}
_LastScreen[j][i] = _NewScreen[j][i];
}
}
}
}
void fill(concol color) {
for (int i =; i <= _ScreenSideLength; ++i) {
for (int j =; j <= _ScreenSideLength; ++j) {
if (_NewScreen[j][i].color != color) {
_NewScreen[j][i].color = color;
}
}
}
}
void rect(int sx, int sy, int ex, int ey, concol color) {
if (ey < sy) swap(ey, sy);
if (ex < sx) swap(ex, sx);
for (int i = sy; i <= ey; ++i) {
for (int j = sx; j <= ex; ++j) {
_NewScreen[j][i].color = color;
}
}
}
void ChangePy(int x, int y)
{
Pydx = x;
Pydy = y;
}
inline void dot(int x, int y, concol color) {
_NewScreen[x][y].color = color;
}
inline void line(int sx, int sy, int ex, int ey, concol color)
{
int xlen = ex - sx ;
int ylen = ey - sy ;
int len = sqrt(pow(xlen,) + pow(ylen, 2)) ;
for(double i=; i<=len; i += 1)
{
int x = xlen * i / len ;
int y = ylen * i / len ;
_NewScreen[x + sx][y + sy].color = color ;
}
}
inline Pos post2(Pos3d pos)
{
if(pos.z ==)
{
return Pos {pos.x, pos.y} ;
}
return Pos{pos.x / (log(pos.z)) + Py3dx, pos.y / (log10(pos.z)) + Py3dy} ;
}
inline void lined(Pos3d a, Pos3d b, concol color)
{
Pos a = pos3t2(a);
Pos b = pos3t2(b);
// line(a.x, b2.y, a2.y, b2.x, color);
line(a.x, a2.y, b2.x, b2.y, color);
}
inline int disd(Pos3d a, Pos3d b)
{
int x = abs(a.x - b.x);
int y = abs(a.y - b.y);
int z = abs(a.z - b.z);
return sqrt(pow(x,) + pow(y, 2) + pow(z, 2)) ;
}
/*
Q------W
\ /
A--S
*/
inline void rectd(Pos3d q, Pos3d w, Pos3d a, Pos3d s, concol color)
{
int qlen = disd(q, a);
int plen = disd(w, s);
int len = max(qlen, plen);
for(double i=;i<=len; i+=0.1)
{
Posd qpos = Pos3d {abs(q.x-a.x)*i/len + q.x, abs(q.y-a.y)*i/len + q.y, abs(q.z-a.z)*i/len + q.z} ;
Posd ppos = Pos3d {abs(w.x-s.x)*i/len + w.x, abs(w.y-s.y)*i/len + w.y, abs(w.z-s.z)*i/len + w.z} ;
lined(qpos, ppos, color);
}
}
struct cube
{
Posd pos ;
int xlen, ylen, zlen ;
void init(int xp, int yp, int zp, int xl, int yl, int zl)
{
pos = Posd {xp - Py3dx, yp - Py3dy, zp};
xlen = xl;
ylen = yl;
zlen = zl;
}
inline Posd getdotpos(int doti)
{
switch(doti)
{
case: return Pos3d {pos.x + xlen, pos.y + ylen, pos.z + zlen} ;
case: return Pos3d {pos.x, pos.y + ylen, pos.z + zlen} ;
case: return Pos3d {pos.x + xlen, pos.y, pos.z + zlen} ;
case: return Pos3d {pos.x + xlen, pos.y + ylen, pos.z} ;
case: return Pos3d {pos.x, pos.y, pos.z + zlen} ;
case: return Pos3d {pos.x, pos.y + ylen, pos.z} ;
case: return Pos3d {pos.x + xlen, pos.y, pos.z} ;
case: return Pos3d {pos.x, pos.y, pos.z} ;
}
return {, 0, 0}; //Else
}
/*----------6
/| /|
/ | / |
/ | / |----------2 |
|------|---8
ylen| / | /
| / | /zlen
|/ xlen |/----------4
^
|
O
*/
inline void draw(concol color, int mode)
{
if(mode & full)
{
rectd(getdotpos(5), getdotpos(7), getdotpos(6), getdotpos(8), green);
rectd(getdotpos(1), getdotpos(3), getdotpos(5), getdotpos(7), red);
rectd(getdotpos(5), getdotpos(1), getdotpos(6), getdotpos(2), blue);
rectd(getdotpos(7), getdotpos(3), getdotpos(8), getdotpos(4), pink);
rectd(getdotpos(2), getdotpos(4), getdotpos(6), getdotpos(8), yellow);
rectd(getdotpos(1), getdotpos(3), getdotpos(2), getdotpos(4), white);
}
if(mode & clean)
{
lined(getdotpos(1), getdotpos(2), color) ;
lined(getdotpos(1), getdotpos(3), color) ;
lined(getdotpos(1), getdotpos(5), color) ;
lined(getdotpos(2), getdotpos(6), color) ;
lined(getdotpos(2), getdotpos(4), color) ;
lined(getdotpos(3), getdotpos(4), color) ;
lined(getdotpos(3), getdotpos(7), color) ;
lined(getdotpos(4), getdotpos(8), color) ;
lined(getdotpos(5), getdotpos(6), color) ;
lined(getdotpos(5), getdotpos(7), color) ;
lined(getdotpos(6), getdotpos(8), color) ;
lined(getdotpos(7), getdotpos(8), color) ;
}
if(mode & box)
{
lined(getdotpos(1), getdotpos(4), color) ;
lined(getdotpos(1), getdotpos(7), color) ;
lined(getdotpos(2), getdotpos(3), color) ;
lined(getdotpos(2), getdotpos(8), color) ;
lined(getdotpos(3), getdotpos(5), color) ;
lined(getdotpos(3), getdotpos(8), color) ;
lined(getdotpos(4), getdotpos(7), color) ;
lined(getdotpos(4), getdotpos(6), color) ;
lined(getdotpos(5), getdotpos(2), color) ;
lined(getdotpos(5), getdotpos(8), color) ;
lined(getdotpos(6), getdotpos(1), color) ;
lined(getdotpos(6), getdotpos(7), color) ;
}
}
};