]> git.localhorst.tv Git - blobs.git/blob - src/world/Planet.hpp
separate tile type and texture
[blobs.git] / src / world / Planet.hpp
1 #ifndef BLOBS_WORLD_PLANET_HPP_
2 #define BLOBS_WORLD_PLANET_HPP_
3
4 #include "Body.hpp"
5
6 #include "Tile.hpp"
7 #include "../graphics/glm.hpp"
8 #include "../graphics/SimpleVAO.hpp"
9
10 #include <cassert>
11 #include <memory>
12 #include <GL/glew.h>
13
14
15 namespace blobs {
16 namespace world {
17
18 class TileSet;
19
20 /// A planet has six surfaces, numbered 0 to 5, each filled with
21 /// sidelength² tiles.
22 class Planet
23 : public Body {
24
25 public:
26         explicit Planet(int sidelength);
27         ~Planet();
28
29         Planet(const Planet &) = delete;
30         Planet &operator =(const Planet &) = delete;
31
32         Planet(Planet &&) = delete;
33         Planet &operator =(Planet &&) = delete;
34
35 public:
36         /// Get the tile at given surface and coordinates.
37         Tile &TileAt(int surface, int x, int y) {
38                 return tiles[IndexOf(surface, x, y)];
39         }
40         const Tile &TileAt(int surface, int x, int y) const {
41                 return tiles[IndexOf(surface, x, y)];
42         }
43
44         /// Convert coordinates into a tile index.
45         int IndexOf(int surface, int x, int y) const {
46                 assert(0 <= surface && surface <= 5);
47                 assert(0 <= x && x <= sidelength);
48                 assert(0 <= y && y <= sidelength);
49                 return surface * TilesPerSurface() + y * SideLength() + x;
50         }
51         /// The length of the side of each surface.
52         int SideLength() const {
53                 return sidelength;
54         }
55         /// The number of tiles of one surface.
56         int TilesPerSurface() const {
57                 return SideLength() * SideLength();
58         }
59         /// Total number of tiles of all surfaces combined.
60         int TilesTotal() const {
61                 return 6 * TilesPerSurface();
62         }
63
64         glm::dvec3 TileCenter(int surface, int x, int y) const noexcept;
65
66         void BuildVAOs(const TileSet &);
67         void Draw(app::Assets &, graphics::Viewport &) override;
68
69 private:
70         int sidelength;
71         std::unique_ptr<Tile []> tiles;
72
73         struct Attributes {
74                 glm::vec3 position;
75                 glm::vec3 tex_coord;
76         };
77         graphics::SimpleVAO<Attributes, unsigned int> vao;
78
79 };
80
81 void GenerateEarthlike(const TileSet &, Planet &) noexcept;
82 void GenerateTest(const TileSet &, Planet &) noexcept;
83
84 }
85 }
86
87 #endif