From 0933f9bdf56f17199e7a51f1cdc0791b44446f56 Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Mon, 24 May 2021 16:20:54 -0400 Subject: [PATCH] Update cpp/sdl-cmake example + Clean CMakeLists of unused options + Reorganize the linking of SDL to custom lib-sdl-test library + Improve Shape and Rectangle to better utilize inheritance --- cpp/sdl-cmake/CMakeLists.txt | 93 +++++++++++++----------------- cpp/sdl-cmake/apps/sdl-test.cpp | 26 +++++---- cpp/sdl-cmake/src/lib-sdl-test.cpp | 40 +++---------- cpp/sdl-cmake/src/lib-sdl-test.h | 39 ++++++++----- 4 files changed, 89 insertions(+), 109 deletions(-) diff --git a/cpp/sdl-cmake/CMakeLists.txt b/cpp/sdl-cmake/CMakeLists.txt index 12f0d48..b0df5e3 100644 --- a/cpp/sdl-cmake/CMakeLists.txt +++ b/cpp/sdl-cmake/CMakeLists.txt @@ -1,70 +1,59 @@ ############################################################################### ## Author: Shaun Reed ## -## Legal: All Content (c) 2019 Shaun Reed, all rights reserved ## +## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## ## ## ## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## ############################################################################## -# Root CMakeLists.txt of cpp practice 4-inheritance +# # Define CMake version cmake_minimum_required(VERSION 3.15) -project( # Define project - inheritance # Project name - DESCRIPTION "Example project for class inheritance" - LANGUAGES CXX - ) - -# Pass this to program to control debug output -option (DB_CONF "Should we debug and configure files with cmake values?" ON) -option (DB_BUILD "Should we run the build in debug mode?" OFF) -option (EXE_BUILD "Should we build the executable?" ON) - -add_library( # Add Library - lib-sdl-test # Library Name - "src/lib-sdl-test.cpp" # Sources.. - "src/lib-sdl-test.h" +project( + #[[NAME]] SDL-Cmake + DESCRIPTION "Example project for building SDL projects with CMake" + LANGUAGES CXX ) -target_include_directories( # When calling library, include a directory - lib-sdl-test # Library name - PUBLIC # - "${CMAKE_CURRENT_SOURCE_DIR}" # Source directory of exe including our library +# Add Library +add_library( + lib-sdl-test # Library Name + "src/lib-sdl-test.cpp" # Sources.. + "src/lib-sdl-test.h" ) -if (EXE_BUILD) - set(BUILD_STATUS "Building default executable") - include(FindPkgConfig) - pkg_search_module(SDL2 REQUIRED sdl2) - include_directories(${SDL2_INCLUDE_DIRS}) +target_include_directories( # When calling library, include a directory + lib-sdl-test # Library name + PUBLIC # Visibility + "${CMAKE_CURRENT_SOURCE_DIR}/src" # Source directory for library + ) - add_executable( # Creating executable - sdl-test # Exe name - "apps/sdl-test.cpp" # Exe Source(s) +# Search for SDL2 package +find_package(SDL2 REQUIRED sdl2) + +# If SDL2 was found successfully, link to lib-sdl-test +if (SDL2_FOUND) + # Any target that links with this library will also link to SDL2 + # + Because we choose PUBLIC visibility + target_include_directories(lib-sdl-test PUBLIC ${SDL2_INCLUDE_DIRS}) + target_link_libraries(lib-sdl-test PUBLIC "${SDL2_LIBRARIES}") + + # Creating executable + add_executable( + sdl-test # Exe name + "apps/sdl-test.cpp" # Exe Source(s) ) - target_link_libraries( # Linking the exe to library - sdl-test # Executable to link - PRIVATE # - lib-sdl-test # Library to link - ${SDL2_LIBRARIES} + + # Linking the exe to library + target_link_libraries( + sdl-test # Executable to link + PRIVATE # Visibility + lib-sdl-test # Library to link ) -elseif(DB_BUILD) - set(BUILD_STATUS "Building in debug mode") - # Create compile_commands.json for linter - set(CMAKE_EXPORT_COMPILE_COMMANDS ON) - - target_compile_definitions(inherited PRIVATE BUILD_STATUS=${BUILD_STATUS}) -elseif(DB_CONF) - set(BUILD_STATUS "Building in debug mode, configuring files") - - # Configure header file with CMake variables defined in src/lib-inherit.h.in - # @ONLY is specified, only variables of the form @VAR@ will be replaced and ${VAR} will be ignored. - # configure_file(src/lib-inherit.h src/lib-inherit.h @ONLY) - configure_file(apps/sdl-test.cpp apps/sdl-test.cpp @ONLY) - - # Create compile_commands.json for linter - set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +else() + message( + "Error: CMake was unable to find SDL2 package.\n" + "Please install the libsdl2-dev package and try again.\n" + ) endif() -#target_compile_definitions(inherited PRIVATE BUILD_STATUS=${BUILD_STATUS}) - diff --git a/cpp/sdl-cmake/apps/sdl-test.cpp b/cpp/sdl-cmake/apps/sdl-test.cpp index 6e5317d..4165692 100644 --- a/cpp/sdl-cmake/apps/sdl-test.cpp +++ b/cpp/sdl-cmake/apps/sdl-test.cpp @@ -9,29 +9,35 @@ ## apps/inherited.cpp */ -#include +#include #include -//#include int main (int argc, char const * argv[]) { // Cast cli arguments to void since they are unused in this exe (void)argc; (void)argv; - + // Ensure SDL is initialized before continuing - if (SDL_Init(SDL_INIT_EVERYTHING) < 0) - exit(2); - + if (SDL_Init(SDL_INIT_EVERYTHING) < 0) exit(2); + SDL_Window *window; SDL_Renderer *renderer; - if (InitScreen(window, renderer) < 0) + if (InitScreen(window, renderer) < 0) { std::cout << "Error - Unable to initialize SDL screen\n"; - - DrawDelay(renderer, 3000); + } - std::cout << "Test\n"; + // Draw a window for 3000ms + DrawDelay(renderer, 3000); + // Destroy the window after 3 seconds + SDL_DestroyWindow(window); + // Destroy the renderer, since we won't be using it anymore + SDL_DestroyRenderer(renderer); + + std::cout << "Testing creation of Shape, Rectangle...\n"; + // Create a custom shape, and a default shape Shape shape(4,4), dShape; + // Create a custom rectangle, and a default rectangle Rectangle rect(4,8), dRect; std::cout << dShape.PrintInfo() << std::endl; std::cout << shape.PrintInfo() << std::endl; diff --git a/cpp/sdl-cmake/src/lib-sdl-test.cpp b/cpp/sdl-cmake/src/lib-sdl-test.cpp index ba7b239..8a9d93e 100644 --- a/cpp/sdl-cmake/src/lib-sdl-test.cpp +++ b/cpp/sdl-cmake/src/lib-sdl-test.cpp @@ -4,45 +4,22 @@ ## ## ## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## ############################################################################## -## src/lib-inherit.cpp +## */ -#include +#include -// Shape class definitions -Shape::Shape(double w, double h) { - height = h; - width = w; +/******************************************************************************/ +// Shape base class definitions +std::string Shape::PrintInfo() { + return name + " HxW: " + std::to_string(height) + "x" + std::to_string(width); }; -Shape::Shape() { - height = 2; - width = 2; -}; -Shape::~Shape() { /* Shape destructor */}; +/******************************************************************************/ +// SDL helper function definitions -const std::string Shape::PrintInfo() { - info = name + " HxW: " + std::to_string(height) + "x" + std::to_string(width); - return info; -}; - -// Rectangle Class definitions - -Rectangle::Rectangle(double w, double h) { - height = h; - width = w; -}; - -Rectangle::Rectangle() { - height = 2; - width = 4; -}; - -Rectangle::~Rectangle() { /* Rectangle destructor */ }; - -/// SDL Helper Definitions int InitScreen(SDL_Window* &window, SDL_Renderer* &renderer) { int state = 0; @@ -66,6 +43,5 @@ void DrawDelay(SDL_Renderer* renderer, int delay) { SDL_RenderPresent(renderer); // Wait 3000ms, then continue SDL_Delay(delay); - return; } diff --git a/cpp/sdl-cmake/src/lib-sdl-test.h b/cpp/sdl-cmake/src/lib-sdl-test.h index 9ad7cc1..ad3eeb8 100644 --- a/cpp/sdl-cmake/src/lib-sdl-test.h +++ b/cpp/sdl-cmake/src/lib-sdl-test.h @@ -10,33 +10,42 @@ #include #include #include +#include #include + class Shape { +public: + // Provide ctor to set name of derived shape + Shape(double w, double h, std::string name_) : + width(w), height(h), name(std::move(name_)) {} + Shape(double w, double h) : width(w), height(h) {} + Shape() : width(2), height(2) {} + virtual ~Shape() = default; + + // All derived inherit ability to show name + virtual std::string PrintInfo(); + private: double width, height; - std::string info; const std::string name = "Shape"; - - public: - Shape(double w, double h); - Shape(); - ~Shape(); - virtual const std::string PrintInfo(); }; + +/******************************************************************************/ +// Rectangle derived Shape + class Rectangle: public Shape { - private: - double width, height; - std::string info; - public: - Rectangle(double w, double h); - Rectangle(); - ~Rectangle(); - + Rectangle(double w, double h) : Shape(w, h, "Rectangle") {} + Rectangle() : Shape(4, 2, "Rectangle") {} + ~Rectangle() override = default; }; + +/******************************************************************************/ +// SDL helper functions + int InitScreen(SDL_Window* &window, SDL_Renderer* &renderer); void DrawDelay(SDL_Renderer* renderer, int delay);