Symphony Of Empires
triangle.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 // triangle.hpp
20 //
21 // Abstract:
22 // Small program to test that rendering works.
23 // ----------------------------------------------------------------------------
24 
25 #include <iostream>
26 
27 #include "eng3d/state.hpp"
28 #include "eng3d/string.hpp"
29 #include "eng3d/event.hpp"
30 #include "eng3d/profiler.hpp"
31 #include "eng3d/interface.hpp"
32 #include "eng3d/ui/ui.hpp"
33 #include "eng3d/ui/components.hpp"
34 
35 struct GameState: public Eng3D::State {
36  GameState(const std::vector<std::string>& pkg_paths)
37  : Eng3D::State::State(pkg_paths)
38  {
39 
40  }
41  ~GameState() = default;
42 
45 
46  void handle_key(const Eng3D::Event::Key& e) override {
47  if(e.hold) {
48  switch(e.type) {
50  this->run = !this->run;
51  break;
53  if(this->profiler_view) {
54  this->profiler_view->kill();
55  profiler_view = nullptr;
56  }
57  else this->profiler_view = new Eng3D::Interface::ProfilerView(*this, this->profiler);
58  break;
59  default:
60  break;
61  }
62  }
63  }
64 };
65 
66 int main(int argc, char** argv) {
67  std::vector<std::string> pkg_paths;
68  for(int i = 1; i < argc; i++) {
69  std::string arg = std::string(argv[i]);
70  if(arg == "--mod") {
71  i++;
72  if(i >= argc)
73  CXX_THROW(std::runtime_error, translate("Expected an absolute path after --mod"));
74  arg = std::string(argv[i]);
75  pkg_paths.push_back(arg);
76  }
77  }
78 
79  GameState gs(pkg_paths);
80  gs.run = true;
81 
82  new UI::Image(0, 0, gs.width, gs.height, "gfx/sky.png", nullptr);
83 
84  auto* info_win = new UI::Window(32, 32, 512, 256 + 96);
85  info_win->set_text("Economics");
86  auto& chart = info_win->make_widget<UI::Chart>(0, 0, 256, 128);
87  chart.set_text("Economy");
88  chart.set_data({ 1.f, 2.5f, 5.f, 4.f });
89 
90  auto& candlechart = info_win->make_widget<UI::CandleChart>(0, 150, 256, 128);
91  candlechart.set_text("soil composition (very soil)");
92 
93  std::vector<UI::CandleData> candles;
94  auto prev_close = 0.f;
95  for(size_t i = 0; i < 10; i++)
96  {
97  UI::CandleData candle{};
98  candle.open = prev_close;
99  candle.close = candle.open + i;
100  candle.max = glm::max(candle.open, candle.close) + i;
101  candle.min = glm::min(candle.open, candle.close) - i;
102 
103  prev_close = candle.close;
104  candles.push_back(candle);
105  }
106  candlechart.set_data(candles);
107 
108  auto& piechart = info_win->make_widget<UI::PieChart>(256, 0, 128, 128);
109  piechart.set_data({ UI::ChartData(0.5f, "Eng3D", 0xff00ffff), UI::ChartData(0.2f, "Not Eng3D", 0x00ff00ff) });
110 
111  gs.ui_ctx.prompt("شست بلسجليدلطظﻻىرﻻزؤرظءؤزةوئىءؤئمحيبسخ4ه2صثهقعسيبىسب", "σξδφσေိျုူိက့်ိုျ့ငသ်ဆသစနငငသ့ိထာလဘာခလုူ γνακδ ασνし結十と岡統百防能οχψωηδ ασηξφ ξογφφσηγερς");
112  gs.ui_ctx.prompt("Hello world", "This is a demo of Eng3D ^_^");
113 
114  auto* parliament_win = new UI::Window(320, 32, 256 + 64, 256 + 64);
115  parliament_win->set_text("parliamento");
116  auto& waffle_chart = parliament_win->make_widget<UI::WaffleChart>(0, 0, 256, 256);
117  waffle_chart.set_text("Seat row candidates?");
118  waffle_chart.set_data({
119  UI::ChartData(5.234f, "Waffle", 0xff00ffff),
120  UI::ChartData(2.567f, "Tundra", 0x00ff00ff),
121  UI::ChartData(3.23423f, "Fjord", 0xff5f70ff)
122  });
123 
124  gs.do_run([&gs](){ return gs.run == true; },
125  ([&gs]() {
126  gs.profiler.start("UI Event handling");
127  gs.do_event();
128  gs.profiler.stop("UI Event handling");
129  }), ([&gs]() {
130  gs.profiler.start("Clearing");
131  gs.clear();
132  gs.profiler.stop("Clearing");
133  gs.profiler.render_done();
134  })
135  );
136  std::cout << "Test passed" << std::endl;
137  return 0;
138 }
void do_event()
Definition: state.cpp:427
UI::Context ui_ctx
Definition: state.hpp:128
State(const std::vector< std::string > &pkg_paths)
Definition: state.cpp:318
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
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
void clear() const
Definition: state.cpp:401
Eng3D::Profiler profiler
Definition: pyscript.cpp:45
~GameState()=default
UI::Widget * profiler_view
Definition: game_state.hpp:168
GameState(const std::vector< std::string > &pkg_paths)
Definition: triangle.cpp:36
void handle_key(const Eng3D::Event::Key &e) override
Definition: triangle.cpp:46
A graph chart.
Definition: candle.hpp:62
Generalized chart data, used mostly by chart widgets, however it's not specific to any widget.
Definition: widget.hpp:129
A graph chart.
Definition: chart.hpp:40
void prompt(const std::string &title, const std::string &text)
Definition: ui.cpp:183
Image widget, can display pictures or effects on the screen.
Definition: image.hpp:43
Piechart widget.
Definition: piechart.hpp:43
void set_data(std::vector< ChartData > data)
Definition: piechart.cpp:52
void kill()
Kills the current widget, setting it up for deletion when dead widgets are cleared by the UI context.
Definition: widget.hpp:283
virtual void set_text(const std::string &text)
Generates text for the widget and overrides the current text texture.
Definition: widget.cpp:445
Window widget, this widget is similar to a Group widget, the key difference is that this one can be m...
Definition: window.hpp:39
std::string translate(const std::string_view str)
Definition: string.cpp:76
Eng3D::Event::Key::Type type
Definition: event.hpp:48
bool hold
Whetever the key is being held.
Definition: event.hpp:50
void stop(const std::string &name)
Definition: profiler.cpp:95
void render_done()
Definition: profiler.cpp:112
void start(const std::string &name)
Definition: profiler.cpp:84
float open
Definition: candle.hpp:54
A graph chart.
Definition: waffle.hpp:40
int main(int argc, char **argv)
Definition: triangle.cpp:66
#define CXX_THROW(class,...)
Definition: utils.hpp:98