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/
├── algorithms # Examples of various algorithms written in C++
├── cmake # Example of using cmake to build and organize larger projects
├── 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
├── 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

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