Symphony Of Empires
map.cpp
Go to the documentation of this file.
1 // Eng3D - General purpouse game engine
2 // Copyright (C) 2021, Eng3D contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // map.cpp
20 //
21 // Abstract:
22 // Does important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #include <tbb/blocked_range.h>
26 #include <tbb/concurrent_vector.h>
27 #include <tbb/parallel_for.h>
28 #include <tbb/combinable.h>
29 #include "eng3d/map.hpp"
30 #include "eng3d/state.hpp"
31 #include "eng3d/primitive.hpp"
32 #include "eng3d/texture.hpp"
33 #include "eng3d/utils.hpp"
34 
36  : s{ _s },
37  map_sphere(0.f, 0.f, 0.f, Eng3D::GLOBE_RADIUS, 100),
38  map_2d_quad{},
39  rivers(_s),
40  borders(_s)
41 {
42  for(int x = -1; x <= 1; x++) { // Flat surface for drawing flat map
43  auto square = std::make_unique<Eng3D::Square>(size.x * x, 0.f, size.x * (x + 1), size.y);
44  map_quads.push_back(std::move(square));
45  }
46 
47  // Mipmapped textures
48  Eng3D::TextureOptions mipmap_options{};
49  mipmap_options.wrap_s = Eng3D::TextureOptions::Wrap::REPEAT;
50  mipmap_options.wrap_t = Eng3D::TextureOptions::Wrap::REPEAT;
51  mipmap_options.min_filter = Eng3D::TextureOptions::Filter::LINEAR_MIPMAP;
52  mipmap_options.mag_filter = Eng3D::TextureOptions::Filter::LINEAR;
53  mipmap_options.compressed = true;
54 
55  this->noise_tex = this->s.tex_man.load(this->s.package_man.get_unique("gfx/noise_tex.png"), mipmap_options);
56  this->wave1 = this->s.tex_man.load(this->s.package_man.get_unique("gfx/wave1.png"), mipmap_options);
57  this->wave2 = this->s.tex_man.load(this->s.package_man.get_unique("gfx/wave2.png"), mipmap_options);
58 
59  mipmap_options.internal_format = Eng3D::TextureOptions::Format::SRGB;
60  this->water_tex = this->s.tex_man.load(this->s.package_man.get_unique("gfx/water_tex.png"), mipmap_options);
61  this->paper_tex = this->s.tex_man.load(this->s.package_man.get_unique("gfx/paper.png"), mipmap_options);
62  this->stripes_tex = this->s.tex_man.load(this->s.package_man.get_unique("gfx/stripes.png"), mipmap_options);
63 
64  this->terrain_map = std::make_unique<Eng3D::Texture>(this->s.package_man.get_unique("map/color.png")->get_abs_path());
65 
66  tbb::parallel_for(static_cast<size_t>(0), this->terrain_map->width * this->terrain_map->height, [this](const auto i) {
67  auto* data = &(this->terrain_map->buffer.get()[i]);
68  const auto color = std::byteswap<std::uint32_t>((*data) << 8);
69  uint8_t idx = 0;
70  switch(color) {
71  case 0x18200b:
72  idx = 0 * 16;
73  break;
74  case 0x4b482a:
75  idx = 1 * 16;
76  break;
77  case 0x273214:
78  idx = 3 * 16;
79  break;
80  case 0x9c8461:
81  case 0xbfa178:
82  idx = 6 * 16;
83  break;
84  case 0x969a9a:
85  case 0x686963:
86  case 0xffffff:
87  idx = 8 * 16;
88  break;
89  case 0x614a2f:
90  idx = 9 * 16;
91  break;
92  case 0x37311b:
93  case 0x7a6342:
94  idx = 10 * 16;
95  break;
96  default:
97  idx = 0;
98  break;
99  }
100  *data = idx << 24;
101  *data |= (color == 0x243089 ? 0x00 : 0x02) << 16; // Ocean, or ocean
102  });
103  //this->terrain_map->to_file("new_terrain.png");
104 
105  // Terrain textures to sample from
106  this->terrain_sheet = std::make_unique<Eng3D::TextureArray>(this->s.package_man.get_unique("gfx/terrain_sheet.png")->get_abs_path(), 4, 4);
107  this->terrain_sheet->upload();
108 }
109 
111 
112 }
BaseMap(Eng3D::State &s, glm::ivec2 size)
Definition: map.cpp:35
std::shared_ptr< Eng3D::IO::Asset::Base > get_unique(const Eng3D::IO::Path &path)
Obtaining an unique asset means the "first-found" policy applies.
Definition: io.cpp:146
Eng3D::TextureManager tex_man
Definition: state.hpp:124
Eng3D::IO::PackageManager package_man
Definition: state.hpp:122
std::shared_ptr< Eng3D::Texture > load(const std::string &path, TextureOptions options=default_options)
Finds a texture in the list of a texture manager if the texture is already in the list we load the sa...
Definition: texture.cpp:432
enum Eng3D::TextureOptions::Wrap wrap_s
void parallel_for(T range, F &&func)