1 /*
2 PLIB - A Suite of Portable Game Libraries
3 Copyright (C) 1998,2002 Steve Baker
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 For further information visit http://plib.sourceforge.net
20
21 $Id: ssgList.cxx 1949 2004-09-13 11:16:43Z wolfram_kuss $
22 */
23
24
25 #include "ssgLocal.h"
26
ssgList(int init)27 ssgList::ssgList ( int init )
28 {
29 total = 0 ;
30 next = 0 ;
31 entity_list = new ssgEntity * [ limit = (init <= 0) ? 1 : init ] ;
32 }
33
34
ssgKidList(int init)35 ssgKidList::ssgKidList ( int init ) : ssgList ( init )
36 {
37 }
38
39
~ssgList(void)40 ssgList::~ssgList (void)
41 {
42 removeAllEntities () ;
43 delete [] entity_list ;
44 }
45
46
~ssgKidList(void)47 ssgKidList::~ssgKidList (void)
48 {
49 }
50
51
addEntity(ssgEntity * entity)52 void ssgList::addEntity ( ssgEntity *entity )
53 {
54 entity->deadBeefCheck() ;
55 sizeChk () ;
56 entity_list [ total++ ] = entity ;
57 }
58
addEntity(ssgEntity * entity)59 void ssgKidList::addEntity ( ssgEntity *entity )
60 {
61 entity->deadBeefCheck() ;
62 entity -> ref () ;
63 ssgList::addEntity ( entity ) ;
64 }
65
sizeChk(void)66 void ssgList::sizeChk (void)
67 {
68 /* Room for one more Entity? */
69
70 if ( total >= limit )
71 {
72 limit += limit ;
73 ssgEntity **nlist = new ssgEntity * [ limit ] ;
74 memmove ( nlist, entity_list, sizeof(ssgEntity *) * total ) ;
75 delete [] entity_list ;
76 entity_list = nlist ;
77 }
78 }
79
80
searchForEntity(ssgEntity * entity)81 int ssgList::searchForEntity ( ssgEntity *entity )
82 {
83 for ( unsigned int i = 0 ; i < total ; i++ )
84 if ( entity_list [ i ] == entity )
85 return (int) i ;
86
87 return -1 ;
88 }
89
90
removeAllEntities()91 void ssgList::removeAllEntities ()
92 {
93 while ( total > 0 )
94 removeEntity ( (unsigned int) total-1 ) ;
95 }
96
removeEntity(unsigned int n)97 void ssgList::removeEntity ( unsigned int n )
98 {
99 memmove ( &(entity_list[n]), &(entity_list[n+1]), sizeof(ssgEntity *) * (total-n-1) ) ;
100 total-- ;
101
102 if ( next >= n )
103 next-- ;
104 }
105
106
removeEntity(unsigned int n)107 void ssgKidList::removeEntity ( unsigned int n )
108 {
109 entity_list[n]->deadBeefCheck();
110 ssgEntity *e = entity_list [ n ] ;
111
112 ssgList::removeEntity ( n ) ;
113
114 e -> deadBeefCheck () ;
115 // wk 20.4.2002 problems with mywalk... when merging hiearchy nodes:
116 ssgDeRefDelete ( e ) ;
117 }
118
119
replaceEntity(unsigned int n,ssgEntity * new_entity)120 void ssgList::replaceEntity ( unsigned int n, ssgEntity *new_entity )
121 {
122 new_entity -> deadBeefCheck () ;
123 entity_list [ n ] -> deadBeefCheck () ;
124 entity_list [ n ] = new_entity;
125 }
126
replaceEntity(unsigned int n,ssgEntity * new_entity)127 void ssgKidList::replaceEntity ( unsigned int n, ssgEntity *new_entity )
128 {
129 ssgEntity *old_entity = entity_list [ n ] ;
130 new_entity -> ref () ;
131 ssgList::replaceEntity ( n, new_entity ) ;
132 ssgDeRefDelete ( old_entity ) ;
133 }
134