2007-04-16

This commit is contained in:
Anonymous Maarten committed 2015-12-03 01:37:37 +01:00
1 parent 78c27f03c8
commit e20673c2cd
120 files changed
+9578 -1036

No files matched your search

+88
View File
@@ -0,0 +1,88 @@
/************************************************************************
* Copyright (c) 2005-2007 tok@openlinux.org.uk *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#ifndef ABSTRACT_CONTAINER_H
#define ABSTRACT_CONTAINER_H
#include <string>
#include <list>
#include <sstream>
#include "m_exceptions.h"
template <class T>
class AbstractContainer {
public:
T & doGet(uint32_t);
void doRemove(uint32_t);
inline T & doAdd(const T & t) {
objs.push_back(t);
return *objs.rbegin();
}
void doRealRemove();
protected:
typedef typename std::list<T> Storage_T;
Storage_T objs;
typedef typename std::list<T>::iterator Storage_T_Iterator;
typedef typename std::list< Storage_T_Iterator > Iterator_Storage;
Iterator_Storage toBeRemoved;
private:
Storage_T_Iterator findById(uint32_t & id);
};
template <class T>
void AbstractContainer<T>::doRealRemove() {
typename Iterator_Storage::iterator i = toBeRemoved.begin();
while (i != toBeRemoved.end()) {
typename Iterator_Storage::iterator j = i++;
typename Storage_T::iterator ii = *j;
// remove the pointed-to object
objs.erase(ii);
// remove from list of lists
toBeRemoved.erase(j);
}
}
template <class T>
void AbstractContainer<T>::doRemove(uint32_t id) {
typename Storage_T::iterator i = findById(id);
toBeRemoved.push_back(i);
}
template <class T>
T & AbstractContainer<T>::doGet(uint32_t id) {
typename Storage_T::iterator i = findById(id);
return *i;
}
template <class T>
typename std::list<T>::iterator AbstractContainer<T>::findById(uint32_t & id) {
typename Storage_T::iterator i;
for (i = objs.begin(); i != objs.end(); ++i) {
if (i->id() == id)
return i;
}
std::ostringstream ostr;
ostr << id;
throw E_UNKNOWNKEY(ostr.str());
}
#endif
+22
View File
@@ -1,3 +1,25 @@
/************************************************************************
* Copyright (c) 2005-2007 tok@openlinux.org.uk *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#include <sstream>
#include "animation.h"
#include "m_exceptions.h"
+34 -4
View File
@@ -1,3 +1,25 @@
/************************************************************************
* Copyright (c) 2005-2007 tok@openlinux.org.uk *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#ifndef UTIL_ANIMATION_H
#define UTIL_ANIMATION_H
#include <vector>
@@ -5,8 +27,15 @@
#include "log.h"
namespace Util {
class Animation {
/** Capsules play-frames-in-sequence logic.
*
* This class only knows about the number of frames
* and the fps; it does have multiple flags that
* switch the behaviour whenever the 'last' frame is
* finished.
*/
class Animation {
public:
typedef enum {
STOPPED = 0,
@@ -23,7 +52,8 @@ namespace Util {
Animation(const Animation & o);
inline const uint16_t & getCurrentFrameNumber() { return currentFrame; }
inline void set(const Status doThis, const OnDone done = STOP) { status = doThis; onDone = done; }
inline const Status & get() { return status; }
inline const Status & get() const { return status; }
inline const OnDone & getDone() const { return onDone; }
void jumpToFrame(const uint16_t num, const Status andDo);
void update(const uint32_t & nowTicks);
typedef Loki::Functor<void> CallbackType;
@@ -33,7 +63,7 @@ namespace Util {
uint16_t numFrames;
uint32_t delay;
protected:
void flipFrame(bool forward);
void isDone();
Status status;
+64
View File
@@ -0,0 +1,64 @@
/************************************************************************
* Copyright (c) 2005-2007 tok@openlinux.org.uk *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#include "cell_iterator.h"
#include "log.h"
namespace Util {
float distance(const Vector3D & p1, const Vector3D & p2) {
float dx = p1.x - p2.x;
float dy = p1.y - p2.y;
float dz = p1.z - p2.z;
return sqrt(dx * dx + dy * dy + dz * dz);
}
float xz_angle(const Vector3D & from, const Vector3D & to) {
Vector3D rel_to(to);
rel_to = rel_to - from;
double res = atan(rel_to.x / rel_to.z) * 180.0f / M_PI;
if (rel_to.z < 0)
return 180.0f + res;
if (rel_to.z >= 0 && rel_to.x < 0)
return 360.0f + res;
return res;
}
bool CellIterator::isValid() const {
if (x < 0 || x > 255)
return false;
if (y < 0 || y > 255)
return false;
if (z < 0)
return false;
return (z < mapRef.getNumBlocksAtNew(x, y));
}
OpenGTA::Map::BlockInfo & CellIterator::getBlock() {
assert(isValid());
return *mapRef.getBlockAtNew(x, y, z);
}
#define IABS(v) ((v > 0) ? v : -v)
int CellIterator::distance(const CellIterator & o) const {
return (IABS(x - o.x) + IABS(y - o.y) + IABS(z - o.z));
}
}
+100
View File
@@ -0,0 +1,100 @@
/************************************************************************
* Copyright (c) 2005-2007 tok@openlinux.org.uk *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#ifndef UTIL_CELLITERATOR_H
#define UTIL_CELLITERATOR_H
#include <cassert>
#include "opengta.h"
#include "dataholder.h"
#include "math3d.h"
#include "SDL_video.h"
namespace Util {
float distance(const Vector3D & p1, const Vector3D & p2);
float xz_angle(const Vector3D & from, const Vector3D & to);
class CellIterator {
public:
CellIterator(const Vector3D & p) :
x(int(floor(p.x))), y(int(floor(p.z))), z(int(floor(p.y))),
mapRef(OpenGTA::MapHolder::Instance().get()) {}
CellIterator(OpenGTA::Map & map, int _x, int _y, int _z) :
x(_x), y(_y), z(_z), mapRef(map) {}
CellIterator(const CellIterator & o) :
x(o.x), y(o.y), z(o.z), mapRef(o.mapRef) {}
bool isValid() const;
int distance(const CellIterator & o) const;
bool operator == (const CellIterator & o) const {
return (x == o.x && y == o.y && z == o.z);
}
CellIterator operator = (const CellIterator & o) {
mapRef = o.mapRef;
x = o.x;
y = o.y;
z = o.z;
return *this;
}
CellIterator left() const {
CellIterator p(*this);
p.x -= 1;
return p;
}
CellIterator right() const {
CellIterator p(*this);
p.x += 1;
return p;
}
CellIterator top() const {
CellIterator p(*this);
p.y += 1;
return p;
}
CellIterator bottom() const {
CellIterator p(*this);
p.y -= 1;
return p;
}
CellIterator up() const {
CellIterator p(*this);
p.z += 1;
return p;
}
CellIterator down() const {
CellIterator p(*this);
p.z -= 1;
return p;
}
int x, y, z;
OpenGTA::Map::BlockInfo & getBlock();
private:
OpenGTA::Map & mapRef;
};
}
#endif
+39 -5
View File
@@ -1,8 +1,31 @@
/************************************************************************
* Copyright (c) 2005-2007 tok@openlinux.org.uk *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#include <cstdlib>
#include <physfs.h>
#include <sstream>
#include "m_exceptions.h"
#include "file_helper.h"
#include "buffercache.h"
#ifndef OGTA_DEFAULT_DATA_PATH
#define OGTA_DEFAULT_DATA_PATH "gtadata.zip"
@@ -10,12 +33,14 @@
#ifndef OGTA_DEFAULT_MOD_PATH
#define OGTA_DEFAULT_MOD_PATH ""
#endif
#ifndef OGTA_DEFAULT_HOME_PATH
#ifdef LINUX
#define OGTA_DEFAULT_HOME_PATH PHYSFS_getUserDir()
#elif WIN32
#define OGTA_DEFAULT_HOME_PATH "config"
#endif
//#ifdef LINUX
//#define OGTA_DEFAULT_HOME_PATH PHYSFS_getUserDir()
//#elif WIN32
//#define OGTA_DEFAULT_HOME_PATH "config"
//#endif
#define OGTA_DEFAULT_HOME_PATH PHYSFS_getBaseDir()
#endif
namespace Util {
@@ -83,4 +108,13 @@ namespace Util {
// take this one instead
return fd;
}
unsigned char* FileHelper::bufferFromVFS(PHYSFS_file *fd) const {
assert(fd);
unsigned int size = PHYSFS_fileLength(fd);
unsigned char* buffer = BufferCacheHolder::Instance().requestBuffer(size+1);
size = PHYSFS_read(fd, buffer, 1, size);
PHYSFS_close(fd);
return buffer;
}
}
+23
View File
@@ -1,3 +1,25 @@
/************************************************************************
* Copyright (c) 2005-2007 tok@openlinux.org.uk *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#ifndef UTIL_FILEHELPER_H
#define UTIL_FILEHELPER_H
@@ -17,6 +39,7 @@ namespace Util {
bool existsInSystemFS(const std::string & file) const;
bool existsInVFS(const std::string & file) const;
PHYSFS_file* openReadVFS(const std::string & file) const;
unsigned char* bufferFromVFS(PHYSFS_file*) const;
private:
std::string baseDataPath;
std::string modDataPath;
+46 -13
View File
@@ -1,9 +1,32 @@
/************************************************************************
* Copyright (c) 2005-2007 tok@openlinux.org.uk *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#ifndef UTIL_GUI_H
#define UTIL_GUI_H
#include <string>
#include <list>
#include <map>
#include <SDL.h>
#include "Singleton.h"
#include "animation.h"
#include "gl_pagedtexture.h"
#include "image_loader.h"
@@ -48,6 +71,9 @@ namespace GUI {
};
typedef Loki::SingletonHolder<Manager, Loki::CreateUsingNew, Loki::DefaultLifetime,
Loki::SingleThreaded> ManagerHolder;
class Animation : public Util::Animation {
public:
Animation(const std::vector<uint16_t> & _indices, const uint16_t fps) :
@@ -65,20 +91,11 @@ namespace GUI {
size_t id;
SDL_Rect rect;
SDL_Color color;
inline void copyRect(const SDL_Rect & src) {
rect.x = src.x;
rect.y = src.y;
rect.w = src.w;
rect.h = src.h;
}
inline void copyColor(const SDL_Color & src) {
color.r = src.r;
color.g = src.g;
color.b = src.b;
color.unused = src.unused;
}
void copyRect(const SDL_Rect & src);
void copyColor(const SDL_Color & src);
virtual void draw();
Manager * manager;
virtual void update(Uint32 ticks) {}
Manager & manager;
};
struct TexturedObject : public Object {
@@ -121,5 +138,21 @@ namespace GUI {
std::string text;
void draw();
};
struct Pager : public Object {
Pager(const size_t Id, const SDL_Rect & r, const size_t texid,
const std::string & fontFile, const size_t fontScale) : Object(Id, r) {
OpenGL::DrawableFont & fnt = OpenGTA::FontCacheHolder::Instance().getFont(fontFile, fontScale);
font = &fnt;
texId = texid;
offset = r.w-5;
}
OpenGL::DrawableFont * font;
size_t texId;
void update(Uint32 ticks);
void draw();
std::string lastMsg;
int offset;
};
}
#endif
+22
View File
@@ -1,3 +1,25 @@
/************************************************************************
* Copyright (c) 2005-2007 tok@openlinux.org.uk *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#include <cstddef>
#include <physfs.h>
#include "image_loader.h"
+22
View File
@@ -1,3 +1,25 @@
/************************************************************************
* Copyright (c) 2005-2007 tok@openlinux.org.uk *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#ifndef UTIL_IMAGE_LOADER_H
#define UTIL_IMAGE_LOADER_H
+14
View File
@@ -0,0 +1,14 @@
#include "map_helper.h"
#include "log.h"
namespace Util {
void SpriteCreationArea::setRects(const SDL_Rect & allowed, const SDL_Rect & denied) {
validRects = std::make_pair<SDL_Rect, SDL_Rect>(allowed, denied);
}
TupleOfUint8 SpriteCreationArea::getValidCoord() {
uint32_t x = rnd.nextUint(validRects.first.w) + validRects.first.x;
uint32_t y = rnd.nextUint(validRects.first.h) + validRects.first.y;
return std::make_pair<uint8_t, uint8_t>(x, y);
}
}
+22
View File
@@ -0,0 +1,22 @@
#ifndef MAP_HELPER_H
#define MAP_HELPER_H
#include <list>
#include <SDL_video.h>
#include "yasli/random.h"
namespace Util {
typedef std::pair<SDL_Rect, SDL_Rect> TupleOfRects;
typedef std::pair<uint8_t, uint8_t> TupleOfUint8;
typedef std::list< TupleOfRects > ListOfTupleOfRects;
class SpriteCreationArea {
public:
//ListOfTupleOfRects validTuple;
TupleOfRects validRects;
void setRects(const SDL_Rect & allowed, const SDL_Rect & denied);
TupleOfUint8 getValidCoord();
Random rnd;
};
}
#endif
+191
View File
@@ -0,0 +1,191 @@
/*
* This code provides a glue layer between PhysicsFS and Simple Directmedia
* Layer's (SDL) RWops i/o abstraction.
*
* License: this code is public domain. I make no warranty that it is useful,
* correct, harmless, or environmentally safe.
*
* This particular file may be used however you like, including copying it
* verbatim into a closed-source project, exploiting it commercially, and
* removing any trace of my name from the source (although I hope you won't
* do that). I welcome enhancements and corrections to this file, but I do
* not require you to send me patches if you make changes.
*
* Unless otherwise stated, the rest of PhysicsFS falls under the GNU Lesser
* General Public License: http://www.gnu.org/licenses/lgpl.txt
*
* SDL falls under the LGPL, too. You can get SDL at http://www.libsdl.org/
*
* This file was written by Ryan C. Gordon. (icculus@clutteredmind.org).
*/
#include <stdio.h> /* used for SEEK_SET, SEEK_CUR, SEEK_END ... */
#include "physfsrwops.h"
static int physfsrwops_seek(SDL_RWops *rw, int offset, int whence)
{
PHYSFS_file *handle = (PHYSFS_file *) rw->hidden.unknown.data1;
int pos = 0;
if (whence == SEEK_SET)
{
pos = offset;
} /* if */
else if (whence == SEEK_CUR)
{
PHYSFS_sint64 current = PHYSFS_tell(handle);
if (current == -1)
{
SDL_SetError("Can't find position in file: %s",
PHYSFS_getLastError());
return(-1);
} /* if */
pos = (int) current;
if ( ((PHYSFS_sint64) pos) != current )
{
SDL_SetError("Can't fit current file position in an int!");
return(-1);
} /* if */
if (offset == 0) /* this is a "tell" call. We're done. */
return(pos);
pos += offset;
} /* else if */
else if (whence == SEEK_END)
{
PHYSFS_sint64 len = PHYSFS_fileLength(handle);
if (len == -1)
{
SDL_SetError("Can't find end of file: %s", PHYSFS_getLastError());
return(-1);
} /* if */
pos = (int) len;
if ( ((PHYSFS_sint64) pos) != len )
{
SDL_SetError("Can't fit end-of-file position in an int!");
return(-1);
} /* if */
pos += offset;
} /* else if */
else
{
SDL_SetError("Invalid 'whence' parameter.");
return(-1);
} /* else */
if ( pos < 0 )
{
SDL_SetError("Attempt to seek past start of file.");
return(-1);
} /* if */
if (!PHYSFS_seek(handle, (PHYSFS_uint64) pos))
{
SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
return(-1);
} /* if */
return(pos);
} /* physfsrwops_seek */
static int physfsrwops_read(SDL_RWops *rw, void *ptr, int size, int maxnum)
{
PHYSFS_file *handle = (PHYSFS_file *) rw->hidden.unknown.data1;
PHYSFS_sint64 rc = PHYSFS_read(handle, ptr, size, maxnum);
if (rc != maxnum)
{
if (!PHYSFS_eof(handle)) /* not EOF? Must be an error. */
SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
} /* if */
return((int) rc);
} /* physfsrwops_read */
static int physfsrwops_write(SDL_RWops *rw, const void *ptr, int size, int num)
{
PHYSFS_file *handle = (PHYSFS_file *) rw->hidden.unknown.data1;
PHYSFS_sint64 rc = PHYSFS_write(handle, ptr, size, num);
if (rc != num)
SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
return((int) rc);
} /* physfsrwops_write */
static int physfsrwops_close(SDL_RWops *rw)
{
PHYSFS_file *handle = (PHYSFS_file *) rw->hidden.unknown.data1;
if (!PHYSFS_close(handle))
{
SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
return(-1);
} /* if */
SDL_FreeRW(rw);
return(0);
} /* physfsrwops_close */
static SDL_RWops *create_rwops(PHYSFS_file *handle)
{
SDL_RWops *retval = NULL;
if (handle == NULL)
SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
else
{
retval = SDL_AllocRW();
if (retval != NULL)
{
retval->seek = physfsrwops_seek;
retval->read = physfsrwops_read;
retval->write = physfsrwops_write;
retval->close = physfsrwops_close;
retval->hidden.unknown.data1 = handle;
} /* if */
} /* else */
return(retval);
} /* create_rwops */
SDL_RWops *PHYSFSRWOPS_makeRWops(PHYSFS_file *handle)
{
SDL_RWops *retval = NULL;
if (handle == NULL)
SDL_SetError("NULL pointer passed to PHYSFSRWOPS_makeRWops().");
else
retval = create_rwops(handle);
return(retval);
} /* PHYSFSRWOPS_makeRWops */
SDL_RWops *PHYSFSRWOPS_openRead(const char *fname)
{
return(create_rwops(PHYSFS_openRead(fname)));
} /* PHYSFSRWOPS_openRead */
SDL_RWops *PHYSFSRWOPS_openWrite(const char *fname)
{
return(create_rwops(PHYSFS_openWrite(fname)));
} /* PHYSFSRWOPS_openWrite */
SDL_RWops *PHYSFSRWOPS_openAppend(const char *fname)
{
return(create_rwops(PHYSFS_openAppend(fname)));
} /* PHYSFSRWOPS_openAppend */
/* end of physfsrwops.c ... */
+87
View File
@@ -0,0 +1,87 @@
/*
* This code provides a glue layer between PhysicsFS and Simple Directmedia
* Layer's (SDL) RWops i/o abstraction.
*
* License: this code is public domain. I make no warranty that it is useful,
* correct, harmless, or environmentally safe.
*
* This particular file may be used however you like, including copying it
* verbatim into a closed-source project, exploiting it commercially, and
* removing any trace of my name from the source (although I hope you won't
* do that). I welcome enhancements and corrections to this file, but I do
* not require you to send me patches if you make changes.
*
* Unless otherwise stated, the rest of PhysicsFS falls under the GNU Lesser
* General Public License: http://www.gnu.org/licenses/lgpl.txt
*
* SDL falls under the LGPL, too. You can get SDL at http://www.libsdl.org/
*
* This file was written by Ryan C. Gordon. (icculus@clutteredmind.org).
*/
#ifndef _INCLUDE_PHYSFSRWOPS_H_
#define _INCLUDE_PHYSFSRWOPS_H_
#include "physfs.h"
#include "SDL.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* Open a platform-independent filename for reading, and make it accessible
* via an SDL_RWops structure. The file will be closed in PhysicsFS when the
* RWops is closed. PhysicsFS should be configured to your liking before
* opening files through this method.
*
* @param filename File to open in platform-independent notation.
* @return A valid SDL_RWops structure on success, NULL on error. Specifics
* of the error can be gleaned from PHYSFS_getLastError().
*/
__EXPORT__ SDL_RWops *PHYSFSRWOPS_openRead(const char *fname);
/**
* Open a platform-independent filename for writing, and make it accessible
* via an SDL_RWops structure. The file will be closed in PhysicsFS when the
* RWops is closed. PhysicsFS should be configured to your liking before
* opening files through this method.
*
* @param filename File to open in platform-independent notation.
* @return A valid SDL_RWops structure on success, NULL on error. Specifics
* of the error can be gleaned from PHYSFS_getLastError().
*/
__EXPORT__ SDL_RWops *PHYSFSRWOPS_openWrite(const char *fname);
/**
* Open a platform-independent filename for appending, and make it accessible
* via an SDL_RWops structure. The file will be closed in PhysicsFS when the
* RWops is closed. PhysicsFS should be configured to your liking before
* opening files through this method.
*
* @param filename File to open in platform-independent notation.
* @return A valid SDL_RWops structure on success, NULL on error. Specifics
* of the error can be gleaned from PHYSFS_getLastError().
*/
__EXPORT__ SDL_RWops *PHYSFSRWOPS_openAppend(const char *fname);
/**
* Make a SDL_RWops from an existing PhysicsFS file handle. You should
* dispose of any references to the handle after successful creation of
* the RWops. The actual PhysicsFS handle will be destroyed when the
* RWops is closed.
*
* @param handle a valid PhysicsFS file handle.
* @return A valid SDL_RWops structure on success, NULL on error. Specifics
* of the error can be gleaned from PHYSFS_getLastError().
*/
__EXPORT__ SDL_RWops *PHYSFSRWOPS_makeRWops(PHYSFS_file *handle);
#ifdef __cplusplus
}
#endif
#endif /* include-once blocker */
/* end of physfsrwops.h ... */
+29 -5
View File
@@ -1,3 +1,26 @@
/************************************************************************
* Copyright (c) 2005-2007 tok@openlinux.org.uk *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#ifdef WITH_SOUND
#include <SDL_sound.h>
#include <SDL_mixer.h>
#include "sound_device.h"
@@ -15,14 +38,13 @@ namespace Audio {
}
SoundDevice::~SoundDevice() {
Mix_HaltGroup(-1);
Mix_HaltMusic();
close();
}
void SoundDevice::close() {
if (!status)
return;
Mix_HaltMusic();
Mix_CloseAudio();
status = CLOSED;
}
@@ -35,9 +57,10 @@ namespace Audio {
SDL_AudioDriverName (cardName, sizeof (cardName));
if (!Mix_QuerySpec(&rate, &format, &channels))
throw E_NOTSUPPORTED(SDL_GetError());
INFO << "Sound-device [" << cardName << "] opened at " << rate << " Hz " <<
(format & 0xff) << " bit " << channels << " channels " << bufSize <<
" buffer size" << std::endl;
INFO <<
"Sound-device [" << cardName << "], rate " << rate << " Hz, " <<
(format & 0xff) << " bit, " << std::endl << " " <<
channels << " channels, " << bufSize << " buffer size" << std::endl;
status = OPEN;
}
@@ -50,3 +73,4 @@ namespace Audio {
open();
}
}
#endif
+26
View File
@@ -1,6 +1,30 @@
/************************************************************************
* Copyright (c) 2005-2007 tok@openlinux.org.uk *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#ifndef UTIL_SOUND_DEVICE_H
#define UTIL_SOUND_DEVICE_H
#ifdef WITH_SOUND
#include <SDL_stdinc.h>
namespace Audio {
@@ -15,6 +39,7 @@ namespace Audio {
inline const int getRate() { return rate; }
inline const int getNumChannels() { return channels; }
inline const int getBufferSize() { return bufSize; }
inline const Uint16 getFormat() { return format; }
enum Status {
CLOSED = 0,
OPEN,
@@ -30,5 +55,6 @@ namespace Audio {
Status status;
};
}
#endif
#endif
+59
View File
@@ -0,0 +1,59 @@
/************************************************************************
* Copyright (c) 2005-2007 tok@openlinux.org.uk *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#ifdef WITH_SOUND
#include "sound_fx_cache.h"
#include "sound_resample2.h"
namespace Audio {
ChunkData & AudioChunkCache::getChunk(std::string & file, size_t idx) {
ChunkId id(file, idx);
CacheType::iterator i = cached.find(id);
if (i == cached.end()) {
ChunkData c = loadChunk(file, idx);
cached.insert(std::make_pair<ChunkId, ChunkData>(id, c));
i = cached.find(id);
}
return i->second;
}
ChunkData AudioChunkCache::loadChunk(std::string & file, size_t idx) {
LookupCache::iterator j = lookup.find(file);
if (j == lookup.end()) {
prepareDB(file);
j = lookup.find(file);
}
OpenGTA::SoundsDB & db = *j->second;
OpenGTA::SoundsDB::Entry & e = db.getEntry(idx);
size_t si;
unsigned char* mem = db.getBuffered(idx);
Uint8 *mem2 = (Uint8*)Audio::resample_new(mem, e.rawSize, si, e.sampleRate, 44100);
Mix_Chunk * music = Mix_QuickLoad_RAW(mem2, si);
return ChunkData(mem2, music, 1);
}
void AudioChunkCache::prepareDB(std::string db) {
lookup[db] = new OpenGTA::SoundsDB(db);
}
}
#endif
+76
View File
@@ -0,0 +1,76 @@
/************************************************************************
* Copyright (c) 2005-2007 tok@openlinux.org.uk *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#ifndef SOUND_FX_CACHE_H
#define SOUND_FX_CACHE_H
#ifdef WITH_SOUND
#include <map>
#include <string>
#include <SDL_mixer.h>
#include "fx_sdt.h"
namespace Audio {
struct ChunkId {
std::string src_file;
size_t idx_in_file;
ChunkId(const std::string & file, const size_t idx) :
src_file(file), idx_in_file(idx) {}
bool operator == (const ChunkId & o) const {
return (idx_in_file == o.idx_in_file && src_file == o.src_file);
}
bool operator < (const ChunkId & o) const {
if (idx_in_file < o.idx_in_file)
return true;
if (idx_in_file > o.idx_in_file)
return false;
return src_file < o.src_file;
}
};
struct ChunkData {
Uint8 * mem_buf;
Mix_Chunk * chunk;
size_t ref;
ChunkData(Uint8 * m, Mix_Chunk * c, size_t r = 1) :
mem_buf(m), chunk(c), ref(r) {}
ChunkData(const ChunkData & o) :
mem_buf(o.mem_buf), chunk(o.chunk), ref(o.ref) {}
};
class AudioChunkCache {
public:
typedef std::map< ChunkId, ChunkData > CacheType;
CacheType cached;
ChunkData & getChunk(std::string & file, size_t idx);
OpenGTA::SoundsDB * getDB(std::string db_file);
private:
void prepareDB(std::string db_file);
ChunkData loadChunk(std::string & file, size_t idx);
typedef std::map< std::string, OpenGTA::SoundsDB* > LookupCache;
LookupCache lookup;
};
}
#endif
#endif
+88
View File
@@ -0,0 +1,88 @@
/************************************************************************
* Copyright (c) 2005-2007 tok@openlinux.org.uk *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#ifdef WITH_SOUND
#include <string>
#include "m_exceptions.h"
#include "sound_music_player.h"
namespace Audio {
bool MusicPlayerCtrl::isPlaying = false;
Sound_Sample * MusicPlayerCtrl::music = 0;
MusicFinishedCallbackType MusicPlayerCtrl::musicFinishedCB;
void MusicPlayerCtrl::clear() {
if (MusicPlayerCtrl::isPlaying)
MusicPlayerCtrl::isPlaying = false;
if (MusicPlayerCtrl::music) {
Sound_FreeSample(MusicPlayerCtrl::music);
MusicPlayerCtrl::music = 0;
}
if (MusicPlayerCtrl::musicFinishedCB)
MusicPlayerCtrl::musicFinishedCB();
}
// taken from an example on the web, can't remember where...
// slightly simplified: removed looping
void musicPlayerFunc(void *udata, Uint8 *stream, int len) {
int i,act=0;
Sint16 *ptr2;
if (stream == 0)
throw E_INVALIDFORMAT("Encountered a zero music stream while playing :-(");
ptr2=(Sint16 *)stream;
if (MusicPlayerCtrl::isPlaying) {
while (act < len) {
if (MusicPlayerCtrl::music != 0) {
if ((MusicPlayerCtrl::music->flags&SOUND_SAMPLEFLAG_EOF)) {
MusicPlayerCtrl::clear();
}
else {
/* In the middle of the file: */
int decoded=0;
Sint16 *ptr;
Sound_SetBufferSize(MusicPlayerCtrl::music, len-act);
decoded=Sound_Decode(MusicPlayerCtrl::music);
ptr=(Sint16 *)MusicPlayerCtrl::music->buffer;
for(i=0;i<decoded;i+=2,ptr++,ptr2++) {
*ptr2=((Sint32(*ptr)*Sint32(music_volume))/127);
} /* for */
act+=decoded;
}
}
else {
for(i=act;i<len;i++) stream[i]=0;
act=len;
}
}
}
else { // not playing; fill with zero
for(i=0;i<len;i++) stream[i]=0;
}
}
}
#endif
+44
View File
@@ -0,0 +1,44 @@
/************************************************************************
* Copyright (c) 2005-2007 tok@openlinux.org.uk *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#ifndef SOUND_MUSIC_PLAYER_H
#define SOUND_MUSIC_PLAYER_H
#ifdef WITH_SOUND
#include <SDL_sound.h>
#include "Functor.h"
namespace Audio {
static int music_volume = 127;
typedef Loki::Functor<void> MusicFinishedCallbackType;
struct MusicPlayerCtrl {
static bool isPlaying;
static Sound_Sample *music;
static void clear();
static MusicFinishedCallbackType musicFinishedCB;
};
void musicPlayerFunc(void *udata, Uint8 *stream, int len);
}
#endif
#endif
+92
View File
@@ -0,0 +1,92 @@
/************************************************************************
* Copyright (c) 2005-2007 tok@openlinux.org.uk *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#include "sound_resample2.h"
#include "interpolate.hpp"
namespace Audio {
// resample uint8 mono to Sint16 stereo
// assumption: src_rate < wanted_rate
// derived from some sweet code found in the exult repository,
// now rewritten to avoid license trouble...
// yes, it is (still) similiar - who would have guessed?
Sint16 *resample_new (Uint8 * src, size_t sourcelen, size_t & size,
int rate, int wanted_rate) {
int fp_pos = 0;
int fp_speed = (1 << 16) * rate / wanted_rate;
size = sourcelen;
// adjust the magnitudes of size and rate to prevent division error
while (size & 0xFFFF0000)
size >>= 1, rate = (rate >> 1) + 1;
// Compute the output size (times 4 since it is 16 stereo)
size = (size * wanted_rate / rate) << 2;
Sint16 *stereo_data = new Sint16[size];
Sint16 *data = stereo_data;
Uint8 *src_end = src + sourcelen;
int result;
// Compute the initial data feed for the interpolator. We don't simply
// shift by 8, but rather duplicate the byte, this way we cover the full
// range. Probably doesn't make a big difference, listening wise :-)
int a = *(src + 0);
a = (a | (a << 8)) - 32768;
int b = *(src + 1);
b = (a | (b << 8)) - 32768;
int c = *(src + 2);
c = (a | (c << 8)) - 32768;
// scale down by 0.8333 to avoid oversampling
Math::Interpolator::Cubic<float> inter(a * 0.8333f, b * 0.8333f, c * 0.8333f);
do
{
do
{
result = int(inter.getAt(fp_pos / float(0x0000FFFF)));
// limit the range
if (result < -32768)
result = -32768;
else if (result > 32767)
result = 32767;
*data++ = result;
*data++ = result;
fp_pos += fp_speed;
}
while (!(fp_pos & 0xFFFF0000)); // need more input data
src++;
fp_pos &= 0x0000FFFF;
if (src + 2 < src_end) // still data to fetch
{
c = *(src + 2);
c = (c | (c << 8)) - 32768;
inter.shiftAndFeed (c * 0.8333f);
}
else // guess & interpolate... this the end, my only friend the end
inter.shift ();
}
while (src < src_end);
return stereo_data;
}
}
+35
View File
@@ -0,0 +1,35 @@
/************************************************************************
* Copyright (c) 2005-2007 tok@openlinux.org.uk *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#ifndef SOUND_RESAMPLE_H2
#define SOUND_RESAMPLE_H2
#include <SDL_types.h>
namespace Audio
{
Sint16 *resample_new (Uint8 * sourcedata, size_t sourcelen,
size_t & destlen, int current_rate, int wanted_rate);
Sint16 *resample_new_mono (Uint8 * sourcedata, size_t sourcelen,
size_t & destlen, int current_rate,
int wanted_rate);
}
#endif
+100
View File
@@ -0,0 +1,100 @@
#include <iostream>
#include <iomanip>
#include "sound_system.h"
#include "physfsrwops.h"
#include "m_exceptions.h"
#include "log.h"
namespace Audio {
#ifdef WITH_SOUND
SoundSystem::SoundSystem() : device(), chunkCache() {
SDL_InitSubSystem(SDL_INIT_AUDIO);
device.open();
enabled = true;
if (device.getStatus() == SoundDevice::OPEN)
Sound_Init();
else {
WARN << "Could not open audio-device - disabling sound!" << std::endl;
enabled = false;
}
}
SoundSystem::~SoundSystem() {
device.close();
Sound_Quit();
}
void SoundSystem::playFx(std::string file, size_t idx) {
if (!enabled)
return;
ChunkData & cd = chunkCache.getChunk(file, idx);
Mix_PlayChannel(0, cd.chunk, 0);
}
void SoundSystem::playMusic(std::string file) {
if (!enabled)
return;
Sound_AudioInfo inf;
inf.format = device.getFormat();
inf.channels = device.getNumChannels();
inf.rate = device.getRate();
MusicPlayerCtrl::clear();
SDL_RWops * rw = PHYSFSRWOPS_openRead(file.c_str());
if (!rw)
throw E_FILENOTFOUND(file);
size_t pos = file.rfind('.');
std::string ext(file.substr(pos + 1, pos - 1));
MusicPlayerCtrl::music = Sound_NewSample(rw, ext.c_str(), &inf, device.getBufferSize());
if (!MusicPlayerCtrl::music)
throw E_NOTSUPPORTED("file: " + file + " - " + Sound_GetError());
Mix_HookMusic(musicPlayerFunc, 0);
MusicPlayerCtrl::isPlaying = true;
}
void SoundSystem::listMusicDecoders() {
std::cout << "* Supported music decoders *" << std::endl;
const Sound_DecoderInfo **i;
for (i = Sound_AvailableDecoders(); *i != NULL; i++) {
std::cout << std::setfill(' ') << std::setw(5) <<
(*i)->extensions[0] << " : " << (*i)->description << std::endl;
}
}
#else
SoundSystem::SoundSystem() : enabled(false) {
}
SoundSystem::~SoundSystem() {}
void SoundSystem::playFx(std::string file, size_t idx) {}
void SoundSystem::playMusic(std::string file) {}
void SoundSystem::listMusicDecoders() {}
#endif
void CB_MusicDone() {
std::cout << "music finished" << std::endl;
}
}
#ifdef SOUND_TEST
using namespace Audio;
int main(int argc, char* argv[]) {
PHYSFS_init(argv[0]);
PHYSFS_addToSearchPath("gtadata.zip", 1);
PHYSFS_addToSearchPath(PHYSFS_getBaseDir(), 1);
SoundSystem noisemaker;
noisemaker.listMusicDecoders();
noisemaker.playMusic(argv[1]);
MusicPlayerCtrl::musicFinishedCB = CB_MusicDone;
while (MusicPlayerCtrl::isPlaying) {
SDL_Delay(1000);
}
}
#endif
+27
View File
@@ -0,0 +1,27 @@
#ifndef SOUND_SYSTEM_H
#define SOUND_SYSTEM_H
#ifdef WITH_SOUND
#include "sound_device.h"
#include "sound_music_player.h"
#include "sound_fx_cache.h"
#endif
namespace Audio {
class SoundSystem {
#ifdef WITH_SOUND
private:
SoundDevice device;
AudioChunkCache chunkCache;
#endif
public:
SoundSystem();
~SoundSystem();
void playFx(std::string file, size_t idx);
void SoundSystem::playMusic(std::string file);
void SoundSystem::listMusicDecoders();
bool enabled;
};
}
#endif
+43 -2
View File
@@ -1,6 +1,28 @@
/************************************************************************
* Copyright (c) 2005-2007 tok@openlinux.org.uk *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#include <SDL.h>
#include "timer.h"
#include <iostream>
#include "timer.h"
Timer::TimeEvent::TimeEvent(const uint32_t & b, const uint32_t e, CallbackType & c) :
begin(b), end(e), callback(c) {}
@@ -12,18 +34,30 @@ Timer::TimeEvent::TimeEvent(const TimeEvent & o) :
begin(o.begin), end(o.end), callback(o.callback) {}
Timer::Timer() {
simIsRunning = false;
#ifdef TIMER_OPENSTEER_CLOCK
sdlTicks = uint32_t(floor(clock.realTimeSinceFirstClockUpdate()*1000));
clock.setPausedState(simIsRunning);
#else
sdlTicks = SDL_GetTicks();
#endif
simTicks = 0;
delta = 0;
simIsRunning = false;
}
Timer::~Timer() {
clearAllEvents();
}
void Timer::update() {
#ifdef TIMER_OPENSTEER_CLOCK
if (simIsRunning)
clock.update();
uint32_t nowTicks = uint32_t(floor(clock.realTimeSinceFirstClockUpdate()*1000));
#else
uint32_t nowTicks = SDL_GetTicks();
#endif
delta = nowTicks - sdlTicks;
sdlTicks = nowTicks;
if (simIsRunning)
@@ -36,6 +70,13 @@ void Timer::update() {
checkSimEvents();
}
void Timer::setSimulationRunning(bool yes) {
simIsRunning = yes;
#ifdef TIMER_OPENSTEER_CLOCK
clock.setPausedState(!yes);
#endif
}
void Timer::checkRTEvents() {
RealTimeMap::iterator i = realTimeEvents.begin();
while (i != realTimeEvents.end() && i->first <= sdlTicks) {
+33 -1
View File
@@ -1,9 +1,38 @@
/************************************************************************
* Copyright (c) 2005-2007 tok@openlinux.org.uk *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#ifndef OGTA_TIMER_H
#define OGTA_TIMER_H
#include <map>
#include "Singleton.h"
#include "Functor.h"
// instead of SDL_GetTicks()
#define TIMER_OPENSTEER_CLOCK
#ifdef TIMER_OPENSTEER_CLOCK
#include "OpenSteer/Clock.h"
#endif
class Timer {
public:
typedef Loki::Functor<void, LOKI_TYPELIST_1(float)> CallbackType;
@@ -22,11 +51,14 @@ class Timer {
// time since SDL_Init
inline const uint32_t & getRealTime() const { return sdlTicks; }
void update();
inline void setSimulationRunning(bool yes) { simIsRunning = yes; }
void setSimulationRunning(bool yes);
inline bool getSimulationRunning() const { return simIsRunning; }
void registerCallback(bool simTime, CallbackType & c, const uint32_t & b, const uint32_t & e);
void registerCallback(bool simTime, CallbackType & c, const uint32_t & b);
void clearAllEvents();
#ifdef TIMER_OPENSTEER_CLOCK
OpenSteer::Clock clock;
#endif
private:
void checkRTEvents();
void checkSimEvents();