cleaned up the code. fixed so that SDL is inited on main thread and then transfers the gl context to the render thread

This commit is contained in:
Magnus Auvinen 2012-01-03 21:39:10 +01:00
parent e59b24d8db
commit 50d872531a
9 changed files with 772 additions and 578 deletions

View file

@ -0,0 +1,495 @@
#include "SDL.h"
#include "SDL_opengl.h"
#include "graphics_threaded.h"
#include "backend_sdl.h"
// ------------ CGraphicsBackend_Threaded
void CGraphicsBackend_Threaded::ThreadFunc(void *pUser)
{
CGraphicsBackend_Threaded *pThis = (CGraphicsBackend_Threaded *)pUser;
while(!pThis->m_Shutdown)
{
pThis->m_Activity.wait();
if(pThis->m_pBuffer)
{
pThis->m_pProcessor->RunBuffer(pThis->m_pBuffer);
sync_barrier();
pThis->m_pBuffer = 0x0;
pThis->m_BufferDone.signal();
}
}
}
CGraphicsBackend_Threaded::CGraphicsBackend_Threaded()
{
m_pBuffer = 0x0;
m_pProcessor = 0x0;
m_pThread = 0x0;
}
void CGraphicsBackend_Threaded::StartProcessor(ICommandProcessor *pProcessor)
{
m_Shutdown = false;
m_pProcessor = pProcessor;
m_pThread = thread_create(ThreadFunc, this);
m_BufferDone.signal();
}
void CGraphicsBackend_Threaded::StopProcessor()
{
m_Shutdown = true;
m_Activity.signal();
thread_wait(m_pThread);
thread_destroy(m_pThread);
}
void CGraphicsBackend_Threaded::RunBuffer(CCommandBuffer *pBuffer)
{
WaitForIdle();
m_pBuffer = pBuffer;
m_Activity.signal();
}
bool CGraphicsBackend_Threaded::IsIdle() const
{
return m_pBuffer == 0x0;
}
void CGraphicsBackend_Threaded::WaitForIdle()
{
while(m_pBuffer != 0x0)
m_BufferDone.wait();
}
// ------------ CCommandProcessorFragment_General
void CCommandProcessorFragment_General::Cmd_Signal(const CCommandBuffer::SCommand_Signal *pCommand)
{
pCommand->m_pSemaphore->signal();
}
bool CCommandProcessorFragment_General::RunCommand(const CCommandBuffer::SCommand * pBaseCommand)
{
switch(pBaseCommand->m_Cmd)
{
case CCommandBuffer::CMD_NOP: break;
case CCommandBuffer::CMD_SIGNAL: Cmd_Signal(static_cast<const CCommandBuffer::SCommand_Signal *>(pBaseCommand)); break;
default: return false;
}
return true;
}
// ------------ CCommandProcessorFragment_OpenGL
int CCommandProcessorFragment_OpenGL::TexFormatToOpenGLFormat(int TexFormat)
{
if(TexFormat == CCommandBuffer::TEXFORMAT_RGB) return GL_RGB;
if(TexFormat == CCommandBuffer::TEXFORMAT_ALPHA) return GL_ALPHA;
if(TexFormat == CCommandBuffer::TEXFORMAT_RGBA) return GL_RGBA;
return GL_RGBA;
}
void CCommandProcessorFragment_OpenGL::SetState(const CCommandBuffer::SState &State)
{
// blend
switch(State.m_BlendMode)
{
case CCommandBuffer::BLEND_NONE:
glDisable(GL_BLEND);
break;
case CCommandBuffer::BLEND_ALPHA:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
break;
case CCommandBuffer::BLEND_ADDITIVE:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
break;
default:
dbg_msg("render", "unknown blendmode %d\n", State.m_BlendMode);
};
// clip
if(State.m_ClipEnable)
{
glScissor(State.m_ClipX, State.m_ClipY, State.m_ClipW, State.m_ClipH);
glEnable(GL_SCISSOR_TEST);
}
else
glDisable(GL_SCISSOR_TEST);
// texture
if(State.m_Texture >= 0 && State.m_Texture < CCommandBuffer::MAX_TEXTURES)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_aTextures[State.m_Texture]);
}
else
glDisable(GL_TEXTURE_2D);
// screen mapping
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(State.m_ScreenTL.x, State.m_ScreenBR.x, State.m_ScreenBR.y, State.m_ScreenTL.y, 1.0f, 10.f);
}
void CCommandProcessorFragment_OpenGL::Cmd_Texture_Update(const CCommandBuffer::SCommand_Texture_Update *pCommand)
{
glBindTexture(GL_TEXTURE_2D, m_aTextures[pCommand->m_Slot]);
glTexSubImage2D(GL_TEXTURE_2D, 0, pCommand->m_X, pCommand->m_Y, pCommand->m_Width, pCommand->m_Height,
TexFormatToOpenGLFormat(pCommand->m_Format), GL_UNSIGNED_BYTE, pCommand->m_pData);
mem_free(pCommand->m_pData);
}
void CCommandProcessorFragment_OpenGL::Cmd_Texture_Destroy(const CCommandBuffer::SCommand_Texture_Destroy *pCommand)
{
glDeleteTextures(1, &m_aTextures[pCommand->m_Slot]);
}
void CCommandProcessorFragment_OpenGL::Cmd_Texture_Create(const CCommandBuffer::SCommand_Texture_Create *pCommand)
{
int Oglformat = TexFormatToOpenGLFormat(pCommand->m_Format);
int StoreOglformat = TexFormatToOpenGLFormat(pCommand->m_StoreFormat);
glGenTextures(1, &m_aTextures[pCommand->m_Slot]);
glBindTexture(GL_TEXTURE_2D, m_aTextures[pCommand->m_Slot]);
if(pCommand->m_Flags&CCommandBuffer::TEXFLAG_NOMIPMAPS)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, StoreOglformat, pCommand->m_Width, pCommand->m_Height, 0, Oglformat, GL_UNSIGNED_BYTE, pCommand->m_pData);
}
else
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, StoreOglformat, pCommand->m_Width, pCommand->m_Height, Oglformat, GL_UNSIGNED_BYTE, pCommand->m_pData);
}
mem_free(pCommand->m_pData);
}
void CCommandProcessorFragment_OpenGL::Cmd_Clear(const CCommandBuffer::SCommand_Clear *pCommand)
{
glClearColor(pCommand->m_Color.r, pCommand->m_Color.g, pCommand->m_Color.b, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void CCommandProcessorFragment_OpenGL::Cmd_Render(const CCommandBuffer::SCommand_Render *pCommand)
{
SetState(pCommand->m_State);
glVertexPointer(3, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices);
glTexCoordPointer(2, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices + sizeof(float)*3);
glColorPointer(4, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices + sizeof(float)*5);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
switch(pCommand->m_PrimType)
{
case CCommandBuffer::PRIMTYPE_QUADS:
glDrawArrays(GL_QUADS, 0, pCommand->m_PrimCount*4);
break;
case CCommandBuffer::PRIMTYPE_LINES:
glDrawArrays(GL_LINES, 0, pCommand->m_PrimCount*2);
break;
default:
dbg_msg("render", "unknown primtype %d\n", pCommand->m_Cmd);
};
}
void CCommandProcessorFragment_OpenGL::Cmd_Screenshot(const CCommandBuffer::SCommand_Screenshot *pCommand)
{
// fetch image data
GLint aViewport[4] = {0,0,0,0};
glGetIntegerv(GL_VIEWPORT, aViewport);
int w = aViewport[2];
int h = aViewport[3];
// we allocate one more row to use when we are flipping the texture
unsigned char *pPixelData = (unsigned char *)mem_alloc(w*(h+1)*3, 1);
unsigned char *pTempRow = pPixelData+w*h*3;
// fetch the pixels
GLint Alignment;
glGetIntegerv(GL_PACK_ALIGNMENT, &Alignment);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0,0, w, h, GL_RGB, GL_UNSIGNED_BYTE, pPixelData);
glPixelStorei(GL_PACK_ALIGNMENT, Alignment);
// flip the pixel because opengl works from bottom left corner
for(int y = 0; y < h/2; y++)
{
mem_copy(pTempRow, pPixelData+y*w*3, w*3);
mem_copy(pPixelData+y*w*3, pPixelData+(h-y-1)*w*3, w*3);
mem_copy(pPixelData+(h-y-1)*w*3, pTempRow,w*3);
}
// fill in the information
pCommand->m_pImage->m_Width = w;
pCommand->m_pImage->m_Height = h;
pCommand->m_pImage->m_Format = CImageInfo::FORMAT_RGB;
pCommand->m_pImage->m_pData = pPixelData;
}
CCommandProcessorFragment_OpenGL::CCommandProcessorFragment_OpenGL()
{
mem_zero(m_aTextures, sizeof(m_aTextures));
}
bool CCommandProcessorFragment_OpenGL::RunCommand(const CCommandBuffer::SCommand * pBaseCommand)
{
switch(pBaseCommand->m_Cmd)
{
case CCommandBuffer::CMD_TEXTURE_CREATE: Cmd_Texture_Create(static_cast<const CCommandBuffer::SCommand_Texture_Create *>(pBaseCommand)); break;
case CCommandBuffer::CMD_TEXTURE_DESTROY: Cmd_Texture_Destroy(static_cast<const CCommandBuffer::SCommand_Texture_Destroy *>(pBaseCommand)); break;
case CCommandBuffer::CMD_TEXTURE_UPDATE: Cmd_Texture_Update(static_cast<const CCommandBuffer::SCommand_Texture_Update *>(pBaseCommand)); break;
case CCommandBuffer::CMD_CLEAR: Cmd_Clear(static_cast<const CCommandBuffer::SCommand_Clear *>(pBaseCommand)); break;
case CCommandBuffer::CMD_RENDER: Cmd_Render(static_cast<const CCommandBuffer::SCommand_Render *>(pBaseCommand)); break;
case CCommandBuffer::CMD_SCREENSHOT: Cmd_Screenshot(static_cast<const CCommandBuffer::SCommand_Screenshot *>(pBaseCommand)); break;
default: return false;
}
return true;
}
// ------------ CCommandProcessorFragment_SDL
void CCommandProcessorFragment_SDL::Cmd_Init(const SCommand_Init *pCommand)
{
m_GLContext = pCommand->m_Context;
GL_MakeCurrent(m_GLContext);
// set some default settings
glEnable(GL_BLEND);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glAlphaFunc(GL_GREATER, 0);
glEnable(GL_ALPHA_TEST);
glDepthMask(0);
}
void CCommandProcessorFragment_SDL::Cmd_Shutdown(const SCommand_Shutdown *pCommand)
{
GL_ReleaseContext(m_GLContext);
}
void CCommandProcessorFragment_SDL::Cmd_Swap(const CCommandBuffer::SCommand_Swap *pCommand)
{
GL_SwapBuffers(m_GLContext);
}
void CCommandProcessorFragment_SDL::Cmd_VideoModes(const CCommandBuffer::SCommand_VideoModes *pCommand)
{
// TODO: fix this code on osx or windows
SDL_Rect **ppModes = SDL_ListModes(NULL, SDL_OPENGL|SDL_GL_DOUBLEBUFFER|SDL_FULLSCREEN);
if(ppModes == NULL)
{
// no modes
*pCommand->m_pNumModes = 0;
}
else if(ppModes == (SDL_Rect**)-1)
{
// no modes
*pCommand->m_pNumModes = 0;
}
else
{
int NumModes = 0;
for(int i = 0; ppModes[i]; ++i)
{
if(NumModes == pCommand->m_MaxModes)
break;
pCommand->m_pModes[NumModes].m_Width = ppModes[i]->w;
pCommand->m_pModes[NumModes].m_Height = ppModes[i]->h;
pCommand->m_pModes[NumModes].m_Red = 8;
pCommand->m_pModes[NumModes].m_Green = 8;
pCommand->m_pModes[NumModes].m_Blue = 8;
NumModes++;
}
*pCommand->m_pNumModes = NumModes;
}
}
CCommandProcessorFragment_SDL::CCommandProcessorFragment_SDL()
{
}
bool CCommandProcessorFragment_SDL::RunCommand(const CCommandBuffer::SCommand *pBaseCommand)
{
switch(pBaseCommand->m_Cmd)
{
case CCommandBuffer::CMD_SWAP: Cmd_Swap(static_cast<const CCommandBuffer::SCommand_Swap *>(pBaseCommand)); break;
case CCommandBuffer::CMD_VIDEOMODES: Cmd_VideoModes(static_cast<const CCommandBuffer::SCommand_VideoModes *>(pBaseCommand)); break;
case CMD_INIT: Cmd_Init(static_cast<const SCommand_Init *>(pBaseCommand)); break;
case CMD_SHUTDOWN: Cmd_Shutdown(static_cast<const SCommand_Shutdown *>(pBaseCommand)); break;
default: return false;
}
return true;
}
// ------------ CCommandProcessor_SDL_OpenGL
void CCommandProcessor_SDL_OpenGL::RunBuffer(CCommandBuffer *pBuffer)
{
unsigned CmdIndex = 0;
while(1)
{
const CCommandBuffer::SCommand *pBaseCommand = pBuffer->GetCommand(&CmdIndex);
if(pBaseCommand == 0x0)
break;
if(m_OpenGL.RunCommand(pBaseCommand))
continue;
if(m_SDL.RunCommand(pBaseCommand))
continue;
if(m_General.RunCommand(pBaseCommand))
continue;
dbg_msg("graphics", "unknown command %d", pBaseCommand->m_Cmd);
}
}
// ------------ CGraphicsBackend_SDL_OpenGL
int CGraphicsBackend_SDL_OpenGL::Init(const char *pName, int Width, int Height, int FsaaSamples, int Flags)
{
if(!SDL_WasInit(SDL_INIT_VIDEO))
{
if(SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
{
dbg_msg("gfx", "unable to init SDL video: %s", SDL_GetError());
return -1;
}
#ifdef CONF_FAMILY_WINDOWS
if(!getenv("SDL_VIDEO_WINDOW_POS") && !getenv("SDL_VIDEO_CENTERED")) // ignore_convention
putenv("SDL_VIDEO_WINDOW_POS=8,27"); // ignore_convention
#endif
}
const SDL_VideoInfo *pInfo = SDL_GetVideoInfo();
// set flags
int SdlFlags = SDL_OPENGL;
if(Flags&IGraphicsBackend::INITFLAG_RESIZABLE)
SdlFlags |= SDL_RESIZABLE;
if(pInfo->hw_available) // ignore_convention
SdlFlags |= SDL_HWSURFACE;
else
SdlFlags |= SDL_SWSURFACE;
if(pInfo->blit_hw) // ignore_convention
SdlFlags |= SDL_HWACCEL;
if(Flags&IGraphicsBackend::INITFLAG_FULLSCREEN)
SdlFlags |= SDL_FULLSCREEN;
// set gl attributes
if(FsaaSamples)
{
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, FsaaSamples);
}
else
{
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
}
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, Flags&CCommandBuffer::INITFLAG_VSYNC ? 1 : 0);
// set caption
SDL_WM_SetCaption(pName, pName);
// create window
m_pScreenSurface = SDL_SetVideoMode(Width, Height, 0, SdlFlags);
if(!m_pScreenSurface)
{
dbg_msg("gfx", "unable to set video mode: %s", SDL_GetError());
//*pCommand->m_pResult = -1;
return -1;
}
SDL_ShowCursor(0);
// fetch gl contexts and release the context from this thread
m_GLContext = GL_GetCurrentContext();
GL_ReleaseContext(m_GLContext);
// start the command processor
m_pProcessor = new CCommandProcessor_SDL_OpenGL;
StartProcessor(m_pProcessor);
// issue a init command
CCommandBuffer CmdBuffer(1024, 512);
CCommandProcessorFragment_SDL::SCommand_Init Cmd;
Cmd.m_Context = m_GLContext;
CmdBuffer.AddCommand(Cmd);
RunBuffer(&CmdBuffer);
WaitForIdle();
// return
return 0;
}
int CGraphicsBackend_SDL_OpenGL::Shutdown()
{
// issue a shutdown command
CCommandBuffer CmdBuffer(1024, 512);
CCommandProcessorFragment_SDL::SCommand_Shutdown Cmd;
CmdBuffer.AddCommand(Cmd);
RunBuffer(&CmdBuffer);
WaitForIdle();
// stop and delete the processor
StopProcessor();
delete m_pProcessor;
m_pProcessor = 0;
SDL_QuitSubSystem(SDL_INIT_VIDEO);
return 0;
}
void CGraphicsBackend_SDL_OpenGL::Minimize()
{
SDL_WM_IconifyWindow();
}
void CGraphicsBackend_SDL_OpenGL::Maximize()
{
// TODO: SDL
}
int CGraphicsBackend_SDL_OpenGL::WindowActive()
{
return SDL_GetAppState()&SDL_APPINPUTFOCUS;
}
int CGraphicsBackend_SDL_OpenGL::WindowOpen()
{
return SDL_GetAppState()&SDL_APPACTIVE;
}
IGraphicsBackend *CreateGraphicsBackend() { return new CGraphicsBackend_SDL_OpenGL; }

View file

@ -0,0 +1,180 @@
#include "SDL.h"
#include "SDL_opengl.h"
#include "graphics_threaded.h"
// platform dependent implementations for transfering render context from the main thread to the graphics thread
// TODO: when SDL 1.3 comes, this can be removed
#if defined(CONF_FAMILY_WINDOWS)
struct SGLContext
{
HDC m_hDC;
HGLRC m_hGLRC;
};
static SGLContext GL_GetCurrentContext()
{
SGLContext Context;
Context.m_hDC = wglGetCurrentDC();
Context.m_hGLRC = wglGetCurrentContext();
return Context;
}
static void GL_MakeCurrent(const SGLContext &Context) { wglMakeCurrent(Context.m_hDC, Context.m_hGLRC); }
static void GL_ReleaseContext(const SGLContext &Context) { wglMakeCurrent(Context.m_hDC, NULL); }
static void GL_SwapBuffers(const SGLContext &Context) { SwapBuffers(Context.m_hDC); }
#elif defined(CONF_PLATFORM_MACOSX)
#error missing implementation
#elif defined(CONF_FAMILY_UNIX)
#include <GL/glx.h>
struct SGLContext
{
Display *m_pDisplay;
GLXDrawable m_Drawable;
GLXContext m_Context;
};
static SGLContext GL_GetCurrentContext()
{
SGLContext Context;
Context.m_pDisplay = glXGetCurrentDisplay();
Context.m_Drawable = glXGetCurrentDrawable();
Context.m_Context = glXGetCurrentContext();
return Context;
}
static void GL_MakeCurrent(const SGLContext &Context) { glXMakeCurrent(Context.m_pDisplay, Context.m_Drawable, Context.m_Context); }
static void GL_ReleaseContext(const SGLContext &Context) { glXMakeCurrent(Context.m_pDisplay, None, 0x0); }
static void GL_SwapBuffers(const SGLContext &Context) { glXSwapBuffers(Context.m_pDisplay, Context.m_Drawable); }
#else
#error missing implementation
#endif
// basic threaded backend, abstract, missing init and shutdown functions
class CGraphicsBackend_Threaded : public IGraphicsBackend
{
public:
// constructed on the main thread, the rest of the functions is runned on the render thread
class ICommandProcessor
{
public:
virtual ~ICommandProcessor() {}
virtual void RunBuffer(CCommandBuffer *pBuffer) = 0;
};
CGraphicsBackend_Threaded();
virtual void RunBuffer(CCommandBuffer *pBuffer);
virtual bool IsIdle() const;
virtual void WaitForIdle();
protected:
void StartProcessor(ICommandProcessor *pProcessor);
void StopProcessor();
private:
ICommandProcessor *m_pProcessor;
CCommandBuffer * volatile m_pBuffer;
volatile bool m_Shutdown;
semaphore m_Activity;
semaphore m_BufferDone;
void *m_pThread;
static void ThreadFunc(void *pUser);
};
// takes care of implementation independent operations
class CCommandProcessorFragment_General
{
void Cmd_Nop();
void Cmd_Signal(const CCommandBuffer::SCommand_Signal *pCommand);
public:
bool RunCommand(const CCommandBuffer::SCommand * pBaseCommand);
};
// takes care of opengl related rendering
class CCommandProcessorFragment_OpenGL
{
GLuint m_aTextures[CCommandBuffer::MAX_TEXTURES];
static int TexFormatToOpenGLFormat(int TexFormat);
void SetState(const CCommandBuffer::SState &State);
void Cmd_Texture_Update(const CCommandBuffer::SCommand_Texture_Update *pCommand);
void Cmd_Texture_Destroy(const CCommandBuffer::SCommand_Texture_Destroy *pCommand);
void Cmd_Texture_Create(const CCommandBuffer::SCommand_Texture_Create *pCommand);
void Cmd_Clear(const CCommandBuffer::SCommand_Clear *pCommand);
void Cmd_Render(const CCommandBuffer::SCommand_Render *pCommand);
void Cmd_Screenshot(const CCommandBuffer::SCommand_Screenshot *pCommand);
public:
CCommandProcessorFragment_OpenGL();
bool RunCommand(const CCommandBuffer::SCommand * pBaseCommand);
};
// takes care of sdl related commands
class CCommandProcessorFragment_SDL
{
// SDL stuff
SGLContext m_GLContext;
public:
enum
{
CMD_INIT = CCommandBuffer::CMDGROUP_PLATFORM,
CMD_SHUTDOWN,
};
struct SCommand_Init : public CCommandBuffer::SCommand
{
SCommand_Init() : SCommand(CMD_INIT) {}
SGLContext m_Context;
};
struct SCommand_Shutdown : public CCommandBuffer::SCommand
{
SCommand_Shutdown() : SCommand(CMD_SHUTDOWN) {}
};
private:
void Cmd_Init(const SCommand_Init *pCommand);
void Cmd_Shutdown(const SCommand_Shutdown *pCommand);
void Cmd_Swap(const CCommandBuffer::SCommand_Swap *pCommand);
void Cmd_VideoModes(const CCommandBuffer::SCommand_VideoModes *pCommand);
public:
CCommandProcessorFragment_SDL();
bool RunCommand(const CCommandBuffer::SCommand *pBaseCommand);
};
// command processor impelementation, uses the fragments to combine into one processor
class CCommandProcessor_SDL_OpenGL : public CGraphicsBackend_Threaded::ICommandProcessor
{
CCommandProcessorFragment_OpenGL m_OpenGL;
CCommandProcessorFragment_SDL m_SDL;
CCommandProcessorFragment_General m_General;
public:
virtual void RunBuffer(CCommandBuffer *pBuffer);
};
// graphics backend implemented with SDL and OpenGL
class CGraphicsBackend_SDL_OpenGL : public CGraphicsBackend_Threaded
{
SDL_Surface *m_pScreenSurface;
ICommandProcessor *m_pProcessor;
SGLContext m_GLContext;
public:
virtual int Init(const char *pName, int Width, int Height, int FsaaSamples, int Flags);
virtual int Shutdown();
virtual void Minimize();
virtual void Maximize();
virtual int WindowActive();
virtual int WindowOpen();
};

View file

@ -1690,11 +1690,24 @@ void CClient::InitInterfaces()
m_Friends.Init();
}
#include "SDL.h"
void CClient::Run()
{
m_LocalStartTime = time_get();
m_SnapshotParts = 0;
// init SDL
{
if(SDL_Init(0) < 0)
{
dbg_msg("client", "unable to init SDL base: %s", SDL_GetError());
return;
}
atexit(SDL_Quit); // ignore_convention
}
// init graphics
{
if(g_Config.m_GfxThreaded)
@ -1713,6 +1726,9 @@ void CClient::Run()
}
}
// init sound, allowed to fail
m_SoundInitFailed = Sound()->Init() != 0;
// open socket
{
NETADDR BindAddr;
@ -1737,8 +1753,6 @@ void CClient::Run()
// init the editor
//m_pEditor->Init();
// init sound, allowed to fail
m_SoundInitFailed = Sound()->Init() != 0;
// load data
if(!LoadData())
@ -1937,6 +1951,11 @@ void CClient::Run()
m_pGraphics->Shutdown();
m_pSound->Shutdown();
// shutdown SDL
{
SDL_Quit();
}
}

View file

@ -707,7 +707,7 @@ void CGraphics_OpenGL::QuadsText(float x, float y, float Size, float r, float g,
QuadsEnd();
}
bool CGraphics_OpenGL::Init()
int CGraphics_OpenGL::Init()
{
m_pStorage = Kernel()->RequestInterface<IStorage>();
m_pConsole = Kernel()->RequestInterface<IConsole>();
@ -743,7 +743,7 @@ bool CGraphics_OpenGL::Init()
m_InvalidTexture = LoadTextureRaw(4,4,CImageInfo::FORMAT_RGBA,aNullTextureData,CImageInfo::FORMAT_RGBA,TEXLOAD_NORESAMPLE);
return true;
return 0;
}
int CGraphics_SDL::TryInit()
@ -841,7 +841,7 @@ CGraphics_SDL::CGraphics_SDL()
m_pScreenSurface = 0;
}
bool CGraphics_SDL::Init()
int CGraphics_SDL::Init()
{
{
int Systems = SDL_INIT_VIDEO;
@ -855,7 +855,7 @@ bool CGraphics_SDL::Init()
if(SDL_Init(Systems) < 0)
{
dbg_msg("gfx", "unable to init SDL: %s", SDL_GetError());
return true;
return -1;
}
}
@ -867,14 +867,14 @@ bool CGraphics_SDL::Init()
#endif
if(InitWindow() != 0)
return true;
return -1;
SDL_ShowCursor(0);
CGraphics_OpenGL::Init();
MapScreen(0,0,g_Config.m_GfxScreenWidth, g_Config.m_GfxScreenHeight);
return false;
return 0;
}
void CGraphics_SDL::Shutdown()

View file

@ -118,7 +118,7 @@ public:
virtual void QuadsDrawFreeform(const CFreeformItem *pArray, int Num);
virtual void QuadsText(float x, float y, float Size, float r, float g, float b, float a, const char *pText);
virtual bool Init();
virtual int Init();
};
class CGraphics_SDL : public CGraphics_OpenGL
@ -130,7 +130,7 @@ class CGraphics_SDL : public CGraphics_OpenGL
public:
CGraphics_SDL();
virtual bool Init();
virtual int Init();
virtual void Shutdown();
virtual void Minimize();

View file

@ -4,9 +4,6 @@
#include <base/detect.h>
#include <base/math.h>
#include "SDL.h"
#include "SDL_opengl.h"
#include <base/system.h>
#include <engine/external/pnglite/pnglite.h>
@ -20,7 +17,6 @@
#include "graphics_threaded.h"
static CVideoMode g_aFakeModes[] = {
{320,240,8,8,8}, {400,300,8,8,8}, {640,480,8,8,8},
{720,400,8,8,8}, {768,576,8,8,8}, {800,600,8,8,8},
@ -45,469 +41,6 @@ static CVideoMode g_aFakeModes[] = {
{2048,1536,5,6,5}
};
class CCommandProcessorFragment_General
{
public:
void Cmd_Signal(const CCommandBuffer::SCommand_Signal *pCommand)
{
pCommand->m_pSemaphore->signal();
}
bool RunCommand(const CCommandBuffer::SCommand * pBaseCommand)
{
switch(pBaseCommand->m_Cmd)
{
case CCommandBuffer::CMD_NOP: break;
case CCommandBuffer::CMD_SIGNAL: Cmd_Signal(static_cast<const CCommandBuffer::SCommand_Signal *>(pBaseCommand)); break;
default: return false;
}
return true;
}
};
class CCommandProcessorFragment_OpenGL
{
GLuint m_aTextures[CCommandBuffer::MAX_TEXTURES];
void SetState(const CCommandBuffer::SState &State)
{
// blend
switch(State.m_BlendMode)
{
case CCommandBuffer::BLEND_NONE:
glDisable(GL_BLEND);
break;
case CCommandBuffer::BLEND_ALPHA:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
break;
case CCommandBuffer::BLEND_ADDITIVE:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
break;
default:
dbg_msg("render", "unknown blendmode %d\n", State.m_BlendMode);
};
// clip
if(State.m_ClipEnable)
{
glScissor(State.m_ClipX, State.m_ClipY, State.m_ClipW, State.m_ClipH);
glEnable(GL_SCISSOR_TEST);
}
else
glDisable(GL_SCISSOR_TEST);
// texture
if(State.m_Texture >= 0 && State.m_Texture < CCommandBuffer::MAX_TEXTURES)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_aTextures[State.m_Texture]);
}
else
glDisable(GL_TEXTURE_2D);
// screen mapping
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(State.m_ScreenTL.x, State.m_ScreenBR.x, State.m_ScreenBR.y, State.m_ScreenTL.y, 1.0f, 10.f);
}
static int TexFormatToOpenGLFormat(int TexFormat)
{
if(TexFormat == CCommandBuffer::TEXFORMAT_RGB) return GL_RGB;
if(TexFormat == CCommandBuffer::TEXFORMAT_ALPHA) return GL_ALPHA;
if(TexFormat == CCommandBuffer::TEXFORMAT_RGBA) return GL_RGBA;
return GL_RGBA;
}
void Cmd_Texture_Update(const CCommandBuffer::SCommand_Texture_Update *pCommand)
{
glBindTexture(GL_TEXTURE_2D, m_aTextures[pCommand->m_Slot]);
glTexSubImage2D(GL_TEXTURE_2D, 0, pCommand->m_X, pCommand->m_Y, pCommand->m_Width, pCommand->m_Height,
TexFormatToOpenGLFormat(pCommand->m_Format), GL_UNSIGNED_BYTE, pCommand->m_pData);
mem_free(pCommand->m_pData);
}
void Cmd_Texture_Destroy(const CCommandBuffer::SCommand_Texture_Destroy *pCommand)
{
glDeleteTextures(1, &m_aTextures[pCommand->m_Slot]);
}
void Cmd_Texture_Create(const CCommandBuffer::SCommand_Texture_Create *pCommand)
{
int Oglformat = TexFormatToOpenGLFormat(pCommand->m_Format);
// upload texture
int StoreOglformat = Oglformat;
if(g_Config.m_GfxTextureCompression)
{
StoreOglformat = GL_COMPRESSED_RGBA_ARB;
if(pCommand->m_StoreFormat == CCommandBuffer::TEXFORMAT_RGB)
StoreOglformat = GL_COMPRESSED_RGB_ARB;
else if(Oglformat == CCommandBuffer::TEXFORMAT_ALPHA)
StoreOglformat = GL_COMPRESSED_ALPHA_ARB;
}
else
{
StoreOglformat = TexFormatToOpenGLFormat(pCommand->m_StoreFormat);
}
glGenTextures(1, &m_aTextures[pCommand->m_Slot]);
glBindTexture(GL_TEXTURE_2D, m_aTextures[pCommand->m_Slot]);
if(pCommand->m_Flags&CCommandBuffer::TEXFLAG_NOMIPMAPS)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, StoreOglformat, pCommand->m_Width, pCommand->m_Height, 0, Oglformat, GL_UNSIGNED_BYTE, pCommand->m_pData);
}
else
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, StoreOglformat, pCommand->m_Width, pCommand->m_Height, Oglformat, GL_UNSIGNED_BYTE, pCommand->m_pData);
}
mem_free(pCommand->m_pData);
}
void Cmd_Clear(const CCommandBuffer::SCommand_Clear *pCommand)
{
glClearColor(pCommand->m_Color.r, pCommand->m_Color.g, pCommand->m_Color.b, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void Cmd_Render(const CCommandBuffer::SCommand_Render *pCommand)
{
SetState(pCommand->m_State);
glVertexPointer(3, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices);
glTexCoordPointer(2, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices + sizeof(float)*3);
glColorPointer(4, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices + sizeof(float)*5);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
switch(pCommand->m_PrimType)
{
case CCommandBuffer::PRIMTYPE_QUADS:
glDrawArrays(GL_QUADS, 0, pCommand->m_PrimCount*4);
break;
case CCommandBuffer::PRIMTYPE_LINES:
glDrawArrays(GL_LINES, 0, pCommand->m_PrimCount*2);
break;
default:
dbg_msg("render", "unknown primtype %d\n", pCommand->m_Cmd);
};
}
void Cmd_Screenshot(const CCommandBuffer::SCommand_Screenshot *pCommand)
{
// fetch image data
GLint aViewport[4] = {0,0,0,0};
glGetIntegerv(GL_VIEWPORT, aViewport);
int w = aViewport[2];
int h = aViewport[3];
dbg_msg("graphics", "grabbing %d x %d", w, h);
// we allocate one more row to use when we are flipping the texture
unsigned char *pPixelData = (unsigned char *)mem_alloc(w*(h+1)*3, 1);
unsigned char *pTempRow = pPixelData+w*h*3;
// fetch the pixels
GLint Alignment;
glGetIntegerv(GL_PACK_ALIGNMENT, &Alignment);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0,0, w, h, GL_RGB, GL_UNSIGNED_BYTE, pPixelData);
glPixelStorei(GL_PACK_ALIGNMENT, Alignment);
// flip the pixel because opengl works from bottom left corner
for(int y = 0; y < h/2; y++)
{
mem_copy(pTempRow, pPixelData+y*w*3, w*3);
mem_copy(pPixelData+y*w*3, pPixelData+(h-y-1)*w*3, w*3);
mem_copy(pPixelData+(h-y-1)*w*3, pTempRow,w*3);
}
// fill in the information
pCommand->m_pImage->m_Width = w;
pCommand->m_pImage->m_Height = h;
pCommand->m_pImage->m_Format = CImageInfo::FORMAT_RGB;
pCommand->m_pImage->m_pData = pPixelData;
}
public:
CCommandProcessorFragment_OpenGL()
{
mem_zero(m_aTextures, sizeof(m_aTextures));
}
bool RunCommand(const CCommandBuffer::SCommand * pBaseCommand)
{
switch(pBaseCommand->m_Cmd)
{
case CCommandBuffer::CMD_TEXTURE_CREATE: Cmd_Texture_Create(static_cast<const CCommandBuffer::SCommand_Texture_Create *>(pBaseCommand)); break;
case CCommandBuffer::CMD_TEXTURE_DESTROY: Cmd_Texture_Destroy(static_cast<const CCommandBuffer::SCommand_Texture_Destroy *>(pBaseCommand)); break;
case CCommandBuffer::CMD_TEXTURE_UPDATE: Cmd_Texture_Update(static_cast<const CCommandBuffer::SCommand_Texture_Update *>(pBaseCommand)); break;
case CCommandBuffer::CMD_CLEAR: Cmd_Clear(static_cast<const CCommandBuffer::SCommand_Clear *>(pBaseCommand)); break;
case CCommandBuffer::CMD_RENDER: Cmd_Render(static_cast<const CCommandBuffer::SCommand_Render *>(pBaseCommand)); break;
case CCommandBuffer::CMD_SCREENSHOT: Cmd_Screenshot(static_cast<const CCommandBuffer::SCommand_Screenshot *>(pBaseCommand)); break;
default: return false;
}
return true;
}
};
class CCommandProcessorFragment_SDL
{
// SDL stuff
SDL_Surface *m_pScreenSurface;
bool m_SystemInited;
void Cmd_Init(const CCommandBuffer::SCommand_Init *pCommand)
{
if(!m_SystemInited)
{
int Systems = SDL_INIT_VIDEO;
if(g_Config.m_SndEnable) // TODO: remove
Systems |= SDL_INIT_AUDIO;
if(g_Config.m_ClEventthread) // TODO: remove
Systems |= SDL_INIT_EVENTTHREAD;
if(SDL_Init(Systems) < 0)
{
dbg_msg("gfx", "unable to init SDL: %s", SDL_GetError());
*pCommand->m_pResult = -1;
return;
}
atexit(SDL_Quit); // ignore_convention
#ifdef CONF_FAMILY_WINDOWS
if(!getenv("SDL_VIDEO_WINDOW_POS") && !getenv("SDL_VIDEO_CENTERED")) // ignore_convention
putenv("SDL_VIDEO_WINDOW_POS=8,27"); // ignore_convention
#endif
m_SystemInited = true;
}
const SDL_VideoInfo *pInfo = SDL_GetVideoInfo();
SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
// set flags
int Flags = SDL_OPENGL;
if(pCommand->m_Flags&CCommandBuffer::INITFLAG_RESIZABLE)
Flags |= SDL_RESIZABLE;
if(pInfo->hw_available) // ignore_convention
Flags |= SDL_HWSURFACE;
else
Flags |= SDL_SWSURFACE;
if(pInfo->blit_hw) // ignore_convention
Flags |= SDL_HWACCEL;
if(pCommand->m_Flags&CCommandBuffer::INITFLAG_FULLSCREEN)
Flags |= SDL_FULLSCREEN;
// set gl attributes
if(pCommand->m_FsaaSamples)
{
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, pCommand->m_FsaaSamples);
}
else
{
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
}
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, pCommand->m_Flags&CCommandBuffer::INITFLAG_VSYNC ? 1 : 0);
// set caption
SDL_WM_SetCaption(pCommand->m_aName, pCommand->m_aName);
// create window
m_pScreenSurface = SDL_SetVideoMode(pCommand->m_ScreenWidth, pCommand->m_ScreenHeight, 0, Flags);
if(m_pScreenSurface)
{
SDL_ShowCursor(0);
// set some default settings
glEnable(GL_BLEND);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glAlphaFunc(GL_GREATER, 0);
glEnable(GL_ALPHA_TEST);
glDepthMask(0);
*pCommand->m_pResult = 0;
}
else
{
dbg_msg("gfx", "unable to set video mode: %s", SDL_GetError());
*pCommand->m_pResult = -1;
}
}
void Cmd_Shutdown(const CCommandBuffer::SCommand_Shutdown *pCommand)
{
SDL_Quit();
}
void Cmd_Swap(const CCommandBuffer::SCommand_Swap *pCommand)
{
SDL_GL_SwapBuffers();
}
void Cmd_VideoModes(const CCommandBuffer::SCommand_VideoModes *pCommand)
{
// TODO: fix this code on osx or windows
SDL_Rect **ppModes = SDL_ListModes(NULL, SDL_OPENGL|SDL_GL_DOUBLEBUFFER|SDL_FULLSCREEN);
if(ppModes == NULL)
{
// no modes
*pCommand->m_pNumModes = 0;
}
else if(ppModes == (SDL_Rect**)-1)
{
// no modes
*pCommand->m_pNumModes = 0;
}
else
{
int NumModes = 0;
for(int i = 0; ppModes[i]; ++i)
{
if(NumModes == pCommand->m_MaxModes)
break;
pCommand->m_pModes[NumModes].m_Width = ppModes[i]->w;
pCommand->m_pModes[NumModes].m_Height = ppModes[i]->h;
pCommand->m_pModes[NumModes].m_Red = 8;
pCommand->m_pModes[NumModes].m_Green = 8;
pCommand->m_pModes[NumModes].m_Blue = 8;
NumModes++;
}
*pCommand->m_pNumModes = NumModes;
}
}
public:
CCommandProcessorFragment_SDL()
{
m_SystemInited = false;
m_pScreenSurface = 0x0;
}
bool RunCommand(const CCommandBuffer::SCommand *pBaseCommand)
{
switch(pBaseCommand->m_Cmd)
{
case CCommandBuffer::CMD_INIT: Cmd_Init(static_cast<const CCommandBuffer::SCommand_Init *>(pBaseCommand)); break;
case CCommandBuffer::CMD_SHUTDOWN: Cmd_Shutdown(static_cast<const CCommandBuffer::SCommand_Shutdown *>(pBaseCommand)); break;
case CCommandBuffer::CMD_SWAP: Cmd_Swap(static_cast<const CCommandBuffer::SCommand_Swap *>(pBaseCommand)); break;
case CCommandBuffer::CMD_VIDEOMODES: Cmd_VideoModes(static_cast<const CCommandBuffer::SCommand_VideoModes *>(pBaseCommand)); break;
default: return false;
}
return true;
}
};
class CCommandProcessor_SDL_OpenGL : public ICommandProcessor
{
CCommandProcessorFragment_OpenGL m_OpenGL;
CCommandProcessorFragment_SDL m_SDL;
CCommandProcessorFragment_General m_General;
public:
virtual void RunBuffer(CCommandBuffer *pBuffer)
{
unsigned CmdIndex = 0;
while(1)
{
const CCommandBuffer::SCommand *pBaseCommand = pBuffer->GetCommand(&CmdIndex);
if(pBaseCommand == 0x0)
break;
if(m_OpenGL.RunCommand(pBaseCommand))
continue;
if(m_SDL.RunCommand(pBaseCommand))
continue;
if(m_General.RunCommand(pBaseCommand))
continue;
dbg_msg("graphics", "unknown command %d", pBaseCommand->m_Cmd);
}
}
};
void CCommandProcessorHandler::ThreadFunc(void *pUser)
{
CCommandProcessorHandler *pThis = (CCommandProcessorHandler *)pUser;
while(!pThis->m_Shutdown)
{
pThis->m_Activity.wait();
if(pThis->m_pBuffer)
{
pThis->m_pProcessor->RunBuffer(pThis->m_pBuffer);
sync_barrier();
pThis->m_pBuffer = 0x0;
pThis->m_BufferDone.signal();
}
}
}
CCommandProcessorHandler::CCommandProcessorHandler()
{
m_pBuffer = 0x0;
m_pProcessor = 0x0;
m_pThread = 0x0;
}
void CCommandProcessorHandler::Start(ICommandProcessor *pProcessor)
{
m_Shutdown = false;
m_pProcessor = pProcessor;
m_pThread = thread_create(ThreadFunc, this);
m_BufferDone.signal();
}
void CCommandProcessorHandler::Stop()
{
m_Shutdown = true;
m_Activity.signal();
thread_wait(m_pThread);
thread_destroy(m_pThread);
}
void CCommandProcessorHandler::RunBuffer(CCommandBuffer *pBuffer)
{
WaitForIdle();
m_pBuffer = pBuffer;
m_Activity.signal();
}
void CCommandProcessorHandler::WaitForIdle()
{
while(m_pBuffer != 0x0)
m_BufferDone.wait();
}
void CGraphics_Threaded::FlushVertices()
{
if(m_NumVertices == 0)
@ -910,7 +443,7 @@ int CGraphics_Threaded::LoadPNG(CImageInfo *pImg, const char *pFilename, int Sto
void CGraphics_Threaded::KickCommandBuffer()
{
m_Handler.RunBuffer(m_pCommandBuffer);
m_pBackend->RunBuffer(m_pCommandBuffer);
// swap buffer
m_CurrentCommandBuffer ^= 1;
@ -1156,27 +689,12 @@ void CGraphics_Threaded::QuadsText(float x, float y, float Size, float r, float
int CGraphics_Threaded::IssueInit()
{
// issue init command
m_pCommandBuffer->Reset();
volatile int Result;
CCommandBuffer::SCommand_Init Cmd;
str_copy(Cmd.m_aName, "Teeworlds", sizeof(Cmd.m_aName));
Cmd.m_pResult = &Result;
Cmd.m_ScreenWidth = g_Config.m_GfxScreenWidth;
Cmd.m_ScreenHeight = g_Config.m_GfxScreenHeight;
Cmd.m_FsaaSamples = g_Config.m_GfxFsaaSamples;
Cmd.m_Flags = 0;
if(g_Config.m_GfxFullscreen) Cmd.m_Flags |= CCommandBuffer::INITFLAG_FULLSCREEN;
if(g_Config.m_GfxVsync) Cmd.m_Flags |= CCommandBuffer::INITFLAG_VSYNC;
if(g_Config.m_DbgResizable) Cmd.m_Flags |= CCommandBuffer::INITFLAG_RESIZABLE;
int Flags = 0;
if(g_Config.m_GfxFullscreen) Flags |= IGraphicsBackend::INITFLAG_FULLSCREEN;
if(g_Config.m_GfxVsync) Flags |= IGraphicsBackend::INITFLAG_VSYNC;
if(g_Config.m_DbgResizable) Flags |= IGraphicsBackend::INITFLAG_RESIZABLE;
m_pCommandBuffer->AddCommand(Cmd);
m_Handler.RunBuffer(m_pCommandBuffer);
m_Handler.WaitForIdle();
return Result;
return m_pBackend->Init("Teeworlds", g_Config.m_GfxScreenWidth, g_Config.m_GfxScreenHeight, g_Config.m_GfxFsaaSamples, Flags);
}
int CGraphics_Threaded::InitWindow()
@ -1214,7 +732,7 @@ int CGraphics_Threaded::InitWindow()
return -1;
}
bool CGraphics_Threaded::Init()
int CGraphics_Threaded::Init()
{
// fetch pointers
m_pStorage = Kernel()->RequestInterface<IStorage>();
@ -1230,23 +748,19 @@ bool CGraphics_Threaded::Init()
m_aTextures[i].m_Next = i+1;
m_aTextures[MAX_TEXTURES-1].m_Next = -1;
// start the command processor
m_pProcessor = new CCommandProcessor_SDL_OpenGL;
m_Handler.Start(m_pProcessor);
m_pBackend = CreateGraphicsBackend();
if(InitWindow() != 0)
return -1;
// fetch final resolusion
m_ScreenWidth = g_Config.m_GfxScreenWidth;
m_ScreenHeight = g_Config.m_GfxScreenHeight;
// create command buffers
m_apCommandBuffers[0] = new CCommandBuffer(1024*512, 1024*1024);
m_apCommandBuffers[1] = new CCommandBuffer(1024*512, 1024*1024);
m_pCommandBuffer = m_apCommandBuffers[0];
// try to init the window
if(InitWindow() != 0)
return 0;
// fetch final resolusion
m_ScreenWidth = g_Config.m_GfxScreenWidth;
m_ScreenHeight = g_Config.m_GfxScreenHeight;
// create null texture, will get id=0
static const unsigned char aNullTextureData[] = {
0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0x00,0xff,0x00,0xff, 0x00,0xff,0x00,0xff,
@ -1261,36 +775,31 @@ bool CGraphics_Threaded::Init()
void CGraphics_Threaded::Shutdown()
{
// add swap command
CCommandBuffer::SCommand_Shutdown Cmd;
m_pCommandBuffer->AddCommand(Cmd);
m_Handler.RunBuffer(m_pCommandBuffer);
// wait for everything to process and then stop the command processor
m_Handler.WaitForIdle();
m_Handler.Stop();
delete m_pProcessor;
m_pProcessor = 0;
// shutdown the backend
m_pBackend->Shutdown();
delete m_pBackend;
m_pBackend = 0x0;
}
void CGraphics_Threaded::Minimize()
{
SDL_WM_IconifyWindow();
m_pBackend->Minimize();
}
void CGraphics_Threaded::Maximize()
{
// TODO: SDL
m_pBackend->Maximize();
}
int CGraphics_Threaded::WindowActive()
{
return SDL_GetAppState()&SDL_APPINPUTFOCUS;
return m_pBackend->WindowActive();
}
int CGraphics_Threaded::WindowOpen()
{
return SDL_GetAppState()&SDL_APPACTIVE;
return m_pBackend->WindowOpen();
}
@ -1330,12 +839,12 @@ void CGraphics_Threaded::InsertSignal(semaphore *pSemaphore)
bool CGraphics_Threaded::IsIdle()
{
return m_Handler.IsIdle();
return m_pBackend->IsIdle();
}
void CGraphics_Threaded::WaitForIdle()
{
m_Handler.WaitForIdle();
m_pBackend->WaitForIdle();
}
int CGraphics_Threaded::GetVideoModes(CVideoMode *pModes, int MaxModes)

View file

@ -2,6 +2,8 @@
#include <base/tl/threading.h>
#include <engine/graphics.h>
class CCommandBuffer
{
class CBuffer
@ -54,12 +56,12 @@ public:
enum
{
//
CMD_NOP = 0,
// commadn groups
CMDGROUP_CORE = 0, // commands that everyone has to implement
CMDGROUP_PLATFORM = 10000, // commands specific to a platform
//
CMD_INIT,
CMD_SHUTDOWN,
CMD_NOP = CMDGROUP_CORE,
//
CMD_RUNBUFFER,
@ -82,6 +84,7 @@ public:
// misc
CMD_SCREENSHOT,
CMD_VIDEOMODES,
};
enum
@ -155,25 +158,6 @@ public:
SCommand_Clear() : SCommand(CMD_CLEAR) {}
SColor m_Color;
};
struct SCommand_Init : public SCommand
{
SCommand_Init() : SCommand(CMD_INIT) {}
char m_aName[256];
int m_ScreenWidth;
int m_ScreenHeight;
int m_FsaaSamples;
int m_Flags;
volatile int *m_pResult;
};
struct SCommand_Shutdown : public SCommand
{
SCommand_Shutdown() : SCommand(CMD_SHUTDOWN) {}
};
struct SCommand_Signal : public SCommand
{
@ -269,10 +253,13 @@ public:
template<class T>
void AddCommand(const T &Command)
{
// make sure that we don't do something stupid like ->AddCommand(&Cmd);
(void)static_cast<const SCommand *>(&Command);
// allocate and copy the command into the buffer
SCommand *pCmd = (SCommand *)m_CmdBuffer.Alloc(sizeof(Command));
if(!pCmd)
return;
mem_copy(pCmd, &Command, sizeof(Command));
pCmd->m_Size = sizeof(Command);
}
@ -294,40 +281,35 @@ public:
}
};
class ICommandProcessor
// interface for the graphics backend
// all these functions are called on the main thread
class IGraphicsBackend
{
public:
virtual ~ICommandProcessor() {}
enum
{
INITFLAG_FULLSCREEN = 1,
INITFLAG_VSYNC = 2,
INITFLAG_RESIZABLE = 4,
};
virtual int Init(const char *pName, int Width, int Height, int FsaaSamples, int Flags) = 0;
virtual int Shutdown() = 0;
virtual void Minimize() = 0;
virtual void Maximize() = 0;
virtual int WindowActive() = 0;
virtual int WindowOpen() = 0;
virtual void RunBuffer(CCommandBuffer *pBuffer) = 0;
};
class CCommandProcessorHandler
{
ICommandProcessor *m_pProcessor;
CCommandBuffer * volatile m_pBuffer;
volatile bool m_Shutdown;
semaphore m_Activity;
semaphore m_BufferDone;
void *m_pThread;
static void ThreadFunc(void *pUser);
public:
CCommandProcessorHandler();
void Start(ICommandProcessor *pProcessor);
void Stop();
void RunBuffer(CCommandBuffer *pBuffer);
bool IsIdle() const { return m_pBuffer == 0; }
void WaitForIdle();
virtual bool IsIdle() const = 0;
virtual void WaitForIdle() = 0;
};
class CGraphics_Threaded : public IEngineGraphics
{
CCommandBuffer::SState m_State;
CCommandProcessorHandler m_Handler;
ICommandProcessor *m_pProcessor;
IGraphicsBackend *m_pBackend;
CCommandBuffer *m_apCommandBuffers[2];
CCommandBuffer *m_pCommandBuffer;
@ -440,7 +422,7 @@ public:
virtual int WindowActive();
virtual int WindowOpen();
virtual bool Init();
virtual int Init();
virtual void Shutdown();
virtual void TakeScreenshot(const char *pFilename);
@ -452,4 +434,6 @@ public:
virtual void InsertSignal(semaphore *pSemaphore);
virtual bool IsIdle();
virtual void WaitForIdle();
};
};
extern IGraphicsBackend *CreateGraphicsBackend();

View file

@ -209,6 +209,12 @@ int CSound::Init()
if(!g_Config.m_SndEnable)
return 0;
if(SDL_InitSubSystem(SDL_INIT_AUDIO) < 0)
{
dbg_msg("gfx", "unable to init SDL audio: %s", SDL_GetError());
return -1;
}
m_MixingRate = g_Config.m_SndRate;
// Set 16-bit stereo audio at 22Khz
@ -256,6 +262,7 @@ int CSound::Update()
int CSound::Shutdown()
{
SDL_CloseAudio();
SDL_QuitSubSystem(SDL_INIT_AUDIO);
lock_destroy(m_SoundLock);
return 0;
}

View file

@ -146,7 +146,7 @@ class IEngineGraphics : public IGraphics
{
MACRO_INTERFACE("enginegraphics", 0)
public:
virtual bool Init() = 0;
virtual int Init() = 0;
virtual void Shutdown() = 0;
virtual void Minimize() = 0;