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

View File

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

View File

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