diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-01-30 05:30:22 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-01-30 05:30:22 -0500 |
commit | 78e5bd2e622204d0deab252d9b2ab90c3095b67d (patch) | |
tree | 56fe7cfac1b46a32b3eb0949c139bdb56d436918 /src | |
parent | 0334d396e4ff1b2f8a1e147b753a4de34f55c08b (diff) | |
download | tanetane-78e5bd2e622204d0deab252d9b2ab90c3095b67d.tar.gz tanetane-78e5bd2e622204d0deab252d9b2ab90c3095b67d.tar.bz2 tanetane-78e5bd2e622204d0deab252d9b2ab90c3095b67d.zip |
Player character can move now
Diffstat (limited to 'src')
-rw-r--r-- | src/main.cpp | 41 | ||||
-rw-r--r-- | src/sprite.h | 4 |
2 files changed, 40 insertions, 5 deletions
diff --git a/src/main.cpp b/src/main.cpp index e91f8f0..8bd7f89 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
@@ -4,20 +4,51 @@ | |||
4 | 4 | ||
5 | void loop(Renderer& renderer) { | 5 | void loop(Renderer& renderer) { |
6 | Game game; | 6 | Game game; |
7 | game.addSprite(Sprite("../res/lucas.png", renderer)); | 7 | int playerSpriteId = game.addSprite(Sprite("../res/lucas.png", renderer)); |
8 | 8 | ||
9 | renderer.render(game); | 9 | renderer.render(game); |
10 | 10 | ||
11 | size_t inputDt = 50; | ||
12 | size_t inputAcc = 0; | ||
13 | |||
14 | size_t lastTime = SDL_GetTicks(); | ||
15 | |||
11 | for (;;) { | 16 | for (;;) { |
17 | size_t currentTime = SDL_GetTicks(); | ||
18 | size_t frameTime = currentTime - lastTime; | ||
19 | lastTime = currentTime; | ||
20 | |||
12 | SDL_Event e; | 21 | SDL_Event e; |
13 | while (SDL_PollEvent(&e)) | 22 | while (SDL_PollEvent(&e)) { |
14 | { | 23 | if (e.type == SDL_QUIT) { |
15 | if (e.type == SDL_QUIT) | ||
16 | { | ||
17 | return; | 24 | return; |
18 | } | 25 | } |
19 | } | 26 | } |
20 | 27 | ||
28 | Sprite& playerSprite = game.getSprite(playerSpriteId); | ||
29 | const Uint8* state = SDL_GetKeyboardState(NULL); | ||
30 | |||
31 | inputAcc += frameTime; | ||
32 | while (inputAcc > inputDt) { | ||
33 | inputAcc -= inputDt; | ||
34 | |||
35 | if (state[SDL_SCANCODE_LEFT]) { | ||
36 | playerSprite.setX(playerSprite.getX()-8); | ||
37 | } | ||
38 | |||
39 | if (state[SDL_SCANCODE_RIGHT]) { | ||
40 | playerSprite.setX(playerSprite.getX()+8); | ||
41 | } | ||
42 | |||
43 | if (state[SDL_SCANCODE_UP]) { | ||
44 | playerSprite.setY(playerSprite.getY()-8); | ||
45 | } | ||
46 | |||
47 | if (state[SDL_SCANCODE_DOWN]) { | ||
48 | playerSprite.setY(playerSprite.getY()+8); | ||
49 | } | ||
50 | } | ||
51 | |||
21 | renderer.render(game); | 52 | renderer.render(game); |
22 | } | 53 | } |
23 | } | 54 | } |
diff --git a/src/sprite.h b/src/sprite.h index 3af9341..d868bdb 100644 --- a/src/sprite.h +++ b/src/sprite.h | |||
@@ -15,6 +15,10 @@ public: | |||
15 | 15 | ||
16 | int getX() { return x_; } | 16 | int getX() { return x_; } |
17 | 17 | ||
18 | void setX(int x) { x_ = x; } | ||
19 | |||
20 | void setY(int y) { y_ = y; } | ||
21 | |||
18 | int getY() { return y_; } | 22 | int getY() { return y_; } |
19 | 23 | ||
20 | int getWidth() { return width_; } | 24 | int getWidth() { return width_; } |