openclonk/src/platform/C4AppSDL.cpp

202 lines
4.5 KiB
C++
Raw Normal View History

2009-05-08 13:28:41 +00:00
/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 2006 Julian Raschke
* Copyright (c) 2008-2009 Günther Brammer
* Copyright (c) 2009 Martin Plicht
* Copyright (c) 2010 Benjamin Herr
* Copyright (c) 2010 Peter Wortmann
2009-05-08 13:28:41 +00:00
* Copyright (c) 2005-2009, RedWolf Design GmbH, http://www.clonk.de
*
* Portions might be copyrighted by other authors who have contributed
* to OpenClonk.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
* See isc_license.txt for full license and disclaimer.
*
* "Clonk" is a registered trademark of Matthes Bender.
* See clonk_trademark_license.txt for full license.
*/
/* A wrapper class to OS dependent event and window interfaces, SDL version */
#include <C4Include.h>
#include "C4App.h"
#include <C4Window.h>
2009-05-08 13:28:41 +00:00
#include <StdGL.h>
#include <StdDDraw2.h>
#include <StdFile.h>
#include <StdBuf.h>
#include <string>
#include <sstream>
#include <sys/select.h>
#include <sys/time.h>
#include <time.h>
#include <errno.h>
2011-08-27 14:20:39 +00:00
/* C4AbstractApp */
2009-05-08 13:28:41 +00:00
2011-08-27 14:20:39 +00:00
C4AbstractApp::C4AbstractApp(): Active(false), fQuitMsgReceived(false),
2012-03-11 22:06:23 +00:00
MainThread(pthread_self()), fDspModeSet(false)
2009-05-08 13:28:41 +00:00
{
}
2011-08-27 14:20:39 +00:00
C4AbstractApp::~C4AbstractApp()
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
}
2011-08-27 14:20:39 +00:00
bool C4AbstractApp::Init(int argc, char * argv[])
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// Set locale
setlocale(LC_ALL,"");
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_NOPARACHUTE) < 0)
2010-03-28 18:58:01 +00:00
{
Log("Error initializing SDL.");
2009-05-08 13:28:41 +00:00
return false;
}
2009-05-08 13:28:41 +00:00
SDL_EnableUNICODE(1);
2009-08-12 12:16:35 +00:00
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
2009-05-08 13:28:41 +00:00
// Custom initialization
return DoInit (argc, argv);
2009-05-08 13:28:41 +00:00
}
2011-08-27 14:20:39 +00:00
void C4AbstractApp::Clear()
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
SDL_Quit();
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
2011-08-27 14:20:39 +00:00
void C4AbstractApp::Quit()
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
fQuitMsgReceived = true;
}
2011-08-27 14:20:39 +00:00
bool C4AbstractApp::FlushMessages()
2010-03-28 18:58:01 +00:00
{
// Always fail after quit message
2010-03-28 18:58:01 +00:00
if (fQuitMsgReceived)
return false;
2009-05-08 13:28:41 +00:00
// Handle pending SDL messages
SDL_Event event;
2010-03-28 18:58:01 +00:00
while (SDL_PollEvent(&event))
{
2009-05-08 13:28:41 +00:00
HandleSDLEvent(event);
}
return true;
2009-05-08 13:28:41 +00:00
}
2011-08-27 14:20:39 +00:00
void C4AbstractApp::HandleSDLEvent(SDL_Event& event)
2010-03-28 18:58:01 +00:00
{
// Directly handle QUIT messages.
switch (event.type)
{
case SDL_QUIT:
Quit();
break;
2009-05-08 13:28:41 +00:00
}
2010-03-28 18:58:01 +00:00
2009-08-12 12:30:48 +00:00
#ifdef __APPLE__
MacUtility::ensureWindowInFront();
#endif
2009-05-08 13:28:41 +00:00
2010-03-28 18:58:01 +00:00
// Everything else goes to the window.
2009-05-08 13:28:41 +00:00
if (pWindow)
pWindow->HandleMessage(event);
}
2011-08-27 14:20:39 +00:00
bool C4AbstractApp::GetIndexedDisplayMode(int32_t iIndex, int32_t *piXRes, int32_t *piYRes, int32_t *piBitDepth, int32_t *piRefreshRate, uint32_t iMonitor)
2010-03-28 18:58:01 +00:00
{
// No support for multiple monitors.
if (iMonitor != 0)
return false;
2009-05-08 13:28:41 +00:00
static SDL_Rect** modes = 0;
static int modeCount = 0;
2010-03-28 18:58:01 +00:00
if (!modes)
{
modes = SDL_ListModes(NULL, SDL_OPENGL | SDL_FULLSCREEN);
// -1 means "all modes allowed". Clonk is not prepared
// for this; should probably give some random resolutions
// then.
assert(reinterpret_cast<intptr_t>(modes) != -1);
if (!modes)
modeCount = 0;
else
2010-03-28 18:58:01 +00:00
// Count available modes.
for (SDL_Rect** iter = modes; *iter; ++iter)
++modeCount;
}
2009-05-08 13:28:41 +00:00
if (iIndex >= modeCount)
return false;
2009-05-08 13:28:41 +00:00
*piXRes = modes[iIndex]->w;
*piYRes = modes[iIndex]->h;
*piBitDepth = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
return true;
2009-05-08 13:28:41 +00:00
}
2011-08-27 14:20:39 +00:00
bool C4AbstractApp::SetVideoMode(unsigned int iXRes, unsigned int iYRes, unsigned int iColorDepth, unsigned int RefreshRate, unsigned int iMonitor, bool fFullScreen)
2010-03-28 18:58:01 +00:00
{
//RECT r;
//pWindow->GetSize(&r);
2011-08-27 21:12:01 +00:00
// FIXME: optimize redundant calls away. maybe make all platforms implicitely call SetVideoMode in C4Window::Init?
2009-05-12 21:42:34 +00:00
// SDL doesn't support multiple monitors.
2010-03-28 18:58:01 +00:00
if (!SDL_SetVideoMode(iXRes, iYRes, iColorDepth, SDL_OPENGL | (fFullScreen ? SDL_FULLSCREEN : 0)))
{
2009-05-12 21:42:34 +00:00
sLastError.Copy(SDL_GetError());
return false;
}
2009-05-08 13:28:41 +00:00
SDL_ShowCursor(SDL_DISABLE);
2009-05-12 21:42:34 +00:00
pWindow->SetSize(iXRes, iYRes);
OnResolutionChanged(iXRes, iYRes);
return true;
2009-05-08 13:28:41 +00:00
}
2011-08-27 14:20:39 +00:00
void C4AbstractApp::RestoreVideoMode()
2010-03-28 18:58:01 +00:00
{
}
2009-05-08 13:28:41 +00:00
bool C4AbstractApp::ApplyGammaRamp(_D3DGAMMARAMP& ramp, bool fForce)
{
return SDL_SetGammaRamp(ramp.red, ramp.green, ramp.blue) != -1;
}
bool C4AbstractApp::SaveDefaultGammaRamp(_D3DGAMMARAMP& ramp)
{
return SDL_GetGammaRamp(ramp.red, ramp.green, ramp.blue) != -1;
}
// For Max OS X, the implementation resides in StdMacApp.mm
#ifndef __APPLE__
2009-05-08 13:28:41 +00:00
// stubs
2011-08-27 14:20:39 +00:00
bool C4AbstractApp::Copy(const StdStrBuf & text, bool fClipboard)
2010-03-28 18:58:01 +00:00
{
return false;
2009-05-08 13:28:41 +00:00
}
2011-08-27 14:20:39 +00:00
StdStrBuf C4AbstractApp::Paste(bool fClipboard)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
return StdStrBuf(0);
}
2011-08-27 14:20:39 +00:00
bool C4AbstractApp::IsClipboardFull(bool fClipboard)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
return false;
}
2011-08-27 14:20:39 +00:00
void C4AbstractApp::MessageDialog(const char * message)
{
}
#endif