diff options
| author | Ben Boeckel <MathStuf@gmail.com> | 2008-01-19 01:22:22 +0000 |
|---|---|---|
| committer | Ben Boeckel <MathStuf@gmail.com> | 2008-01-19 01:22:22 +0000 |
| commit | 342d0201b07d9030fced2f3baf3ecdefc841879a (patch) | |
| tree | f0715cc7b267e2ce2bc93971d9e95c7e0c9b2b62 /general | |
| parent | d017d90324a439dfb4bd3f25d30deb672bc40fd4 (diff) | |
[DEL] Removed PokeGen namespace
[ADD] general/AudioCache.h completed
[DEL] VERSION removed from subdir .pro files
[DEL] audio/
git-svn-id: https://pokegen.svn.sourceforge.net/svnroot/pokegen/trunk@33 6ecfd1a5-f3ed-3746-8530-beee90d26b22
Diffstat (limited to 'general')
| -rw-r--r-- | general/AudioCache.h | 117 | ||||
| -rw-r--r-- | general/Exception.h | 197 | ||||
| -rw-r--r-- | general/Flag.cpp | 2 | ||||
| -rw-r--r-- | general/Flag.h | 91 | ||||
| -rw-r--r-- | general/Frac.cpp | 12 | ||||
| -rw-r--r-- | general/Frac.h | 101 | ||||
| -rw-r--r-- | general/FracMatrix.h | 97 | ||||
| -rw-r--r-- | general/Hat.h | 99 | ||||
| -rw-r--r-- | general/ImageCache.cpp | 2 | ||||
| -rw-r--r-- | general/ImageCache.h | 11 | ||||
| -rw-r--r-- | general/Ini.cpp | 38 | ||||
| -rw-r--r-- | general/Ini.h | 73 | ||||
| -rw-r--r-- | general/Matrix.h | 379 | ||||
| -rw-r--r-- | general/Point.h | 71 | ||||
| -rw-r--r-- | general/Ref.cpp | 16 | ||||
| -rw-r--r-- | general/Ref.h | 153 | ||||
| -rw-r--r-- | general/general.pro | 1 |
17 files changed, 773 insertions, 687 deletions
diff --git a/general/AudioCache.h b/general/AudioCache.h new file mode 100644 index 00000000..4a691ac5 --- /dev/null +++ b/general/AudioCache.h @@ -0,0 +1,117 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: general/AudioCache.h +// Purpose: Cache for sound effects +// Author: Ben Boeckel +// Modified by: Ben Boeckel +// Created: Fri Jan 18 15:29:22 2008 +// Copyright: ©2007-2008 Ben Boeckel and Nerdy Productions +// Licence: +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program. If not, see <http://www.gnu.org/licenses/>. +///////////////////////////////////////////////////////////////////////////// + +#ifndef __AUDIO_CACHE__ +#define __AUDIO_CACHE__ + +#include <QCache> +#include <QMutableVectorIterator> +#include <QVector> +#include <Phonon/MediaObject> +#include "Exception.h" + +class AudioCache +{ + public: + void playSFX(const QString& url, const bool force = false) static throw(OpenException) + { + if (force) + cache.remove(url); + if (!cache.contains(url)) + { + MediaObject sfx; + sfx.setCurrentSource(url); + if (sfx.state() == Phonon::ErrorState) + throw(OpenException("AudioCache", url)); + cache.insert(url, sfx); + } + MediaObject sfx = *(cache.object(url)); + sfx.play(); + curPlay.append(sfx); + curPlay[curPlay.size() - 1].play(); + } + + void playMusic(const QString& url) static + { + if (!started) + start(); + musicUrl = url; + if (music.state() == Phonon::PlayingState) + music.seek(music.totalTime - 1000); + else + { + music.setCurrentSource(url); + music.play(); + } + } + + void clear() static + { + cache.clear(); + } + void prune() static + { + for (QMutableVectorIterator<MediaObject> i(curPlay); i.hasNext(); i.next()) + { + if (i.value().state() == Phonon::StoppedState) + i.remove(); + } + } + + void remove(const QString& url) static + { + cache.remove(url); + } + + void start() static + { + if (!started) + connect(&music, SIGNAL(aboutToFinish()), SLOT(loopMusic())); + } + + float getVolume() static + { + return output.volume; + } + void setVolume(const float v) static + { + output.volume = v; + } + private: + void loopMusic() static + { + music.enqueue(musicUrl); + } + + static QCache<QString, MediaObject> cache; + static QVector<MediaObject*> curPlay; + static MediaObject music; + static QString musicUrl; + static AudiOutput output; + static bool started; +}; + +QCache<QString, MediaObject> AudioCache::sounds(4096); +AudioOutput AudioCache::output(Phonon::MusicCategory); +bool AudioCache::started = false; + +#endif diff --git a/general/Exception.h b/general/Exception.h index 53a7ab4b..5197e88d 100644 --- a/general/Exception.h +++ b/general/Exception.h @@ -23,106 +23,103 @@ #ifndef __EXCEPTIONS__ #define __EXCEPTIONS__ -namespace PokeGen +class Exception { - class Exception - { - public: - Exception(const QString& c, const QString e) : - cls(c), - error(e) - { - } - - QString getMsg() const - { - return QString("%1: %2").arg(cls).arg(error); - } - private: - QString cls; - QString error; - }; - - class UnusedException : public Exception - { - public: - UnusedException(const QString& c, const QString& var) : - Exception(c, QString("setting unused %1").arg(var)) - { - } - }; - - class BoundsException : public Exception - { - public: - BoundsException(const QString& c, const QString& var) : - Exception(c, QString("%1 out-of-bounds").arg(var)) - { - } - }; - - class IndexException : public BoundsException - { - public: - IndexException(const QString& c) : - BoundsException(c, "index") - { - } - }; - - class ReplaceException : public Exception - { - public: - ReplaceException(const QString& c, const QString& f) : - Exception(c, QString("unable to replace %1").arg(f)) - { - } - }; - - class SaveException : public Exception - { - public: - SaveException(const QString& c, const QString& f) : - Exception(c, QString("unable to save %1").arg(f)) - { - } - }; - - class OpenException : public Exception - { - public: - OpenException(const QString& c, const QString& f) : - Exception(c, QString("unable to open %1").arg(f)) - { - } - }; - - class DirException : public Exception - { - public: - DirException(const QString& c, const QString& p) : - Exception(c, QString("unable to create path %1").arg(p)) - { - } - }; - - class InvalidException : public Exception - { - public: - InvalidException(const QString& c, const QString& f) : - Exception(c, QString("%1 is invalid").arg(f)) - { - } - }; - - class RemoveException : public Exception - { - public: - RemoveException(const QString& c, const QString& f) : - Exception(c, QString("unable to remove %1").arg(f)) - { - } - }; -} + public: + Exception(const QString& c, const QString e) : + cls(c), + error(e) + { + } + + QString getMsg() const + { + return QString("%1: %2").arg(cls).arg(error); + } + private: + QString cls; + QString error; +}; + +class UnusedException : public Exception +{ + public: + UnusedException(const QString& c, const QString& var) : + Exception(c, QString("setting unused %1").arg(var)) + { + } +}; + +class BoundsException : public Exception +{ + public: + BoundsException(const QString& c, const QString& var) : + Exception(c, QString("%1 out-of-bounds").arg(var)) + { + } +}; + +class IndexException : public BoundsException +{ + public: + IndexException(const QString& c) : + BoundsException(c, "index") + { + } +}; + +class ReplaceException : public Exception +{ + public: + ReplaceException(const QString& c, const QString& f) : + Exception(c, QString("unable to replace %1").arg(f)) + { + } +}; + +class SaveException : public Exception +{ + public: + SaveException(const QString& c, const QString& f) : + Exception(c, QString("unable to save %1").arg(f)) + { + } +}; + +class OpenException : public Exception +{ + public: + OpenException(const QString& c, const QString& f) : + Exception(c, QString("unable to open %1").arg(f)) + { + } +}; + +class DirException : public Exception +{ + public: + DirException(const QString& c, const QString& p) : + Exception(c, QString("unable to create path %1").arg(p)) + { + } +}; + +class InvalidException : public Exception +{ + public: + InvalidException(const QString& c, const QString& f) : + Exception(c, QString("%1 is invalid").arg(f)) + { + } +}; + +class RemoveException : public Exception +{ + public: + RemoveException(const QString& c, const QString& f) : + Exception(c, QString("unable to remove %1").arg(f)) + { + } +}; #endif diff --git a/general/Flag.cpp b/general/Flag.cpp index 8dc6026e..ab86fa1b 100644 --- a/general/Flag.cpp +++ b/general/Flag.cpp @@ -22,4 +22,4 @@ #include "Flag.h" -const char* PokeGen::Flag::ValueStr[PokeGen::Flag::End] = {"Off", "On", "Ignore"}; +const char* Flag::ValueStr[Flag::End] = {"Off", "On", "Ignore"}; diff --git a/general/Flag.h b/general/Flag.h index 3862ad3b..0aae7fda 100644 --- a/general/Flag.h +++ b/general/Flag.h @@ -27,54 +27,51 @@ #include <QString> #include "Ref.h" -namespace PokeGen +class Flag { - class Flag - { - public: - enum - { - Off, - On, - Ignore, - End - }; - static const char* ValueStr[End]; - - Flag(const unsigned f = 0, const unsigned s = 0) : - flag(f) - { - setStatus(s); - } - - void set(const unsigned f, const unsigned s) - { - setFlag(f); - setStatus(s); - } - void setFlag(const unsigned f) - { - flag = f; - } - void setStatus(const unsigned s) - { - if (End <= s) - throw("Flag: status out-of-bounds"); - status = s; - } + public: + enum + { + Off, + On, + Ignore, + End + }; + static const char* ValueStr[End]; - unsigned getFlag() const - { - return flag; - } - unsigned getStatus() const - { - return status; - } - private: - unsigned flag; - unsigned status; - }; -} + Flag(const unsigned f = 0, const unsigned s = 0) : + flag(f) + { + setStatus(s); + } + + void set(const unsigned f, const unsigned s) + { + setFlag(f); + setStatus(s); + } + void setFlag(const unsigned f) + { + flag = f; + } + void setStatus(const unsigned s) + { + if (End <= s) + throw("Flag: status out-of-bounds"); + status = s; + } + + unsigned getFlag() const + { + return flag; + } + unsigned getStatus() const + { + return status; + } + private: + unsigned flag; + unsigned status; +}; #endif diff --git a/general/Frac.cpp b/general/Frac.cpp index 539205ef..76e7fd0b 100644 --- a/general/Frac.cpp +++ b/general/Frac.cpp @@ -22,7 +22,7 @@ #include "Frac.h" -void PokeGen::Frac::set(const unsigned n, const unsigned d) throw(Exception) +void Frac::set(const unsigned n, const unsigned d) throw(Exception) { if ((type == Improper) || ((n < d) && (type == Proper)) || ((d < n) && (type == Over1))) { @@ -33,23 +33,23 @@ void PokeGen::Frac::set(const unsigned n, const unsigned d) throw(Exception) throw(Exception("Frac", "values conflict with type")); } -void PokeGen::Frac::set(const unsigned n, const unsigned d, const unsigned t) throw(Exception) +void Frac::set(const unsigned n, const unsigned d, const unsigned t) throw(Exception) { setType(t); set(n, d); } -void PokeGen::Frac::setNum(const unsigned n) throw(Exception) +void Frac::setNum(const unsigned n) throw(Exception) { set(n, denom); } -void PokeGen::Frac::setDenom(const unsigned d) throw(Exception) +void Frac::setDenom(const unsigned d) throw(Exception) { set(num, d); } -void PokeGen::Frac::setType(const unsigned t) throw(BoundsException) +void Frac::setType(const unsigned t) throw(BoundsException) { if (t < End) { @@ -60,7 +60,7 @@ void PokeGen::Frac::setType(const unsigned t) throw(BoundsException) throw(BoundsException("Frac", "type")); } -void PokeGen::Frac::reduce() +void Frac::reduce() { int i = num; int j = denom; diff --git a/general/Frac.h b/general/Frac.h index 7be576c6..f5b178b1 100644 --- a/general/Frac.h +++ b/general/Frac.h @@ -27,58 +27,55 @@ #include <QString> #include "Exception.h" -namespace PokeGen +class Frac { - class Frac - { - public: - enum - { - Proper = 0, - Improper = 1, - Over1 = 2, - End = 3 - }; - - Frac(const unsigned t = Proper) - { - setType(t); - } - Frac(const unsigned n, const unsigned d, const unsigned t = Proper) - { - set(n, d, t); - } - - void set(const unsigned n, const unsigned d) throw(Exception); - void set(const unsigned n, const unsigned d, const unsigned t) throw(Exception); - void setNum(const unsigned n) throw(Exception); - void setDenom(const unsigned d) throw(Exception); - void setType(const unsigned t) throw(BoundsException); - - unsigned getNum() const - { - return num; - } - unsigned getDenom() const - { - return denom; - } - unsigned getType() const - { - return type; - } - - void reduce(); - - operator float() const - { - return (num / denom); - } - private: - unsigned num; - unsigned denom; - unsigned type; - }; -} + public: + enum + { + Proper = 0, + Improper = 1, + Over1 = 2, + End = 3 + }; + + Frac(const unsigned t = Proper) + { + setType(t); + } + Frac(const unsigned n, const unsigned d, const unsigned t = Proper) + { + set(n, d, t); + } + + void set(const unsigned n, const unsigned d) throw(Exception); + void set(const unsigned n, const unsigned d, const unsigned t) throw(Exception); + void setNum(const unsigned n) throw(Exception); + void setDenom(const unsigned d) throw(Exception); + void setType(const unsigned t) throw(BoundsException); + + unsigned getNum() const + { + return num; + } + unsigned getDenom() const + { + return denom; + } + unsigned getType() const + { + return type; + } + + void reduce(); + + operator float() const + { + return (num / denom); + } + private: + unsigned num; + unsigned denom; + unsigned type; +}; #endif diff --git a/general/FracMatrix.h b/general/FracMatrix.h index 3becd3dc..8d884910 100644 --- a/general/FracMatrix.h +++ b/general/FracMatrix.h @@ -26,64 +26,61 @@ #include "Frac.h" #include "Matrix.h" -namespace PokeGen +class FracMatrix : public Matrix<Frac> { - class FracMatrix : public Matrix<Frac> - { - public: - FracMatrix() : - Matrix<Frac>() + public: + FracMatrix() : + Matrix<Frac>() + { + } + FracMatrix(const unsigned w, const unsigned h, const Frac &d = Frac()) : + Matrix<Frac>(w, h, d) + { + } + FracMatrix(const QString& fname) + { + load(fname); + } + + void load(const QString& fname) + { + Ini ini(fname); + ini.getValue("width", Matrix<Frac>::width, 0); + ini.getValue("height", Matrix<Frac>::height, 0); + if (!(Matrix<Frac>::width && Matrix<Frac>::height)) + return; + Matrix<Frac>::matrix.resize(Matrix<Frac>::width); + Matrix<Frac>::matrix.fill(QVector<Frac>(Matrix<Frac>::height, Frac(1, 1))); + for (unsigned i = 0; i < Matrix<Frac>::width; ++i) { - } - FracMatrix(const unsigned w, const unsigned h, const Frac &d = Frac()) : - Matrix<Frac>(w, h, d) - { - } - FracMatrix(const QString& fname) - { - load(fname); - } - - void load(const QString& fname) - { - Ini ini(fname); - ini.getValue("width", Matrix<Frac>::width, 0); - ini.getValue("height", Matrix<Frac>::height, 0); - if (!(Matrix<Frac>::width && Matrix<Frac>::height)) - return; - Matrix<Frac>::matrix.resize(Matrix<Frac>::width); - Matrix<Frac>::matrix.fill(QVector<Frac>(Matrix<Frac>::height, Frac(1, 1))); - for (unsigned i = 0; i < Matrix<Frac>::width; ++i) + for (unsigned j = 0; j < Matrix<Frac>::height; ++j) { - for (unsigned j = 0; j < Matrix<Frac>::height; ++j) - { - unsigned k; - unsigned l; - bool m; - ini.getValue(QString("Element-%1-%2-n").arg(i).arg(j), k, 0); - ini.getValue(QString("Element-%1-%2-d").arg(i).arg(j), l, 0); - ini.getValue(QString("Element-%1-%2-t").arg(i).arg(j), m, 0); - Matrix<Frac>::matrix[i][j].set(k, l, m); - } + unsigned k; + unsigned l; + bool m; + ini.getValue(QString("Element-%1-%2-n").arg(i).arg(j), k, 0); + ini.getValue(QString("Element-%1-%2-d").arg(i).arg(j), l, 0); + ini.getValue(QString("Element-%1-%2-t").arg(i).arg(j), m, 0); + Matrix<Frac>::matrix[i][j].set(k, l, m); } } - void save(const QString& fname) const + } + void save(const QString& fname) const + { + Ini ini; + ini.addField("height", Matrix<Frac>::height); + ini.addField("width", Matrix<Frac>::width); + for (unsigned i = 0; i < Matrix<Frac>::width; ++i) { - Ini ini; - ini.addField("height", Matrix<Frac>::height); - ini.addField("width", Matrix<Frac>::width); - for (unsigned i = 0; i < Matrix<Frac>::width; ++i) + for (unsigned j = 0; j < Matrix<Frac>::height; ++j) { - for (unsigned j = 0; j < Matrix<Frac>::height; ++j) - { - ini.addField(QString("Element-%1-%2-t").arg(i).arg(j), Matrix<Frac>::matrix[i][j].getType()); - ini.addField(QString("Element-%1-%2-n").arg(i).arg(j), Matrix<Frac>::matrix[i][j].getNum()); - ini.addField(QString("Element-%1-%2-d").arg(i).arg(j), Matrix<Frac>::matrix[i][j].getDenom()); - } + ini.addField(QString("Element-%1-%2-t").arg(i).arg(j), Matrix<Frac>::matrix[i][j].getType()); + ini.addField(QString("Element-%1-%2-n").arg(i).arg(j), Matrix<Frac>::matrix[i][j].getNum()); + ini.addField(QString("Element-%1-%2-d").arg(i).arg(j), Matrix<Frac>::matrix[i][j].getDenom()); } - ini.save(fname); } - }; -} + ini.save(fname); + } +}; #endif diff --git a/general/Hat.h b/general/Hat.h index a6e892e0..54169e1d 100644 --- a/general/Hat.h +++ b/general/Hat.h @@ -27,59 +27,56 @@ #include <cstdlib> #include <ctime> -namespace PokeGen +template<class T> class Hat { - template<class T> class Hat - { - public: - Hat() : - totalWeight(1) + public: + Hat() : + totalWeight(1) + { + std::srand(std::time(NULL)) + } + + T pick() const + { + int choice = std::rand() % totalWeight; + for (typename QMap<T, unsigned>::Iterator i = objects.begin(); i != objects.end(); ++i) { - std::srand(std::time(NULL)) + choice -= i.value(); + if (choice < 0) + return i.key(); } - - T pick() const - { - int choice = std::rand() % totalWeight; - for (typename QMap<T, unsigned>::Iterator i = objects.begin(); i != objects.end(); ++i) - { - choice -= i.value(); - if (choice < 0) - return i.key(); - } - } - T remove() - { - T chosen = pick(); - if (!(--objects[chosen])) - objects.erase(chosen); - --totalWeight; - return chosen; - } - - void setCount(const T& key, const unsigned wt) - { - if (objects.find(key) != objects.end()) - totalWeight -= objects[key]; - objects[key] = wt; - totalWeight += wt; - } - - unsigned getCount(const T& key) const - { - if (objects.find(key) != objects.end()) - return objects[key]; - throw("Hat: key does not exist"); - } - - unsigned operator[](const T& key) const - { - return getCount(key); - } - private: - QMap<T, unsigned> objects; - unsigned totalCount; - }; -} + } + T remove() + { + T chosen = pick(); + if (!(--objects[chosen])) + objects.erase(chosen); + --totalWeight; + return chosen; + } + + void setCount(const T& key, const unsigned wt) + { + if (objects.find(key) != objects.end()) + totalWeight -= objects[key]; + objects[key] = wt; + totalWeight += wt; + } + + unsigned getCount(const T& key) const + { + if (objects.find(key) != objects.end()) + return objects[key]; + throw("Hat: key does not exist"); + } + + unsigned operator[](const T& key) const + { + return getCount(key); + } + private: + QMap<T, unsigned> objects; + unsigned totalCount; +}; #endif diff --git a/general/ImageCache.cpp b/general/ImageCache.cpp index c02342ae..e5585651 100644 --- a/general/ImageCache.cpp +++ b/general/ImageCache.cpp @@ -22,7 +22,7 @@ #include "ImageCache.h" -QPixmap PokeGen::ImageCache::open(const QString& fname, const bool force) throw(OpenException) +QPixmap ImageCache::open(const QString& fname, const bool force) throw(OpenException) { QPixmap pm; if (force) diff --git a/general/ImageCache.h b/general/ImageCache.h index 097d099c..1b06576f 100644 --- a/general/ImageCache.h +++ b/general/ImageCache.h @@ -28,13 +28,10 @@ #include <QString> #include "Exception.h" -namespace PokeGen +class ImageCache { - class ImageCache - { - public: - static QPixmap open(const QString& fname, const bool force = false) throw(OpenException); - }; -} + public: + static QPixmap open(const QString& fname, const bool force = false) throw(OpenException); +}; #endif diff --git a/general/Ini.cpp b/general/Ini.cpp index df2b74a0..b829b115 100644 --- a/general/Ini.cpp +++ b/general/Ini.cpp @@ -22,12 +22,12 @@ #include "Ini.h" -PokeGen::Ini::Ini(const QString& fname) +Ini::Ini(const QString& fname) { load(fname); } -void PokeGen::Ini::load(const QString& fname) throw(Exception) +void Ini::load(const QString& fname) throw(Exception) { QFile fin(fname); if (!fin.exists()) @@ -36,7 +36,7 @@ void PokeGen::Ini::load(const QString& fname) throw(Exception) fin.close(); } -void PokeGen::Ini::load(QFile& fin) throw(InvalidException) +void Ini::load(QFile& fin) throw(InvalidException) { QTextStream file(&fin); QString line = file.readLine(); @@ -58,7 +58,7 @@ void PokeGen::Ini::load(QFile& fin) throw(InvalidException) } } -void PokeGen::Ini::save(const QString& fname) const throw(Exception) +void Ini::save(const QString& fname) const throw(Exception) { QStringList path = fname.split(QDir::separator(), QString::SkipEmptyParts); path.removeLast(); @@ -71,7 +71,7 @@ void PokeGen::Ini::save(const QString& fname) const throw(Exception) fout.close(); } -void PokeGen::Ini::save(QFile& file) const +void Ini::save(QFile& file) const { QTextStream fout(&file); for (QMapIterator<QString, QString> i(fields); i.hasNext(); i.next()) @@ -79,42 +79,42 @@ void PokeGen::Ini::save(QFile& file) const fout << '\n'; } -void PokeGen::Ini::addField(const QString& n, const bool v) +void Ini::addField(const QString& n, const bool v) { fields[n] = v ? "true" : "false"; } -void PokeGen::Ini::addField(const QString& n, const unsigned char v) +void Ini::addField(const QString& n, const unsigned char v) { fields[n] = QString("%1").arg(v); } -void PokeGen::Ini::addField(const QString& n, const int v) +void Ini::addField(const QString& n, const int v) { fields[n] = QString("%1").arg(v); } -void PokeGen::Ini::addField(const QString& n, const unsigned v) +void Ini::addField(const QString& n, const unsigned v) { fields[n] = QString("%1").arg(v); } -void PokeGen::Ini::addField(const QString& n, const float v) +void Ini::addField(const QString& n, const float v) { fields[n] = QString("%1").arg(v); } -void PokeGen::Ini::addField(const QString& n, const double v) +void Ini::addField(const QString& n, const double v) { fields[n] = QString("%1").arg(v); } -void PokeGen::Ini::addField(const QString& n, const QString& v) +void Ini::addField(const QString& n, const QString& v) { fields[n] = v; } -void PokeGen::Ini::getValue(const QString& field, bool& val, const bool def) +void Ini::getValue(const QString& field, bool& val, const bool def) { if (!fields.contains(field)) { @@ -129,7 +129,7 @@ void PokeGen::Ini::getValue(const QString& field, bool& val, const bool def) val = def; } -void PokeGen::Ini::getValue(const QString& field, unsigned char& val, const unsigned char def) +void Ini::getValue(const QString& field, unsigned char& val, const unsigned char def) { if (!fields.contains(field)) { @@ -145,7 +145,7 @@ void PokeGen::Ini::getValue(const QString& field, unsigned char& val, const unsi val = def; } -void PokeGen::Ini::getValue(const QString& field, int& val, const int def) +void Ini::getValue(const QString& field, int& val, const int def) { if (!fields.contains(field)) { @@ -158,7 +158,7 @@ void PokeGen::Ini::getValue(const QString& field, int& val, const int def) val = def; } -void PokeGen::Ini::getValue(const QString& field, unsigned& val, const unsigned def) +void Ini::getValue(const QString& field, unsigned& val, const unsigned def) { if (!fields.contains(field)) { @@ -171,7 +171,7 @@ void PokeGen::Ini::getValue(const QString& field, unsigned& val, const unsigned val = def; } -void PokeGen::Ini::getValue(const QString& field, float& val, const float def) +void Ini::getValue(const QString& field, float& val, const float def) { if (!fields.contains(field)) { @@ -184,7 +184,7 @@ void PokeGen::Ini::getValue(const QString& field, float& val, const float def) val = def; } -void PokeGen::Ini::getValue(const QString& field, double& val, const double def) +void Ini::getValue(const QString& field, double& val, const double def) { if (!fields.contains(field)) { @@ -197,7 +197,7 @@ void PokeGen::Ini::getValue(const QString& field, double& val, const double def) val = def; } -void PokeGen::Ini::getValue(const QString& field, QString& val, const QString& def) +void Ini::getValue(const QString& field, QString& val, const QString& def) { if (!fields.contains(field)) { diff --git a/general/Ini.h b/general/Ini.h index 6cb441c7..c56060a3 100644 --- a/general/Ini.h +++ b/general/Ini.h @@ -31,45 +31,42 @@ #include <QTextStream> #include "Exception.h" -namespace PokeGen +class Ini { - class Ini - { - public: - Ini(); - Ini(const QString& fname); - - void load(const QString& fname) throw(Exception); - void save(const QString& fname) const throw(Exception); - - void addField(const QString& n, const bool v); - void addField(const QString& n, const unsigned char v); - void addField(const QString& n, const int v); - void addField(const QString& n, const unsigned v); - void addField(const QString& n, const float v); - void addField(const QString& n, const double v); - void addField(const QString& n, const QString& v); - - QString getName() const; - void getValue(const QString& field, bool& val, const bool def = true); - void getValue(const QString& field, unsigned char& val, const unsigned char def = UCHAR_MAX); - void getValue(const QString& field, int& val, const int def = INT_MAX); - void getValue(const QString& field, unsigned& val, const unsigned def = UINT_MAX); - void getValue(const QString& field, float& val, const float def = 0); - void getValue(const QString& field, double& val, const double def = 0); - void getValue(const QString& field, QString& val, const QString& def = ""); - protected: - enum LinePos - { - Field = 0, - Value = 1 - }; + public: + Ini(); + Ini(const QString& fname); - void load(QFile& fin) throw(InvalidException); - void save(QFile& fout) const; - - QMap<QString, QString> fields; - }; -} + void load(const QString& fname) throw(Exception); + void save(const QString& fname) const throw(Exception); + + void addField(const QString& n, const bool v); + void addField(const QString& n, const unsigned char v); + void addField(const QString& n, const int v); + void addField(const QString& n, const unsigned v); + void addField(const QString& n, const float v); + void addField(const QString& n, const double v); + void addField(const QString& n, const QString& v); + + QString getName() const; + void getValue(const QString& field, bool& val, const bool def = true); + void getValue(const QString& field, unsigned char& val, const unsigned char def = UCHAR_MAX); + void getValue(const QString& field, int& val, const int def = INT_MAX); + void getValue(const QString& field, unsigned& val, const unsigned def = UINT_MAX); + void getValue(const QString& field, float& val, const float def = 0); + void getValue(const QString& field, double& val, const double def = 0); + void getValue(const QString& field, QString& val, const QString& def = ""); + protected: + enum LinePos + { + Field = 0, + Value = 1 + }; + + void load(QFile& fin) throw(InvalidException); + void save(QFile& fout) const; + + QMap<QString, QString> fields; +}; #endif diff --git a/general/Matrix.h b/general/Matrix.h index f4fd638e..9e89bae1 100644 --- a/general/Matrix.h +++ b/general/Matrix.h @@ -29,205 +29,202 @@ #include <QVectorIterator> #include "Ini.h" -namespace PokeGen +template<class T> class Matrix { - template<class T> class Matrix - { - public: - Matrix() : - width(0), - height(0) - { - } - Matrix(const unsigned w, const unsigned h, const T& d = T()) : - width(w), - height(h), - matrix(h, QVector<T>(w, d)) - { - } - Matrix(const Matrix<T>& rhs) : - width(rhs.getWidth()), - height(rhs.getHeight()), - matrix(height, QVector<T>(width, T())) - { - for (unsigned i = 0; i < height; ++i) - { - for (unsigned j = 0; j < width; ++j) - matrix[i][j] = rhs(i, j); - } - } - - void load(const QString& fname) - { - Ini ini(fname); - ini.getValue("width", width, 0); - ini.getValue("height", height, 0); - if (!(width && height)) - return; - matrix.resize(width, QVector<T>(height, 0)); - for (unsigned i = 0; i < width; ++i) + public: + Matrix() : + width(0), + height(0) + { + } + Matrix(const unsigned w, const unsigned h, const T& d = T()) : + width(w), + height(h), + matrix(h, QVector<T>(w, d)) + { + } + Matrix(const Matrix<T>& rhs) : + width(rhs.getWidth()), + height(rhs.getHeight()), + matrix(height, QVector<T>(width, T())) + { + for (unsigned i = 0; i < height; ++i) + { + for (unsigned j = 0; j < width; ++j) + matrix[i][j] = rhs(i, j); + } + } + + void load(const QString& fname) + { + Ini ini(fname); + ini.getValue("width", width, 0); + ini.getValue("height", height, 0); + if (!(width && height)) + return; + matrix.resize(width, QVector<T>(height, 0)); + for (unsigned i = 0; i < width; ++i) + { + for (unsigned j = 0; j < height; ++j) { - for (unsigned j = 0; j < height; ++j) - { - int k; - ini.getValue(QString("Element-%1-%2").arg(i).arg(j), k, 0); - matrix[i][j] = k; - } + int k; + ini.getValue(QString("Element-%1-%2").arg(i).arg(j), k, 0); + matrix[i][j] = k; } } - void save(const QString& fname, const QString& val) const - { - Ini ini; - ini.addField("height", height); - ini.addField("width", width); - for (int i = 0; i < width; ++i) - { - for (unsigned j = 0; j < height; ++j) - ini.addField(QString("Element-%1-%2").arg(i).arg(j), matrix[i][j]); - } - ini.save(fname); - } - - void addRow(const T& d = T()) - { - matrix.append(QVector<T>(width, d)); - ++height; - } - void addCol(const T& d = T()) - { - for (QMutableVectorIterator< QVector<T> > i(matrix); i.hasNext(); ) - i.next().append(d); - ++width; - } - void insertRow(const unsigned pos, const T& d = T()) - { - if (height < pos) - throw("Matrix: dimension out-of-bounds"); - matrix.insert(pos, QVector<T>(width, d)); - ++height; - } - void insertCol(const unsigned pos, const T& d = T()) - { - if (width < pos) - throw("Matrix: dimension out-of-bounds"); - for (QMutableVectorIterator< QVector<T> > i(matrix); i.hasNext(); ) - i.next().insert(pos, d); - ++width; - } - void deleteRow(const unsigned pos) - { - if (height <= pos) - throw("Matrix: dimension out-of-bounds"); - matrix.remove(pos); - --height; - } - void deleteCol(const unsigned pos) - { - if (width <= pos) - throw("Matrix: dimension out-of-bounds"); - for (QMutableVectorIterator< QVector<T> > i(matrix); i.hasNext(); ) - i.next().remove(pos); - --width; - } - void clear() - { - matrix.clear(); - } - - void set(const unsigned row, const unsigned col, const T& s) - { - if ((height <= row) || (width <= col)) - throw("Matrix: dimension out-of-bounds"); - matrix(row, col) = s; - return true; - } - void resize(const unsigned h, const unsigned w, const T& d = T()) - { - clear(); - width = w; - height = h; - matrix = QVector< QVector<T> >(h, QVector<T>(w, d)); - } - - const T& at(const unsigned row, const unsigned col) const - { - if ((height <= row) || (width <= col)) - throw("Matrix: dimension out-of-bounds"); - return matrix.at(row).at(col); - } - const QVector<T> getRow(const unsigned row) const - { - if (height <= row) - throw("Matrix: dimension out-of-bounds"); - return matrix.at(row); - } - const QVector<T> getCol(const unsigned col) const - { - if (width <= col) - throw("Matrix: dimension out-of-bounds"); - QVector<T> c; - for (QVectorIterator< QVector<T> > i(matrix); i.hasNext(); ) - c.append(i.next().at(col)); - return c; - } - unsigned getHeight() const - { - return height; - } - unsigned getWidth() const - { - return width; - } - - T& operator()(const unsigned row, const unsigned col) - { - if ((height <= row) || (width <= col)) - throw("Matrix: dimension out-of-bounds"); - return (matrix[row])[col]; - } - const T& operator()(const unsigned row, const unsigned col) const - { - return at(row, col); - } - Matrix<T> operator=(const Matrix<T>& rhs) - { - if (this == &rhs) - return *this; - height = rhs.getHeight(); - width = rhs.getWidth(); - resize(height, width); - for (unsigned i = 0; i < height; ++i) - { - for (unsigned j = 0; j < width; ++j) - matrix[i][j] = rhs(i, j); - } + } + void save(const QString& fname, const QString& val) const + { + Ini ini; + ini.addField("height", height); + ini.addField("width", width); + for (int i = 0; i < width; ++i) + { + for (unsigned j = 0; j < height; ++j) + ini.addField(QString("Element-%1-%2").arg(i).arg(j), matrix[i][j]); + } + ini.save(fname); + } + + void addRow(const T& d = T()) + { + matrix.append(QVector<T>(width, d)); + ++height; + } + void addCol(const T& d = T()) + { + for (QMutableVectorIterator< QVector<T> > i(matrix); i.hasNext(); ) + i.next().append(d); + ++width; + } + void insertRow(const unsigned pos, const T& d = T()) + { + if (height < pos) + throw("Matrix: dimension out-of-bounds"); + matrix.insert(pos, QVector<T>(width, d)); + ++height; + } + void insertCol(const unsigned pos, const T& d = T()) + { + if (width < pos) + throw("Matrix: dimension out-of-bounds"); + for (QMutableVectorIterator< QVector<T> > i(matrix); i.hasNext(); ) + i.next().insert(pos, d); + ++width; + } + void deleteRow(const unsigned pos) + { + if (height <= pos) + throw("Matrix: dimension out-of-bounds"); + matrix.remove(pos); + --height; + } + void deleteCol(const unsigned pos) + { + if (width <= pos) + throw("Matrix: dimension out-of-bounds"); + for (QMutableVectorIterator< QVector<T> > i(matrix); i.hasNext(); ) + i.next().remove(pos); + --width; + } + void clear() + { + matrix.clear(); + } + + void set(const unsigned row, const unsigned col, const T& s) + { + if ((height <= row) || (width <= col)) + throw("Matrix: dimension out-of-bounds"); + matrix(row, col) = s; + return true; + } + void resize(const unsigned h, const unsigned w, const T& d = T()) + { + clear(); + width = w; + height = h; + matrix = QVector< QVector<T> >(h, QVector<T>(w, d)); + } + + const T& at(const unsigned row, const unsigned col) const + { + if ((height <= row) || (width <= col)) + throw("Matrix: dimension out-of-bounds"); + return matrix.at(row).at(col); + } + const QVector<T> getRow(const unsigned row) const + { + if (height <= row) + throw("Matrix: dimension out-of-bounds"); + return matrix.at(row); + } + const QVector<T> getCol(const unsigned col) const + { + if (width <= col) + throw("Matrix: dimension out-of-bounds"); + QVector<T> c; + for (QVectorIterator< QVector<T> > i(matrix); i.hasNext(); ) + c.append(i.next().at(col)); + return c; + } + unsigned getHeight() const + { + return height; + } + unsigned getWidth() const + { + return width; + } + + T& operator()(const unsigned row, const unsigned col) + { + if ((height <= row) || (width <= col)) + throw("Matrix: dimension out-of-bounds"); + return (matrix[row])[col]; + } + const T& operator()(const unsigned row, const unsigned col) const + { + return at(row, col); + } + Matrix<T> operator=(const Matrix<T>& rhs) + { + if (this == &rhs) return *this; - } - bool operator==(const Matrix<T>& rhs) + height = rhs.getHeight(); + width = rhs.getWidth(); + resize(height, width); + for (unsigned i = 0; i < height; ++i) + { + for (unsigned j = 0; j < width; ++j) + matrix[i][j] = rhs(i, j); + } + return *this; + } + bool operator==(const Matrix<T>& rhs) + { + if (this == &rhs) + return true; + if ((height != rhs.getHeight()) || (width == rhs.getWidth())) + return false; + for (unsigned i = 0; i < height; ++i) { - if (this == &rhs) - return true; - if ((height != rhs.getHeight()) || (width == rhs.getWidth())) - return false; - for (unsigned i = 0; i < height; ++i) + for (unsigned j = 0; j < width; ++j) { - for (unsigned j = 0; j < width; ++j) - { - if (matrix[i][j] != rhs(i, j)) - return false; - } + if (matrix[i][j] != rhs(i, j)) + return false; } - return true; - } - bool operator!=(const Matrix<T>& rhs) - { - return !(*this == rhs); } - protected: - unsigned width; - unsigned height; - QVector< QVector<T> > matrix; - }; -} + return true; + } + bool operator!=(const Matrix<T>& rhs) + { + return !(*this == rhs); + } + protected: + unsigned width; + unsigned height; + QVector< QVector<T> > matrix; +}; #endif diff --git a/general/Point.h b/general/Point.h index 9143b491..2f0d75c2 100644 --- a/general/Point.h +++ b/general/Point.h @@ -25,43 +25,40 @@ #include <QFile> -namespace PokeGen +class Point { - class Point - { - public: - Point(const unsigned _x = 0, const unsigned _y = 0) : - x(_x), - y(_y) - { - } - - void set(const unsigned _x, const unsigned _y) - { - x = _x; - y = _y; - } - void setX(const unsigned _x) - { - x = _x; - } - void setY(const unsigned _y) - { - y = _y; - } - - unsigned getX() const - { - return x; - } - unsigned getY() const - { - return y; - } - private: - unsigned x; - unsigned y; - }; -} + public: + Point(const unsigned _x = 0, const unsigned _y = 0) : + x(_x), + y(_y) + { + } + + void set(const unsigned _x, const unsigned _y) + { + x = _x; + y = _y; + } + void setX(const unsigned _x) + { + x = _x; + } + void setY(const unsigned _y) + { + y = _y; + } + + unsigned getX() const + { + return x; + } + unsigned getY() const + { + return y; + } + private: + unsigned x; + unsigned y; +}; #endif diff --git a/general/Ref.cpp b/general/Ref.cpp index e8ca482b..cad1b624 100644 --- a/general/Ref.cpp +++ b/general/Ref.cpp @@ -22,7 +22,7 @@ #include "Ref.h" -unsigned PokeGen::findIn(const unsigned end, const QString& str, const char* array[]) +unsigned findIn(const unsigned end, const QString& str, const char* array[]) { unsigned i = 0; for (; i < end; ++i) @@ -33,15 +33,15 @@ unsigned PokeGen::findIn(const unsigned end, const QString& str, const char* arr return i; } -const char* PokeGen::StatRBYStr[PokeGen::ST_End_Battle] = {"HP", "Attack", "Defense", "Speed", "Special", "Special", "Accuracy", "Evasion"}; -const char* PokeGen::StatGSCStr[PokeGen::ST_End_Battle] = {"HP", "Attack", "Defense", "Speed", "Special Attack", "Special Defense", "Accuracy", "Evasion"}; +const char* StatRBYStr[ST_End_Battle] = {"HP", "Attack", "Defense", "Speed", "Special", "Special", "Accuracy", "Evasion"}; +const char* StatGSCStr[ST_End_Battle] = {"HP", "Attack", "Defense", "Speed", "Special Attack", "Special Defense", "Accuracy", "Evasion"}; -const char* PokeGen::BattleMemberStr[PokeGen::BM_End] = {"Player", "Enemy"}; +const char* BattleMemberStr[BM_End] = {"Player", "Enemy"}; -const char* PokeGen::WeatherStr[PokeGen::W_End_All] = {"Ice", "Rain", "Sun", "Sand", "All"}; +const char* WeatherStr[W_End_All] = {"Ice", "Rain", "Sun", "Sand", "All"}; -const char* PokeGen::DirectionStr[PokeGen::D_End_None] = {"Up", "Down", "Left", "Right", "None"}; +const char* DirectionStr[D_End_None] = {"Up", "Down", "Left", "Right", "None"}; -const char* PokeGen::RelativeStr[PokeGen::REL_End] = {"Less", "Less or Equal", "Equal", "Greater or Equal", "Greater", "Not Equal"}; +const char* RelativeStr[REL_End] = {"Less", "Less or Equal", "Equal", "Greater or Equal", "Greater", "Not Equal"}; -const char* PokeGen::StatusStr[PokeGen::STS_End] = {"Freeze", "Paralyze", "Sleep", "Poison", "Toxic Poison", "Burn", "Any"}; +const char* StatusStr[STS_End] = {"Freeze", "Paralyze", "Sleep", "Poison", "Toxic Poison", "Burn", "Any"}; diff --git a/general/Ref.h b/general/Ref.h index 12508aed..89875f4f 100644 --- a/general/Ref.h +++ b/general/Ref.h @@ -25,84 +25,81 @@ #include <QString> -namespace PokeGen +unsigned findIn(const unsigned end, const QString& str, const char* array[]); + +enum EnumStat +{ + ST_HP = 0, + ST_Attack = 1, + ST_Defense = 2, + ST_Speed = 3, + ST_Special = 4, + ST_End_RBY = 5, + ST_SpecialAttack = 4, + ST_SpecialDefense = 5, + ST_End_GSC = 6, + ST_Accuracy = 6, + ST_Evasion = 7, + ST_End_Battle = 8, +}; +extern const char* StatRBYStr[ST_End_Battle]; +extern const char* StatGSCStr[ST_End_Battle]; + +enum EnumBattleMember +{ + BM_Player = 0, + BM_Enemy = 1, + BM_End = 2 +}; +extern const char* BattleMemberStr[BM_End]; + +enum EnumWeather +{ + W_Ice = 0, + W_Rain = 1, + W_Sun = 2, + W_Sand = 3, + W_End_Real = 4, + W_All = 4, + W_End_All = 5 +}; +extern const char* WeatherStr[W_End_All]; + +enum EnumDirection +{ + D_Up = 0, + D_Down = 1, + D_Left = 2, + D_Right = 3, + D_End = 4, + D_None = 4, + D_End_None = 5 +}; +extern const char* DirectionStr[D_End_None]; + +enum EnumRelative +{ + REL_Less = 0, + REL_LessOrEqual = 1, + REL_Equal = 2, + REL_GreaterOrEqual = 3, + REL_Greater = 4, + REL_NotEqual = 5, + REL_End = 6 +}; +extern const char* RelativeStr[REL_End]; + +enum EnumStatus { - unsigned findIn(const unsigned end, const QString& str, const char* array[]); - - enum EnumStat - { - ST_HP = 0, - ST_Attack = 1, - ST_Defense = 2, - ST_Speed = 3, - ST_Special = 4, - ST_End_RBY = 5, - ST_SpecialAttack = 4, - ST_SpecialDefense = 5, - ST_End_GSC = 6, - ST_Accuracy = 6, - ST_Evasion = 7, - ST_End_Battle = 8, - }; - extern const char* StatRBYStr[ST_End_Battle]; - extern const char* StatGSCStr[ST_End_Battle]; - - enum EnumBattleMember - { - BM_Player = 0, - BM_Enemy = 1, - BM_End = 2 - }; - extern const char* BattleMemberStr[BM_End]; - - enum EnumWeather - { - W_Ice = 0, - W_Rain = 1, - W_Sun = 2, - W_Sand = 3, - W_End_Real = 4, - W_All = 4, - W_End_All = 5 - }; - extern const char* WeatherStr[W_End_All]; - - enum EnumDirection - { - D_Up = 0, - D_Down = 1, - D_Left = 2, - D_Right = 3, - D_End = 4, - D_None = 4, - D_End_None = 5 - }; - extern const char* DirectionStr[D_End_None]; - - enum EnumRelative - { - REL_Less = 0, - REL_LessOrEqual = 1, - REL_Equal = 2, - REL_GreaterOrEqual = 3, - REL_Greater = 4, - REL_NotEqual = 5, - REL_End = 6 - }; - extern const char* RelativeStr[REL_End]; - - enum EnumStatus - { - STS_Freeze = 0, - STS_Paralyze = 1, - STS_Sleep = 2, - STS_Poison = 3, - STS_ToxicPoison = 4, - STS_Burn = 5, - STS_Any = 6, - STS_End = 7 - }; - extern const char* StatusStr[STS_End]; -} + STS_Freeze = 0, + STS_Paralyze = 1, + STS_Sleep = 2, + STS_Poison = 3, + STS_ToxicPoison = 4, + STS_Burn = 5, + STS_Any = 6, + STS_End = 7 +}; +extern const char* StatusStr[STS_End]; #endif diff --git a/general/general.pro b/general/general.pro index 3fe8b64a..e174ccec 100644 --- a/general/general.pro +++ b/general/general.pro @@ -1,6 +1,5 @@ OBJECTS_DIR = ../../obj/pokemod DESTDIR = ../../lib -VERSION = 1.0.0 TEMPLATE = lib CONFIG += qt warn_on dll |
