<< Back to Category List
pages:
prev | 1 | next
GetLockedArray Tutorial
Ok this will probably be one of the most confusing tutorials you have ever read. This is not for beginers, and will be written towards
someone who has some previous experience with DirectDraw. Well now that I have said that let get to work!
What can this be used for?
Well this function can be used to get the color values from a direct draw surface, or for creating special effect like screen wipes, darkening a whole surface, fades, etc.
How is this different then GetLockedPixel?
GetLockedArray reads the pixel of a surace into an array of bytes, then you can access the array for the pixel values, Whereas GetLockedPixel read only one pixel at a time, and is slower for doing large operations.
My Assumptions:
Ok, I am asuming that you all ready have a surface initialized, and it is named ddsArray. That you know what RECT's, surfaces, and arrays are.
Step 1. Defining the Variables
We are going to need a couple of variables to store our values in:
* Don't worry I will explain all of the onfo below
Dim bytObj() As Byte 'Stores all the color values
Dim ddsdBlank As DDSURFACEDESC2 'blank Surface desciption
Dim rectBlank as RECT 'Empty rect
Step 2. Preforming the Action
This code will actually put all of the pixel values into the array.
ddsArray.Lock rectBlank, ddsdBlank, DDLOCK_WAIT Or DDLOCK_READONLY, 0
ddsArray.GetLockedArray bytArray
Explaination:
Before you call the actual GetLockedArray function we MUST Lock the surface.
This line locks the surface:
ddsArray.Lock rectBlank, ddsdtest, DDLOCK_WAIT Or DDLOCK_READONLY, 0
rectBlank is just an empty Rect, it is just required by the function
ddsdBlank is a blank surface description, there is no need to fill this type with any values.
DDLOCK_WAIT flag specifies to wait for the last surface opperation to finnish.
DDLOCK_READONLY flag tell it that you will not be writing to
the array, which makes it faster.
This line puts the pixel values into the byte array:
ddsArray.GetLockedArray bytArray
bytArray() is a blank byte array
Step 3. Learning time
Time to take a litle break from the code, and learn how to extract the long color values, were used to, out of the array.
Here is the confusing part, when we use getlocked array it stores the long integer in to 2 different byte values(Confused yet?). So if you do a BltColorFill(65500)which is near white, to a surface that is 4x1, then our array will be bytArray(7, 0), or 8x1
Yes the width is twice the value of the real surface width, I will explain that now to save room directx splits the long value into two bytes which is why the array width is double. Two change those two bytes back into the long(to find the pixel value) we do this:
This example is for the pixel at 0x0 in the array:
LongVal = (256*bytArray(1, 0)) + bytArray(0, 0)
*Remember that the array dimension start at 0.
To get the long value we multiply the second byte by 256, then add the first value to it.
Step 4. Another Strange Problem
Well here is another strange thing that Microsoft
did to confuse me, The width of the array is always a multple of 4,
so if your surface is only 1 pixel wide then the array will still be 8, because it will assume 4 pixel wide then two bytes for each makes 8. This doesn't pose much a problem becuase it just fills the extra space with 0's, but if you want to precalculate the width I will throw in some code to do so, but I am not going to bother explaining it because you will probably never
use it.
Dim tmp1 As Integer
Dim tmp2 As Integer
tmp1 = (Surface Width) Mod 4
If tmp1 0 Then
tmp2 =? ((Surface Width) - tmp1 + 4)* 2
Else
tmp2 = (Surface Width) * 2
End If
Step 5. CleanUp
To clean up all we really have to do is unlock the surface, using this line of code
ddsArray.Unlock rectBlank
Some Notes:
Here is just a few thing to keep in mind,
1. If you unlock the surface your whole array will also be erased!
2. Please let me know what you think about this tutorial, more info, more detail, more examples?
3. Thank you for reading this whole article!!
4. Your DONE, go home!
Please send comments to Troll1184@cs.com. Later
pages:
prev | 1 | next |