My PB Woe's, need some help calling API stuff!

Windows specific forum
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Shagwana.

Hello everyone!, Right I want ot use PB to code myself a map editor (much like mappy / tile studio ) but have 'hit' a brick wall while trying. As of version 2.30 (for windows) there are no commands for loading / drawing images to windows so it all has to be done using the Windows API I guess! So my question is how can I acheive the following things using the Windows API, because Im not to hot on this stuff myself!;


1.) Create a image in memory?
2.) Load a bitmap (or other image file) to the above image?
3.) Be able to read and write pixels to the above image?
4.) Be able to cut/copy smaller images out of the given image above?
5.) Be able to draw any given image to the windows display, usign it as solid block (every pixel is drawn) and also using a single colour as see-through?
6.) Also to be able to free the above image in memory?




Anyway thanks in advanced!



http://www.sublimegames.com
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Mr.Skunk.

here is a small listing to draw a picture in memory if it can help you
;
; to create an image in memory (bitmap) and draw in it...
;

InitWindow(0)

If OpenWindow(0,100,100,200,200,#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget,"My WIndow")

*DC=GetDC_(WIndowID()) ; to retrieve the device context of the window
*DCM=CreateCompatibleDC_(*DC) ; to create a memory context device compatible with th context device of your window
; all your drawing operations will be in this device context.

*BMP=CreateCompatibleBitmap_(*DC,100,100) ; create a bitmap of 100*100
; the bitmap MUST be compatible with the COntext device, NOT the memory context device

*SelectObject_(*DCM,*BMP) ; select the bitmap into the memory device context
; all the operations you will make in the *DCM will be reported to the Bitmap

#Ps_Solid=0 ; (1)

brush.l=CreateSolidBrush_(255+(255*256)+(255*256*256)) ; (2)

*SelectObject_(*DCM,brush) ; need a brush to FILL a rectangle

Rectangle_(*DCM,0,0,100,100) ; draw a filled rectangle

pen.l=CreatePen_(#Ps_Solid,1,255+(0*256)+(0*256*256)) ; create a : solid pen with 1 pixel height and of pure red colour (2)

*Pen=SelectObject_(*DCM,pen.l) ; Select the pen if the memory context device

MoveToEx_(*DCM,10,10,0):LineTo_(*DCM,90,90) ; trace a ligne in the memory context device
; from 10,10 to 90,90
; you need to se DLL for the moment to draw in the device context

#SrcCopy=13369376 ; rule to copy the bitmap to it's destination
; THIS is a plain copy

BitBlt_(*DC,20,20,300,100,*DCM,0,0,#SrcCopy)
; BitBlit_(destination,xdest,ydest,ldest,hdest,source,xsrc,ysrc,copymode)

Repeat
Event=WaitWindowEvent()
Until Event=#PB_EventCloseWindow

DeleteObject_(*BMP)
DeleteObject_(pen)
DeleteObject_(brush)
ReleaseDC_(WindowID(),*DC)
DeleteDC_(*DC) ; to remove the context device from memory (important to free the ressources)

EndIf
End

;(1) Different types of Pen :
;--------------------------
; #Ps_Solid=0
; #Ps_Dash=1
; #Ps_Dot=2
; #Ps_DashDot=3
; #Ps_DashDotDot=4
; #Ps_Null=5
; #Ps_InsideFrame=6

;(2) How to code the colours :
;---------------------------
; It Works with RGB (Reg Green Blue)
; Red, Green and Blue are 0-255 range.
; colour=Red+(Green*256)+Blue(256*256)

But fred says me that soon, we don't need more api call for images and bitmaps :wink:
don't hesitate if you need more help, i must go work now...

bye

Mr Skunk
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Shagwana.

Thanks for your help, its pointed me in the right direction!. Now i know i need to create a compatible bitmap with the window so that it may be displayed and BitBlt can be used to copy chunks around!.

Now i still need to figure out how to load a bitmap to the compatible bitmap :(.
Also if BitBlt will let the users draw blocks, while leaving a single colour see-through!. Single colour of my choice as well

You might have noticed that windows API is not my thing! and any help is very welcome!!, Thanks again!.

PS) any idea as to when the next patch is out?

http://www.sublimegames.com
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Shagwana.

Hehe) Right no need to worry about loading bitmaps (as i have figured out how to do this). I have also figured out how to bitblt from the loaded bitmap to the display as well!. Can you see anything wrong with this like silly illeagle stuff? ......

----8<----
;This code is modifyed example of code sent by the author of PB
; --------------------------------------------------------------
; Quick error requester
; --------------------------------------------------------------
Procedure Error (a$)
MessageBox_ (0, a$, "Alert", 0) ;Display a error message in a message box!
EndProcedure

; --------------------------------------------------------------
; Open a window
; --------------------------------------------------------------
InitWindow(1)
InitFile (1)
InitRequester ()

; Can't return a custom structure -- gotta read width and height separately!
;This will read a bitmaps width
Procedure.l ReadBMPWidth (bmp$)
If ReadFile (0, bmp$)
FileSeek (18)
width.l = ReadLong ()
CloseFile (0)
EndIf
ProcedureReturn width
EndProcedure

;This will return a bitmaps height
Procedure.l ReadBMPHeight (bmp$)
If ReadFile (0, bmp$)
FileSeek (22)
height.l = ReadLong ()
CloseFile (0)
EndIf
ProcedureReturn height
EndProcedure

; ------------------------------------------------------------
; Load an image
; ------------------------------------------------------------

a$ = OpenFileRequester ("Select BMP:", "My Documents\", "Bitmap files|*.bmp", 0)
If a$
bmpw.l = ReadBMPWidth (a$)
bmph.l = ReadBMPHeight (a$)
Else
Error ("No image selected!")
End
EndIf

*win = OpenWindow (1, 0, 0, bmpw+20, bmph+20, #PB_Window_SystemMenu, "Test")

If *win
*result = LoadImage_ (0, a$, #IMAGE_BITMAP, 0, 0, #LR_LOADFROMFILE)
If *result
*dc=GetWindowDC_(WindowID())
*DCM=CreateCompatibleDC_(*dc)
*SelectObject_(*DCM,*result)
ReleaseDC_(WindowID(), *dc)

Repeat
Select WaitWindowEvent()

Case #PB_EventCloseWindow
End
Case #PB_EventRepaint
;Window requires a repaint!

*dc=GetWindowDC_(WindowID())
For x=0 To 4
For y=0 To 4
xpos=(x*32)+32
ypos=(y*32)+32
BitBlt_(*dc,ypos,xpos,32,32,*DCM,xpos,ypos,#SRCCOPY) ;Will copy loaded bitmap to window!
Next
Next
ReleaseDC_(WindowID(), *dc)
EndSelect
ForEver
DeleteObject_(*result)
EndIf
EndIf
End


----8<-----


Now all i need to figure out is how to draw masked blocks to the window (transparent parts!), my guess is using MaskBlt() function. Any help would be welcomed with open arms!

http://www.sublimegames.com

Edited by - Shagwana on 13 June 2001 12:39:01
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Mr.Skunk.

i looked at MaskBlt() function, but sorry, it's over my knowledge...

Mr Skunk
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Mr.Skunk.

2 things.
You did not use DELETEDC_ to free resources, and the bitmap of your image is never erased from the memory (when you catch eventclosewindow you use directely END)

Mr Skunk
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Shagwana.

Thanks alot for that!
Anyone else know how MaskBlt() works?

http://www.sublimegames.com
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Mr.Skunk.

look here :
http://www.codeproject.com/bitmap/
there is explanations and tutorials about bitmaps and different ways to do transparent bitmaps

Mr Skunk
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Mr.Skunk.

I dDon't know if you want absolutely use MaskBlt, either there is an other commande wich seems to be easier :TransparentBlt .It's a simply blit and you can choose a transparent colour.

here:
http://msdn.microsoft.com/library/psdk/ ... s_2y9g.htm


Mr Skunk
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Shagwana.

/me hits head and does a hommer simpson DOH!

Thanks for that, and it does indeed seam to do the job I want!.

On another note, any chance of renaming the forums to 'Shags and Shunkys' place? (just joking!).

http://www.sublimegames.com
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Shagwana.

After playing around with the above command, i have hit another problem

I just cant seam to call it full stop!. Any ideas why....

Have tryied;

TransparentBlt_()
TransparentBlt()

I looks like its the function I need, but now i just cant use it

http://www.sublimegames.com
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Mr.Skunk.

Oupsss, it's my fault. TransparentBlt doesn't seems to be included in PB definitions.
The only way to use it for the moment is to call it with assembly.
I'll try to do something tomorrow morning (must go to work for this night )
I Don't promise i'll be able to code it but i'll try (6 days learning assembly)..

AND... It's NOT sHunky, but sKunky

Mr Skunk
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Mr.Skunk.

Sorry, but i've a problem with the API call in libraries.
Trying to find the solution...

Mr Skunk
Post Reply