Skip to content

Commit

Permalink
[GameState] Now possible to take a screenshot by pressing F2 in-game.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jul 8, 2020
1 parent 5ba2699 commit 4421f82
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ config/client.lua
config/keys.lua
config/server.lua
TODO
Screenshot-*

2 changes: 1 addition & 1 deletion external/gamekit
20 changes: 12 additions & 8 deletions source/client/hud/Chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,25 @@
#include "Client.hpp"
#include "Config.hpp"

Chat::Chat(Client &client) {
Chat::Chat(Client &client) : m_client(client) {
client.setCommandCallback(Network::Command::ChatMessage, [&](sf::Packet &packet) {
u16 clientID;
std::string message;
packet >> clientID >> message;

if (clientID != 0)
m_chatMessages.emplace_back(message, m_posY, &client.getPlayer(clientID));
else
m_chatMessages.emplace_back(message, m_posY);
addChatMessage(clientID, message);
});
}

m_posY += m_chatMessages.back().text().getSize().y + 1;
void Chat::addChatMessage(u16 clientID, const std::string &message) {
if (clientID != 0)
m_chatMessages.emplace_back(message, m_posY, &m_client.getPlayer(clientID));
else
m_chatMessages.emplace_back(message, m_posY);

move(0, -m_chatMessages.back().text().getSize().y - 1);
});
m_posY += m_chatMessages.back().text().getSize().y + 1;

move(0, -m_chatMessages.back().text().getSize().y - 1);
}

void Chat::setMessageVisibility(bool areMessagesVisible) {
Expand Down
4 changes: 4 additions & 0 deletions source/client/hud/Chat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class Chat : public gk::Drawable, public gk::Transformable {
public:
Chat(Client &client);

void addChatMessage(u16 clientID, const std::string &message);

void setMessageVisibility(bool areMessagesVisible);

const std::string &getHistoryEntry(u32 id) const { return m_history.at(m_history.size() - id - 1); }
Expand All @@ -46,6 +48,8 @@ class Chat : public gk::Drawable, public gk::Transformable {
private:
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;

Client &m_client;

std::deque<ChatMessage> m_chatMessages;
std::deque<std::string> m_history;

Expand Down
13 changes: 13 additions & 0 deletions source/client/states/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ void GameState::onEvent(const SDL_Event &event) {
}
}
else if (event.type == SDL_KEYDOWN) {
if (event.key.keysym.sym == SDLK_F2) {
std::time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());

char filename[100];
std::strftime(filename, sizeof(filename), "Screenshot-%Y-%m-%d-%H-%M-%S.png", std::localtime(&now));

bool isScreenshotSaved = gk::Window::saveScreenshot(0, 0, Config::screenWidth, Config::screenHeight, filename);
if (isScreenshotSaved)
m_hud.chat().addChatMessage(0, "Screenshot saved: " + std::string(filename));
else
m_hud.chat().addChatMessage(0, "Failed to save screenshot");
}

for (auto &key : Registry::getInstance().keys()) {
if (event.key.keysym.sym == key.keycode()) {
m_clientCommandHandler.sendKeyPressed(key.id());
Expand Down

0 comments on commit 4421f82

Please sign in to comment.