Skip to content

Commit

Permalink
[ItemWidget] Fixed item rendering after a texture pack switch. [GameC…
Browse files Browse the repository at this point in the history
…onfig] 'texturePack' moved to 'Config' ans is now saved on client close.
  • Loading branch information
Unarelith committed Jul 17, 2020
1 parent 98c599d commit 072c3b2
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 17 deletions.
3 changes: 1 addition & 2 deletions source/client/core/ClientApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include "Config.hpp"
#include "EngineConfig.hpp"
#include "Font.hpp"
#include "GameConfig.hpp"
#include "TextureAtlas.hpp"
#include "TextureLoader.hpp"
#include "Vertex.hpp"
Expand Down Expand Up @@ -101,7 +100,7 @@ void ClientApplication::init() {
Config::defaultUsername = m_argumentParser.getArgument("username").parameter;

if (m_argumentParser.getArgument("texture-pack").isFound)
GameConfig::texturePack = m_argumentParser.getArgument("texture-pack").parameter;
Config::texturePack = m_argumentParser.getArgument("texture-pack").parameter;

m_keyboardHandler.loadKeysFromFile("config/keys.lua");
gk::GamePad::init(m_keyboardHandler);
Expand Down
3 changes: 3 additions & 0 deletions source/client/core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ u8 Config::mouseSensitivity = 8;
// Other
std::string Config::defaultUsername = "";
std::string Config::defaultServerAddress = "localhost:4242";
std::string Config::texturePack = "";

void Config::loadConfigFromFile(const char *filename) {
if (!fs::exists("config") && !fs::create_directory("config")) {
Expand Down Expand Up @@ -107,6 +108,7 @@ void Config::loadConfigFromFile(const char *filename) {

defaultUsername = lua["defaultUsername"].get_or(defaultUsername);
defaultServerAddress = lua["defaultServerAddress"].get_or(defaultServerAddress);
texturePack = lua["texturePack"].get_or(texturePack);

gkInfo() << "Config file loaded successfully";
}
Expand Down Expand Up @@ -144,5 +146,6 @@ void Config::saveConfigToFile(const char *filename) {
file << std::endl;
file << "defaultUsername = \"" << defaultUsername << "\"" << std::endl;
file << "defaultServerAddress = \"" << defaultServerAddress << "\"" << std::endl;
file << "texturePack = \"" << texturePack << "\"" << std::endl;
}

1 change: 1 addition & 0 deletions source/client/core/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ namespace Config {
// Other
extern std::string defaultUsername;
extern std::string defaultServerAddress;
extern std::string texturePack;

void loadConfigFromFile(const char *filename);
void saveConfigToFile(const char *filename);
Expand Down
1 change: 0 additions & 1 deletion source/client/core/GameConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

// Pre-game configuration
std::string GameConfig::worldName = "";
std::string GameConfig::texturePack = "";

// Screen effects
u16 GameConfig::currentScreenEffect = 0;
Expand Down
1 change: 0 additions & 1 deletion source/client/core/GameConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
namespace GameConfig {
// Pre-game configuration
extern std::string worldName;
extern std::string texturePack;

// Screen effects
extern u16 currentScreenEffect;
Expand Down
8 changes: 3 additions & 5 deletions source/client/gui/ItemWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/
#include <gk/resource/ResourceHandler.hpp>

#include "Config.hpp"
#include "ItemWidget.hpp"
#include "Registry.hpp"
#include "TextureAtlas.hpp"
Expand Down Expand Up @@ -57,12 +58,9 @@ void ItemWidget::update() {
m_text.setPosition(16 - 4 - 6 * floor(log10(stack().amount())), 16 - 6, 0);
}

#include "GameConfig.hpp"

void ItemWidget::updateImage(const BlockState *blockState) {
static std::string oldTexturePack{""};
if (m_image.width() == 0 || oldTexturePack != GameConfig::texturePack) {
oldTexturePack = GameConfig::texturePack;
if (m_image.width() == 0 || m_oldTexturePack != Config::texturePack) {
m_oldTexturePack = Config::texturePack;

m_image.load(m_textureAtlas.texture());
m_image.setPosition(1, 1, 0);
Expand Down
2 changes: 2 additions & 0 deletions source/client/gui/ItemWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class ItemWidget : public Widget {
bool m_isImage = false;

bool m_hasChanged = false;

std::string m_oldTexturePack{""};
};

#endif // ITEMWIDGET_HPP_
3 changes: 1 addition & 2 deletions source/client/states/ServerLoadingState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

#include "Config.hpp"
#include "ConnectionErrorState.hpp"
#include "GameConfig.hpp"
#include "GameState.hpp"
#include "Events.hpp"
#include "ServerLoadingState.hpp"
Expand Down Expand Up @@ -96,7 +95,7 @@ void ServerLoadingState::update() {
}
else if (!m_game.textureAtlas().isReady()) {
try {
m_game.textureAtlas().loadFromRegistry(GameConfig::texturePack);
m_game.textureAtlas().loadFromRegistry(Config::texturePack);
}
catch (gk::Exception &e) {
m_game.client().disconnect();
Expand Down
11 changes: 5 additions & 6 deletions source/client/states/TexturePackSelectionState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include <filesystem.hpp>

#include "Config.hpp"
#include "GameConfig.hpp"
#include "TextureAtlas.hpp"
#include "TexturePackSelectionState.hpp"
#include "World.hpp"
Expand All @@ -55,12 +54,12 @@ TexturePackSelectionState::TexturePackSelectionState(gk::ApplicationState *paren
const ScrollableListElement *element = m_texturePackList.selectedElement();
if (element) {
std::string texturePack = (element->id()) ? element->line1() : "";
if (GameConfig::texturePack != texturePack) {
GameConfig::texturePack = texturePack;
if (Config::texturePack != texturePack) {
Config::texturePack = texturePack;

auto &atlas = gk::ResourceHandler::getInstance().get<TextureAtlas>("atlas-blocks");
atlas.clear();
atlas.loadFromRegistry(GameConfig::texturePack);
atlas.loadFromRegistry(Config::texturePack);

World::isReloadRequested = true;
}
Expand Down Expand Up @@ -122,7 +121,7 @@ void TexturePackSelectionState::updateWidgetPosition() {
}

void TexturePackSelectionState::loadTexturePackList() {
m_texturePackList.addElement("No Texture Pack", "Use default mod textures", "", GameConfig::texturePack.empty());
m_texturePackList.addElement("No Texture Pack", "Use default mod textures", "", Config::texturePack.empty());

if (fs::is_directory("texturepacks")) {
std::vector<fs::directory_entry> dirs;
Expand Down Expand Up @@ -151,7 +150,7 @@ void TexturePackSelectionState::loadTexturePackList() {
textures += "GUI";
}

bool isSelected = GameConfig::texturePack == dirname;
bool isSelected = Config::texturePack == dirname;
m_texturePackList.addElement(dirname, textures, "", isSelected);
}
}
Expand Down

0 comments on commit 072c3b2

Please sign in to comment.