Compare commits

..

No commits in common. "6d44399044b88fea92f7289697cdcd9d50ec9935" and "04bc08e9c0373aa7f8f8f417cfc32672b345f80b" have entirely different histories.

11 changed files with 44 additions and 255 deletions

View File

@ -1,43 +1,16 @@
#include "camera.hpp"
#include "math.hpp"
#include "log.hpp"
// for now only pass-through placeholder functions,
// since we draw the whole map
void Camera::Pan(const WorldPos& delta)
{
LOG_DEBUG("m_Pan before: ", m_Pan, " delta ", delta);
m_Pan += delta;
LOG_DEBUG("m_Pan after: ", m_Pan);
}
void Camera::Zoom(const WorldPos&)
{
}
WindowPos Camera::WorldToWindow(WorldPos world) const
{
const auto& v = world + m_Pan;
return WindowPos{v[0], v[1]};
return WindowPos{world[0], world[1]};
}
WorldPos Camera::WindowToWorld(WindowPos window) const
{
return WorldPos{window[0], window[1]} - m_Pan;
}
WindowSize Camera::WorldToWindowSize(WorldSize world) const
{
const auto& v = world;
// no zoom yet, just pass-through
return WindowSize{v[0], v[1]};
}
WorldSize Camera::WindowToWorldSize(WindowSize window) const
{
return WorldSize{window[0], window[1]};
return WorldPos{window[0], window[1]};
}

View File

@ -5,16 +5,12 @@
class Camera
{
public:
void Pan(const WorldPos& delta);
void Zoom(const WorldPos& delta);
WindowPos WorldToWindow(WorldPos) const;
WorldPos WindowToWorld(WindowPos) const;
WindowSize WorldToWindowSize(WorldSize) const;
WorldSize WindowToWorldSize(WindowSize) const;
WindowPos WorldToWindow(WorldPos) const;
WorldPos WindowToWorld(WindowPos) const;
private:
// TODO this should be replaced with a matrix
float m_Zoom;
WorldPos m_Pan;
WorldPos m_RectCorner; // upper left corner (0,0) of the drawn regios
};

View File

@ -27,19 +27,14 @@ void GameLoop::Run() {
for (size_t row = 0; row < tiles.size(); row++) {
for (size_t col = 0; col < tiles[row].size(); col++) {
const auto& camera = m_Game->GetCamera();
const auto& position = camera.WorldToWindow(
map.TileEdgeToWorld(
TilePos{static_cast<int32_t>(row), static_cast<int32_t>(col)}
)
);
const auto& size = camera.WorldToWindowSize(
map.GetTileSize()
);
// LOG_DEBUG("Drawing rect (", row, ", ", col, ")");
m_Window->DrawRect(
position,
size,
tiles[row][col]->R, tiles[row][col]->G,
camera.WorldToWindow(
map.TileEdgeToWorld(
TilePos{static_cast<int32_t>(row), static_cast<int32_t>(col)}
)
),
camera.WorldToWindow(map.GetTileSize()), tiles[row][col]->R, tiles[row][col]->G,
tiles[row][col]->B, tiles[row][col]->A);
}
}

View File

@ -28,7 +28,8 @@ TilePos Map::WorldToTile(WorldPos p) const {
return TilePos{static_cast<int32_t>(p.x() / TILE_SIZE), static_cast<int32_t>(p.y() / TILE_SIZE)};
}
WorldSize Map::GetTileSize() const { return WorldSize{TILE_SIZE, TILE_SIZE}; }
// TODO this should probably use something like WorldSize or WorldVec to make the distinction clear
WorldPos Map::GetTileSize() const { return WorldPos{TILE_SIZE, TILE_SIZE}; }
const Tile *Map::GetTileAt(TilePos p) const {
assert(IsTilePosValid(p));

View File

@ -25,8 +25,8 @@ public:
WorldPos TileToWorld(TilePos p) const;
WorldPos TileEdgeToWorld(TilePos p) const;
TilePos WorldToTile(WorldPos p) const;
WorldSize GetTileSize() const;
WorldPos GetTileSize() const;
const Tile *GetTileAt(TilePos p) const;
const Tile *GetTileAt(WorldPos p) const;

View File

@ -238,22 +238,14 @@ using uvec3 = vec<std::uint32_t, 3>;
using uvec4 = vec<std::uint32_t, 4>;
// tags for differentiating between domains
struct WorldPosTag {};
struct WorldSizeTag {};
struct WindowPosTag {};
struct WindowSizeTag {};
struct TilePosTag {};
struct TileSizeTag {};
struct WorldTag {};
struct WindowTag {};
struct TileTag {};
// types for each domain
using WorldPos = vec<float, 2, WorldPosTag>;
using WindowPos = vec<float, 2, WindowPosTag>;
using TilePos = vec<int32_t, 2, TilePosTag>;
// Size
using WorldSize = vec<float, 2, WorldSizeTag>;
using WindowSize = vec<float, 2, WindowSizeTag>;
using TileSize = vec<int32_t, 2, TileSizeTag>;
using WorldPos = vec<float, 2, WorldTag>;
using WindowPos = vec<float, 2, WindowTag>;
using TilePos = vec<int32_t, 2, TileTag>;
//
// Utils
@ -267,121 +259,3 @@ struct TilePosHash {
}
};
// old stuff - TODO delete
// constexpr double EQUALITY_LIMIT = 1e-6;
// template <typename T> struct Vec2D {
// public:
// Vec2D() = default;
// ~Vec2D() = default;
//
// template <typename U>
// Vec2D(Vec2D<U> other) {
// this->x = static_cast<T>(other.x);
// this->y = static_cast<T>(other.y);
// }
//
// Vec2D& operator+=(const Vec2D &other) {
// x += other.x;
// y += other.y;
// return *this;
// }
//
// template <typename U>
// requires std::is_arithmetic_v<U>
// Vec2D& operator/=(U k) {
// this->x /= static_cast<T>(k);
// this->y /= static_cast<T>(k);
// return *this;
// }
//
// friend Vec2D operator+(const Vec2D &a, const Vec2D &b) {
// return Vec2D{a.x + b.x, a.y + b.y};
// }
//
// friend Vec2D operator-(const Vec2D &a, const Vec2D &b) {
// return Vec2D{a.x - b.x, a.y - b.y};
// }
//
// template <typename U>
// requires std::is_arithmetic_v<U>
// friend Vec2D operator*(U k, const Vec2D &v)
// {
// return Vec2D{k * v.x, k * v.y};
// }
//
// template <typename U>
// requires std::is_arithmetic_v<U>
// friend Vec2D operator/(const Vec2D &v, U k)
// {
// return Vec2D{v.x / k, v.y / k};
// }
//
// friend bool operator==(const Vec2D &a, const Vec2D &b) {
// if constexpr (std::is_integral_v<T>) {
// return a.x == b.x && a.y == b.y;
// } else if constexpr (std::is_floating_point_v<T>) {
// return a.distance(b) < EQUALITY_LIMIT;
// } else {
// static_assert("Unhandled comparison");
// }
// }
//
// Vec2D operator*(float b) const { return Vec2D{b * x, b * y}; }
//
// T distance_squared(const Vec2D &other) const {
// T dx = x - other.x;
// T dy = y - other.y;
// return dx * dx + dy * dy;
// }
//
// T distance(const Vec2D &other) const
// requires std::floating_point<T>
// {
// return sqrt(distance_squared(other));
// }
//
// void normalize()
// requires std::floating_point<T>
// {
// auto length = sqrt(x * x + y * y);
// if (length < EQUALITY_LIMIT) {
// x = y = 0;
// } else {
// x /= length;
// y /= length;
// }
// }
//
// Vec2D normalized()
// requires std::floating_point<T>
// {
// Vec2D v(*this);
// v.normalize();
// return v;
// }
//
// Vec2D orthogonal() const
// {
// Vec2D v(*this);
//
// std::swap(v.x, v.y);
// v.x = -v.x;
// return v;
// }
//
// template <typename U> Vec2D(std::initializer_list<U> list) {
// assert(list.size() == 2);
// auto first_element = *list.begin();
// auto second_element = *(list.begin() + 1);
// x = static_cast<T>(first_element);
// y = static_cast<T>(second_element);
// }
//
// T x, y;
//
// friend std::ostream &operator<<(std::ostream &os, const Vec2D &obj) {
// os << "( " << obj.x << ", " << obj.y << ")";
// return os;
// }
// };

View File

@ -110,7 +110,7 @@ void PathFindingDemo::HandleActions(const std::vector<UserAction> &actions) {
LOG_INFO("Exit requested");
m_ExitRequested = true;
} else if (action.type == UserAction::Type::SET_MOVE_TARGET) {
WorldPos wp = m_Camera.WindowToWorld(action.Argument.position);
WorldPos wp = action.Argument.position;
LOG_INFO("Calculating path to target: ", wp);
m_Path = m_PathFinder->CalculatePath(m_Player->GetPosition(), wp);
LOG_INFO("Done, path node count: ", m_Path.size());
@ -119,11 +119,6 @@ void PathFindingDemo::HandleActions(const std::vector<UserAction> &actions) {
PathFinderType type = static_cast<PathFinderType>(action.Argument.number);
m_PathFinder = pathfinder::utils::create(type, (const Map*)&m_Map);
LOG_INFO("Switched to path finding method: ", m_PathFinder->GetName());
} else if (action.type == UserAction::Type::CAMERA_PAN) {
const auto& window_pan = action.Argument.position;
WorldPos world_pan{window_pan.x(), window_pan.y()};
m_Camera.Pan(world_pan);
LOG_INFO("Camera pan delta: ", world_pan);
}
};
}

View File

@ -19,43 +19,17 @@ UserInput::~UserInput() { LOG_DEBUG("."); };
std::expected<void, std::string> UserInput::Init() { return {}; }
void UserInput::GetActions_mouse(const SDL_Event& event)
{
static bool mouse_pan = false;
const std::vector<UserAction> &UserInput::GetActions() {
m_Actions.clear();
SDL_Event event;
if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
SDL_MouseButtonEvent mouse_event = event.button;
MouseButton button = static_cast<MouseButton>(mouse_event.button);
if (button == MouseButton::LEFT) {
LOG_DEBUG("Mouse down: ", mouse_event.x, ", ", mouse_event.y);
m_Actions.emplace_back(UserAction::Type::SET_MOVE_TARGET,
WindowPos{mouse_event.x, mouse_event.y});
} else if (button == MouseButton::MIDDLE) {
mouse_pan = true;
}
} else if (event.type == SDL_EVENT_MOUSE_BUTTON_UP) {
SDL_MouseButtonEvent mouse_event = event.button;
MouseButton button = static_cast<MouseButton>(mouse_event.button);
if (button == MouseButton::MIDDLE) {
mouse_pan = false;
}
} else if (event.type == SDL_EVENT_MOUSE_MOTION) {
SDL_MouseMotionEvent motion_event = event.motion;
if (mouse_pan) {
m_Actions.emplace_back(UserAction::Type::CAMERA_PAN,
WindowPos{motion_event.xrel, motion_event.yrel});
}
}
}
void UserInput::GetActions_kbd(const SDL_Event& event)
{
while (SDL_PollEvent(&event)) {
if (event.type == SDL_EVENT_KEY_DOWN || event.type == SDL_EVENT_KEY_UP) {
bool key_down = event.type == SDL_EVENT_KEY_DOWN ? true : false;
SDL_KeyboardEvent kbd_event = event.key;
if (kbd_event.repeat) {
// SDL repeats KEY_DOWN if key is held down, we ignore that
return;
continue;
}
LOG_DEBUG("Key '", static_cast<char>(kbd_event.key),
key_down ? "' down" : "' up");
@ -63,7 +37,8 @@ void UserInput::GetActions_kbd(const SDL_Event& event)
switch (kbd_event.key) {
case 'q':
m_Actions.emplace_back(UserAction::Type::EXIT);
return;
// further processing of inputs is not needed
return m_Actions;
case '1':
case '2':
case '3':
@ -78,26 +53,12 @@ void UserInput::GetActions_kbd(const SDL_Event& event)
LOG_INFO("Key '", static_cast<char>(kbd_event.key), "' not mapped");
break;
}
}
const std::vector<UserAction>& UserInput::GetActions() {
m_Actions.clear();
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_EVENT_KEY_DOWN
|| event.type == SDL_EVENT_KEY_UP)
{
GetActions_kbd(event);
}
else if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN
|| event.type == SDL_EVENT_MOUSE_BUTTON_UP
|| event.type == SDL_EVENT_MOUSE_MOTION)
{
GetActions_mouse(event);
}
else
{
} else if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
SDL_MouseButtonEvent mouse_event = event.button;
LOG_DEBUG("Mouse down: ", mouse_event.x, ", ", mouse_event.y);
m_Actions.emplace_back(UserAction::Type::SET_MOVE_TARGET,
WorldPos{mouse_event.x, mouse_event.y});
} else {
// TODO uncomment, for now too much noise
// LOG_WARNING("Action not processed");
}

View File

@ -7,30 +7,27 @@
#include "log.hpp"
#include "math.hpp"
// Seems like SDL doesn't have named constants for mouse button
enum class MouseButton { LEFT = 1, MIDDLE, RIGHT };
class UserAction {
public:
enum class Type { NONE, EXIT, SET_MOVE_TARGET, SELECT_PATHFINDER, CAMERA_PAN, CAMERA_ZOOM };
enum class Type { NONE, EXIT, SET_MOVE_TARGET, SELECT_PATHFINDER };
UserAction() : type(Type::NONE), Argument{.number = 0} {}
UserAction(Type t) : type(t), Argument{.number = 0} {}
UserAction(Type t, char key) : type(t), Argument{.key = key} {}
UserAction(Type t, WindowPos v) : type(t), Argument{.position = v} {}
UserAction(Type t, WorldPos v) : type(t), Argument{.position = v} {}
UserAction(Type t, int arg) : type(t), Argument{.number = arg} {}
~UserAction() = default;
Type type;
union {
WindowPos position;
WorldPos position;
char key;
int number;
} Argument;
// TODO use std::variant
//std::variant<WindowPos, char, int> Argument;
//std::variant<WorldPos, char, int> Argument;
};
class UserInput {
@ -49,7 +46,4 @@ public:
private:
std::vector<UserAction> m_Actions;
void GetActions_kbd(const SDL_Event&);
void GetActions_mouse(const SDL_Event&);
};

View File

@ -84,7 +84,7 @@ void Window::DrawSprite(const WindowPos &position, Sprite &s) {
SDL_RenderTexture(m_Renderer.get(), s.GetTexture(), nullptr, &rect);
}
void Window::DrawRect(const WindowPos &position, const WindowSize size, uint8_t R,
void Window::DrawRect(const WindowPos &position, const WindowPos size, uint8_t R,
uint8_t G, uint8_t B, uint8_t A) {
SDL_FRect rect = {position.x(), position.y(), size.x(), size.y()};
SDL_SetRenderDrawColor(m_Renderer.get(), R, G, B, A);

View File

@ -23,7 +23,7 @@ public:
std::expected<void, std::string> Init();
void DrawSprite(const WindowPos &position, Sprite &s);
void DrawRect(const WindowPos &position, const WindowSize size, uint8_t R,
void DrawRect(const WindowPos &position, const WindowPos size, uint8_t R,
uint8_t G, uint8_t B, uint8_t A);
void ClearWindow();
void Flush();