Symphony Of Empires
pyscript.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 // pyscript.hpp
20 //
21 // Abstract:
22 // Play around with the Python3 scripting.
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 #include "eng3d/pyvm.hpp"
35 
36 struct GameState : public Eng3D::State {
37  GameState(const std::vector<std::string>& pkg_paths)
38  : Eng3D::State::State(pkg_paths)
39  {
40 
41  }
42  ~GameState() = default;
43 
46 
47  void handle_key(const Eng3D::Event::Key& e) override {
48  if(e.hold) {
49  switch(e.type) {
51  this->run = !this->run;
52  break;
54  if(this->profiler_view) {
55  this->profiler_view->kill();
56  profiler_view = nullptr;
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 #ifdef E3D_FEATURE_PYTHON
83  Eng3D::PythonVM pyvm(gs);
84  pyvm.run_string("main", gs.package_man.get_unique("python/main.py")->read_all());
85 #endif
86 
87  gs.do_run([&gs]() { return gs.run == true; },
88  ([&gs]() {
89  gs.profiler.start("UI Event handling");
90  gs.do_event();
91  gs.profiler.stop("UI Event handling");
92  }), ([&gs]() {
93  gs.profiler.start("Clearing");
94  gs.clear();
95  gs.profiler.stop("Clearing");
96  gs.profiler.render_done();
97  })
98  );
99  std::cout << "Test passed" << std::endl;
100  return 0;
101 }
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
void run_string(const std::string_view name, const std::string_view path)
void do_event()
Definition: state.cpp:427
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::IO::PackageManager package_man
Definition: state.hpp:122
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: pyscript.cpp:37
void handle_key(const Eng3D::Event::Key &e) override
Definition: pyscript.cpp:47
void kill()
Kills the current widget, setting it up for deletion when dead widgets are cleared by the UI context.
Definition: widget.hpp:283
std::string translate(const std::string_view str)
Definition: string.cpp:76
int main(int argc, char **argv)
Definition: pyscript.cpp:66
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
#define CXX_THROW(class,...)
Definition: utils.hpp:98