/
Game.cpp
162 lines (135 loc) · 3.99 KB
/
Game.cpp
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
#include "Game.h"
#include <fstream>
#include <string>
#include <sstream>
#include <iostream>
Game::Game() : window(sf::VideoMode(360, 240), "SFML Game Engin"){
this->dt = this->dtClock.restart().asSeconds();
/* Pencere ile ilgili konfigürasyonlar dosyadan okunur. */
std::ifstream ifs("Config/Window.config");
std::string title = "None";
sf::VideoMode windowBounds(854, 480);
unsigned frame_limit = 120;
bool vertical_sync_enabled = false;
if( ifs.is_open() ){
std::getline(ifs, title);
ifs >> windowBounds.width >> windowBounds.height;
ifs >> frame_limit;
ifs >> vertical_sync_enabled;
} else{
// std::cout << "File Error: Could not open file" << std::endl;
}
/* Konfigürasyon dosyasýndan okunan veriler global olarak tutulur. */
globals.setWindowBounds(windowBounds);
/* Pencere iþlemleri gerçekleþtirilir. */
this->window.setSize(sf::Vector2u(windowBounds.width, windowBounds.height));
this->window.setTitle(title);
this->window.setFramerateLimit(frame_limit);
this->window.setVerticalSyncEnabled(vertical_sync_enabled);
ifs.close();
}
Game::~Game(){
/* Herhangi bir state kalmayana kadar stack'i
boþaltýr ve oyunu bitirir. */
while(!this->states.empty()){
// TODO: Burada çöküyor.
//delete this->states.top();
this->states.pop();
}
}
void Game::addState(State* state){
this->states.push(state);
}
void Game::addEntity(Entity* entity){
this->newEntities.push_back(entity);
}
void Game::updateDt(){
/* Calculates the elapsed time between two sequential frames. */
this->dt = this->dtClock.restart().asSeconds();
/* Ýki kare arasýnda geçen zaman
This shows elapsed time between two frames */
//std::cout << this->dt << std::endl;
/* FPS Sayacý
This shows the count of frames per second */
//std::cout << "FPS: " << 1.f / this->dt << std::endl;
}
void Game::run(){
this->init();
this->OnStart();
while(window.isOpen()){
this->updateDt();
this->processEvents();
this->OnUpdate();
this->update();
this->render();
}
}
sf::RenderWindow* Game::getWindow(){
return &this->window;
}
sf::Vector2i Game::getMousePosition(){
return sf::Mouse::getPosition(this->window);
}
void Game::processEvents(){
/* Pencere ile ilgili olaylar burada tanýmlanýr. */
sf::Event event;
while(window.pollEvent(event)){
if(event.type == sf::Event::Closed){
window.close();
}
if(event.type == sf::Event::Resized){
globals.setWindowBounds(sf::VideoMode(this->window.getSize().x, this->window.getSize().y));
//std::cout << this->window.getSize().x << " : " << this->window.getSize().y << std::endl;
}
if(this->states.empty()){
window.close();
}
}
}
void Game::init(){
/* Init something
Stateler ve eklenecek varlýklar burada setleniyor. */
this->initStates();
this->initEntities();
globals.setCurrentEntities(this->states.top()->getEntities());
this->states.top()->createSolidEntities();
}
void Game::update(){
/* Stack'te tutulan statelerin en üstteki update ediliyor. */
if(!this->states.empty()){
this->states.top()->update(dt);
if(this->states.top()->getQuit()){
this->states.top()->endState();
delete this->states.top();
this->states.pop();
globals.setCurrentEntities(this->states.top()->getEntities());
}
}
}
void Game::render(){
this->window.clear(sf::Color::White);
//this->window.clear(sf::Color(128, 190, 31));
/* Ekrana çizdirme olaylarý burada gerçekleþiyor. */
if(!this->states.empty()){
/* State sýnýflarý da sf::Drawable sýnýfýndan
türetildiði için bu þekilde bir kullaným yapabiliyoruz. */
this->window.draw(*this->states.top());
}
this->window.display();
}
void Game::initStates(){
/* Oyun baþlatýldýðýnda herhangi bir state belirtilmemiþse
GameState türünde bir state ile oyun baþlatýlýyor. */
if(this->states.empty()){
this->states.push(new GameState());
}
}
void Game::initEntities(){
/* Kullanýcý tarafýndan eklenecek entityler,
state listesinin en üstüne ekleniyor. */
if(!this->states.empty()){
for(auto* newEntity : this->newEntities){
this->states.top()->addEntity(newEntity);
}
}
}