Rearrange graphics projects into subdirectory
This commit is contained in:
59
cpp/graphics/sdl-cmake/CMakeLists.txt
Normal file
59
cpp/graphics/sdl-cmake/CMakeLists.txt
Normal file
@@ -0,0 +1,59 @@
|
||||
###############################################################################
|
||||
## Author: Shaun Reed ##
|
||||
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
|
||||
## ##
|
||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||
##############################################################################
|
||||
#
|
||||
|
||||
# Define CMake version
|
||||
cmake_minimum_required(VERSION 3.15)
|
||||
|
||||
project(
|
||||
#[[NAME]] SDL-Cmake
|
||||
DESCRIPTION "Example project for building SDL projects with CMake"
|
||||
LANGUAGES CXX
|
||||
)
|
||||
|
||||
# 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
|
||||
lib-sdl-test # Library name
|
||||
PUBLIC # Visibility
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/src" # Source directory for library
|
||||
)
|
||||
|
||||
# Search for SDL2 package
|
||||
find_package(SDL2 REQUIRED sdl2)
|
||||
|
||||
# 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)
|
||||
)
|
||||
|
||||
# Linking the exe to library
|
||||
target_link_libraries(
|
||||
sdl-test # Executable to link
|
||||
PRIVATE # Visibility
|
||||
lib-sdl-test # Library to link
|
||||
)
|
||||
|
||||
else()
|
||||
message(
|
||||
"Error: CMake was unable to find SDL2 package.\n"
|
||||
"Please install the libsdl2-dev package and try again.\n"
|
||||
)
|
||||
endif()
|
||||
48
cpp/graphics/sdl-cmake/apps/sdl-test.cpp
Normal file
48
cpp/graphics/sdl-cmake/apps/sdl-test.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
/*#############################################################################
|
||||
## Author: Shaun Reed ##
|
||||
## Legal: All Content (c) 2019 Shaun Reed, all rights reserved ##
|
||||
## Requires SDL: `sudo apt-get install libsdl2-dev` ##
|
||||
## To build: `mkdir build && cd build && cmake .. cmake --build .` ##
|
||||
## ##
|
||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||
##############################################################################
|
||||
## apps/inherited.cpp
|
||||
*/
|
||||
|
||||
#include <lib-sdl-test.h>
|
||||
#include <iostream>
|
||||
|
||||
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";
|
||||
}
|
||||
|
||||
// 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;
|
||||
// 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;
|
||||
std::cout << dRect.PrintInfo() << std::endl;
|
||||
std::cout << rect.PrintInfo() << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
47
cpp/graphics/sdl-cmake/src/lib-sdl-test.cpp
Normal file
47
cpp/graphics/sdl-cmake/src/lib-sdl-test.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
/*##############################################################################
|
||||
## Author: Shaun Reed ##
|
||||
## Legal: All Content (c) 2019 Shaun Reed, all rights reserved ##
|
||||
## ##
|
||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||
##############################################################################
|
||||
##
|
||||
*/
|
||||
|
||||
#include <lib-sdl-test.h>
|
||||
|
||||
|
||||
/******************************************************************************/
|
||||
// Shape base class definitions
|
||||
std::string Shape::PrintInfo() {
|
||||
return name + " HxW: " + std::to_string(height) + "x" + std::to_string(width);
|
||||
};
|
||||
|
||||
|
||||
/******************************************************************************/
|
||||
// SDL helper function 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;
|
||||
}
|
||||
51
cpp/graphics/sdl-cmake/src/lib-sdl-test.h
Normal file
51
cpp/graphics/sdl-cmake/src/lib-sdl-test.h
Normal file
@@ -0,0 +1,51 @@
|
||||
/*#############################################################################
|
||||
## 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 <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;
|
||||
const std::string name = "Shape";
|
||||
};
|
||||
|
||||
|
||||
/******************************************************************************/
|
||||
// Rectangle derived Shape
|
||||
|
||||
class Rectangle: public Shape {
|
||||
public:
|
||||
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);
|
||||
Reference in New Issue
Block a user