GPWiki.org
GPWiki.org
It is currently Fri May 24, 2013 3:19 pm

All times are UTC




Post new topic Reply to topic  [ 15 posts ] 
Author Message
PostPosted: Fri May 27, 2005 9:24 pm 
Prolific Poster

Joined: Fri May 27, 2005 9:12 pm
Posts: 16
http://www.rookscape.com/vbgaming/tutAY.php

I am useing Vb6, What i have done (from a new project):

copy the user-defines types and the declare statements into a module
created a picturebox with a bmp for the picture
copy dim's into form1's code section
option explicit in both form and module
added several command buttons to run parts of the code (labeled for sequence)

from the first command button:

GetObjectAPI Picture1.Picture, Len(bmp), bmp

With sa
.cbElements = 1
.cDims = 2
.Bounds(0).lLbound = 0
.Bounds(0).cElements = bmp.bmHeight
.Bounds(1).lLbound = 0
.Bounds(1).cElements = bmp.bmWidthBytes
.pvData = bmp.bmBits
End With

CopyMemory ByVal VarPtrArray(pic), VarPtrArray(sa), 4

When run, vb6 highlights sa from the last line and spits out an error of "expected array or user defined type " and quits.

Any ideas where i screwed up?

:edit- 5-28-2005
being that sa is a user-defined type,
Dim pic() As Byte
Dim sa As SAFEARRAY2D

Type SAFEARRAYBOUND
cElements As Long
lLbound As Long
End Type

Type SAFEARRAY2D
cDims As Integer
fFeatures As Integer
cbElements As Long
cLocks As Long
pvData As Long
Bounds(0 To 1) As SAFEARRAYBOUND
End Type

this makes me think the variables that are in safearray2d were once compatable with an array, but have now changed from this to something else...
this is puzzeling.

im wondering if this code was written for an earlier version of vb and vb changed between then and now. the orginal article that was translated for this page no-longer exists (all i get now is a web cam pop-up through this link
http://www.vb-empire.de.vu/

if thats the case, does anyone think this code can be salvaged/jumpstarted to a working state in vb6 or .net?


Last edited by Zombie on Thu Oct 13, 2005 3:56 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sun May 29, 2005 12:41 am 
Also known as "Lucky"

Joined: Sat Aug 07, 2004 9:22 pm
Posts: 5853
Location: Vancouver, Canada
If you could give us more code (ie. Where "sa" is declared) that would help.

Are you sure you declared "sa" within the proper scope?


Top
 Profile  
 
PostPosted: Sun May 29, 2005 10:26 am 
P2k
User avatar

Joined: Tue Sep 14, 2004 8:34 pm
Posts: 2168
Location: Aarhus, Denmark
Zombie wrote:
"expected array or user defined type "


My guess is that the error is thrown because you don't declare "sa" as an array, but as a UDT (user defined type).
Code:
Dim sa As SAFEARRAY2D


For "sa" to be an array of the UDT the code should be:
Code:
Dim sa() As SAFEARRAY2D
' or
Dim sa(3) As SAFEARRAY2D


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 29, 2005 5:24 pm 
Prolific Poster

Joined: Fri May 27, 2005 9:12 pm
Posts: 16
ok, my full code:

Code:

-from form1-
Option Explicit
Private Type SAFEARRAYBOUND
    cElements As Long
    lLbound As Long
End Type

Private Type SAFEARRAY2D
    cDims As Integer
    fFeatures As Integer
    cbElements As Long
    cLocks As Long
    pvData As Long
    Bounds(0 To 1) As SAFEARRAYBOUND
End Type

Private Type BITMAP
    bmType As Long
    bmWidth As Long
    bmHeight As Long
    bmWidthBytes As Long
    bmPlanes As Integer
    bmBitsPixel As Integer
    bmBits As Long
End Type

Dim pic() As Byte
Dim sa As SAFEARRAY2D
Dim bmp As BITMAP
Dim r As Long, g As Long, b As Long, i As Long, j As Long

Private Sub Command1_Click()
GetObjectAPI Picture1.Picture, Len(bmp), bmp
With sa
    .cbElements = 1
    .cDims = 2
    .Bounds(0).lLbound = 0
    .Bounds(0).cElements = bmp.bmHeight
    .Bounds(1).lLbound = 0
    .Bounds(1).cElements = bmp.bmWidthBytes
    .pvData = bmp.bmBits
End With


CopyMemory ByVal VarPtrArray(pic), VarPtrArray(sa), 4


End Sub

Private Sub Command2_Click()
'invert image
For i = 0 To UBound(pic, 1)
    For j = 0 To UBound(pic, 2)
        pic(i, j) = 255 - pic(i, j)
    Next j
Next i
End Sub

Private Sub Command3_Click()
'change colors seprately
For i = 0 To UBound(pic, 1) - 3 Step 3
    For j = 0 To UBound(pic, 2)
        r = pic(i + 2, j)
        g = pic(i + 1, j)
        b = pic(i, j)
        r = ((g * b) \ 128)
        g = ((r * b) \ 128)
        b = ((r * g) \ 128)
        If r > 255 Then r = 255
        If r < 0 Then r = 0
        If g > 255 Then g = 255
        If g < 0 Then g = 0
        If b > 255 Then b = 255
        If b < 0 Then b = 0
        pic(i, j) = b
        pic(i + 1, j) = g
        pic(i + 2, j) = r
    Next j
Next i
End Sub

Private Sub Command4_Click()
'Command4.Caption = RBG(pic(0, 2), pic(0, 1), pic(0, 0))
End Sub

Private Sub Command5_Click()
CopyMemory ByVal VarPtrArray(pic), 0&, 4
'You should delete your array after your changes, like in the line shown above.
Picture1.Refresh
End Sub

Private Sub Form_Load()
Set Picture1.Picture = LoadPicture("c:\circ.bmp")
End Sub

-from module1-
Option Explicit
Declare Function VarPtrArray Lib "msvbvm50.dll" Alias "VarPtr" (Ptr() As Any) As Long
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Declare Function GetObjectAPI Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long


ok, when i declare sa as:
Code:
Dim sa() As SAFEARRAY2D



width sa throws out an error - must use user defined type, object, or variant

when i change width to

Code:

'With sa
     sa.cbElements = 1
     sa.cDims = 2
     sa.Bounds(0).lLbound = 0
     sa.Bounds(0).cElements = bmp.bmHeight
     sa.Bounds(1).lLbound = 0
     sa.Bounds(1).cElements = bmp.bmWidthBytes
     sa.pvData = bmp.bmBits
'End With


i get invalid qualifier error

with

Code:

Dim sa(3) As SAFEARRAY2D



i modified width to

Code:

With sa(0)
    .cbElements = 1
    .cDims = 2
    .Bounds(0).lLbound = 0
    .Bounds(0).cElements = bmp.bmHeight
    .Bounds(1).lLbound = 0
    .Bounds(1).cElements = bmp.bmWidthBytes
    .pvData = bmp.bmBits
End With



and left

Code:

CopyMemory ByVal VarPtrArray(pic), VarPtrArray(sa), 4



as is. When command1_click is run, no errors result, but command2_click (run after command1_click is run) causes a crash of vb (windows xp style must close program, send or dont send info to microsoft...)

im wondering if the variables need an overhaul in this example. It seems that the intent of this was to have:

Code:

dim pix(0 To bmp.bmHeight, 0 To bmp.bmWidthBytes) As Byte
'pix(0,0)=red pix (0,1) = blue pix (0,2) = green



a seperate 255 byte for each color value on the bitmap, so in theory this would remove the need to convert each color from 3x 255 values into hex format (which would otherwise be converted into long for a vb color change useing .pset or setpixel) and display the changes, but the above code would need 2 nested for...next loops to transfer from picture1.picture to the variables (plus conversion code from a single long to 3 bytes) and that part seems to be done with this code:

Code:
.pvData = bmp.bmBits


which should be faster, is the tutorial is to be believed:
Quote:
This method allows really fast pixel manipulation, that could also be used for games.
I also created a table to show you the speed differences. The picture was 433 by 263 pixels large and has been inverted every time. My PC is a P2 MMX at 300 MHz and 96 MB RAM. Every test has been repeated five times. Then I calculated a middle of all the five tests, and here they are:

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

As you can see, the 'Pointer' method a lot faster than any other. That's why you should use it, when ever you need pixel plotting in you programs.


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 29, 2005 6:15 pm 
Also known as "Lucky"

Joined: Sat Aug 07, 2004 9:22 pm
Posts: 5853
Location: Vancouver, Canada
I'm afraid I can't really help much, as I work on Linux now, so I can't test out your code myself. It sounds like the call to CopyMemory itself is the problem... it might help if you read up on it:

http://msdn.microsoft.com/library/defau ... trings.asp

Google has more, if you search for VB CopyMemory.

Or maybe someone working on Windows will be kind enough to test out your code in VB :)


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 29, 2005 11:41 pm 
Prolific Poster

Joined: Fri May 27, 2005 9:12 pm
Posts: 16
thanks for the link and the point about copymemory. I'll lookinto the link and google's results. If i ever get this working, and if its faster (by this point im doubting the credibility of the test on that page) i'll reply it to this thread with the code.

i'm guessing once someone hits the limits of vb, they switch to c+ (which means all the interesting and technical open source projects (linux) suddenly become easier to understand and modify) because of how direct the control of the code and memory storage is.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 30, 2005 12:07 am 
Also known as "Lucky"

Joined: Sat Aug 07, 2004 9:22 pm
Posts: 5853
Location: Vancouver, Canada
Yes, having C++ knowledge certainly helps you navigate the world of Linux and open source.

If you're interested in getting started, we can help you get on your feet! The SDL library makes cross platform C++ game programming quite easy... and if you're into 3D, OpenGL (with SDL, or GLFW) is great.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 01, 2005 2:24 am 
Prolific Poster

Joined: Fri May 27, 2005 9:12 pm
Posts: 16
after reading up on the copy memory article, it seems the source article didnt test-run his code before posting it or it changed durring translation... its over my head anyway.

when it comes to codeing, im just screwing around in my spare time looking at tutorials and examples. What i have is the visual studio student kit (c++, foxpro, vb, and some others) and v.net student kit (c# and i forget the rest) When it comes to c-type programing, i've heard that there are many changes in syntax between different versions (c+, c++, c#, and 5 other versions).

a recomendation for a c type compiler (from either of the 2 i have or an open-source or free one) and some easy tutorials would be very helpful. the reasion i orginaly picked vb over c++ is microsofts tutorial for c++ (500 lines to display hello world calling 50 functions, declaring 20 api's) all for a little box with a single line of text. it seems a little excessive for a first app when a dos-box style run-time would've worked just fine and cut down on the code (probibly 490 of those lines are for generating the window box, way to much to throw at a newbie)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 01, 2005 3:59 am 
Also known as "Lucky"

Joined: Sat Aug 07, 2004 9:22 pm
Posts: 5853
Location: Vancouver, Canada
For getting started in C++ you may want to look at this thread:

http://gpwiki.org/forums/viewtopic.php?t=1343

Also, check out our wiki pages:

http://gpwiki.org/index.php/C:Online_Resources
http://gpwiki.org/index.php/C:Development_Environments

I'd suggest that you download Dev-C++ (free) and get started with the tutorials here. If you get stuck, let us know :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 5:36 am 
i think i found the error in the code above:

when you call copymemory (CopyMemory ByVal VarPtrArray(pic), VarPtrArray(sa), 4 ), the second parameter, "VarPtrArray(sa) should read "VarPtr(sa)"


Top
  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 4:53 am 
Prolific Poster

Joined: Fri May 27, 2005 9:12 pm
Posts: 16
woa, i thought for sure this thread was dead, i'll give it a try in the morning, thanks for the tip.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 2:11 pm 
Technomaniac
User avatar

Joined: Mon Aug 23, 2004 12:24 am
Posts: 3096
Location: Washington , USA
It is much faster. I must have missed this thread altogether. I used this in a little hieghtmap editor I made.

I also got that error, there's something weird you have to change but I don't have the heightmap editor source code anymore. :(

_________________
GMan
Black Ninja Games
Imagine ASCII art here!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 3:52 pm 
Prolific Poster

Joined: Fri May 27, 2005 9:12 pm
Posts: 16
8) 8) 8) it works!!!
its just a little mangeled code wise, but with a few easy changes, it works perfectly:

how i got it to work:

(thanks to whoever that guest is)
changed
Code:
CopyMemory ByVal VarPtrArray(pic), VarPtrArray(sa), 4


to
Code:
CopyMemory ByVal VarPtrArray(pic), VarPtr(sa), 4

from
Code:
Private Sub Command2_Click()
'invert image
For i = 0 To UBound(pic, 1)
    For j = 0 To UBound(pic, 2)
        pic(i, j) = 255 - pic(i, j)
    Next j
Next i
End Sub

to
Code:
Private Sub Command2_Click()
'invert image
For i = 0 To UBound(pic, 1)
    For j = 0 To UBound(pic, 2)
        pic(i, j) = 255 - pic(i, j)
    Next j
Next i
Picture1.Refresh
End Sub


and command3 to
Code:
Private Sub Command3_Click()
'change colors seprately
For i = 0 To UBound(pic, 1) - 3 Step 3
    For j = 0 To UBound(pic, 2)
        r = pic(i + 2, j)
        g = pic(i + 1, j)
        b = pic(i, j)
        r = ((g * b) \ 128)
        g = ((r * b) \ 128)
        b = ((r * g) \ 128)
        If r > 255 Then r = 255
        If r < 0 Then r = 0
        If g > 255 Then g = 255
        If g < 0 Then g = 0
        If b > 255 Then b = 255
        If b < 0 Then b = 0
        pic(i, j) = b
        pic(i + 1, j) = g
        pic(i + 2, j) = r
    Next j
Next i
Picture1.Refresh
End Sub


changeing VarPtrArray to VarPtr was the first step, putting in
Picture1.Refresh was the second. now it works 8)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 1:04 am 
Technomaniac
User avatar

Joined: Mon Aug 23, 2004 12:24 am
Posts: 3096
Location: Washington , USA
Ah yes, that was it....
:)

_________________
GMan
Black Ninja Games
Imagine ASCII art here!


Top
 Profile  
 
PostPosted: Wed Sep 28, 2011 3:48 pm 
Hey zombie, gd job at doing this but i solved the problem before looking at this thread the same way you did. Then i compared my code to yours and its almost the same (exept i dont cut the color to rgb).
Anyways, im having a small problem that all pic(i, j) return a "0". I have picture1 already and its full with colors. so im kinda confused here! Did it happen to you and you fixed it somehow? ill check back to see your reply. Thank you


Top
  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group