Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grid #5

Merged
merged 8 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions include/object.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#pragma once

#include "SDL.h"

#include <array>
#include <vector>
#include <forward_list>

namespace king {

struct Object
{
protected:
void pan_point(SDL_FPoint& point, float dx, float dy);
};

class Farmhouse : private Object
{
public:
Farmhouse(SDL_Renderer* renderer, std::array<SDL_Vertex, 4> const& _grid_snap_vertices);
~Farmhouse();

public:
void pan(float dx, float dy);
/*
* @returns True if mouse is pressed over the object
*/
bool mouse_pressed(float mx, float my);
void drag(float mx, float my, std::forward_list<Farmhouse> const& farmhouses);
void mouse_released();
void zoom(SDL_MouseWheelEvent const& wheel);
void render(SDL_Renderer* renderer);

bool isPointInObject(int px, int py) const;
bool isPolygonInObject(std::array<SDL_Vertex, 4> const& _vertices) const;

public:
inline static Farmhouse* drag_ptr = nullptr;

public:
// Display
SDL_Texture* texture;
int texture_width, texture_height;
SDL_Colour clr;

// Position
std::array<SDL_Vertex, 4> grid_snap_vertices;
std::array<SDL_Vertex, 4> absolute_vertices;

// Movement
float start_mouse_drag_x, start_mouse_drag_y;
// If the movement ends at an invalid location, reset the position to original
std::array<SDL_Vertex, 4> start_grid_snap_vertices;
std::array<SDL_Vertex, 4> start_absolute_vertices;

// Scaling
float old_scale;
};

} // namespace king
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
add_executable(kingdoms main.cpp ${SDL_fontcache_DIR}/SDL_FontCache.c)
add_executable(kingdoms main.cpp object.cpp ${SDL_fontcache_DIR}/SDL_FontCache.c)

# Specify the SDL2 and SDL2_ttf library directories
target_link_libraries(kingdoms
Expand Down
Loading
Loading