2014-12-15 16:47:30 +01:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* OpenMiner
|
2020-02-25 01:42:10 +09:00
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
|
2020-02-25 01:42:10 +09:00
|
|
|
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
|
|
|
|
*
|
|
|
|
* This file is part of OpenMiner.
|
2014-12-15 16:47:30 +01:00
|
|
|
*
|
2020-02-25 01:42:10 +09:00
|
|
|
* OpenMiner is free software; you can redistribute it and/or
|
2020-02-08 18:34:26 +09:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2014-12-15 16:47:30 +01:00
|
|
|
*
|
2020-02-25 01:42:10 +09:00
|
|
|
* OpenMiner is distributed in the hope that it will be useful,
|
2020-02-08 18:34:26 +09:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2014-12-15 16:47:30 +01:00
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2020-02-25 01:42:10 +09:00
|
|
|
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
|
2020-02-08 18:34:26 +09:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2014-12-15 16:47:30 +01:00
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
2020-03-13 21:11:01 +01:00
|
|
|
#include "filesystem.hpp"
|
|
|
|
|
2018-12-29 02:23:23 +01:00
|
|
|
#include <gk/core/input/GamePad.hpp>
|
|
|
|
#include <gk/core/Mouse.hpp>
|
2019-02-27 02:17:43 +01:00
|
|
|
#include <gk/gl/GLCheck.hpp>
|
2018-12-29 02:23:23 +01:00
|
|
|
|
2020-03-11 03:53:04 +01:00
|
|
|
#include "BlockGeometry.hpp"
|
2019-01-09 20:44:58 +01:00
|
|
|
#include "ClientApplication.hpp"
|
2014-12-15 16:47:30 +01:00
|
|
|
#include "Config.hpp"
|
2020-03-04 14:58:14 +01:00
|
|
|
#include "EngineConfig.hpp"
|
2020-03-03 15:39:50 +01:00
|
|
|
#include "Font.hpp"
|
2020-01-30 15:31:49 +09:00
|
|
|
#include "TextureAtlas.hpp"
|
2018-12-29 02:23:23 +01:00
|
|
|
#include "TextureLoader.hpp"
|
2019-04-08 18:13:51 +02:00
|
|
|
|
2020-01-10 16:08:57 +09:00
|
|
|
#include "TitleScreenState.hpp"
|
2014-12-15 16:47:30 +01:00
|
|
|
|
2020-03-13 21:11:01 +01:00
|
|
|
namespace fs = ghc::filesystem;
|
2019-01-16 22:40:29 +01:00
|
|
|
|
|
|
|
ClientApplication::ClientApplication(int argc, char **argv) : gk::CoreApplication(argc, argv) {
|
2020-03-11 03:53:04 +01:00
|
|
|
BlockGeometry::initOrientation();
|
2019-01-16 22:40:29 +01:00
|
|
|
}
|
|
|
|
|
2019-01-09 20:44:58 +01:00
|
|
|
void ClientApplication::init() {
|
2019-01-22 00:08:04 +01:00
|
|
|
m_argumentParser.addArgument("host", {"-h", "--host", true});
|
2019-01-21 23:59:43 +01:00
|
|
|
m_argumentParser.addArgument("port", {"-p", "--port", true});
|
2020-03-04 14:58:14 +01:00
|
|
|
m_argumentParser.addArgument("singleplayer", {"-s", "--singleplayer", false});
|
2020-03-04 20:42:58 +01:00
|
|
|
m_argumentParser.addArgument("multiplayer", {"-m", "--multiplayer", false});
|
2020-03-13 21:11:01 +01:00
|
|
|
m_argumentParser.addArgument("working_dir", {"-w", "--working-dir", true});
|
2019-01-21 23:59:43 +01:00
|
|
|
|
2018-12-29 02:23:23 +01:00
|
|
|
gk::CoreApplication::init();
|
|
|
|
|
2020-03-13 21:11:01 +01:00
|
|
|
if (m_argumentParser.getArgument("working_dir").isFound)
|
|
|
|
fs::current_path(m_argumentParser.getArgument("working_dir").parameter);
|
|
|
|
|
2019-01-22 00:08:04 +01:00
|
|
|
if (m_argumentParser.getArgument("host").isFound)
|
|
|
|
m_host = m_argumentParser.getArgument("host").parameter;
|
2019-01-21 23:59:43 +01:00
|
|
|
if (m_argumentParser.getArgument("port").isFound)
|
|
|
|
m_port = std::stoi(m_argumentParser.getArgument("port").parameter);
|
|
|
|
|
2020-02-15 23:06:01 +09:00
|
|
|
Config::loadConfigFromFile("config.lua");
|
|
|
|
|
2019-04-08 20:22:35 +02:00
|
|
|
m_keyboardHandler.loadKeysFromFile("resources/config/keys.xml");
|
2018-12-29 02:23:23 +01:00
|
|
|
gk::GamePad::init(m_keyboardHandler);
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2020-02-15 22:48:56 +09:00
|
|
|
createWindow(Config::screenWidth, Config::screenHeight, APP_NAME);
|
2020-02-24 23:16:51 +09:00
|
|
|
m_window.setVerticalSyncEnabled(Config::isVerticalSyncEnabled);
|
2019-01-02 01:44:12 +01:00
|
|
|
m_window.disableView();
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2018-12-29 23:24:38 +01:00
|
|
|
initOpenGL();
|
|
|
|
|
2018-06-21 05:45:17 +02:00
|
|
|
m_resourceHandler.loadConfigFile<TextureLoader>("resources/config/textures.xml");
|
2020-03-03 15:39:50 +01:00
|
|
|
m_resourceHandler.add<Font>("font-ascii", "texture-font", "resources/textures/font.properties");
|
2020-01-30 15:31:49 +09:00
|
|
|
m_resourceHandler.add<TextureAtlas>("atlas-blocks");
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2018-06-23 23:05:36 +02:00
|
|
|
Registry::setInstance(m_registry);
|
2019-01-07 03:55:37 +01:00
|
|
|
|
2020-03-04 14:58:14 +01:00
|
|
|
auto &titleScreen = m_stateStack.push<TitleScreenState>(m_port);
|
|
|
|
if (m_argumentParser.getArgument("singleplayer").isFound)
|
2020-03-04 17:17:02 +01:00
|
|
|
titleScreen.startSingleplayer(false);
|
2020-03-05 11:43:53 +01:00
|
|
|
else if (m_argumentParser.getArgument("multiplayer").isFound)
|
2020-03-04 20:42:58 +01:00
|
|
|
titleScreen.startMultiplayer(m_host);
|
2015-07-17 17:08:53 +02:00
|
|
|
}
|
|
|
|
|
2020-01-27 15:33:06 +09:00
|
|
|
void ClientApplication::handleEvents() {
|
|
|
|
gk::CoreApplication::handleEvents();
|
|
|
|
|
|
|
|
if ((Config::isFullscreenModeEnabled && m_window.getWindowMode() != gk::Window::Mode::Fullscreen)
|
|
|
|
|| (!Config::isFullscreenModeEnabled && m_window.getWindowMode() != gk::Window::Mode::Windowed)) {
|
|
|
|
m_window.setWindowMode(Config::isFullscreenModeEnabled ? gk::Window::Mode::Fullscreen : gk::Window::Mode::Windowed);
|
|
|
|
}
|
2020-02-15 22:25:29 +09:00
|
|
|
|
2020-02-15 22:48:56 +09:00
|
|
|
if (Config::screenWidth != m_window.getSize().x || Config::screenHeight != m_window.getSize().y) {
|
|
|
|
m_window.resize(Config::screenWidth, Config::screenHeight);
|
2020-02-15 22:25:29 +09:00
|
|
|
}
|
2020-02-24 23:16:51 +09:00
|
|
|
|
|
|
|
if (Config::isVerticalSyncEnabled != m_window.isVerticalSyncEnabled())
|
|
|
|
m_window.setVerticalSyncEnabled(Config::isVerticalSyncEnabled);
|
2020-01-27 15:33:06 +09:00
|
|
|
}
|
|
|
|
|
2020-02-18 18:59:40 +09:00
|
|
|
void ClientApplication::onEvent(const SDL_Event &event) {
|
|
|
|
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F11)
|
|
|
|
Config::isFullscreenModeEnabled ^= 1;
|
|
|
|
}
|
|
|
|
|
2019-01-09 20:44:58 +01:00
|
|
|
void ClientApplication::initOpenGL() {
|
2019-01-07 03:55:37 +01:00
|
|
|
// Enable transparency
|
2019-02-27 02:17:43 +01:00
|
|
|
glCheck(glEnable(GL_BLEND));
|
|
|
|
glCheck(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
|
2018-12-29 23:24:38 +01:00
|
|
|
|
2019-01-07 03:55:37 +01:00
|
|
|
// Enable depth and hide backside of faces
|
2019-02-27 02:17:43 +01:00
|
|
|
glCheck(glEnable(GL_DEPTH_TEST));
|
|
|
|
glCheck(glEnable(GL_CULL_FACE));
|
2018-12-29 23:24:38 +01:00
|
|
|
|
2019-02-27 02:17:43 +01:00
|
|
|
glCheck(glEnable(GL_POLYGON_OFFSET_FILL));
|
|
|
|
glCheck(glPolygonOffset(1, 1));
|
2018-12-29 23:24:38 +01:00
|
|
|
|
2019-01-07 03:55:37 +01:00
|
|
|
// Set best quality for mipmaps
|
2019-02-27 02:17:43 +01:00
|
|
|
glCheck(glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST));
|
2018-12-29 23:24:38 +01:00
|
|
|
}
|
|
|
|
|