-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathG2DX.cpp
More file actions
285 lines (212 loc) · 6.31 KB
/
G2DX.cpp
File metadata and controls
285 lines (212 loc) · 6.31 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
////////////////////////////////////////////////////////
//
// graphiX functions of G2D
//
///////////////////////////////////////////////////////
#include "G2Dfull.h"
#include <string>
#include <map>
#include <vector>
#include <chrono>
using namespace std;
/////////////////////////////////////////////////////////////
//
// Window Management
//
/////////////////////////////////////////////////////////////
void G2D::clearScreen(Color c)
{
glClearColor(c.R, c.G, c.B, c.A);
glClear(GL_COLOR_BUFFER_BIT);
}
void G2D::Show()
{
glFlush(); // single buffer
glutSwapBuffers(); // double buffer
}
/////////////////////////////////////////////////////////////
//
// RectWithTexture
//
/////////////////////////////////////////////////////////////
void G2D::drawRectWithTexture(int texture, V2 pos, V2 size, float angleDeg)
{
glLineWidth(0);
float w = size.x;
float h = size.y;
const double MPI = 3.14159265358979323846;
glColor4ub(0, 0, 0, 128);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glBindTexture(GL_TEXTURE_2D, texture);
glEnable(GL_TEXTURE_2D);
//angleDeg = 45;
float angleRad = angleDeg / 180 * MPI;
float rX = cos(angleRad) * w - sin(angleRad) * h;
float rY = sin(angleRad) * w + cos(angleRad) * h;
float cx = pos.x + w / 2 - rX / 2;
float cY = pos.y + h / 2 - rY / 2;
glPushMatrix();
glTranslatef(cx, cY, 0.0);
glRotatef(angleDeg, 0.0, 0.0, 1.0);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(0, 0, 0.0f);
glTexCoord2f(0.0, 1.0); glVertex3f(0, h, 0.0f);
glTexCoord2f(1.0, 1.0); glVertex3f(w, h, 0.0f);
glTexCoord2f(1.0, 0.0); glVertex3f(w, 0, 0.0f);
glEnd();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
}
/////////////////////////////////////////////////////////////
//
// Geometry
//
/////////////////////////////////////////////////////////////
void G2D::setPixel(V2 P,Color c)
{
glColor4d(c.R, c.G, c.B, c.A);
glBegin(GL_POINTS);
glVertex2i(P.x, P.y); //Set pixel coordinates
glEnd();
glFlush(); //Render pixel
}
void G2D::drawRectangle(V2 P1, V2 Size, Color c, bool fill)
{
glLineWidth(1);
glColor4d(c.R, c.G, c.B, c.A);
glDisable(GL_TEXTURE_2D);
if (fill) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
else glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glRecti((int)P1.x, (int)P1.y, (int)(P1.x + Size.x), (int)(P1.y + Size.y));
}
void G2D::drawCircle(V2 C, float r, Color c, bool fill)
{
vector<V2> LPoints;
int lineAmount = r / 4; // nb of triangles used to draw circle
if (lineAmount < 20) lineAmount = 20;
const double PI = 3.14159265358;
//GLfloat radius = 0.8f; //radius
double step = 2 * PI / lineAmount;
for (int i = 0; i <= lineAmount; i++)
LPoints.push_back(V2(C.x + r * cos(i * step), C.y + r * sin(i * step)));
G2D::drawPolygon(LPoints, c, fill);
}
void G2D::drawLine(V2 P1, V2 P2, Color c)
{
glLineWidth(1);
glColor4d(c.R, c.G, c.B, c.A);
glDisable(GL_TEXTURE_2D);
glBegin(GL_LINES);
glVertex2f(P1.x, P1.y);
glVertex2f(P2.x, P2.y);
glEnd();
}
void G2D::drawPolygon(vector<V2> & PointList, Color c, bool fill)
{
if (fill) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
else glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDisable(GL_TEXTURE_2D);
glLineWidth(1);
glColor4d(c.R, c.G, c.B, c.A);
glBegin(GL_POLYGON);
for (V2 P : PointList)
glVertex2f(P.x, P.y);
glEnd();
}
/////////////////////////////////////////////////////////////
//
// Font
//
/////////////////////////////////////////////////////////////
void DrawString(V2 pos, string text, float fontSize, float thickness, Color c, bool FontMono)
{
glColor4f(c.R, c.G, c.B, c.A);
// EPAISSEUR de la font
glLineWidth(thickness);
glPushMatrix();
glTranslatef(pos.x, pos.y, 0);
glScalef(1 / 152.38, 1 / 152.38, 1 / 152.38);
glScalef(fontSize, fontSize, fontSize);
const char * cc = text.c_str();
for (char* p = (char*)cc; *p; p++)
{
if (FontMono) glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, *p);
else glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
}
glPopMatrix();
glLineWidth(1);
}
void G2D::drawStringFontMono(V2 pos, string text, float fontSize, float thickness, Color c)
{
glDisable(GL_TEXTURE_2D);
DrawString(pos, text, fontSize, thickness, c, true);
}
void G2D::drawStringFontRoman(V2 pos, string text, float fontSize, float thickness, Color c)
{
glDisable(GL_TEXTURE_2D);
DrawString(pos, text, fontSize, thickness, c, false);
}
/////////////////////////////////////////////////////////////
//
// Create Texture From String
//
/////////////////////////////////////////////////////////////
typedef unsigned char u8;
u8 LetterToColor[256][4];
void AssignColor(int code, u8 R, u8 G, u8 B, u8 A = 255)
{
LetterToColor[code][0] = R;
LetterToColor[code][1] = G;
LetterToColor[code][2] = B;
LetterToColor[code][3] = A;
}
void InitColors()
{
// https://www.rapidtables.org/fr/web/color/RGB_Color.html
AssignColor('R', 255, 0, 0);
AssignColor('G', 0, 255, 0);
AssignColor('B', 0, 0, 255);
AssignColor('K', 0, 0, 0); // noir
AssignColor('W', 255, 255, 255); // white
AssignColor('M', 255, 0, 255); // magenta
AssignColor('C', 0, 255, 255); // cyan
AssignColor('Y', 255, 255, 0); // yellow
AssignColor('S', 192, 192, 192); // silver
AssignColor('G', 128, 128, 128); // gray
AssignColor('O', 255, 215, 0); // or
AssignColor(' ', 255, 255, 255, 0); // transparent
AssignColor('[', 255, 255, 255, 0); // transparent
AssignColor(']', 255, 255, 255, 0); // transparent
}
int CORECreateTextureFromRGBA(unsigned char * data, int width, int height)
{
GLuint t = 0;
glGenTextures(1, &t);
glBindTexture(GL_TEXTURE_2D, t);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
return t;
}
int G2D::initTextureFromString(V2 & Size, const string & Sprite)
{
int LargPix = Sprite.find(']') + 1;
Size.x = LargPix;
int nb = Sprite.length();
if (nb % LargPix != 0)
cout << "Erreur taille texture/string incorrecte";
int HautPix = nb / LargPix;
Size.y = HautPix;
int pos = 0;
unsigned char data[128 * 128];
for (int y = HautPix - 1; y >= 0; y--)
for (int x = 0; x < LargPix; x++)
{
int p = x + y * LargPix;
char c = Sprite[p];
for (int t = 0; t < 4; t++)
data[pos++] = LetterToColor[c][t];
}
int IdTexture = CORECreateTextureFromRGBA(data, LargPix, HautPix);
return IdTexture;
}