Line | Count | Source |
1 | | #ifndef NANOMMO_CLIENT_RENDERER_H_ |
2 | | #define NANOMMO_CLIENT_RENDERER_H_ |
3 | | |
4 | | #include <raylib.h> |
5 | | |
6 | | #include <array> |
7 | | #include <cstdint> |
8 | | #include <functional> |
9 | | #include <map> |
10 | | #include <memory> |
11 | | #include <optional> |
12 | | #include <utility> |
13 | | #include <vector> |
14 | | |
15 | | #include "client/animator.h" |
16 | | #include "client/atmosphere.h" |
17 | | #include "client/camera.h" |
18 | | #include "client/characters.h" |
19 | | #include "client/chunks.h" |
20 | | #include "client/cube.h" |
21 | | #include "client/daylight.h" |
22 | | #include "client/fx.h" |
23 | | #include "client/outline.h" |
24 | | #include "client/reactions.h" |
25 | | #include "client/sea.h" |
26 | | #include "client/sky.h" |
27 | | #include "client/telegraphs.h" |
28 | | #include "client/trees.h" |
29 | | #include "client/weather.h" |
30 | | #include "content/content.h" |
31 | | #include "terrain/terrain.h" |
32 | | |
33 | | namespace nanommo { |
34 | | |
35 | | class Renderer { |
36 | | public: |
37 | | explicit Renderer(const Content& content = Content()); |
38 | | ~Renderer(); |
39 | | |
40 | | Renderer(const Renderer&) = delete; |
41 | | Renderer& operator=(const Renderer&) = delete; |
42 | | |
43 | | void Draw() const; |
44 | | |
45 | | /// Draws one figure on a plain background, for previews in the UI. |
46 | | void DrawPreview(const Figure& figure, const Camera3D& camera) const; |
47 | | |
48 | | void Clear() const; |
49 | | |
50 | | /// The sky's colour at the horizon, which the fog fades into. |
51 | 9 | void set_background(const std::array<float, 3>& background) { |
52 | 9 | atmosphere_.sky = background; |
53 | 9 | } |
54 | | |
55 | | /// The time of day and the weather, as the server sent them, and which of |
56 | | /// them the player wants to see. Until the first, the land has its own |
57 | | /// look all the time. |
58 | 0 | void set_sky(const protocol::Sky& sky, const Showing& showing) { |
59 | 0 | sky_ = sky; |
60 | 0 | showing_ = showing; |
61 | 0 | } |
62 | | |
63 | | /// Changes the atmosphere each frame, after the time of day and the |
64 | | /// weather: a film's own fog, sun or light. |
65 | 0 | void set_adjust(std::function<void(Atmosphere&)> adjust) { |
66 | 0 | adjust_ = std::move(adjust); |
67 | 0 | } |
68 | | |
69 | | /// The atmosphere as the last frame drew it. |
70 | 0 | const Atmosphere& atmosphere() const { return atmosphere_; } |
71 | | |
72 | | /// What falls from the sky, and the lightning, whose bolts the thunder |
73 | | /// follows. |
74 | 0 | Weather& weather() { return *weather_; } |
75 | | |
76 | 0 | const Camera3D& camera() const { return camera_; } |
77 | 5 | void set_camera(const Camera3D& camera) { camera_ = camera; } |
78 | | |
79 | | /// Cel shading's bands and outlines, on or off. It starts off. |
80 | | void set_cel_shading(bool on); |
81 | | |
82 | | /// Builds the terrain's meshes in the colours of the land's look, by its |
83 | | /// place in the lands, in place of any we had, and takes on its atmosphere. |
84 | | void set_terrain(const Terrain& terrain, int land); |
85 | | |
86 | 1 | void set_cubes(std::vector<Cube> cubes) { cubes_ = std::move(cubes); } |
87 | | |
88 | | /// What the last Draw asked the GPU for: the terrain, the trees and the |
89 | | /// sea. |
90 | | struct Work { |
91 | | int draws = 0; |
92 | | int triangles = 0; |
93 | | }; |
94 | 2 | const Work& work() const { return work_; } |
95 | | |
96 | 0 | const std::vector<Figure>& figures() const { return figures_; } |
97 | 9 | void set_figures(std::vector<Figure> figures) { |
98 | 9 | figures_ = std::move(figures); |
99 | 9 | } |
100 | | |
101 | | /// Where the figure is in its walk or run, as its model's clips have it: |
102 | | /// none, if it's doing neither. |
103 | | std::optional<Gait> GaitOf(const Figure& figure) const; |
104 | | |
105 | | /// Moves the skills' fx, the sea's waves, the clouds and the weather on by |
106 | | /// the seconds, as the figures are now. Then the monsters take their blows, |
107 | | /// and those that just landed hold their players, and them, still for a |
108 | | /// moment. The sky's light is worked out again, for the time and the |
109 | | /// weather now. |
110 | | void Animate(float seconds); |
111 | | |
112 | | /// The player hit the monster, both by their ids. |
113 | 0 | void Struck(uint32_t monster, uint32_t player) { |
114 | 0 | reactions_.Hit(monster, player); |
115 | 0 | } |
116 | | |
117 | | /// The monster the player picked, by its id, or in a duel, the player, by |
118 | | /// theirs, which gets a ring at its feet, or 0 for none. |
119 | 0 | void set_target(uint32_t target, uint32_t player = 0) { |
120 | 0 | target_ = target; |
121 | 0 | target_player_ = player; |
122 | 0 | } |
123 | | |
124 | | /// Where the player's walking to by themselves, which gets a ring of its |
125 | | /// own, if they are. |
126 | 0 | void set_destination(std::optional<Vector3> destination) { |
127 | 0 | destination_ = destination; |
128 | 0 | } |
129 | | |
130 | | /// Where a duel's flag is, while there's one: a pole, with a ring at its |
131 | | /// foot, and a ring as far out as the duellists can go. |
132 | 0 | void set_flag(std::optional<Vector3> flag) { flag_ = flag; } |
133 | | |
134 | | /// The monsters' skills on their way, each a shape on the ground that fills |
135 | | /// until it lands. |
136 | 1 | void set_telegraphs(std::map<uint32_t, Telegraph> telegraphs) { |
137 | 1 | telegraphs_ = std::move(telegraphs); |
138 | 1 | } |
139 | | |
140 | | /// How far the fog has lifted, as Loading::clear says: from 0, where it |
141 | | /// hides all but what's nearest, to 1, the atmosphere's own. |
142 | 0 | void set_clear(float clear) { clear_ = clear; } |
143 | | |
144 | | private: |
145 | | void UnloadTerrain(); |
146 | | void DrawCounted(const Mesh& mesh, const Material& material, |
147 | | const Matrix& transform) const; |
148 | | void DrawFigure(const Figure& figure) const; |
149 | | |
150 | | Atmosphere atmosphere_ = kMeadows.atmosphere; |
151 | | int land_ = 0; |
152 | | std::optional<protocol::Sky> sky_; |
153 | | Showing showing_; |
154 | | std::function<void(Atmosphere&)> adjust_; |
155 | | Camera3D camera_ = GameCamera(); |
156 | | std::vector<Cube> cubes_; |
157 | | std::vector<Figure> figures_; |
158 | | uint32_t target_ = 0; |
159 | | uint32_t target_player_ = 0; |
160 | | std::optional<Vector3> destination_; |
161 | | std::optional<Vector3> flag_; |
162 | | std::map<uint32_t, Telegraph> telegraphs_; |
163 | | float clear_ = 1.0f; |
164 | | /// The seconds Animate has moved the world on by, which the sea's waves and |
165 | | /// the clouds go by. |
166 | | float clock_ = 0.0f; |
167 | | // Draw is const, as drawing doesn't change what we draw, but it counts. |
168 | | mutable Work work_; |
169 | | |
170 | | Shader cube_shader_; |
171 | | Shader character_shader_; |
172 | | Outline outline_; |
173 | | bool cel_shading_ = false; |
174 | | Model cube_; |
175 | | Model disc_; |
176 | | Model spire_; |
177 | | std::vector<Face> tree_; |
178 | | std::vector<Chunk> chunks_; |
179 | | std::unique_ptr<Sea> sea_; |
180 | | /// The land, for the shadows to lie on. |
181 | | std::optional<Terrain> terrain_; |
182 | | Characters characters_; |
183 | | /// Each kind of monster, by id, as its model at its height. |
184 | | std::map<uint32_t, std::unique_ptr<Characters>> monsters_; |
185 | | std::unique_ptr<Fx> fx_; |
186 | | Reactions reactions_; |
187 | | std::unique_ptr<Sky> sky_dome_; |
188 | | std::unique_ptr<Weather> weather_; |
189 | | }; |
190 | | |
191 | | } // namespace nanommo |
192 | | |
193 | | #endif // NANOMMO_CLIENT_RENDERER_H_ |