summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt1
-rw-r--r--src/consts.h2
-rw-r--r--src/game.h7
-rw-r--r--src/main.cpp27
-rw-r--r--src/party.cpp42
-rw-r--r--src/party.h23
-rw-r--r--src/renderer.cpp2
-rw-r--r--src/sprite.h19
-rw-r--r--src/vector.h102
9 files changed, 196 insertions, 29 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 590577a..3b46e02 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt
@@ -29,6 +29,7 @@ add_executable(tanetane
29 src/main.cpp 29 src/main.cpp
30 src/renderer.cpp 30 src/renderer.cpp
31 src/sprite.cpp 31 src/sprite.cpp
32 src/party.cpp
32) 33)
33 34
34set_property(TARGET tanetane PROPERTY CXX_STANDARD 17) 35set_property(TARGET tanetane PROPERTY CXX_STANDARD 17)
diff --git a/src/consts.h b/src/consts.h index 2088f45..96176a4 100644 --- a/src/consts.h +++ b/src/consts.h
@@ -4,4 +4,6 @@
4const int GAME_WIDTH = 640; 4const int GAME_WIDTH = 640;
5const int GAME_HEIGHT = 480; 5const int GAME_HEIGHT = 480;
6 6
7const int MOVEMENT_SPEED = 4;
8
7#endif /* end of include guard: CONSTS_H_9561E49C */ 9#endif /* end of include guard: CONSTS_H_9561E49C */
diff --git a/src/game.h b/src/game.h index 46f1ab2..bb99543 100644 --- a/src/game.h +++ b/src/game.h
@@ -4,6 +4,13 @@
4#include <vector> 4#include <vector>
5#include "sprite.h" 5#include "sprite.h"
6 6
7struct Input {
8 bool left = false;
9 bool right = false;
10 bool up = false;
11 bool down = false;
12};
13
7class Game { 14class Game {
8public: 15public:
9 16
diff --git a/src/main.cpp b/src/main.cpp index 8bd7f89..b282a2e 100644 --- a/src/main.cpp +++ b/src/main.cpp
@@ -1,11 +1,17 @@
1#include <iostream> 1#include <iostream>
2#include "renderer.h" 2#include "renderer.h"
3#include "game.h" 3#include "game.h"
4#include "party.h"
4 5
5void loop(Renderer& renderer) { 6void loop(Renderer& renderer) {
6 Game game; 7 Game game;
7 int playerSpriteId = game.addSprite(Sprite("../res/lucas.png", renderer)); 8 int playerSpriteId = game.addSprite(Sprite("../res/lucas.png", renderer));
8 9
10 Input keystate;
11
12 Party party;
13 party.addMember(playerSpriteId);
14
9 renderer.render(game); 15 renderer.render(game);
10 16
11 size_t inputDt = 50; 17 size_t inputDt = 50;
@@ -25,28 +31,17 @@ void loop(Renderer& renderer) {
25 } 31 }
26 } 32 }
27 33
28 Sprite& playerSprite = game.getSprite(playerSpriteId);
29 const Uint8* state = SDL_GetKeyboardState(NULL); 34 const Uint8* state = SDL_GetKeyboardState(NULL);
35 keystate.left = state[SDL_SCANCODE_LEFT];
36 keystate.right = state[SDL_SCANCODE_RIGHT];
37 keystate.up = state[SDL_SCANCODE_UP];
38 keystate.down = state[SDL_SCANCODE_DOWN];
30 39
31 inputAcc += frameTime; 40 inputAcc += frameTime;
32 while (inputAcc > inputDt) { 41 while (inputAcc > inputDt) {
33 inputAcc -= inputDt; 42 inputAcc -= inputDt;
34 43
35 if (state[SDL_SCANCODE_LEFT]) { 44 party.move(game, keystate);
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 } 45 }
51 46
52 renderer.render(game); 47 renderer.render(game);
diff --git a/src/party.cpp b/src/party.cpp new file mode 100644 index 0000000..559aacb --- /dev/null +++ b/src/party.cpp
@@ -0,0 +1,42 @@
1#include "party.h"
2#include "consts.h"
3
4void Party::addMember(int spriteId) {
5 PartyMember newMember;
6 newMember.spriteId = spriteId;
7
8 members_.push_back(std::move(newMember));
9}
10
11void Party::move(Game& game, const Input& keystate) {
12 if (members_.empty()) {
13 return;
14 }
15
16 Sprite& p1 = game.getSprite(members_[0].spriteId);
17 vec2i pLoc = p1.loc();
18
19 if (keystate.up)
20 {
21 pLoc.y() -= MOVEMENT_SPEED;
22 }
23
24 if (keystate.down)
25 {
26 pLoc.y() += MOVEMENT_SPEED;
27 }
28
29 if (keystate.left)
30 {
31 pLoc.x() -= MOVEMENT_SPEED;
32 }
33
34 if (keystate.right)
35 {
36 pLoc.x() += MOVEMENT_SPEED;
37 }
38
39 if (pLoc != p1.loc()) {
40 p1.loc() = pLoc;
41 }
42} \ No newline at end of file
diff --git a/src/party.h b/src/party.h new file mode 100644 index 0000000..18c57b5 --- /dev/null +++ b/src/party.h
@@ -0,0 +1,23 @@
1#ifndef PARTY_H_826F91BA
2#define PARTY_H_826F91BA
3
4#include <vector>
5#include "game.h"
6
7class Party {
8public:
9
10 void addMember(int spriteId);
11
12 void move(Game& game, const Input& keystate);
13
14private:
15
16 struct PartyMember {
17 int spriteId;
18 };
19
20 std::vector<PartyMember> members_;
21};
22
23#endif /* end of include guard: PARTY_H_826F91BA */
diff --git a/src/renderer.cpp b/src/renderer.cpp index 3bd0beb..bcc3e95 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp
@@ -49,7 +49,7 @@ void Renderer::render(Game& game) {
49 SDL_RenderClear(ren_.get()); 49 SDL_RenderClear(ren_.get());
50 50
51 for (Sprite& sprite : game.getSprites()) { 51 for (Sprite& sprite : game.getSprites()) {
52 SDL_Rect dest { sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight() }; 52 SDL_Rect dest { sprite.loc().x(), sprite.loc().y(), sprite.size().w(), sprite.size().h() };
53 SDL_RenderCopy(ren_.get(), textures_.at(sprite.getTextureId()).get(), nullptr, &dest); 53 SDL_RenderCopy(ren_.get(), textures_.at(sprite.getTextureId()).get(), nullptr, &dest);
54 } 54 }
55 55
diff --git a/src/sprite.h b/src/sprite.h index d868bdb..c45e208 100644 --- a/src/sprite.h +++ b/src/sprite.h
@@ -3,6 +3,7 @@
3 3
4#include <string_view> 4#include <string_view>
5#include "renderer.h" 5#include "renderer.h"
6#include "vector.h"
6 7
7class Sprite { 8class Sprite {
8public: 9public:
@@ -13,25 +14,19 @@ public:
13 return textureId_; 14 return textureId_;
14 } 15 }
15 16
16 int getX() { return x_; } 17 const vec2i& loc() const { return loc_; }
17 18
18 void setX(int x) { x_ = x; } 19 vec2i& loc() { return loc_; }
19 20
20 void setY(int y) { y_ = y; } 21 const vec2i& size() const { return size_; }
21 22
22 int getY() { return y_; } 23 vec2i& size() { return size_; }
23
24 int getWidth() { return width_; }
25
26 int getHeight() { return height_; }
27 24
28private: 25private:
29 26
30 int textureId_; 27 int textureId_;
31 int x_ = 0; 28 vec2i loc_ { 0, 0 };
32 int y_ = 0; 29 vec2i size_ { 17*4, 27 * 4 };
33 int width_ = 17*4;
34 int height_ = 27*4;
35}; 30};
36 31
37#endif /* end of include guard: SPRITE_H_70503825 */ 32#endif /* end of include guard: SPRITE_H_70503825 */
diff --git a/src/vector.h b/src/vector.h new file mode 100644 index 0000000..9973ea6 --- /dev/null +++ b/src/vector.h
@@ -0,0 +1,102 @@
1#ifndef VECTOR_H_5458ED71
2#define VECTOR_H_5458ED71
3
4template <typename T>
5class vec2 {
6public:
7
8 T coords[2];
9
10 vec2() : coords{0, 0} {
11 }
12
13 vec2(T x, T y) : coords{x, y} {
14 }
15
16 inline T& x() {
17 return coords[0];
18 }
19
20 inline const T& x() const {
21 return coords[0];
22 }
23
24 inline T& w() {
25 return coords[0];
26 }
27
28 inline const T& w() const {
29 return coords[0];
30 }
31
32 inline T& y() {
33 return coords[1];
34 }
35
36 inline const T& y() const {
37 return coords[1];
38 }
39
40 inline T& h() {
41 return coords[1];
42 }
43
44 inline const T& h() const {
45 return coords[1];
46 }
47
48 template <typename R>
49 operator vec2<R>() const {
50 return vec2<R>(x(), y());
51 }
52
53 vec2 operator+(const vec2& other) const {
54 return vec2(x() + other.x(), y() + other.y());
55 }
56
57 vec2& operator+=(const vec2& other) {
58 x() += other.x();
59 y() += other.y();
60
61 return *this;
62 }
63
64 vec2 operator-(const vec2& other) const {
65 return vec2(x() - other.x(), y() - other.y());
66 }
67
68 vec2 operator-=(const vec2& other) {
69 x() -= other.x();
70 y() -= other.y();
71
72 return *this;
73 }
74
75 vec2 operator-() const {
76 return vec2(-x(), -y());
77 }
78
79 vec2 operator*(T s) const {
80 return vec2(x() * s, y() * s);
81 }
82
83 vec2& operator*=(T s) {
84 x() *= s;
85 y() *= s;
86
87 return *this;
88 }
89
90 bool operator==(const vec2& other) const {
91 return std::tie(x(), other.x()) == std::tie(y(), other.y());
92 }
93
94 bool operator!=(const vec2& other) const {
95 return std::tie(x(), other.x()) != std::tie(y(), other.y());
96 }
97
98};
99
100using vec2i = vec2<int>;
101
102#endif /* end of include guard: VECTOR_H_5458ED71 */