Skip to content

Commit

Permalink
SDL2: added opengl support
Browse files Browse the repository at this point in the history
  • Loading branch information
a1200 committed Sep 24, 2019
1 parent d6a4c26 commit 0088f79
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 36 deletions.
113 changes: 90 additions & 23 deletions source/Irrlicht/CIrrDeviceSDL2.cpp
Expand Up @@ -21,6 +21,21 @@
#include <SDL2/SDL_video.h>


namespace irr
{
namespace video
{

#ifdef _IRR_COMPILE_WITH_OPENGL_
IVideoDriver* createOpenGLDriver(const SIrrlichtCreationParameters& params,
io::IFileSystem* io, CIrrDeviceSDL* device);
#endif
} // end namespace video

} // end namespace irr



namespace irr
{

Expand Down Expand Up @@ -68,13 +83,34 @@ CIrrDeviceSDL::CIrrDeviceSDL(const SIrrlichtCreationParameters& param)
//! destructor
CIrrDeviceSDL::~CIrrDeviceSDL()
{
if (ScreenTexture)
{
SDL_DestroyTexture(ScreenTexture);
ScreenTexture = NULL;
}
SDL_DestroyRenderer(ScreenRenderer);
SDL_DestroyWindow(ScreenWindow);

if (CreationParams.DriverType == video::EDT_OPENGL)
{
if(glContext)
{
SDL_GL_DeleteContext(glContext);
glContext = NULL;
}
} else {

if (ScreenTexture)
{
SDL_DestroyTexture(ScreenTexture);
ScreenTexture = NULL;
}

if (ScreenRenderer)
{
SDL_DestroyRenderer(ScreenRenderer);
ScreenRenderer = NULL;
}
}

if(ScreenWindow)
{
SDL_DestroyWindow(ScreenWindow);
ScreenWindow = NULL;
}

SDL_Quit();

Expand All @@ -85,13 +121,21 @@ bool CIrrDeviceSDL::createWindow()
{
if ( Close )
return false;

int flags = 0;//SDL_WINDOW_FULLSCREEN;
ScreenWindow = SDL_CreateWindow("Untitled", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Width, Height, flags);
ScreenRenderer = SDL_CreateRenderer(ScreenWindow, -1, 0);//SDL_RENDERER_ACCELERATED);
ScreenTexture = SDL_CreateTexture(ScreenRenderer, SDL_PIXELFORMAT_ARGB1555, SDL_TEXTUREACCESS_STREAMING, Width, Height);


if (CreationParams.DriverType == video::EDT_OPENGL)
{
int flags = SDL_WINDOW_OPENGL;
ScreenWindow = SDL_CreateWindow( "SDLIrrlicht", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Width,Height, flags );
glContext = SDL_GL_CreateContext(ScreenWindow);
}

else
{
int flags = 0;//SDL_WINDOW_FULLSCREEN;
ScreenWindow = SDL_CreateWindow("SDLIrrlicht", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Width, Height, flags);
ScreenRenderer = SDL_CreateRenderer(ScreenWindow, -1, 0);//SDL_RENDERER_ACCELERATED);
ScreenTexture = SDL_CreateTexture(ScreenRenderer, SDL_PIXELFORMAT_ARGB1555, SDL_TEXTUREACCESS_STREAMING, Width, Height);
}

return true;
}
Expand All @@ -110,11 +154,30 @@ void CIrrDeviceSDL::createDriver()
#endif
break;

case video::EDT_BURNINGSVIDEO:
#ifdef _IRR_COMPILE_WITH_BURNINGSVIDEO_
VideoDriver = video::createBurningVideoDriver(CreationParams, FileSystem, this);
#else
os::Printer::log("Burning's video driver was not compiled in.", ELL_ERROR);
#endif
break;

case video::EDT_OPENGL:
#ifdef _IRR_COMPILE_WITH_OPENGL_
VideoDriver = video::createOpenGLDriver(CreationParams, FileSystem, this);
#else
os::Printer::log("No OpenGL support compiled in.", ELL_ERROR);
#endif
break;

case video::EDT_NULL:
VideoDriver = video::createNullDriver(FileSystem, CreationParams.WindowSize);
break;

default:
os::Printer::log("Unable to create video driver of unknown type.", ELL_ERROR);
break;
}

}


Expand Down Expand Up @@ -211,16 +274,20 @@ void CIrrDeviceSDL::setWindowCaption(const wchar_t* text)
//! presents a surface in the client area
bool CIrrDeviceSDL::present(video::IImage* surface, void* windowId, core::rect<s32>* srcClip)
{

if (SDL_UpdateTexture(ScreenTexture, NULL /* update whole texture */, surface->lock(), surface->getPitch()) != 0) {
// SDL_GetError
}
if (CreationParams.DriverType == video::EDT_OPENGL) {
//..
} else {
// software rendering

if (SDL_UpdateTexture(ScreenTexture, NULL /* update whole texture */, surface->lock(), surface->getPitch()) != 0) {
// SDL_GetError
}

if (SDL_RenderCopy(ScreenRenderer, ScreenTexture, NULL, NULL) != 0) {
// SDL_GetError
}
SDL_RenderPresent(ScreenRenderer);

if (SDL_RenderCopy(ScreenRenderer, ScreenTexture, NULL, NULL) != 0) {
// SDL_GetError
}
SDL_RenderPresent(ScreenRenderer);
}

return true;
}
Expand Down
9 changes: 6 additions & 3 deletions source/Irrlicht/CIrrDeviceSDL2.h
Expand Up @@ -179,6 +179,12 @@ namespace irr
core::position2d<s32> CursorPos;
bool IsVisible;
};
public:

SDL_GLContext glContext;
SDL_Window* ScreenWindow;
SDL_Texture* ScreenTexture;
SDL_Renderer* ScreenRenderer;

private:

Expand All @@ -187,9 +193,6 @@ namespace irr

bool createWindow();

SDL_Window* ScreenWindow;
SDL_Texture* ScreenTexture;
SDL_Renderer* ScreenRenderer;
bool Fullscreen;

#if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
Expand Down
30 changes: 22 additions & 8 deletions source/Irrlicht/COpenGLDriver.cpp
Expand Up @@ -26,6 +26,12 @@
#endif


#ifdef _IRR_COMPILE_WITH_SDL2_DEVICE_
#include <SDL2/SDL.h>
#include "CIrrDeviceSDL2.h"
#endif


namespace irr
{
namespace video
Expand Down Expand Up @@ -591,11 +597,10 @@ bool COpenGLDriver::initDriver(CIrrDeviceLinux* device)
// -----------------------------------------------------------------------
// SDL CONSTRUCTOR
// -----------------------------------------------------------------------
#ifdef _IRR_COMPILE_WITH_SDL_DEVICE_
#if defined (_IRR_COMPILE_WITH_SDL_DEVICE_) || defined (_IRR_COMPILE_WITH_SDL2_DEVICE_)
//! SDL constructor and init code
COpenGLDriver::COpenGLDriver(const SIrrlichtCreationParameters& params,
io::IFileSystem* io, CIrrDeviceSDL* device)
: CNullDriver(io, params.WindowSize), COpenGLExtensionHandler(),
COpenGLDriver::COpenGLDriver(const SIrrlichtCreationParameters& params, io::IFileSystem* io, CIrrDeviceSDL* device)
: CNullDriver(io, params.WindowSize), COpenGLExtensionHandler(),
CurrentRenderMode(ERM_NONE), ResetRenderStates(true),
Transformation3DChanged(true), AntiAlias(params.AntiAlias),
RenderTargetTexture(0), CurrentRendertargetSize(0,0), ColorFormat(ECF_R8G8B8),
Expand All @@ -613,7 +618,7 @@ COpenGLDriver::COpenGLDriver(const SIrrlichtCreationParameters& params,
genericDriverInit();
}

#endif // _IRR_COMPILE_WITH_SDL_DEVICE_
#endif // _IRR_COMPILE_WITH_SDL_DEVICE_ || _IRR_COMPILE_WITH_SDL2_DEVICE_


//! destructor
Expand Down Expand Up @@ -861,6 +866,15 @@ bool COpenGLDriver::endScene()
}
#endif

#ifdef _IRR_COMPILE_WITH_SDL2_DEVICE_
if (DeviceType == EIDT_SDL)
{
SDL_GL_SwapWindow(SDLDevice->ScreenWindow);
return true;
}
#endif


// todo: console device present

return false;
Expand Down Expand Up @@ -918,7 +932,7 @@ bool COpenGLDriver::beginScene(bool backBuffer, bool zBuffer, SColor color,
break;
}

#if defined(_IRR_COMPILE_WITH_SDL_DEVICE_)
#if defined(_IRR_COMPILE_WITH_SDL_DEVICE_) || defined(_IRR_COMPILE_WITH_SDL2_DEVICE_)
if (DeviceType == EIDT_SDL)
{
// todo: SDL sets glFrontFace(GL_CCW) after driver creation,
Expand Down Expand Up @@ -4814,7 +4828,7 @@ IVideoDriver* createOpenGLDriver(const SIrrlichtCreationParameters& params,
// -----------------------------------
// SDL VERSION
// -----------------------------------
#ifdef _IRR_COMPILE_WITH_SDL_DEVICE_
#if defined (_IRR_COMPILE_WITH_SDL_DEVICE_) || defined (_IRR_COMPILE_WITH_SDL2_DEVICE_)
IVideoDriver* createOpenGLDriver(const SIrrlichtCreationParameters& params,
io::IFileSystem* io, CIrrDeviceSDL* device)
{
Expand All @@ -4824,7 +4838,7 @@ IVideoDriver* createOpenGLDriver(const SIrrlichtCreationParameters& params,
return 0;
#endif // _IRR_COMPILE_WITH_OPENGL_
}
#endif // _IRR_COMPILE_WITH_SDL_DEVICE_
#endif // _IRR_COMPILE_WITH_SDL_DEVICE_ || IRR_COMPILE_WITH_SDL2_DEVICE_

} // end namespace
} // end namespace
Expand Down
4 changes: 2 additions & 2 deletions source/Irrlicht/COpenGLDriver.h
Expand Up @@ -54,7 +54,7 @@ namespace video
bool changeRenderContext(const SExposedVideoData& videoData, CIrrDeviceLinux* device);
#endif

#ifdef _IRR_COMPILE_WITH_SDL_DEVICE_
#if defined (_IRR_COMPILE_WITH_SDL_DEVICE_) || defined (_IRR_COMPILE_WITH_SDL2_DEVICE_)
COpenGLDriver(const SIrrlichtCreationParameters& params, io::IFileSystem* io, CIrrDeviceSDL* device);
#endif

Expand Down Expand Up @@ -594,7 +594,7 @@ namespace video
#ifdef _IRR_COMPILE_WITH_OSX_DEVICE_
CIrrDeviceMacOSX *OSXDevice;
#endif
#ifdef _IRR_COMPILE_WITH_SDL_DEVICE_
#if defined (_IRR_COMPILE_WITH_SDL_DEVICE_) || defined (_IRR_COMPILE_WITH_SDL2_DEVICE_)
CIrrDeviceSDL *SDLDevice;
#endif
#ifdef _IRR_COMPILE_WITH_CG_
Expand Down

0 comments on commit 0088f79

Please sign in to comment.