+
+typedef struct
+{
+ TAG*tag;
+ SRECT*bbox;
+ enum {none_type, shape_type, image_type, text_type, font_type} type;
+ union {
+ SHAPE2*shape;
+ SWFFONT*font;
+ } obj;
+} character_t;
+
+int compare_placements(const void *v1, const void *v2)
+{
+ SWFPLACEOBJECT*p1 = (SWFPLACEOBJECT*)v1;
+ SWFPLACEOBJECT*p2 = (SWFPLACEOBJECT*)v2;
+ if(p1->depth != p2->depth)
+ return (int)p1->depth - (int)p2->depth;
+ else
+ if(p2->clipdepth)
+ return 1; // do the clip first
+ else
+ return -1;
+
+/* if(!p1->clipdepth) {
+ if(!p2->clipdepth) {
+ // !p1->clipdepth && !p2->clipdepth
+ return (int)p1->depth - (int)p2->depth;
+ } else {
+ // !p1->clipdepth && p2->clipdepth
+ if(p1->depth != p2->clipdepth)
+ return (int)p1->depth - (int)p2->clipdepth;
+ else
+ return 1; // do the clip first
+ }
+ } else {
+ if(!p2->clipdepth) {
+ // p1->clipdepth && !p2->clipdepth
+ if(p1->clipdepth != p2->depth)
+ return (int)p1->clipdepth - (int)p2->depth;
+ else
+ return -1;// do the clip first
+ } else {
+ if(p1->clipdepth != p2->clipdepth)
+ return (int)p1->clipdepth - (int)p2->clipdepth;
+ else
+ return (int)p1->depth - (int)p2->depth;
+ }
+ }*/
+}
+
+void swf_RenderSWF(RENDERBUF*buf, SWF*swf)
+{
+ TAG*tag;
+ int t;
+ int numplacements;
+ RGBA color;
+ SWFPLACEOBJECT* placements;
+
+ character_t* idtable = rfx_calloc(sizeof(character_t)*65536); // id to character mapping
+ SWFPLACEOBJECT** depthtable = rfx_calloc(sizeof(SWFPLACEOBJECT*)*65536); // depth to placeobject mapping
+
+ tag = swf->firstTag;
+ numplacements = 0;
+ while(tag) {
+ if(tag->id == ST_PLACEOBJECT ||
+ tag->id == ST_PLACEOBJECT2) {
+ numplacements++;
+ }
+ tag = tag->next;
+ }
+ placements = rfx_calloc(sizeof(SWFPLACEOBJECT)*numplacements);
+ numplacements = 0;
+
+ /* set background color */
+ color = swf_GetSWFBackgroundColor(swf);
+ swf_Render_SetBackgroundColor(buf, color);
+
+ /* parse definitions */
+ tag = swf->firstTag;
+ while(tag) {
+ if(swf_isDefiningTag(tag)) {
+ int id = swf_GetDefineID(tag);
+ idtable[id].tag = tag;
+ idtable[id].bbox = rfx_alloc(sizeof(SRECT));
+ *idtable[id].bbox = swf_GetDefineBBox(tag);
+
+ if(swf_isShapeTag(tag)) {
+ SHAPE2* shape = rfx_calloc(sizeof(SHAPE2));
+ swf_ParseDefineShape(tag, shape);
+ idtable[id].type = shape_type;
+ idtable[id].obj.shape = shape;
+ } else if(swf_isImageTag(tag)) {
+ int width,height;
+ RGBA*data = swf_ExtractImage(tag, &width, &height);
+ idtable[id].type = image_type;
+ swf_Render_AddImage(buf, id, data, width, height);
+ free(data);
+ } else if(tag->id == ST_DEFINEFONT ||
+ tag->id == ST_DEFINEFONT2) {
+ //swf_FontExtract(swf,id,&idtable[id].font);
+ idtable[id].obj.font = 0;
+ } else if(tag->id == ST_DEFINEFONTINFO ||
+ tag->id == ST_DEFINEFONTINFO2) {
+ idtable[id].type = font_type;
+ } else if(tag->id == ST_DEFINETEXT ||
+ tag->id == ST_DEFINETEXT2) {
+ idtable[id].type = text_type;
+ }
+ } else if(tag->id == ST_PLACEOBJECT ||
+ tag->id == ST_PLACEOBJECT2) {
+ SWFPLACEOBJECT p;
+ swf_GetPlaceObject(tag, &p);
+ /* TODO: add move and deletion */
+ placements[numplacements++] = p;
+ swf_PlaceObjectFree(&p); //dirty! but it only removes items we don't need
+ }
+ tag = tag->next;
+ }
+
+ qsort(placements, numplacements, sizeof(SWFPLACEOBJECT), compare_placements);
+
+ for(t=0;t<numplacements;t++) {
+ SWFPLACEOBJECT*p = &placements[t];
+ int id = p->id;
+
+ if(!idtable[id].tag) {
+ fprintf(stderr, "rfxswf: Id %d is unknown\n", id);
+ continue;
+ }
+
+ if(idtable[id].type == shape_type) {
+ SRECT sbbox = swf_TurnRect(*idtable[id].bbox, &p->matrix);
+ swf_RenderShape(buf, idtable[id].obj.shape, &p->matrix, &p->cxform, p->depth, p->clipdepth);
+ } else if(idtable[id].type == text_type) {
+ /* TODO */
+ } else {
+ fprintf(stderr, "Unknown/Unsupported Object Type for id %d: %s\n", id, swf_TagGetName(idtable[id].tag));
+ }
+ }
+
+ /* free id and depth tables again */
+ for(t=0;t<65536;t++) {
+ if(idtable[t].bbox) {
+ free(idtable[t].bbox);
+ idtable[t].bbox=0;
+ }
+ if(idtable[t].type == shape_type) {
+ SHAPE2* shape = idtable[t].obj.shape;
+ if(shape) {
+ swf_Shape2Free(shape); // FIXME
+ free(idtable[t].obj.shape);idtable[t].obj.shape = 0;
+ }
+ }
+ }
+ free(placements);
+ free(idtable);
+ free(depthtable);
+}
+