Skip to content

Commit

Permalink
First step of SFML migration.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed May 14, 2020
1 parent dc1b714 commit 333222e
Show file tree
Hide file tree
Showing 60 changed files with 243 additions and 306 deletions.
4 changes: 0 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
path = external/entt
url = git://github.com/skypjack/entt.git
ignore = dirty
[submodule "external/SFML"]
path = external/SFML
url = git://github.com/SFML/SFML.git
ignore = dirty
[submodule "external/gamekit"]
path = external/gamekit
url = git://github.com/Unarelith/GameKit.git
Expand Down
55 changes: 1 addition & 54 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ project(openminer)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" "${CMAKE_CURRENT_SOURCE_DIR}/cmake/sdl2")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

if (WIN32)
set(WIN_LIBRARIES_PATH "C:/Libraries")
Expand Down Expand Up @@ -98,47 +98,6 @@ endif(NOT GLM_FOUND)

include_directories(${GLM_INCLUDE_DIRS})

#------------------------------------------------------------------------------
# - SDL2, SDL2_image, SDL2_mixer
#------------------------------------------------------------------------------
if (MSVC)
set(SDL2_PATH ${WIN_LIBRARIES_PATH}/SDL2 CACHE BOOL "" FORCE)
set(SDL2_IMAGE_PATH ${WIN_LIBRARIES_PATH}/SDL2_image CACHE BOOL "" FORCE)
set(SDL2_MIXER_PATH ${WIN_LIBRARIES_PATH}/SDL2_mixer CACHE BOOL "" FORCE)
set(SDL2_TTF_PATH ${WIN_LIBRARIES_PATH}/SDL2_ttf CACHE BOOL "" FORCE)
elseif (WIN32)
set(SDL2_PATH ${WIN_LIBRARIES_PATH}/SDL2/i686-w64-mingw32 CACHE BOOL "" FORCE)
set(SDL2_IMAGE_PATH ${WIN_LIBRARIES_PATH}/SDL2_image/i686-w64-mingw32 CACHE BOOL "" FORCE)
set(SDL2_MIXER_PATH ${WIN_LIBRARIES_PATH}/SDL2_mixer/i686-w64-mingw32 CACHE BOOL "" FORCE)
set(SDL2_TTF_PATH ${WIN_LIBRARIES_PATH}/SDL2_ttf/i686-w64-mingw32 CACHE BOOL "" FORCE)
endif ()

find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
find_package(SDL2_mixer REQUIRED)
find_package(SDL2_ttf REQUIRED)

if(NOT SDL2_FOUND)
message(FATAL_ERROR "SDL2 not found!")
endif(NOT SDL2_FOUND)

if(NOT SDL2_IMAGE_FOUND)
message(FATAL_ERROR "SDL2_image not found!")
endif(NOT SDL2_IMAGE_FOUND)

if(NOT SDL2_MIXER_FOUND)
message(FATAL_ERROR "SDL2_mixer not found!")
endif(NOT SDL2_MIXER_FOUND)

if(NOT SDL2_TTF_FOUND)
message(FATAL_ERROR "SDL2_ttf not found!")
endif(NOT SDL2_TTF_FOUND)

include_directories(${SDL2_INCLUDE_DIRS}
${SDL2_IMAGE_INCLUDE_DIRS}
${SDL2_MIXER_INCLUDE_DIRS}
${SDL2_TTF_INCLUDE_DIRS})

#------------------------------------------------------------------------------
# - GLEW
#------------------------------------------------------------------------------
Expand Down Expand Up @@ -189,18 +148,6 @@ include_directories(external/entt/single_include)
add_subdirectory(external/gamekit)
include_directories(external/gamekit/include)

#------------------------------------------------------------------------------
# - SFML network
#------------------------------------------------------------------------------
set(SFML_BUILD_AUDIO FALSE)
set(SFML_BUILD_GRAPHICS FALSE)
set(SFML_BUILD_WINDOW FALSE)
set(SFML_STATIC TRUE)

set(BUILD_SHARED_LIBS OFF)

add_subdirectory(external/SFML)

#------------------------------------------------------------------------------
# - zlib
#------------------------------------------------------------------------------
Expand Down
1 change: 0 additions & 1 deletion external/SFML
Submodule SFML deleted from 50e173
2 changes: 1 addition & 1 deletion external/gamekit
Submodule gamekit updated 66 files
+1 −0 .gitignore
+3 −0 .gitmodules
+33 −43 CMakeLists.txt
+6 −8 README.md
+0 −14 TODO
+1 −1 cmake/FindTinyXml2.cmake
+0 −388 cmake/sdl2/FindSDL2.cmake
+0 −222 cmake/sdl2/FindSDL2_gfx.cmake
+0 −222 cmake/sdl2/FindSDL2_image.cmake
+0 −220 cmake/sdl2/FindSDL2_mixer.cmake
+0 −222 cmake/sdl2/FindSDL2_net.cmake
+0 −222 cmake/sdl2/FindSDL2_ttf.cmake
+0 −1 examples/CMakeLists.txt
+0 −127 examples/sample/CMakeLists.txt
+0 −16 examples/sample/cmake/FindGameKit.cmake
+0 −45 examples/sample/include/Application.hpp
+0 −45 examples/sample/include/GameState.hpp
+0 −21 examples/sample/shaders/game.f.glsl
+0 −24 examples/sample/shaders/game.v.glsl
+0 −48 examples/sample/source/Application.cpp
+0 −33 examples/sample/source/main.cpp
+1 −0 external/SFML
+0 −40 include/gk/audio.hpp
+0 −57 include/gk/audio/AudioPlayer.hpp
+0 −58 include/gk/audio/Music.hpp
+0 −119 include/gk/audio/Sound.hpp
+5 −4 include/gk/core/ApplicationState.hpp
+3 −7 include/gk/core/CoreApplication.hpp
+1 −2 include/gk/core/Debug.hpp
+5 −2 include/gk/core/GameClock.hpp
+1 −9 include/gk/core/Logger.hpp
+22 −12 include/gk/core/LoggerUtils.hpp
+0 −42 include/gk/core/SDLHeaders.hpp
+0 −53 include/gk/core/SDLLoader.hpp
+4 −40 include/gk/core/Window.hpp
+5 −6 include/gk/core/input/KeyboardHandler.hpp
+11 −9 include/gk/core/input/KeyboardUtils.hpp
+5 −2 include/gk/gl/RenderStates.hpp
+0 −157 include/gk/gl/Texture.hpp
+0 −144 include/gk/graphics/Font.hpp
+5 −3 include/gk/graphics/Image.hpp
+2 −2 include/gk/graphics/RectangleShape.hpp
+0 −97 include/gk/graphics/Text.hpp
+0 −70 include/gk/graphics/TextInput.hpp
+4 −2 include/gk/graphics/Tileset.hpp
+1 −9 source/CMakeLists.txt
+0 −68 source/audio/AudioPlayer.cpp
+0 −49 source/audio/Music.cpp
+0 −64 source/audio/Sound.cpp
+7 −13 source/core/CoreApplication.cpp
+7 −5 source/core/GameClock.cpp
+0 −4 source/core/Logger.cpp
+6 −2 source/core/LoggerUtils.cpp
+14 −9 source/core/Mouse.cpp
+0 −73 source/core/SDLLoader.cpp
+9 −75 source/core/Window.cpp
+4 −11 source/core/input/KeyboardHandler.cpp
+248 −0 source/core/input/KeyboardUtils.cpp
+3 −2 source/gl/RenderTarget.cpp
+0 −115 source/gl/Texture.cpp
+0 −64 source/graphics/Font.cpp
+5 −6 source/graphics/Image.cpp
+0 −127 source/graphics/Text.cpp
+0 −83 source/graphics/TextInput.cpp
+1 −1 source/graphics/Tileset.cpp
+3 −2 source/resource/TextureLoader.cpp
7 changes: 3 additions & 4 deletions source/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,14 @@ target_link_libraries(${PROJECT_NAME}
${CMAKE_PROJECT_NAME}_common
gamekit
${OPENGL_LIBRARIES}
${SDL2_LIBRARIES}
${SDL2_IMAGE_LIBRARIES}
${SDL2_MIXER_LIBRARIES}
${SDL2_TTF_LIBRARIES}
${TINYXML2_LIBRARIES}
${GLEW_LIBRARIES}
${LUA_LIBRARIES}
sfml-system
sfml-window
sfml-network
sfml-graphics
sfml-audio
zlib
${UNIX_LIBS})

27 changes: 14 additions & 13 deletions source/client/core/ClientApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,22 @@ void ClientApplication::init() {
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);
}

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

if (Config::isVerticalSyncEnabled != m_window.isVerticalSyncEnabled())
m_window.setVerticalSyncEnabled(Config::isVerticalSyncEnabled);
// FIXME: SFML
// 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.resize(Config::screenWidth, Config::screenHeight);
// }
//
// if (Config::isVerticalSyncEnabled != m_window.isVerticalSyncEnabled())
// m_window.setVerticalSyncEnabled(Config::isVerticalSyncEnabled);
}

void ClientApplication::onEvent(const SDL_Event &event) {
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F11)
void ClientApplication::onEvent(const sf::Event &event) {
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::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 @@ -41,7 +41,7 @@ class ClientApplication : public gk::CoreApplication {
private:
void handleEvents() override;

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

void initOpenGL();

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<gk::Texture>("texture-player"))
m_texture(gk::ResourceHandler::getInstance().get<sf::Texture>("texture-player"))
{
updateVertexBuffer();
}
Expand Down
5 changes: 3 additions & 2 deletions source/client/graphics/PlayerBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@
#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/Transformable.hpp>
#include <gk/gl/Texture.hpp>

#include "Player.hpp"

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

const gk::Camera &m_camera;

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

#endif // PLAYERBOX_HPP_
124 changes: 64 additions & 60 deletions source/client/graphics/TextureAtlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/
#include <gk/core/Exception.hpp>
#include <gk/core/Filesystem.hpp>
#include <gk/gl/OpenGL.hpp>

#include "Registry.hpp"
#include "TextureAtlas.hpp"
Expand All @@ -37,75 +38,78 @@ void TextureAtlas::addFile(const std::string &path, const std::string &filename)
if (it != m_textureMap.end())
return;

SurfacePtr surface{IMG_Load((path + filename).c_str()), &SDL_FreeSurface};
if(!surface) {
gkWarning() << "Failed to load texture:" << path + filename;
return;
}

if (!m_tileSize)
m_tileSize = surface->w;

if (m_tileSize != surface->w || m_tileSize != surface->h)
throw EXCEPTION("Texture size unexpected for", path + filename + ". Got", surface->w, surface->h, "instead of", m_tileSize, m_tileSize);

m_textureMap.emplace(filename, m_textures.size());
m_textures.emplace_back(std::move(surface));
// FIXME: SFML
// SurfacePtr surface{IMG_Load((path + filename).c_str()), &SDL_FreeSurface};
// if(!surface) {
// gkWarning() << "Failed to load texture:" << path + filename;
// return;
// }
//
// if (!m_tileSize)
// m_tileSize = surface->w;
//
// if (m_tileSize != surface->w || m_tileSize != surface->h)
// throw EXCEPTION("Texture size unexpected for", path + filename + ". Got", surface->w, surface->h, "instead of", m_tileSize, m_tileSize);
//
// m_textureMap.emplace(filename, m_textures.size());
// m_textures.emplace_back(std::move(surface));
}

void TextureAtlas::packTextures() {
if (!m_tileSize)
throw EXCEPTION("Cannot pack zero-sized textures!");

SurfacePtr atlas{nullptr, &SDL_FreeSurface};

// Max amount of textures on one line
const u16 atlasWidth = 16;

// Max amount of textures on one column
const u16 atlasHeight = std::ceil((float)m_textures.size() / atlasWidth);

Uint32 rmask, gmask, bmask, amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif

atlas.reset(SDL_CreateRGBSurface(0, atlasWidth * m_tileSize, atlasHeight * m_tileSize, 32, rmask, gmask, bmask, amask));
if (!atlas) {
throw EXCEPTION("Failed to create surface:", SDL_GetError());
}

u16 i = 0;
for (auto &it : m_textures) {
SDL_Rect outRect;
outRect.x = (i % atlasWidth) * m_tileSize;
outRect.y = (i / atlasWidth) * m_tileSize;
outRect.w = m_tileSize;
outRect.h = m_tileSize;

SDL_BlitSurface(it.get(), nullptr, atlas.get(), &outRect);

++i;
}

m_textures.clear();
// FIXME: SFML
// SurfacePtr atlas{nullptr, &SDL_FreeSurface};
//
// // Max amount of textures on one line
// const u16 atlasWidth = 16;
//
// // Max amount of textures on one column
// const u16 atlasHeight = std::ceil((float)m_textures.size() / atlasWidth);
//
// Uint32 rmask, gmask, bmask, amask;
// #if SDL_BYTEORDER == SDL_BIG_ENDIAN
// rmask = 0xff000000;
// gmask = 0x00ff0000;
// bmask = 0x0000ff00;
// amask = 0x000000ff;
// #else
// rmask = 0x000000ff;
// gmask = 0x0000ff00;
// bmask = 0x00ff0000;
// amask = 0xff000000;
// #endif
//
// atlas.reset(SDL_CreateRGBSurface(0, atlasWidth * m_tileSize, atlasHeight * m_tileSize, 32, rmask, gmask, bmask, amask));
// if (!atlas) {
// throw EXCEPTION("Failed to create surface:", SDL_GetError());
// }
//
// u16 i = 0;
// for (auto &it : m_textures) {
// SDL_Rect outRect;
// outRect.x = (i % atlasWidth) * m_tileSize;
// outRect.y = (i / atlasWidth) * m_tileSize;
// outRect.w = m_tileSize;
// outRect.h = m_tileSize;
//
// SDL_BlitSurface(it.get(), nullptr, atlas.get(), &outRect);
//
// ++i;
// }
//
// m_textures.clear();

m_isReady = true;

if (IMG_SavePNG(atlas.get(), "test_atlas.png") < 0)
throw EXCEPTION("Failed to save texture to: test_atlas.png. Reason:", IMG_GetError());

m_texture.loadFromSurface(atlas.get());
// FIXME: SFML
// if (IMG_SavePNG(atlas.get(), "test_atlas.png") < 0)
// throw EXCEPTION("Failed to save texture to: test_atlas.png. Reason:", IMG_GetError());
//
// m_texture.loadFromSurface(atlas.get());

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

glGenerateMipmap(GL_TEXTURE_2D);

Expand All @@ -115,7 +119,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);

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

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

#include <SFML/Graphics/Texture.hpp>

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

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

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

bool isReady() const { return m_isReady; }

Expand All @@ -60,12 +60,8 @@ class TextureAtlas {
// Mapping between filename and internal texture ID
std::unordered_map<std::string, u16> m_textureMap;

// Textures to pack together
using SurfacePtr = std::unique_ptr<SDL_Surface, decltype(&SDL_FreeSurface)>;
std::vector<SurfacePtr> m_textures;

// Packed texture
gk::Texture m_texture;
sf::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 SDL_Event &event) {
void CraftingWidget::onEvent(const sf::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 SDL_Event &event) override;
void onEvent(const sf::Event &event) override;

void update() override;

Expand Down
Loading

0 comments on commit 333222e

Please sign in to comment.