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: ssgSimpleList.cxx 2057 2005-12-06 14:25:18Z bram $
22 */
23 
24 
25 #include "ssgLocal.h"
26 
copy_from(ssgSimpleList * src,int clone_flags)27 void ssgSimpleList::copy_from ( ssgSimpleList *src, int clone_flags )
28 {
29   ssgBase::copy_from ( src, clone_flags ) ;
30 
31   if ( own_mem )
32     delete [] list ;
33   size_of = src -> getSizeOf () ;
34   total   = src -> getNum () ;
35   limit   = total ;
36   list    = new char [ limit * size_of ] ;
37   own_mem = true ;
38   memcpy ( list, src->raw_get ( 0 ), limit * size_of ) ;
39 }
40 
clone(int clone_flags)41 ssgBase *ssgSimpleList::clone ( int clone_flags )
42 {
43   ssgSimpleList *b = new ssgSimpleList () ;
44   b -> copy_from ( this, clone_flags ) ;
45   return b ;
46 }
47 
clone(int clone_flags)48 ssgBase *ssgNormalArray::clone ( int clone_flags )
49 {
50   ssgNormalArray *b = new ssgNormalArray () ;
51   b -> copy_from ( this, clone_flags ) ;
52   return b ;
53 }
54 
clone(int clone_flags)55 ssgBase *ssgVertexArray::clone ( int clone_flags )
56 {
57   ssgVertexArray *b = new ssgVertexArray () ;
58   b -> copy_from ( this, clone_flags ) ;
59   return b ;
60 }
61 
clone(int clone_flags)62 ssgBase *ssgTexCoordArray::clone ( int clone_flags )
63 {
64   ssgTexCoordArray *b = new ssgTexCoordArray () ;
65   b -> copy_from ( this, clone_flags ) ;
66   return b ;
67 }
68 
clone(int clone_flags)69 ssgBase *ssgColourArray::clone ( int clone_flags )
70 {
71   ssgColourArray *b = new ssgColourArray () ;
72   b -> copy_from ( this, clone_flags ) ;
73   return b ;
74 }
75 
clone(int clone_flags)76 ssgBase *ssgIndexArray::clone ( int clone_flags )
77 {
78   ssgIndexArray *b = new ssgIndexArray () ;
79   b -> copy_from ( this, clone_flags ) ;
80   return b ;
81 }
82 
83 
84 
85 
print(FILE * fd,char * indent,int how_much)86 void ssgVertexArray::print ( FILE *fd, char *indent, int how_much )
87 {
88   ssgSimpleList::print ( fd, indent, how_much ) ;
89 
90   if ( how_much < 4 )
91     return;
92 
93   for ( unsigned int i = 0 ; i < total ; i++ )
94     fprintf ( fd, "%s  V%d) { %f, %f, %f }\n", indent, i,
95                      get(i)[0], get(i)[1], get(i)[2] ) ;
96 }
97 
98 
99 
100 
print(FILE * fd,char * indent,int how_much)101 void ssgNormalArray::print ( FILE *fd, char *indent, int how_much )
102 {
103   ssgSimpleList::print ( fd, indent, how_much ) ;
104 
105   if ( how_much < 4 )
106     return;
107 
108   for ( unsigned int i = 0 ; i < total ; i++ )
109     fprintf ( fd, "%s  N%d) { %f, %f, %f }\n", indent, i,
110                      get(i)[0], get(i)[1], get(i)[2] ) ;
111 }
112 
113 
114 
print(FILE * fd,char * indent,int how_much)115 void ssgIndexArray::print ( FILE *fd, char *indent, int how_much )
116 {
117   ssgSimpleList::print ( fd, indent, how_much ) ;
118 
119   if ( how_much < 4 )
120     return;
121 
122   for ( unsigned int i = 0 ; i < total ; i++ )
123     fprintf ( fd, "%s  I%d) { %d }\n", indent, i,
124                      (int) (*get(i)) ) ;
125 }
126 
print(FILE * fd,char * indent,int how_much)127 void ssgTexCoordArray::print ( FILE *fd, char *indent, int how_much )
128 {
129   ssgSimpleList::print ( fd, indent, how_much ) ;
130 
131   if ( how_much < 4 )
132     return;
133 
134   for ( unsigned int i = 0 ; i < total ; i++ )
135     fprintf ( fd, "%s  T%d) { S=%f, T=%f }\n", indent, i,
136                      get(i)[0], get(i)[1] ) ;
137 }
138 
139 
140 
141 
print(FILE * fd,char * indent,int how_much)142 void ssgColourArray::print ( FILE *fd, char *indent, int how_much )
143 {
144   ssgSimpleList::print ( fd, indent, how_much ) ;
145 
146   if ( how_much < 4 )
147     return;
148 
149   for ( unsigned int i = 0 ; i < total ; i++ )
150     fprintf ( fd, "%s  C%d) { R=%f, G=%f, B=%f, A=%f }\n", indent, i,
151                      get(i)[0], get(i)[1], get(i)[2], get(i)[3] ) ;
152 }
153 
154 
155 
print(FILE * fd,char * indent,int how_much)156 void ssgSimpleList::print ( FILE *fd, char *indent, int how_much )
157 {
158   ssgBase::print ( fd, indent, how_much ) ;
159 
160   fprintf ( fd, "%s  Total # items = %d\n", indent, total ) ;
161 
162   if ( how_much < 3 )
163     return;
164 
165   fprintf ( fd, "%s  Size of items = %d bytes\n", indent, size_of ) ;
166 }
167 
168 
169 
load(FILE * fd)170 int ssgSimpleList::load ( FILE *fd )
171 {
172   delete [] list ;
173   _ssgReadUInt ( fd, &size_of ) ;
174   _ssgReadUInt ( fd, &total   ) ;
175   limit = total ;
176   list = new char [ limit * size_of ] ;
177   assert(list!=NULL);
178   // wk: The old code:
179   //_ssgReadFloat ( fd, limit * size_of / sizeof(float), (float *)list ) ;
180   // doesn't work since some ssgSimpleLists consist of shorts, so
181   // limit * size_of may not be divisible by sizeof(float).
182   // The new code works, but I am not 100% sure what we want in
183   // the event that there are machines with another sizeof(float).
184   _ssgReadBytes   ( fd, limit * size_of , list) ;
185 
186   return ! _ssgReadError () ;
187 }
188 
189 
190 
save(FILE * fd)191 int ssgSimpleList::save ( FILE *fd )
192 {
193   _ssgWriteUInt ( fd, size_of ) ;
194   _ssgWriteUInt ( fd, total   ) ;
195   // see comment in ssgSimpleList::load
196   // _ssgWriteFloat( fd, total * size_of / sizeof(float), (float *)list ) ;
197   _ssgWriteBytes ( fd, total * size_of, list);
198   return ! _ssgWriteError () ;
199 }
200 
201 
202