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

Added SharedContext class that allows you to create a shared context … #103

Open
wants to merge 1 commit 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
60 changes: 60 additions & 0 deletions oxygine/src/core/SharedContext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "SharedContext.h"
#include <SDL_video.h>
#include "oxygine.h"
#include "gl/oxgl.h"

namespace oxygine
{
/** Allows the usage of shared OpenGL contexts. Needs to be called before core::init(). */
void SharedContext::Enable()
{
//SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
}

/** Creates a shared context. Can be called from the main thread. */
void SharedContext::create()
{
OX_ASSERT(_context == NULL);

SDL_GLContext currentContext = SDL_GL_GetCurrentContext();

OX_ASSERT(currentContext != NULL);

_context = SDL_GL_CreateContext( core::getWindow() );

CHECKGL();

OX_ASSERT(_context != NULL);

// restore the previous context
SDL_GL_MakeCurrent( core::getWindow(), currentContext );
}

/** Makes this context the current one. Must be called from within the thread that will use it. */
void SharedContext::makeCurrent()
{
OX_ASSERT(_context != NULL);

SDL_GL_MakeCurrent( core::getWindow(), _context );

CHECKGL();
}

/** The destructor which releases the context again. */
SharedContext::~SharedContext()
{
if(_context != NULL)
{
// disable the context if it is this one
if( SDL_GL_GetCurrentContext() == _context )
SDL_GL_MakeCurrent( core::getWindow(), NULL );

SDL_GL_DeleteContext(_context);

CHECKGL();

// just be safe in case of other destructor code accessing this object
_context = NULL;
}
}
}
60 changes: 60 additions & 0 deletions oxygine/src/core/SharedContext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#ifndef SHAREDCONTEXT_H
#define SHAREDCONTEXT_H

typedef void *SDL_GLContext;

namespace oxygine
{
/** This class creates a shared context that can be used to call OpenGL functions inside of other threads.
*
* Usage:
*
* main thread
* SharedContext::Enable();
*
* core::init();
*
* mythread.sharedContext.create();
*
* mythread
* void run()
* {
* sharedContext.makeCurrent();
*
* while(true) { }
* }
*/

class SharedContext
{
private:

/** The shared context that was created. */
SDL_GLContext _context;

public:

/** Allows the usage of shared OpenGL contexts. Needs to be called before core::init(). */
static void Enable();

/** Creates a shared context object with no OpenGL context being created yet. */
SharedContext() :
_context(0)
{
}

/** Returns true when the context was created. Can be used to wait for a thread to create its context. */
bool created() const { return _context != 0; }

/** Creates a shared context. Can be called from the main thread. */
void create();

/** Makes this context the current one. Must be called from within the thread that will use it. */
void makeCurrent();

/** The destructor which releases the context again. */
~SharedContext();
};
}

#endif // SHAREDCONTEXT_H
5 changes: 5 additions & 0 deletions oxygine/src/core/oxygine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,11 @@ namespace oxygine
return STDRenderer::isReady();
}

void makeContextCurrent()
{
SDL_GL_MakeCurrent(_window, _context);
}

bool beginRendering(window w)
{

Expand Down
4 changes: 4 additions & 0 deletions oxygine/src/core/oxygine.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ namespace oxygine

/**returns True if device is ready for rendering*/
bool isReady2Render();

/***makes the global context the current one. Handy for when sharing OpenGL contexts*/
void makeContextCurrent();

/**returns True if device is ready for rendering*/
bool beginRendering(window i = 0);

Expand Down