From 82effe520370280416dba621703c46525ca6c259 Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Mon, 24 May 2021 17:36:57 -0400 Subject: [PATCH] Reorganize cpp/opengl-cmake + So project structure is closer to that of cpp/sdl-cmake --- cpp/opengl-cmake/CMakeLists.txt | 23 +++--- cpp/opengl-cmake/apps/test-gl.cpp | 60 ++++++++++++++ .../{test-gl.cpp => src/lib-opengl-test.cpp} | 81 ++++--------------- cpp/opengl-cmake/src/lib-opengl-test.hpp | 25 ++++++ 4 files changed, 114 insertions(+), 75 deletions(-) create mode 100644 cpp/opengl-cmake/apps/test-gl.cpp rename cpp/opengl-cmake/{test-gl.cpp => src/lib-opengl-test.cpp} (62%) create mode 100644 cpp/opengl-cmake/src/lib-opengl-test.hpp diff --git a/cpp/opengl-cmake/CMakeLists.txt b/cpp/opengl-cmake/CMakeLists.txt index 59a413c..952908c 100644 --- a/cpp/opengl-cmake/CMakeLists.txt +++ b/cpp/opengl-cmake/CMakeLists.txt @@ -15,15 +15,15 @@ project( LANGUAGES CXX ) -# Add test executable -add_executable(opengl-test "test-gl.cpp") +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(opengl-test PUBLIC ${OPENGL_INCLUDE_DIR}) - target_link_libraries(opengl-test PUBLIC ${OPENGL_LIBRARIES}) + target_include_directories(lib-opengl-test PUBLIC ${OPENGL_INCLUDE_DIR}) + target_link_libraries(lib-opengl-test PUBLIC ${OPENGL_LIBRARIES}) else() message( @@ -35,13 +35,18 @@ endif() # Find GLUT package find_package(GLUT REQUIRED) if (GLUT_FOUND) - # Link opengl-test executable to GLUT - target_include_directories(opengl-test PUBLIC ${GLUT_INCLUDE_DIR}) - target_link_libraries(opengl-test PUBLIC ${GLUT_LIBRARIES}) + # 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 OpenGL package\n" - "Please install OpenGL and try again\n" + "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) diff --git a/cpp/opengl-cmake/apps/test-gl.cpp b/cpp/opengl-cmake/apps/test-gl.cpp new file mode 100644 index 0000000..678b647 --- /dev/null +++ b/cpp/opengl-cmake/apps/test-gl.cpp @@ -0,0 +1,60 @@ +/*############################################################################# +## 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 + +#include + +#include + +//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[] ) +{ + //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; +} + diff --git a/cpp/opengl-cmake/test-gl.cpp b/cpp/opengl-cmake/src/lib-opengl-test.cpp similarity index 62% rename from cpp/opengl-cmake/test-gl.cpp rename to cpp/opengl-cmake/src/lib-opengl-test.cpp index 9ea87b1..d6d8773 100644 --- a/cpp/opengl-cmake/test-gl.cpp +++ b/cpp/opengl-cmake/src/lib-opengl-test.cpp @@ -1,18 +1,11 @@ -/*############################################################################# -## 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 #include #include #include -#include + +#include //Screen constants const int SCREEN_WIDTH = 640; @@ -24,11 +17,21 @@ const int COLOR_MODE_CYAN = 0; const int COLOR_MODE_MULTI = 1; //The current color rendering mode -int gColorMode = COLOR_MODE_CYAN; +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 @@ -139,57 +142,3 @@ void handleKeys( unsigned char key, int x, int y ) } } -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 ); -} diff --git a/cpp/opengl-cmake/src/lib-opengl-test.hpp b/cpp/opengl-cmake/src/lib-opengl-test.hpp new file mode 100644 index 0000000..16998d2 --- /dev/null +++ b/cpp/opengl-cmake/src/lib-opengl-test.hpp @@ -0,0 +1,25 @@ + +#ifndef LIB_OPENGL_TEST_HPP +#define LIB_OPENGL_TEST_HPP + +#include +#include +#include + +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