2006-12-10
This commit is contained in:
commit
78c27f03c8
153 files changed
+24678
No files matched your search
@@ -0,0 +1,94 @@
|
||||
#include <sstream>
|
||||
#include "animation.h"
|
||||
#include "m_exceptions.h"
|
||||
|
||||
namespace Util {
|
||||
Animation::Animation(uint16_t num, uint16_t fps) :
|
||||
callback() {
|
||||
status = STOPPED;
|
||||
numFrames = num;
|
||||
currentFrame = 0;
|
||||
delay = 1000 / fps;
|
||||
lastChangeTicks = 0;
|
||||
}
|
||||
|
||||
Animation::Animation(const Animation & other) {
|
||||
status = other.status;
|
||||
onDone = other.onDone;
|
||||
numFrames = other.numFrames;
|
||||
currentFrame = other.currentFrame;
|
||||
delay = other.delay;
|
||||
|
||||
lastChangeTicks = other.lastChangeTicks;
|
||||
callback = other.callback;
|
||||
}
|
||||
|
||||
void Animation::update(const uint32_t & nowTicks) {
|
||||
if (status == STOPPED)
|
||||
return;
|
||||
if (lastChangeTicks == 0)
|
||||
lastChangeTicks = nowTicks;
|
||||
if (nowTicks < lastChangeTicks + delay)
|
||||
return;
|
||||
lastChangeTicks = nowTicks;
|
||||
if (status == PLAY_FORWARD)
|
||||
flipFrame(true);
|
||||
else if (status == PLAY_BACKWARD)
|
||||
flipFrame(false);
|
||||
}
|
||||
|
||||
void Animation::flipFrame(bool forward = true) {
|
||||
switch(forward) {
|
||||
case true:
|
||||
if (currentFrame < numFrames - 1)
|
||||
++currentFrame;
|
||||
else if (currentFrame == numFrames - 1)
|
||||
isDone();
|
||||
break;
|
||||
case false:
|
||||
if (currentFrame == 0)
|
||||
isDone();
|
||||
else
|
||||
--currentFrame;
|
||||
}
|
||||
}
|
||||
|
||||
void Animation::jumpToFrame(const uint16_t num, const Status andDo) {
|
||||
if (num >= numFrames) {
|
||||
std::ostringstream o;
|
||||
o << num << " >= " << numFrames;
|
||||
throw E_OUTOFRANGE(o.str());
|
||||
}
|
||||
currentFrame = num;
|
||||
status = andDo;
|
||||
}
|
||||
|
||||
void Animation::isDone() {
|
||||
if (onDone == STOP) {
|
||||
status = STOPPED;
|
||||
return;
|
||||
}
|
||||
if (onDone == REVERSE) {
|
||||
status = (status == PLAY_FORWARD) ? PLAY_BACKWARD : PLAY_FORWARD;
|
||||
return;
|
||||
}
|
||||
if (onDone == LOOP) {
|
||||
if (status == PLAY_FORWARD)
|
||||
jumpToFrame(0, PLAY_FORWARD);
|
||||
else if (status == PLAY_BACKWARD)
|
||||
jumpToFrame(numFrames - 1, PLAY_BACKWARD);
|
||||
return;
|
||||
}
|
||||
status = STOPPED;
|
||||
if (onDone == FCALLBACK) {
|
||||
if (callback)
|
||||
callback();
|
||||
else
|
||||
ERROR << "Wanted to call callback, but nobody was there" << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AnimCallback() {
|
||||
WARN << "EmptyAnimCallback called" << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#ifndef UTIL_ANIMATION_H
|
||||
#define UTIL_ANIMATION_H
|
||||
#include <vector>
|
||||
#include "Functor.h"
|
||||
#include "log.h"
|
||||
|
||||
namespace Util {
|
||||
|
||||
class Animation {
|
||||
public:
|
||||
typedef enum {
|
||||
STOPPED = 0,
|
||||
PLAY_FORWARD,
|
||||
PLAY_BACKWARD
|
||||
} Status;
|
||||
typedef enum {
|
||||
STOP = 0,
|
||||
REVERSE,
|
||||
LOOP,
|
||||
FCALLBACK
|
||||
} OnDone;
|
||||
Animation(uint16_t numFrames, uint16_t fps);
|
||||
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; }
|
||||
void jumpToFrame(const uint16_t num, const Status andDo);
|
||||
void update(const uint32_t & nowTicks);
|
||||
typedef Loki::Functor<void> CallbackType;
|
||||
void setCallback(CallbackType & cb) { callback = cb; }
|
||||
|
||||
uint16_t currentFrame;
|
||||
uint16_t numFrames;
|
||||
uint32_t delay;
|
||||
protected:
|
||||
|
||||
void flipFrame(bool forward);
|
||||
void isDone();
|
||||
Status status;
|
||||
OnDone onDone;
|
||||
uint32_t lastChangeTicks;
|
||||
|
||||
CallbackType callback;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,147 @@
|
||||
/************************************************************************
|
||||
* Copyright (c) 2005-2006 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 <iostream>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <cassert>
|
||||
#include "buffercache.h"
|
||||
#include "log.h"
|
||||
#include "m_exceptions.h"
|
||||
|
||||
namespace Util {
|
||||
|
||||
BufferCache::BufferCache() {
|
||||
total_bytes = 0;
|
||||
}
|
||||
BufferCache::~BufferCache() {
|
||||
BufferMap_t::const_iterator i = allocated.begin();
|
||||
while (i != allocated.end()) {
|
||||
freeBuffer(i);
|
||||
i++;
|
||||
}
|
||||
allocated.clear();
|
||||
BufferMap_T::const_iterator j = locked.begin();
|
||||
if (locked.size())
|
||||
WARN << "Locked elements remaining in cache: " << locked.size() << std::endl;
|
||||
while (j != locked.end()) {
|
||||
delete [] j->first;
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned char* BufferCache::requestBuffer(unsigned int len) {
|
||||
unsigned char *result = NULL;
|
||||
BufferMap_t::const_iterator i = findMatchingBuffer(len);
|
||||
if (i == allocated.end()) {
|
||||
INFO << "No matching buffer for size: " << len << std::endl;
|
||||
result = createBuffer(len);
|
||||
if (result == NULL)
|
||||
throw E_OUTOFMEMORY("Failed to allocate buffer");
|
||||
//throw std::string("Error: out of memory");
|
||||
allocated[len] =result;
|
||||
printStatus();
|
||||
}
|
||||
else {
|
||||
result = i->second;
|
||||
}
|
||||
std::memset(result, 0, len);
|
||||
return result;
|
||||
}
|
||||
|
||||
unsigned char* BufferCache::requestLockedBuffer(unsigned int len) {
|
||||
unsigned char *result = requestBuffer(len);
|
||||
lockBuffer(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
void BufferCache::lockBuffer(unsigned char* tb) {
|
||||
BufferMap_t::const_iterator i = allocated.begin();
|
||||
while (i != allocated.end()) {
|
||||
if (i->second == tb) {
|
||||
locked[i->second] = i->first;
|
||||
allocated.erase(i->first);
|
||||
return;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
std::ostringstream o;
|
||||
o << "Cannot lock unknown buffer " << tb;
|
||||
throw E_UNKNOWNKEY(o.str());
|
||||
//throw std::string("Unknown buffer - cannot lock it");
|
||||
}
|
||||
|
||||
void BufferCache::unlockBuffer(unsigned char* tb) {
|
||||
BufferMap_T::const_iterator i = locked.find(tb);
|
||||
if (i == locked.end()) {
|
||||
std::ostringstream o;
|
||||
o << "Cannot unlock unknown buffer " << tb;
|
||||
throw E_UNKNOWNKEY(o.str());
|
||||
//throw std::string("Unknow buffer - cannot unlock it");
|
||||
}
|
||||
allocated[i->second] = i->first;
|
||||
locked.erase(i->first);
|
||||
}
|
||||
|
||||
BufferCache::BufferMap_t::const_iterator BufferCache::findMatchingBuffer(unsigned int len) {
|
||||
BufferMap_t::const_iterator i = allocated.find(len);
|
||||
if (i != allocated.end())
|
||||
return i;
|
||||
i = allocated.begin();
|
||||
while (i != allocated.end()) {
|
||||
if (i->first > len)
|
||||
return i;
|
||||
++i;
|
||||
}
|
||||
return allocated.end();
|
||||
}
|
||||
|
||||
void BufferCache::printStatus() {
|
||||
INFO << "status: " << allocated.size() << " allocated " <<
|
||||
locked.size() << " locked buffers " << total_bytes <<
|
||||
" bytes" << std::endl;
|
||||
}
|
||||
|
||||
unsigned char* BufferCache::createBuffer(unsigned int len) {
|
||||
total_bytes += len;
|
||||
return new unsigned char[len];
|
||||
}
|
||||
|
||||
void BufferCache::freeBuffer(BufferMap_t::const_iterator pos) {
|
||||
delete [] pos->second;
|
||||
total_bytes -= pos->first;
|
||||
}
|
||||
|
||||
BufferCache::LockedBuffer::LockedBuffer(unsigned char *b) {
|
||||
assert(b);
|
||||
buf_p = b;
|
||||
}
|
||||
|
||||
BufferCache::LockedBuffer::LockedBuffer(unsigned int len) {
|
||||
buf_p = BufferCacheHolder::Instance().requestLockedBuffer(len);
|
||||
}
|
||||
|
||||
BufferCache::LockedBuffer::~LockedBuffer() {
|
||||
BufferCacheHolder::Instance().unlockBuffer(buf_p);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
/************************************************************************
|
||||
* Copyright (c) 2005-2006 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 BUFFER_CACHE_H
|
||||
#define BUFFER_CACHE_H
|
||||
#include <map>
|
||||
#include "Singleton.h"
|
||||
|
||||
using namespace Loki;
|
||||
|
||||
namespace Util {
|
||||
|
||||
/** A simple memory-buffer pool.
|
||||
*
|
||||
* - You should never manually delete a buffer created through the cache.
|
||||
* - You should however \e unlock buffers that were locked once you
|
||||
* no longer need them.
|
||||
* - You don't need to check for NULL pointers; exceptions are used for
|
||||
* errors.
|
||||
* - Buffers are initialised with zeroes.
|
||||
*
|
||||
* Beware:
|
||||
* \code
|
||||
* // assuming the cache is empty
|
||||
*
|
||||
* a = requestBuffer(10)
|
||||
* b = requestBuffer(5)
|
||||
*
|
||||
* a == b
|
||||
* ----------------------
|
||||
* a = requestBuffer(5)
|
||||
* b = requestBuffer(6)
|
||||
*
|
||||
* a != b
|
||||
* \endcode
|
||||
*/
|
||||
class BufferCache {
|
||||
public:
|
||||
BufferCache();
|
||||
~BufferCache();
|
||||
|
||||
/** Returns a buffer of (at least) len bytes.
|
||||
*
|
||||
* @param Length of requested buffer in bytes.
|
||||
* @return The buffer.
|
||||
* @note May throw a OutOfMemory exception.
|
||||
*/
|
||||
unsigned char* requestBuffer(unsigned int len);
|
||||
/** Returns a locked buffer of (at least) len bytes.
|
||||
* Just a convenience function.
|
||||
*
|
||||
* @see requestBuffer
|
||||
* @see lockBuffer
|
||||
*/
|
||||
unsigned char* requestLockedBuffer(unsigned int len);
|
||||
/** Lock a buffer (which was allocated by this cache).
|
||||
*
|
||||
* @param Pointer to the buffer.
|
||||
*
|
||||
* You need this when you request multiple buffers at the same time; lock
|
||||
* the current buffer before requesting the next one.
|
||||
*/
|
||||
void lockBuffer(unsigned char* tb);
|
||||
/** Unlock a buffer (which was previously locked).
|
||||
* @param Pointer to the buffer.
|
||||
*
|
||||
* Returns the buffer to the shared pool.
|
||||
*/
|
||||
void unlockBuffer(unsigned char* tb);
|
||||
class LockedBuffer {
|
||||
public:
|
||||
LockedBuffer(unsigned char* b);
|
||||
LockedBuffer(unsigned int len);
|
||||
~LockedBuffer();
|
||||
inline unsigned char* operator()() { return buf_p; }
|
||||
private:
|
||||
unsigned char* buf_p;
|
||||
};
|
||||
void printStatus();
|
||||
private:
|
||||
typedef std::map<unsigned int, unsigned char*> BufferMap_t;
|
||||
typedef std::map<unsigned char*, unsigned int> BufferMap_T;
|
||||
BufferMap_t allocated;
|
||||
BufferMap_T locked;
|
||||
BufferMap_t::const_iterator findMatchingBuffer(unsigned int len);
|
||||
BufferMap_t::iterator findLockedBuffer(unsigned char* tb);
|
||||
unsigned char* createBuffer(unsigned int len);
|
||||
void freeBuffer(BufferMap_t::const_iterator pos);
|
||||
unsigned int total_bytes;
|
||||
};
|
||||
|
||||
/** Instance of BufferCache.
|
||||
*/
|
||||
typedef SingletonHolder<BufferCache, CreateUsingNew, DefaultLifetime,
|
||||
SingleThreaded> BufferCacheHolder;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,43 @@
|
||||
#ifndef CASE_INSENSITIVE_STRING_H
|
||||
#define CASE_INSENSITIVE_STRING_H
|
||||
|
||||
namespace Util {
|
||||
|
||||
static int memicmp (const char *s, const char *t, int n) {
|
||||
int r = 0;
|
||||
while (n-- > 0 && (r = toupper (*s++) - toupper (*t++)) == 0);
|
||||
return (r);
|
||||
}
|
||||
|
||||
struct ci_char_traits : public std::char_traits<char>
|
||||
{
|
||||
static bool eq( char c1, char c2)
|
||||
{
|
||||
return toupper(c1) == toupper(c2);
|
||||
}
|
||||
static bool lt( char c1, char c2)
|
||||
{
|
||||
return toupper(c1) < toupper(c2);
|
||||
}
|
||||
static int compare( const char *s1, const char *s2, size_t n)
|
||||
{
|
||||
return memicmp( s1, s2, n); // non-standard !
|
||||
}
|
||||
static const char *find( const char *s, int n, char ch)
|
||||
{
|
||||
while ( n-- > 0 && toupper(*s) != toupper(ch) )
|
||||
{
|
||||
++s;
|
||||
}
|
||||
return n > 0 ? s : 0;
|
||||
}
|
||||
};
|
||||
|
||||
/** case-insensitive compare of std::string.
|
||||
*
|
||||
* Can't remember where I found this, probably derived from here
|
||||
* http://www.gotw.ca/gotw/029.htm anyway.
|
||||
*/
|
||||
typedef std::basic_string<char, ci_char_traits> ci_string;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,86 @@
|
||||
#include <cstdlib>
|
||||
#include <physfs.h>
|
||||
#include <sstream>
|
||||
#include "m_exceptions.h"
|
||||
#include "file_helper.h"
|
||||
|
||||
#ifndef OGTA_DEFAULT_DATA_PATH
|
||||
#define OGTA_DEFAULT_DATA_PATH "gtadata.zip"
|
||||
#endif
|
||||
#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
|
||||
#endif
|
||||
|
||||
namespace Util {
|
||||
FileHelper::FileHelper() :
|
||||
baseDataPath(OGTA_DEFAULT_DATA_PATH),
|
||||
modDataPath(OGTA_DEFAULT_MOD_PATH),
|
||||
userHomeDir(OGTA_DEFAULT_HOME_PATH) {
|
||||
const char *e = getenv("OGTA_DATA");
|
||||
if (e != NULL)
|
||||
baseDataPath = std::string(e);
|
||||
e = getenv("OGTA_MOD");
|
||||
if (e != NULL)
|
||||
modDataPath = std::string(e);
|
||||
e = getenv("OGTA_HOME");
|
||||
if (e != NULL)
|
||||
userHomeDir = std::string(e);
|
||||
}
|
||||
|
||||
const std::string & FileHelper::getBaseDataPath() const {
|
||||
return baseDataPath;
|
||||
}
|
||||
|
||||
const std::string & FileHelper::getModDataPath() const {
|
||||
return modDataPath;
|
||||
}
|
||||
|
||||
const std::string & FileHelper::getUserHomeDir() const {
|
||||
return userHomeDir;
|
||||
}
|
||||
|
||||
bool FileHelper::existsInSystemFS(const std::string & file) const {
|
||||
if (file.length() == 0)
|
||||
return 0;
|
||||
#ifdef LINUX
|
||||
return (access(file.c_str(), R_OK) == 0);
|
||||
#endif
|
||||
#ifdef WIN32
|
||||
FILE * f = fopen(file.c_str(), "rb");
|
||||
bool res = (f != NULL);
|
||||
if (f)
|
||||
fclose(f);
|
||||
return res;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool FileHelper::existsInVFS(const std::string & file) const {
|
||||
if (file.length() == 0)
|
||||
return 0;
|
||||
return PHYSFS_exists(file.c_str());
|
||||
}
|
||||
|
||||
PHYSFS_file *FileHelper::openReadVFS(const std::string & file) const {
|
||||
PHYSFS_file * fd = PHYSFS_openRead(file.c_str());
|
||||
if (fd)
|
||||
return fd;
|
||||
// try lower case
|
||||
std::string name2(file);
|
||||
transform(name2.begin(), name2.end(), name2.begin(), tolower);
|
||||
fd = PHYSFS_openRead(name2.c_str());
|
||||
if (!fd) { // still no joy, give up
|
||||
std::ostringstream o;
|
||||
o << file << " with error: " << PHYSFS_getLastError();
|
||||
throw E_FILENOTFOUND(o.str());
|
||||
}
|
||||
// take this one instead
|
||||
return fd;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef UTIL_FILEHELPER_H
|
||||
#define UTIL_FILEHELPER_H
|
||||
|
||||
#include <string>
|
||||
#include <physfs.h>
|
||||
#include "Singleton.h"
|
||||
|
||||
namespace Util {
|
||||
// central path storage
|
||||
// Note: assumes that physfs is ready when instance is created
|
||||
class FileHelper {
|
||||
public:
|
||||
FileHelper();
|
||||
const std::string & getBaseDataPath() const;
|
||||
const std::string & getModDataPath() const;
|
||||
const std::string & getUserHomeDir() const;
|
||||
bool existsInSystemFS(const std::string & file) const;
|
||||
bool existsInVFS(const std::string & file) const;
|
||||
PHYSFS_file* openReadVFS(const std::string & file) const;
|
||||
private:
|
||||
std::string baseDataPath;
|
||||
std::string modDataPath;
|
||||
std::string userHomeDir;
|
||||
};
|
||||
|
||||
typedef Loki::SingletonHolder<FileHelper, Loki::CreateUsingNew, Loki::DefaultLifetime,
|
||||
Loki::SingleThreaded> FileHelperHolder;
|
||||
}
|
||||
#define GET_FILE_HELPER Util::FileHelperHolder::Instance()
|
||||
|
||||
#endif
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
#ifndef UTIL_GUI_H
|
||||
#define UTIL_GUI_H
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <SDL.h>
|
||||
#include "animation.h"
|
||||
#include "gl_pagedtexture.h"
|
||||
#include "image_loader.h"
|
||||
#include "font_cache.h"
|
||||
|
||||
namespace GUI {
|
||||
struct Object;
|
||||
class Animation;
|
||||
|
||||
class Manager {
|
||||
public:
|
||||
Manager() {}
|
||||
~Manager();
|
||||
void add(Object * obj, uint8_t onLevel);
|
||||
void remove(Object * obj);
|
||||
void removeById(size_t id);
|
||||
Object* findObject(const size_t id);
|
||||
void draw();
|
||||
void clearObjects();
|
||||
void clearCache();
|
||||
void cacheImageRAW(const std::string & file, size_t asId);
|
||||
void cacheImageRAT(const std::string & file, const std::string & palette, size_t asId);
|
||||
void cacheImageSDL(const std::string & file, size_t asId);
|
||||
ImageUtil::WidthHeightPair cacheStyleArrowSprite(const size_t id, int remap);
|
||||
const OpenGL::PagedTexture & getCachedImage(size_t Id);
|
||||
void receive(SDL_MouseButtonEvent & mb_event);
|
||||
Animation* findAnimation(uint16_t id);
|
||||
void createAnimation(const std::vector<uint16_t> & indices, uint16_t fps, size_t asAnimId);
|
||||
void update(uint32_t nowTicks);
|
||||
private:
|
||||
bool isInside(Object & o, Uint16 x, Uint16 y) const;
|
||||
typedef std::map< size_t, OpenGL::PagedTexture > GuiTextureCache;
|
||||
GuiTextureCache::iterator findByCacheId(const size_t & Id);
|
||||
|
||||
typedef std::map<uint16_t, Animation*> AnimationMap;
|
||||
AnimationMap guiAnimations;
|
||||
typedef std::list<Object*> GuiObjectList;
|
||||
typedef std::map< uint8_t, GuiObjectList > GuiObjectListMap;
|
||||
GuiObjectListMap::iterator findLayer(uint8_t l);
|
||||
GuiObjectListMap guiLayers;
|
||||
GuiTextureCache texCache;
|
||||
|
||||
};
|
||||
|
||||
class Animation : public Util::Animation {
|
||||
public:
|
||||
Animation(const std::vector<uint16_t> & _indices, const uint16_t fps) :
|
||||
Util::Animation(_indices.size(), fps),
|
||||
indices(_indices) {}
|
||||
std::vector<uint16_t> indices;
|
||||
uint16_t getCurrentFrame();
|
||||
};
|
||||
|
||||
struct Object {
|
||||
Object(const SDL_Rect & r);
|
||||
Object(const size_t Id, const SDL_Rect & r);
|
||||
Object(const size_t Id, const SDL_Rect & r, const SDL_Color & c);
|
||||
virtual ~Object() {}
|
||||
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;
|
||||
}
|
||||
virtual void draw();
|
||||
Manager * manager;
|
||||
};
|
||||
|
||||
struct TexturedObject : public Object {
|
||||
TexturedObject(const SDL_Rect & r, const size_t texid) : Object(r),
|
||||
texId(texid) {
|
||||
}
|
||||
TexturedObject(size_t Id, const SDL_Rect & r, const size_t texid) : Object(Id, r),
|
||||
texId(texid) {
|
||||
}
|
||||
size_t texId;
|
||||
void draw();
|
||||
};
|
||||
|
||||
struct AnimatedTextureObject : public Object {
|
||||
AnimatedTextureObject(const SDL_Rect & r, const size_t animid) : Object(r),
|
||||
animId(animid) {
|
||||
animation = NULL;
|
||||
}
|
||||
AnimatedTextureObject(size_t Id, const SDL_Rect & r, const size_t animid) : Object(Id, r),
|
||||
animId(animid) {
|
||||
animation = NULL;
|
||||
}
|
||||
size_t animId;
|
||||
Animation * animation;
|
||||
void draw();
|
||||
};
|
||||
|
||||
struct Label : public Object {
|
||||
Label(const SDL_Rect & r, const std::string & s,
|
||||
const std::string & fontFile, const size_t fontScale) : Object(r), text(s) {
|
||||
OpenGL::DrawableFont & fnt = OpenGTA::FontCacheHolder::Instance().getFont(fontFile, fontScale);
|
||||
font = &fnt;
|
||||
}
|
||||
Label(const size_t Id, const SDL_Rect & r, const std::string & s,
|
||||
const std::string & fontFile, const size_t fontScale) : Object(Id, r), text(s) {
|
||||
OpenGL::DrawableFont & fnt = OpenGTA::FontCacheHolder::Instance().getFont(fontFile, fontScale);
|
||||
font = &fnt;
|
||||
}
|
||||
OpenGL::DrawableFont * font;
|
||||
std::string text;
|
||||
void draw();
|
||||
};
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,155 @@
|
||||
#include <cstddef>
|
||||
#include <physfs.h>
|
||||
#include "image_loader.h"
|
||||
#include "file_helper.h"
|
||||
#include "buffercache.h"
|
||||
#include "log.h"
|
||||
#include "cistring.h"
|
||||
#include "opengta.h"
|
||||
|
||||
namespace ImageUtil {
|
||||
using OpenGL::PagedTexture;
|
||||
|
||||
WidthHeightPair lookupImageSize(const std::string & name, const uint32_t size) {
|
||||
Util::ci_string iname(name.c_str());
|
||||
uint16_t width = 0;
|
||||
uint16_t height = 0;
|
||||
uint32_t bpp = 0;
|
||||
|
||||
// m4 tools/raw_images.m4
|
||||
if ((iname.find("CUT") == 0) && (iname.find(".RA") == 4)) {
|
||||
width = 640; height = 480;
|
||||
}
|
||||
if ((iname.find("F_BMG.RA") == 0)) {
|
||||
width = 100; height = 50;
|
||||
}
|
||||
if ((iname.find("F_DMA.RA") == 0)) {
|
||||
width = 78; height = 109;
|
||||
}
|
||||
if ((iname.find("F_LOGO") == 0) && (iname.find(".RA") == 7)) {
|
||||
width = 640; height = 168;
|
||||
}
|
||||
if ((iname.find("F_LOWER") == 0) && (iname.find(".RA") == 8)) {
|
||||
width = 640; height = 312;
|
||||
}
|
||||
if ((iname.find("F_PLAYN.RA") == 0)) {
|
||||
width = 180; height = 50;
|
||||
}
|
||||
if ((iname.find("F_PLAY") == 0) && (iname.find(".RA") == 7)) {
|
||||
width = 102; height = 141;
|
||||
}
|
||||
if ((iname.find("F_UPPER.RA") == 0)) {
|
||||
width = 640; height = 168;
|
||||
}
|
||||
|
||||
// end-of-generated code
|
||||
if (iname.find(".RAW") == iname.length() - 4)
|
||||
bpp = 3;
|
||||
|
||||
if (iname.find(".RAT") == iname.length() - 4)
|
||||
bpp = 1;
|
||||
|
||||
if (!(bpp && bpp * width * height == size))
|
||||
ERROR << "could not identify image: " << name << " size: " << size << std::endl;
|
||||
return std::make_pair<uint16_t, uint16_t>(width, height);
|
||||
}
|
||||
|
||||
|
||||
OpenGL::PagedTexture loadImageRAW(const std::string & name) {
|
||||
|
||||
PHYSFS_file * fd = GET_FILE_HELPER.openReadVFS(name);
|
||||
|
||||
uint32_t nbytes = PHYSFS_fileLength(fd);
|
||||
|
||||
WidthHeightPair whp = lookupImageSize(name, nbytes);
|
||||
|
||||
if (whp.first == 0 || whp.second == 0) {
|
||||
PHYSFS_close(fd);
|
||||
WARN << "aborting image load" << std::endl;
|
||||
}
|
||||
|
||||
Util::BufferCache::LockedBuffer lbuf(nbytes);
|
||||
/*
|
||||
uint8_t * buffer = Util::BufferCacheHolder::Instance().
|
||||
requestBuffer(nbytes);
|
||||
*/
|
||||
uint8_t *buffer = lbuf();
|
||||
PHYSFS_read(fd, buffer, 1, nbytes);
|
||||
PHYSFS_close(fd);
|
||||
|
||||
return createEmbeddedTexture(whp.first, whp.second, false, buffer);
|
||||
}
|
||||
|
||||
|
||||
OpenGL::PagedTexture loadImageRATWithPalette(const std::string & name,
|
||||
const std::string & palette_file) {
|
||||
|
||||
Util::FileHelper & fh = GET_FILE_HELPER;
|
||||
|
||||
PHYSFS_file * fd = fh.openReadVFS(name);
|
||||
uint32_t nbytes = PHYSFS_fileLength(fd);
|
||||
|
||||
WidthHeightPair whp = lookupImageSize(name, nbytes);
|
||||
if (whp.first == 0 || whp.second == 0) {
|
||||
PHYSFS_close(fd);
|
||||
WARN << "aborting image load" << std::endl;
|
||||
}
|
||||
Util::BufferCache::LockedBuffer lb1(nbytes);
|
||||
PHYSFS_read(fd, lb1(), 1, nbytes);
|
||||
PHYSFS_close(fd);
|
||||
|
||||
// if this causes an exception, the buffercache will cleanup
|
||||
fd = fh.openReadVFS(palette_file);
|
||||
OpenGTA::Graphics8Bit::RGBPalette rgb(fd);
|
||||
PHYSFS_close(fd);
|
||||
|
||||
Util::BufferCache::LockedBuffer lb2(nbytes * 3);
|
||||
rgb.apply(nbytes, lb1(), lb2(), false);
|
||||
|
||||
return createEmbeddedTexture(whp.first, whp.second, false, lb2());
|
||||
}
|
||||
|
||||
GLuint createGLTexture(GLsizei w, GLsizei h, bool rgba, const void* pixels) {
|
||||
GLuint tex;
|
||||
glGenTextures(1, &tex);
|
||||
glBindTexture(GL_TEXTURE_2D, tex);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
if (rgba)
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
|
||||
//gluBuild2DMipmaps(GL_TEXTURE_2D, 4, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
|
||||
else
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
|
||||
//gluBuild2DMipmaps(GL_TEXTURE_2D, 3, w, h, GL_RGB, GL_UNSIGNED_BYTE, pixels);
|
||||
|
||||
GL_CHECKERROR;
|
||||
return tex;
|
||||
}
|
||||
|
||||
void copyImage2Image(uint8_t *dest, const uint8_t *src, const uint16_t
|
||||
srcWidth, const uint16_t srcHeight, const uint16_t destWidth) {
|
||||
uint8_t *d = dest;
|
||||
uint32_t srcOff = 0;
|
||||
for (uint16_t j = 0; j < srcHeight; ++j) {
|
||||
memcpy(d, src + srcOff, srcWidth);
|
||||
srcOff += srcWidth;
|
||||
d += destWidth;
|
||||
}
|
||||
}
|
||||
|
||||
OpenGL::PagedTexture createEmbeddedTexture(GLsizei w, GLsizei h,
|
||||
bool rgba, const void* pixels) {
|
||||
|
||||
NextPowerOfTwo npot(w, h);
|
||||
uint32_t bpp = (rgba ? 4 : 3);
|
||||
|
||||
uint32_t bufSize = npot.w * npot.h * bpp;
|
||||
uint8_t* buff = Util::BufferCacheHolder::Instance().requestBuffer(bufSize);
|
||||
copyImage2Image(buff, (uint8_t*)pixels, w * bpp, h, npot.w * bpp);
|
||||
|
||||
GLuint tex = createGLTexture(npot.w, npot.h, rgba, buff);
|
||||
return PagedTexture(tex, 0, 0, float(w) / npot.w, float(h) / npot.h);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
#ifndef UTIL_IMAGE_LOADER_H
|
||||
#define UTIL_IMAGE_LOADER_H
|
||||
|
||||
#include <SDL_opengl.h>
|
||||
#include <string>
|
||||
#include "gl_pagedtexture.h"
|
||||
|
||||
namespace ImageUtil {
|
||||
|
||||
typedef std::pair<uint16_t, uint16_t> WidthHeightPair;
|
||||
|
||||
struct NextPowerOfTwo {
|
||||
NextPowerOfTwo(uint32_t _w, uint32_t _h) {
|
||||
w = 1; h = 1;
|
||||
while (w < _w) { w <<= 1; }
|
||||
while (h < _h) { h <<= 1; }
|
||||
}
|
||||
uint32_t w;
|
||||
uint32_t h;
|
||||
};
|
||||
|
||||
// hardcoded data for known images
|
||||
WidthHeightPair lookupImageSize(const std::string & name, const uint32_t size);
|
||||
// load a rgb image
|
||||
OpenGL::PagedTexture loadImageRAW(const std::string & name);
|
||||
// load a palette image and guess the palette filename
|
||||
OpenGL::PagedTexture loadImageRAT(const std::string & name);
|
||||
// load a palette image using palette file
|
||||
OpenGL::PagedTexture loadImageRATWithPalette(const std::string & name,
|
||||
const std::string & palette_file);
|
||||
// plain simple garden-variety create-a-texture; needs to be ^2
|
||||
GLuint createGLTexture(GLsizei w, GLsizei h, bool rgba, const void* pixels);
|
||||
|
||||
// blitting a buffer into another; they should exist!
|
||||
void copyImage2Image(uint8_t *dest, const uint8_t *src, const uint16_t
|
||||
srcWidth, const uint16_t srcHeight, const uint16_t destWidth);
|
||||
|
||||
OpenGL::PagedTexture createEmbeddedTexture(GLsizei w, GLsizei h, bool rgba,
|
||||
const void *pixels);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,29 @@
|
||||
#include "log.h"
|
||||
#include <SDL_opengl.h>
|
||||
|
||||
namespace Util {
|
||||
unsigned int Log::level = 0;
|
||||
std::ostream Log::emptyStream(0);
|
||||
|
||||
void Log::setOutputLevel(unsigned int newLevel) {
|
||||
level = newLevel;
|
||||
}
|
||||
|
||||
const char* Log::glErrorName(int k) {
|
||||
switch(k) {
|
||||
case GL_NO_ERROR:
|
||||
return "GL_NO_ERROR";
|
||||
case GL_INVALID_VALUE:
|
||||
return "GL_INVALID_VALUE";
|
||||
case GL_INVALID_OPERATION:
|
||||
return "GL_INVALID_OPERATION";
|
||||
case GL_STACK_OVERFLOW:
|
||||
return "GL_STACK_OVERFLOW";
|
||||
case GL_STACK_UNDERFLOW:
|
||||
return "GL_STACK_UNDERFLOW";
|
||||
case GL_OUT_OF_MEMORY:
|
||||
return "GL_OUT_OF_MEMORY";
|
||||
}
|
||||
return "Unknown-Error";
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
#ifndef LOG_FUNCS_H
|
||||
#define LOG_FUNCS_H
|
||||
#include <iostream>
|
||||
|
||||
#ifdef WIN32
|
||||
#undef ERROR
|
||||
#endif
|
||||
|
||||
#define INFO Util::Log::info(__FILE__, __LINE__)
|
||||
#define WARN Util::Log::warn(__FILE__, __LINE__)
|
||||
#define ERROR Util::Log::error(__FILE__, __LINE__)
|
||||
#define ERROR_AND_EXIT(ec) error_code = ec; exit(ec);
|
||||
#define GL_CHECKERROR { int _err = glGetError(); if (_err != GL_NO_ERROR) \
|
||||
Util::Log::error(__FILE__, __LINE__) << "GL error: " << _err << " = " << Util::Log::glErrorName(_err) << std::endl; }
|
||||
namespace Util {
|
||||
class Log {
|
||||
public:
|
||||
inline static std::ostream & info(const char* f, int l) {
|
||||
if (level) return emptyStream; std::cout << "Info (" << f << ":" << l << "): "; return std::cout;
|
||||
}
|
||||
inline static std::ostream & warn(const char* f, int l) {
|
||||
if (level > 1) return emptyStream; std::cerr << "Warning (" << f << ":" << l << "): "; return std::cerr;
|
||||
}
|
||||
inline static std::ostream & error(const char* f, int l) {
|
||||
std::cerr << "Error ("<< f << ":" << l << "): "; return std::cerr;
|
||||
}
|
||||
static void setOutputLevel(unsigned int newLevel);
|
||||
static const char* glErrorName(int k);
|
||||
private:
|
||||
static unsigned int level;
|
||||
static std::ostream emptyStream;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,55 @@
|
||||
#include "m_exceptions.h"
|
||||
#include <sstream>
|
||||
|
||||
namespace Util {
|
||||
|
||||
LocalException::LocalException(const char *f, const size_t l, const char* n) :
|
||||
inFile(f), typeName(n), msg(""), inLine(l) {}
|
||||
|
||||
LocalException::LocalException(const char *f, const size_t l, const char* n,
|
||||
const std::string _msg) :
|
||||
inFile(f), typeName(n), msg(_msg), inLine(l) {}
|
||||
|
||||
const char* LocalException::what() const throw() {
|
||||
std::ostringstream o;
|
||||
o << typeName << " (" << inFile << ":" << inLine << "): " << msg;
|
||||
return o.str().c_str();
|
||||
}
|
||||
|
||||
FileNotFound::FileNotFound(const char* f, const size_t l, const std::string _msg) :
|
||||
LocalException(f, l, "FileNotFound", _msg) {}
|
||||
|
||||
IOError::IOError(const char* f, const size_t l, const std::string _msg) :
|
||||
LocalException(f, l, "IOError", _msg) {}
|
||||
|
||||
InvalidFormat::InvalidFormat(const char* f, const size_t l, const std::string _msg) :
|
||||
LocalException(f, l, "InvalidFormat", _msg) {}
|
||||
|
||||
UnknownKey::UnknownKey(const char* f, const size_t l, const std::string _msg) :
|
||||
LocalException(f, l, "UnknownKey", _msg) {}
|
||||
|
||||
OutOfRange::OutOfRange(const char* f, const size_t l, const std::string _msg) :
|
||||
LocalException(f, l, "OutOfRange", _msg) {}
|
||||
|
||||
OutOfMemory::OutOfMemory(const char* f, const size_t l, const std::string _msg) :
|
||||
LocalException(f, l, "OutOfMemory", _msg) {}
|
||||
|
||||
ScriptError::ScriptError(const char* f, const size_t l, const std::string _msg) :
|
||||
LocalException(f, l, "ScriptError", _msg) {}
|
||||
|
||||
NotSupported::NotSupported(const char* f, const size_t l, const std::string _msg) :
|
||||
LocalException(f, l, "NotSupported", _msg) {}
|
||||
|
||||
}
|
||||
|
||||
#if 0
|
||||
using namespace Util;
|
||||
int main(int argc, char* argv[]) {
|
||||
try {
|
||||
throw E_FILENOTFOUND("foobar.zip");
|
||||
}
|
||||
catch (Exception & e) {
|
||||
std::cout << "E: " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,86 @@
|
||||
#ifndef LOCAL_EXCEPTIONS_OH_WHAT_JOY
|
||||
#define LOCAL_EXCEPTIONS_OH_WHAT_JOY
|
||||
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
|
||||
namespace Util {
|
||||
|
||||
struct LocalException : public std::exception {
|
||||
LocalException(const char *f, const size_t l, const char* n);
|
||||
LocalException(const char *f, const size_t l, const char* n,
|
||||
const std::string _msg);
|
||||
virtual ~LocalException() throw() {}
|
||||
const char * what() const throw();
|
||||
std::string inFile;
|
||||
std::string typeName;
|
||||
std::string msg;
|
||||
size_t inLine;
|
||||
};
|
||||
|
||||
/* Actually I wouldn't make the derived destructors virtual,
|
||||
* but g++ complains if I don't. No idea why.
|
||||
*
|
||||
* Anyway exceptions shouldn't happen that often and gcc is
|
||||
* usually right about those things...
|
||||
*/
|
||||
|
||||
struct FileNotFound : public LocalException {
|
||||
FileNotFound(const char* f, const size_t l, const std::string _msg);
|
||||
virtual ~FileNotFound() throw() {}
|
||||
};
|
||||
|
||||
struct IOError : public LocalException {
|
||||
IOError(const char* f, const size_t l, const std::string _msg);
|
||||
virtual ~IOError() throw() {}
|
||||
};
|
||||
|
||||
struct InvalidFormat : public LocalException {
|
||||
InvalidFormat(const char* f, const size_t l, const std::string _msg);
|
||||
virtual ~InvalidFormat() throw() {}
|
||||
};
|
||||
|
||||
struct UnknownKey : public LocalException {
|
||||
UnknownKey(const char* f, const size_t l, const std::string _msg);
|
||||
virtual ~UnknownKey() throw() {}
|
||||
};
|
||||
|
||||
struct OutOfRange : public LocalException {
|
||||
OutOfRange(const char* f, const size_t l, const std::string _msg);
|
||||
virtual ~OutOfRange() throw () {}
|
||||
};
|
||||
|
||||
struct OutOfMemory : public LocalException {
|
||||
OutOfMemory(const char* f, const size_t l, const std::string _msg);
|
||||
virtual ~OutOfMemory() throw() {}
|
||||
};
|
||||
|
||||
struct ScriptError : public LocalException {
|
||||
ScriptError(const char* f, const size_t l, const std::string _msg);
|
||||
virtual ~ScriptError() throw() {}
|
||||
};
|
||||
|
||||
struct NotSupported : public LocalException {
|
||||
NotSupported(const char* f, const size_t l, const std::string _msg);
|
||||
virtual ~NotSupported() throw() {}
|
||||
};
|
||||
}
|
||||
|
||||
// to avoid the need for the namespace when writing catch-all blocks
|
||||
typedef Util::LocalException Exception;
|
||||
|
||||
#ifdef WIN32
|
||||
#undef E_OUTOFMEMORY
|
||||
#endif
|
||||
|
||||
// to auto-fill line+file information where the exception was created
|
||||
#define E_FILENOTFOUND(m) Util::FileNotFound(__FILE__, __LINE__, m)
|
||||
#define E_IOERROR(m) Util::IOError(__FILE__, __LINE__, m)
|
||||
#define E_INVALIDFORMAT(m) Util::InvalidFormat(__FILE__, __LINE__, m)
|
||||
#define E_UNKNOWNKEY(m) Util::UnknownKey(__FILE__, __LINE__, m)
|
||||
#define E_OUTOFRANGE(m) Util::OutOfRange(__FILE__, __LINE__, m)
|
||||
#define E_OUTOFMEMORY(m) Util::OutOfMemory(__FILE__, __LINE__, m)
|
||||
#define E_SCRIPTERROR(m) Util::ScriptError(__FILE__, __LINE__, m)
|
||||
#define E_NOTSUPPORTED(m) Util::NotSupported(__FILE__, __LINE__, m)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,77 @@
|
||||
/************************************************************************
|
||||
* Copyright (c) 2005-2006 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 SAMPLE_CACHE_H
|
||||
#define SAMPLE_CACHE_H
|
||||
|
||||
#include <SDL_mixer.h>
|
||||
#include <map>
|
||||
#include "Singleton.h"
|
||||
#include "SmallObj.h"
|
||||
|
||||
namespace Audio {
|
||||
|
||||
class SampleType_MixChunk : public Loki::SmallObject<> {
|
||||
public:
|
||||
SampleType_MixChunk(Uint8 *mem, Mix_Chunk *ch) : memory(mem), chunk(ch) {}
|
||||
~SampleType_MixChunk() {
|
||||
delete [] memory;
|
||||
Mix_FreeChunk(chunk);
|
||||
}
|
||||
Uint8 *memory;
|
||||
Mix_Chunk *chunk;
|
||||
private:
|
||||
SampleType_MixChunk(const SampleType_MixChunk & other);
|
||||
SampleType_MixChunk() : memory(0), chunk(0) {}
|
||||
};
|
||||
|
||||
typedef size_t SampleKeyType;
|
||||
|
||||
template < typename K, typename V >
|
||||
class Cache {
|
||||
public:
|
||||
~Cache() {
|
||||
clear;
|
||||
}
|
||||
void clear();
|
||||
typedef std::map< K, V > CacheMapType;
|
||||
V operator[](K k) { return m_map[k]; }
|
||||
void add(K k, V v) { m_map.insert(std::make_pair<K, V>(k, v)); }
|
||||
private:
|
||||
CacheMapType m_map;
|
||||
|
||||
};
|
||||
|
||||
typedef Cache< SampleKeyType, SampleType_MixChunk* > SampleCache;
|
||||
|
||||
template<> void SampleCache::clear() {
|
||||
for (CacheMapType::iterator i = m_map.begin(); i != m_map.end(); i++) {
|
||||
delete i->second;
|
||||
}
|
||||
m_map.clear();
|
||||
}
|
||||
|
||||
typedef Loki::SingletonHolder< SampleCache , Loki::CreateUsingNew,
|
||||
Loki::DefaultLifetime, Loki::SingleThreaded> SampleCacheHolder;
|
||||
}
|
||||
|
||||
#endif
|
||||
+298
@@ -0,0 +1,298 @@
|
||||
/************************************************************************
|
||||
* Copyright (c) 2005-2006 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 <cstring>
|
||||
#include <cstdlib>
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include "set.h"
|
||||
|
||||
#define INTEGRATE_OGTA
|
||||
#ifdef INTEGRATE_OGTA
|
||||
#include <sstream>
|
||||
#include "m_exceptions.h"
|
||||
#endif
|
||||
|
||||
namespace Util {
|
||||
|
||||
// some bits of binary magic from someone on IRC
|
||||
// by the name of Zhivago
|
||||
// only the 8-bit variants are used
|
||||
#define maskn(x, w) ((x) & ((1<<w)-1))
|
||||
|
||||
#define tstbn(a, x, w) ((a)[x>>w] & (1<<(maskn(x, w))))
|
||||
#define setbn(a, x, w) ((a)[x>>w] |= (1<<(maskn(x, w))))
|
||||
#define clrbn(a, x, w) ((a)[x>>w] &= ~(1<<(maskn(x, w))))
|
||||
|
||||
#define tstb8(a, x) tstbn((a), (x), 3)
|
||||
#define setb8(a, x) setbn((a), (x), 3)
|
||||
#define clrb8(a, x) clrbn((a), (x), 3)
|
||||
|
||||
#define tstb32(a, x) tstbn((a), (x), 5)
|
||||
#define setb32(a, x) setbn((a), (x), 5)
|
||||
#define clrb32(a, x) clrbn((a), (x), 5)
|
||||
|
||||
// and something to move through an array to the correct byte
|
||||
#define mv2byte(a, k, p) \
|
||||
if (k >= 8) { \
|
||||
p = a + k / 8; \
|
||||
k = k % 8; \
|
||||
} \
|
||||
else { \
|
||||
p = a; \
|
||||
}
|
||||
|
||||
// note: given a set of 1..N values
|
||||
// this means one needs to malloc(N/8 + (n%8 == 0 ? 0 : 1) bytes of mem
|
||||
// the bit status (reading from left to right) corresponds
|
||||
// to the existence of the index number in the set
|
||||
|
||||
Set::Set() {
|
||||
last = MAX_SET_COUNT;
|
||||
storage = new unsigned char[MAX_SET_COUNT / 8];
|
||||
memset((void*)storage, 0, (size_t) MAX_SET_COUNT / 8);
|
||||
ext_data = 0;
|
||||
}
|
||||
|
||||
Set::Set(int n) {
|
||||
last = n;
|
||||
assert(n>0);
|
||||
int k = ((n % 8) > 0) ? n / 8 + 1 : n / 8;
|
||||
storage = new unsigned char[k];
|
||||
memset((void*)storage, 0, (size_t)k);
|
||||
ext_data = 0;
|
||||
}
|
||||
|
||||
Set::Set(const Set & other) {
|
||||
last = other.get_last();
|
||||
unsigned char* os = other.give_storage();
|
||||
assert(last > 0);
|
||||
int k = ((last % 8) > 0) ? last / 8 + 1 : last / 8;
|
||||
storage = new unsigned char[k];
|
||||
memcpy(storage, os, k);
|
||||
ext_data = 0;
|
||||
}
|
||||
|
||||
Set::Set(int k, unsigned char* data) {
|
||||
storage = data;
|
||||
last = k;
|
||||
ext_data = 1;
|
||||
}
|
||||
|
||||
Set::~Set() {
|
||||
if (!ext_data && storage != NULL)
|
||||
delete [] storage;
|
||||
}
|
||||
|
||||
void Set::set_data(int n, unsigned char* data) {
|
||||
if (!ext_data) {
|
||||
#ifdef INTEGRATE_OGTA
|
||||
throw E_NOTSUPPORTED("set_data() called on an instance with own data");
|
||||
#else
|
||||
fprintf(stderr, "Set::Err: set_data() called on an instance with own data\n");
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void Set::set_last(int n) {
|
||||
if (n > last) {
|
||||
#ifdef INTEGRATE_OGTA
|
||||
std::ostringstream o;
|
||||
o << n << " > " << last << std::endl;
|
||||
throw E_OUTOFRANGE(o.str());
|
||||
#else
|
||||
printf("%i is larger than previous last n (%i), aborting\n", n, last);
|
||||
#endif
|
||||
}
|
||||
last = n;
|
||||
}
|
||||
|
||||
int Set::get_last() const {
|
||||
return(last);
|
||||
}
|
||||
|
||||
void Set::print_set() const {
|
||||
bool first = true;
|
||||
printf("{");
|
||||
for (int i = 0; i < last; i++) {
|
||||
if (get_item(i)) {
|
||||
if (!first) printf(", ");
|
||||
printf("%d", i);
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
printf("}\n");
|
||||
}
|
||||
|
||||
void Set::set_item(int k, bool val) {
|
||||
if (k < last) {
|
||||
unsigned char* pos = NULL;
|
||||
mv2byte(storage, k, pos);
|
||||
if (val == true) {
|
||||
setb8(pos, k);
|
||||
}
|
||||
else {
|
||||
clrb8(pos, k);
|
||||
}
|
||||
}
|
||||
else
|
||||
#ifdef INTEGRATE_OGTA
|
||||
{
|
||||
std::ostringstream o;
|
||||
o << k << " >= " << last << std::endl;
|
||||
throw E_OUTOFRANGE(o.str());
|
||||
}
|
||||
#else
|
||||
assert(k < last);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool Set::get_item(int k) const {
|
||||
bool b = false;
|
||||
if (k < last) {
|
||||
unsigned char* pos;
|
||||
mv2byte(storage, k, pos);
|
||||
b = tstb8(pos, k);
|
||||
}
|
||||
else
|
||||
#ifdef INTEGRATE_OGTA
|
||||
{
|
||||
std::ostringstream o;
|
||||
o << k << " >= " << last << std::endl;
|
||||
throw E_OUTOFRANGE(o.str());
|
||||
}
|
||||
#else
|
||||
assert(k < last);
|
||||
#endif
|
||||
return b;
|
||||
}
|
||||
|
||||
int Set::as_int(int start, int len) const {
|
||||
if (start < 0 || start > last || start + len > last) {
|
||||
#ifdef INTEGRATE_OGTA
|
||||
std::ostringstream o;
|
||||
o << "invalid query: " << start << " length " << len <<
|
||||
" with data-length " << last << std::endl;
|
||||
throw E_OUTOFRANGE(o.str());
|
||||
#else
|
||||
fprintf(stderr, "Set::Err: queried index out of range (%i, %i ; %i)\n", start, len, last);
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
int v = 0;
|
||||
int t = 0;
|
||||
for (int i=0; i< len; i++) {
|
||||
if (get_item(i+start)) {
|
||||
v = (int) 1 << i;
|
||||
t += v;
|
||||
}
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
int Set::as_int2(int start, int len) const {
|
||||
if (start < 0 || start > last || start + len > last) {
|
||||
#ifdef INTEGRATE_OGTA
|
||||
std::ostringstream o;
|
||||
o << "invalid query: " << start << " length " << len <<
|
||||
" with data-length " << last << std::endl;
|
||||
throw E_OUTOFRANGE(o.str());
|
||||
#else
|
||||
fprintf(stderr, "Set::Err: queried index out of range (%i, %i ; %i)\n", start, len, last);
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
int v = 0;
|
||||
int t = 0;
|
||||
for (int i=0; i< len; i++) {
|
||||
if (get_item(i+start)) {
|
||||
v = (int)1 << (len-i-1);
|
||||
t += v;
|
||||
}
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
int Set::compare(const Set & other) const {
|
||||
int res = 0;
|
||||
unsigned char* oc;
|
||||
int o_s = 0;
|
||||
int l;
|
||||
int both = 0; // #elements in both sets
|
||||
int io = 0; // #in other set
|
||||
int it = 0; // #in this set
|
||||
|
||||
o_s = other.get_last();
|
||||
oc = other.give_storage();
|
||||
// find the smaller index
|
||||
l = (last >= o_s)? o_s : last;
|
||||
// count away ;-)
|
||||
for (int i = 0; i < l; i++) {
|
||||
unsigned char* o;
|
||||
unsigned char* s;
|
||||
int j = i;
|
||||
mv2byte(storage, j, s);
|
||||
j = i;
|
||||
mv2byte(oc, j, o);
|
||||
if (tstb8(s, j)) {
|
||||
it++;
|
||||
if (tstb8(o, j)) {
|
||||
both++;
|
||||
io++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (tstb8(o, j)) {
|
||||
io++;
|
||||
}
|
||||
}
|
||||
} // for
|
||||
//printf("both %i, it %i, io %i\n", both, it, io);
|
||||
if (both >= 0) {
|
||||
if (both == it && both == io) {
|
||||
res = 3; // sets are equal (maybe empty)
|
||||
}
|
||||
else if (both == it) {
|
||||
res = -1; // this set is smaller than the other, other contains this
|
||||
}
|
||||
else if (both == io) {
|
||||
res = 1; // this set is larger and contains the other
|
||||
}
|
||||
else if (both > 0) {
|
||||
if (io > it) {
|
||||
res = -2;
|
||||
}
|
||||
else {
|
||||
res = 2;
|
||||
}
|
||||
}
|
||||
else res = 0;
|
||||
}
|
||||
return(res);
|
||||
}
|
||||
|
||||
unsigned char* Set::give_storage() const {
|
||||
return(storage);
|
||||
}
|
||||
|
||||
}
|
||||
+135
@@ -0,0 +1,135 @@
|
||||
/************************************************************************
|
||||
* Copyright (c) 2005-2006 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 SET_H
|
||||
#define SET_H
|
||||
|
||||
namespace Util {
|
||||
|
||||
/* Implements a set for non-negative integer values.
|
||||
* storage is a char* which is either a member of this class,
|
||||
* or given to an instance via alternative constructor
|
||||
* or the set_data() function.
|
||||
* Some macros are used to move to the correct char and
|
||||
* read/write the binary data.
|
||||
*/
|
||||
class Set {
|
||||
public:
|
||||
/** Base constructor.
|
||||
* Creates a set of size #MAX_SET_COUNT.
|
||||
*/
|
||||
Set();
|
||||
|
||||
/** Constructor with defined size.
|
||||
* @param n the element count in an integer variable
|
||||
*/
|
||||
Set(int n);
|
||||
/** Copy constructor, copies internal data of another Set.
|
||||
* @param other reference to the other set
|
||||
*
|
||||
* \note This is a deep copy, rather than a shallow one; meaning
|
||||
* this set will allocate memory and memcpy the data, not
|
||||
* hold a pointer into the other set.
|
||||
*/
|
||||
Set(const Set & other);
|
||||
|
||||
/** Constructor with external data set.
|
||||
* @param n Size of storage (in bits)
|
||||
* @param data pointer to external storage
|
||||
* \note The set will not handle freeing of this memory,
|
||||
* it just works on it.
|
||||
*/
|
||||
Set(int n, unsigned char* data);
|
||||
|
||||
~Set();
|
||||
|
||||
/** Give another storage pointer to an instance.
|
||||
* @param n size
|
||||
* @param data pointer to data
|
||||
* @note This only works for instances that were already
|
||||
* constructed with external data.
|
||||
*/
|
||||
void set_data(int n, unsigned char* data);
|
||||
|
||||
/** Mutator for the last index.
|
||||
* @param n the element count
|
||||
* @note You should only shrink sets previously defined @e larger,
|
||||
* not the other way round!
|
||||
*/
|
||||
void set_last(int n);
|
||||
|
||||
/** Set2Number - least significant bits first.
|
||||
*/
|
||||
int as_int(int start, int len) const;
|
||||
|
||||
/** Set2Number - most significant bits first.
|
||||
*/
|
||||
int as_int2(int start, int len) const;
|
||||
|
||||
/** Accessor for the last index.
|
||||
* @return n the element count
|
||||
*/
|
||||
int get_last() const;
|
||||
|
||||
/** Mutator for the contained data.
|
||||
* @param k the index of the item to change
|
||||
* @param val true (k-th item in set) or false (not in set)
|
||||
*/
|
||||
void set_item(int k, bool val);
|
||||
|
||||
/** Accessor for the contained data.
|
||||
* @param k item index to query
|
||||
* @return val either true (item in set) or false (not in set)
|
||||
*/
|
||||
bool get_item(int k) const;
|
||||
|
||||
/** Test set for inclusion with another.
|
||||
* the return value is either:
|
||||
* 0 - the two sets are disjoint
|
||||
* 1 - this set contains the other
|
||||
* 2 - this set contains a subset of the other
|
||||
* -1 - the other set contains this
|
||||
* -2 - the other contains a subset of this
|
||||
* 3 - the two sets are equal
|
||||
* @param other a reference to another Set
|
||||
* @return res an int value as listed above
|
||||
*/
|
||||
int compare(const Set & other) const;
|
||||
|
||||
/** Returns the address of the internal storage data.
|
||||
* KNOW WHAT YOU ARE DOING WITH THIS
|
||||
* @return storage a char* to the entire storage block
|
||||
*/
|
||||
unsigned char* give_storage() const;
|
||||
|
||||
void print_set() const;
|
||||
|
||||
protected:
|
||||
int last;
|
||||
private:
|
||||
unsigned char* storage;
|
||||
bool ext_data;
|
||||
// make sure this is a power of 8
|
||||
static const int MAX_SET_COUNT = 32;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,52 @@
|
||||
#include <SDL_sound.h>
|
||||
#include <SDL_mixer.h>
|
||||
#include "sound_device.h"
|
||||
#include "m_exceptions.h"
|
||||
#include "log.h"
|
||||
|
||||
namespace Audio {
|
||||
SoundDevice::SoundDevice() {
|
||||
memset(cardName, 0, sizeof(cardName));
|
||||
rate = 44100;
|
||||
channels = 2;
|
||||
bufSize = 4096;
|
||||
format = AUDIO_S16;
|
||||
status = CLOSED;
|
||||
}
|
||||
|
||||
SoundDevice::~SoundDevice() {
|
||||
Mix_HaltGroup(-1);
|
||||
Mix_HaltMusic();
|
||||
close();
|
||||
}
|
||||
|
||||
void SoundDevice::close() {
|
||||
if (!status)
|
||||
return;
|
||||
Mix_CloseAudio();
|
||||
status = CLOSED;
|
||||
}
|
||||
|
||||
void SoundDevice::open() {
|
||||
if (status)
|
||||
close();
|
||||
if (Mix_OpenAudio(rate, format, channels, bufSize))
|
||||
throw E_NOTSUPPORTED(SDL_GetError());
|
||||
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;
|
||||
|
||||
status = OPEN;
|
||||
}
|
||||
|
||||
void SoundDevice::open(int r, Uint16 f, int c, int bs) {
|
||||
rate = r;
|
||||
format = f;
|
||||
channels = c;
|
||||
bufSize = bs;
|
||||
open();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
#ifndef UTIL_SOUND_DEVICE_H
|
||||
#define UTIL_SOUND_DEVICE_H
|
||||
|
||||
#include <SDL_stdinc.h>
|
||||
|
||||
namespace Audio {
|
||||
class SoundDevice {
|
||||
public:
|
||||
SoundDevice();
|
||||
~SoundDevice();
|
||||
void close();
|
||||
void open();
|
||||
void open(int _rate, Uint16 _format, int _channels, int _bufsize);
|
||||
inline const char* getCardName() { return cardName; }
|
||||
inline const int getRate() { return rate; }
|
||||
inline const int getNumChannels() { return channels; }
|
||||
inline const int getBufferSize() { return bufSize; }
|
||||
enum Status {
|
||||
CLOSED = 0,
|
||||
OPEN,
|
||||
ERROR,
|
||||
};
|
||||
inline Status getStatus() const { return status; }
|
||||
private:
|
||||
char cardName[50];
|
||||
int rate;
|
||||
int channels;
|
||||
int bufSize;
|
||||
Uint16 format;
|
||||
Status status;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,35 @@
|
||||
#ifndef UTIL_SOUND_MIXER_H
|
||||
#define UTIL_SOUND_MIXER_H
|
||||
|
||||
#include <SDL_sound.h>
|
||||
#include <SDL_mixer.h>
|
||||
#include "sound_device.h"
|
||||
#include "Singleton.h"
|
||||
|
||||
namespace Audio {
|
||||
class Mixer {
|
||||
public:
|
||||
Mixer();
|
||||
~Mixer();
|
||||
|
||||
Audio::SoundDevice device;
|
||||
|
||||
int setVolume(int channel, int volume);
|
||||
int setVolumeMusic();
|
||||
int getVolume(int channel);
|
||||
int getVolumeMusic();
|
||||
|
||||
void playSample(const size_t & sampleId);
|
||||
|
||||
void pause(int channel);
|
||||
void pauseMusic();
|
||||
private:
|
||||
inline bool checkDeviceOK() { return (device.getStatus() ==
|
||||
Audio::SoundDevice::OPEN); }
|
||||
};
|
||||
|
||||
typedef Loki::SingletonHolder<Mixer, Loki::CreateUsingNew,
|
||||
Loki::DefaultLifetime, Loki::SingleThreaded> MixerHolder;
|
||||
}
|
||||
|
||||
#endif
|
||||
+159
@@ -0,0 +1,159 @@
|
||||
#include <SDL.h>
|
||||
#include "timer.h"
|
||||
#include <iostream>
|
||||
|
||||
Timer::TimeEvent::TimeEvent(const uint32_t & b, const uint32_t e, CallbackType & c) :
|
||||
begin(b), end(e), callback(c) {}
|
||||
|
||||
Timer::TimeEvent::TimeEvent(const uint32_t & b, CallbackType & c) :
|
||||
begin(b), end(b), callback(c) {}
|
||||
|
||||
Timer::TimeEvent::TimeEvent(const TimeEvent & o) :
|
||||
begin(o.begin), end(o.end), callback(o.callback) {}
|
||||
|
||||
Timer::Timer() {
|
||||
sdlTicks = SDL_GetTicks();
|
||||
simTicks = 0;
|
||||
delta = 0;
|
||||
simIsRunning = false;
|
||||
}
|
||||
|
||||
Timer::~Timer() {
|
||||
clearAllEvents();
|
||||
}
|
||||
|
||||
void Timer::update() {
|
||||
uint32_t nowTicks = SDL_GetTicks();
|
||||
delta = nowTicks - sdlTicks;
|
||||
sdlTicks = nowTicks;
|
||||
if (simIsRunning)
|
||||
simTicks += delta;
|
||||
|
||||
if (realTimeEvents.size() > 0)
|
||||
checkRTEvents();
|
||||
|
||||
if (simIsRunning && simTimeEvents.size() > 0)
|
||||
checkSimEvents();
|
||||
}
|
||||
|
||||
void Timer::checkRTEvents() {
|
||||
RealTimeMap::iterator i = realTimeEvents.begin();
|
||||
while (i != realTimeEvents.end() && i->first <= sdlTicks) {
|
||||
TimeEvent & te = i->second;
|
||||
float as_float = 0.0f;
|
||||
bool doRemove = false;
|
||||
if (te.begin == te.end) {
|
||||
doRemove = true;
|
||||
}
|
||||
else {
|
||||
if (sdlTicks > te.end) {
|
||||
as_float = 1.0f;
|
||||
doRemove = true;
|
||||
}
|
||||
else
|
||||
as_float = float(sdlTicks - te.begin) / float(te.end - te.begin);
|
||||
}
|
||||
te.callback(as_float);
|
||||
if (doRemove) {
|
||||
RealTimeMap::iterator j = i++;
|
||||
realTimeEvents.erase(j);
|
||||
}
|
||||
else
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
void Timer::checkSimEvents() {
|
||||
SimTimeMap::iterator i = simTimeEvents.begin();
|
||||
while (i != simTimeEvents.end() && i->first <= simTicks) {
|
||||
std::cout << "event start: " << i->first << " now " << simTicks << std::endl;
|
||||
TimeEvent & te = i->second;
|
||||
float as_float = 0.0f;
|
||||
bool doRemove = false;
|
||||
if (te.begin == te.end) {
|
||||
doRemove = true;
|
||||
}
|
||||
else {
|
||||
if (simTicks > te.end) {
|
||||
as_float = 1.0f;
|
||||
doRemove = true;
|
||||
}
|
||||
else
|
||||
as_float = float(simTicks - te.begin) / float(te.end - te.begin);
|
||||
}
|
||||
te.callback(as_float);
|
||||
if (doRemove) {
|
||||
SimTimeMap::iterator j = i++;
|
||||
simTimeEvents.erase(j);
|
||||
}
|
||||
else
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
void Timer::registerCallback(bool simTime, CallbackType & c, const uint32_t & b, const uint32_t & e) {
|
||||
if (simTime)
|
||||
simTimeEvents.insert(std::make_pair<uint32_t, TimeEvent>(b, TimeEvent(b, e, c)));
|
||||
else
|
||||
realTimeEvents.insert(std::make_pair<uint32_t, TimeEvent>(b, TimeEvent(b, e, c)));
|
||||
}
|
||||
|
||||
void Timer::registerCallback(bool simTime, CallbackType & c, const uint32_t & b) {
|
||||
if (simTime)
|
||||
simTimeEvents.insert(std::make_pair<uint32_t, TimeEvent>(b, TimeEvent(b, c)));
|
||||
else
|
||||
realTimeEvents.insert(std::make_pair<uint32_t, TimeEvent>(b, TimeEvent(b, c)));
|
||||
}
|
||||
|
||||
void Timer::clearAllEvents() {
|
||||
realTimeEvents.clear();
|
||||
simTimeEvents.clear();
|
||||
}
|
||||
|
||||
|
||||
#if 0
|
||||
void test1(float a) {
|
||||
std::cout << "test1: " << a << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
SDL_Init(SDL_INIT_TIMER);
|
||||
|
||||
SDL_Delay(10);
|
||||
Timer * t = new Timer;
|
||||
t->setSimulationRunning(true);
|
||||
Timer::CallbackType cb(test1);
|
||||
uint32_t target = t->getTime() + 100;
|
||||
uint32_t finish = target + 100;
|
||||
t->registerCallback(true, cb, target, finish);
|
||||
|
||||
while (t->getRealTime() < target + 1000) {
|
||||
t->update();
|
||||
SDL_Delay(10);
|
||||
}
|
||||
|
||||
/*
|
||||
Timer * t = new Timer;
|
||||
if (t->getRealTime() < 8 || t->getRealTime() > 12)
|
||||
std::cout << "expected 10 got " << t->getRealTime() << std::endl;
|
||||
else
|
||||
std::cout << "got ~10: " << t->getRealTime() << std::endl;
|
||||
|
||||
Timer::CallbackType cb(test1);
|
||||
|
||||
uint32_t target = t->getRealTime() + 100;
|
||||
uint32_t finish = target + 100;
|
||||
t->registerCallback(cb, target, finish);
|
||||
|
||||
uint32_t slept = 0;
|
||||
while (t->getRealTime() < target + 200) {
|
||||
t->update();
|
||||
std::cout << "now: " << t->getRealTime() << std::endl;
|
||||
|
||||
SDL_Delay(10);
|
||||
}
|
||||
*/
|
||||
|
||||
delete t;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,47 @@
|
||||
#ifndef OGTA_TIMER_H
|
||||
#define OGTA_TIMER_H
|
||||
#include <map>
|
||||
#include "Singleton.h"
|
||||
#include "Functor.h"
|
||||
|
||||
class Timer {
|
||||
public:
|
||||
typedef Loki::Functor<void, LOKI_TYPELIST_1(float)> CallbackType;
|
||||
struct TimeEvent {
|
||||
TimeEvent(const uint32_t & b, const uint32_t e, CallbackType & c);
|
||||
TimeEvent(const uint32_t & b, CallbackType & c);
|
||||
TimeEvent(const TimeEvent & o);
|
||||
const uint32_t begin;
|
||||
const uint32_t end;
|
||||
CallbackType callback;
|
||||
};
|
||||
Timer();
|
||||
~Timer();
|
||||
// simulation time in ticks
|
||||
inline const uint32_t & getTime() const { return simTicks; }
|
||||
// time since SDL_Init
|
||||
inline const uint32_t & getRealTime() const { return sdlTicks; }
|
||||
void update();
|
||||
inline void setSimulationRunning(bool yes) { simIsRunning = 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();
|
||||
private:
|
||||
void checkRTEvents();
|
||||
void checkSimEvents();
|
||||
uint32_t sdlTicks;
|
||||
uint32_t simTicks;
|
||||
uint32_t delta;
|
||||
bool simIsRunning;
|
||||
|
||||
typedef std::multimap<uint32_t, TimeEvent> RealTimeMap;
|
||||
RealTimeMap realTimeEvents;
|
||||
typedef RealTimeMap SimTimeMap;
|
||||
SimTimeMap simTimeEvents;
|
||||
};
|
||||
|
||||
typedef Loki::SingletonHolder<Timer, Loki::CreateUsingNew,
|
||||
Loki::DefaultLifetime, Loki::SingleThreaded> TimerHolder;
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user