GUI And Graphics

BlitzPlus Forums/BlitzPlus Beginners Area/GUI And Graphics

_PJ_(Posted 2015) [#1]
Apparently Loading Images to use for display on Canvasses will result in memory corruption of Fonts unless there is a graphic driver initialised.

The GUI commands, however require a blitzhandle to a Window. In every example, these windows are created with CreateWindow() and handle returned, however, there doesn't appear to be any way to attach graphics mode to a window created in that manner.

The only alternative appears to be some means of utilising User32 somehow to obtain a Window Handle for which a Window created with "Graphics" command which can then be used with GUI commands;
since SystemProperty"AppHwnd" is not valid for B+ and QueryObject only seems to operate on Gadget objects.


How have others overcome such issues using Graphics with GUI commands???


Andy_A(Posted 2015) [#2]
I don't have regular internet access however, you need to set the buffer to the canvas of the parent window.

Example:
a) Create window
b) Create a canvas for said window. We'll call it 'myCanvas' in this example
c) Setbuffer Canvasbuffer (myCanvas) ;<--- omitting this is why your graphics don't work
d) Do your graphics stuff on 'myCanvas'
e) Don't forget to FlipCanvas (myCanvas) to update the graphics

(complete example on how to draw graphics within GUI here)
Code Archives: http://www.blitzbasic.com/codearcs/codearcs.php?code=2221


_PJ_(Posted 2016) [#3]
I don't think you understand. I know how to use canvases for lines/rects/ovals and writepixel etc.
Thats not at all what I mentioned in my post.

Nowhere did I state I had omitted a SetBuffer(CanvasBuffer()) command either so not sure why you feel that was the probelm.

The problem is, as I actuially described, in attaching a Graphics Mode to the window.

See the following for an example of the error.

Window=CreateWindow("A Window",0,0,256,128)
Menu=WindowMenu(Window)
Pulldown=CreateMenu("Pulldown",1,menu)
Canvas=CreateCanvas(0,0,128,64,window)

Image=LoadImage("F:\bb\B3D\Scrapbook\My Test Pieces\FilterDemo\Test2.jpg");("image.bmp")
Font=LoadFont("Blitz",12)
SetBuffer CanvasBuffer(canvas)
SetFont font
SetBuffer ImageBuffer(image)
Text 0,0,"Text"


The Unknown object is that graphical infrastructure which is necessary in order to manipulate (for example loaded font and loaded images) without causing corruption to the debugger.

The extent of the problem when there is no such graphical window is evident here:

http://www.blitzbasic.com/Community/posts.php?topic=104420#1265805

and as per Kryzon's Post:

BlitzPlus is now open source, so you have the luxury of checking how the functions work internally:
https://github.com/blitz-research/blitzplus/blob/master/blitz2d/b2dimage.cpp#L145
"LoadImage asks the graphics driver to create a graphics object, and the driver needs a graphics context to do this (that is, yes, a Graphics call is needed before loading or using images). "



Here:
http://www.blitzbasic.com/Community/posts.php?topic=104452#1265689

______________________________________________________________

So the only means to employ Loaded Fonts and Images is to initalise graphics mode, this always creates its own Window.
I need to be able to either assign the GUI Window handle to point to the Graphics mode window.
Hence my request: http://www.blitzbasic.com/Community/posts.php?topic=104603


grable(Posted 2016) [#4]
I just downloaded BlitzPlus 1.47 and tried your example and got no errors or graphics corruption...
Am i doing it wrong?

The code i ended up with:
Window=CreateWindow("A Window",0,0,256,256)
Menu=WindowMenu(Window)
Pulldown=CreateMenu("Pulldown",1,menu)
Canvas=CreateCanvas(0,0,256,256,window)

Image=LoadImage("c:\dump\test.png")
Font=LoadFont("Fixedsys",60) ; Blitz.fon did not register on Windows 10

SetBuffer ImageBuffer(image)
SetFont font
Text 0,0,"Text"

SetBuffer CanvasBuffer(canvas)
DrawImage image, 0,0

; needed to see result in debug mode
FlipCanvas canvas

WaitKey
After looking over the code, it seems that CreateCanvas() sets the global graphics context just like Graphics() does.


_PJ_(Posted 2016) [#5]
That's not my example.

Try this:
Window=CreateWindow("A Window",0,0,256,128)
Menu=WindowMenu(Window)
Pulldown=CreateMenu("Pulldown",1,menu)
Canvas=CreateCanvas(0,0,128,64,window)

Image=CreateImage(128,64)
Font=LoadFont("Fixedsys",60)
SetBuffer CanvasBuffer(canvas)
SetFont font
SetBuffer ImageBuffer(image)
Text 0,0,"Text"


Ensure the debugger is running.


grable(Posted 2016) [#6]
Aha, thats what i was missing lol.
I get an error on exit now "Attempt to release <unknown> object".

And with that reduced it down to
Image=CreateImage(128,64)
SetBuffer ImageBuffer(Image)
Sticking "SetBuffer CanvasBuffer(canvas)" at the end of your example does not produce an error at least.


_PJ_(Posted 2016) [#7]
Yes the "Attempt to Release" error comes from trying to release the "current" buffer as a graphicsbuffer, but when that is undefined.

It's not easy to identify the exact nature of the more serious problems that occur, but if you consider the following:

Window=CreateWindow("A Window",100,0,256,128)
Menu=WindowMenu(Window)
Pulldown=CreateMenu("Pulldown",1,menu)
Canvas=CreateCanvas(0,0,128,64,window)
Textarea=CreateTextArea(128,64,128,64,canvas)
Image=CreateImage(128,64)
Font=LoadFont("Fixedsys",60)
SetBuffer CanvasBuffer(canvas)
SetFont font
SetBuffer ImageBuffer(image)
Text 0,0,"Text"
DebugLog(FontName(font)+" THIS IS OKAY")

;DebugLog(FontName(font)+" THIS IS CORRUPTING")

SetTextAreaText textarea,"Text"
DebugLog(FontName(font)+" THIS IS OKAY")
SetBuffer CanvasBuffer(canvas)
WaitKey()


The Window is now positioned along a little basically so that it
is possible to see the Debugger output behind.
The WAITKEY() is introduced to prevent the screen from closing at compilation end.

Now, run the code as is an d all will appear perfectly as expected.

UNCOMMENT the commented line, and you will see the corruption in effect.

This is a simplistic example, but in a larger project it can be catastrophic.