Skip to content

Commit

Permalink
feat(): add tick rate modification
Browse files Browse the repository at this point in the history
  • Loading branch information
DiaboloAB committed Jun 23, 2024
1 parent fc351e1 commit 2d49d4d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/gui/src/render/core/Core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Core {
ServerConnect _server;
std::map<std::string, sf::Sound> _sounds;
bool isFullscreen() { return _fullscreen; }
int _tickRate = 2;

bool _funMode = false;
private:
Expand Down
24 changes: 22 additions & 2 deletions src/gui/src/render/scenes/Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Menu::Menu(Core *core) : _core(core) {
_quitButton = std::make_shared<Button>(sf::Vector2f(100, 500), sf::Vector2f(100, 100), "Quit", _core->getFont());
_homeButton = std::make_shared<Button>(sf::Vector2f(100, 450), sf::Vector2f(100, 100), "Home", _core->getFont());
_homeButton->setCallBack(std::bind(&Menu::getBackHome, this));
_hzInput = std::make_shared<Input>(sf::Vector2f(100, 200), sf::Vector2f(100, 100), "60", _core->getFont(), "123456789");
_hzInput = std::make_shared<Input>(sf::Vector2f(100, 200), sf::Vector2f(100, 100), "60", _core->getFont(), "1234567890");
_hzInput->setEnd("Hz");
_hzInput->setInput("60");
_funMode = std::make_shared<Button>(sf::Vector2f(100, 250), sf::Vector2f(100, 100), "Fun Mode Off", _core->getFont());
Expand Down Expand Up @@ -43,7 +43,27 @@ bool Menu::update(sf::Event event, sf::RenderWindow &window) {
if (_quitButton->update(event, window))
window.close();
_homeButton->update(event, window);
_hzInput->update(event, window);
if (_hzInput->isFocused() == false) {
if (_hzInput->getInput().size() == 0)
_hzInput->setInput("0");
if (std::atoi(_hzInput->getInput().c_str()) != _core->_tickRate)
_hzInput->setInput(std::to_string(_core->_tickRate));
}
if (_hzInput->update(event, window)) {
int newTickRate = 2;
try {
newTickRate = std::stoi(_hzInput->getInput());
} catch (std::exception &e) {
}
if (newTickRate < 2)
newTickRate = 2;
if (newTickRate > 1000)
newTickRate = 1000;
_core->_tickRate = newTickRate;
_core->_data.requestNewTickRate(newTickRate, _core->_server);
_hzInput->setInput(std::to_string(newTickRate));
return true;
}
return true;
}

Expand Down
3 changes: 2 additions & 1 deletion src/gui/src/render/scenes/world/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ void World::getServerInit()
_teams.push_back(team);
_chat->addMessage(" - " + team);
}

_core->_tickRate = _core->_data.getTickRate();
_chat->addMessage("Tick rate: " + std::to_string(_core->_tickRate));
Ranking::getRanking(_rankings, _core->_data);
}

Expand Down
13 changes: 6 additions & 7 deletions src/gui/src/render/ui/Input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Input::Input(sf::Vector2f pos, [[maybe_unused]] sf::Vector2f size, std::string t

bool Input::update(sf::Event event, sf::RenderWindow &window) {
sf::Vector2f mousePos = window.mapPixelToCoords(sf::Mouse::getPosition(window));
bool ret = false;

if (!_isFocused)
_hover = _text.getGlobalBounds().contains(mousePos.x, mousePos.y);
Expand All @@ -32,20 +33,18 @@ bool Input::update(sf::Event event, sf::RenderWindow &window) {
_input.pop_back();
} else if (event.text.unicode == 13) {
_isFocused = false;
ret = true;
} else {
if (_accept.find(event.text.unicode) != std::string::npos)
_input += event.text.unicode;
}
}
if (event.type == sf::Event::KeyPressed) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::BackSpace) && sf::Keyboard::isKeyPressed(sf::Keyboard::LControl)) {
if (event.type == sf::Event::KeyPressed &&
sf::Keyboard::isKeyPressed(sf::Keyboard::BackSpace) &&
sf::Keyboard::isKeyPressed(sf::Keyboard::LControl))
_input = "";
}
}
}


return false;
return ret;
}

void Input::draw(sf::RenderWindow &window, float deltaTime) {
Expand Down
3 changes: 3 additions & 0 deletions src/gui/src/render/ui/Input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class Input {
void setEnd(std::string end) {
_end = end;
}
bool isFocused() {
return _isFocused;
}

protected:
private:
Expand Down

0 comments on commit 2d49d4d

Please sign in to comment.