/
Scene.cpp
138 lines (113 loc) · 2.7 KB
/
Scene.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
#include <QMatrix4x4>
#include "scene.h"
#include "painter.h"
scene::scene( QWidget *parent ) :
QOpenGLWidget( parent )
{
this->setFocusPolicy( Qt::StrongFocus );
w = 0;
h = 0;
connect( &m_timer, SIGNAL( timeout() ),
this, SLOT( slotUpdate() ) );
}
scene::~scene()
{
delete m_game;
}
void scene::setSize(int wS, int hS, int s, bool **f)
{
sp = 100 * 10/s;
m_game = new game(wS, hS, f);
m_game->newGame();
sendStatus();
m_timer.start( sp );
w = wS * 10.0f;
h = hS * 10.0f;
paintGL();
}
void scene::slotUpdate()
{
snake::Status status = m_game->status();
switch( status ) {
case snake::LIVE:
break;
case snake::INCREASED:
sendStatus();
break;
case snake::DEAD:
m_timer.stop();
sendStatus();
break;
case snake::WIN:
m_timer.stop();
sendStatus();
return;
}
m_game->tick();
update();
}
void scene::initializeGL()
{
glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
QOpenGLShader vShader( QOpenGLShader::Vertex );
vShader.compileSourceFile( ":/Shaders/vShader.glsl" );
QOpenGLShader fShader( QOpenGLShader::Fragment );
fShader.compileSourceFile( ":/Shaders/fShader.glsl" );
m_program.addShader( &vShader );
m_program.addShader( &fShader );
if ( !m_program.link() )
{
qWarning( "Error: unable to link a shader program" );
return;
}
}
void scene::paintGL()
{
// Clear the window with current clearing color
glClear( GL_COLOR_BUFFER_BIT );
if ( !m_program.bind() )
return;
QMatrix4x4 matrix;
matrix.ortho( 0.0f, w, h, 0.0f, -1.0f, 1.0f );
//matrix.translate( 0.0f, 0.0f, -0.5f );
m_program.setUniformValue( m_program.uniformLocation( "matrix" ), matrix );
painter p(&m_program);
if ( m_timer.isActive() ) {
m_game->draw( p );
}
m_program.release();
}
void scene::resizeGL( int w, int h )
{
glViewport( 0, 0, w, h );
}
void scene::keyPressEvent( QKeyEvent *event )
{
switch ( event->key() ) {
case Qt::Key_A:
m_game->keyEvent( snake::LEFT );
break;
case Qt::Key_W:
m_game->keyEvent( snake::UP );
break;
case Qt::Key_D:
m_game->keyEvent( snake::RIGHT );
break;
case Qt::Key_S:
m_game->keyEvent( snake::DOWN );
break;
case Qt::Key_Space:
m_game->newGame();
sendStatus();
m_timer.start( sp );
break;
}
}
void scene::sendStatus()
{
if (m_game->status() == snake::WIN)
emit signalShowStatus("Вы выйграли!");
else if (m_game->status() == snake::DEAD){
emit signalShowStatus("Змейка умерла!");
}
}