Add cpp template for SDL library

This commit is contained in:
Shaun Reed 2019-10-14 21:51:50 -04:00
parent 0a759c0d1f
commit 84ab7580a7
5 changed files with 250 additions and 0 deletions

27
plates/cpp-sdl/.vscode/launch.json vendored Normal file
View File

@ -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
}
]
}
]
}

View File

@ -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})

View File

@ -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 <src/lib-inherit.h>
#include <iostream>
//#include <string>
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;
}

View File

@ -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 <src/lib-inherit.h>
// 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;
}

View File

@ -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 <iostream>
#include <string>
#include <SDL2/SDL.h>
#include <vector>
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);