macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Basics of Transparent Blitting, Part 1
Pages: 1, 2, 3

Converting to Hexadecimal

Uh-oh! I warned you this was coming. Whenever you deal with computer memory (buffers) you’re going to have to speak hexadecimal. The contents of our offscreen buffers are all stored using hexadecimal values. If we’re going to scan a buffer for RGB values, then we should be using RGB values in hexadecimal format. We can convert the RGB values we currently have to hexadecimal format using the following lines of code:



# convert to hexadecimal
set R_value [format "%02x" [lindex $transparency_pixel 0]]
set G_value [format "%02x" [lindex $transparency_pixel 1]]
set B_value [format "%02x" [lindex $transparency_pixel 2]]

I am using the tools provided in the Tcl scripting language to convert values to hexadecimal. So no complex math is involved.

set transparency_pixel {198 0 107}
198 0 107
() 2 % # convert to hexadecimal
() 3 % set R_value \
[format "%02x" [lindex $transparency_pixel 0]]
c6
() 4 % set G_value \
[format "%02x" [lindex $transparency_pixel 1]]
00
() 5 % set B_value \
[format "%02x" [lindex $transparency_pixel 2]]
6b

These values match the hexadecimal values shown in Figure 2. Now we define the transparency pixel value that we are going to scan for in the sprite's buffer.

# create the transparency value string
set RGB_value "#$R_value$G_value$B_value"

Testing the above code in the Wish Shell we see:

() 6 % # create the transparency value string
() 7 % set RGB_value "#$R_value$G_value$B_value"
#c6006b

The value #c6006b is the pattern we are going to scan for in the sprites buffer.

Creating a Hexadecimal Procedure for our Graphics Tool Kit

Wouldn’t it be nice if we had a library procedure we could call to magically handle the RGB decimal-to-hexadecimal conversion for us? Well, the Tcl scripting language allows us to add tools we need for our code. We’ll use the Tcl proc command to write a procedure.

proc RGB_convert_decimal_hexadeimal { transparency_pixel } {
    # convert to hexadecimal
    set R_value \
	[format "%02x" [lindex $transparency_pixel 0]]
	set G_value \
	[format "%02x" [lindex $transparency_pixel 1]]
	set B_value \
	[format "%02x" [lindex $transparency_pixel 2]]
	# create the transparency value string

    set RGB_value "#$R_value$G_value$B_value"return $RGB_value
}

Let’s again try out our previous example of converting the transparency pixel RGB values from decimal to hexadecimal.

() 5 % RGB_convert_decimal_hexadeimal $transparency_pixel
#c6006b
() 6 %

Pretty slick, huh? The Tcl proc command allows us to add our own library calls.

I haven’t introduced anything new inside the body of the procedure. But check out what we can do now. Let’s take a close look at the procedure.

proc RGB_convert_decimal_hexadeimal { transparency_pixel } {
    # convert to hexadecimal
	:
	return $RGB_value
}

We start with the Tcl proc command to write a procedure. The proc command is followed by the name of the procedure. The name of the procedure is how we will call the operation from our script. Next, in parenthesis, we have what is called an argument. An argument is a Tcl variable from the script we want to use inside the procedure. This is called passing in an argument. In this case, we pass in the transparency_pixel RGB decimal value. When we convert the transparency_pixel values from decimal to hexadecimal we need to give the new value back to the calling script. We use the Tcl command return. This returns, or gives back, the new hexadecimal value we were after.

Lesson 3: Scanning Raw Sprite Pixels

Now we have our work cut out for us. We need to implement a more complex procedure than we wrote for converting between decimal and hexadecimal values. The Tk image library does not provide a library to blit using a transparency pixel. Not to worry, let’s roll up our sleeves and get to work. We’ll make our own!

The first task at hand is gaining access to the raw data pixels of our sprite. The Tk image library, fortunately, provides a procedure for us to do this. Otherwise, we would have had to close up shop and go home. The Tk image library procedure that does this is data.

Let’s grab the raw data pixels of our happy, jumping monster.

# get image data set raw_monster_data [$sprites data -from 265 737 328 800]

Go ahead and follow along in the Wish Shell console. Experiment a bit. You’ll see some interesting concepts we’ve been playing with thus far. Remember, you will need the directory, /game_dev created on your hard drive. You will need the art file downloaded to that directory as well.

The Script:

# load the art file
set filename "/game_dev/game_art.gif"
set sprites [image create photo -file $filename]

# load the raw pixel data into raw_monster_data
set raw_monster_data [$sprites data -from 265 737 328 800] 

# raw_monster 63 lines long
llength $raw_monster_data

When the script executes you will see some familiar hexadecimal values, just like the ones we have been playing with. I threw in the Tcl llength command to demonstrate to you that the monster pixel data contains 63 lines or <rows> of hexadecimal values. Do you wonder why that is? Let’s do a little math, shall we?

The top pixel of the monster is at location y1=737. The bottom pixel is at location y2=800. Let’s subtract,

800 – 737 = 63 <height>

Let’s check our monster sprites.

() 9 % llength $raw_monster_data
63

Coincidence? Not exactly. Our monster sprite is 63 pixels tall. Now consider these to be 63 rows of pixel information. Each row of pixel information contains data for 63 pixels of information.

The row pixels across the monster are at location x1=265. The rightmost pixel is at location x2=328. Let’s subtract,

328 – 265 = 63 <width>

For each row we will call a scan row for this lesson. There are 63 scan rows of pixel information. Each individual scan row contains data for 63 pixels.

Figure 3. There are 63 Pixels per Scan Row.
Figure 3. There are 63 pixels per scan row.

Pages: 1, 2, 3

Next Pagearrow