2007-06-14

This commit is contained in:
Anonymous Maarten committed 2015-12-03 01:38:22 +01:00
1 parent e20673c2cd
commit 1ae34ae340
96 files changed
+5680 -1020

No files matched your search

+79 -1
View File
@@ -40,6 +40,12 @@ namespace Util {
return 360.0f + res;
return res;
}
float xz_turn_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;
return res;
}
bool CellIterator::isValid() const {
if (x < 0 || x > 255)
@@ -51,7 +57,7 @@ namespace Util {
return (z < mapRef.getNumBlocksAtNew(x, y));
}
OpenGTA::Map::BlockInfo & CellIterator::getBlock() {
OpenGTA::Map::BlockInfo & CellIterator::getBlock() const {
assert(isValid());
return *mapRef.getBlockAtNew(x, y, z);
}
@@ -61,4 +67,76 @@ namespace Util {
return (IABS(x - o.x) + IABS(y - o.y) + IABS(z - o.z));
}
bool CellIterator::isBlockType(uint8_t t) const {
if (!isValid())
return false;
return (getBlock().blockType() == t);
}
std::pair<bool, CellIterator> CellIterator::findTypeInCol(uint8_t t) const {
if (isBlockType(t))
return std::make_pair<bool, CellIterator>(true, *this);
CellIterator below = down();
while (below.isValid()) {
if (below.isBlockType(t))
return std::make_pair<bool, CellIterator>(true, below);
below = below.down();
}
CellIterator above = up();
while (above.isValid()) {
if (above.isBlockType(t))
return std::make_pair<bool, CellIterator>(true, above);
above = above.up();
}
return std::make_pair<bool, CellIterator>(false, *this);
}
std::pair<bool, CellIterator> CellIterator::findNeighbourWithType(
uint8_t t, float angle_hint) {
assert(isValid());
if (angle_hint >= 315 || angle_hint < 45) {
std::pair<bool, CellIterator> p = top().findTypeInCol(t);
// if (p.first) INFO << p.second.x << " " << p.second.y << " " << p.second.z << std::endl;
if (p.first) return p;
}
if (angle_hint >= 0 || angle_hint < 90) {
std::pair<bool, CellIterator> p = top().right().findTypeInCol(t);
// if (p.first) INFO << p.second.x << " " << p.second.y << " " << p.second.z << std::endl;
if (p.first) return p;
}
if (angle_hint >= 45 && angle_hint < 135) {
std::pair<bool, CellIterator> p = right().findTypeInCol(t);
// if (p.first) INFO << p.second.x << " " << p.second.y << " " << p.second.z << std::endl;
if (p.first) return p;
}
if (angle_hint >= 90 && angle_hint < 180) {
std::pair<bool, CellIterator> p = right().bottom().findTypeInCol(t);
// if (p.first) INFO << p.second.x << " " << p.second.y << " " << p.second.z << std::endl;
if (p.first) return p;
}
if (angle_hint >= 135 && angle_hint < 225) {
std::pair<bool, CellIterator> p = bottom().findTypeInCol(t);
// if (p.first) INFO << p.second.x << " " << p.second.y << " " << p.second.z << std::endl;
if (p.first) return p;
}
if (angle_hint >= 180 && angle_hint < 270) {
std::pair<bool, CellIterator> p = left().bottom().findTypeInCol(t);
// if (p.first) INFO << p.second.x << " " << p.second.y << " " << p.second.z << std::endl;
if (p.first) return p;
}
if (angle_hint >= 225 && angle_hint < 315) {
std::pair<bool, CellIterator> p = left().findTypeInCol(t);
// if (p.first) INFO << p.second.x << " " << p.second.y << " " << p.second.z << std::endl;
if (p.first) return p;
}
if (angle_hint >= 270 && angle_hint < 360) {
std::pair<bool, CellIterator> p = top().left().findTypeInCol(t);
// if (p.first) INFO << p.second.x << " " << p.second.y << " " << p.second.z << std::endl;
if (p.first) return p;
}
return std::make_pair<bool, CellIterator>(false, *this);
}
}
+5 -1
View File
@@ -31,6 +31,7 @@
namespace Util {
float distance(const Vector3D & p1, const Vector3D & p2);
float xz_angle(const Vector3D & from, const Vector3D & to);
float xz_turn_angle(const Vector3D & from, const Vector3D & to);
class CellIterator {
public:
@@ -89,8 +90,11 @@ namespace Util {
p.z -= 1;
return p;
}
bool isBlockType(uint8_t t) const;
std::pair<bool, CellIterator> findTypeInCol(uint8_t t) const;
std::pair<bool, CellIterator> findNeighbourWithType(uint8_t t, float angle_hint);
int x, y, z;
OpenGTA::Map::BlockInfo & getBlock();
OpenGTA::Map::BlockInfo & getBlock() const;
private:
OpenGTA::Map & mapRef;
};
+21
View File
@@ -26,6 +26,7 @@
#include "m_exceptions.h"
#include "file_helper.h"
#include "buffercache.h"
#include "log.h"
#ifndef OGTA_DEFAULT_DATA_PATH
#define OGTA_DEFAULT_DATA_PATH "gtadata.zip"
@@ -117,4 +118,24 @@ namespace Util {
PHYSFS_close(fd);
return buffer;
}
const std::string FileHelper::lang2MsgFilename(const char *l) {
assert(l);
std::string lang(l);
if (lang.size() > 2)
lang = lang.substr(0, 2);
std::transform(lang.begin(), lang.end(), lang.begin(), tolower);
if (lang == "en")
return std::string("ENGLISH.FXT");
else if (lang == "de")
return std::string("GERMAN.FXT");
else if (lang == "fr")
return std::string("FRENCH.FXT");
else if (lang == "it")
return std::string("ITALIAN.FXT");
WARN << "Unknown language: " << l << " - falling back to english"
<< std::endl;
return std::string("ENGLISH.FXT");
}
}
+1
View File
@@ -40,6 +40,7 @@ namespace Util {
bool existsInVFS(const std::string & file) const;
PHYSFS_file* openReadVFS(const std::string & file) const;
unsigned char* bufferFromVFS(PHYSFS_file*) const;
static const std::string lang2MsgFilename(const char* l);
private:
std::string baseDataPath;
std::string modDataPath;
+559
View File
@@ -0,0 +1,559 @@
#include "gui.h"
#include "m_exceptions.h"
#include "dataholder.h"
#include "gl_spritecache.h"
#include "gl_screen.h"
#ifdef WITH_LUA
#include "lua_vm.h"
#endif
#include <sstream>
#include "localplayer.h"
extern float screen_gamma;
namespace GUI {
Object::Object(const SDL_Rect & r) :
id(0), rect(), color(), borderColor(), drawBorder(false),
manager(ManagerHolder::Instance()) {
copyRect(r);
color.r = 255; color.g = 255; color.b = 255; color.unused = 255;
}
Object::Object(const size_t Id, const SDL_Rect & r) :
id(Id), rect(), color(), borderColor(), drawBorder(false),
manager(ManagerHolder::Instance()) {
copyRect(r);
color.r = 255; color.g = 255; color.b = 255; color.unused = 255;
}
Object::Object(const size_t Id, const SDL_Rect & r, const SDL_Color & c) :
id(Id), rect(), color(), borderColor(), drawBorder(false),
manager(ManagerHolder::Instance()) {
copyRect(r);
copyColor(c);
}
void Object::draw() {
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4ub(color.r, color.g, color.b, color.unused);
glDisable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glVertex2i(rect.x, rect.y);
glVertex2i(rect.x + rect.w, rect.y);
glVertex2i(rect.x + rect.w, rect.y + rect.h);
glVertex2i(rect.x, rect.y + rect.h);
glEnd();
if (drawBorder)
draw_border();
glDisable(GL_BLEND);
}
void Object::draw_border() {
glColor4ub(borderColor.r, borderColor.g, borderColor.b,
borderColor.unused);
glBegin(GL_LINE_LOOP);
glVertex2i(rect.x, rect.y);
glVertex2i(rect.x + rect.w, rect.y);
glVertex2i(rect.x + rect.w, rect.y + rect.h);
glVertex2i(rect.x, rect.y + rect.h);
glEnd();
}
void Object::copyRect(const SDL_Rect & src) {
rect.x = src.x;
rect.y = src.y;
rect.w = src.w;
rect.h = src.h;
}
void Object::copyColor(const SDL_Color & src) {
color.r = src.r;
color.g = src.g;
color.b = src.b;
color.unused = src.unused;
}
void TexturedObject::draw() {
if (texId == 0)
return;
const OpenGL::PagedTexture & tex = manager.getCachedImage(texId);
glColor4ub(color.r, color.g, color.b, color.unused);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex.inPage);
glBegin(GL_QUADS);
glTexCoord2f(tex.coords[0].u, tex.coords[1].v);
glVertex2i(rect.x, rect.y);
glTexCoord2f(tex.coords[1].u, tex.coords[1].v);
glVertex2i(rect.x + rect.w, rect.y);
glTexCoord2f(tex.coords[1].u, tex.coords[0].v);
glVertex2i(rect.x + rect.w, rect.y + rect.h);
glTexCoord2f(tex.coords[0].u, tex.coords[0].v);
glVertex2i(rect.x, rect.y + rect.h);
glEnd();
glDisable(GL_TEXTURE_2D);
if (drawBorder)
draw_border();
}
void AnimatedTextureObject::draw() {
if (!animation)
animation = manager.findAnimation(animId);
assert(animation);
size_t texId = animation->getCurrentFrame();
const OpenGL::PagedTexture & tex = manager.getCachedImage(texId);
glColor4ub(color.r, color.g, color.b, color.unused);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex.inPage);
glBegin(GL_QUADS);
glTexCoord2f(tex.coords[0].u, tex.coords[1].v);
glVertex2i(rect.x, rect.y);
glTexCoord2f(tex.coords[1].u, tex.coords[1].v);
glVertex2i(rect.x + rect.w, rect.y);
glTexCoord2f(tex.coords[1].u, tex.coords[0].v);
glVertex2i(rect.x + rect.w, rect.y + rect.h);
glTexCoord2f(tex.coords[0].u, tex.coords[0].v);
glVertex2i(rect.x, rect.y + rect.h);
glEnd();
glDisable(GL_TEXTURE_2D);
if (drawBorder)
draw_border();
}
void Label::draw() {
glPushMatrix();
glColor4ub(color.r, color.g, color.b, color.unused);
glEnable(GL_TEXTURE_2D);
if (align == 0) {
glTranslatef(rect.x, rect.y, 0);
rect.w = uint16_t(font->drawString(text));
}
else if (align == 1) {
glTranslatef(rect.x, rect.y, 0);
rect.w = uint16_t(font->drawString_r2l(text));
}
rect.h = font->getHeight();
glPopMatrix();
/*
glDisable(GL_TEXTURE_2D);
glBegin(GL_LINE_STRIP);
glVertex2i(rect.x, rect.y);
glVertex2i(rect.x + rect.w, rect.y);
glVertex2i(rect.x + rect.w, rect.y + rect.h);
glVertex2i(rect.x, rect.y + rect.h);
glVertex2i(rect.x, rect.y);
glEnd();
*/
glDisable(GL_TEXTURE_2D);
if (drawBorder)
draw_border();
}
void Pager::draw() {
const OpenGL::PagedTexture & tex = manager.getCachedImage(texId);
glColor4ub(color.r, color.g, color.b, color.unused);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex.inPage);
glBegin(GL_QUADS);
glTexCoord2f(tex.coords[0].u, tex.coords[1].v);
glVertex2i(rect.x, rect.y);
glTexCoord2f(tex.coords[1].u, tex.coords[1].v);
glVertex2i(rect.x + rect.w, rect.y);
glTexCoord2f(tex.coords[1].u, tex.coords[0].v);
glVertex2i(rect.x + rect.w, rect.y + rect.h);
glTexCoord2f(tex.coords[0].u, tex.coords[0].v);
glVertex2i(rect.x, rect.y + rect.h);
glEnd();
glScissor(rect.x, rect.y, rect.w, rect.h);
glEnable(GL_SCISSOR_TEST);
glPushMatrix();
glColor4ub(color.r, color.g, color.b, color.unused);
glTranslatef(rect.x+offset, rect.y+4, 0);
int slen = (int)font->drawString("test - hello world, this is a really long message. it will not end. never... or maybe sometime, but not yet. The end.");
if (slen + offset < -10) {
color.unused = 0;
manager.remove(this);
}
INFO << slen <<" " << offset << std::endl;
glPopMatrix();
glDisable(GL_SCISSOR_TEST);
glDisable(GL_TEXTURE_2D);
if (drawBorder)
draw_border();
}
void Pager::update(Uint32 ticks) {
offset -= 1;
//if (offset < -50)
// color.unused = 0;
}
void Manager::draw() {
GuiObjectListMap::iterator layer_it = guiLayers.begin();
while (layer_it != guiLayers.end()) {
GuiObjectList & inThisLayer = layer_it->second;
for (GuiObjectList::iterator obj_it = inThisLayer.begin();
obj_it != inThisLayer.end(); ++obj_it) {
Object * obj = *obj_it;
obj->draw();
}
++layer_it;
}
glColor4f(1, 1, 1, 1);
glEnable(GL_TEXTURE_2D);
}
Manager::~Manager() {
clearObjects();
clearCache();
}
void Manager::clearObjects() {
INFO << "clearing gui objects" << std::endl;
GuiObjectListMap::iterator layer_it = guiLayers.begin();
while (layer_it != guiLayers.end()) {
GuiObjectList & list = layer_it->second;
for (GuiObjectList::iterator i = list.begin(); i != list.end(); ++i) {
delete *i;
}
list.clear();
++layer_it;
}
guiLayers.clear();
}
void Manager::clearCache() {
INFO << "clearing gui texture cache" << std::endl;
for (GuiTextureCache::iterator i = texCache.begin(); i != texCache.end(); ++i) {
OpenGL::PagedTexture & p = i->second;
glDeleteTextures(1, &p.inPage);
}
texCache.clear();
for (AnimationMap::iterator i = guiAnimations.begin(); i != guiAnimations.end(); ++i) {
delete i->second;
}
guiAnimations.clear();
}
const OpenGL::PagedTexture & Manager::getCachedImage(size_t Id) {
GuiTextureCache::iterator i = findByCacheId(Id);
if (i == texCache.end()) {
std::ostringstream o;
o << "cached texture id " << int(Id);
throw E_UNKNOWNKEY(o.str());
}
return i->second;
}
Manager::GuiTextureCache::iterator Manager::findByCacheId(const size_t & Id) {
return texCache.find(Id);
}
Manager::GuiObjectListMap::iterator Manager::findLayer(uint8_t l) {
return guiLayers.find(l);
}
void Manager::cacheImageRAW(const std::string & file, size_t k) {
texCache.insert(std::make_pair<size_t, OpenGL::PagedTexture>(k, ImageUtil::loadImageRAW(file)));
}
void Manager::cacheImageRAT(const std::string & file, const std::string & palette, size_t k) {
texCache.insert(std::make_pair<size_t, OpenGL::PagedTexture>(k,
ImageUtil::loadImageRATWithPalette(file, palette)));
}
ImageUtil::WidthHeightPair Manager::cacheStyleArrowSprite(const size_t id, int remap) {
OpenGTA::GraphicsBase & graphics = OpenGTA::StyleHolder::Instance().get();
PHYSFS_uint16 t = graphics.spriteNumbers.reIndex(id, OpenGTA::GraphicsBase::SpriteNumbers::ARROW);
OpenGTA::GraphicsBase::SpriteInfo * info = graphics.getSprite(t);
texCache.insert(std::make_pair<size_t, OpenGL::PagedTexture>(
id, OpenGL::SpriteCacheHolder::Instance().createSprite(size_t(t), remap, 0, info)
));
return ImageUtil::WidthHeightPair(info->w, info->h);
}
#ifdef WITH_SDL_IMAGE
void Manager::cacheImageSDL(const std::string & file, size_t k) {
texCache.insert(std::make_pair<size_t, OpenGL::PagedTexture>(k,
ImageUtil::loadImageSDL(file)));
}
#endif
void Manager::add(Object * obj, uint8_t onLevel) {
GuiObjectListMap::iterator l = findLayer(onLevel);
if (l == guiLayers.end()) {
GuiObjectList list;
list.push_back(obj);
guiLayers.insert(std::make_pair<uint8_t, GuiObjectList>(onLevel, list));
return;
}
GuiObjectList & list = l->second;
list.push_back(obj);
}
void Manager::remove(Object * obj) {
for (GuiObjectListMap::iterator l = guiLayers.begin(); l != guiLayers.end(); ++l) {
GuiObjectList & list = l->second;
for (GuiObjectList::iterator m = list.begin(); m != list.end(); ++m) {
Object * o = *m;
if (o == obj) {
delete o;
list.erase(m);
return;
}
}
}
throw E_UNKNOWNKEY("not a managed object-ptr");
}
Object* Manager::findObject(const size_t id) {
for (GuiObjectListMap::iterator l = guiLayers.begin(); l != guiLayers.end(); ++l) {
GuiObjectList & list = l->second;
for (GuiObjectList::iterator m = list.begin(); m != list.end(); ++m) {
Object * o = *m;
if (o->id == id)
return o;
}
}
std::ostringstream o;
o << "object by id " << int(id);
throw E_UNKNOWNKEY(o.str());
return 0;
}
void Manager::removeById(const size_t id) {
Object * o = findObject(id);
if (o)
remove(o);
//else
// ERROR << "failed to find object " << id << " - cannot remove it" << std::endl;
}
bool Manager::isInside(Object & obj, Uint16 x, Uint16 y) const {
if ((obj.rect.x <= x) && (x <= obj.rect.x + obj.rect.w) &&
(obj.rect.y <= y) && (y <= obj.rect.y + obj.rect.h))
return true;
return false;
}
void Manager::receive(SDL_MouseButtonEvent & mb_event) {
Uint32 sh = OpenGL::ScreenHolder::Instance().getHeight();
GuiObjectListMap::reverse_iterator l = guiLayers.rbegin();
while (l != guiLayers.rend()) {
GuiObjectList & list = l->second;
for (GuiObjectList::iterator i = list.begin(); i != list.end(); ++i) {
Object * obj = *i;
if (isInside(*obj, mb_event.x, sh - mb_event.y)) {
// std::cout << "mouse inside obj id: " << obj->id << " at " << mb_event.x << "," << mb_event.y << std::endl;
obj->receive(mb_event);
return;
}
}
++l;
}
}
void Manager::update(uint32_t nowticks) {
for (AnimationMap::iterator i = guiAnimations.begin(); i != guiAnimations.end(); ++i) {
i->second->update(nowticks);
}
GuiObjectListMap::iterator layer_it = guiLayers.begin();
while (layer_it != guiLayers.end()) {
GuiObjectList & inThisLayer = layer_it->second;
for (GuiObjectList::iterator obj_it = inThisLayer.begin();
obj_it != inThisLayer.end(); ++obj_it) {
Object * obj = *obj_it;
obj->update(nowticks);
}
++layer_it;
}
}
Animation* Manager::findAnimation(uint16_t id) {
AnimationMap::iterator i = guiAnimations.find(id);
return i->second;
}
void Manager::createAnimation(const std::vector<uint16_t> & indices, uint16_t fps, size_t k) {
Animation * anim = new Animation(indices, fps);
guiAnimations.insert(std::make_pair<size_t, Animation*>(k, anim));
anim->set(Util::Animation::PLAY_FORWARD, Util::Animation::LOOP);
}
uint16_t Animation::getCurrentFrame() {
return indices[getCurrentFrameNumber()];
}
const int WEAPON_DISPLAY_ID = 100;
const SDL_Rect sdl_rect(size_t a, size_t b, size_t c, size_t d) {
SDL_Rect rect;
rect.x = a;
rect.y = b;
rect.w = c;
rect.h = d;
return rect;
}
WeaponDisplay::WeaponDisplay(const SDL_Rect & r) : Object(WEAPON_DISPLAY_ID, r),
img(sdl_rect(r.x+2, r.y+2, r.w - 4, r.h - 4), 0),
label(sdl_rect(r.x+2, r.y+r.h, r.w - 4, r.h - 4), "", "F_MTEXT.FON", 1) {
}
void WeaponDisplay::draw() {
img.draw();
label.draw();
}
size_t WeaponDisplay::getWeaponIdx(const size_t wt) {
if (wt == 0)
return 0;
if (wt < 5)
return 1000 + wt;
return 0;
}
void WeaponDisplay::setWeapon(const size_t wt) {
img.texId = getWeaponIdx(wt);
try {
manager.getCachedImage(img.texId);
}
catch (const Util::UnknownKey & ek) {
WARN << "GUI image for weapon " << wt << " not loaded - retrying" << std::endl;
manager.cacheStyleArrowSprite(img.texId, -1);
}
}
void ScrollBar::draw() {
Object::draw();
glColor3ub(innerColor.r, innerColor.g, innerColor.b);
glBegin(GL_QUADS);
glVertex2i(rect.x+2, rect.y+2);
glVertex2i(rect.x + int(rect.w * value) -2, rect.y+2);
glVertex2i(rect.x + int(rect.w * value) -2, rect.y + rect.h-2);
glVertex2i(rect.x+2, rect.y + rect.h-2);
glEnd();
}
void ScrollBar::receive(SDL_MouseButtonEvent & mb_event) {
value = (mb_event.x - rect.x) / float(rect.w - 4);
INFO << value << std::endl;
if (changeCB)
changeCB(value * 2);
else
WARN << "No callback function set - I ain't seen nothing" <<
std::endl;
/*
SDL_SetGamma(value * 2, value * 2, value * 2);
Object * o = ManagerHolder::Instance().findObject(101);
if (o) {
std::ostringstream os;
os << "Gamma: " << value * 2;
static_cast<Label*>(o)->text = os.str();
}*/
}
void screen_gamma_callback(float v) {
screen_gamma = v;
SDL_SetGamma(v, v, v);
#ifdef WITH_LUA
OpenGTA::Script::LuaVM & vm = OpenGTA::Script::LuaVMHolder::Instance();
lua_State *L = vm.getInternalState();
int top = lua_gettop(L);
lua_getglobal(L, "config");
if (lua_type(L, -1) != LUA_TTABLE) {
lua_pop(L, 1);
lua_newtable(L);
lua_pushvalue(L, -1);
lua_setglobal(L, "config");
}
uint8_t sf = OpenGTA::StyleHolder::Instance().get().getFormat();
if (sf)
vm.setFloat("screen_gamma_g24", v);
else
vm.setFloat("screen_gamma_gry", v);
lua_settop(L, top);
#endif
Object * o = ManagerHolder::Instance().findObject(GAMMA_LABEL_ID);
if (o) {
std::ostringstream os;
os << "Gamma: " << v;
static_cast<Label*>(o)->text = os.str();
}
/*
Object * o2 = ManagerHolder::Instance().findObject(1001);
if (o2) {
static_cast<ImageStatusDisplay*>(o2)->number = screen_gamma * 3;
}*/
}
AnimStatusDisplay* wantedLevel = NULL;
Label* cashLabel = NULL;
void create_ingame_gui(bool is32bit) {
OpenGL::Screen & screen = OpenGL::ScreenHolder::Instance();
GUI::Manager & gm = ManagerHolder::Instance();
assert(!wantedLevel);
{
SDL_Rect r;
r.h = 32;
r.x = screen.getWidth() / 2 - 50;
r.y = screen.getHeight() - r.h;
r.w = 100;
SDL_Rect rs;
rs.x = rs.y = 0;
rs.w = rs.h = 16;
gm.cacheStyleArrowSprite(16, -1);
gm.cacheStyleArrowSprite(17, -1);
std::vector<uint16_t> anim2frames(2);
anim2frames[0] = 16;
anim2frames[1] = 17;
gm.createAnimation(anim2frames, 10, 2);
wantedLevel = new AnimStatusDisplay(WANTED_LEVEL_ID, r, rs, 2);
/*
wantedLevel->borderColor.r = wantedLevel->borderColor.g = wantedLevel->borderColor.b = wantedLevel->borderColor.unused = 255;
wantedLevel->drawBorder = 1;
*/
gm.add(wantedLevel, 50);
}
assert(!cashLabel);
{
SDL_Rect r;
r.x = screen.getWidth() - 5;
r.y = screen.getHeight() - 30;
cashLabel = new Label(GUI::CASH_ID, r, "0", "F_MTEXT.FON", 1);
cashLabel->align = 1;
gm.add(cashLabel, 50);
}
}
void update_ingame_gui_values() {
OpenGTA::PlayerController & pc = OpenGTA::LocalPlayer::Instance();
if (wantedLevel)
wantedLevel->number = pc.getWantedLevel();
if (cashLabel) {
std::ostringstream os;
os << pc.getCash();
cashLabel->text = os.str();
}
}
void remove_ingame_gui() {
GUI::Manager & gm = ManagerHolder::Instance();
if (wantedLevel)
gm.remove(wantedLevel);
wantedLevel = NULL;
if (cashLabel)
gm.remove(cashLabel);
cashLabel = NULL;
}
}
+89 -4
View File
@@ -36,6 +36,18 @@ namespace GUI {
struct Object;
class Animation;
/** Wrapper around GUI elements.
*
* Contains a number of lists of objects; mouse click events can be delegated
* to any element. Elements are placed into "layers"; drawing is done from
* low to high (0 .. 255), clicked-inside is tested high to low (255 ... 0).
*
* Objects _should_ have a unique id (removeById only finds the first match),
* but this is neither ensured nor required.
*
* Finally there are helper functions (which are used by derived objects) to
* load/access images/animations.
*/
class Manager {
public:
Manager() {}
@@ -47,11 +59,13 @@ namespace GUI {
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);
void cacheImageRAW(const std::string & file, size_t id);
void cacheImageRAT(const std::string & file, const std::string & palette, size_t id);
#ifdef WITH_SDL_IMAGE
void cacheImageSDL(const std::string & file, size_t id);
#endif
ImageUtil::WidthHeightPair cacheStyleArrowSprite(const size_t id, int remap);
const OpenGL::PagedTexture & getCachedImage(size_t Id);
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);
@@ -83,6 +97,8 @@ namespace GUI {
uint16_t getCurrentFrame();
};
/** Base-object - Can be used to draw coloured area & border.
*/
struct Object {
Object(const SDL_Rect & r);
Object(const size_t Id, const SDL_Rect & r);
@@ -91,10 +107,14 @@ namespace GUI {
size_t id;
SDL_Rect rect;
SDL_Color color;
SDL_Color borderColor;
bool drawBorder;
void copyRect(const SDL_Rect & src);
void copyColor(const SDL_Color & src);
virtual void draw();
virtual void update(Uint32 ticks) {}
virtual void receive(SDL_MouseButtonEvent & mb_event) {}
void draw_border();
Manager & manager;
};
@@ -133,9 +153,11 @@ namespace GUI {
const std::string & fontFile, const size_t fontScale) : Object(Id, r), text(s) {
OpenGL::DrawableFont & fnt = OpenGTA::FontCacheHolder::Instance().getFont(fontFile, fontScale);
font = &fnt;
align = 0;
}
OpenGL::DrawableFont * font;
std::string text;
uint8_t align;
void draw();
};
@@ -154,5 +176,68 @@ namespace GUI {
std::string lastMsg;
int offset;
};
struct WeaponDisplay : public Object {
public:
WeaponDisplay(const SDL_Rect & r);
void setWeapon(const size_t wt);
void draw();
private:
size_t getWeaponIdx(const size_t wt);
TexturedObject img;
Label label;
};
struct ScrollBar : public Object {
ScrollBar(const size_t Id, const SDL_Rect & r) : Object(Id, r) {
value = 0.5f;
}
void draw();
void receive(SDL_MouseButtonEvent & mb_event);
SDL_Color innerColor;
float value;
typedef Loki::Functor<void, LOKI_TYPELIST_1(float)> SC_Functor;
SC_Functor changeCB;
};
template <class Child_T, typename V>
struct Number2Status : public Object {
Number2Status(const size_t Id, const SDL_Rect & r, const SDL_Rect & ir, const size_t vId) :
Object(Id, r),
item(r, vId),
number(0), align(0),
innerRect(ir) {}
Child_T item;
V number;
uint8_t align;
SDL_Rect innerRect;
void draw() {
innerRect.x = (align == 0 ? rect.x : rect.x + rect.w - innerRect.w);
innerRect.y = rect.y;
memcpy(&item.rect, &innerRect, sizeof(rect));
for (V k = 0; k < number; k++) {
item.draw();
item.rect.x += (align == 0 ? item.rect.w + 1 : -item.rect.w - 1);
}
if (drawBorder)
draw_border();
}
};
typedef Number2Status<TexturedObject, int32_t> ImageStatusDisplay;
typedef Number2Status<AnimatedTextureObject, int32_t> AnimStatusDisplay;
void screen_gamma_callback(float v);
void create_ingame_gui(bool is32bit);
void update_ingame_gui_values();
void remove_ingame_gui();
static const uint32_t GAMMA_SCROLLBAR_ID = 100;
static const uint32_t GAMMA_LABEL_ID = 101;
static const uint32_t CASH_ID = 200;
static const uint32_t WANTED_LEVEL_ID = 201;
}
#endif
+122 -11
View File
@@ -28,6 +28,9 @@
#include "log.h"
#include "cistring.h"
#include "opengta.h"
#include "physfsrwops.h"
#include "SDL_image.h"
#include "m_exceptions.h"
namespace ImageUtil {
using OpenGL::PagedTexture;
@@ -88,6 +91,7 @@ using OpenGL::PagedTexture;
if (whp.first == 0 || whp.second == 0) {
PHYSFS_close(fd);
WARN << "aborting image load" << std::endl;
throw E_UNKNOWNKEY(name + " - RAW file size unknown");
}
Util::BufferCache::LockedBuffer lbuf(nbytes);
@@ -115,6 +119,7 @@ using OpenGL::PagedTexture;
if (whp.first == 0 || whp.second == 0) {
PHYSFS_close(fd);
WARN << "aborting image load" << std::endl;
throw E_UNKNOWNKEY(name + " - RAT file size unknown");
}
Util::BufferCache::LockedBuffer lb1(nbytes);
PHYSFS_read(fd, lb1(), 1, nbytes);
@@ -131,20 +136,53 @@ using OpenGL::PagedTexture;
return createEmbeddedTexture(whp.first, whp.second, false, lb2());
}
#ifdef WITH_SDL_IMAGE
OpenGL::PagedTexture loadImageSDL(const std::string & name) {
SDL_RWops * rwops = PHYSFSRWOPS_openRead(name.c_str());
SDL_Surface *surface = IMG_Load_RW(rwops, 1);
assert(surface);
NextPowerOfTwo npot(surface->w, surface->h);
uint16_t bpp = surface->format->BytesPerPixel;
uint8_t * buffer = Util::BufferCacheHolder::Instance().requestBuffer(npot.w * npot.h * bpp);
SDL_LockSurface(surface);
copyImage2Image(buffer, (uint8_t*)surface->pixels, surface->pitch, surface->h,
npot.w * bpp);
SDL_UnlockSurface(surface);
SDL_FreeSurface(surface);
GLuint texture = createGLTexture(npot.w, npot.h, (bpp == 4) ? true : false, buffer);
return OpenGL::PagedTexture(texture, 0, 0, GLfloat(surface->w)/npot.w,
GLfloat(surface->h)/npot.h);
}
#endif
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);
if (!mipmapTextures)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
else
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
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);
if (rgba) {
if (mipmapTextures)
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
}
else {
if (mipmapTextures)
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, w, h, GL_RGB, GL_UNSIGNED_BYTE, pixels);
else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
}
if (supportedMaxAnisoDegree > 1.0f)
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, &supportedMaxAnisoDegree);
GL_CHECKERROR;
return tex;
@@ -165,13 +203,86 @@ using OpenGL::PagedTexture;
bool rgba, const void* pixels) {
NextPowerOfTwo npot(w, h);
uint32_t bpp = (rgba ? 4 : 3);
uint8_t* buff = (uint8_t*)pixels;
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);
if (!(npot.w == uint32_t(w) && npot.h == uint32_t(h))) {
uint32_t bpp = (rgba ? 4 : 3);
uint32_t bufSize = npot.w * npot.h * bpp;
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);
}
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define READINT24(x) ((x)[0]<<16 | (x)[1]<<8 | (x)[2])
#define WRITEINT24(x, i) {(x)[0]=i>>16; (x)[1]=(i>>8)&0xff; x[2]=i&0xff; }
uint8_t* scale2x_24bit(const uint8_t* src, const int src_width, const int src_height) {
const int srcpitch = src_width * 3;
const int dstpitch = src_width * 6;
uint8_t *dstpix = Util::BufferCacheHolder::Instance().requestBuffer(src_width *
src_height * 3 * 4);
int E0, E1, E2, E3, B, D, E, F, H;
for(int looph = 0; looph < src_height; ++looph)
{
for(int loopw = 0; loopw < src_width; ++ loopw)
{
B = READINT24(src + (MAX(0,looph-1)*srcpitch) + (3*loopw));
D = READINT24(src + (looph*srcpitch) + (3*MAX(0,loopw-1)));
E = READINT24(src + (looph*srcpitch) + (3*loopw));
F = READINT24(src + (looph*srcpitch) + (3*MIN(src_width-1,loopw+1)));
H = READINT24(src + (MIN(src_height-1,looph+1)*srcpitch) + (3*loopw));
E0 = D == B && B != F && D != H ? D : E;
E1 = B == F && B != D && F != H ? F : E;
E2 = D == H && D != B && H != F ? D : E;
E3 = H == F && D != H && B != F ? F : E;
WRITEINT24((dstpix + looph*2*dstpitch + loopw*2*3), E0);
WRITEINT24((dstpix + looph*2*dstpitch + (loopw*2+1)*3), E1);
WRITEINT24((dstpix + (looph*2+1)*dstpitch + loopw*2*3), E2);
WRITEINT24((dstpix + (looph*2+1)*dstpitch + (loopw*2+1)*3), E3);
}
}
return dstpix;
}
uint8_t* scale2x_32bit(const uint8_t* src, const int src_width, const int src_height) {
const int srcpitch = src_width * 4;
const int dstpitch = src_width * 8;
uint8_t* dstpix = Util::BufferCacheHolder::Instance().requestBuffer(src_width *
src_height * 4 * 4);
Uint32 E0, E1, E2, E3, B, D, E, F, H;
for(int looph = 0; looph < src_height; ++looph)
{
for(int loopw = 0; loopw < src_width; ++ loopw)
{
B = *(Uint32*)(src + (MAX(0,looph-1)*srcpitch) + (4*loopw));
D = *(Uint32*)(src + (looph*srcpitch) + (4*MAX(0,loopw-1)));
E = *(Uint32*)(src + (looph*srcpitch) + (4*loopw));
F = *(Uint32*)(src + (looph*srcpitch) + (4*MIN(src_width-1,loopw+1)));
H = *(Uint32*)(src + (MIN(src_height-1,looph+1)*srcpitch) + (4*loopw));
E0 = D == B && B != F && D != H ? D : E;
E1 = B == F && B != D && F != H ? F : E;
E2 = D == H && D != B && H != F ? D : E;
E3 = H == F && D != H && B != F ? F : E;
*(Uint32*)(dstpix + looph*2*dstpitch + loopw*2*4) = E0;
*(Uint32*)(dstpix + looph*2*dstpitch + (loopw*2+1)*4) = E1;
*(Uint32*)(dstpix + (looph*2+1)*dstpitch + loopw*2*4) = E2;
*(Uint32*)(dstpix + (looph*2+1)*dstpitch + (loopw*2+1)*4) = E3;
}
}
return dstpix;
}
bool mipmapTextures = false;
GLfloat supportedMaxAnisoDegree = 1.0f;
}
+25 -6
View File
@@ -29,8 +29,8 @@
namespace ImageUtil {
typedef std::pair<uint16_t, uint16_t> WidthHeightPair;
/** Helper to calculate 2^k texture sizes.
*/
struct NextPowerOfTwo {
NextPowerOfTwo(uint32_t _w, uint32_t _h) {
w = 1; h = 1;
@@ -41,22 +41,41 @@ namespace ImageUtil {
uint32_t h;
};
/** Run scale2x on 32bit input image.
*
* \note Uses Util::BufferCache for dst memory; lock 'src' before calling
* if it also is a BufferCache buffer.
*/
uint8_t* scale2x_32bit(const uint8_t* src, const int src_width, const int src_height);
uint8_t* scale2x_24bit(const uint8_t* src, const int src_width, const int src_height);
typedef std::pair<uint16_t, uint16_t> WidthHeightPair;
// 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);
//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);
#ifdef WITH_SDL_IMAGE
OpenGL::PagedTexture loadImageSDL(const std::string & name);
#endif
// blitting a buffer into another; they should exist!
extern bool mipmapTextures;
extern GLfloat supportedMaxAnisoDegree;
// plain simple garden-variety create-a-texture; needs to be 2^k
GLuint createGLTexture(GLsizei w, GLsizei h, bool rgba,
const void* pixels);
// blitting a buffer into another; no checks done!
void copyImage2Image(uint8_t *dest, const uint8_t *src, const uint16_t
srcWidth, const uint16_t srcHeight, const uint16_t destWidth);
// texture-class instance from pixel data; does transform to 2^k if required
OpenGL::PagedTexture createEmbeddedTexture(GLsizei w, GLsizei h, bool rgba,
const void *pixels);
}
+68
View File
@@ -0,0 +1,68 @@
/************************************************************************
* 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_keysym.h>
#include "key_handler.h"
#include "log.h"
namespace Util {
bool IngameCommonKeys::up(const uint32_t & key) {
bool handled = false;
return handled;
}
bool IngameCommonKeys::down(const uint32_t & key) {
return false;
}
void KeyHandlerChain::up(const uint32_t & key) {
for (ListOfHandlers::iterator i = activeHandlers.begin();
i != activeHandlers.end(); i++) {
if ((*i)->up(key))
return;
}
}
void KeyHandlerChain::down(const uint32_t & key) {
for (ListOfHandlers::iterator i = activeHandlers.begin();
i != activeHandlers.end(); i++) {
if ((*i)->down(key))
return;
}
}
void KeyHandlerChain::addHandler(KeyHandler * kh, bool front) {
if (front)
activeHandlers.push_front(kh);
else
activeHandlers.push_back(kh);
}
void KeyHandlerChain::removeHandler(KeyHandler * kh) {
for (ListOfHandlers::iterator i = activeHandlers.begin();
i != activeHandlers.end(); i++) {
if (kh == *i)
activeHandlers.erase(i);
}
ERROR << "not a valid handler pointer" << std::endl;
}
}
+86
View File
@@ -0,0 +1,86 @@
/************************************************************************
* 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_KEYHANDLER_H
#define UTIL_KEYHANDLER_H
#include <stdint.h>
#include <list>
#include <vector>
#include "pf_tree.hpp"
namespace Util {
/** Interface for key listeners.
*
* Must implement both keypress and release functions;
* the functions should return \em true when the input
* value is consumed by a handler.
*/
class KeyHandler {
public:
virtual ~KeyHandler() {}
virtual bool up(const uint32_t & key) = 0;
virtual bool down(const uint32_t & key) = 0;
};
/** Keys that do _not_ control the player character.
*/
class IngameCommonKeys : public KeyHandler {
public:
bool up(const uint32_t & key);
bool down(const uint32_t & key);
};
class MenuKeys : public KeyHandler {
public:
bool up(const uint32_t & key);
bool down(const uint32_t & key);
};
class CheatKeys : public KeyHandler {
public:
bool up(const uint32_t & key);
bool down(const uint32_t & key);
template <typename T>
struct Callback_Lua {
void call(T);
};
typedef PrefixFreeTree::Walker<uint32_t, Callback_Lua> TreeOfCheats;
private:
std::vector<uint32_t> cheat_fn_refs;
};
class KeyHandlerChain {
public:
void up(const uint32_t & key);
void down(const uint32_t & key);
void addHandler(KeyHandler*, bool front = true);
void removeHandler(KeyHandler*);
private:
typedef std::list<KeyHandler*> ListOfHandlers;
ListOfHandlers activeHandlers;
};
}
#endif
+44 -3
View File
@@ -12,17 +12,58 @@
#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; }
#define ANSI_COLOR_OFF "\033[0m"
#define ANSI_COLOR_INFO "\033[32mInfo ("
#define ANSI_COLOR_WARN "\033[33mWarning ("
#define ANSI_COLOR_ERR "\033[31mError ("
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;
if (level) return emptyStream;
#ifdef LOG_USE_ANSI_COLORS
std::cout << ANSI_COLOR_INFO <<
#else
std::cout << "Info (" <<
#endif
f << ":" << l << "): "
#ifdef LOG_USE_ANSI_COLORS
<< ANSI_COLOR_OFF;
#else
;
#endif
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;
if (level > 1) return emptyStream;
#ifdef LOG_USE_ANSI_COLORS
std::cerr << ANSI_COLOR_WARN <<
#else
std::cerr << "Warning (" <<
#endif
f << ":" << l << "): "
#ifdef LOG_USE_ANSI_COLORS
<< ANSI_COLOR_OFF;
#else
;
#endif
return std::cerr;
}
inline static std::ostream & error(const char* f, int l) {
std::cerr << "Error ("<< f << ":" << l << "): "; return std::cerr;
#ifdef LOG_USE_ANSI_COLORS
std::cerr << ANSI_COLOR_ERR <<
#else
std::cerr << "Error (" <<
#endif
f << ":" << l << "): "
#ifdef LOG_USE_ANSI_COLORS
<< ANSI_COLOR_OFF;
#else
;
#endif
return std::cerr;
}
static void setOutputLevel(unsigned int newLevel);
static const char* glErrorName(int k);
+41
View File
@@ -4,6 +4,22 @@
namespace Util {
void SpriteCreationArea::setRects(const SDL_Rect & allowed, const SDL_Rect & denied) {
validRects = std::make_pair<SDL_Rect, SDL_Rect>(allowed, denied);
onScreen = denied;
}
bool SpriteCreationArea::isOnScreen(const Vector3D & p) {
/* INFO << p.x << " " << p.y << " " << p.z << std::endl;
INFO << onScreen.x <<", " << onScreen.y << " -> " <<
onScreen.x + onScreen.w << ", " << onScreen.y + onScreen.h << std::endl;
*/
if ((p.x >= onScreen.x) && (p.x <= onScreen.x + onScreen.w) &&
(p.z >= onScreen.y) && (p.z <= onScreen.y + onScreen.h))
return true;
return false;
}
bool SpriteCreationArea::isOffScreen(const Vector3D & p) {
return false;
}
TupleOfUint8 SpriteCreationArea::getValidCoord() {
@@ -11,4 +27,29 @@ namespace Util {
uint32_t y = rnd.nextUint(validRects.first.h) + validRects.first.y;
return std::make_pair<uint8_t, uint8_t>(x, y);
}
int item_count(MapOfPair2Int & m, int a, int b) {
MapOfPair2Int::iterator j = m.find( std::make_pair<int, int>(a, b) );
if (j == m.end())
return 0;
return j->second;
}
void register_item1(MapOfPair2Int & m, int a, int b) {
m.insert( std::make_pair< Pair2Int, int>(
std::make_pair<int, int>(a, b),
1)
);
}
void register_item(MapOfPair2Int & m, int a, int b) {
Pair2Int _p(std::make_pair<int, int>(a, b));
MapOfPair2Int::iterator j = m.find(_p);
if (j == m.end())
register_item1(m, a, b);
else
j->second++;
}
}
+39
View File
@@ -2,7 +2,10 @@
#define MAP_HELPER_H
#include <list>
#include <map>
#include <SDL_video.h>
#include <cassert> // yasli/random.h needs it
#include "math3d.h"
#include "yasli/random.h"
namespace Util {
@@ -17,6 +20,42 @@ namespace Util {
void setRects(const SDL_Rect & allowed, const SDL_Rect & denied);
TupleOfUint8 getValidCoord();
Random rnd;
bool isOnScreen(const Vector3D & p);
bool isOffScreen(const Vector3D & p);
private:
SDL_Rect onScreen;
};
/** lesser than comparison of two std::pair<int, int>.
*/
template <typename T>
struct lt_pair {
bool operator() (const T & a, const T & b) {
if (a.first < b.first)
return true;
if (a.first > b.first)
return false;
return (a.second < b.second);
}
};
typedef std::pair<int, int> Pair2Int;
typedef lt_pair<std::pair<int, int> > cmp_Pair2Int;
typedef std::map<Pair2Int, int, cmp_Pair2Int> MapOfPair2Int;
/** count of Pair2Int(a,b) in the map.
* @return 0 if no entry at that \e position
* @return count otherwise
*/
int item_count(MapOfPair2Int & m, int a, int b);
/** insert (may overwrite) count 1 at Pair2Int(a, b).
*/
void register_item1(MapOfPair2Int & m, int a, int b);
/** register and count Pair2Int(a, b).
*/
void register_item(MapOfPair2Int & m, int a, int b);
}
#endif
+96
View File
@@ -0,0 +1,96 @@
/************************************************************************
* 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 "pf_tree.hpp"
namespace PrefixFreeTree {
Node::~Node() {
for (MapType::iterator i = map.begin(); i != map.end(); i++) {
delete i->second;
}
map.clear();
}
Node & Node::insert(const char * str, size_t offset) {
size_t str_len = strlen(str);
if (offset < str_len) {
value_t node_val = str[offset];
MapType::iterator i = map.find(node_val);
offset++;
if (i == map.end()) {
map[node_val] = new Node();
return map[node_val]->insert(str, offset);
}
else {
if (i->second->isLeaf()) {
std::ostringstream os;
os << "Cannot enter '" << str << "' at offset " << offset <<
" as a leaf node already exists";
throw E_INVALIDFORMAT(os.str());
}
if ((offset == str_len) && (!i->second->isLeaf())) {
std::ostringstream os;
os << "Cannot enter '" << str
<< "' as a non-leaf node already exists" << std::endl;
throw E_INVALIDFORMAT(os.str());
}
return i->second->insert(str, offset);
}
}
return *this;
}
}
#ifdef SIMPLE_TREE_DEMO
#include <iostream>
template <typename T>
struct Echo {
void call(T t) {
std::cout << "Echo: " << t << std::endl;
}
};
using namespace std;
int main(int argc, char* argv[]) {
PrefixFreeTree::Walker<uint32_t, Echo> m;
uint32_t i = 1;
while (!cin.eof()) {
std::string inputs;
cin >> inputs;
if (inputs.size()) {
std::cout << "-" << inputs << "-" << std::endl;
PrefixFreeTree::Node & n = m.insert(inputs.c_str());
n.leaf_value = i;
i++;
}
}
const char* input = argv[1];
int len = strlen(input);
for (int i = 0; i < len; i++) {
m.push(input[i]);
}
}
#endif
+75
View File
@@ -0,0 +1,75 @@
/************************************************************************
* 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 PREFIX_FREE_TREE_H
#define PREFIX_FREE_TREE_H
#include <string.h>
#include <map>
#include <string>
#include <sstream>
#include "m_exceptions.h"
namespace PrefixFreeTree {
struct Node {
~Node();
typedef int value_t;
uint32_t leaf_value;
typedef std::map<value_t, Node* > MapType;
MapType map;
bool isLeaf() const { return (map.size() == 0); }
Node & insert(const char * str, size_t offset = 0);
};
#define CHECK_HANDLE_LEAF if (curPos->isLeaf()) { Handler<T>::call(curPos->leaf_value); }
template <typename T, template <class> class Handler>
class Walker : public Handler<T>, public Node {
public:
Walker() : Handler<T>(), Node(), curPos(this) {}
void push(int v) {
MapType::iterator i = curPos->map.find(v);
if (i != curPos->map.end()) {
curPos = i->second;
CHECK_HANDLE_LEAF;
}
else {
i = map.find(v);
if (i != map.end()) {
curPos = i->second;
CHECK_HANDLE_LEAF;
}
else
curPos = this;
}
}
private:
Node * curPos;
};
}
#undef CHECK_HANDLE_LEAF
#endif
+6 -1
View File
@@ -46,7 +46,12 @@ namespace Audio {
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);
Uint8 *mem2 = NULL;
if (file.find("000") != std::string::npos) {
mem2 = (Uint8*)Audio::resample16(mem, e.rawSize, si, e.sampleRate, 44100);
}
else
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);
}
+55
View File
@@ -20,6 +20,7 @@
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#include <iostream>
#include "sound_resample2.h"
#include "interpolate.hpp"
@@ -89,4 +90,58 @@ namespace Audio {
while (src < src_end);
return stereo_data;
}
Sint16 *resample16 (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;
while (size & 0xFFFF0000)
size >>= 1, rate = (rate >> 1) + 1;
size = (size * wanted_rate / rate) << 1;
Sint16 *stereo_data = new Sint16[size];
Sint16 *data = stereo_data;
Uint8 *src_end = src + sourcelen;
int result;
int a = *(src + 0);
a = a + (*(src + 1) << 8);
int b = *(src + 2);
b = b + (*(src + 3) << 8);
int c = *(src + 4);
c = c + (*(src + 5) << 8);
std::cout << a << " " << b << " " << c << std::endl;
Math::Interpolator::Cubic<float> inter(a * 0.8333f, b * 0.8333f, c * 0.8333f);
do
{
do
{
result = int(inter.getAt(fp_pos / float(0x0000FFFF)));
if (result < -32768)
result = -32768;
else if (result > 32767)
result = 32767;
*data++ = result;
fp_pos += fp_speed;
}
while (!(fp_pos & 0xFFFF0000));
src++;
fp_pos &= 0x0000FFFF;
if (src + 2 < src_end)
{
c = *(src + 2);
c = c + (*(src + 3) << 8);
inter.shiftAndFeed (c * 0.8333f);
}
else
inter.shift ();
}
while (src < src_end);
return stereo_data;
}
}
+1 -1
View File
@@ -28,7 +28,7 @@ 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,
Sint16 *resample16 (Uint8 * sourcedata, size_t sourcelen,
size_t & destlen, int current_rate,
int wanted_rate);
}
+16 -2
View File
@@ -1,6 +1,7 @@
#include <iostream>
#include <iomanip>
#include "sound_system.h"
#include "sound_music_player.h"
#include "physfsrwops.h"
#include "m_exceptions.h"
#include "log.h"
@@ -10,7 +11,12 @@ namespace Audio {
#ifdef WITH_SOUND
SoundSystem::SoundSystem() : device(), chunkCache() {
SDL_InitSubSystem(SDL_INIT_AUDIO);
device.open();
try {
device.open();
}
catch (const Exception & e) {
ERROR << e.what() << std::endl;
}
enabled = true;
if (device.getStatus() == SoundDevice::OPEN)
Sound_Init();
@@ -59,6 +65,8 @@ namespace Audio {
}
void SoundSystem::listMusicDecoders() {
if (!enabled)
return;
std::cout << "* Supported music decoders *" << std::endl;
const Sound_DecoderInfo **i;
for (i = Sound_AvailableDecoders(); *i != NULL; i++) {
@@ -91,7 +99,13 @@ int main(int argc, char* argv[]) {
SoundSystem noisemaker;
noisemaker.listMusicDecoders();
noisemaker.playMusic(argv[1]);
if (argc == 2)
noisemaker.playMusic(argv[1]);
if (argc == 3) {
noisemaker.playFx(argv[1], atoi(argv[2]));
while (Mix_Playing(-1))
SDL_Delay(500);
}
MusicPlayerCtrl::musicFinishedCB = CB_MusicDone;
while (MusicPlayerCtrl::isPlaying) {
SDL_Delay(1000);
+2 -2
View File
@@ -18,8 +18,8 @@ namespace Audio {
SoundSystem();
~SoundSystem();
void playFx(std::string file, size_t idx);
void SoundSystem::playMusic(std::string file);
void SoundSystem::listMusicDecoders();
void playMusic(std::string file);
void listMusicDecoders();
bool enabled;
};
}