diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2019-03-16 17:45:16 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2019-03-16 17:45:16 -0400 |
commit | 4a03d938451763dfb95b534f9002865e72ea7ec5 (patch) | |
tree | 9f2a0f074869009760fc33af5032c6de13da897e /src | |
parent | 57fe8f3c4124819b95164547333a33f4c45eac8d (diff) | |
download | dispatcher-4a03d938451763dfb95b534f9002865e72ea7ec5.tar.gz dispatcher-4a03d938451763dfb95b534f9002865e72ea7ec5.tar.bz2 dispatcher-4a03d938451763dfb95b534f9002865e72ea7ec5.zip |
Diffstat (limited to 'src')
-rw-r--r-- | src/consts.h | 7 | ||||
-rw-r--r-- | src/editor.cpp | 39 | ||||
-rw-r--r-- | src/input.h | 45 | ||||
-rw-r--r-- | src/rectangle.h | 36 | ||||
-rw-r--r-- | src/vector.h | 18 |
5 files changed, 141 insertions, 4 deletions
diff --git a/src/consts.h b/src/consts.h index a6b5094..4e33922 100644 --- a/src/consts.h +++ b/src/consts.h | |||
@@ -2,6 +2,7 @@ | |||
2 | #define CONSTS_H_8019F1B3 | 2 | #define CONSTS_H_8019F1B3 |
3 | 3 | ||
4 | #include "vector.h" | 4 | #include "vector.h" |
5 | #include "rectangle.h" | ||
5 | 6 | ||
6 | constexpr vec2s TILE_SIZE { 32, 32 }; | 7 | constexpr vec2s TILE_SIZE { 32, 32 }; |
7 | constexpr vec2s MAP_SIZE { 16, 16 }; | 8 | constexpr vec2s MAP_SIZE { 16, 16 }; |
@@ -9,4 +10,10 @@ constexpr vec2s LEVEL_SIZE = TILE_SIZE * MAP_SIZE; | |||
9 | constexpr vec2s BORDER_SIZE = TILE_SIZE; | 10 | constexpr vec2s BORDER_SIZE = TILE_SIZE; |
10 | constexpr vec2s WINDOW_SIZE = LEVEL_SIZE * vec2s { 2, 1 } + BORDER_SIZE * 2; | 11 | constexpr vec2s WINDOW_SIZE = LEVEL_SIZE * vec2s { 2, 1 } + BORDER_SIZE * 2; |
11 | 12 | ||
13 | constexpr rect4s EDITOR_MAP_AREA { BORDER_SIZE, LEVEL_SIZE }; | ||
14 | constexpr rect4s EDITOR_TILESET_AREA { | ||
15 | { BORDER_SIZE.w() * 2 + LEVEL_SIZE.w(), BORDER_SIZE.h() }, | ||
16 | { TILE_SIZE.w(), LEVEL_SIZE.h() } | ||
17 | }; | ||
18 | |||
12 | #endif /* end of include guard: CONSTS_H_8019F1B3 */ | 19 | #endif /* end of include guard: CONSTS_H_8019F1B3 */ |
diff --git a/src/editor.cpp b/src/editor.cpp index 30dde45..8ca850a 100644 --- a/src/editor.cpp +++ b/src/editor.cpp | |||
@@ -63,7 +63,7 @@ void Editor::tick( | |||
63 | } | 63 | } |
64 | 64 | ||
65 | // Check for keypress changes since the last tick. | 65 | // Check for keypress changes since the last tick. |
66 | input_.tick(keystate); | 66 | input_.tick(); |
67 | 67 | ||
68 | if (input_.wasPressed(SDL_SCANCODE_Z)) | 68 | if (input_.wasPressed(SDL_SCANCODE_Z)) |
69 | { | 69 | { |
@@ -130,10 +130,45 @@ void Editor::tick( | |||
130 | mapFocus_ = !mapFocus_; | 130 | mapFocus_ = !mapFocus_; |
131 | } | 131 | } |
132 | 132 | ||
133 | bool newlyTabbed = false; | ||
134 | |||
135 | if (input_.wasClicked(SDL_BUTTON_LEFT)) | ||
136 | { | ||
137 | if (EDITOR_MAP_AREA.contains(input_.getMousePos())) | ||
138 | { | ||
139 | if (!mapFocus_) | ||
140 | { | ||
141 | newlyTabbed = true; | ||
142 | } | ||
143 | |||
144 | mapFocus_ = true; | ||
145 | } else if (EDITOR_TILESET_AREA.contains(input_.getMousePos())) | ||
146 | { | ||
147 | if (mapFocus_) | ||
148 | { | ||
149 | newlyTabbed = true; | ||
150 | } | ||
151 | |||
152 | mapFocus_ = false; | ||
153 | |||
154 | selectedTile_ = | ||
155 | (input_.getMousePos() - EDITOR_TILESET_AREA.pos).y() / TILE_SIZE.h(); | ||
156 | } | ||
157 | } | ||
158 | |||
133 | if (mapFocus_) | 159 | if (mapFocus_) |
134 | { | 160 | { |
161 | if (EDITOR_MAP_AREA.contains(input_.getMousePos()) && | ||
162 | ( input_.hasMouseMoved() || newlyTabbed )) | ||
163 | { | ||
164 | cursor_ = (input_.getMousePos() - EDITOR_MAP_AREA.pos) / TILE_SIZE; | ||
165 | } | ||
166 | |||
135 | if (keystate[SDL_SCANCODE_RETURN] || | 167 | if (keystate[SDL_SCANCODE_RETURN] || |
136 | keystate[SDL_SCANCODE_SPACE]) | 168 | keystate[SDL_SCANCODE_SPACE] || |
169 | ( input_.isClicked(SDL_BUTTON_LEFT) && | ||
170 | EDITOR_MAP_AREA.contains(input_.getMousePos()) && | ||
171 | !newlyTabbed )) | ||
137 | { | 172 | { |
138 | level_.at(cursor_.x(), cursor_.y(), layer_) = selectedTile_; | 173 | level_.at(cursor_.x(), cursor_.y(), layer_) = selectedTile_; |
139 | 174 | ||
diff --git a/src/input.h b/src/input.h index ad8b761..9f6ff47 100644 --- a/src/input.h +++ b/src/input.h | |||
@@ -5,7 +5,7 @@ | |||
5 | #include <vector> | 5 | #include <vector> |
6 | 6 | ||
7 | /** | 7 | /** |
8 | * Helper class that detects when a key is newly pressed. | 8 | * Helper class that detects when a key or mouse button is newly pressed. |
9 | */ | 9 | */ |
10 | class Input { | 10 | class Input { |
11 | public: | 11 | public: |
@@ -16,12 +16,22 @@ public: | |||
16 | 16 | ||
17 | curstate_.assign(s, s + length_); | 17 | curstate_.assign(s, s + length_); |
18 | prevstate_ = curstate_; | 18 | prevstate_ = curstate_; |
19 | |||
20 | prevmouse_ = SDL_GetMouseState(&mousePos_.x(), &mousePos_.y()); | ||
21 | prevMousePos_ = mousePos_; | ||
22 | curmouse_ = prevmouse_; | ||
19 | } | 23 | } |
20 | 24 | ||
21 | void tick(const Uint8* keystate) | 25 | void tick() |
22 | { | 26 | { |
27 | const Uint8* keystate = SDL_GetKeyboardState(nullptr); | ||
28 | |||
23 | prevstate_ = std::move(curstate_); | 29 | prevstate_ = std::move(curstate_); |
24 | curstate_.assign(keystate, keystate + length_); | 30 | curstate_.assign(keystate, keystate + length_); |
31 | |||
32 | prevmouse_ = curmouse_; | ||
33 | prevMousePos_ = mousePos_; | ||
34 | curmouse_ = SDL_GetMouseState(&mousePos_.x(), &mousePos_.y()); | ||
25 | } | 35 | } |
26 | 36 | ||
27 | bool wasPressed(int scancode) const | 37 | bool wasPressed(int scancode) const |
@@ -29,11 +39,42 @@ public: | |||
29 | return curstate_.at(scancode) && !prevstate_.at(scancode); | 39 | return curstate_.at(scancode) && !prevstate_.at(scancode); |
30 | } | 40 | } |
31 | 41 | ||
42 | bool wasClicked(Uint32 mask) const | ||
43 | { | ||
44 | return (curmouse_ & SDL_BUTTON(mask)) && !(prevmouse_ & SDL_BUTTON(mask)); | ||
45 | } | ||
46 | |||
47 | bool wasReleased(Uint32 mask) const | ||
48 | { | ||
49 | return !(curmouse_ & SDL_BUTTON(mask)) && (prevmouse_ & SDL_BUTTON(mask)); | ||
50 | } | ||
51 | |||
52 | bool isClicked(Uint32 mask) const | ||
53 | { | ||
54 | return curmouse_ & SDL_BUTTON(mask); | ||
55 | } | ||
56 | |||
57 | const vec2i& getMousePos() const | ||
58 | { | ||
59 | return mousePos_; | ||
60 | } | ||
61 | |||
62 | bool hasMouseMoved() const | ||
63 | { | ||
64 | return mousePos_ != prevMousePos_; | ||
65 | } | ||
66 | |||
32 | private: | 67 | private: |
33 | 68 | ||
34 | int length_; | 69 | int length_; |
35 | std::vector<Uint8> curstate_; | 70 | std::vector<Uint8> curstate_; |
36 | std::vector<Uint8> prevstate_; | 71 | std::vector<Uint8> prevstate_; |
72 | |||
73 | Uint32 curmouse_; | ||
74 | Uint32 prevmouse_; | ||
75 | |||
76 | vec2i mousePos_; | ||
77 | vec2i prevMousePos_; | ||
37 | }; | 78 | }; |
38 | 79 | ||
39 | #endif /* end of include guard: INPUT_H_0FB34C42 */ | 80 | #endif /* end of include guard: INPUT_H_0FB34C42 */ |
diff --git a/src/rectangle.h b/src/rectangle.h new file mode 100644 index 0000000..7815b56 --- /dev/null +++ b/src/rectangle.h | |||
@@ -0,0 +1,36 @@ | |||
1 | #ifndef RECTANGLE_H_23049366 | ||
2 | #define RECTANGLE_H_23049366 | ||
3 | |||
4 | #include "vector.h" | ||
5 | |||
6 | template <typename T> | ||
7 | class rect { | ||
8 | public: | ||
9 | |||
10 | using vector_type = vec2<T>; | ||
11 | |||
12 | constexpr rect( | ||
13 | vector_type pos, | ||
14 | vector_type size) : | ||
15 | pos(pos), | ||
16 | size(size) | ||
17 | { | ||
18 | } | ||
19 | |||
20 | const vector_type pos; | ||
21 | const vector_type size; | ||
22 | |||
23 | bool contains(vector_type point) const | ||
24 | { | ||
25 | return ( | ||
26 | point.x() >= pos.x() && | ||
27 | point.x() < pos.x() + size.w() && | ||
28 | point.y() >= pos.y() && | ||
29 | point.y() < pos.y() + size.h()); | ||
30 | } | ||
31 | |||
32 | }; | ||
33 | |||
34 | using rect4s = rect<size_t>; | ||
35 | |||
36 | #endif /* end of include guard: RECTANGLE_H_23049366 */ | ||
diff --git a/src/vector.h b/src/vector.h index c207dc8..ed20936 100644 --- a/src/vector.h +++ b/src/vector.h | |||
@@ -112,11 +112,29 @@ public: | |||
112 | return vec2(x() * other.x(), y() * other.y()); | 112 | return vec2(x() * other.x(), y() * other.y()); |
113 | } | 113 | } |
114 | 114 | ||
115 | vec2& operator/=(T s) | ||
116 | { | ||
117 | x() /= s; | ||
118 | y() /= s; | ||
119 | |||
120 | return *this; | ||
121 | } | ||
122 | |||
123 | constexpr vec2 operator/(const vec2& other) const | ||
124 | { | ||
125 | return vec2(x() / other.x(), y() / other.y()); | ||
126 | } | ||
127 | |||
115 | constexpr bool operator==(const vec2& other) const | 128 | constexpr bool operator==(const vec2& other) const |
116 | { | 129 | { |
117 | return (x() == other.x()) && (y() == other.y()); | 130 | return (x() == other.x()) && (y() == other.y()); |
118 | } | 131 | } |
119 | 132 | ||
133 | constexpr bool operator!=(const vec2& other) const | ||
134 | { | ||
135 | return !(*this == other); | ||
136 | } | ||
137 | |||
120 | }; | 138 | }; |
121 | 139 | ||
122 | using vec2s = vec2<size_t>; | 140 | using vec2s = vec2<size_t>; |