Rearrange graphics projects into subdirectory

This commit is contained in:
2021-05-29 15:08:39 -04:00
parent 255d7efe9f
commit c2300d7121
20 changed files with 42 additions and 146 deletions

View File

@@ -0,0 +1,21 @@
###############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
## About: A root project for practicing C++ ##
## This project can be built to debug and run all nested projects ##
## Or, any subdirectory with a project() statement can be selected ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################
#
cmake_minimum_required(VERSION 3.15)
project(
#[[NAME]] Graphics
VERSION 1.0
DESCRIPTION "A root project for practicing graphics programming in C++"
LANGUAGES CXX
)
add_subdirectory(opengl-cmake)
add_subdirectory(sdl-cmake)

12
cpp/graphics/README.md Normal file
View File

@@ -0,0 +1,12 @@
# Graphics
Example graphics programming projects written in C++
```
klips/cpp/graphics
.
├── opengl # Barebones opengl application written in C++ built with gcc
├── opengl-cmake# Barebones opengl application written in C++ built with cmake
├── sdl-cmake # Barebones sdl application written in C++ built with cmake
└── sdl # Barebones sdl application written in C++ built with gcc
```

View File

@@ -0,0 +1,52 @@
###############################################################################
## 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]] OpenGL-Cmake
DESCRIPTION "Example project for building OpenGL projects with CMake"
LANGUAGES CXX
)
add_library(lib-opengl-test "src/lib-opengl-test.cpp")
target_include_directories(lib-opengl-test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
# Find OpenGL package
find_package(OpenGL REQUIRED)
if (OPENGL_FOUND)
# Link opengl-test executable to OpenGL
target_include_directories(lib-opengl-test PUBLIC ${OPENGL_INCLUDE_DIR})
target_link_libraries(lib-opengl-test PUBLIC ${OPENGL_LIBRARIES})
else()
message(
"Error: CMake was unable to find the OpenGL package\n"
"Please install OpenGL and try again\n"
)
endif()
# Find GLUT package
find_package(GLUT REQUIRED)
if (GLUT_FOUND)
# Link lib-opengl-test executable to GLUT
target_include_directories(lib-opengl-test PUBLIC ${GLUT_INCLUDE_DIR})
target_link_libraries(lib-opengl-test PUBLIC ${GLUT_LIBRARIES})
else()
message(
"Error: CMake was unable to find the GLUT package\n"
"Please install GLUT (freeglut3-dev) and try again\n"
)
endif()
# Add test executable
add_executable(opengl-test "apps/test-gl.cpp")
target_link_libraries(opengl-test PRIVATE lib-opengl-test)
target_include_directories(opengl-test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)

View File

@@ -0,0 +1,64 @@
/*#############################################################################
## Requires freeglut3-dev to be installed with your package manager ##
## To build an executable: `g++ test-gl.cpp -w -lGL -lGLU -lglut -o test` ##
## ##
## Testing building OpenGL projects with source code from lazyfoo - ##
## https://lazyfoo.net/tutorials/OpenGL/ ##
##############################################################################
## test-gl.cpp
*/
#include <lib-opengl-test.hpp>
#include <GL/freeglut.h>
#include <cstdio>
#include <iostream>
//Screen constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_FPS = 60;
//Color modes
const int COLOR_MODE_CYAN = 0;
const int COLOR_MODE_MULTI = 1;
int main( int argc, char* args[] )
{
std::cout << "Press Q to change color mode, E to adjust zoom\n";
//Initialize FreeGLUT
glutInit( &argc, args );
//Create OpenGL 2.1 context
glutInitContextVersion( 2, 1 );
//Create Double Buffered Window
glutInitDisplayMode( GLUT_DOUBLE );
glutInitWindowSize( SCREEN_WIDTH, SCREEN_HEIGHT );
glutCreateWindow( "OpenGL" );
//Do post window/context creation initialization
if( !initGL() )
{
printf( "Unable to initialize graphics library!\n" );
return 1;
}
//Set keyboard handler
glutKeyboardFunc( handleKeys );
//Set rendering function
glutDisplayFunc( render );
//Set main loop
glutTimerFunc( 1000 / SCREEN_FPS, runMainLoop, 0 );
//Start GLUT main loop
glutMainLoop();
return 0;
}

View File

@@ -0,0 +1,144 @@
#include <lib-opengl-test.hpp>
#include <GL/freeglut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <cstdio>
//Screen constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_FPS = 60;
//Color modes
const int COLOR_MODE_CYAN = 0;
const int COLOR_MODE_MULTI = 1;
//The current color rendering mode
int gColorMode = 0;
//The projection scale
GLfloat gProjectionScale = 1.f;
void runMainLoop( int val )
{
//Frame logic
update();
render();
//Run frame one more time
glutTimerFunc( 1000 / SCREEN_FPS, runMainLoop, val );
}
bool initGL()
{
//Initialize Projection Matrix
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0.0, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0, 1.0, -1.0 );
//Initialize Modelview Matrix
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
//Initialize clear color
glClearColor( 0.f, 0.f, 0.f, 1.f );
//Check for error
GLenum error = glGetError();
if( error != GL_NO_ERROR )
{
printf( "Error initializing OpenGL! %s\n", gluErrorString( error ) );
return false;
}
return true;
}
void update()
{
}
void render()
{
//Clear color buffer
glClear( GL_COLOR_BUFFER_BIT );
//Reset modelview matrix
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
//Move to center of the screen
glTranslatef( SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f, 0.f );
//Render quad
if( gColorMode == COLOR_MODE_CYAN )
{
//Solid Cyan
glBegin( GL_QUADS );
glColor3f( 0.f, 1.f, 1.f );
glVertex2f( -50.f, -50.f );
glVertex2f( 50.f, -50.f );
glVertex2f( 50.f, 50.f );
glVertex2f( -50.f, 50.f );
glEnd();
}
else
{
//RYGB Mix
glBegin( GL_QUADS );
glColor3f( 1.f, 0.f, 0.f ); glVertex2f( -50.f, -50.f );
glColor3f( 1.f, 1.f, 0.f ); glVertex2f( 50.f, -50.f );
glColor3f( 0.f, 1.f, 0.f ); glVertex2f( 50.f, 50.f );
glColor3f( 0.f, 0.f, 1.f ); glVertex2f( -50.f, 50.f );
glEnd();
}
//Update screen
glutSwapBuffers();
}
void handleKeys( unsigned char key, int x, int y )
{
//If the user presses q
if( key == 'q' )
{
//Toggle color mode
if( gColorMode == COLOR_MODE_CYAN )
{
gColorMode = COLOR_MODE_MULTI;
}
else
{
gColorMode = COLOR_MODE_CYAN;
}
}
else if( key == 'e' )
{
//Cycle through projection scales
if( gProjectionScale == 1.f )
{
//Zoom out
gProjectionScale = 2.f;
}
else if( gProjectionScale == 2.f )
{
//Zoom in
gProjectionScale = 0.5f;
}
else if( gProjectionScale == 0.5f )
{
//Regular zoom
gProjectionScale = 1.f;
}
//Update projection matrix
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0.0, SCREEN_WIDTH * gProjectionScale, SCREEN_HEIGHT * gProjectionScale, 0.0, 1.0, -1.0 );
}
}

View File

@@ -0,0 +1,25 @@
#ifndef LIB_OPENGL_TEST_HPP
#define LIB_OPENGL_TEST_HPP
#include <GL/freeglut.h>
#include <GL/gl.h>
#include <GL/glu.h>
void runMainLoop( int val );
/*
Pre Condition:
-Initialized freeGLUT
Post Condition:
-Calls the main loop functions and sets itself to be called back in 1000 / SCREEN_FPS milliseconds
Side Effects:
-Sets glutTimerFunc
*/
bool initGL();
void update();
void render();
void handleKeys( unsigned char key, int x, int y );
#endif // LIB_OPENGL_TEST_HPP

View File

@@ -0,0 +1,195 @@
/*#############################################################################
## Author: Shaun Reed ##
## Requires freeglut3-dev to be installed with your package manager ##
## To build an executable: `g++ test-gl.cpp -w -lGL -lGLU -lglut -o test` ##
## ##
## Testing building OpenGL projects with source code from lazyfoo - ##
## https://lazyfoo.net/tutorials/OpenGL/ ##
##############################################################################
## test-gl.cpp
*/
#include <GL/freeglut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdio.h>
//Screen constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_FPS = 60;
//Color modes
const int COLOR_MODE_CYAN = 0;
const int COLOR_MODE_MULTI = 1;
//The current color rendering mode
int gColorMode = COLOR_MODE_CYAN;
//The projection scale
GLfloat gProjectionScale = 1.f;
bool initGL()
{
//Initialize Projection Matrix
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0.0, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0, 1.0, -1.0 );
//Initialize Modelview Matrix
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
//Initialize clear color
glClearColor( 0.f, 0.f, 0.f, 1.f );
//Check for error
GLenum error = glGetError();
if( error != GL_NO_ERROR )
{
printf( "Error initializing OpenGL! %s\n", gluErrorString( error ) );
return false;
}
return true;
}
void update()
{
}
void render()
{
//Clear color buffer
glClear( GL_COLOR_BUFFER_BIT );
//Reset modelview matrix
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
//Move to center of the screen
glTranslatef( SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f, 0.f );
//Render quad
if( gColorMode == COLOR_MODE_CYAN )
{
//Solid Cyan
glBegin( GL_QUADS );
glColor3f( 0.f, 1.f, 1.f );
glVertex2f( -50.f, -50.f );
glVertex2f( 50.f, -50.f );
glVertex2f( 50.f, 50.f );
glVertex2f( -50.f, 50.f );
glEnd();
}
else
{
//RYGB Mix
glBegin( GL_QUADS );
glColor3f( 1.f, 0.f, 0.f ); glVertex2f( -50.f, -50.f );
glColor3f( 1.f, 1.f, 0.f ); glVertex2f( 50.f, -50.f );
glColor3f( 0.f, 1.f, 0.f ); glVertex2f( 50.f, 50.f );
glColor3f( 0.f, 0.f, 1.f ); glVertex2f( -50.f, 50.f );
glEnd();
}
//Update screen
glutSwapBuffers();
}
void handleKeys( unsigned char key, int x, int y )
{
//If the user presses q
if( key == 'q' )
{
//Toggle color mode
if( gColorMode == COLOR_MODE_CYAN )
{
gColorMode = COLOR_MODE_MULTI;
}
else
{
gColorMode = COLOR_MODE_CYAN;
}
}
else if( key == 'e' )
{
//Cycle through projection scales
if( gProjectionScale == 1.f )
{
//Zoom out
gProjectionScale = 2.f;
}
else if( gProjectionScale == 2.f )
{
//Zoom in
gProjectionScale = 0.5f;
}
else if( gProjectionScale == 0.5f )
{
//Regular zoom
gProjectionScale = 1.f;
}
//Update projection matrix
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0.0, SCREEN_WIDTH * gProjectionScale, SCREEN_HEIGHT * gProjectionScale, 0.0, 1.0, -1.0 );
}
}
void runMainLoop( int val );
/*
Pre Condition:
-Initialized freeGLUT
Post Condition:
-Calls the main loop functions and sets itself to be called back in 1000 / SCREEN_FPS milliseconds
Side Effects:
-Sets glutTimerFunc
*/
int main( int argc, char* args[] )
{
//Initialize FreeGLUT
glutInit( &argc, args );
//Create OpenGL 2.1 context
glutInitContextVersion( 2, 1 );
//Create Double Buffered Window
glutInitDisplayMode( GLUT_DOUBLE );
glutInitWindowSize( SCREEN_WIDTH, SCREEN_HEIGHT );
glutCreateWindow( "OpenGL" );
//Do post window/context creation initialization
if( !initGL() )
{
printf( "Unable to initialize graphics library!\n" );
return 1;
}
//Set keyboard handler
glutKeyboardFunc( handleKeys );
//Set rendering function
glutDisplayFunc( render );
//Set main loop
glutTimerFunc( 1000 / SCREEN_FPS, runMainLoop, 0 );
//Start GLUT main loop
glutMainLoop();
return 0;
}
void runMainLoop( int val )
{
//Frame logic
update();
render();
//Run frame one more time
glutTimerFunc( 1000 / SCREEN_FPS, runMainLoop, val );
}

View 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()

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

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

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

View File

@@ -0,0 +1,110 @@
/*#############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2019 Shaun Reed, all rights reserved ##
## Requires SDL: `sudo apt-get install libsdl2-dev` ##
## To build an executable: `g++ inherited.cpp -lSDL2 -o test` ##
## ##
## 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;
}