Add basic cpp-sdl project using g++ within vscode

This commit is contained in:
Shaun Reed 2019-10-15 19:05:20 -04:00
parent 84ab7580a7
commit f7740de1f5
8 changed files with 276 additions and 0 deletions

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

View File

@ -4,6 +4,8 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{ {
"name": "(gdb) Launch", "name": "(gdb) Launch",
"type": "cppdbg", "type": "cppdbg",

26
plates/cpp-sdl/.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,26 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "cpp build active file",
"command": "g++",
"args": [
"${workspaceFolder}/inherited.cpp",
"-lSDL2",
"-o",
"${workspaceFolder}/build/inherited",
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

View File

@ -0,0 +1,108 @@
/*#############################################################################
## 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 <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) {
height = h;
width = w;
};
Shape() {
height = 2;
width = 2;
};
~Shape() { /* Shape destructor */};
virtual const std::string PrintInfo() {
info = name + " HxW: " + std::to_string(height) + "x" + std::to_string(width);
return info;
};
};
class Rectangle: public Shape {
private:
double width, height;
std::string info;
public:
Rectangle(double w, double h) {
height = h;
width = w;
};
Rectangle() {
height = 2;
width = 4;
};
~Rectangle() { /* Rectangle destructor */ };
};
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;
}
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;
}