-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglacial.cpp
More file actions
259 lines (235 loc) · 7.92 KB
/
glacial.cpp
File metadata and controls
259 lines (235 loc) · 7.92 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
#include <array>
#include <string_view>
#include <tuple>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
struct TextureSlice {
int x, y, w, h;
};
namespace {
const char* const TITLE = "Glacial Mountains - Parallax Example";
constexpr int WINDOW_WIDTH = 768;
constexpr int WINDOW_HEIGHT = 432;
const char* const TEXTURES_PATH = "rsc/glacial_mountains_textures.png";
constexpr int DEFAULT_WIDTH = 384;
constexpr int DEFAULT_HEIGHT = 216;
constexpr TextureSlice bg_clouds{0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT};
constexpr TextureSlice mountains{384, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT};
constexpr TextureSlice fg_clouds_2{0, 216, DEFAULT_WIDTH, DEFAULT_HEIGHT};
constexpr TextureSlice fg_clouds_1{384, 216, DEFAULT_WIDTH, DEFAULT_HEIGHT};
constexpr TextureSlice credits{384, 432, DEFAULT_WIDTH, DEFAULT_HEIGHT};
}
SDL_Texture* load_texture(SDL_Renderer* renderer, std::string_view path) {
SDL_Surface* surface = IMG_Load(path.data());
if (surface == nullptr) {
throw SDL_GetError();
}
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
return texture;
}
struct Sdl {
Sdl() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
throw SDL_GetError();
}
window = SDL_CreateWindow(
TITLE,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
WINDOW_WIDTH,
WINDOW_HEIGHT,
SDL_WINDOW_SHOWN// | SDL_WINDOW_FULLSCREEN_DESKTOP
);
if (window == nullptr) {
throw SDL_GetError();
}
renderer = SDL_CreateRenderer(
window,
-1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
);
if (renderer == nullptr) {
throw SDL_GetError();
}
}
~Sdl() {
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
SDL_Window* window;
SDL_Renderer* renderer;
};
struct SideScrolling {
float speed_factor = 0.0f; // 1: camera speed; 0.5: half that etc.
float screen_x = 0.0f;
};
enum class TitleEffectStatus {
INITIAL,
SCROLLING_IN,
AT_DESTINATION,
SCROLLING_OUT
};
struct TitleEffect {
float speed = 0.0f;
float screen_y = 0.0f;
uint32_t on_timeout = 0;
uint32_t off_timeout = 0;
uint32_t base_ticks = SDL_GetTicks();
TitleEffectStatus status = TitleEffectStatus::INITIAL;
};
struct Layer {
Layer(const TextureSlice& t_slice, SideScrolling side_scrolling) :
t_slice{t_slice},
side_scrolling{side_scrolling}
{
}
Layer(const TextureSlice& t_slice, TitleEffect title_effect) :
t_slice{t_slice},
title_effect{title_effect}
{
}
TextureSlice t_slice;
SideScrolling side_scrolling;
TitleEffect title_effect;
};
struct Camera {
float x_speed = 0;
};
struct Scene {
Scene(Sdl& sdl) :
texture{load_texture(sdl.renderer, TEXTURES_PATH)},
layers{
Layer{bg_clouds, SideScrolling{0.15f}},
Layer{mountains, SideScrolling{0.25f}},
Layer{credits, TitleEffect{-2.0f, WINDOW_HEIGHT, 2000, 3000}},
Layer{fg_clouds_2, SideScrolling{0.50f}},
Layer{fg_clouds_1, SideScrolling{0.75f}}
}
{
}
~Scene() {
SDL_DestroyTexture(texture);
}
SDL_Texture* texture;
std::array<Layer, 5> layers;
Camera camera{4.0f};
};
struct Timer {
Timer() : start{SDL_GetTicks()} {}
uint32_t elapsed() { return SDL_GetTicks() - start; }
uint32_t start;
};
bool handle_input() {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
return false;
}
if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
return false;
break;
}
}
}
return true;
}
inline bool has_side_scrolling(const Layer& layer) {
return layer.side_scrolling.speed_factor != 0.0f;
}
inline bool has_title_effect(const Layer& layer) {
return layer.title_effect.speed != 0.0f;
}
inline bool reached_destination(const TitleEffect& title_effect, float dest_y) {
return title_effect.speed < 0.0f && dest_y <= 0.0f
|| title_effect.speed > 0.0f && dest_y >= 0.0f;
}
inline bool went_off_screen(const TitleEffect& title_effect, float dest_y) {
return title_effect.speed < 0.0f && dest_y <= -WINDOW_HEIGHT
||title_effect.speed > 0.0f && dest_y >= WINDOW_HEIGHT;
}
void update_scene(Scene& scene, Timer& timer) {
for (Layer& layer : scene.layers) {
// side scrolling
if (has_side_scrolling(layer)) {
layer.side_scrolling.screen_x -= scene.camera.x_speed * layer.side_scrolling.speed_factor;
if (layer.side_scrolling.screen_x < -WINDOW_WIDTH) {
layer.side_scrolling.screen_x += WINDOW_WIDTH;
}
}
// title effect
if (has_title_effect(layer)) {
float elapsed = SDL_GetTicks() - layer.title_effect.base_ticks;
switch (layer.title_effect.status) {
case TitleEffectStatus::INITIAL:
if (elapsed >= layer.title_effect.on_timeout) {
layer.title_effect.status = TitleEffectStatus::SCROLLING_IN;
}
break;
case TitleEffectStatus::SCROLLING_IN: {
float dest_y = layer.title_effect.screen_y + layer.title_effect.speed;
if (reached_destination(layer.title_effect, dest_y)) {
dest_y = 0.0f;
layer.title_effect.status = TitleEffectStatus::AT_DESTINATION;
layer.title_effect.base_ticks = SDL_GetTicks();
}
layer.title_effect.screen_y = dest_y;
break;
}
case TitleEffectStatus::AT_DESTINATION:
if (elapsed >= layer.title_effect.off_timeout) {
layer.title_effect.status = TitleEffectStatus::SCROLLING_OUT;
}
break;
case TitleEffectStatus::SCROLLING_OUT: {
float dest_y = layer.title_effect.screen_y + layer.title_effect.speed;
if (went_off_screen(layer.title_effect, dest_y)) {
dest_y = layer.title_effect.speed < 0.0f ? WINDOW_HEIGHT : -WINDOW_HEIGHT;
layer.title_effect.status = TitleEffectStatus::INITIAL;
layer.title_effect.base_ticks = SDL_GetTicks();
}
layer.title_effect.screen_y = dest_y;
break;
}}
}
}
}
inline void render_layer(const Layer& layer, SDL_Renderer* renderer,
SDL_Texture* texture, int dest_x, int dest_y) {
SDL_Rect src{layer.t_slice.x, layer.t_slice.y, layer.t_slice.w, layer.t_slice.h};
SDL_Rect dst{dest_x, dest_y, WINDOW_WIDTH, WINDOW_HEIGHT};
SDL_RenderCopy(renderer, texture, &src, &dst);
}
void render_scene(Sdl& sdl, Scene& scene) {
// clear
SDL_SetRenderDrawColor(sdl.renderer, 0x08, 0xa9, 0xfc, 0xff);
SDL_RenderClear(sdl.renderer);
// layers
for (const Layer& layer : scene.layers) {
render_layer(layer, sdl.renderer, scene.texture, layer.side_scrolling.screen_x,
layer.title_effect.screen_y);
if (has_side_scrolling(layer)) {
int dest_x = layer.side_scrolling.screen_x;
for (int i = 0; i < 2; ++i) {
dest_x += WINDOW_WIDTH;
render_layer(layer, sdl.renderer, scene.texture, dest_x,
layer.title_effect.screen_y);
}
}
}
SDL_RenderPresent(sdl.renderer);
}
int main() {
Sdl sdl;
Scene scene{sdl};
Timer timer;
bool run = true;
while (run) {
run = handle_input();
update_scene(scene, timer);
render_scene(sdl, scene);
}
}