/
GraphicsCP.cpp
246 lines (198 loc) · 4.42 KB
/
GraphicsCP.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "GraphicsCP.h"
#ifdef _WIN32
HANDLE hScreen;
void init_screen()
{
hScreen = GetStdHandle(STD_OUTPUT_HANDLE);
InitScreen(hScreen);
}
void refresh()
{
//Doing nothing, only for linux
system("CLS");
}
void setColour(int color)
{
TextColor(color,BLACK,hScreen);
}
void writeString(std::string str, int x, int y,int len)
{
GotoXY(x,y,hScreen);
printf(str.c_str());
}
void writeChar(int c, int x, int y)
{
GotoXY(x,y,hScreen);
printf("%c",c);
}
char keypress()
{
char tecla = '0';
if (_kbhit())
tecla=_getch();
return tecla;
}
std::string keypress_str()
{
char c = keypress();
std::string tecla = std::string(c, 1);
return tecla;
}
void clear_screen()
{
system("CLS");
}
void pause_screen()
{
system("PAUSE");
}
void sleep_screen(int ms)
{
Sleep(ms);
}
#else
#include <iostream>
#include <X11/Xlib.h>
#include <X11/keysymdef.h>
#include <assert.h>
#include <unistd.h>
#define NIL (0)
#define RED "#FF0000"
///Globals
Display *dpy;
Window w;
GC gc;
int blackColor;
int whiteColor;
XEvent e;
std::string key;
//void initialize();
void init_screen()
{
dpy = XOpenDisplay(NIL);
assert(dpy);
blackColor = BlackPixel(dpy, DefaultScreen(dpy));
whiteColor = WhitePixel(dpy, DefaultScreen(dpy));
createWindow(DY*10,DX*10);
}
void createWindow(int width, int height)
{
w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
width, height, 0, blackColor, blackColor);
XSelectInput(dpy, w, StructureNotifyMask | KeyPressMask );
XMapWindow(dpy, w);
gc = XCreateGC(dpy, w, 0, NIL);
XSetForeground(dpy, gc, whiteColor);
for(;;) {
XNextEvent(dpy, &e);
if (e.type == MapNotify)
break;
}
}
std::string keypress_str()
{
if (e.type == KeyPress)
return XKeysymToString(XKeycodeToKeysym(dpy,e.xkey.keycode,0));
return "0";
}
char keypress()
{
std::string str_c = keypress_str();
char tecla = str_c[0];
if(str_c.compare("Up")==0)
return 37;
else if(str_c.compare("Down")==0)
return 38;
else if(str_c.compare("Right")==0)
return 39;
else if(str_c.compare("Left")==0)
return 40;
else if(str_c.compare("space")==0)
return 32;
else if(str_c.compare("Escape")==0)
return 27;
else
return tecla;
}
void setColour_str(std::string color)
{
XColor xcolour;
Colormap colormap;
colormap = DefaultColormap(dpy, 0);
XParseColor(dpy, colormap, color.c_str(), &xcolour);
XAllocColor(dpy, colormap, &xcolour);
// I guess XParseColor will work here
//xcolour.red = R; xcolour.green = G; xcolour.blue = B;
//xcolour.flags = DoRed | DoGreen | DoBlue;
XSetForeground(dpy, gc, xcolour.pixel);
}
void setColour(int colour)
{
switch(colour)
{
case 0: setColour_str("#000000");
break;
case 1: setColour_str("#0000FF");
break;
case 2: setColour_str("#00FF00");
break;
case 3: setColour_str("#00FFFF");
break;
case 4: setColour_str("#FF0000");
break;
case 5: setColour_str("#FF00FF");
break;
case 6: setColour_str("#8B4513");
break;
case 7: setColour_str("#8B4513");
break;
case 8: setColour_str("#8B4513");
break;
case 9: setColour_str("#8B4513");
break;
case 10 : setColour_str("#90EE90");
break;
case 11 : setColour_str("#E0FFFF");
break;
case 12 : setColour_str("#FF4500");
break;
case 13 : setColour_str("#EE82EE");
break;
case 14 : setColour_str("#FFFF00");
break;
case 15 : setColour_str("#FFFFFF");
break;
default: setColour_str("#FFFFFF");
break;
}
}
void writeString(std::string word, int x, int y, int len)
{
XDrawString(dpy,w, gc,x*10,y*10,word.c_str(),len);
XFlush(dpy);
}
void writeChar(int c, int x, int y)
{
std::string str_word = std::string(1,char(c));
writeString(str_word, x,y, 1);
}
void refresh()
{
if(XPending(dpy))
XNextEvent(dpy, &e);
XFlush(dpy);
XClearWindow(dpy,w);
}
void clear_screen()
{
std::system ("clear");
}
void pause_screen()
{
sleep(10);
}
void sleep_screen(int ms)
{
usleep(ms*1000);
}
#endif