-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcbmm_sim.cc
285 lines (244 loc) · 9.3 KB
/
cbmm_sim.cc
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
285
#include <cassert>
#include <cmath>
#include <iostream>
#include <memory>
#include <vector>
// TODO: Why is this necessary on Jason's build?
#define SDL_MAIN_HANDLED
#include <SDL.h>
#include "Animation.h"
#include "Bog.h"
#include "Box.h"
#include "Camera.h"
#include "Display.h"
#include "EntityManager.h"
#include "Event.h"
#include "Font.h"
#include "GeometryManager.h"
#include "Input.h"
#include "Physics.h"
#include "ShaderManager.h"
#include "State.h"
#include "Text.h"
#include "TextureManager.h"
int main(int, char**) {
const unsigned int SCREEN_WIDTH = 640;
const unsigned int SCREEN_HEIGHT = 480;
const unsigned int SCREEN_WIDTH_TILES = 20;
const unsigned int SCREEN_HEIGHT_TILES = 15;
const unsigned int TILE_SIZE = 16;
const unsigned int SCREEN_BPP = 32;
Display display(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP);
std::unique_ptr<MapProgram> tileProgram = MapProgram::Make();
std::unique_ptr<TextProgram> textProgram = TextProgram::Make();
std::unique_ptr<TextureProgram> textureProgram = TextureProgram::Make();
std::unique_ptr<ColorProgram> colorProgram = ColorProgram::Make();
GeometryManager geometryManager;
TextureManager textureManager;
std::vector<Animation> animations;
TextureRef tileSetRef =
textureManager.LoadTexture("resources/tileset.png", 0);
TextureRef collisionSetRef =
textureManager.LoadTexture("resources/collision.png", 0);
TextureRef dogRef =
textureManager.LoadTexture("resources/dog_tilesheet.png", 0);
TextureRef bgRef =
textureManager.LoadTexture("resources/bg.png", 0);
TextureRef fontRef =
textureManager.LoadTexture("resources/dialogue.png", 0);
animations.push_back(Animation({{32, 0.1}, {33, 0.1}, {34, 0.1},
{35, 0.1}, {36, 0.1}, {37, 0.1}}));
animations.push_back(Animation({{48, 0.1}, {49, 0.1}, {50, 0.1},
{51, 0.1}, {52, 0.1}, {53, 0.1},
{54, 0.1}, {55, 0.1}, {56, 0.1},
{57, 0.1}, {58, 0.1}, {59, 0.1}}));
std::unique_ptr<Font> font;
{
std::unique_ptr<PixelData> pd = LoadToPixelData("resources/dialogue.png");
font = Font::MakeFont(*pd.get());
}
std::unique_ptr<Text> text = Text::MakeText(font.get());
for (auto c : "And so begins our hero's adventure!") {
text->AddCharacter(c);
}
auto jump_state_system = MakeJumpStateSystem();
auto lr_state_system = MakeLRStateSystem();
auto box_state_system = MakeBoxStateSystem();
Camera camera({0, 0}, {SCREEN_WIDTH_TILES, SCREEN_HEIGHT_TILES});
BoundingBoxGraphicsSystem bb_graphics(&geometryManager, colorProgram.get());
SubSpriteGraphicsSystem ss_graphics(&geometryManager, textureProgram.get(),
&textureManager);
AnimationSystem animation_system(&animations);
Map level;
if (level.LoadTmx("resources/test.tmx")) {
std::cout << "Error loading test.tmx" << std::endl;
}
const TileMap* collision_map = level.GetLayer("Collision");
assert(collision_map);
Physics physics(collision_map);
const TileMap* tilemap = level.GetLayer("Tiles");
assert(tilemap);
TextureRef tileMapRef = textureManager.LoadTilemapTexture(*tilemap);
TextureRef collisionMapRef = textureManager.LoadTilemapTexture(*collision_map);
EntityManager em;
std::vector<Entity> bogs;
{
// Les' make us a bawg.
const MapObject* mo = level.GetNamedObject("bog-start");
assert(mo);
EntityId id = em.CreateEntity();
bogs.emplace_back(id);
bogs.back().AddComponent(std::unique_ptr<Transform>(new Transform()));
bogs.back().AddComponent(std::unique_ptr<Body>(
new Body(true, {mo->pos, 0.9, 0.75}, {0, 0})));
bogs.back().AddComponent(std::unique_ptr<JumpStateComponent>(
new JumpStateComponent(JumpState::STANDING)));
bogs.back().AddComponent(std::unique_ptr<LRStateComponent>(
new LRStateComponent(LRState::STILL)));
bogs.back().AddComponent(std::unique_ptr<Sprite>(
new Sprite(dogRef, 0, Orientation::NORMAL, {-1.0/16.0, 0})));
bogs.back().AddComponent(std::unique_ptr<AnimationComponent>(
new AnimationComponent(0)));
const MapObject* box_mo = level.GetNamedObject("box");
assert(box_mo);
id = em.CreateEntity();
bogs.emplace_back(id);
bogs.back().AddComponent(std::unique_ptr<Transform>(new Transform()));
bogs.back().AddComponent(std::unique_ptr<Body>(
new Body(true, {box_mo->pos, 0.9, 0.9}, {0, 0})));
bogs.back().AddComponent(std::unique_ptr<BoxStateComponent>(
new BoxStateComponent(BoxState::AT_REST)));
bogs.back().AddComponent(std::unique_ptr<Sprite>(
new Sprite(tileSetRef, 1 /* block index */, Orientation::NORMAL, {0,0})));
}
bool running = true;
bool paused = true;
bool debug = false;
int frames = 0;
int logic_frames = 0;
double t = 0;
double time_scale = 1;
int last_ticks = SDL_GetTicks();
const int logics_per_second = 50;
const int ticks_per_logic = 1000 / logics_per_second;
int next_logic_ticks = last_ticks + ticks_per_logic;
while (running) {
last_ticks = SDL_GetTicks();
while (last_ticks >= next_logic_ticks) {
for (ButtonEvent event : GetButtonEvents()) {
if (event.button_state() == ButtonState::RELEASED) {
switch (event.button()) {
case Button::QUIT:
running = false;
continue;
case Button::PAUSE:
paused = !paused;
continue;
case Button::DEBUG:
debug = !debug;
continue;
default:
break;
}
} else if (event.button_state() == ButtonState::PRESSED) {
switch (event.button()) {
case Button::PLUS:
time_scale *= 1.2;
break;
case Button::MINUS:
time_scale /= 1.2;
break;
default:
break;
}
}
// May want to prevent input from triggering two immediate state
// changes?
jump_state_system->HandleEvent(&event, bogs);
lr_state_system->HandleEvent(&event, bogs);
// TODO: Seems like there should be a way to manage the state systems,
// this will get out of hand :P
box_state_system->HandleEvent(&event, bogs);
}
// Fixed timestep
double dt = (double)(ticks_per_logic) / 1000.0;
if (!paused) {
jump_state_system->Update(dt, bogs);
lr_state_system->Update(dt, bogs);
box_state_system->Update(dt, bogs);
animation_system.Update(dt, bogs);
std::vector<std::unique_ptr<Event>> events = physics.Update(dt, bogs);
for (const auto& event : events) {
auto* collision = static_cast<CollisionEvent*>(event.get());
jump_state_system->HandleEvent(event.get(), bogs);
lr_state_system->HandleEvent(event.get(), bogs);
box_state_system->HandleEvent(event.get(), bogs);
}
// Interpolate camera to Bog.
vec2f bog_pos = bogs.at(0).GetComponent<Body>()->bbox.lowerLeft;
camera.center(bog_pos*0.2 + camera.center()*0.8);
// for (const Collision& c : collisions) {
// std::cout << "a " << c.first << " b " << c.second << " @ (" << c.fix.x
// << "," << c.fix.y << ")" << std::endl;
// }
}
last_ticks = SDL_GetTicks();
next_logic_ticks += ticks_per_logic;
++logic_frames;
t += dt;
if (logic_frames % 100 == 0) {
std::cout << "Logic/s: " << (float)logic_frames / t << std::endl;
}
}
frames++;
// Begin drawing
display.Clear();
glDisable(GL_DEPTH_TEST);
textureManager.BindTexture(bgRef, 0);
textureManager.BindTexture(-1, 1);
textureProgram->Use();
textureProgram->Setup();
// bg is four times as large as a tile.
// Change the src coords based on camera to give a parallax vibe.
geometryManager.DrawSubTexture(camera.center().x / 16,
camera.center().y / 16,
camera.half_size().x / 2,
camera.half_size().y / 2,
-1, -1, 2, 2);
textureManager.BindTexture(dogRef, 0);
textureManager.BindTexture(-1, 1);
textureProgram->Use();
textureProgram->Setup();
if (debug) {
bb_graphics.Update(0 /* unused */, camera, bogs);
}
ss_graphics.Update(0 /* unused */, camera, bogs);
textureManager.BindTexture(tileSetRef, 0);
textureManager.BindTexture(tileMapRef, 1);
tileProgram->Use();
tileProgram->map_offset(camera.center() - camera.half_size());
tileProgram->Setup();
geometryManager.DrawTileMap(camera);
if (debug) {
textureManager.BindTexture(collisionSetRef, 0);
textureManager.BindTexture(collisionMapRef, 1);
tileProgram->Use();
tileProgram->map_offset(camera.center() - camera.half_size());
tileProgram->Setup();
geometryManager.DrawTileMap(camera);
}
textureManager.BindTexture(fontRef, 0);
textureManager.BindTexture(-1, 1);
textProgram->Use();
textProgram->offset({-0.5,0.3});
textProgram->scale(2.0/15.0);
textProgram->Setup();
text->Draw();
glEnable(GL_DEPTH_TEST);
display.Swap();
if (frames % 100 == 0) {
std::cout << "Graphics/s: " << (float)frames / t << std::endl;
}
}
return 0;
}