Skip to content

Commit

Permalink
Basic texture pack system added (see #34).
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Mar 17, 2020
1 parent ef79c5b commit 93308c9
Show file tree
Hide file tree
Showing 88 changed files with 62 additions and 18 deletions.
9 changes: 6 additions & 3 deletions source/client/core/ClientApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ void ClientApplication::init() {
m_argumentParser.addArgument("port", {"-p", "--port", true});
m_argumentParser.addArgument("singleplayer", {"-s", "--singleplayer", false});
m_argumentParser.addArgument("multiplayer", {"-m", "--multiplayer", false});
m_argumentParser.addArgument("working_dir", {"-w", "--working-dir", true});
m_argumentParser.addArgument("working-dir", {"-w", "--working-dir", true});
m_argumentParser.addArgument("texture-pack", {"-t", "--texture-pack", true});

gk::CoreApplication::init();

if (m_argumentParser.getArgument("working_dir").isFound)
fs::current_path(m_argumentParser.getArgument("working_dir").parameter);
if (m_argumentParser.getArgument("working-dir").isFound)
fs::current_path(m_argumentParser.getArgument("working-dir").parameter);

if (m_argumentParser.getArgument("host").isFound)
m_host = m_argumentParser.getArgument("host").parameter;
Expand All @@ -81,6 +82,8 @@ void ClientApplication::init() {
Registry::setInstance(m_registry);

auto &titleScreen = m_stateStack.push<TitleScreenState>(m_port);
if (m_argumentParser.getArgument("texture-pack").isFound)
titleScreen.setTexturePack(m_argumentParser.getArgument("texture-pack").parameter);
if (m_argumentParser.getArgument("singleplayer").isFound)
titleScreen.startSingleplayer(false);
else if (m_argumentParser.getArgument("multiplayer").isFound)
Expand Down
32 changes: 26 additions & 6 deletions source/client/graphics/TextureAtlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,44 @@ void TextureAtlas::packTextures() {
m_texture.loadFromSurface(atlas.get());
}

void TextureAtlas::loadFromRegistry() {
addFile("mods/default/textures/blocks/", "undefined.png");
#include <gk/core/Filesystem.hpp>

void TextureAtlas::loadFromRegistry(const std::string &texturePack) {
if (!gk::Filesystem::fileExists("texturepacks/" + texturePack))
throw EXCEPTION("Texture pack '" + texturePack +"' doesn't exist");

if (texturePack.empty())
addFile("mods/default/textures/blocks/", "undefined.png");
else
addFile("texturepacks/" + texturePack + "/blocks/", "undefined.png");

for (auto &block : Registry::getInstance().blocks()) {
std::string path;
if (texturePack.empty())
path = "mods/" + block->modName() + "/textures/blocks/";
else
path = "texturepacks/" + texturePack + "/blocks/";

const TilesDef &tiles = block->tiles();
for (auto &textureFilename : tiles.textureFilenames())
addFile("mods/" + block->modName() + "/textures/blocks/", textureFilename);
addFile(path, textureFilename);
for (auto &textureFilename : tiles.altTextureFilenames())
addFile("mods/" + block->modName() + "/textures/blocks/", textureFilename);
addFile(path, textureFilename);
}

for (auto &item : Registry::getInstance().items()) {
if (!item.isBlock() || !item.tiles().textureFilenames().empty()) {
std::string path;
if (texturePack.empty())
path = "mods/" + item.modName() + "/textures/items/";
else
path = "texturepacks/" + texturePack + "/items/";

const TilesDef &tiles = item.tiles();
for (auto &textureFilename : tiles.textureFilenames())
addFile("mods/" + item.modName() + "/textures/items/", textureFilename);
addFile(path, textureFilename);
for (auto &textureFilename : tiles.altTextureFilenames())
addFile("mods/" + item.modName() + "/textures/items/", textureFilename);
addFile(path, textureFilename);
}
}

Expand Down
2 changes: 1 addition & 1 deletion source/client/graphics/TextureAtlas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class TextureAtlas {
void addFile(const std::string &path, const std::string &filename);
void packTextures();

void loadFromRegistry();
void loadFromRegistry(const std::string &texturePack = "");

u16 getTextureID(const std::string &filename) const;
gk::FloatRect getTexCoords(const std::string &filename, bool normalized = true) const;
Expand Down
3 changes: 2 additions & 1 deletion source/client/states/ServerConnectState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ ServerConnectState::ServerConnectState(gk::ApplicationState *parent) : Interface
auto &game = m_stateStack->push<GameState>();
game.connect(host, port);

m_stateStack->push<ServerLoadingState>(game, true, this);
auto &serverLoadingState = m_stateStack->push<ServerLoadingState>(game, true, this);
serverLoadingState.setTexturePack(m_texturePack);
});

m_cancelButton.setText("Cancel");
Expand Down
4 changes: 4 additions & 0 deletions source/client/states/ServerConnectState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ class ServerConnectState : public InterfaceState {

void update() override;

void setTexturePack(const std::string &texturePack) { m_texturePack = texturePack; }

private:
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;

TextInput m_textInput;

TextButton m_connectButton;
TextButton m_cancelButton;

std::string m_texturePack;
};

#endif // SERVERCONNECTSTATE_HPP_
8 changes: 7 additions & 1 deletion source/client/states/ServerLoadingState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ void ServerLoadingState::update() {
gk::Mouse::setCursorGrabbed(true);
}
else if (!m_game.textureAtlas().isReady()) {
m_game.textureAtlas().loadFromRegistry();
try {
m_game.textureAtlas().loadFromRegistry(m_texturePack);
}
catch (gk::Exception &e) {
m_game.client().disconnect();
throw e;
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions source/client/states/ServerLoadingState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class ServerLoadingState : public InterfaceState {

void update() override;

void setTexturePack(const std::string &texturePack) { m_texturePack = texturePack; }

private:
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;

Expand All @@ -51,6 +53,8 @@ class ServerLoadingState : public InterfaceState {

bool m_showLoadingState;
mutable bool m_hasBeenRendered = false;

std::string m_texturePack;
};

#endif // SERVERLOADINGSTATE_HPP_
8 changes: 5 additions & 3 deletions source/client/states/TitleScreenState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ TitleScreenState::TitleScreenState(u16 port) : m_port(port) {
});

m_menuWidget.addButton("Multiplayer", [this] (TextButton &) {
m_stateStack->push<ServerConnectState>(this);
m_stateStack->push<ServerConnectState>(this).setTexturePack(m_texturePack);
});

m_menuWidget.addButton("Options...", [this] (TextButton &) {
Expand Down Expand Up @@ -92,7 +92,8 @@ void TitleScreenState::update() {
game.setSingleplayer(true);
game.connect("localhost", m_port);

m_stateStack->push<ServerLoadingState>(game, m_showLoadingState, this);
auto &serverLoadingState = m_stateStack->push<ServerLoadingState>(game, m_showLoadingState, this);
serverLoadingState.setTexturePack(m_texturePack);
}
}

Expand All @@ -116,7 +117,8 @@ void TitleScreenState::startMultiplayer(const std::string &host) {
auto &game = m_stateStack->push<GameState>();
game.connect(host, m_port);

m_stateStack->push<ServerLoadingState>(game, false, this);
auto &serverLoadingState = m_stateStack->push<ServerLoadingState>(game, false, this);
serverLoadingState.setTexturePack(m_texturePack);
}

void TitleScreenState::onGuiScaleChanged(const GuiScaleChangedEvent &event) {
Expand Down
4 changes: 4 additions & 0 deletions source/client/states/TitleScreenState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class TitleScreenState : public InterfaceState {
void startSingleplayer(bool showLoadingState);
void startMultiplayer(const std::string &host);

void setTexturePack(const std::string &texturePack) { m_texturePack = texturePack; }

private:
void onGuiScaleChanged(const GuiScaleChangedEvent &event);
void onServerOnlineEvent(const ServerOnlineEvent &event);
Expand All @@ -69,6 +71,8 @@ class TitleScreenState : public InterfaceState {

bool m_isServerOnline = false;
bool m_isServerLaunched = false;

std::string m_texturePack;
};

#endif // TITLESCREENSTATE_HPP_
6 changes: 3 additions & 3 deletions source/server/core/ServerApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ void ServerApplication::init() {
BlockGeometry::initOrientation();

m_argumentParser.addArgument("port", {"-p", "--port", true});
m_argumentParser.addArgument("working_dir", {"-w", "--working-dir", true});
m_argumentParser.addArgument("working-dir", {"-w", "--working-dir", true});

m_argumentParser.parse();

if (m_argumentParser.getArgument("working_dir").isFound)
fs::current_path(m_argumentParser.getArgument("working_dir").parameter);
if (m_argumentParser.getArgument("working-dir").isFound)
fs::current_path(m_argumentParser.getArgument("working-dir").parameter);

if (m_argumentParser.getArgument("port").isFound)
m_port = std::stoi(m_argumentParser.getArgument("port").parameter);
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes

0 comments on commit 93308c9

Please sign in to comment.