aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAiden Woodruff <aiden.woodruff@gmail.com>2023-11-30 00:23:46 -0500
committerAiden Woodruff <aiden.woodruff@gmail.com>2023-11-30 00:23:46 -0500
commita055c44165285f5f63fd092e0cd5ea80f59cbc0f (patch)
tree1ab9cffb3aed43739e3750cca85760ca3abd1dbd
parent3e55710755603c0236191ee95fe6f54bc4124c8f (diff)
downloadnoise-a055c44165285f5f63fd092e0cd5ea80f59cbc0f.tar.gz
noise-a055c44165285f5f63fd092e0cd5ea80f59cbc0f.tar.bz2
noise-a055c44165285f5f63fd092e0cd5ea80f59cbc0f.zip
Build updates (vcpkg, warnings, setsmooth)
Add vcpkg toolchains if present Add cmake build types (debug or release) Require sfml >= 2 in cmake (match readme) Add a config check for sf::font.setSmooth() only added in sfml 2.6 Install font as a build step once we know where the exe is Use std::pow instead of std::powf Compiles on windows Signed-off-by: Aiden Woodruff <aiden.woodruff@gmail.com>
-rw-r--r--CMakeLists.txt32
-rw-r--r--cmake/vcpkg.cmake11
-rw-r--r--config.h.in1
-rw-r--r--noise.cc13
4 files changed, 51 insertions, 6 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b2facc6..778a4e9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,10 +1,38 @@
1cmake_minimum_required(VERSION 3.8) 1cmake_minimum_required(VERSION 3.8)
2 2
3include("cmake/vcpkg.cmake")
4
5set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build.")
6message("Build type: ${CMAKE_BUILD_TYPE}")
7set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release")
8set(CMAKE_CONFIGURATION_TYPES "Debug;Release")
9
3project(noise VERSION 0.0 LANGUAGES CXX) 10project(noise VERSION 0.0 LANGUAGES CXX)
4 11
5find_package(SFML COMPONENTS graphics REQUIRED) 12if (CMAKE_BUILD_TYPE STREQUAL "Debug" OR $<CONFIG:Debug>)
13 # Enable extra compiler warnings in debug builds.
14 if (MSVC)
15 add_compile_options("/W4" "/WX")
16 else()
17 add_compile_options("-Wall" "-Wextra" "-pedantic")
18 endif()
19endif()
20
21find_package(SFML 2 COMPONENTS graphics REQUIRED)
22
23if("${SFML_VERSION}" VERSION_GREATER_EQUAL 2.6)
24set(HAVE_FONT_SMOOTHING TRUE)
25endif()
26
27configure_file(config.h.in config.h @ONLY)
28include_directories("${CMAKE_CURRENT_BINARY_DIR}")
6 29
7add_executable(noise noise.cc brush.cc) 30add_executable(noise noise.cc brush.cc)
8target_link_libraries(noise sfml-graphics) 31target_link_libraries(noise sfml-graphics)
9target_compile_features(noise PRIVATE cxx_std_17) 32target_compile_features(noise PRIVATE cxx_std_17)
10configure_file(OpenSans-Regular.ttf OpenSans.ttf COPYONLY) 33
34add_custom_command(TARGET noise POST_BUILD COMMAND
35 cmake ARGS -E copy_if_different
36 "${CMAKE_CURRENT_SOURCE_DIR}/OpenSans-Regular.ttf"
37 "$<TARGET_FILE_DIR:noise>/OpenSans.ttf" COMMENT "Copying font."
38)
diff --git a/cmake/vcpkg.cmake b/cmake/vcpkg.cmake
new file mode 100644
index 0000000..58c2c05
--- /dev/null
+++ b/cmake/vcpkg.cmake
@@ -0,0 +1,11 @@
1# optional vcpkg support
2if(NOT DEFINED CMAKE_TOOLCHAIN_FILE)
3 if(DEFINED ENV{VCPKG_ROOT})
4 set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
5 CACHE STRING "")
6 elseif(DEFINED ENV{VCPKG_INSTALLATION_ROOT})
7 set(CMAKE_TOOLCHAIN_FILE
8 "$ENV{VCPKG_INSTALLATION_ROOT}/scripts/buildsystems/vcpkg.cmake"
9 CACHE STRING "")
10 endif()
11endif()
diff --git a/config.h.in b/config.h.in
new file mode 100644
index 0000000..46f9ecd
--- /dev/null
+++ b/config.h.in
@@ -0,0 +1 @@
#cmakedefine HAVE_FONT_SMOOTHING
diff --git a/noise.cc b/noise.cc
index b5651e8..065e854 100644
--- a/noise.cc
+++ b/noise.cc
@@ -1,8 +1,11 @@
1#include "config.h"
2
1#include <filesystem> 3#include <filesystem>
2#include <iostream> 4#include <iostream>
3#include <random> 5#include <random>
4#include <stdexcept> 6#include <stdexcept>
5#include <vector> 7#include <vector>
8#include <cmath>
6 9
7#include <SFML/Graphics.hpp> 10#include <SFML/Graphics.hpp>
8 11
@@ -50,9 +53,9 @@ sf::Color random_color() {
50 53
51// Distort a black-white color. assume color_mode == 3 or 4. 54// Distort a black-white color. assume color_mode == 3 or 4.
52sf::Color distort(const sf::Color& c, float p = 2) { 55sf::Color distort(const sf::Color& c, float p = 2) {
53 return sf::Color(255 * std::powf(c.r / 255.0, p), 56 return sf::Color(255 * std::pow(c.r / 255.0, p),
54 255 * std::powf(c.g / 255.0, p), 57 255 * std::pow(c.g / 255.0, p),
55 255 * std::powf(c.b / 255.0, p), 255); 58 255 * std::pow(c.b / 255.0, p), 255);
56} 59}
57 60
58bool in_range(const sf::Vector2u& size, const sf::Vector2u& v) { 61bool in_range(const sf::Vector2u& size, const sf::Vector2u& v) {
@@ -146,11 +149,13 @@ int main(int argc, char* argv[]) {
146 sf::RenderWindow window(sf::VideoMode(640, 400), "Noise"); 149 sf::RenderWindow window(sf::VideoMode(640, 400), "Noise");
147 150
148 sf::Font font; 151 sf::Font font;
149 if (!font.loadFromFile(fs::path(argv[0]).parent_path() / "OpenSans.ttf")) { 152 if (!font.loadFromFile((fs::path(argv[0]).parent_path() / "OpenSans.ttf").string())) {
150 std::cerr << "FATAL: Failed to load font OpenSans.ttf." << std::endl; 153 std::cerr << "FATAL: Failed to load font OpenSans.ttf." << std::endl;
151 return -1; 154 return -1;
152 } 155 }
156#ifdef HAVE_FONT_SMOOTHING
153 font.setSmooth(true); 157 font.setSmooth(true);
158#endif
154 159
155 sf::Text counter("Iteration 0 Blur 0 Brightness 0", font); 160 sf::Text counter("Iteration 0 Blur 0 Brightness 0", font);
156 counter.setPosition(5, 5); 161 counter.setPosition(5, 5);