Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Advanced : Reading strange datatypes (from a SWF flash file)

Author
Message
dj chainz
20
Years of Service
User Offline
Joined: 25th Sep 2004
Location: England
Posted: 19th Dec 2004 23:40
I am using SWF files in my DBPro game, as cutscenes/minigames. I already have a plugin that lets me use the flash files in my game (thanx www.accode.com). However, now I need to read the width & height of the movie from the file.

I already have obtained the file format spec from macromedia, read through the important parts etc. The problem lies in reading strange datatypes - you will see if you look at the attached screenshot from the SWF specification what I mean.

I need to be able to read an unsigned 5 bit number, and four signed Nbit numbers. If you understand, please help - otherwise dont try.

I am the lead programmer at red spark studios
I also have a blog on how to make a game engine
http://www.dbgame.blogspot.com

Attachments

Login to view attachments
David T
Retired Moderator
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 20th Dec 2004 02:32
I too tried to understand the flash format. As TCA explained to me - they're just integers with different ranges. I think for this level of detail you may need to write it in C++ and access it via a TPC.

Get 15 new commands, all the date / time commands left out of DBPro for free!
DOWNLOAD PLUGINS HERE: http://www.davidtattersall.me.uk/ and select "DarkBasic"
dj chainz
20
Years of Service
User Offline
Joined: 25th Sep 2004
Location: England
Posted: 20th Dec 2004 05:06
well, the C++ book and compiler are coming out... wish me luck

I am the lead programmer at red spark studios
I also have a blog on how to make a game engine
http://www.dbgame.blogspot.com
Clueless
21
Years of Service
User Offline
Joined: 16th Feb 2004
Location: Corbin, KY, USA
Posted: 20th Dec 2004 09:06 Edited at: 20th Dec 2004 11:14
Hi DJ,

This is easier in C++ or assembler, but you can pull it off in most flavors of Basic. Here are some thoughts in case you're still stuck.

I think UB[5] in the flash docs means you want the rightmost 5 bits of an unsigned byte. In the example they gave where they encode the value 11 decimal in 5 bits, you want to mask the bit positions with X's here (remembering bit positions are numbered 7 to 0, starting from the left and working right):

x x x 0 1 0 1 1 (binary) = 11 (decimal)

In assembler you could do this with boolean AND or you could do arithmetic shift lefts on the value, which is equiv to repeatedly multiplying by two but throwing away the overflow bits. In Basic, you could fake this, I think, like this:

read value_from_file

for e = 7 to 5 step -1
test = 2 ** e
if value_from_file > test
value_from file = value_from_file - test
endif
next e

What I'm proposing, given the example of a 5-bit integer encoded into an 8-bit field, is treating the input as an 8-bit integer and then "masking" off the leftmost 3 bits by comparing the input value to what you'd have if you took 2 raised to the power of the bit position you wish to mask.

If you look at just the values you'd have if bit 7 was set, or only bit 6 was set, or only bit 5 was set, you'd see:

Binary Dec
--------------- -------------
1 0 0 0 0 0 0 0 = 128 (2 ^ 7)
0 1 0 0 0 0 0 0 = 64 (2 ^ 6)
0 0 1 0 0 0 0 0 = 32 (2 ^ 5)

So if you read an 8-bit value and saw it was > 128, then you know bit 7 is set. You don't want bit 7 in your result, so you subtract 128 from the value you read. And so on. for example:

If value_from_file = 11101011 = 235 decimal

In the pseudo code I wrote above, the loop would execute 3 times giving you these values:

e = 7, test = 128, value_from_file = 235 - 128 = 107
e = 6, test = 64, value_from_file = 107 - 64 = 43
e = 5, test = 32, value_from_file = 43 - 32 = 11

You'd want to alter the hard-coded values in the loop "for e = 7 to 5 skip -1" of course, after you learned your NBits value was 11 (that is, 11 bits were used in the reset of the rectangle definition to define the window). In the shockwave example of NBits=11, you'd go "e = 15 to 12 skip -1" to mask the bits NOT needed in an 11 bit integer.
dj chainz
20
Years of Service
User Offline
Joined: 25th Sep 2004
Location: England
Posted: 21st Dec 2004 00:51
Hmmm, i understand what you're saying Clueless. I did think of what you are saying above, but I don't know if the 5bit number is within a byte - it appears not to be. And an 11 bit number would not fit inside a byte (which has 8 bits), so I cannot read a byte... I have started to work on a plugin (installing C++ from that disk I found under my PC, and reading the dbpro sdk), so it shouldnt take me that much time anyhow.

I am the lead programmer at red spark studios
I also have a blog on how to make a game engine
http://www.dbgame.blogspot.com
Clueless
21
Years of Service
User Offline
Joined: 16th Feb 2004
Location: Corbin, KY, USA
Posted: 21st Dec 2004 06:53
Hi DJ,

Quote: "it appears not to be. And an 11 bit number would not fit inside a byte"


Very true. I meant only that they're telling you (in the SWF docs) that they're storing the NBITS value as the lower 5 bits of a byte, when they refer to UB[5], so before you can do anything else you've got to decode the lower 5 bits of the first byte. From there, you could theoretically be faced with the need to decode longwords ("11111" = 31 bits maximum), not just the 8-bit bytes or 16-bit words.

Generally though, any time you're faced with the need to read variable-length data and the data itself contains a field length value, there's always a predefined starting point that everybody can agree upon. From the page in your example, the 5 bit length always appears as the lower 5 bits of the first byte of data in the RECT structure.

From there, you're guaranteed to have your xmin,xmax,ymin,ymax values actually span multiple bytes of data, so you'll have to slice bits and reassemble them. Xmin is going to always start at the 6th bit (immediately after the last bit of the NBits) but could end within the same byte as the NBITS field or stretch almost all the way across the following longword.

Anyway, you'll solve those mysteries one way or the other as you develop your plugin I think you're heading in the right direction doing it in C++. Let us know what you find out. You've shown me something I didn't know already, which was the ability to play flash movies via a plugin. Thankee.
dj chainz
20
Years of Service
User Offline
Joined: 25th Sep 2004
Location: England
Posted: 21st Dec 2004 15:30
It plays Flash games too....

And I may not develop a plugin, if I can do it in DBPro, as I will end up with some functions instead

I am the lead programmer at red spark studios
I also have a blog on how to make a game engine
http://www.dbgame.blogspot.com

Login to post a reply

Server time is: 2025-05-28 06:13:24
Your offset time is: 2025-05-28 06:13:24