-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
143 lines (118 loc) · 3.13 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
#include "Game.hpp"
#include <iostream>
#include <chrono>
using namespace std::chrono_literals;
Game::Game(unsigned int width, unsigned int height, std::string name)
: m_width{width}, m_height{height}, m_name{name}
{
}
void Game::run()
{
//Showing loading screen
loadingScreen();
m_future = std::async(std::launch::async, [&]
{
return Setup();
});
GameLoop();
}
//Initialize only need for loading screen and others initialize in setup function
void Game::loadingScreen()
{
m_window.create(sf::VideoMode(m_width, m_height), m_name);
m_window.setFramerateLimit(30);
if (!m_loadingTex.loadFromFile("assets/images/sencan.png"))
{
std::cout << " \"assets/images/sencan.png\" is not exsist\n";
}
m_loadingSpr.setTexture(m_loadingTex);
m_loadingSpr.setScale(sf::Vector2f(0.3, 0.3));
m_loadingSpr.setOrigin(m_loadingTex.getSize().x / 2, m_loadingTex.getSize().y / 2);
m_loadingSpr.setPosition(m_width >> 1, (m_height >> 1) + m_loadingTex.getSize().y / 30);
if (!m_animTex.loadFromFile("assets/images/loading.png"))
{
std::cout << " \"assets/images/loading.png\" is not exsist\n";
}
rect = sf::IntRect(0, 0, 188, 188);
m_animSpr.setScale(0.3f, 0.3f);
m_animSpr.setTexture(m_animTex);
m_animSpr.setTextureRect(rect);
m_animSpr.setOrigin((m_animTex.getSize().x * 0.3f) / 2, (m_animTex.getSize().y * 0.3f) / 2);
m_animSpr.setPosition(m_width, m_height - 50);
if (!m_loadingFont.loadFromFile("assets/fonts/komika-text_bold.ttf"))
{
std::cout << " \"assets/images/loading.png\" is not exsist\n";
}
m_loadingText.setFont(m_loadingFont);
m_loadingText.setFillColor(sf::Color::Black);
m_loadingText.setString("LOADING");
m_loadingText.setCharacterSize(24);
m_loadingText.setPosition(m_animSpr.getPosition().x - 225, m_animSpr.getPosition().y - m_loadingText.getCharacterSize());
}
//Initialize all you need in game
void Game::Setup()
{
//Initialize here
m_playText.setFont(m_loadingFont);
m_playText.setCharacterSize(60);
m_playText.setString("Game is running :)");
m_playText.setFillColor(sf::Color::White);
m_playText.setOrigin((m_playText.getCharacterSize() * m_playText.getString().getSize()) >> 1, m_playText.getCharacterSize() >> 1);
m_playText.setPosition(m_width, m_height >> 1);
//Waiting 10 seconds
std::this_thread::sleep_for(10s);
//After initializing return
return;
}
//Simple GameLoop
void Game::GameLoop()
{
while (m_window.isOpen())
{
update();
draw();
}
}
void Game::update()
{
m_status = m_future.wait_for(0ms);
//Still Running
if (m_status != std::future_status::ready)
{
if (rect.left < 2256 - 188)
rect.left += 188;
else
{
if (rect.top < 752 - 188)
rect.top += 188;
else
rect.top = 0;
rect.left = 0;
}
m_animSpr.setTextureRect(rect);
}
//Game is playing here!!!
else
{
std::cout << "Playing\n";
}
}
void Game::draw()
{
//Still running draw loading screen
if (m_status != std::future_status::ready)
{
m_window.clear(sf::Color::White);
m_window.draw(m_loadingSpr);
m_window.draw(m_animSpr);
m_window.draw(m_loadingText);
m_window.display();
}
//Game is playing here!!!
else
{
m_window.clear();
m_window.draw(m_playText);
m_window.display();
}
}