-
Notifications
You must be signed in to change notification settings - Fork 1
/
Stage.cpp
208 lines (169 loc) · 6.2 KB
/
Stage.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
// ========================================================================= //
// Fighting game framework (2D) with online multiplayer.
// Copyright(C) 2014 Jordan Sparks <[email protected]>
//
// This program is free software; you can redistribute it and / or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or(at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// ========================================================================= //
// File: Stage.cpp
// Author: Jordan Sparks <[email protected]>
// ================================================ //
// Implements Stage class.
// ================================================ //
#include "Stage.hpp"
#include "Engine.hpp"
#include "Config.hpp"
#include "Client.hpp"
#include "Game.hpp"
#include "PlayerManager.hpp"
#include "Camera.hpp"
// ================================================ //
Stage::Stage(const std::string& stageFile) :
Object(),
m_layers(),
m_rightEdge(0),
m_shiftUpdates()
{
Config c(stageFile);
if (!c.isLoaded()){
Log::getSingletonPtr()->logMessage("Failed to load stage file \"" + stageFile + "\"");
return;
}
Log::getSingletonPtr()->logMessage("Loading stage from file \"" + std::string(stageFile) + "\"");
const int numLayers = c.parseIntValue("core", "layers");
// Parse layer data one by one.
for (int i = 1; i <= numLayers; ++i){
StageLayer layer;
std::string layerName = (std::string("layer") + Engine::toString(i));
layer.pTexture = Engine::getSingletonPtr()->loadTexture(
Engine::getSingletonPtr()->getDataDirectory() + "/" + c.parseValue(layerName, "texture"));
///f: re-organize the layer struct to be more understandable
layer.src.w = c.parseIntValue(layerName, "w");
layer.src.h = c.parseIntValue(layerName, "h");
// Get texture data.
SDL_QueryTexture(layer.pTexture, nullptr, nullptr, &layer.w, &layer.h);
layer.src.x = 0;
layer.src.y = layer.h - layer.src.h;
// Set default camera position.
Camera::getSingletonPtr()->panX((layer.w / 2) - (layer.src.w / 2));
layer.dst.x = layer.dst.y = 0;
// Render the texture with virtual width/height by default.
layer.dst.w = Engine::getSingletonPtr()->getLogicalWindowWidth();
layer.dst.h = Engine::getSingletonPtr()->getLogicalWindowHeight();
layer.Effect.scrollX = c.parseIntValue(layerName, "scrollX");
layer.Effect.scrollY = c.parseIntValue(layerName, "scrollY");
m_layers.push_back(layer);
}
m_rightEdge = m_layers[0].w - m_layers[0].src.w;
Camera::getSingletonPtr()->setRightBound(m_rightEdge);
Log::getSingletonPtr()->logMessage("Stage loaded with " + Engine::toString(numLayers) + " layer(s)!");
}
// ================================================ //
Stage::~Stage(void)
{
for (StageLayerList::iterator itr = m_layers.begin();
itr != m_layers.end();
++itr){
Engine::getSingletonPtr()->destroyTexture(itr->pTexture);
}
}
// ================================================ //
void Stage::shift(const int x)
{
m_layers[0].src.x += x;
if (m_layers[0].src.x < 0){
m_layers[0].src.x = 0;
}
else if (m_layers[0].src.x > m_rightEdge){
m_layers[0].src.x = m_rightEdge;
}
}
// ================================================ //
void Stage::setShift(const int x)
{
m_layers[0].src.x = x;
if (m_layers[0].src.x < 0){
m_layers[0].src.x = 0;
}
else if (m_layers[0].src.x > m_rightEdge){
m_layers[0].src.x = m_rightEdge;
}
}
// ================================================ //
void Stage::updateShiftFromServer(const Stage::ShiftUpdate& update)
{
if (Game::getSingletonPtr()->getMode() == Game::CLIENT){
m_shiftUpdates.push(update);
}
}
// ================================================ //
void Stage::serverReconciliation(void)
{
while (!m_shiftUpdates.empty()){
ShiftUpdate update = m_shiftUpdates.front();
this->setShift(update.shift);
for (std::list<Client::StageShift>::iterator itr = Client::getSingletonPtr()->m_pendingStageShifts.begin();
itr != Client::getSingletonPtr()->m_pendingStageShifts.end();){
if (itr->seq <= update.lastProcessedShift){
itr = Client::getSingletonPtr()->m_pendingStageShifts.erase(itr);
}
else{
printf("reapplying stage shift %d/%d\n", itr->shift, itr->seq);
this->shift(itr->shift);
if (Game::getSingletonPtr()->getPlaying() == Game::PLAYING_RED){
//PlayerManager::getSingletonPtr()->getBluePlayer()->
}
++itr;
}
}
m_shiftUpdates.pop();
}
}
// ================================================ //
void Stage::update(double dt)
{
Camera::getSingletonPtr()->update(dt);
// Render the stage background.
for (unsigned int i = 0; i<m_layers.size(); ++i){
// Render the layer.
SDL_Rect src = m_layers[i].src;
m_layers[i].src.x = Camera::getSingletonPtr()->getX();
if (m_layers[i].src.x < 0){
m_layers[i].src.x = 0;
}
else if (m_layers[i].src.x > m_rightEdge){
m_layers[i].src.x = m_rightEdge;
}
SDL_RenderCopyEx(Engine::getSingletonPtr()->getRenderer(),
m_layers[i].pTexture, &m_layers[i].src, &m_layers[i].dst, 0, nullptr, SDL_FLIP_NONE);
// Process stage effects.
if (m_layers[i].Effect.scrollX || m_layers[i].Effect.scrollY){
// Scroll the layer.
m_layers[i].dst.x += static_cast<int>(m_layers[i].Effect.scrollX * dt);
//m_layers[i].dst.y += static_cast<int>(m_layers[i].Effect.scrollY * dt);
// Calculate offset for second rendering for seamless scrolling.
SDL_Rect dst2 = m_layers[i].dst;
//dst2.x -= dst2.w;
dst2.x = dst2.x - dst2.w - m_layers[i].src.x + m_rightEdge;
//dst2.y = dst2.y - dst2.h - m_layers[i].src.y;
// Render a second time with offset.
SDL_RenderCopyEx(Engine::getSingletonPtr()->getRenderer(),
m_layers[i].pTexture, &m_layers[i].src, &dst2, 0, nullptr, SDL_FLIP_NONE);
// Wrap back around to beginning.
if (dst2.x >= 0){
m_layers[i].dst.x = 0;
}
}
}
}
// ================================================ //