From 84ab7580a74eff12baf766498a5ffca76e1190df Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Mon, 14 Oct 2019 21:51:50 -0400 Subject: [PATCH] Add cpp template for SDL library --- plates/cpp-sdl/.vscode/launch.json | 27 ++++++++++++ plates/cpp-sdl/CMakeLists.txt | 70 +++++++++++++++++++++++++++++ plates/cpp-sdl/apps/inherited.cpp | 40 +++++++++++++++++ plates/cpp-sdl/src/lib-inherit.cpp | 71 ++++++++++++++++++++++++++++++ plates/cpp-sdl/src/lib-inherit.h | 42 ++++++++++++++++++ 5 files changed, 250 insertions(+) create mode 100644 plates/cpp-sdl/.vscode/launch.json create mode 100644 plates/cpp-sdl/CMakeLists.txt create mode 100644 plates/cpp-sdl/apps/inherited.cpp create mode 100644 plates/cpp-sdl/src/lib-inherit.cpp create mode 100644 plates/cpp-sdl/src/lib-inherit.h diff --git a/plates/cpp-sdl/.vscode/launch.json b/plates/cpp-sdl/.vscode/launch.json new file mode 100644 index 0000000..5a63ae7 --- /dev/null +++ b/plates/cpp-sdl/.vscode/launch.json @@ -0,0 +1,27 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "(gdb) Launch", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/build/inherited", + "args": [], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/plates/cpp-sdl/CMakeLists.txt b/plates/cpp-sdl/CMakeLists.txt new file mode 100644 index 0000000..105e0f8 --- /dev/null +++ b/plates/cpp-sdl/CMakeLists.txt @@ -0,0 +1,70 @@ +############################################################################### +## Author: Shaun Reed ## +## Legal: All Content (c) 2019 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-inherit # Library Name + "src/lib-inherit.cpp" # Sources.. + "src/lib-inherit.h" +) + +target_include_directories( # When calling library, include a directory + lib-inherit # Library name + PUBLIC # + "${CMAKE_CURRENT_SOURCE_DIR}" # Source directory of exe including our library +) + +if (EXE_BUILD) + set(BUILD_STATUS "Building default executable") + include(FindPkgConfig) + pkg_search_module(SDL2 REQUIRED sdl2) + include_directories(${SDL2_INCLUDE_DIRS}) + + add_executable( # Creating executable + inherited # Exe name + "apps/inherited.cpp" # Exe Source(s) + ) + target_link_libraries( # Linking the exe to library + inherited # Executable to link + PRIVATE # + lib-inherit # 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/inherited.cpp apps/inherited.cpp @ONLY) + + # Create compile_commands.json for linter + set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +endif() +#target_compile_definitions(inherited PRIVATE BUILD_STATUS=${BUILD_STATUS}) + diff --git a/plates/cpp-sdl/apps/inherited.cpp b/plates/cpp-sdl/apps/inherited.cpp new file mode 100644 index 0000000..4b958cd --- /dev/null +++ b/plates/cpp-sdl/apps/inherited.cpp @@ -0,0 +1,40 @@ +/*############################################################################# +## Author: Shaun Reed ## +## Legal: All Content (c) 2019 Shaun Reed, all rights reserved ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################## +## apps/inherited.cpp +*/ + +#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); + + SDL_Window *window; + SDL_Renderer *renderer; + + if (InitScreen(window, renderer) < 0) + std::cout << "Error - Unable to initialize SDL screen\n"; + + DrawDelay(renderer, 3000); + + std::cout << "Test\n"; + Shape shape(4,4), dShape; + Rectangle rect(4,8), dRect; + std::cout << dShape.PrintInfo() << std::endl; + std::cout << shape.PrintInfo() << std::endl; + std::cout << dRect.PrintInfo() << std::endl; + std::cout << rect.PrintInfo() << std::endl; + + return 0; +} diff --git a/plates/cpp-sdl/src/lib-inherit.cpp b/plates/cpp-sdl/src/lib-inherit.cpp new file mode 100644 index 0000000..365afda --- /dev/null +++ b/plates/cpp-sdl/src/lib-inherit.cpp @@ -0,0 +1,71 @@ +/*############################################################################## +## Author: Shaun Reed ## +## Legal: All Content (c) 2019 Shaun Reed, all rights reserved ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################## +## src/lib-inherit.cpp +*/ + +#include + +// Shape class definitions + +Shape::Shape(double w, double h) { + height = h; + width = w; +}; + +Shape::Shape() { + height = 2; + width = 2; +}; + +Shape::~Shape() { /* Shape destructor */}; + +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; + + // Create window, renderer to draw to screen + if (SDL_CreateWindowAndRenderer(500, 500, 0, &window, &renderer) < 0) { + std::cout << "Error - Unable to create SDL screen and renderer objects\n"; + state = -1; + } + + // Set render DrawColor, fill screen with color + if (SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255) < 0 || SDL_RenderClear(renderer) < 0) { + std::cout << "Error - Unable to set renderer draw color\n"; + state = -1; + } + + return state; +} + +void DrawDelay(SDL_Renderer* renderer, int delay) { + // Show what we have rendered + SDL_RenderPresent(renderer); + // Wait 3000ms, then continue + SDL_Delay(delay); + + return; +} diff --git a/plates/cpp-sdl/src/lib-inherit.h b/plates/cpp-sdl/src/lib-inherit.h new file mode 100644 index 0000000..9ad7cc1 --- /dev/null +++ b/plates/cpp-sdl/src/lib-inherit.h @@ -0,0 +1,42 @@ +/*############################################################################# +## Author: Shaun Reed ## +## Legal: All Content (c) 2019 Shaun Reed, all rights reserved ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################## +## src/lib-inherit.h +*/ + +#include +#include +#include +#include + +class Shape { + 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(); +}; + +class Rectangle: public Shape { + private: + double width, height; + std::string info; + + public: + Rectangle(double w, double h); + Rectangle(); + ~Rectangle(); + +}; + +int InitScreen(SDL_Window* &window, SDL_Renderer* &renderer); + +void DrawDelay(SDL_Renderer* renderer, int delay);