Skip to content

Commit b2c97e4

Browse files
authored
Resources (#8)
* smoother building drag * almost fixed rhombus collision detection * screen lerps back to bounds when panned too far * timer for wheat * wheat * resource collection * reworked event handling * updates resource bar when resource collected
1 parent c008f43 commit b2c97e4

18 files changed

+794
-513
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.10)
22
project(kingdoms)
33

4-
set(CMAKE_CXX_STANDARD 17)
4+
set(CMAKE_CXX_STANDARD 20)
55
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/build")
66

77
# Add the include directory

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,9 @@ There's this upcoming third person Steam game called [Kingmakers](https://store.
5050

5151
### Technical Information
5252

53-
Made using C++ 17, [SDL 2.30.3](https://github.com/libsdl-org/SDL/releases/tag/release-2.30.3), [SDL_ttf 2.22.0](https://github.com/libsdl-org/SDL_ttf/releases/tag/release-2.22.0), and [SDL_image 2.8.2](https://github.com/libsdl-org/SDL_image/releases/tag/release-2.8.2). Models made using Blender. Developed on Visual Studio using CMake.
53+
`C++20`, `CMake`, `Visual Studio`
54+
[SDL 2.30.3](https://github.com/libsdl-org/SDL/releases/tag/release-2.30.3)
55+
[SDL_ttf 2.22.0](https://github.com/libsdl-org/SDL_ttf/releases/tag/release-2.22.0)
56+
[SDL_image 2.8.2](https://github.com/libsdl-org/SDL_image/releases/tag/release-2.8.2)
57+
[SDL_FontCache](https://github.com/grimfang4/SDL_FontCache)
58+
Models made with `Blender`

assets/Cinzel.ttf

122 KB
Binary file not shown.

assets/static/Cinzel-Black.ttf

75.3 KB
Binary file not shown.

assets/static/Cinzel-Bold.ttf

75.3 KB
Binary file not shown.

assets/static/Cinzel-ExtraBold.ttf

75.3 KB
Binary file not shown.

assets/static/Cinzel-Medium.ttf

75.1 KB
Binary file not shown.

assets/static/Cinzel-Regular.ttf

74.8 KB
Binary file not shown.

assets/static/Cinzel-SemiBold.ttf

75.2 KB
Binary file not shown.

assets/wheat.png

54.3 KB
Loading

include/farmhouse.hpp

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#pragma once
2+
3+
#include "object.hpp"
4+
#include "grid.hpp"
5+
6+
#include "SDL.h"
7+
8+
#include <forward_list>
9+
#include <array>
10+
#include <optional>
11+
12+
namespace king {
13+
14+
class Farmhouse : private Object
15+
{
16+
public:
17+
/*
18+
* @param renderer To create the farmhouse texture
19+
* @param pos The inital position used to calculate the grid snap
20+
* position
21+
*/
22+
Farmhouse(
23+
SDL_Renderer* renderer,
24+
SDL_FPoint const& pos,
25+
Grid const& grid,
26+
float _scale
27+
);
28+
29+
void init_resource_timer();
30+
31+
~Farmhouse();
32+
33+
public:
34+
void pan(float dx, float dy);
35+
36+
/*
37+
* @returns True if mouse is pressed over the object
38+
*/
39+
bool mouse_press(float mx, float my);
40+
int mouse_press_update();
41+
42+
/*
43+
* @param farmhouses For collision detection
44+
*/
45+
void mouse_drag(float mx, float my, std::forward_list<Farmhouse> const& farmhouses, float scale);
46+
47+
void mouse_release();
48+
49+
/*
50+
* Zoom based on mouse position and scale
51+
*/
52+
void mouse_wheel(int mouse_x, int mouse_y, float scale_ratio);
53+
54+
void render(SDL_Renderer* renderer, float scale);
55+
56+
void update();
57+
58+
static Uint32 resource_callback(Uint32 interval, void* obj);
59+
60+
private:
61+
bool is_rhombus_in_rhombus(std::array<SDL_Vertex, 4> const& _vertices) const;
62+
63+
public:
64+
inline static Farmhouse* drag_ptr = nullptr;
65+
66+
public:
67+
// Display
68+
SDL_Texture* texture;
69+
int texture_width, texture_height;
70+
SDL_Colour clr;
71+
72+
// Position
73+
float offset_x, offset_y;
74+
std::array<SDL_Vertex, 4> grid_snap_vertices;
75+
std::array<SDL_Vertex, 4> absolute_vertices;
76+
77+
// Movement
78+
float start_mouse_drag_x, start_mouse_drag_y;
79+
// If the movement ends at an invalid location, reset the position to original
80+
bool record_start_vertices;
81+
std::array<SDL_Vertex, 4> start_grid_snap_vertices;
82+
std::array<SDL_Vertex, 4> start_absolute_vertices;
83+
84+
// Resourse Generation
85+
SDL_Texture* resource_texture;
86+
int resource_texture_width, resource_texture_height;
87+
bool display_resource;
88+
float resource_amount;
89+
float resource_per_sec;
90+
};
91+
92+
} // namespace king

include/grid.hpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
3+
#include "object.hpp"
4+
5+
#include "SDL.h"
6+
7+
namespace king {
8+
9+
class Grid : private Object
10+
{
11+
public:
12+
Grid(int _width, int _height);
13+
~Grid();
14+
15+
public:
16+
void mouse_wheel(int mouse_x, int mouse_y, float scale_ratio);
17+
void mouse_drag(float dx, float dy);
18+
void render(SDL_Renderer* renderer, float scale);
19+
20+
public:
21+
int width, height;
22+
SDL_FPoint** data;
23+
};
24+
25+
}

include/object.hpp

+13-48
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,24 @@
22

33
#include "SDL.h"
44

5-
#include <array>
6-
#include <vector>
7-
#include <forward_list>
8-
95
namespace king {
106

11-
struct Object
7+
/*
8+
* The standard action of an Object is to handle house presses (eg. resource collection),
9+
* mouse drag (eg. drag and rellocate) and mouse release (clear grid background).
10+
* It should also provide a pointer that points to the active object in the
11+
* collection. This should be set by the caller to know which object to drag
12+
* when mouse is pressed and dragged.
13+
* Note that mouse drag should only be applied to the active object, not the
14+
* entire collection.
15+
*
16+
* mx, my - mouse x and y position
17+
* dx, dy - delta/change in x and y position
18+
*/
19+
class Object
1220
{
1321
protected:
1422
void pan_point(SDL_FPoint& point, float dx, float dy);
1523
};
1624

17-
class Farmhouse : private Object
18-
{
19-
public:
20-
Farmhouse(SDL_Renderer* renderer, std::array<SDL_Vertex, 4> const& _grid_snap_vertices);
21-
~Farmhouse();
22-
23-
public:
24-
void pan(float dx, float dy);
25-
/*
26-
* @returns True if mouse is pressed over the object
27-
*/
28-
bool mouse_pressed(float mx, float my);
29-
void drag(float mx, float my, std::forward_list<Farmhouse> const& farmhouses);
30-
void mouse_released();
31-
void zoom(SDL_MouseWheelEvent const& wheel);
32-
void render(SDL_Renderer* renderer);
33-
34-
bool isPointInObject(int px, int py) const;
35-
bool isPolygonInObject(std::array<SDL_Vertex, 4> const& _vertices) const;
36-
37-
public:
38-
inline static Farmhouse* drag_ptr = nullptr;
39-
40-
public:
41-
// Display
42-
SDL_Texture* texture;
43-
int texture_width, texture_height;
44-
SDL_Colour clr;
45-
46-
// Position
47-
std::array<SDL_Vertex, 4> grid_snap_vertices;
48-
std::array<SDL_Vertex, 4> absolute_vertices;
49-
50-
// Movement
51-
float start_mouse_drag_x, start_mouse_drag_y;
52-
// If the movement ends at an invalid location, reset the position to original
53-
std::array<SDL_Vertex, 4> start_grid_snap_vertices;
54-
std::array<SDL_Vertex, 4> start_absolute_vertices;
55-
56-
// Scaling
57-
float old_scale;
58-
};
59-
6025
} // namespace king

src/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
add_executable(kingdoms main.cpp object.cpp ${SDL_fontcache_DIR}/SDL_FontCache.c)
1+
add_executable(kingdoms main.cpp object.cpp farmhouse.cpp grid.cpp ${SDL_fontcache_DIR}/SDL_FontCache.c)
22

33
# Specify the SDL2 and SDL2_ttf library directories
44
target_link_libraries(kingdoms

0 commit comments

Comments
 (0)