 |
|

02-13-2003, 01:39 PM
|
|
Junior Contributor
|
|
Join Date: Feb 2002
Location: They call me nowhere man.
Posts: 351
|
|
Pixel color replacement in VB games...?
|
Has anyone already written a tutorial for color-switching (by pixel) in VB?
Failing that, could anyone point me in the right direction with color swapping by pixels?
If I'm not describing this correctly, perhaps this picture will help.
|
|

02-13-2003, 02:17 PM
|
 |
Regular
|
|
Join Date: Dec 2002
Location: North Carolina
Posts: 54
|
|
You could use the API SetPixel
Code:
Public Declare Function SetPixel Lib "gdi32" Alias "SetPixel" _
(ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal _
crColor As Long) As Long
...as long as you know what positions in the bitmap you want to change.
|
|

02-13-2003, 02:20 PM
|
|
Junior Contributor
|
|
Join Date: Feb 2002
Location: They call me nowhere man.
Posts: 351
|
|
|
I guess that's an option, though it seems a little more involved than I wanted to get. Using SetPixel, I'd need to know the location of every single pixel I might want to change ---
--- unless, of course, I simply looped through the entire image and checked for my swapping pixels with GetPixel.
Heh. Might be a viable solution. =)
|
|

02-13-2003, 02:45 PM
|
 |
Contributor
|
|
Join Date: Nov 2002
Location: American Canyon, CA
Posts: 622
|
|
I know, I know, Palettes are completley shunned. Their old, and alpha blending is better. But...
..., just for a refrence on history, try looking up palletes (palettes? pallettes?) on the search thingy or on google. At the very least you'll learn the history on why/why not use them anymore. 
|
|

02-13-2003, 04:37 PM
|
|
Junior Contributor
|
|
Join Date: Feb 2002
Location: They call me nowhere man.
Posts: 351
|
|
|
The jury's still out on how I'll do the pixel replacement. Alpha blending is a good idea as well, and I'm sure I could find a C++ dll to do the trick for me.
But I've run into a bit more serious (and slightly embarassing) realisation: I've somehow managed to mess up doing simple BitBlt transparancies. It's not my computer, because a previous project which does the same thing that this one does runs just fine. But for some weird reason, this project (which I've attached) doesn't want to do transparancies.
If someone would be willing to step through the minimal (~30) lines of code in this project, and give me an idea of what I've done wrong, I'd be greatful.
|
|

02-13-2003, 05:30 PM
|
|
Contributor
|
|
Join Date: Jul 2002
Location: Houston, Texas
Posts: 445
|
|
|
You could also loop though a 2dimensional array of pixels, check the pixel color with getpixel api. Then replace the pixel with the it's color alternative.
|
|

02-13-2003, 05:36 PM
|
 |
Contributor
|
|
Join Date: Nov 2002
Location: American Canyon, CA
Posts: 622
|
|
|
is the hair supposed to be transparent? (looks like jelly). if so, than there is no problem.
|
|

02-13-2003, 05:39 PM
|
|
Contributor
|
|
Join Date: Jul 2002
Location: Houston, Texas
Posts: 445
|
|
|
Here is an example of what I meant...
|
|

02-13-2003, 05:43 PM
|
|
Junior Contributor
|
|
Join Date: Feb 2002
Location: They call me nowhere man.
Posts: 351
|
|
Heh, no... though that is an (admittedly unintended) cool effect.
I've narrowed the problem to the blitting of the mask image - the second of three commands in the Blitting sub. For some odd reason, the command
Code:
Call BlitBitmap(Bits(3), vbSrcAnd)
fails.
I ended up changing that particular command to
Code:
Call BitBlt(frmMain.Surface.hdc, 0, 0, Bits(3).Bits.bmWidth, _
Bits(3).Bits.bmHeight, Bits(3).hdcBitmap, 0, 0, vbSrcAnd)
but BitBlt fails outright.
I'm terribly confused... but then again, I usually am. =)
[edit]
Atonal, that's a good idea, but wouldn't looping through every single pixel be too slow? I'd like to be able to swap colors as quickly as possible... But then again, I don't know of any other way to swap pixels, aside from Alpha Blending,which was suggested earlier. =/
|
Last edited by Mithrandel; 02-13-2003 at 05:49 PM.
|

02-13-2003, 05:45 PM
|
 |
Contributor
|
|
Join Date: Nov 2002
Location: American Canyon, CA
Posts: 622
|
|
|
Uh, just change the rgb values to 255,255,255 in the 'If this color' part of the if/then statement to see it work
|
|

02-13-2003, 05:56 PM
|
|
Junior Contributor
|
|
Join Date: Feb 2002
Location: They call me nowhere man.
Posts: 351
|
|
|
Okay, I'm an idiot. I should have guessed this would have been the problem:
Bits(3) = LoadBitmap(App.Path & "\m_hair_mask.bmp")
The file's name is m_hair mask.bmp.
|
|

02-13-2003, 05:57 PM
|
|
Contributor
|
|
Join Date: Jul 2002
Location: Houston, Texas
Posts: 445
|
|
|
Thats true, but why would you loop through it every second? There are 2 options as I see it, find a Optimization method(and I mean one heck of one), or do the coloring once and save the pic. Then go on from there.
|
|

02-14-2003, 06:14 AM
|
 |
Jedi Coder
* Expert *
|
|
Join Date: Aug 2002
Location: Abingdon, MD
Posts: 3,438
|
|
Quote:
Originally posted by Mithrandel
I've narrowed the problem to the blitting of the mask image - the second of three commands in the Blitting sub. For some odd reason, the command
Code:
Call BlitBitmap(Bits(3), vbSrcAnd)
fails.
I ended up changing that particular command to
Code:
Call BitBlt(frmMain.Surface.hdc, 0, 0, Bits(3).Bits.bmWidth, _
Bits(3).Bits.bmHeight, Bits(3).hdcBitmap, 0, 0, vbSrcAnd)
but BitBlt fails outright.
|
ALWAYS check the return value of your function calls! It'll make your debugging much easier in most cases.
Code:
'In a module
Public Declare Function GetLastError Lib "kernel32.dll" () As Long
Dim lRet As Long
lRet = BitBlt(frmMain.Surface.hdc, 0, 0, Bits(3).Bits.bmWidth, _
Bits(3).Bits.bmHeight, Bits(3).hdcBitmap, 0, 0, vbSrcAnd)
If lRet = 0 Then
MsgBox GetLastError
End If
|
|

02-14-2003, 09:43 AM
|
 |
Contributor
|
|
Join Date: Nov 2002
Location: American Canyon, CA
Posts: 622
|
|
Wow, didnt know that existed.  Super!
|
|

02-14-2003, 05:21 PM
|
|
Junior Contributor
|
|
Join Date: Feb 2002
Location: They call me nowhere man.
Posts: 351
|
|
|
Okay, I tried out the per-pixel swapping. It takes one second to swap out all the colors I need to on a single bitmap (on a 233mhz P2), and I'll probably need to do four at a time. Four seconds isn't all that bad. I can live with four seconds.
However, I think I can make it a lot faster if I get/set the pixels while they're still in memory, rather than from the PictureBox.
But I have no idea where the bits of a bitmap in memory are located. Could someone give me a push in the right direction? =)
|
|

02-14-2003, 06:29 PM
|
 |
Political Coder
Retired Moderator * Guru *
|
|
Join Date: Mar 2001
Location: London, England
Posts: 8,037
|
|
|

02-14-2003, 06:42 PM
|
|
Junior Contributor
|
|
Join Date: Feb 2002
Location: They call me nowhere man.
Posts: 351
|
|
Quote:
...I also created a table to show you the speed differences. Every test has been repeated five times. Then I calculated a [mean] of all the five tests:
PSet and Point - 3737.6 ms
GetPixel and SetPixel - 3133.4 ms
GetPixel and SetPixelV - 3210.4 ms
GetPixel and SetPixel with created DC - 2032.4 ms
GetPixel and SetPixelV with created DC - 1936.0 ms
Pointer - 222.0 ms
|
Obviously, I need to check out pointers (I thought you couldn't do them in VB?). As for you, Squirm... you seem to know where everything is. Much thanks. =)
|
|

02-14-2003, 07:04 PM
|
 |
Jedi Coder
* Expert *
|
|
Join Date: Aug 2002
Location: Abingdon, MD
Posts: 3,438
|
|
|
Pointer manipulation in VB exists, but it's very undocumented. The various ...Ptr function (VarPtr, ObjPtr, StrPtr) are what you would use.
|
|

02-14-2003, 09:10 PM
|
|
Contributor
|
|
Join Date: Jul 2002
Location: Houston, Texas
Posts: 445
|
|
|
and if u find the way, return the favor...
|
|

02-14-2003, 11:18 PM
|
 |
Jedi Coder
* Expert *
|
|
Join Date: Aug 2002
Location: Abingdon, MD
Posts: 3,438
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|