-
Notifications
You must be signed in to change notification settings - Fork 9
/
lesson03.cc
109 lines (89 loc) · 2.83 KB
/
lesson03.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
//
// SDL2pp tutorial
//
// Written in 2015 by Dmitry Marakasiv <amdmi3@amdmi3.ru>
//
// To the extent possible under law, the author(s) have dedicated all copyright
// and related and neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along with
// this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//
//
// Lesson 03:
//
// - Timing and animation
//
#include <iostream>
#include <exception>
#include <SDL2pp/SDL2pp.hh>
using namespace SDL2pp;
int main() try {
// Initialize SDL library
SDL sdl(SDL_INIT_VIDEO);
// Create main window: 640x480 dimensions, resizable, "SDL2pp demo" title
Window window("SDL2pp demo",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
640, 480,
SDL_WINDOW_RESIZABLE);
// Create accelerated video renderer with default driver
Renderer renderer(window, -1, SDL_RENDERER_ACCELERATED);
// Load sprites image as a new texture
Texture sprites(renderer, DATA_PATH "/M484SpaceSoldier.png");
// Game state
int run_phase = -1; // run animation phase
float position = 0.0; // player position
unsigned int prev_ticks = SDL_GetTicks();
// Main loop
while (1) {
// Timing: calculate difference between this and previous frame
// in milliseconds
unsigned int frame_ticks = SDL_GetTicks();
unsigned int frame_delta = frame_ticks - prev_ticks;
prev_ticks = frame_ticks;
// Event processing:
// - If window is closed, or Q or Escape buttons are pressed,
// quit the application
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
return 0;
} else if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym) {
case SDLK_ESCAPE: case SDLK_q:
return 0;
}
}
}
// Update game state for this frame:
// if character is runnung, move it to the right
position += frame_delta * 0.2;
run_phase = (frame_ticks / 100) % 8;
// If player passes past the right side of the window, wrap him
// to the left side
if (position > renderer.GetOutputWidth())
position = -50;
int vcenter = renderer.GetOutputHeight() / 2; // Y coordinate of window center
// Clear screen
renderer.Clear();
// Pick sprite from running animantion sequence
int src_x = 8 + 51 * run_phase, src_y = 67;
// Draw player sprite
renderer.Copy(
sprites,
Rect(src_x, src_y, 50, 50),
Rect((int)position, vcenter - 50, 50, 50)
);
// Show rendered frame
renderer.Present();
// Frame limiter: sleep for a little bit to not eat 100% of CPU
SDL_Delay(1);
}
// Here all resources are automatically released and library deinitialized
return 0;
} catch (std::exception& e) {
// If case of error, print it and exit with error
std::cerr << e.what() << std::endl;
return 1;
}