Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDL2 support, with multiple windows and fullscreen support. #204

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmake/FindVL.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
# VLWX: wxWidgets gui bindings.
# VLGLUT: GLUT gui bindings.
# VLSDL: SDL gui bindings.
# VLSDL2: SDL2 gui bindings.
# VLEGL: EGL support [EXPERIMENTAL]

macro(_vl_find_library _var)
Expand Down
9 changes: 9 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ if(VL_GUI_SDL_SUPPORT)
add_subdirectory("gui/vlSDL")
endif()

# SDL2
option(VL_GUI_SDL2_SUPPORT "Build SDL2 support" OFF)

if(VL_GUI_SDL2_SUPPORT)
find_package(SDL2 REQUIRED)
add_subdirectory("gui/vlSDL2")
endif()

# wxWidgets
option(VL_GUI_WXWIDGETS_SUPPORT "Build wxWidgets support" OFF)
if(VL_GUI_WXWIDGETS_SUPPORT)
Expand Down Expand Up @@ -173,6 +181,7 @@ cmake_dependent_option(VL_GUI_QT6_EXAMPLES "Build Qt6 examples" ON "VL_GUI_QT6_S
cmake_dependent_option(VL_GUI_MFC_EXAMPLES "Build MFC examples" ON "VL_GUI_MFC_SUPPORT" OFF)
cmake_dependent_option(VL_GUI_WIN32_EXAMPLES "Build win32 examples" ON "VL_GUI_WIN32_SUPPORT" OFF)
cmake_dependent_option(VL_GUI_SDL_EXAMPLES "Build SDL examples" ON "VL_GUI_SDL_SUPPORT" OFF)
cmake_dependent_option(VL_GUI_SDL2_EXAMPLES "Build SDL2 examples" ON "VL_GUI_SDL2_SUPPORT" OFF)
cmake_dependent_option(VL_GUI_WXWIDGETS_EXAMPLES "Build wxWidgets examples" ON "VL_GUI_WXWIDGETS_SUPPORT" OFF)
cmake_dependent_option(VL_GUI_GLES_EXAMPLES "Build OpenGL ES examples" ON "VL_GUI_EGL_SUPPORT" OFF)
#cmake_dependent_option(VL_GUI_COCOA_EXAMPLES "Build Cocoa examples" ON "VL_GUI_COCOA_SUPPORT" OFF)
Expand Down
23 changes: 22 additions & 1 deletion src/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ if( VL_GUI_QT4_EXAMPLES OR
VL_GUI_GLES_EXAMPLES OR
VL_GUI_GLFW_EXAMPLES OR
VL_GUI_GLUT_EXAMPLES OR
VL_GUI_SDL_EXAMPLES )
VL_GUI_SDL_EXAMPLES OR
VL_GUI_SDL2_EXAMPLES
)

################################################################################
# Compile all Applets
Expand Down Expand Up @@ -59,6 +61,25 @@ if( VL_GUI_QT4_EXAMPLES OR
VL_INSTALL_TARGET(vlSDL_tests)
endif()

if(VL_GUI_SDL2_EXAMPLES)
# Darwin
if( ${CMAKE_SYSTEM} MATCHES "Darwin" )
set(SDL2MAIN_M_PATH "Specify path to SDLMain.m" CACHE FILEPATH "Path to SDLMain.m")
endif()

include_directories(${SDL2_INCLUDE_DIR})

# Example
add_executable(vlSDL2_example SDL2_example.cpp Applets/App_RotatingCube.hpp ${SDL2MAIN_M_PATH})
target_link_libraries(vlSDL2_example VLSDL2 ${VL_LIBS_EXAMPLE} ${SDL2MAIN_LIBRARY} ${SDL2_LIBRARY})
VL_INSTALL_TARGET(vlSDL2_example)

# Tests
add_executable(vlSDL2_tests SDL2_tests.cpp ${SDL2MAIN_M_PATH})
target_link_libraries(vlSDL2_tests VLApplets VLSDL2 ${VL_LIBS_TESTS} ${SDL2MAIN_LIBRARY} ${SDL2_LIBRARY})
VL_INSTALL_TARGET(vlSDL2_tests)
endif()

if(VL_GUI_GLUT_EXAMPLES)
include_directories(${GLUT_INCLUDE_DIR})

Expand Down
4 changes: 2 additions & 2 deletions src/examples/GLFW_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ int main ( int argc, char *argv[] )
format.setMultisampleSamples(16);
format.setMultisample(false);
format.setFullscreen(false);
//format.setOpenGLProfile( GLP_Core );
//format.setVersion( 3, 3 );
// format.setOpenGLProfile( GLP_Core );
// format.setVersion( 3, 3 );

TestBatteryGLFW test_battery;
test_battery.run(test, test_str, format, false);
Expand Down
88 changes: 88 additions & 0 deletions src/examples/SDL2_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**************************************************************************************/
/* */
/* Visualization Library */
/* http://visualizationlibrary.org */
/* */
/* Copyright (c) 2005-2020, Michele Bosi */
/* All rights reserved. */
/* */
/* Redistribution and use in source and binary forms, with or without modification, */
/* are permitted provided that the following conditions are met: */
/* */
/* - Redistributions of source code must retain the above copyright notice, this */
/* list of conditions and the following disclaimer. */
/* */
/* - Redistributions in binary form must reproduce the above copyright notice, this */
/* list of conditions and the following disclaimer in the documentation and/or */
/* other materials provided with the distribution. */
/* */
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
/* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
/* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
/* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
/* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
/* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
/* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
/* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
/* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/* */
/**************************************************************************************/

#include <vlCore/VisualizationLibrary.hpp>
#include <vlSDL2/SDLWindow.hpp>
#include "Applets/App_RotatingCube.hpp"

using namespace vl;

int main(int argc, char* args[])
{
/* init Visualization Library */
VisualizationLibrary::init();

/* setup the OpenGL context format */
OpenGLContextFormat format;
format.setDoubleBuffer(true);
format.setRGBABits( 8,8,8,0 );
format.setDepthBufferBits(24);
format.setStencilBufferBits(8);
format.setFullscreen(false);
//format.setMultisampleSamples(16);
//format.setMultisample(true);

/* create the applet to be run */
ref<Applet> applet = new App_RotatingCube;
applet->initialize();
/* create a native SDL window */
ref<vlSDL2::SDLWindow> sdl_window = new vlSDL2::SDLWindow;
/* bind the applet so it receives all the GUI events related to the OpenGLContext */
sdl_window->addEventListener(applet.get());
/* target the window so we can render on it */
applet->rendering()->as<Rendering>()->renderer()->setFramebuffer( sdl_window->framebuffer() );
/* black background */
applet->rendering()->as<Rendering>()->camera()->viewport()->setClearColor( black );
/* define the camera position and orientation */
vec3 eye = vec3(0,10,35); // camera position
vec3 center = vec3(0,0,0); // point the camera is looking at
vec3 up = vec3(0,1,0); // up direction
mat4 view_mat = mat4::getLookAt(eye, center, up);
applet->rendering()->as<Rendering>()->camera()->setViewMatrix( view_mat );
/* Initialize the OpenGL context and window properties */
int x = 0;
int y = 0;
int width = 512;
int height= 512;
sdl_window->initSDLWindow( "Visualization Library on SDL - Rotating Cube", format, x, y, width, height );

/* run SDL message loop */
vlSDL2::messageLoop();

/* deallocate the window with all the OpenGL resources before shutting down Visualization Library */
sdl_window = NULL;

/* shutdown Visualization Library */
VisualizationLibrary::shutdown();

return 0;
}
// Have fun!
95 changes: 95 additions & 0 deletions src/examples/SDL2_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**************************************************************************************/
/* */
/* Visualization Library */
/* http://visualizationlibrary.org */
/* */
/* Copyright (c) 2005-2020, Michele Bosi */
/* All rights reserved. */
/* */
/* Redistribution and use in source and binary forms, with or without modification, */
/* are permitted provided that the following conditions are met: */
/* */
/* - Redistributions of source code must retain the above copyright notice, this */
/* list of conditions and the following disclaimer. */
/* */
/* - Redistributions in binary form must reproduce the above copyright notice, this */
/* list of conditions and the following disclaimer in the documentation and/or */
/* other materials provided with the distribution. */
/* */
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
/* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
/* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
/* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
/* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
/* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
/* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
/* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
/* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/* */
/**************************************************************************************/

#include <vlCore/DiskDirectory.hpp>
#include <vlSDL2/SDLWindow.hpp>
#include <vlCore/Log.hpp>
#include <vlCore/Say.hpp>
#include "tests.hpp"

using namespace vl;
using namespace vlSDL2;

class TestBatterySDL: public TestBattery
{
public:
void runGUI(const vl::String& title, BaseDemo* applet, vl::OpenGLContextFormat format, int x, int y, int width, int height, vl::fvec4 bk_color, vl::vec3 eye, vl::vec3 center)
{
/* used to display the application title next to FPS counter */
applet->setAppletName(title);

/* create a SDL window */
vl::ref<vlSDL2::SDLWindow> sdl_window = new vlSDL2::SDLWindow;

setupApplet(applet, sdl_window.get(), bk_color, eye, center);

/* Initialize the OpenGL context and window properties */
sdl_window->initSDLWindow( title, format, x, y, width, height );

/* show the window */
sdl_window->show();

/* run the SDL message loop */
vlSDL2::messageLoop();

/* deallocate the window with all the OpenGL resources before shutting down Visualization Library */
sdl_window = NULL;
}
};
//-----------------------------------------------------------------------------
int main(int argc, char* argv[])
{
/* parse command line arguments */
int test = -1;
std::string test_str;
if ( argc >= 2 )
{
test = atoi( argv[ 1 ] );
test_str = argv[ 1 ];
}

/* setup the OpenGL context format */
vl::OpenGLContextFormat format;
format.setDoubleBuffer(true);
format.setRGBABits( 8,8,8,0 );
format.setDepthBufferBits(24);
format.setStencilBufferBits(8);
format.setFullscreen(false);
// format.setMultisampleSamples(16);
// format.setMultisample(false);
// format.setOpenGLProfile( GLP_Core );
// format.setVersion( 3, 3 );

TestBatterySDL test_battery;
test_battery.run(test, test_str, format);

return 0;
}
38 changes: 38 additions & 0 deletions src/gui/vlSDL2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
################################################################################
# #
# Copyright (c) 2005-2020, Michele Bosi, Thiago Bastos #
# All rights reserved. #
# #
# This file is part of Visualization Library #
# http://visualizationlibrary.org #
# #
# Released under the OSI approved Simplified BSD License #
# http://www.opensource.org/licenses/bsd-license.php #
# #
################################################################################

################################################################################
# VLSDL2 Library
################################################################################

project(VLSDL2)

# Gather VLSDL2 source files
file(GLOB VLSDL2_SRC "*.cpp")
file(GLOB VLSDL2_INC "*.hpp")
# ${SDL2_LIBRARIES}, ${SDL2_INCLUDE_DIRS}
include_directories(${SDL2_INCLUDE_DIRS})

add_library(VLSDL2 ${VL_SHARED_OR_STATIC} ${VLSDL2_SRC} ${VLSDL2_INC})
VL_DEFAULT_TARGET_PROPERTIES(VLSDL2)

target_link_libraries(VLSDL2 VLMain ${SDL2_LIBRARIES})

################################################################################
# Install Rules
################################################################################

VL_INSTALL_TARGET(VLSDL2)

# VLSDL2 headers
install(FILES ${VLSDL2_INC} DESTINATION "${VL_INCLUDE_INSTALL_DIR}/vlSDL2")
Loading