Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Toggle main menu visibility
Loading...
Searching...
No Matches
sprites.cpp
Go to the documentation of this file.
1
/*
2
* Copyright 2019 Twilight
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
21
22
#include <QJsonArray>
23
#include <QQmlEngine>
24
#include <
pse-common/utility.h
>
25
26
#include "
./sprites.h
"
27
#include "
./util/gamedata.h
"
28
29
SpriteDBEntry::SpriteDBEntry
() {}
30
SpriteDBEntry::SpriteDBEntry
(QJsonValue& data)
31
{
32
name
= data[
"name"
].toString();
33
ind
=
static_cast<
var8
>
(data[
"ind"
].toDouble());
34
}
35
36
SpritesDB*
SpritesDB::inst
()
37
{
38
static
SpritesDB* _inst =
new
SpritesDB;
39
return
_inst;
40
}
41
42
const
QVector<SpriteDBEntry*>
SpritesDB::getStore
()
const
{
return
store; }
43
const
QHash<QString, SpriteDBEntry*>
SpritesDB::getInd
()
const
{
return
ind; }
44
int
SpritesDB::getStoreSize
()
const
{
return
store.size(); }
45
46
SpriteDBEntry
*
SpritesDB::getStoreAt
(
int
idx)
const
47
{
48
if
(idx < 0 || idx >= store.size())
return
nullptr
;
49
return
store.at(idx);
50
}
51
52
SpriteDBEntry
*
SpritesDB::getIndAt
(
const
QString& key)
const
53
{
54
return
ind.value(key,
nullptr
);
55
}
56
57
void
SpritesDB::load
()
58
{
59
static
bool
once =
false
;
60
if
(once)
return
;
61
auto
jsonData =
GameData::inst
()->
json
(
"sprites"
);
62
for
(QJsonValue entry : jsonData.array())
63
store.append(
new
SpriteDBEntry
(entry));
64
once =
true
;
65
}
66
67
void
SpritesDB::index
()
68
{
69
static
bool
once =
false
;
70
if
(once)
return
;
71
for
(
auto
* entry : store) {
72
ind.insert(entry->name, entry);
73
ind.insert(QString::number(entry->ind), entry);
74
}
75
once =
true
;
76
}
77
78
void
SpritesDB::qmlProtect
(
const
QQmlEngine*
const
engine)
const
79
{
80
Utility::qmlProtectUtil
(
this
, engine);
81
}
82
83
void
SpritesDB::qmlRegister()
const
84
{
85
static
bool
once =
false
;
86
if
(once)
return
;
87
qmlRegisterUncreatableType<SpritesDB>(
"PSE.DB.SpritesDB"
, 1, 0,
"SpritesDB"
,
"Can't instantiate in QML"
);
88
once =
true
;
89
}
90
91
SpritesDB::SpritesDB()
92
{
93
qmlRegister();
94
}
GameData::json
const QJsonDocument json(const QString filename) const
Parsed document for filename.
Definition
gamedata.cpp:45
GameData::inst
static GameData * inst()
The process-wide GameData singleton.
Definition
gamedata.cpp:71
SpritesDB::inst
static SpritesDB * inst()
< Number of sprites.
Definition
sprites.cpp:36
SpritesDB::getIndAt
SpriteDBEntry * getIndAt(const QString &key) const
Sprite by name key (for QML).
Definition
sprites.cpp:52
SpritesDB::qmlProtect
void qmlProtect(const QQmlEngine *const engine) const
Pin to C++ ownership.
Definition
sprites.cpp:78
SpritesDB::getInd
const QHash< QString, SpriteDBEntry * > getInd() const
Name->entry index.
Definition
sprites.cpp:43
SpritesDB::load
void load()
Load sprites from JSON.
Definition
sprites.cpp:57
SpritesDB::getStore
const QVector< SpriteDBEntry * > getStore() const
All sprites.
Definition
sprites.cpp:42
SpritesDB::index
void index()
Build the name->entry index.
Definition
sprites.cpp:67
SpritesDB::getStoreSize
int getStoreSize() const
Sprite count.
Definition
sprites.cpp:44
SpritesDB::getStoreAt
SpriteDBEntry * getStoreAt(int idx) const
Sprite by store index (for QML).
Definition
sprites.cpp:46
Utility::qmlProtectUtil
static void qmlProtectUtil(const QObject *const obj, const QQmlEngine *const engine)
Pin obj to C++ ownership so the QML engine never garbage-collects it.
Definition
utility.cpp:63
var8
var8e var8
Everyday 8-bit alias. Exact (not "fastest") to dodge the pointer-width bug noted above.
Definition
types.h:124
gamedata.h
sprites.h
SpriteDBEntry
One sprite definition: its name/picture-id and the maps that use it.
Definition
sprites.h:38
SpriteDBEntry::ind
var8 ind
Picture id.
Definition
sprites.h:43
SpriteDBEntry::name
QString name
Sprite name (key).
Definition
sprites.h:42
SpriteDBEntry::SpriteDBEntry
SpriteDBEntry()
Empty entry.
Definition
sprites.cpp:29
utility.h
projects
db
src
pse-db
sprites.cpp
Generated by
1.17.0