Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Game Programming > Pixel color replacement in VB games...?


Reply
 
Thread Tools Display Modes
  #1  
Old 02-13-2003, 01:39 PM
Mithrandel Mithrandel is offline
Junior Contributor
 
Join Date: Feb 2002
Location: They call me nowhere man.
Posts: 351
Default 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.
Attached Images
File Type: gif switcheroo.gif (4.4 KB, 121 views)
Reply With Quote
  #2  
Old 02-13-2003, 02:17 PM
happy_mouse's Avatar
happy_mouse happy_mouse is offline
Regular
 
Join Date: Dec 2002
Location: North Carolina
Posts: 54
Default

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.
Reply With Quote
  #3  
Old 02-13-2003, 02:20 PM
Mithrandel Mithrandel is offline
Junior Contributor
 
Join Date: Feb 2002
Location: They call me nowhere man.
Posts: 351
Default

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. =)
Reply With Quote
  #4  
Old 02-13-2003, 02:45 PM
ALEX_0077's Avatar
ALEX_0077 ALEX_0077 is offline
Contributor
 
Join Date: Nov 2002
Location: American Canyon, CA
Posts: 622
Default

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.
Reply With Quote
  #5  
Old 02-13-2003, 04:37 PM
Mithrandel Mithrandel is offline
Junior Contributor
 
Join Date: Feb 2002
Location: They call me nowhere man.
Posts: 351
Default

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.
Attached Files
File Type: zip facemaker.zip (4.7 KB, 11 views)
Reply With Quote
  #6  
Old 02-13-2003, 05:30 PM
AtonalPanic AtonalPanic is offline
Contributor
 
Join Date: Jul 2002
Location: Houston, Texas
Posts: 445
Default

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.
Reply With Quote
  #7  
Old 02-13-2003, 05:36 PM
ALEX_0077's Avatar
ALEX_0077 ALEX_0077 is offline
Contributor
 
Join Date: Nov 2002
Location: American Canyon, CA
Posts: 622
Default

is the hair supposed to be transparent? (looks like jelly). if so, than there is no problem.
Attached Images
File Type: gif myviewpoint.gif (3.3 KB, 101 views)
Reply With Quote
  #8  
Old 02-13-2003, 05:39 PM
AtonalPanic AtonalPanic is offline
Contributor
 
Join Date: Jul 2002
Location: Houston, Texas
Posts: 445
Default

Here is an example of what I meant...
Attached Files
File Type: zip desktop.zip (20.1 KB, 19 views)
Reply With Quote
  #9  
Old 02-13-2003, 05:43 PM
Mithrandel Mithrandel is offline
Junior Contributor
 
Join Date: Feb 2002
Location: They call me nowhere man.
Posts: 351
Default

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.
Reply With Quote
  #10  
Old 02-13-2003, 05:45 PM
ALEX_0077's Avatar
ALEX_0077 ALEX_0077 is offline
Contributor
 
Join Date: Nov 2002
Location: American Canyon, CA
Posts: 622
Default

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
Reply With Quote
  #11  
Old 02-13-2003, 05:56 PM
Mithrandel Mithrandel is offline
Junior Contributor
 
Join Date: Feb 2002
Location: They call me nowhere man.
Posts: 351
Default

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.
Reply With Quote
  #12  
Old 02-13-2003, 05:57 PM
AtonalPanic AtonalPanic is offline
Contributor
 
Join Date: Jul 2002
Location: Houston, Texas
Posts: 445
Default

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.
Reply With Quote
  #13  
Old 02-14-2003, 06:14 AM
Machaira's Avatar
Machaira Machaira is offline
Jedi Coder

* Expert *
 
Join Date: Aug 2002
Location: Abingdon, MD
Posts: 3,438
Default

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
Reply With Quote
  #14  
Old 02-14-2003, 09:43 AM
ALEX_0077's Avatar
ALEX_0077 ALEX_0077 is offline
Contributor
 
Join Date: Nov 2002
Location: American Canyon, CA
Posts: 622
Default

Wow, didnt know that existed. Super!
Reply With Quote
  #15  
Old 02-14-2003, 05:21 PM
Mithrandel Mithrandel is offline
Junior Contributor
 
Join Date: Feb 2002
Location: They call me nowhere man.
Posts: 351
Default

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? =)
Reply With Quote
  #16  
Old 02-14-2003, 06:29 PM
Squirm's Avatar
Squirm Squirm is offline
Political Coder

Retired Moderator
* Guru *
 
Join Date: Mar 2001
Location: London, England
Posts: 8,037
Default

GetObject followed by GetBitmapBits followed by SetBitmapBits
Or maybe this will be of help:
http://www.rookscape.com/vbgaming/tutAY.php
__________________
Search the forums | Use [vb][/vb] tags | Still IRCing
Reply With Quote
  #17  
Old 02-14-2003, 06:42 PM
Mithrandel Mithrandel is offline
Junior Contributor
 
Join Date: Feb 2002
Location: They call me nowhere man.
Posts: 351
Default

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. =)
Reply With Quote
  #18  
Old 02-14-2003, 07:04 PM
Machaira's Avatar
Machaira Machaira is offline
Jedi Coder

* Expert *
 
Join Date: Aug 2002
Location: Abingdon, MD
Posts: 3,438
Default

Pointer manipulation in VB exists, but it's very undocumented. The various ...Ptr function (VarPtr, ObjPtr, StrPtr) are what you would use.
Reply With Quote
  #19  
Old 02-14-2003, 09:10 PM
AtonalPanic AtonalPanic is offline
Contributor
 
Join Date: Jul 2002
Location: Houston, Texas
Posts: 445
Default

and if u find the way, return the favor...
Reply With Quote
  #20  
Old 02-14-2003, 11:18 PM
Machaira's Avatar
Machaira Machaira is offline
Jedi Coder

* Expert *
 
Join Date: Aug 2002
Location: Abingdon, MD
Posts: 3,438
Default

Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->