survival guide - graphics 1 - bitmaps, blcokcopy, winapi

Share your advanced PureBasic knowledge/code with the community.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

survival guide - graphics 1 - bitmaps, blcokcopy, winapi

Post by blueznl »

Code updated For 5.20+

the following might be good for people like me struggling with the basics...

Code: Select all


; purebasic survival guide (http://www.xs4all.nl/~bluez)
; graphics 1: bitmaps, blockcopy, winapi
; 31.10.2003 ejn (blueznl)
;
; this code shows some alternatives for the build in commands of purebasic, using:
;
; getdc_()
; loadimage()
; createcompatiblebitmap_()
; loadimage_()
; createcompatibledc_()
; selectobject_()
; deletedc_()
; releasedc_()
;
; plgblt_() is an api call that can transform and rotate objects (as a variation of
; bitblt), it does not run on all variations of windows
;
; note: plgblt doesn't directly output on screen, unless the debugger is switched off (!)
; however it can be used using an intermediate step
;
main_whnd = OpenWindow(0,200,100,660,500,"Test",#PB_Window_SystemMenu)
main_dc = GetDC_(WindowID(0))
;
; creating your own device independent bitmap
;
; note: creating a bitmap with the wrong number of colours (aka. not compatible / identical
; with current screen) doesn't display it when using DrawImage()
;
;   bitmap_hnd.l = createbitmap_(10,10,1,32,0)
;
; or use a compatible bitmap
;
;   bitmap_hnd.l = createcompatiblebitmap_(main_hdc,10,10)
;
; or load a bitmap (which, funny enough, doesn't have to be compatible) appearently the
; bitmap and / or structure generated is not identical to a createbitmap_() yet no dc
; is given during the call, so how does windows know how to (re)format the image? i'm missing
; something here... but it works anyway :-)
;
;   bitmap_hnd.l = LoadImage_(0,"..\_projects\zm24.bmp",#IMAGE_BITMAP,0,0,#LR_LOADFROMFILE|#LR_CREATEDIBSECTION)
;
; but let's stick to the regular purebasic loadimage command :-)
;
bitmap1_hnd.l = LoadImage(0,"1.bmp")
bitmap_w = ImageWidth(0)
bitmap_h = ImageHeight(0)
;
bitmap2_hnd = CreateImage(1,bitmap_w,bitmap_h)
;
; structure needed for plgblt_()
;
Structure corners
  x1.l            ; left upper corner
  y1.l           
  x2.l            ; right upper corner
  y2.l
  x3.l            ; left lower corner
  y3.l
EndStructure
;
new.corners
new\x1 = bitmap_w
new\y1 = 0
new\x2 = bitmap_w
new\y2 = bitmap_h
new\x3 = 0
new\y3 = 0
; 
; create device contexts (placeholders) and select images into them
;
source_dc = CreateCompatibleDC_(main_dc)
SelectObject_(source_dc,bitmap1_hnd)
dest_dc = CreateCompatibleDC_(main_dc)
SelectObject_(dest_dc,bitmap2_hnd)
;
;
; plgblt allows rotation and transformation
;
;   PlgBlt_(dest_dc,@new\x1,source_dc,0,0,bitmap_w,bitmap_h,0,0,0)
;
; bitblt is a block copy but with a parameter that affects how the copy is handled
;
BitBlt_(dest_dc,0,0,bitmap_w,bitmap_h,source_dc,0,0,#SRCCOPY)
;
;
DeleteDC_(source_dc)
DeleteDC_(dest_dc)
;
; draw the results (from bitmap 2) into the screen
;
;   UseWindow(0)
;   StartDrawing(WindowOutput())
;   DrawImage(bitmap2_hnd,10,10)
;   StopDrawing()
;
; you could do it with bitblt as well...
;
;   UseWindow(0)
;   dest_dc = StartDrawing(WindowOutput())
;   source_dc = CreateCompatibleDC_(main_dc)
;   SelectObject_(source_dc,bitmap2_hnd)
;   BitBlt_(dest_dc,10,10,bitmap_w,bitmap_h,source_dc,0,0,#SRCCOPY)
;   deletedc_(source_dc)
;   StopDrawing()
;
; however, drawing directly on screen means it's not protected, and can be overwritten
; so either copy it back using bitblt on every redraw or using a callback, or...
; use an imagegadget
;
ImageGadget(0,10,10,bitmap_w,bitmap_h,bitmap2_hnd)
;
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
;
FreeGadget(0)
ReleaseDC_(main_whnd,main_dc)


( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post by fsw »

Keep in mind that from a logical standpoint it's better to write:

Code: Select all

PlgBlt_(dest_dc,@new,source_dc,0,0,bitmap_w,bitmap_h,0,0,0)
than:

Code: Select all

PlgBlt_(dest_dc,@new\x1,source_dc,0,0,bitmap_w,bitmap_h,0,0,0)
Parameter 2 points to an array in general, not to the first element of the array.

@new\x1 works now too (because the address of the array is the same as the address of the first element of the array) but is not necessary.

Anyhow, keep up the good work :wink:

I am to provide the public with beneficial shocks.
Alfred Hitshock
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

... except that i found a bug, the f!@#$%^&*()! debugger screws around with plgblt_()

work for fred, i guess...
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Post Reply