Update OpenGL standalone example

+ Remove unused directories
+ Update cpp/README.txt to include new directories
This commit is contained in:
Shaun Reed 2021-05-24 13:12:14 -04:00
parent b2bdd62fb2
commit 16ac2046fa
4 changed files with 115 additions and 240 deletions

View File

@ -2,12 +2,14 @@
``` ```
shaunrd0/klips/cpp/ shaunrd0/klips/cpp/
├── algorithms # Examples of various algorithms written in C++
├── cmake # Example of using cmake to build and organize larger projects ├── cmake # Example of using cmake to build and organize larger projects
├── datastructs # Collection of useful datastructures written in C++ ├── datastructs # Collection of useful datastructures written in C++
├── opengl # Barebones opengl application written in C++ built with make ├── opengl # Barebones opengl application written in C++ built with gcc
├── patterns # Examples of various design patterns written in C++
├── README.md ├── README.md
├── sdl # Barebones sdl application written in C++ built with make ├── sdl-cmake # Barebones sdl 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
``` ```
This directory contains a `CMakeLists.txt`, which can be selected to open as a This directory contains a `CMakeLists.txt`, which can be selected to open as a

View File

@ -1,58 +0,0 @@
###############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2019 Shaun Reed, all rights reserved ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
###############################################################################
# Define CMake version
cmake_minimum_required(VERSION 3.16)
project( # Define project
invaders # Project name
DESCRIPTION "Space invaders remake using OpenGL and CPP"
LANGUAGES CXX
)
# Pass this to program to control debug output
option (EXE_BUILD "Should we build the executable?" ON)
add_library( # Add Library
lib-invaders # Library Name
"src/lib-invaders.cpp" # Sources..
"src/lib-invaders.h"
)
target_include_directories( # When calling library, include a directory
lib-invaders # Library name
PUBLIC #
"${CMAKE_CURRENT_SOURCE_DIR}" # Source directory of exe including our library
)
if (EXE_BUILD)
set(BUILD_STATUS "Building default executable")
include(FindOpenGL)
include(FindGLUT)
include_directories(${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIR})
add_executable( # Creating executable
invaders # Exe name
"app/invaders.cpp" # Exe Source(s)
)
# Link the executable with OpenGL libraries
target_link_libraries( # Linking the exe to library
invaders # Executable to link
PUBLIC #
lib-invaders # Library to link
${OPENGL_LIBRARIES}
)
# Link the executable with OpenGL Utility Toolkit
target_link_libraries( # Linking the exe to library
invaders # Executable to link
PUBLIC #
lib-invaders # Library to link
${GLUT_LIBRARIES}
)
endif()

View File

@ -8,6 +8,7 @@
############################################################################## ##############################################################################
## test-gl.cpp ## test-gl.cpp
*/ */
#include <GL/freeglut.h> #include <GL/freeglut.h>
#include <GL/gl.h> #include <GL/gl.h>
#include <GL/glu.h> #include <GL/glu.h>
@ -30,27 +31,27 @@ GLfloat gProjectionScale = 1.f;
bool initGL() bool initGL()
{ {
//Initialize Projection Matrix //Initialize Projection Matrix
glMatrixMode( GL_PROJECTION ); glMatrixMode( GL_PROJECTION );
glLoadIdentity(); glLoadIdentity();
glOrtho( 0.0, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0, 1.0, -1.0 ); glOrtho( 0.0, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0, 1.0, -1.0 );
//Initialize Modelview Matrix //Initialize Modelview Matrix
glMatrixMode( GL_MODELVIEW ); glMatrixMode( GL_MODELVIEW );
glLoadIdentity(); glLoadIdentity();
//Initialize clear color //Initialize clear color
glClearColor( 0.f, 0.f, 0.f, 1.f ); glClearColor( 0.f, 0.f, 0.f, 1.f );
//Check for error //Check for error
GLenum error = glGetError(); GLenum error = glGetError();
if( error != GL_NO_ERROR ) if( error != GL_NO_ERROR )
{ {
printf( "Error initializing OpenGL! %s\n", gluErrorString( error ) ); printf( "Error initializing OpenGL! %s\n", gluErrorString( error ) );
return false; return false;
} }
return true; return true;
} }
void update() void update()
@ -60,82 +61,82 @@ void update()
void render() void render()
{ {
//Clear color buffer //Clear color buffer
glClear( GL_COLOR_BUFFER_BIT ); glClear( GL_COLOR_BUFFER_BIT );
//Reset modelview matrix //Reset modelview matrix
glMatrixMode( GL_MODELVIEW ); glMatrixMode( GL_MODELVIEW );
glLoadIdentity(); glLoadIdentity();
//Move to center of the screen //Move to center of the screen
glTranslatef( SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f, 0.f ); glTranslatef( SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f, 0.f );
//Render quad //Render quad
if( gColorMode == COLOR_MODE_CYAN ) if( gColorMode == COLOR_MODE_CYAN )
{ {
//Solid Cyan //Solid Cyan
glBegin( GL_QUADS ); glBegin( GL_QUADS );
glColor3f( 0.f, 1.f, 1.f ); 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 );
glVertex2f( 50.f, 50.f ); glVertex2f( 50.f, 50.f );
glVertex2f( -50.f, 50.f ); glVertex2f( -50.f, 50.f );
glEnd(); glEnd();
} }
else else
{ {
//RYGB Mix //RYGB Mix
glBegin( GL_QUADS ); glBegin( GL_QUADS );
glColor3f( 1.f, 0.f, 0.f ); glVertex2f( -50.f, -50.f ); 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( 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, 1.f, 0.f ); glVertex2f( 50.f, 50.f );
glColor3f( 0.f, 0.f, 1.f ); glVertex2f( -50.f, 50.f ); glColor3f( 0.f, 0.f, 1.f ); glVertex2f( -50.f, 50.f );
glEnd(); glEnd();
} }
//Update screen //Update screen
glutSwapBuffers(); glutSwapBuffers();
} }
void handleKeys( unsigned char key, int x, int y ) void handleKeys( unsigned char key, int x, int y )
{ {
//If the user presses q //If the user presses q
if( key == 'q' ) if( key == 'q' )
{
//Toggle color mode
if( gColorMode == COLOR_MODE_CYAN )
{ {
//Toggle color mode gColorMode = COLOR_MODE_MULTI;
if( gColorMode == COLOR_MODE_CYAN )
{
gColorMode = COLOR_MODE_MULTI;
}
else
{
gColorMode = COLOR_MODE_CYAN;
}
} }
else if( key == 'e' ) else
{ {
//Cycle through projection scales gColorMode = COLOR_MODE_CYAN;
if( gProjectionScale == 1.f ) }
{ }
//Zoom out else if( key == 'e' )
gProjectionScale = 2.f; {
} //Cycle through projection scales
else if( gProjectionScale == 2.f ) if( gProjectionScale == 1.f )
{ {
//Zoom in //Zoom out
gProjectionScale = 0.5f; gProjectionScale = 2.f;
} }
else if( gProjectionScale == 0.5f ) else if( gProjectionScale == 2.f )
{ {
//Regular zoom //Zoom in
gProjectionScale = 1.f; gProjectionScale = 0.5f;
} }
else if( gProjectionScale == 0.5f )
{
//Regular zoom
gProjectionScale = 1.f;
}
//Update projection matrix //Update projection matrix
glMatrixMode( GL_PROJECTION ); glMatrixMode( GL_PROJECTION );
glLoadIdentity(); glLoadIdentity();
glOrtho( 0.0, SCREEN_WIDTH * gProjectionScale, SCREEN_HEIGHT * gProjectionScale, 0.0, 1.0, -1.0 ); glOrtho( 0.0, SCREEN_WIDTH * gProjectionScale, SCREEN_HEIGHT * gProjectionScale, 0.0, 1.0, -1.0 );
} }
} }
void runMainLoop( int val ); void runMainLoop( int val );
@ -150,45 +151,45 @@ Side Effects:
int main( int argc, char* args[] ) int main( int argc, char* args[] )
{ {
//Initialize FreeGLUT //Initialize FreeGLUT
glutInit( &argc, args ); glutInit( &argc, args );
//Create OpenGL 2.1 context //Create OpenGL 2.1 context
glutInitContextVersion( 2, 1 ); glutInitContextVersion( 2, 1 );
//Create Double Buffered Window //Create Double Buffered Window
glutInitDisplayMode( GLUT_DOUBLE ); glutInitDisplayMode( GLUT_DOUBLE );
glutInitWindowSize( SCREEN_WIDTH, SCREEN_HEIGHT ); glutInitWindowSize( SCREEN_WIDTH, SCREEN_HEIGHT );
glutCreateWindow( "OpenGL" ); glutCreateWindow( "OpenGL" );
//Do post window/context creation initialization //Do post window/context creation initialization
if( !initGL() ) if( !initGL() )
{ {
printf( "Unable to initialize graphics library!\n" ); printf( "Unable to initialize graphics library!\n" );
return 1; return 1;
} }
//Set keyboard handler //Set keyboard handler
glutKeyboardFunc( handleKeys ); glutKeyboardFunc( handleKeys );
//Set rendering function //Set rendering function
glutDisplayFunc( render ); glutDisplayFunc( render );
//Set main loop //Set main loop
glutTimerFunc( 1000 / SCREEN_FPS, runMainLoop, 0 ); glutTimerFunc( 1000 / SCREEN_FPS, runMainLoop, 0 );
//Start GLUT main loop //Start GLUT main loop
glutMainLoop(); glutMainLoop();
return 0; return 0;
} }
void runMainLoop( int val ) void runMainLoop( int val )
{ {
//Frame logic //Frame logic
update(); update();
render(); render();
//Run frame one more time //Run frame one more time
glutTimerFunc( 1000 / SCREEN_FPS, runMainLoop, val ); glutTimerFunc( 1000 / SCREEN_FPS, runMainLoop, val );
} }

View File

@ -1,70 +0,0 @@
###############################################################################
## 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})