-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglwgt.cpp
185 lines (159 loc) · 3.87 KB
/
glwgt.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
#include "glwgt.h"
#include <iostream> // for debug
#include <QDebug>
#include <QKeyEvent>
#include <qmath.h>
#include <QTimer>
GLWidget::GLWidget(QWidget *parent)
: QGLWidget(parent)
{
setFocusPolicy(Qt::StrongFocus);
lgt->setQueen(0);
a = 0;
string_ren5 = "1";
retranslateGLWidget();
font = new QFont("Courier", 9);
data.load(":/queen.png");
gldata = QGLWidget::convertToGLFormat(data);
resize(data.size());
}
void GLWidget::retranslateGLWidget()
{
string_ren = tr("This program shows how to solve");
string_ren2 = tr("the problem of the eight queens");
string_ren3 = tr("Use arrow to change");
string_ren4 = "<- ";
string_ren6 = " ->";
}
void GLWidget::initializeGL()
{
glEnable(GL_NORMALIZE);
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
/* For Alpha */
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glLoadIdentity();
glOrtho(-2.0, 10.0, -2.0, 10.0, -1.0, 1.0);
}
void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
drawGrid(1);
renderText(40, 20, string_ren, *font);
renderText(40, 35, string_ren2, *font);
renderText(60, 270, string_ren3, *font);
renderText(200, 270, string_ren4, *font);
renderText(215, 270, string_ren5, *font);
if(a > 8)
renderText(224, 270, string_ren6, *font);
else
renderText(217, 270, string_ren6, *font);
glLineWidth(1);
restructRes(a);
glPopMatrix();
}
void GLWidget::restructRes(int a)
{
for (int i = 0; i < 8; ++i)
{
for (int j = 0; j < 8; ++j)
{
if(boards[a][i][j] == 1)
drawPoint(i + 0.5, j + 0.5);
}
}
}
void GLWidget::reverse(double *i, double *j)
{
double swap = 0;
swap = 8 - *i;
*i = *j;
*j = swap;
}
void GLWidget::drawPoint(double i, double j)
{
reverse(&i, &j);
glRasterPos2i(i, j);
glDrawPixels(data.width(), data.height(), GL_RGBA, GL_UNSIGNED_BYTE, gldata.bits());
}
void GLWidget::drawGrid(double step)
{
glColor3f(0.0, 0.0, 0.0);
for(int i = 0; i < 8; i+=step)
{
for(int j = 0; j < 8; j+=step)
{
if ((!(i%2) && !(j%2)) || ((i%2) && (j%2)))
{
glBegin(GL_QUADS);
glVertex2f(0 + i, 0 + j);
glVertex2f(0 + i, 1 + j);
glVertex2f(1 + i, 1 + j);
glVertex2f(1 + i, 0 + j);
glEnd();
}
}
}
for (int i = 0; i <= 8; i+=step)
{
if (i == 0 || i == 8)
{
glLineWidth(2);
glColor3f(0.5, 0.0, 0.0);
}
else
{
glLineWidth(1);
glColor3f(0.8, 0.8, 0.8);
}
glBegin(GL_LINES);
glVertex2f(i, 8);
glVertex2f(i, 0);
glVertex2f(8, i);
glVertex2f(0, i);
glEnd();
}
glColor3f(0.0, 0.0, 0.0);
}
void GLWidget::keyPressEvent(QKeyEvent * keyEvent)
{
switch (keyEvent->key())
{
case Qt::Key_Left:
if (a <= 0)
{
a = 0;
string_ren5 = "1";
}
else
{
a-=1;
string_ren5 = QString("%1").arg(a+1);
}
break;
case Qt::Key_Right:
if (a >= 91)
{
a = 91;
string_ren5 = "92";
}
else
{
a+=1;
string_ren5 = QString("%1").arg(a+1);
}
break;
}
updateGL();
}
void GLWidget::resizeGL(int width, int height)
{
int side = qMin(width, height);
glViewport((width - side) / 2, (height - side) / 2, side, side);
}
GLWidget::~GLWidget()
{
delete font;
}