nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
renderer.h
Go to the documentation of this file.
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
33namespace nanommo {
34
35class 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 void set_background(const std::array<float, 3>& background) {
52 atmosphere_.sky = background;
53 }
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 void set_sky(const protocol::Sky& sky, const Showing& showing) {
59 sky_ = sky;
60 showing_ = showing;
61 }
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 void set_adjust(std::function<void(Atmosphere&)> adjust) {
66 adjust_ = std::move(adjust);
67 }
68
69 /// The atmosphere as the last frame drew it.
70 const Atmosphere& atmosphere() const { return atmosphere_; }
71
72 /// What falls from the sky, and the lightning, whose bolts the thunder
73 /// follows.
74 Weather& weather() { return *weather_; }
75
76 const Camera3D& camera() const { return camera_; }
77 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 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 const Work& work() const { return work_; }
95
96 const std::vector<Figure>& figures() const { return figures_; }
97 void set_figures(std::vector<Figure> figures) {
98 figures_ = std::move(figures);
99 }
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 void Struck(uint32_t monster, uint32_t player) {
114 reactions_.Hit(monster, player);
115 }
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 void set_target(uint32_t target, uint32_t player = 0) {
120 target_ = target;
121 target_player_ = player;
122 }
123
124 /// Where the player's walking to by themselves, which gets a ring of its
125 /// own, if they are.
126 void set_destination(std::optional<Vector3> destination) {
127 destination_ = destination;
128 }
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 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 void set_telegraphs(std::map<uint32_t, Telegraph> telegraphs) {
137 telegraphs_ = std::move(telegraphs);
138 }
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 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_
const Work & work() const
Definition renderer.h:94
void Struck(uint32_t monster, uint32_t player)
The player hit the monster, both by their ids.
Definition renderer.h:113
std::optional< Gait > GaitOf(const Figure &figure) const
Definition renderer.cc:609
void set_background(const std::array< float, 3 > &background)
The sky's colour at the horizon, which the fog fades into.
Definition renderer.h:51
const Atmosphere & atmosphere() const
The atmosphere as the last frame drew it.
Definition renderer.h:70
void set_figures(std::vector< Figure > figures)
Definition renderer.h:97
void set_camera(const Camera3D &camera)
Definition renderer.h:77
void set_clear(float clear)
Definition renderer.h:142
Renderer(const Content &content=Content())
Definition renderer.cc:358
Weather & weather()
Definition renderer.h:74
void set_cel_shading(bool on)
Cel shading's bands and outlines, on or off. It starts off.
Definition renderer.cc:421
void DrawPreview(const Figure &figure, const Camera3D &camera) const
Draws one figure on a plain background, for previews in the UI.
Definition renderer.cc:586
void set_terrain(const Terrain &terrain, int land)
Definition renderer.cc:408
void set_adjust(std::function< void(Atmosphere &)> adjust)
Definition renderer.h:65
void Animate(float seconds)
Definition renderer.cc:435
void set_sky(const protocol::Sky &sky, const Showing &showing)
Definition renderer.h:58
const std::vector< Figure > & figures() const
Definition renderer.h:96
void Draw() const
Definition renderer.cc:477
Renderer(const Renderer &)=delete
Renderer & operator=(const Renderer &)=delete
void set_flag(std::optional< Vector3 > flag)
Definition renderer.h:132
void set_cubes(std::vector< Cube > cubes)
Definition renderer.h:86
void set_telegraphs(std::map< uint32_t, Telegraph > telegraphs)
Definition renderer.h:136
void set_destination(std::optional< Vector3 > destination)
Definition renderer.h:126
const Camera3D & camera() const
Definition renderer.h:76
void set_target(uint32_t target, uint32_t player=0)
Definition renderer.h:119
void Clear() const
Definition renderer.cc:639
Definition chat.cc:9
Camera3D GameCamera()
Looking down at the middle of the board, from above and behind.
Definition camera.cc:23
constexpr Look kMeadows
Definition atmosphere.h:80
The sky and the light, which the shaders read each frame.
Definition atmosphere.h:13
What turns on and off: the time of day, and the weather.
Definition daylight.h:18