-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameEngine.cpp
More file actions
116 lines (96 loc) · 3.17 KB
/
GameEngine.cpp
File metadata and controls
116 lines (96 loc) · 3.17 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
#include <iostream>
#include "GameEngine.h"
#include "Scene_Play.h"
GameEngine::GameEngine(const std::string& configPath)
{
//we need to load game specific from config file
init();
}
void GameEngine::init()
{
std::cout << "Started Engine" << std::endl;
//load window
m_window.create(sf::VideoMode(1280, 720), "Game Window");
m_window.setFramerateLimit(60);
//load game assets here
m_assets = Assets();
m_assets.addTexture("player", "sprites/character.png");
m_assets.addTexture("player_idle", "sprites/character_idle.png");
m_assets.addTexture("player_walk", "sprites/character_walk.png");
m_assets.addTexture("player_jump", "sprites/character_jump.png");
m_assets.addTexture("environment", "sprites/environment.png");
//m_assets.addTexture("bg", "sprites/tilemap_backgrounds.png");
//set default current scene
changeScene("play", std::make_shared<Scene_Play>(this, "levelpath.text"));
update();
}
void GameEngine::update()
{
//run game main loop here
while (m_running) {
sUserInput();
m_scenes[m_currentScene]->update();
}
}
void GameEngine::quit()
{
}
std::shared_ptr<Scene> GameEngine::currentScene()
{
return m_scenes[m_currentScene];
}
void GameEngine::changeScene(const std::string& name,std::shared_ptr<Scene> scene)
{
m_currentScene = name;
m_scenes[name] = scene;
}
Assets& GameEngine::getAssets()
{
return m_assets;
}
Physics& GameEngine::getPhysics()
{
return m_physics;
}
sf::RenderWindow& GameEngine::getWindow()
{
return m_window;
}
void GameEngine::sUserInput()
{
sf::Event event;
while (m_window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
{
m_running = false;
}
//player mechanics related keys
//key pressed
if (event.type == sf::Event::KeyPressed || event.type == sf::Event::KeyReleased) {
//if the current scene does not have an action associated with the key , skip the event
//.find(...) looks for this key in the map.
//If found, it returns an iterator pointing to the key - value pair.
//If not found, it returns actionMap.end().
if (currentScene()->getActionMap().find(event.key.code) == currentScene()->getActionMap().end()) {
continue;
}
const std::string actionType = (event.type == sf::Event::KeyPressed) ? "START" : "END";
//look up the action and send the action to the scene
currentScene()->sDoAction(Action(currentScene()->getActionMap().at(event.key.code), actionType));
}
if (event.type == sf::Event::MouseButtonPressed || event.type == sf::Event::MouseButtonReleased) {
//adding extra offset for mouse
int keycode = event.mouseButton.button + 1000;
//if the current scene does not have an action associated with the key , skip the event
//.find(...) looks for this key in the map.
//If found, it returns an iterator pointing to the key - value pair.
//If not found, it returns actionMap.end().
if (currentScene()->getActionMap().find(keycode) == currentScene()->getActionMap().end()) {
continue;
}
const std::string actionType = (event.type == sf::Event::MouseButtonPressed) ? "START" : "END";
//look up the action and send the action to the scene
currentScene()->sDoAction(Action(currentScene()->getActionMap().at(keycode), actionType));
}
}
}