Browse Source

integrate imgui, and associated build system work

opengl-renderer-broken
Martin Dørum 4 years ago
parent
commit
e1fef8792e

+ 6
- 0
.gitmodules View File

@@ -0,0 +1,6 @@
[submodule "third_party/imgui"]
path = third_party/imgui
url = https://github.com/ocornut/imgui.git
[submodule "third_party/PerlinNoise"]
path = third_party/PerlinNoise
url = https://github.com/Reputeless/PerlinNoise.git

+ 13
- 6
CMakeLists.txt View File

@@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.0)
project(swan)

find_package(SFML 2.5 COMPONENTS graphics system window REQUIRED)
find_package(ImGui-SFML REQUIRED)

set(CMAKE_CXX_CLANG_TIDY
clang-tidy
@@ -9,10 +10,13 @@ set(CMAKE_CXX_CLANG_TIDY
--checks=-*,bugprone-*,cert-*,performance-*,clang-analyzer-*,-cert-dcl16-c,-cert-err58-cpp,-clang-analyzer-optin.cplusplus.VirtualCall)

add_compile_options(-std=c++2a -Wall -Wextra -Wpedantic -Wno-unused-parameter)
if(CMAKE_BUILD_TYPE STREQUAL Debug OR CMAKE_BUILD_TYPE STREQUAL "")
message(STATUS "Build mode: Debug")
if(CMAKE_BUILD_TYPE STREQUAL Sanitize OR CMAKE_BUILD_TYPE STREQUAL "")
message(STATUS "Build mode: Sanitize")
add_compile_options(-g -fsanitize=address)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
elseif(CMAKE_BUILD_TYPE STREQUAL Debug)
message(STATUS "Build mode: Debug")
add_compile_options(-g)
elseif(CMAKE_BUILD_TYPE STREQUAL Release)
message(STATUS "Build mode: Release")
add_compile_options(-O3 -flto)
@@ -21,7 +25,9 @@ else()
message(FATAL_ERROR "CMAKE_BUILD_TYPE must be Debug or Release.")
endif()

set(LIBRARIES sfml-graphics sfml-system sfml-window sfml-audio dl)
add_subdirectory(third_party)

set(libraries sfml-graphics sfml-system sfml-window sfml-audio ImGui-SFML::ImGui-SFML imgui dl)

# We want to be able to use C++20 designated initializers,
# but Clang doesn't support them yet.
@@ -31,13 +37,14 @@ add_compile_options(-Wno-c99-extensions)
include_directories(
${PROJECT_SOURCE_DIR}/libswan/include
${PROJECT_SOURCE_DIR}/third_party)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib;${CMAKE_INSTALL_PREFIX}/swan/libswan")
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib;${CMAKE_INSTALL_PREFIX}/swan/libswan;${CMAKE_INSTALL_PREFIX}/swan/third_party")

add_subdirectory(libswan)
add_subdirectory(core.mod)

add_executable(swan src/main.cc)
target_link_libraries(swan libswan ${LIBRARIES})
add_executable(swan
src/main.cc)
target_link_libraries(swan libswan ${libraries} imgui)

set(assets
assets/icon.png

+ 1
- 1
core.mod/CMakeLists.txt View File

@@ -4,7 +4,7 @@ add_library(core.mod SHARED
src/entities/EntPlayer.cc
src/entities/EntItemStack.cc)
set_target_properties(core.mod PROPERTIES OUTPUT_NAME mod PREFIX "")
target_link_libraries(core.mod libswan ${LIBRARIES})
target_link_libraries(core.mod libswan ${libraries})

set(assets
assets/entities/player-still.png

+ 20
- 28
dist/CMakeLists.txt View File

@@ -2,29 +2,11 @@ cmake_minimum_required(VERSION 3.0)
project(swan-dist)
include(ExternalProject)

ExternalProject_Add(compiler
GIT_REPOSITORY https://github.com/llvm/llvm-project.git
GIT_TAG llvmorg-9.0.0
GIT_SHALLOW true
GIT_PROGRESS true
SOURCE_SUBDIR llvm
LIST_SEPARATOR ::
CMAKE_ARGS
-DCMAKE_BUILD_TYPE=Release
-DLLVM_BUILD_LLVM_DYLIB=ON
-DLLVM_LINK_LLVM_DYLIB=ON
-DCLANG_LINK_CLANG_DYLIB=ON
-DLLVM_ENABLE_RTTI=ON
-DLLVM_ENABLE_PROJECTS=clang::libcxx::libcxxabi::compiler-rt::lld
-DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR}/prefix)

set(PREFIX_DIR ${PROJECT_BINARY_DIR}/prefix)
set(prefix ${PROJECT_BINARY_DIR}/prefix)

set(toolchain
-DCMAKE_CXX_COMPILER=${PREFIX_DIR}/bin/clang++
-DCMAKE_C_COMPILER=${PREFIX_DIR}/bin/clang
-DCMAKE_LINKER=${PREFIX_DIR}/bin/lld
-DCMAKE_PREFIX_PATH=${PREFIX})
set(common
-DCMAKE_INSTALL_PREFIX=${prefix}
-DCMAKE_PREFIX_PATH=${prefix})

ExternalProject_Add(sfml
GIT_REPOSITORY https://github.com/SFML/SFML.git
@@ -33,13 +15,23 @@ ExternalProject_Add(sfml
GIT_PROGRESS true
CMAKE_ARGS
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR}/prefix
${toolchain}
DEPENDS compiler)
${common})

ExternalProject_Add(imgui-sfml
GIT_REPOSITORY https://github.com/eliasdaler/imgui-sfml.git
GIT_TAG v2.1
GIT_SHALLOW true
GIT_PROGRESS true
CMAKE_ARGS
-DCMAKE_BUILD_TYPE=Release
-DIMGUI_DIR=${PROJECT_SOURCE_DIR}/../third_party/imgui
-DBUILD_SHARED_LIBS=true
${common}
DEPENDS sfml)

ExternalProject_Add(swan
SOURCE_DIR ${PROJECT_SOURCE_DIR}/..
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR}/prefix
${toolchain}
DEPENDS compiler sfml)
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
${common}
DEPENDS sfml imgui-sfml)

+ 1
- 1
libswan/CMakeLists.txt View File

@@ -13,6 +13,6 @@ add_library(libswan SHARED
src/WorldPlane.cc)
target_include_directories(libswan PUBLIC "include/swan")
set_target_properties(libswan PROPERTIES OUTPUT_NAME swan)
target_link_libraries(libswan ${LIBRARIES})
target_link_libraries(libswan ${libraries})

install(TARGETS libswan DESTINATION swan/libswan)

+ 15
- 0
src/main.cc View File

@@ -12,6 +12,9 @@
#include <SFML/System/Clock.hpp>
#include <SFML/Audio.hpp>

#include <imgui/imgui.h>
#include <imgui-SFML.h>

using namespace Swan;

int main() {
@@ -27,6 +30,9 @@ int main() {
window.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr());
Win win(&window);

// Initialize ImGui
ImGui::SFML::Init(window);

// Create music
sf::SoundBuffer musicbuf;
sf::Sound music;
@@ -54,6 +60,8 @@ int main() {
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
ImGui::SFML::ProcessEvent(event);

switch (event.type) {
case sf::Event::Closed:
window.close();
@@ -120,8 +128,15 @@ int main() {
}
}

ImGui::SFML::Update(window, sf::seconds(dt));
ImGui::Begin("Test Window");
ImGui::Button("No.");
ImGui::End();
ImGui::ShowTestWindow();

window.clear();
game.draw();
ImGui::SFML::Render(window);
window.display();
}


+ 8
- 0
third_party/CMakeLists.txt View File

@@ -0,0 +1,8 @@
add_library(imgui SHARED
${PROJECT_SOURCE_DIR}/third_party/imgui/imgui_demo.cpp
${PROJECT_SOURCE_DIR}/third_party/imgui/imgui_draw.cpp
${PROJECT_SOURCE_DIR}/third_party/imgui/imgui_widgets.cpp
${PROJECT_SOURCE_DIR}/third_party/imgui/imgui.cpp)
set_target_properties(imgui PROPERTIES CXX_CLANG_TIDY "")

install(TARGETS imgui DESTINATION swan/third_party)

+ 1
- 0
third_party/PerlinNoise

@@ -0,0 +1 @@
Subproject commit cc91d1c55b952869090e4c15e388875e2aa88f35

+ 0
- 219
third_party/PerlinNoise/PerlinNoise.hpp View File

@@ -1,219 +0,0 @@
//----------------------------------------------------------------------------------------
//
// siv::PerlinNoise
// Perlin noise library for modern C++
//
// Copyright (C) 2013-2018 Ryo Suzuki <reputeless@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//----------------------------------------------------------------------------------------

# pragma once
# include <cstdint>
# include <numeric>
# include <algorithm>
# include <random>

namespace siv
{
class PerlinNoise
{
private:

std::uint8_t p[512];

static double Fade(double t) noexcept
{
return t * t * t * (t * (t * 6 - 15) + 10);
}

static double Lerp(double t, double a, double b) noexcept
{
return a + t * (b - a);
}

static double Grad(std::uint8_t hash, double x, double y, double z) noexcept
{
const std::uint8_t h = hash & 15;
const double u = h < 8 ? x : y;
const double v = h < 4 ? y : h == 12 || h == 14 ? x : z;
return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
}

public:

explicit PerlinNoise(std::uint32_t seed = std::default_random_engine::default_seed)
{
reseed(seed);
}

template <class URNG>
explicit PerlinNoise(URNG& urng)
{
reseed(urng);
}

void reseed(std::uint32_t seed)
{
for (size_t i = 0; i < 256; ++i)
{
p[i] = static_cast<std::uint8_t>(i);
}

std::shuffle(std::begin(p), std::begin(p) + 256, std::default_random_engine(seed));

for (size_t i = 0; i < 256; ++i)
{
p[256 + i] = p[i];
}
}

template <class URNG>
void reseed(URNG& urng)
{
for (size_t i = 0; i < 256; ++i)
{
p[i] = static_cast<std::uint8_t>(i);
}

std::shuffle(std::begin(p), std::begin(p) + 256, urng);

for (size_t i = 0; i < 256; ++i)
{
p[256 + i] = p[i];
}
}

double noise(double x) const
{
return noise(x, 0.0, 0.0);
}

double noise(double x, double y) const
{
return noise(x, y, 0.0);
}

double noise(double x, double y, double z) const
{
const std::int32_t X = static_cast<std::int32_t>(std::floor(x)) & 255;
const std::int32_t Y = static_cast<std::int32_t>(std::floor(y)) & 255;
const std::int32_t Z = static_cast<std::int32_t>(std::floor(z)) & 255;

x -= std::floor(x);
y -= std::floor(y);
z -= std::floor(z);

const double u = Fade(x);
const double v = Fade(y);
const double w = Fade(z);

const std::int32_t A = p[X] + Y, AA = p[A] + Z, AB = p[A + 1] + Z;
const std::int32_t B = p[X + 1] + Y, BA = p[B] + Z, BB = p[B + 1] + Z;

return Lerp(w, Lerp(v, Lerp(u, Grad(p[AA], x, y, z),
Grad(p[BA], x - 1, y, z)),
Lerp(u, Grad(p[AB], x, y - 1, z),
Grad(p[BB], x - 1, y - 1, z))),
Lerp(v, Lerp(u, Grad(p[AA + 1], x, y, z - 1),
Grad(p[BA + 1], x - 1, y, z - 1)),
Lerp(u, Grad(p[AB + 1], x, y - 1, z - 1),
Grad(p[BB + 1], x - 1, y - 1, z - 1))));
}

double octaveNoise(double x, std::int32_t octaves) const
{
double result = 0.0;
double amp = 1.0;

for (std::int32_t i = 0; i < octaves; ++i)
{
result += noise(x) * amp;
x *= 2.0;
amp *= 0.5;
}

return result;
}

double octaveNoise(double x, double y, std::int32_t octaves) const
{
double result = 0.0;
double amp = 1.0;

for (std::int32_t i = 0; i < octaves; ++i)
{
result += noise(x, y) * amp;
x *= 2.0;
y *= 2.0;
amp *= 0.5;
}

return result;
}

double octaveNoise(double x, double y, double z, std::int32_t octaves) const
{
double result = 0.0;
double amp = 1.0;

for (std::int32_t i = 0; i < octaves; ++i)
{
result += noise(x, y, z) * amp;
x *= 2.0;
y *= 2.0;
z *= 2.0;
amp *= 0.5;
}

return result;
}

double noise0_1(double x) const
{
return noise(x) * 0.5 + 0.5;
}

double noise0_1(double x, double y) const
{
return noise(x, y) * 0.5 + 0.5;
}

double noise0_1(double x, double y, double z) const
{
return noise(x, y, z) * 0.5 + 0.5;
}

double octaveNoise0_1(double x, std::int32_t octaves) const
{
return octaveNoise(x, octaves) * 0.5 + 0.5;
}

double octaveNoise0_1(double x, double y, std::int32_t octaves) const
{
return octaveNoise(x, y, octaves) * 0.5 + 0.5;
}

double octaveNoise0_1(double x, double y, double z, std::int32_t octaves) const
{
return octaveNoise(x, y, z, octaves) * 0.5 + 0.5;
}
};
}

+ 1
- 0
third_party/imgui

@@ -0,0 +1 @@
Subproject commit 688cf868ea83db6c2958dd6bf7a20d471d00940b

Loading…
Cancel
Save