-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_sprite.cpp
More file actions
77 lines (63 loc) · 2.05 KB
/
2_sprite.cpp
File metadata and controls
77 lines (63 loc) · 2.05 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
#include <game/window.h>
#include <game/scene.h>
#include <game/sprite.h>
#include <game/clip.h>
#include <game/image.h>
#include <game/frame.h>
class MyScene : public Scene {
using Scene::Scene;
public:
// Sprite holds atlas image and its frame related data
Sprite* sprite;
SDL_FRect position;
int currentFrame = 0;
virtual void prepare() {
// Create sprite instanse with default config
sprite = new Sprite(
new Image(renderer, "doc/images/planet.png"),
100,
100
);
sprite->addClip(
1, // Clip index
1, // Start row in sprite sheet
1, // Start cell in sprite sheet
24 // Frame count to generate from row, cell
// We know our sprite contains 6x4 frames so 24 is total frame count
);
// Scale the frame a bit from its original size
position.w = 200;
position.h = 200;
}
virtual void update(State* state) {
// Here we center image based on scene width and height
// No matter window size it will always stay in center
position.x = this->width/2 - position.w/2;
position.y = this->height/2 - position.h/2;
// Increase current frame
currentFrame++;
// Reset current frame to 0 if it becomes 24
if (currentFrame == sprite->clips[1]->getFrameCount()) {
currentFrame = 0;
}
}
virtual void render(State* state) {
clear();
// Here we specify what to render from image with &frame
// and where to render on scene with &position
sprite->image->render(
// From clip with index 1
sprite->getClip(1)->getFrame(currentFrame)->getRect(),
&position
);
present();
}
};
int main(int argc, char** argv){
// Just create the window
Window* window = new Window("Hello World", 800, 600);
// Pass our scene instanse
window->setScene(new MyScene(window->window));
// Do the thing
return window->run();
}