Symphony Of Empires
state.hpp
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 // state.hpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #pragma once
26 
27 #include <vector>
28 #include <mutex>
29 #include <memory>
30 #include <map>
31 #include <string>
32 #include <atomic>
33 #include <chrono>
34 #include <functional>
35 
36 #include "eng3d/ttf.hpp"
37 #include "eng3d/io.hpp"
38 #include "eng3d/ui/ui.hpp"
39 #include "eng3d/audio.hpp"
40 #include "eng3d/material.hpp"
41 #include "eng3d/model.hpp"
42 #include "eng3d/texture.hpp"
43 #include "eng3d/string.hpp"
44 #include "eng3d/event.hpp"
45 
46 struct SDL_Window;
47 namespace Eng3D {
48  namespace OpenGL {
49  class Shader;
50  }
51 
52  class State;
53  class Installer {
54  Eng3D::State& s;
55  public:
57  ~Installer();
58  };
59 
60  class State {
61  glm::ivec2 mouse_pos; // Mouse position
62  public:
63  State(const std::vector<std::string>& pkg_paths);
64  ~State();
65  void init_window(void);
66  void clear() const;
67  void reload_shaders();
68  void swap();
69  void do_event();
70  void set_multisamples(int samples) const;
71  static State& get_instance();
72  void do_run(std::function<bool(void)> cond, std::function<void(void)> event, std::function<void(void)> render);
73 
75  constexpr static auto JOYSTICK_DEAD_ZONE = 3000;
76 
77  virtual void handle_resize();
78  virtual void handle_mouse_btn(const Eng3D::Event::MouseButton& e);
79  virtual void handle_mouse_motion(const Eng3D::Event::MouseMotion& e);
80  virtual void handle_mouse_wheel(const Eng3D::Event::MouseWheel& e);
81  virtual void handle_key(const Eng3D::Event::Key& e);
82 
88  int zoomin_button_num = 10; // Plus
89  int zoomout_button_num = 11; // Minus
90  int left_button_num = 12; // Dpad left
91  int up_button_num = 13; // Dpad up
92  int right_button_num = 14; // Dpad right
93  int down_button_num = 15; // Dpad down
94  int select_button_num = 0; // A
95  int back_button_num = 1; // B
96  int map_select_button_num = 2; // X
97  int map_back_button_num = 3; // Y
98 
101  std::atomic<bool> run;
102  bool show_ui = true;
103  std::chrono::system_clock::time_point current_frame_time;
104  float delta_time;
105 
106  // These variables needs to be initialized before any installers
107  SDL_Window* window;
108 #if defined E3D_BACKEND_OPENGL || defined E3D_BACKEND_GLES
109  void* context;
110  int width, height;
111  // Builtin shaders
112  std::map<std::string, std::unique_ptr<Eng3D::OpenGL::Shader>> builtin_shaders;
113 #else
114  // RVL uses global state variables that are platform specific
115 #endif
116 #if defined E3D_TARGET_SWITCH
117  uint64_t application_id = 0x0000000000000000;
118 #endif
119  // Above variables might get modified by these so keep them in order as-is
129  };
130 }
Installer(Eng3D::State &s)
Definition: state.cpp:177
int up_button_num
Definition: state.hpp:91
void swap()
Definition: state.cpp:413
void reload_shaders()
Definition: state.cpp:365
std::chrono::system_clock::time_point current_frame_time
Definition: state.hpp:103
void do_event()
Definition: state.cpp:427
int map_movement_axis_num
Number of the axis assigned to map movement.
Definition: state.hpp:84
UI::Context ui_ctx
Definition: state.hpp:128
void init_window(void)
bool show_ui
Definition: state.hpp:102
Eng3D::AudioManager audio_man
Definition: state.hpp:123
void set_multisamples(int samples) const
Definition: state.cpp:507
SDL_Window * window
Definition: state.hpp:107
int map_back_button_num
Definition: state.hpp:97
State(const std::vector< std::string > &pkg_paths)
Definition: state.cpp:318
Eng3D::ModelManager model_man
Definition: state.hpp:126
int map_select_button_num
Definition: state.hpp:96
virtual void handle_key(const Eng3D::Event::Key &e)
Definition: state.cpp:575
virtual void handle_mouse_btn(const Eng3D::Event::MouseButton &e)
Definition: state.cpp:541
void do_run(std::function< bool(void)> cond, std::function< void(void)> event, std::function< void(void)> render)
Perform the main game loop.
Definition: state.cpp:522
Eng3D::MaterialManager material_man
Definition: state.hpp:125
int right_button_num
Definition: state.hpp:92
Eng3D::StringManager string_man
Definition: state.hpp:120
int zoomout_button_num
Definition: state.hpp:89
int down_button_num
Definition: state.hpp:93
std::atomic< bool > run
Variable telling if the game should quit, honored by most event loops but should be used explicitly i...
Definition: state.hpp:101
int left_button_num
Definition: state.hpp:90
int zoomin_button_num
Button for zooming in.
Definition: state.hpp:88
static State & get_instance()
Definition: state.cpp:514
Eng3D::Installer installer
Definition: state.hpp:121
Eng3D::TrueType::Manager ttf_man
Definition: state.hpp:127
int select_button_num
Definition: state.hpp:94
int cursor_movement_axis_num
Number of the axis assigned to cursor movement.
Definition: state.hpp:86
Eng3D::TextureManager tex_man
Definition: state.hpp:124
virtual void handle_mouse_motion(const Eng3D::Event::MouseMotion &e)
Definition: state.cpp:560
int back_button_num
Definition: state.hpp:95
constexpr static auto JOYSTICK_DEAD_ZONE
Value to ignore x/y axis motion taps (useful ignoring stray joystick input)
Definition: state.hpp:75
virtual void handle_mouse_wheel(const Eng3D::Event::MouseWheel &e)
Definition: state.cpp:568
void clear() const
Definition: state.cpp:401
float delta_time
Definition: state.hpp:104
Eng3D::IO::PackageManager package_man
Definition: state.hpp:122
virtual void handle_resize()
Definition: state.cpp:537
The string pool manager (singleton), used mainly for translation purpouses. But also helps to reduce ...
Definition: string.hpp:70
General manager for textures, caches textures into the memory instead of reading them off the disk ev...
Definition: texture.hpp:202
The UI context that handles all the ui widgets.
Definition: ui.hpp:63