1 /*
2     Ming, an SWF output library
3     Copyright (C) 2002  Opaque Industries - http://www.opaque.net/
4 
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 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     Lesser General Public License for more details.
14 
15     You should have received a copy of the GNU Lesser 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 
20 #include <stdlib.h>
21 
22 #include "libming.h"
23 #include "blocklist.h"
24 #include "blocks/block.h"
25 #include "blocks/character.h"
26 
27 
28 /*
29 	we note which blocks are characters here because characters are allocated
30 	and destroyed separately from the movie- so they might not be valid by the
31 	time we call destroySWFBlockList.  (I think..)
32 */
33 
34 struct blockListEntry
35 {
36 	SWFBlock block;
37 	BOOL isCharacter;
38 };
39 
40 struct SWFBlockList_s
41 {
42 	struct blockListEntry *blocks;
43 	int nBlocks;
44 };
45 
46 
destroySWFBlockList(SWFBlockList list)47 void destroySWFBlockList(SWFBlockList list)
48 {
49 	int i;
50 
51 	for ( i=0; i<list->nBlocks; ++i )
52 	{
53 		/* characters other than fontchars were instantiated by the user,
54 			 so user has to destroy them. */
55 
56 		if ( !list->blocks[i].isCharacter )
57 			destroySWFBlock(list->blocks[i].block);
58 	}
59 
60 	free(list->blocks);
61 	free(list);
62 }
63 
64 
65 SWFBlockList
newSWFBlockList()66 newSWFBlockList()
67 {
68 	SWFBlockList blockList = (SWFBlockList) malloc(sizeof(struct SWFBlockList_s));
69 
70 	blockList->nBlocks = 0;
71 	blockList->blocks = NULL;
72 
73 	return blockList;
74 }
75 
76 
77 #define SWFBLOCKLIST_INCREMENT 16
78 
79 void
SWFBlockList_addBlock(SWFBlockList list,SWFBlock block)80 SWFBlockList_addBlock(SWFBlockList list, SWFBlock block)
81 {
82 	if ( SWFBlock_isDefined(block) )
83 		return;
84 
85 	if ( list->nBlocks % SWFBLOCKLIST_INCREMENT == 0 )
86 	{
87 		list->blocks = (struct blockListEntry*) realloc(list->blocks,
88 													 (list->nBlocks + SWFBLOCKLIST_INCREMENT) *
89 													 sizeof(struct blockListEntry));
90 	}
91 
92 	list->blocks[list->nBlocks].block = block;
93 
94 	list->blocks[list->nBlocks].isCharacter =
95 		SWFBlock_isCharacter(block) &&
96 		SWFBlock_getType(block) != SWF_DEFINEFONT &&
97 		SWFBlock_getType(block) != SWF_DEFINEFONT2;
98 
99 	++list->nBlocks;
100 
101 	SWFBlock_setDefined(block);
102 }
103 
104 
105 void
SWFBlockList_addToSprite(SWFBlockList list,SWFSprite sprite)106 SWFBlockList_addToSprite(SWFBlockList list, SWFSprite sprite)
107 {
108 	int i;
109 
110 	for ( i=0; i<list->nBlocks; ++i )
111 		SWFSprite_addBlock(sprite, list->blocks[i].block);
112 
113 	list->nBlocks = 0;
114 
115 	free(list->blocks);
116 	list->blocks = NULL;
117 }
118 
119 
120 int
SWFBlockList_completeBlocks(SWFBlockList list,int version)121 SWFBlockList_completeBlocks(SWFBlockList list, int version)
122 {
123 	int i, total = 0;
124 
125 	for ( i=0; i<list->nBlocks; ++i )
126 	{
127 		/* ensure movie version information at completion time */
128 		list->blocks[i].block->swfVersion = version;
129 		total += completeSWFBlock(list->blocks[i].block);
130 	}
131 
132 	return total;
133 }
134 
135 
136 int
SWFBlockList_writeBlocksToMethod(SWFBlockList list,SWFByteOutputMethod method,void * data)137 SWFBlockList_writeBlocksToMethod(SWFBlockList list,
138 																 SWFByteOutputMethod method, void *data)
139 {
140 	int i, size = 0;
141 
142 	for ( i=0; i<list->nBlocks; ++i )
143 		size += writeSWFBlockToMethod(list->blocks[i].block, method, data);
144 
145 	return size;
146 }
147 
148 /* Return last block in the list, or NULL if the list is empty */
149 SWFBlock
SWFBlockList_getLastBlock(SWFBlockList list)150 SWFBlockList_getLastBlock(SWFBlockList list)
151 {
152 	if ( list->nBlocks ) return list->blocks[list->nBlocks-1].block;
153 	else return NULL;
154 }
155 
156 
157 /*
158  * Local variables:
159  * tab-width: 2
160  * c-basic-offset: 2
161  * End:
162  */
163