-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
171 lines (145 loc) · 3.72 KB
/
game.cpp
File metadata and controls
171 lines (145 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "game.h"
#include "system.h"
#include "stdafx.h"
#include "ui.h"
#include "physics_engine.h"
void Game::register_classes(){
registered_class={
{"Ui",
[](){
return new UI();}
},
// {"Camera",
// [](){
// return new Camera();}
// },
// {"Player",
// [](){
// return new Player();}
// },
// {"Circle",
// [](){
// return new Circle();}
// },
{"PhyEngine",
[](){
return new PhyEngine();}
}
// {"Box",
// [](){
// return new Box();}
// }
};
}
void Game::initVariables(){
name = "Engine";
systemRef = new System();
window.create(sf::VideoMode(800, 600), "My window",sf::Style::Default );
clock.restart();
this->register_classes();
}
void Game::initSystemReference(){
systemRef->objs = &objs;
systemRef->window = &window;
systemRef->dt = 0;
systemRef->font.loadFromFile("config/times.ttf");
systemRef->registered_class = ®istered_class;
}
void Game::setChild(GameObject* child){
objs[child->name] = child;
children.push_back(child);
}
size_t Game::childCount() const{
return children.size();
}
GameObject* Game::getChild(size_t i){
return children[i];
}
void Game::setParent(GameObject* parent){
return;
}
GameObject* Game::getParent(){
return nullptr;
}
void Game::loadObjects(const std::string& address){
load(this,std::filesystem::path(address));
}
void Game::load(GameObject* parent,const std::filesystem::path &path){
std::string name,type;
std::filesystem::path cur_path;
GameObject* child=nullptr;
for(const auto& entry :std::filesystem::directory_iterator(path)){
cur_path = entry.path();
std::tie(name,type) = split(cur_path.stem().string());
std::cout<<"Loading:"<<name<<std::endl;
if(type.empty()){ continue;}
auto it = registered_class.find(type);
if (it == registered_class.end()) {
std::cerr << "Unregistered object type: " << type << std::endl;
continue;
}
child = it->second(); //the() means to run the function
child->initNameSys(name,systemRef);
child->setParent(parent);
parent->setChild(child);
if(is_directory(cur_path)){
load(child,cur_path);
}
}
}
Game::Game(){
this->initVariables();
this->initSystemReference();
this->loadObjects("data");
this->start();
}
Game::~Game(){
for(GameObject* child:children){
delete child;
}
delete systemRef;
}
void Game::handleEvent(){
while (window.pollEvent(systemRef->event)){
if(systemRef->event.type == sf::Event::LostFocus){
this->active = false;continue;
}
if(systemRef->event.type == sf::Event::GainedFocus){
this->active = true;continue;
}
if(systemRef->event.type == sf::Event::Closed){
window.close();continue;
}
for(GameObject* child:children){
child->handleEvent();
}
}
}
void Game::update(){
if(active){
systemRef->dt = clock.restart().asSeconds();
systemRef->mouse_position = sf::Mouse::getPosition(window);
for(GameObject* child: children){
child->update();
}
}
}
void Game::render(){
window.clear();
for(GameObject* x: children){
x->render();
}
window.display();
}
void Game::start(){
for(GameObject* child:children){
child->start();
}
}
void Game::run(){
while(window.isOpen()){
handleEvent();
update();
render();
}
}