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
This commit is contained in:
Shaun Reed 2021-05-24 16:20:54 -04:00
parent 16ac2046fa
commit 0933f9bdf5
4 changed files with 89 additions and 109 deletions

View File

@ -1,70 +1,59 @@
############################################################################### ###############################################################################
## Author: Shaun Reed ## ## 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 ## ## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
############################################################################## ##############################################################################
# Root CMakeLists.txt of cpp practice 4-inheritance #
# Define CMake version # Define CMake version
cmake_minimum_required(VERSION 3.15) cmake_minimum_required(VERSION 3.15)
project( # Define project project(
inheritance # Project name #[[NAME]] SDL-Cmake
DESCRIPTION "Example project for class inheritance" DESCRIPTION "Example project for building SDL projects with CMake"
LANGUAGES CXX 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"
) )
target_include_directories( # When calling library, include a directory # Add Library
lib-sdl-test # Library name add_library(
PUBLIC # lib-sdl-test # Library Name
"${CMAKE_CURRENT_SOURCE_DIR}" # Source directory of exe including our library "src/lib-sdl-test.cpp" # Sources..
"src/lib-sdl-test.h"
) )
if (EXE_BUILD) target_include_directories( # When calling library, include a directory
set(BUILD_STATUS "Building default executable") lib-sdl-test # Library name
include(FindPkgConfig) PUBLIC # Visibility
pkg_search_module(SDL2 REQUIRED sdl2) "${CMAKE_CURRENT_SOURCE_DIR}/src" # Source directory for library
include_directories(${SDL2_INCLUDE_DIRS}) )
add_executable( # Creating executable # Search for SDL2 package
sdl-test # Exe name find_package(SDL2 REQUIRED sdl2)
"apps/sdl-test.cpp" # Exe Source(s)
# 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 # Linking the exe to library
PRIVATE # target_link_libraries(
lib-sdl-test # Library to link sdl-test # Executable to link
${SDL2_LIBRARIES} PRIVATE # Visibility
lib-sdl-test # Library to link
) )
elseif(DB_BUILD)
set(BUILD_STATUS "Building in debug mode")
# Create compile_commands.json for linter else()
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) message(
"Error: CMake was unable to find SDL2 package.\n"
target_compile_definitions(inherited PRIVATE BUILD_STATUS=${BUILD_STATUS}) "Please install the libsdl2-dev package and try again.\n"
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)
endif() endif()
#target_compile_definitions(inherited PRIVATE BUILD_STATUS=${BUILD_STATUS})

View File

@ -9,29 +9,35 @@
## apps/inherited.cpp ## apps/inherited.cpp
*/ */
#include <src/lib-sdl-test.h> #include <lib-sdl-test.h>
#include <iostream> #include <iostream>
//#include <string>
int main (int argc, char const * argv[]) { int main (int argc, char const * argv[]) {
// Cast cli arguments to void since they are unused in this exe // Cast cli arguments to void since they are unused in this exe
(void)argc; (void)argc;
(void)argv; (void)argv;
// Ensure SDL is initialized before continuing // Ensure SDL is initialized before continuing
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) if (SDL_Init(SDL_INIT_EVERYTHING) < 0) exit(2);
exit(2);
SDL_Window *window; SDL_Window *window;
SDL_Renderer *renderer; SDL_Renderer *renderer;
if (InitScreen(window, renderer) < 0) if (InitScreen(window, renderer) < 0) {
std::cout << "Error - Unable to initialize SDL screen\n"; 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; Shape shape(4,4), dShape;
// Create a custom rectangle, and a default rectangle
Rectangle rect(4,8), dRect; Rectangle rect(4,8), dRect;
std::cout << dShape.PrintInfo() << std::endl; std::cout << dShape.PrintInfo() << std::endl;
std::cout << shape.PrintInfo() << std::endl; std::cout << shape.PrintInfo() << std::endl;

View File

@ -4,45 +4,22 @@
## ## ## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## ## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
############################################################################## ##############################################################################
## src/lib-inherit.cpp ##
*/ */
#include <src/lib-sdl-test.h> #include <lib-sdl-test.h>
// Shape class definitions
Shape::Shape(double w, double h) { /******************************************************************************/
height = h; // Shape base class definitions
width = w; 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 InitScreen(SDL_Window* &window, SDL_Renderer* &renderer) {
int state = 0; int state = 0;
@ -66,6 +43,5 @@ void DrawDelay(SDL_Renderer* renderer, int delay) {
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
// Wait 3000ms, then continue // Wait 3000ms, then continue
SDL_Delay(delay); SDL_Delay(delay);
return; return;
} }

View File

@ -10,33 +10,42 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <utility>
#include <vector> #include <vector>
class Shape { 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: private:
double width, height; double width, height;
std::string info;
const std::string name = "Shape"; 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 { class Rectangle: public Shape {
private:
double width, height;
std::string info;
public: public:
Rectangle(double w, double h); Rectangle(double w, double h) : Shape(w, h, "Rectangle") {}
Rectangle(); Rectangle() : Shape(4, 2, "Rectangle") {}
~Rectangle(); ~Rectangle() override = default;
}; };
/******************************************************************************/
// SDL helper functions
int InitScreen(SDL_Window* &window, SDL_Renderer* &renderer); int InitScreen(SDL_Window* &window, SDL_Renderer* &renderer);
void DrawDelay(SDL_Renderer* renderer, int delay); void DrawDelay(SDL_Renderer* renderer, int delay);