| 1 |
/* |
| 2 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
| 3 |
* |
| 4 |
* Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team |
| 5 |
* Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
| 6 |
* |
| 7 |
* Deliantra is free software: you can redistribute it and/or modify it under |
| 8 |
* the terms of the Affero GNU General Public License as published by the |
| 9 |
* Free Software Foundation, either version 3 of the License, or (at your |
| 10 |
* option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the Affero GNU General Public License |
| 18 |
* and the GNU General Public License along with this program. If not, see |
| 19 |
* <http://www.gnu.org/licenses/>. |
| 20 |
* |
| 21 |
* The authors can be reached via e-mail to <support@deliantra.net> |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <global.h> |
| 25 |
#include <stdio.h> |
| 26 |
|
| 27 |
#include "face.h" |
| 28 |
#include "crc.h" |
| 29 |
|
| 30 |
faceidx blank_face, empty_face, magicmouth_face; |
| 31 |
|
| 32 |
facehash_t facehash; |
| 33 |
std::vector<faceinfo> faces; |
| 34 |
|
| 35 |
static std::vector<faceidx> faces_freelist; |
| 36 |
|
| 37 |
faceidx face_alloc () |
| 38 |
{ |
| 39 |
faceidx idx; |
| 40 |
|
| 41 |
if (!faces_freelist.empty ()) |
| 42 |
{ |
| 43 |
idx = faces_freelist.back (); |
| 44 |
faces_freelist.pop_back (); |
| 45 |
} |
| 46 |
else |
| 47 |
{ |
| 48 |
idx = faces.size (); |
| 49 |
|
| 50 |
if (!idx) // skip index 0 |
| 51 |
idx = 1; |
| 52 |
|
| 53 |
faces.resize (idx + 1); |
| 54 |
} |
| 55 |
|
| 56 |
return idx; |
| 57 |
} |
| 58 |
|
| 59 |
void |
| 60 |
faceinfo::unref () |
| 61 |
{ |
| 62 |
if (--refcnt) |
| 63 |
return; |
| 64 |
|
| 65 |
refcnt = 1; |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
faceidx |
| 70 |
face_find (const char *name, faceidx defidx) |
| 71 |
{ |
| 72 |
if (!name) |
| 73 |
return defidx; |
| 74 |
|
| 75 |
facehash_t::iterator i = facehash.find (name); |
| 76 |
|
| 77 |
return i == facehash.end () |
| 78 |
? defidx : i->second; |
| 79 |
} |
| 80 |
|
| 81 |
faceinfo * |
| 82 |
face_info (faceidx idx) |
| 83 |
{ |
| 84 |
assert (0 < (faceidx)-1); // faceidx must be unsigned |
| 85 |
|
| 86 |
if (idx >= faces.size ()) |
| 87 |
return 0; |
| 88 |
|
| 89 |
return &faces [idx]; |
| 90 |
} |
| 91 |
|
| 92 |
facedata * |
| 93 |
faceinfo::data (int faceset) const |
| 94 |
{ |
| 95 |
if (!face [faceset].chksum_len) |
| 96 |
faceset = 0; |
| 97 |
|
| 98 |
return (facedata *)(face + faceset); |
| 99 |
} |
| 100 |
|
| 101 |
facedata * |
| 102 |
face_data (faceidx idx, int faceset) |
| 103 |
{ |
| 104 |
if (faceinfo *f = face_info (idx)) |
| 105 |
return f->data (faceset); |
| 106 |
|
| 107 |
return 0; |
| 108 |
} |
| 109 |
|