Is PureBasic good for interactive 2D graphics applications?

Everything else that doesn't fall into one of the other PB categories.
ifmihai
New User
New User
Posts: 5
Joined: Tue Oct 14, 2014 3:32 pm

Is PureBasic good for interactive 2D graphics applications?

Post by ifmihai »

First of all, hi,
I'm new here

Before the actual question,
What I like about PB is: multi-platform and basic language

Now, what I want
is to create mini apps for myself mostly, research purposes, which can maybe grow into bigger applications.

but I need the possibility to draw interactive objects (circles, rectangles, lines, etc)
that can be receive events (that can be clicked, dragged around, etc)

The toolkit which seems closest to my needs, is Qt framework (QGraphicsScene, QGraphicsView, QGraphicsItem),
but it's c++, and... ughhh :P
It has a port in python (my language of choice), but python is slow at these things (in my perception at least)

I'm aware of q7basic, basic for Qt, which could be great, but,
from what I read here, it's not so good (compile times, updates, support, etc)

Back to PB,
I browsed the docs, I did a little browsing in this forum,
but it's not so clear if interactivity is provided

It seems to me drawing in PB is like in delphi's panel, painting only, which is too primitive for me
But I'm not sure if I'm right, I didn't go too deep

I wanted to ask here because it gets too much time now to find the answer by myself,
and I suppose this is a trivial question for average PB users
hence the question :)

Thanks
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6175
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Is PureBasic good for interactive 2D graphics applicatio

Post by blueznl »

Drawn objects are, by their nature, not interactive. They aren't objects as such, but they are parts of a bitmap that is being drawn upon.
( 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... )
Little John
Addict
Addict
Posts: 4869
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Is PureBasic good for interactive 2D graphics applicatio

Post by Little John »

Hi ifmihai,

welcome on board! :-)

I think you should have a close look at PB's CanvasGadget.
Reference Manual wrote:Create a canvas gadget in the current GadgetList. This gadget provides a drawing surface and events for mouse and keyboard interaction to easily create custom views.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Is PureBasic good for interactive 2D graphics applicatio

Post by IdeasVacuum »

Here is a good example of what can be done with the Canvas Gadget:
MindMap
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
ifmihai
New User
New User
Posts: 5
Joined: Tue Oct 14, 2014 3:32 pm

Re: Is PureBasic good for interactive 2D graphics applicatio

Post by ifmihai »

thank you for pointing out canvasgadget, Little John
and thank you IdeasVacuum for MindMap link

I browsed both superficially (i've given more attention to canvas widget)

It's still not clear to me if events are passed down to shapes in the canvas

in the following scenario:

1. add canvas widget
2. add a circle into it
3. mouse click on canvas widget

is this event transmitted to the circle drawn?
can circle shape (or object) receive events?

Or circles and other basic shapes are drawn through GDI, painted only on the canvas?

Another way to ask is:
Do I have to implement something to know if user clicked on the circle?
Or is this functionality already into canvas widget ?
User avatar
idle
Always Here
Always Here
Posts: 6238
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Is PureBasic good for interactive 2D graphics applicatio

Post by idle »

no you would have to use hit tests for the various shapes or regions on mouse clicks
here's a very crude example, right click to create a circle, left click to move

Code: Select all

EnableExplicit 

Structure sCircle
  r.i
EndStructure 

Structure sBox 
  w.i
  h.i
EndStructure   

Structure objects 
  type.i 
  x.i
  y.i
  color.i
  StructureUnion 
    scircle.scircle
    sbox.sbox 
  EndStructureUnion 
EndStructure 

#circle =1
#box =2 

Global NewList objects.objects()  
Global *Obj.objects 
Global Event 

Procedure ReDraw()
  If StartDrawing(CanvasOutput(0))
    Box(0,0,GadgetWidth(0),GadgetHeight(0),RGB(255,255,255)) 
    ForEach objects() 
      Select objects()\type 
        Case #circle 
          Circle(objects()\x,objects()\y,objects()\scircle\r,objects()\color)
        Case #box 
          Box(objects()\x,objects()\y,objects()\sbox\w,objects()\sbox\h,objects()\color)
      EndSelect 
    Next 
    StopDrawing()
  EndIf   
EndProcedure  

Procedure HitTest() 
  Protected x,y,dx.f,dy.f,d.f
  Protected *tmp = LastElement(objects()) 
  
  x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
  y = GetGadgetAttribute(0, #PB_Canvas_MouseY)
  
  ForEach objects() 
    Select objects()\type 
      Case #circle 
        dx.f = x - objects()\x   
        dy.f = y - objects()\y  
        d.f = Sqr(dx*dx+dy*dy) 
        If d < objects()\scircle\r 
          SwapElements(objects(),@objects(),*tmp)
          *obj = @objects() 
          Break 
        EndIf 
      Case #box 
        If (x > objects()\x And x < objects()\x+objects()\sbox\w And y > objects()\y And y < objects()\y + objects()\sbox\h) 
          SwapElements(objects(),@objects(),*tmp)
          *obj = @objects() 
          Break 
        EndIf 
    EndSelect 
  Next 
EndProcedure  

Procedure MouseMove()
  Protected x,y 
  If *obj 
    x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
    y = GetGadgetAttribute(0, #PB_Canvas_MouseY) 
    Select *obj\type 
      Case #circle   
        *obj\x = x 
        *obj\y = y 
      Case #box 
        *obj\x = x - (*obj\sbox\w * 0.5)
        *obj\y = y - (*obj\sbox\h * 0.5) 
    EndSelect   
    ReDraw() 
  EndIf   
EndProcedure   

Procedure MouseUP()
  *obj = 0 
EndProcedure

Procedure MouseRightClick()
  Protected type,x,y 
  If StartDrawing(CanvasOutput(0))
    x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
    y = GetGadgetAttribute(0, #PB_Canvas_MouseY)
    AddElement(objects())
    type = Random(2,1)
    Select type 
      Case #circle    
        objects()\type = #circle 
        objects()\x = x 
        objects()\y = y 
        objects()\scircle\r = 10 
        objects()\color = RGB(Random(255), Random(255), Random(255))
        Circle(x, y, 10, objects()\color)
      Case #box 
        objects()\type = #box 
        objects()\x = x 
        objects()\y = y 
        objects()\sbox\w = 20
        objects()\sbox\h = 20 
        objects()\color = RGB(Random(255), Random(255), Random(255))
        Box(x,y,20,20,objects()\color)
    EndSelect    
    StopDrawing()
  EndIf
EndProcedure   

If OpenWindow(0, 0, 0, 220, 220, "CanvasGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CanvasGadget(0, 10, 10, 200, 200)
  BindGadgetEvent(0,@HitTest(),#PB_EventType_LeftButtonDown)
  BindGadgetEvent(0,@MouseMove(),#PB_EventType_MouseMove)
  BindGadgetEvent(0,@MouseUP(),#PB_EventType_LeftButtonUp)
  BindGadgetEvent(0,@MouseRightClick(),#PB_EventType_RightClick)
  Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow
EndIf
Windows 11, Manjaro, Raspberry Pi OS
Image
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Is PureBasic good for interactive 2D graphics applicatio

Post by IdeasVacuum »

You could also try using canvas gadgets that represent your graphical items - for example a circle can be drawn on a transparent canvas and that canvas can be user-resized. This is somewhat similar to how a Form Designer works.
Code Example: http://www.purebasic.fr/english/viewtop ... 52&start=0
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: Is PureBasic good for interactive 2D graphics applicatio

Post by electrochrisso »

@idle: Might be crude, but a nice example. 8)
PureBasic! Purely the best 8)
ifmihai
New User
New User
Posts: 5
Joined: Tue Oct 14, 2014 3:32 pm

Re: Is PureBasic good for interactive 2D graphics applicatio

Post by ifmihai »

Now it's clear.
Thank you very much for the example and clarification.

Then i will pass PB, as i consider these as basic requirements.

Im not into reinventing the wheels,

But why isnt there a more advanced canvas?
Maybe it's because gtk?

I read somewhere here in the forum,
When reading about PB vs q7basic, and some user(s) suggested replacing Gtk with qt
Providing qt capabilities in a basic language would be perfect.

Thank you again.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Is PureBasic good for interactive 2D graphics applicatio

Post by IdeasVacuum »

Providing qt capabilities in a basic language would be perfect.
....it might be if you can afford to pay 3195 Euros plus an annual renewal fee of 985 Euros :mrgreen:
However, I don't think QT actually delivers what you want. Their 'dynamic objects' is reference to adding gadgets/widgets/controls at runtime, something that PB is very good at.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Is PureBasic good for interactive 2D graphics applicatio

Post by Tenaja »

ifmihai wrote:Now it's clear.
Thank you very much for the example and clarification.

Then i will pass PB, as i consider these as basic requirements.

Im not into reinventing the wheels,
I am not aware of any compiler that does exactly what you have described. Idle has provided a great PB example, but if there is something similar with a lot less code, I'd love to see it. If you find one capable, please come back here and share it with us, as I have a similar project in mind.

Thanks.
Last edited by Tenaja on Thu Oct 16, 2014 2:17 pm, edited 1 time in total.
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: Is PureBasic good for interactive 2D graphics applicatio

Post by heartbone »

ifmihai wrote:Is PureBasic good for interactive 2D graphics applications?
Yes, it is excellent for creating them.
First of all, hi,
I'm new here
Hi fellow creator.
Before the actual question,
Then what was the title about??? :?:
What I like about PB is: multi-platform and basic language

Now, what I want
is to create mini apps for myself mostly, research purposes, which can maybe grow into bigger applications.
Nice idea.
but I need the possibility to draw interactive objects (circles, rectangles, lines, etc)
that can be receive events (that can be clicked, dragged around, etc)
That possibility certainly exists in PureBasic.
The toolkit which seems closest to my needs, is Qt framework (QGraphicsScene, QGraphicsView, QGraphicsItem),
but it's c++, and... ughhh :P
I commiserate with you there. UGGGH.
It has a port in python (my language of choice), but python is slow at these things (in my perception at least)

I'm aware of q7basic, basic for Qt, which could be great, but,
from what I read here, it's not so good (compile times, updates, support, etc)
I like Python as well, but the high level constructs means loss of fine control.
Back to PB,
I browsed the docs, I did a little browsing in this forum,
but it's not so clear if interactivity is provided
Interactivity with program created objects is provided the programmer.
It seems to me drawing in PB is like in delphi's panel, painting only, which is too primitive for me
But I'm not sure if I'm right, I didn't go too deep

I wanted to ask here because it gets too much time now to find the answer by myself,
and I suppose this is a trivial question for average PB users
hence the question :)

Thanks
PureBasic is a programming language application.
It seems clear to me that what you are asking for is an application that instantiates an environment for user manipulatible objects.
That also seems awfully close to the description of a generic 2D game creator.
You will have better results in finding what you seek in a search looking for a 2D game creator.

Not trying to be too picky with you, but:
1) There really are no average PB users in this forum's membership.
2) You never got around to asking a question in your post. :)
ifmihai wrote:Now it's clear.
Thank you very much for the example and clarification.

Then i will pass PB, as i consider these as basic requirements.

Im not into reinventing the wheels,
Seems to me to be more like reinventing the transmission and power steering.
Keep it BASIC.
ifmihai
New User
New User
Posts: 5
Joined: Tue Oct 14, 2014 3:32 pm

Re: Is PureBasic good for interactive 2D graphics applicatio

Post by ifmihai »

@tenaja
By compiler, you mean basic compiler?
Im not aware of any, searching though.

Its quite trivial to do in python through default tkinter canvas, and through pyqt, qt port to python.
But, i dont want python for this.

Another way is javascript way, there are quite a few options there. But. Its hellavascript :P with all its hellish friends, html and css :D


@heartbone
Thank you for the idea with 2d game creator. Seems like a good idea.
Sorry about ‘average’. I dont know if anyone felt hurt by it.
I wanted to say: almost all programmers here could know about drawing capabilities of PB. (English is not my main language)
User avatar
idle
Always Here
Always Here
Posts: 6238
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Is PureBasic good for interactive 2D graphics applicatio

Post by idle »

ifmihai wrote:Now it's clear.
Thank you very much for the example and clarification.

Then i will pass PB, as i consider these as basic requirements.

Im not into reinventing the wheels,

But why isnt there a more advanced canvas?
Maybe it's because gtk?

I read somewhere here in the forum,
When reading about PB vs q7basic, and some user(s) suggested replacing Gtk with qt
Providing qt capabilities in a basic language would be perfect.

Thank you again.
What you're essentially asking for is a scene graph with drawing primitives. something like the clutter project.
A scene graph is fairly trivial to set up in PB to provide the functionality you want, though you may need to add missing
2D primitives like arc and splines primitives, though there are numerous implementations of those on the forum
so again it'd be fairly easy to add them.
It's not really a lot of work to get the basic functionality you asked for but perhaps not what you'd want to be doing
being new to the language.

So perhaps if there's an interest from a few more people, I'll write an extensible oo runtime scene graph framework
which users can contribute to, with the aim to support rendering targets for images, canvas, sprites, screens and later opengl gadget
which will provide 2D primitives, Zorder, events, actions, callbacks, runtime, cache, garbage collection.
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: Is PureBasic good for interactive 2D graphics applicatio

Post by heartbone »

OT:
ifmihai wrote:@heartbone
Thank you for the idea with 2d game creator. Seems like a good idea.
Sorry about ‘average’. I dont know if anyone felt hurt by it.
I wanted to say: almost all programmers here could know about drawing capabilities of PB. (English is not my main language)
ifmihai, I am surprised that English is not your native language because your post was so well written.
Now I feel like a jerk about giving you a hard time about your post.
Although I knew the idea that you were expressing from your original posting, the ambiguity of English allowed me to intentionally misinterpret your post to nitpick.
I apologize for doing that.

end OT

Good luck on finding a solution ifmihai.
Since you have a forum account, after you've made your final decision I hope that you will post your finding.
Keep it BASIC.
Post Reply