Various cleanups in the platform code

Günther Brammer 2010-02-16 02:54:02 +01:00
parent 7ed1264518
commit a16df97a95
15 changed files with 53 additions and 170 deletions

View File

@ -92,7 +92,7 @@ AC_ARG_ENABLE([sound],
GTK_REQUIRED="glib-2.0 >= 2.4 gtk+-2.0 >= 2.4"
AC_ARG_WITH([gtk],
[AC_HELP_STRING([--with-gtk], [Use gtk for the developer mode [default=auto]])],
, [PKG_CHECK_EXISTS([$GTK_REQUIRED], [with_gtk=yes], [with_gtk=no])])
, [AS_IF([test $enable_console = no],[PKG_CHECK_EXISTS([$GTK_REQUIRED], [with_gtk=yes], [with_gtk=no])],[with_gtk=no])])
AS_IF([test $with_gtk = yes],[
PKG_CHECK_MODULES(GTK, [$GTK_REQUIRED])
AC_DEFINE([WITH_GLIB], 1, [Glib])
@ -156,7 +156,6 @@ fi
# Check for libjpeg
AC_CHECK_LIB(jpeg, jpeg_read_header, [
CLONK_LIBS="-ljpeg $CLONK_LIBS"
with_internal_libjpeg=no
], [
AC_MSG_ERROR([libjpeg not found.])
])
@ -164,7 +163,6 @@ AC_CHECK_LIB(jpeg, jpeg_read_header, [
# Check for libpng
AC_CHECK_LIB(png, png_read_image, [
CLONK_LIBS="-lpng $CLONK_LIBS"
with_internal_libpng=no
], [
AC_MSG_ERROR([libpng not found.])
], [-lz])

View File

@ -416,7 +416,7 @@ void C4Application::GameTick()
assert(AppState != C4AS_None);
break;
case C4AS_Quit:
// Do nothing, HandleMessage will return HR_Failure soon
// Do nothing, the main loop will exit soon
break;
case C4AS_PreInit:
if (!PreInit()) Quit();

View File

@ -275,7 +275,7 @@ bool C4GameSave::SaveDesc(C4Group &hToGroup)
WriteDesc(sBuffer);
// End of file
sBuffer.Append(LineFeed "}" LineFeed EndOfFile);
sBuffer.Append(LineFeed "}" LineFeed);
// Generate Filename
StdStrBuf sFilename; char szLang[3];

View File

@ -516,7 +516,6 @@ bool C4MaterialMap::SaveEnumeration(C4Group &hGroup)
SAppend(Map[cnt].Name,mapbuf);
SAppend(LineFeed,mapbuf);
}
SAppend(EndOfFile,mapbuf);
return hGroup.Add(C4CFN_MatMap,mapbuf,SLen(mapbuf),false,true);
}

View File

@ -340,7 +340,9 @@ bool Dialog::CreateConsoleWindow()
return false;
}
// create rendering context
#ifdef USE_GL
if (lpDDraw) pCtx = lpDDraw->CreateContext(pWindow, &Application);
#endif
return true;
}
@ -352,7 +354,9 @@ void Dialog::DestroyConsoleWindow()
delete pWindow;
pWindow = NULL;
}
#ifdef USE_GL
if (pCtx) { delete pCtx; pCtx=NULL; }
#endif
}
Dialog::Dialog(int32_t iWdt, int32_t iHgt, const char *szTitle, bool fViewportDlg):
@ -731,8 +735,7 @@ bool Dialog::DoModal()
// main message loop
while (fShow)
{
int32_t iResult=1;
while ((iResult != HR_Timer) && fShow)
while (fShow)
{
// dialog idle proc
OnIdle();

View File

@ -50,7 +50,6 @@ int32_t Distance(int32_t iX1, int32_t iY1, int32_t iX2, int32_t iY2)
int Angle(int iX1, int iY1, int iX2, int iY2)
{
const double pi = 3.141592654;
int iAngle = (int) ( 180.0 * atan2( float(Abs(iY1-iY2)), float(Abs(iX1-iX2)) ) / pi );
if (iX2>iX1 )
{ if (iY2<iY1) iAngle = 90-iAngle; else iAngle = 90+iAngle; }

View File

@ -161,14 +161,11 @@ typedef ptrdiff_t ssize_t;
typedef uint32_t DWORD;
typedef uint8_t BYTE;
typedef uint16_t WORD;
typedef uint32_t UINT;
typedef struct {
long left; long top; long right; long bottom;
} RECT;
#define INFINITE 0xFFFFFFFF
unsigned long timeGetTime(void);
#include <strings.h>
@ -241,6 +238,9 @@ bool ForLine(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
bool (*fnCallback)(int32_t, int32_t, int32_t), int32_t iPar=0,
int32_t *lastx=NULL, int32_t *lasty=NULL);
// open a weblink in an external browser
bool OpenURL(const char *szURL);
#include <cctype>
inline char CharCapital(char cChar) { return std::toupper(cChar); }
bool IsIdentifier(char cChar);
@ -317,13 +317,6 @@ int SLineGetCharacters(const char *szText, const char *cpPosition);
bool SWildcardMatchEx(const char *szString, const char *szWildcard);
#define LineFeed "\x00D\x00A"
#define EndOfFile "\x020"
#ifdef _WIN32
#define DirSep "\\"
#else
#define DirSep "/"
#endif
// sprintf wrapper

View File

@ -1119,7 +1119,6 @@ bool CStdDDraw::BlitRotate(SURFACE sfcSource, int fx, int fy, int fwdt, int fhgt
rot.SetRotate(iAngle, (float) (tx+tx+twdt)/2, (float) (ty+ty+thgt)/2);
return Blit(sfcSource, float(fx), float(fy), float(fwdt), float(fhgt), sfcTarget, float(tx), float(ty), float(twdt), float(thgt), true, &rot);
}
const double pi=3.1415926535;
// Object is first stretched to dest rect, then rotated at place.
int xcnt,ycnt,fcx,fcy,tcx,tcy,cpcx,cpcy;
int npcx,npcy;

View File

@ -44,14 +44,14 @@ bool CopyFile(const char *szSource, const char *szTarget, bool FailIfExists);
#endif
#ifdef _WIN32
#define DirSep "\\"
#define DirectorySeparator '\\'
#define AltDirectorySeparator '/'
#else
#define DirSep "/"
#define DirectorySeparator '/'
#define AltDirectorySeparator '\\'
#define DIRECTORYSEPARATORS "/"
#endif
#define Wildcard '*'
/** Create a directory and all of its parents.
* \p[in] path Directory to create

View File

@ -61,7 +61,7 @@ public:
// Value to specify infinite wait.
#ifndef INFINITE
#define INFINITE (~0)
#define INFINITE (~0u)
#endif
class CStdCSec

View File

@ -52,11 +52,9 @@ static CStdApp * readline_callback_use_this_app = 0;
# endif
#endif /* HAVE_READLINE_HISTORY */
#include "StdXPrivate.h"
/* CStdApp */
CStdApp::CStdApp(): Active(false), fQuitMsgReceived(false), Priv(new CStdAppPrivate()),
CStdApp::CStdApp(): Active(false), fQuitMsgReceived(false),
Location(""), DoNotDelay(false),
// main thread
#ifdef HAVE_PTHREAD
@ -67,14 +65,13 @@ CStdApp::CStdApp(): Active(false), fQuitMsgReceived(false), Priv(new CStdAppPriv
}
CStdApp::~CStdApp() {
delete Priv;
}
bool CStdApp::Init(int argc, char * argv[]) {
// Set locale
setlocale(LC_ALL,"");
// Try to figure out the location of the executable
Priv->argc=argc; Priv->argv=argv;
this->argc=argc; this->argv=argv;
static char dir[PATH_MAX];
SCopy(argv[0], dir);
if (dir[0] != '/') {
@ -97,115 +94,20 @@ bool CStdApp::Init(int argc, char * argv[]) {
rl_callback_handler_install (">", readline_callback);
readline_callback_use_this_app = this;
#endif
// create pipe
if(pipe(Priv->Pipe) != 0) {
Log("Error creating Pipe");
return false;
}
// Custom initialization
return DoInit ();
}
bool CStdApp::InitTimer() { gettimeofday(&LastExecute, 0); return true; }
void CStdApp::Clear() {
#if USE_CONSOLE && HAVE_LIBREADLINE
rl_callback_handler_remove();
#endif
// close pipe
close(Priv->Pipe[0]);
close(Priv->Pipe[1]);
}
void CStdApp::Quit() {
fQuitMsgReceived = true;
}
void CStdApp::Execute () {
time_t seconds = LastExecute.tv_sec;
timeval tv;
gettimeofday(&tv, 0);
// Too slow?
if (LastExecute.tv_sec < tv.tv_sec + 2) {
gettimeofday(&LastExecute, 0);
} else {
LastExecute.tv_usec += Delay;
if (LastExecute.tv_usec > 1000000) {
++LastExecute.tv_sec;
LastExecute.tv_usec -= 1000000;
}
}
// This will make the FPS look "prettier" in some situations
// But who cares...
if (seconds != LastExecute.tv_sec) {
pWindow->Sec1Timer();
}
}
void CStdApp::NextTick(bool fYield) {
DoNotDelay = true;
}
void CStdApp::Run() {
// Main message loop
while (true) if (HandleMessage(INFINITE, true) == HR_Failure) return;
}
void CStdApp::ResetTimer(unsigned int d) { Delay = 1000 * d; }
C4AppHandleResult CStdApp::HandleMessage(unsigned int iTimeout, bool fCheckTimer) {
// quit check for nested HandleMessage-calls
if (fQuitMsgReceived) return HR_Failure;
bool do_execute = fCheckTimer;
// Wait Delay microseconds.
timeval tv = { 0, 0 };
if (DoNotDelay) {
DoNotDelay = false;
} else if (fCheckTimer) {
gettimeofday(&tv, 0);
//printf("tv %d %d\n", tv.tv_sec, tv.tv_usec);
//printf("le %d %d\n", LastExecute.tv_sec, LastExecute.tv_usec);
tv.tv_usec = LastExecute.tv_usec - tv.tv_usec + Delay
- 1000000 * (tv.tv_sec - LastExecute.tv_sec);
if (iTimeout != INFINITE && iTimeout * 1000 < tv.tv_usec) tv.tv_usec = iTimeout * 1000;
if (tv.tv_usec < 0)
tv.tv_usec = 0;
tv.tv_sec = 0;
} else {
tv.tv_usec = iTimeout * 1000;
}
// Watch dpy to see when it has input.
int max_fd = 0;
fd_set rfds;
FD_ZERO(&rfds);
#ifdef USE_CONSOLE
// Wait for commands from stdin
FD_SET(0, &rfds);
#endif
// And for events from the network thread
FD_SET(Priv->Pipe[0], &rfds);
max_fd = Max(Priv->Pipe[0], max_fd);
//printf("%d %d\n", tv.tv_sec, tv.tv_usec);
switch (select(max_fd + 1, &rfds, NULL, NULL, &tv)) {
// error
case -1:
if (errno == EINTR) return HR_Timeout; //Whatever, probably never needed
Log(strerror(errno));
Log("select error.");
return HR_Failure;
// timeout
case 0:
if (do_execute) {
Execute();
return HR_Timer;
}
return HR_Timeout;
default:
#ifdef USE_CONSOLE
/*
// handle commands
if(FD_ISSET(0, &rfds))
{
@ -214,11 +116,7 @@ C4AppHandleResult CStdApp::HandleMessage(unsigned int iTimeout, bool fCheckTimer
if(!ReadStdInCommand())
return HR_Failure;
}
#endif
return HR_Message;
}
}
*/
bool CStdApp::GetIndexedDisplayMode(int32_t iIndex, int32_t *piXRes, int32_t *piYRes, int32_t *piBitDepth, uint32_t iMonitor) {
return false;
}

View File

@ -341,6 +341,29 @@ public:
#define CStdMultimediaTimerProc CStdTimerProc
#endif
#ifdef USE_CONSOLE
// A simple alertable proc
class CStdInProc : public StdSchedulerProc
{
public:
CStdInProc();
~CStdInProc() { }
public:
void Notify();
bool Check();
bool CheckAndReset();
public:
// StdSchedulerProc override
virtual bool Execute(int iTimeout, pollfd *);
virtual void GetFDs(std::vector<struct pollfd> & checkfds)
{
pollfd pfd = { 0, POLLIN, 0 };
checkfds.push_back(pfd);
}
};
#endif
class CStdApp : public StdScheduler
{
public:
@ -391,13 +414,11 @@ public:
// notify user to get back to the program
void NotifyUserIfInactive()
{
#ifdef _WIN32
#ifdef _WIN32
if (!Active && pWindow) pWindow->FlashWindow();
#elif defined (__APPLE__)
#else
if (pWindow) pWindow->FlashWindow();
#elif defined (USE_X11)
if(pWindow) pWindow->FlashWindow();
#endif
#endif
}
void MessageDialog(const char * message);
const char *GetLastError() { return sLastError.getData(); }
@ -466,14 +487,17 @@ protected:
void OnXInput();
void OnStdInInput();
protected:
#if defined(USE_SDL_MAINLOOP)
#if defined(USE_SDL_MAINLOOP) || defined(USE_CONSOLE)
int argc; char ** argv;
int Pipe[2];
#endif
#ifdef USE_X11
class CStdAppPrivate * Priv;
void HandleXMessage();
#endif
unsigned int KeyMask;
#endif
#ifdef USE_CONSOLE
CStdInProc InProc;
#endif
const char *szCmdLine;
StdStrBuf sLastError;

View File

@ -28,11 +28,7 @@
#include <StdFile.h>
#include <StdBuf.h>
// Xmd.h typedefs bool to CARD8, but we want int
#define bool _BOOL
#include <X11/Xmd.h>
#undef bool
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>

View File

@ -134,15 +134,12 @@ class CStdAppPrivate {
#ifdef WITH_GLIB
GLibProc(g_main_context_default()),
#endif // WITH_GLIB
#ifdef USE_X11
PrimarySelection(), ClipboardSelection(),
LastEventTime(CurrentTime), tasked_out(false), pending_desktop(false),
xim(0), xic(0), X11Proc(pApp),
#endif
argc(0), argv(0) { }
static CStdWindow * GetWindow(unsigned long wnd);
static void SetWindow(unsigned long wnd, CStdWindow * pWindow);
#ifdef USE_X11
bool SwitchToFullscreen(CStdApp * pApp, Window wnd);
void SwitchToDesktop(CStdApp * pApp, Window wnd);
void SetEWMHFullscreen (CStdApp * pApp, bool fFullScreen, Window wnd);
@ -163,7 +160,6 @@ class CStdAppPrivate {
XIC xic;
Bool detectable_autorepeat_supported;
CX11Proc X11Proc;
#endif
int argc; char ** argv;
};

View File

@ -21,6 +21,8 @@
/* A wrapper class to OS dependent event and window interfaces, X11 version */
#include <Standard.h>
#ifdef USE_X11
#include <StdWindow.h>
#include <StdGL.h>
#include <StdDDraw2.h>
@ -29,16 +31,12 @@
#include "C4Version.h"
#ifdef USE_X11
#define bool _BOOL
#include "c4x.xpm"
#include <X11/Xlib.h>
#include <X11/xpm.h>
#include <X11/Xatom.h>
#include <X11/extensions/xf86vmode.h>
#include <GL/glx.h>
#undef bool
#endif
#include <string>
#include <map>
@ -66,9 +64,6 @@ CStdWindow * CStdWindow::Init(CStdApp * pApp) {
}
CStdWindow * CStdWindow::Init(CStdApp * pApp, const char * Title, CStdWindow * pParent, bool HideCursor) {
#ifndef USE_X11
return this;
#else
Active = true;
dpy = pApp->dpy;
@ -181,11 +176,9 @@ CStdWindow * CStdWindow::Init(CStdApp * pApp, const char * Title, CStdWindow * p
renderwnd = wnd;
return this;
#endif // USE_X11
}
void CStdWindow::Clear() {
#ifdef USE_X11
// Destroy window
if (wnd) {
CStdAppPrivate::SetWindow(wnd, 0);
@ -198,10 +191,8 @@ void CStdWindow::Clear() {
XFlush(dpy);
}
wnd = renderwnd = 0;
#endif
}
#ifdef USE_X11
bool CStdWindow::FindInfo()
{
#ifdef USE_GL
@ -243,7 +234,6 @@ bool CStdWindow::FindInfo()
return true;
}
#endif // USE_X11
bool CStdWindow::StorePosition(const char *, const char *, bool) { return true; }
@ -253,7 +243,6 @@ bool CStdWindow::RestorePosition(const char *, const char *, bool) {
}
bool CStdWindow::GetSize(RECT * pRect) {
#ifdef USE_X11
Window winDummy;
unsigned int borderDummy;
int x, y;
@ -265,30 +254,21 @@ bool CStdWindow::GetSize(RECT * pRect) {
pRect->bottom = height + y;
pRect->top = y;
pRect->left = x;
#else
pRect->left = pRect->right = pRect->top = pRect->bottom = 0;
#endif
return true;
}
void CStdWindow::SetSize(unsigned int X, unsigned int Y) {
#ifdef USE_X11
XResizeWindow(dpy, wnd, X, Y);
#endif
}
void CStdWindow::SetTitle(const char * Title) {
#ifdef USE_X11
XTextProperty title_property;
StdStrBuf tbuf(Title, true);
char * tbufstr = tbuf.getMData();
XStringListToTextProperty(&tbufstr, 1, &title_property);
XSetWMName(dpy, wnd, &title_property);
#endif
}
void CStdWindow::FlashWindow() {
#ifdef USE_X11
// This tries to implement flashing via
// _NET_WM_STATE_DEMANDS_ATTENTION, but it simply does not work for me.
// -ck.
@ -314,10 +294,8 @@ void CStdWindow::FlashWindow() {
wm_hint->flags |= XUrgencyHint;
XSetWMHints(dpy, wnd, wm_hint);
}
#endif
}
#ifdef USE_X11
void CStdWindow::HandleMessage(XEvent& event)
{
if(event.type == FocusIn)
@ -346,4 +324,4 @@ void CStdWindow::HandleMessage(XEvent& event)
}
}
}
#endif
#endif // USE_X11