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:
parent
16ac2046fa
commit
0933f9bdf5
|
@ -1,26 +1,22 @@
|
|||
###############################################################################
|
||||
## 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"
|
||||
project(
|
||||
#[[NAME]] SDL-Cmake
|
||||
DESCRIPTION "Example project for building SDL projects with CMake"
|
||||
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
|
||||
# Add Library
|
||||
add_library(
|
||||
lib-sdl-test # Library Name
|
||||
"src/lib-sdl-test.cpp" # Sources..
|
||||
"src/lib-sdl-test.h"
|
||||
|
@ -28,43 +24,36 @@ add_library( # Add Library
|
|||
|
||||
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
|
||||
PUBLIC # Visibility
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/src" # Source directory for library
|
||||
)
|
||||
|
||||
if (EXE_BUILD)
|
||||
set(BUILD_STATUS "Building default executable")
|
||||
include(FindPkgConfig)
|
||||
pkg_search_module(SDL2 REQUIRED sdl2)
|
||||
include_directories(${SDL2_INCLUDE_DIRS})
|
||||
# Search for SDL2 package
|
||||
find_package(SDL2 REQUIRED sdl2)
|
||||
|
||||
add_executable( # Creating executable
|
||||
# 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
|
||||
|
||||
# Linking the exe to library
|
||||
target_link_libraries(
|
||||
sdl-test # Executable to link
|
||||
PRIVATE #
|
||||
PRIVATE # Visibility
|
||||
lib-sdl-test # Library to link
|
||||
${SDL2_LIBRARIES}
|
||||
)
|
||||
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})
|
||||
|
||||
|
|
|
@ -9,9 +9,8 @@
|
|||
## apps/inherited.cpp
|
||||
*/
|
||||
|
||||
#include <src/lib-sdl-test.h>
|
||||
#include <lib-sdl-test.h>
|
||||
#include <iostream>
|
||||
//#include <string>
|
||||
|
||||
int main (int argc, char const * argv[]) {
|
||||
// Cast cli arguments to void since they are unused in this exe
|
||||
|
@ -19,19 +18,26 @@ int main (int argc, char const * argv[]) {
|
|||
(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";
|
||||
}
|
||||
|
||||
// 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 << "Test\n";
|
||||
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;
|
||||
|
|
|
@ -4,45 +4,22 @@
|
|||
## ##
|
||||
## 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;
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -10,33 +10,42 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include <SDL2/SDL.h>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
|
||||
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);
|
||||
|
|
Loading…
Reference in New Issue