Skip to content

Commit

Permalink
First step of SDL2 migration.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jul 7, 2020
1 parent 89f913b commit c9b7990
Show file tree
Hide file tree
Showing 67 changed files with 244 additions and 247 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@
[submodule "external/lua"]
path = external/lua
url = https://github.com/Unarelith/lua
[submodule "external/SFML"]
path = external/SFML
url = https://github.com/SFML/SFML
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ set(BUILD_SHARED_LIBS OFF)
add_subdirectory(external/entt)
include_directories(external/entt/src)

#------------------------------------------------------------------------------
# - SFML
#------------------------------------------------------------------------------
set(SFML_STATIC TRUE)

set(SFML_BUILD_AUDIO FALSE CACHE BOOL "")
set(SFML_BUILD_GRAPHICS FALSE CACHE BOOL "")
set(SFML_BUILD_NETWORK TRUE CACHE BOOL "")
set(SFML_BUILD_WINDOW FALSE CACHE BOOL "")

add_subdirectory(external/SFML)

#------------------------------------------------------------------------------
# - gamekit
#------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions external/SFML
Submodule SFML added at 2f1171
28 changes: 7 additions & 21 deletions source/client/core/ClientApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,7 @@ void ClientApplication::init() {
m_keyboardHandler.loadKeysFromFile("config/keys.lua");
gk::GamePad::init(m_keyboardHandler);

sf::ContextSettings settings;
settings.depthBits = 24;
settings.stencilBits = 8;
settings.antialiasingLevel = 0;
settings.majorVersion = 2;
settings.minorVersion = 1;

createWindow(sf::VideoMode{Config::screenWidth, Config::screenHeight}, APP_NAME, sf::Style::Titlebar | sf::Style::Close, settings);
createWindow(Config::screenWidth, Config::screenHeight, APP_NAME);
m_window.setVerticalSyncEnabled(Config::isVerticalSyncEnabled);
m_window.setOpenGLFlagsSetupFunc(&ClientApplication::initOpenGL);
m_window.disableView();
Expand All @@ -132,30 +125,23 @@ void ClientApplication::init() {
void ClientApplication::handleEvents() {
gk::CoreApplication::handleEvents();

if ((Config::isFullscreenModeEnabled && !m_window.isFullscreenModeEnabled())
|| (!Config::isFullscreenModeEnabled && m_window.isFullscreenModeEnabled())) {
m_window.setFullscreenMode(Config::isFullscreenModeEnabled);

// FIXME: Required for Windows
sf::Event event;
event.type = sf::Event::Resized;
event.size.width = m_window.window().getSize().x;
event.size.height = m_window.window().getSize().y;
onEvent(event);
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);
}

if (Config::screenWidth != m_window.getSize().x || Config::screenHeight != m_window.getSize().y) {
m_window.setSize({Config::screenWidth, Config::screenHeight});
m_window.resize(Config::screenWidth, Config::screenHeight);
}

if (Config::isVerticalSyncEnabled != m_window.isVerticalSyncEnabled())
m_window.setVerticalSyncEnabled(Config::isVerticalSyncEnabled);
}

void ClientApplication::onEvent(const sf::Event &event) {
void ClientApplication::onEvent(const SDL_Event &event) {
gk::CoreApplication::onEvent(event);

if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::F11)
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F11)
Config::isFullscreenModeEnabled ^= 1;
}

Expand Down
2 changes: 1 addition & 1 deletion source/client/core/ClientApplication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ClientApplication : public gk::CoreApplication {
private:
void handleEvents() override;

void onEvent(const sf::Event &event) override;
void onEvent(const SDL_Event &event) override;
void onExit() override;

static void initOpenGL();
Expand Down
35 changes: 20 additions & 15 deletions source/client/core/KeyboardHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@
#include "KeyboardHandler.hpp"

KeyboardHandler::KeyboardHandler() {
addKey(GameKey::Left, "Left", sf::Keyboard::A);
addKey(GameKey::Right, "Right", sf::Keyboard::D);
addKey(GameKey::Up, "Up", sf::Keyboard::W);
addKey(GameKey::Down, "Down", sf::Keyboard::S);
addKey(GameKey::Left, "Left", SDLK_a);
addKey(GameKey::Right, "Right", SDLK_d);
addKey(GameKey::Up, "Up", SDLK_w);
addKey(GameKey::Down, "Down", SDLK_s);

addKey(GameKey::Jump, "Jump", sf::Keyboard::Space);
addKey(GameKey::Fly, "Fly", sf::Keyboard::X);
addKey(GameKey::Sneak, "Sneak", sf::Keyboard::LShift);
addKey(GameKey::Sprint, "Sprint", sf::Keyboard::LControl);
addKey(GameKey::Jump, "Jump", SDLK_SPACE);
addKey(GameKey::Fly, "Fly", SDLK_x);
addKey(GameKey::Sneak, "Sneak", SDLK_LSHIFT);
addKey(GameKey::Sprint, "Sprint", SDLK_LCTRL);

addKey(GameKey::Chat, "Chat", sf::Keyboard::T);
addKey(GameKey::Command, "Command", sf::Keyboard::Divide);
addKey(GameKey::Chat, "Chat", SDLK_t);
addKey(GameKey::Command, "Command", SDLK_KP_DIVIDE);

addKey(GameKey::Shift, "Shift", sf::Keyboard::LShift);
addKey(GameKey::Shift, "Shift", SDLK_LSHIFT);
}

void KeyboardHandler::loadKeysFromFile(const std::string &filename) {
Expand All @@ -65,13 +65,13 @@ void KeyboardHandler::loadKeysFromFile(const std::string &filename) {
const std::string &keyName = it.second.as<sol::table>()["name"].get<std::string>();
const std::string &keyValue = it.second.as<sol::table>()["value"].get<std::string>();

sf::Keyboard::Key keycode = gk::KeyboardUtils::getKeyFromName(keyValue);
SDL_Keycode keycode = SDL_GetKeyFromName(keyValue.c_str());

auto keyit = m_keysID.find(keyID);
if (keyit != m_keysID.end()) {
gk::GameKey key = keyit->second;
m_keys[key].setKeycode(keycode);
if (m_keys[key].keycode() == sf::Keyboard::Unknown) {
if (m_keys[key].keycode() == SDLK_UNKNOWN) {
gkWarning() << "Key name '" + keyValue + "' not recognized";
}
}
Expand Down Expand Up @@ -102,10 +102,15 @@ void KeyboardHandler::saveKeysToFile(const std::string &filename) {
}

bool KeyboardHandler::isKeyPressed(gk::GameKey key) {
return sf::Keyboard::isKeyPressed(m_keys[key].keycode());
const u8 *keyboardState = SDL_GetKeyboardState(nullptr);
SDL_Keycode keyScancode = m_keys[key].keycode();

m_keysPressed[key] = keyboardState[SDL_GetScancodeFromKey(keyScancode)];

return m_keysPressed[key];
}

void KeyboardHandler::addKey(gk::GameKey id, const std::string &name, sf::Keyboard::Key defaultKey, const std::string &stringID, Key *key) {
void KeyboardHandler::addKey(gk::GameKey id, const std::string &name, SDL_Keycode defaultKey, const std::string &stringID, Key *key) {
auto keyit = m_keysID.find(stringID);
if (keyit == m_keysID.end()) {
auto it = m_keys.emplace(id, Key((u16)id, (stringID.empty() ? "_" + name : stringID), name));
Expand Down
9 changes: 4 additions & 5 deletions source/client/core/KeyboardHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include <string>

#include <gk/core/input/InputHandler.hpp>
#include <gk/core/input/KeyboardUtils.hpp>

#include "Key.hpp"

Expand All @@ -44,11 +43,11 @@ class KeyboardHandler : public gk::InputHandler {

bool isKeyPressed(gk::GameKey key) override;

sf::Keyboard::Key getKeycode(gk::GameKey key) { return m_keys[key].keycode(); }
std::string getKeyName(gk::GameKey key) { return gk::KeyboardUtils::getNameFromKey(m_keys[key].keycode()); }
void setKeycode(gk::GameKey key, sf::Keyboard::Key keycode) { m_keys[key].setKeycode(keycode); }
SDL_Keycode getKeycode(gk::GameKey key) { return m_keys[key].keycode(); }
std::string getKeyName(gk::GameKey key) { return SDL_GetKeyName(m_keys[key].keycode()); }
void setKeycode(gk::GameKey key, SDL_Keycode keycode) { m_keys[key].setKeycode(keycode); }

void addKey(gk::GameKey id, const std::string &name, sf::Keyboard::Key defaultKey, const std::string &stringID = "", Key *key = nullptr);
void addKey(gk::GameKey id, const std::string &name, SDL_Keycode defaultKey, const std::string &stringID = "", Key *key = nullptr);
u32 keyCount() { return m_keys.size(); }

const std::map<gk::GameKey, Key> &keys() const { return m_keys; }
Expand Down
2 changes: 1 addition & 1 deletion source/client/graphics/PlayerBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ static constexpr float modelCoords[NUM_QUADS * NUM_VERTICES_PER_QUAD][NUM_VERTEX

PlayerBox::PlayerBox(const gk::Camera &camera)
: m_camera(camera),
m_texture(gk::ResourceHandler::getInstance().get<sf::Texture>("texture-player"))
m_texture(gk::ResourceHandler::getInstance().get<gk::Texture>("texture-player"))
{
updateVertexBuffer();
}
Expand Down
7 changes: 3 additions & 4 deletions source/client/graphics/PlayerBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@
#ifndef PLAYERBOX_HPP_
#define PLAYERBOX_HPP_

#include <SFML/Graphics/Texture.hpp>

#include <gk/gl/Camera.hpp>
#include <gk/gl/Drawable.hpp>
#include <gk/gl/VertexBuffer.hpp>
#include <gk/gl/Texture.hpp>
#include <gk/gl/Transformable.hpp>
#include <gk/gl/VertexBuffer.hpp>

#include "Player.hpp"

Expand All @@ -53,7 +52,7 @@ class PlayerBox : public gk::Drawable, public gk::Transformable, public Player {

const gk::Camera &m_camera;

sf::Texture &m_texture;
gk::Texture &m_texture;
};

#endif // PLAYERBOX_HPP_
4 changes: 2 additions & 2 deletions source/client/graphics/TextureAtlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void TextureAtlas::packTextures() {

m_texture.loadFromImage(atlas);

sf::Texture::bind(&m_texture);
gk::Texture::bind(&m_texture);

glGenerateMipmap(GL_TEXTURE_2D);

Expand All @@ -92,7 +92,7 @@ void TextureAtlas::packTextures() {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

sf::Texture::bind(nullptr);
gk::Texture::bind(nullptr);
}

void TextureAtlas::loadFromRegistry(const std::string &texturePack) {
Expand Down
7 changes: 4 additions & 3 deletions source/client/graphics/TextureAtlas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@
#include <unordered_map>
#include <vector>

#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Image.hpp> // FIXME: Make a similar implementation in GameKit

#include <gk/core/IntTypes.hpp>
#include <gk/core/Rect.hpp>
#include <gk/gl/Texture.hpp>

class TextureAtlas {
public:
Expand All @@ -49,7 +50,7 @@ class TextureAtlas {
u16 getTextureID(const std::string &filename) const;
gk::FloatRect getTexCoords(const std::string &filename, bool normalized = true) const;

const sf::Texture &texture() const { return m_texture; }
const gk::Texture &texture() const { return m_texture; }

bool isReady() const { return m_isReady; }

Expand All @@ -62,7 +63,7 @@ class TextureAtlas {
std::vector<sf::Image> m_images;

// Packed texture
sf::Texture m_texture;
gk::Texture m_texture;

// Is the texture atlas ready to use?
bool m_isReady = false;
Expand Down
2 changes: 1 addition & 1 deletion source/client/gui/CraftingWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void CraftingWidget::init(unsigned int offset, unsigned int size) {
m_craftingResultInventoryWidget.setShiftDestination(m_shiftDestination);
}

void CraftingWidget::onEvent(const sf::Event &event) {
void CraftingWidget::onEvent(const SDL_Event &event) {
m_craftingInventoryWidget.onEvent(event);
m_craftingResultInventoryWidget.onEvent(event);

Expand Down
2 changes: 1 addition & 1 deletion source/client/gui/CraftingWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CraftingWidget : public AbstractInventoryWidget {

void init(unsigned int offset = 0, unsigned int size = 3);

void onEvent(const sf::Event &event) override;
void onEvent(const SDL_Event &event) override;

void update() override;

Expand Down
5 changes: 2 additions & 3 deletions source/client/gui/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@
*/
#include <fstream>

#include <SFML/Graphics/Texture.hpp>

#include <gk/gl/Texture.hpp>
#include <gk/resource/ResourceHandler.hpp>

#include "Font.hpp"

Font::Font(const std::string &textureName, const std::string &configPath)
: m_texture(gk::ResourceHandler::getInstance().get<sf::Texture>(textureName))
: m_texture(gk::ResourceHandler::getInstance().get<gk::Texture>(textureName))
{
std::memset(m_charWidth, 0, sizeof(u8) * 256);

Expand Down
6 changes: 3 additions & 3 deletions source/client/gui/Font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

#include <gk/core/Vector2.hpp>

namespace sf {
namespace gk {
class Texture;
}

Expand All @@ -44,12 +44,12 @@ class Font {
u8 getCharWidth(u8 c) const { return m_charWidth[c]; }
gk::Vector2i getTileSize() const { return {m_width, m_height}; }

const sf::Texture &texture() const { return m_texture; }
const gk::Texture &texture() const { return m_texture; }

private:
void parseConfig(const std::string &configPath);

sf::Texture &m_texture;
gk::Texture &m_texture;

u8 m_charWidth[256];
u8 m_width = 8; // FIXME: Hardcoded value
Expand Down
6 changes: 3 additions & 3 deletions source/client/gui/InventoryWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ void InventoryWidget::scroll(float scrolling) {
loadItemWidgets(offset, size);
}

void InventoryWidget::onEvent(const sf::Event &event) {
if (event.type == sf::Event::MouseMoved) {
void InventoryWidget::onEvent(const SDL_Event &event) {
if (event.type == SDL_MOUSEMOTION) {
m_currentItemWidget = nullptr;
for (std::size_t i = 0 ; i < m_itemWidgets.size() ; ++i) {
if (m_itemWidgets[i].isPointInWidget(event.mouseMove.x, event.mouseMove.y)) {
if (m_itemWidgets[i].isPointInWidget(event.motion.x, event.motion.y)) {
m_currentItemWidget = &m_itemWidgets[i];

m_selectedItemBackground.setPosition(1 + (i % m_inventoryWidth) * 18, 1 + (i / m_inventoryWidth) * 18, 0);
Expand Down
2 changes: 1 addition & 1 deletion source/client/gui/InventoryWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class InventoryWidget : public AbstractInventoryWidget {

void scroll(float scrolling);

void onEvent(const sf::Event &event) override;
void onEvent(const SDL_Event &event) override;

void update() override;

Expand Down
4 changes: 2 additions & 2 deletions source/client/gui/MenuWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ void MenuWidget::reset(u16 width, u16 height) {
Widget::m_height = 0;
}

void MenuWidget::onEvent(const sf::Event &event) {
void MenuWidget::onEvent(const SDL_Event &event) {
for (std::size_t i = 0 ; i < m_buttons.size() ; ++i) {
m_buttons.at(i).onEvent(event);

if (event.type == sf::Event::Resized) {
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
int x = i % m_width;
int y = i / m_width;

Expand Down
2 changes: 1 addition & 1 deletion source/client/gui/MenuWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class MenuWidget : public Widget {

void reset(u16 width, u16 height);

void onEvent(const sf::Event &event) override;
void onEvent(const SDL_Event &event) override;

void onGuiScaleChanged(const GuiScaleChangedEvent &event);

Expand Down
Loading

0 comments on commit c9b7990

Please sign in to comment.