From 4a03d938451763dfb95b534f9002865e72ea7ec5 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 16 Mar 2019 17:45:16 -0400 Subject: Can click to place tiles in editor now --- src/consts.h | 7 +++++++ src/editor.cpp | 39 +++++++++++++++++++++++++++++++++++++-- src/input.h | 45 +++++++++++++++++++++++++++++++++++++++++++-- src/rectangle.h | 36 ++++++++++++++++++++++++++++++++++++ src/vector.h | 18 ++++++++++++++++++ 5 files changed, 141 insertions(+), 4 deletions(-) create mode 100644 src/rectangle.h 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 @@ #define CONSTS_H_8019F1B3 #include "vector.h" +#include "rectangle.h" constexpr vec2s TILE_SIZE { 32, 32 }; constexpr vec2s MAP_SIZE { 16, 16 }; @@ -9,4 +10,10 @@ constexpr vec2s LEVEL_SIZE = TILE_SIZE * MAP_SIZE; constexpr vec2s BORDER_SIZE = TILE_SIZE; constexpr vec2s WINDOW_SIZE = LEVEL_SIZE * vec2s { 2, 1 } + BORDER_SIZE * 2; +constexpr rect4s EDITOR_MAP_AREA { BORDER_SIZE, LEVEL_SIZE }; +constexpr rect4s EDITOR_TILESET_AREA { + { BORDER_SIZE.w() * 2 + LEVEL_SIZE.w(), BORDER_SIZE.h() }, + { TILE_SIZE.w(), LEVEL_SIZE.h() } +}; + #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( } // Check for keypress changes since the last tick. - input_.tick(keystate); + input_.tick(); if (input_.wasPressed(SDL_SCANCODE_Z)) { @@ -130,10 +130,45 @@ void Editor::tick( mapFocus_ = !mapFocus_; } + bool newlyTabbed = false; + + if (input_.wasClicked(SDL_BUTTON_LEFT)) + { + if (EDITOR_MAP_AREA.contains(input_.getMousePos())) + { + if (!mapFocus_) + { + newlyTabbed = true; + } + + mapFocus_ = true; + } else if (EDITOR_TILESET_AREA.contains(input_.getMousePos())) + { + if (mapFocus_) + { + newlyTabbed = true; + } + + mapFocus_ = false; + + selectedTile_ = + (input_.getMousePos() - EDITOR_TILESET_AREA.pos).y() / TILE_SIZE.h(); + } + } + if (mapFocus_) { + if (EDITOR_MAP_AREA.contains(input_.getMousePos()) && + ( input_.hasMouseMoved() || newlyTabbed )) + { + cursor_ = (input_.getMousePos() - EDITOR_MAP_AREA.pos) / TILE_SIZE; + } + if (keystate[SDL_SCANCODE_RETURN] || - keystate[SDL_SCANCODE_SPACE]) + keystate[SDL_SCANCODE_SPACE] || + ( input_.isClicked(SDL_BUTTON_LEFT) && + EDITOR_MAP_AREA.contains(input_.getMousePos()) && + !newlyTabbed )) { level_.at(cursor_.x(), cursor_.y(), layer_) = selectedTile_; 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 @@ #include /** - * Helper class that detects when a key is newly pressed. + * Helper class that detects when a key or mouse button is newly pressed. */ class Input { public: @@ -16,12 +16,22 @@ public: curstate_.assign(s, s + length_); prevstate_ = curstate_; + + prevmouse_ = SDL_GetMouseState(&mousePos_.x(), &mousePos_.y()); + prevMousePos_ = mousePos_; + curmouse_ = prevmouse_; } - void tick(const Uint8* keystate) + void tick() { + const Uint8* keystate = SDL_GetKeyboardState(nullptr); + prevstate_ = std::move(curstate_); curstate_.assign(keystate, keystate + length_); + + prevmouse_ = curmouse_; + prevMousePos_ = mousePos_; + curmouse_ = SDL_GetMouseState(&mousePos_.x(), &mousePos_.y()); } bool wasPressed(int scancode) const @@ -29,11 +39,42 @@ public: return curstate_.at(scancode) && !prevstate_.at(scancode); } + bool wasClicked(Uint32 mask) const + { + return (curmouse_ & SDL_BUTTON(mask)) && !(prevmouse_ & SDL_BUTTON(mask)); + } + + bool wasReleased(Uint32 mask) const + { + return !(curmouse_ & SDL_BUTTON(mask)) && (prevmouse_ & SDL_BUTTON(mask)); + } + + bool isClicked(Uint32 mask) const + { + return curmouse_ & SDL_BUTTON(mask); + } + + const vec2i& getMousePos() const + { + return mousePos_; + } + + bool hasMouseMoved() const + { + return mousePos_ != prevMousePos_; + } + private: int length_; std::vector curstate_; std::vector prevstate_; + + Uint32 curmouse_; + Uint32 prevmouse_; + + vec2i mousePos_; + vec2i prevMousePos_; }; #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 @@ +#ifndef RECTANGLE_H_23049366 +#define RECTANGLE_H_23049366 + +#include "vector.h" + +template +class rect { +public: + + using vector_type = vec2; + + constexpr rect( + vector_type pos, + vector_type size) : + pos(pos), + size(size) + { + } + + const vector_type pos; + const vector_type size; + + bool contains(vector_type point) const + { + return ( + point.x() >= pos.x() && + point.x() < pos.x() + size.w() && + point.y() >= pos.y() && + point.y() < pos.y() + size.h()); + } + +}; + +using rect4s = rect; + +#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: return vec2(x() * other.x(), y() * other.y()); } + vec2& operator/=(T s) + { + x() /= s; + y() /= s; + + return *this; + } + + constexpr vec2 operator/(const vec2& other) const + { + return vec2(x() / other.x(), y() / other.y()); + } + constexpr bool operator==(const vec2& other) const { return (x() == other.x()) && (y() == other.y()); } + constexpr bool operator!=(const vec2& other) const + { + return !(*this == other); + } + }; using vec2s = vec2; -- cgit 1.4.1