Coverage report for gui


src/
File: src/render/core/Core.cpp
Date: 2024-06-25 10:57:02
Lines:
0/96
0.0%
Functions:
0/10
0.0%
Branches:
0/174
0.0%

Line Branch Exec Source
1 #include "Core.hpp"
2
3 #include "../scenes/world/World.hpp"
4 #include "../scenes/Home.hpp"
5 #include "../scenes/Quit.hpp"
6 #include "../scenes/Menu.hpp"
7 #include "../scenes/WinScene.hpp"
8 #include <iostream>
9 #include "../../utils/GuiException.hpp"
10
11 Core::Core(int port, std::string ip) {
12 _window.create(
13 sf::VideoMode(_resolution.x , _resolution.y),
14 "Zappy",
15 sf::Style::Close);
16 _font.loadFromFile("assets/BadComic-Regular.ttf");
17 _state = GameState::HOME;
18 _upperState = GameState::DEFAULT;
19 _shade = sf::RectangleShape(sf::Vector2f(1920, 1080));
20 _shade.setFillColor(sf::Color(0, 0, 0, 150));
21
22 _scenes[GameState::HOME] = std::make_shared<Home>(this, port, ip);
23 _scenes[GameState::END] = std::make_shared<Quit>(this);
24 _scenes[GameState::GAME] = std::make_shared<World>(this);
25 _scenes[GameState::MENU] = std::make_shared<Menu>(this);
26 _scenes[GameState::WIN] = std::make_shared<WinScene>(this);
27 _clock.restart();
28 initSounds();
29 _sounds["music"].play();
30 std::cout << "play music" << std::endl;
31
32 if (!_cursorTexture.loadFromFile("assets/cursor.png"))
33 throw guiException("Failed to load cursor");
34 _cursor.setTexture(_cursorTexture);
35 _window.setMouseCursorVisible(false);
36 initIcon();
37 }
38
39 void Core::update() {
40 _deltaTime = _clock.getElapsedTime().asSeconds();
41 _clock.restart();
42
43 while (_window.pollEvent(_event)) {
44 if (_event.type == sf::Event::Closed)
45 _upperState = GameState::END;
46 if (_event.type == sf::Event::MouseMoved) {
47 _realMousePos = sf::Vector2f(_event.mouseMove.x, _event.mouseMove.y);
48 if (_state == GameState::GAME) {
49 _mousePos = sf::Vector2f(
50 _event.mouseMove.x * 1920 / _window.getSize().x,
51 _event.mouseMove.y * 1080 / _window.getSize().y
52 );
53 } else
54 _mousePos = sf::Vector2f(_event.mouseMove.x, _event.mouseMove.y);
55 }
56 _scenes[
57 ((_upperState != GameState::DEFAULT) ? _upperState : _state)
58 ]->update(_event, _window);
59 }
60 _scenes[
61 ((_upperState != GameState::DEFAULT) ? _upperState : _state)
62 ]->update(_deltaTime);
63 }
64
65 void Core::run() {
66 while (_window.isOpen()) {
67 _parser.updateData(_data, _server);
68 auto players = _data.getPlayers();
69 for (auto &player : players)
70 player.second->getNextEvent();
71 update();
72 draw();
73 }
74 }
75
76 void Core::draw() {
77 _window.clear(sf::Color(82,198,255));
78
79 _scenes[_state]->draw(_window);
80 if (_upperState != GameState::DEFAULT) {
81 _window.draw(_shade);
82 _scenes[_upperState]->draw(_window);
83 }
84 _cursor.setPosition(_window.mapPixelToCoords(sf::Mouse::getPosition(_window)));
85 _window.draw(_cursor);
86 _window.display();
87 }
88
89 void Core::newResolution(sf::Vector2f resolution) {
90 if (resolution.x == _resolution.x && resolution.y == _resolution.y)
91 return;
92 _resolution = resolution;
93 _window.create(
94 sf::VideoMode(_resolution.x, _resolution.y),
95 "Zappy",
96 (_fullscreen) ? sf::Style::Fullscreen : sf::Style::Close);
97 _window.setMouseCursorVisible(false);
98 }
99
100 void Core::switchFullscreen() {
101 _fullscreen = !_fullscreen;
102 _window.create(
103 sf::VideoMode(_resolution.x, _resolution.y),
104 "Zappy",
105 (_fullscreen) ? sf::Style::Fullscreen : sf::Style::Close);
106 _window.setMouseCursorVisible(false);
107 }
108
109 bool Core::connectToServer(int port, std::string ip) {
110 try {
111 _server.connectToServer(port, ip.c_str());
112 } catch (guiException &e) {
113 return false;
114 }
115 return true;
116 }
117
118 void Core::backToHome() {
119 if (_server.disconectFromServer() == true)
120 _data.resetGame();
121 _upperState = GameState::DEFAULT;
122 _state = GameState::HOME;
123 }
124
125 void Core::initSounds() {
126 _music.openFromFile("assets/audio/music.ogg");
127 _music.setLoop(true);
128 _music.play();
129 }
130
131 void Core::initIcon() {
132 try {
133 sf::Image icon;
134 icon.loadFromFile("assets/icon.png");
135 _window.setIcon(32, 32, icon.getPixelsPtr());
136 } catch (const std::exception &e) {
137 std::cerr << "Failed to set icon" << std::endl;
138 }
139 }
140