Help with CGContext

Mac OSX specific forum
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Help with CGContext

Post by coder14 »

Can someone please point out where I've gone wrong here?

Code: Select all

EnableExplicit

#MainWindow=1

Structure CGPoint
  x.f
  y.f
EndStructure

Structure CGSize
  width.f
  height.f
EndStructure

Structure CGRect
  origin.CGPoint
  size.CGSize
EndStructure

Define clipRect.CGRect, cg.l, *wPort, OSStat.L

ImportC ""
  GetWindowPort(*WindowRef)
  QDBeginCGContext(*WindowPtr, *CGContextRef)
  QDEndCGContext(*WindowPtr, *CGContextRef)
  CGContextSetRGBFillColor(*CGContextRef, r.f, g.f, b.f, a.f)
  CGContextFillRect(*CGContextRef, *fillArea.CGRect)
  CGContextFlush(*CGContextRef) 
  CGMakeRect(x.f, y.f, width.f, height.f)  
EndImport

OpenWindow(#MainWIndow, 0, 0, 500, 500, "Carbon CGTest")

;clipRect=CGMakeRect(0, 0, 600, 600) 
; --> CGRectMake not found/doesn't work!?
; --> manually populating the CGRect structure

With clipRect
  \origin\x=10
  \origin\y=10
  \size\height=300
  \size\width=300
EndWith

*wPort = GetWindowPort(WindowID(#MainWindow))
OSStat = QDBeginCGContext(*wPort, @cg)
CGContextSetRGBFillColor(cg, 0.8, 0.5, 0.2, 1.0)
CGContextFillRect(cg, clipRect)
CGContextFlush(cg)
QDEndCGContext(*wPort, @cg)

Repeat
  Event = WindowEvent()
Until event = #PB_Event_CloseWindow
End
I'll be grateful for any assistance.

Thank you.
User avatar
Shardik
Addict
Addict
Posts: 2076
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Help with CGContext

Post by Shardik »

coder14 wrote:Can someone please point out where I've gone wrong here?
After wilbert has solved the mysterious problem with obtaining a blank
sheet when printing by simply changing

Code: Select all

CGContextFillRect(*CGContextRef, *fillArea.CGRect)
against

Code: Select all

CGContextFillRect(*CGContextRef, x.f, y.f, w.f, h.f)
this problem is suddenly solved easily:

Code: Select all

EnableExplicit

#MainWindow=1

Define cg.l, Event.l, *wPort, OSStat.L

ImportC ""
  GetWindowPort(*WindowRef)
  QDBeginCGContext(*WindowPtr, *CGContextRef)
  QDEndCGContext(*WindowPtr, *CGContextRef)
; CGContextFillRect(*CGContextRef, *fillArea.CGRect)
  CGContextFillRect(*CGContextRef, x.f, y.f, w.f, h.f)
  CGContextSetRGBFillColor(*CGContextRef, r.f, g.f, b.f, a.f)
  CGContextFlush(*CGContextRef)
EndImport

OpenWindow(#MainWIndow, 100, 100, 500, 500, "Carbon CGTest")

*wPort = GetWindowPort(WindowID(#MainWindow))
OSStat = QDBeginCGContext(*wPort, @cg)
CGContextSetRGBFillColor(cg, 0.8, 0.5, 0.2, 1.0)
CGContextFillRect(cg, 10.0, 10.0, 300.0, 200.0)
CGContextFlush(cg)
QDEndCGContext(*wPort, @cg)

Repeat
  Event = WindowEvent()
Until event = #PB_Event_CloseWindow
End
Unfortunately I was on the wrong track assuming that we need a composited
window and a HIView and was experimenting the whole weekend. But thanks
to wilbert it's so astonishingly easy... :lol:
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: Help with CGContext

Post by coder14 »

Yeah! Wilbert's breakthrough will have a wide impact on the use of Carbon's API functions from PB.
Post Reply