Page 1 of 2

Text and 2D graphics window

Posted: Fri Feb 25, 2011 6:08 am
by spiral72
I really, really tried to figure this out myself.... It's just not working for me.

This is the graphics portion of a larger program I'm working on. This routine just isn't working properly. I tried to simplify it so what I have left is:

;---------------------------[ Initial setup ]--------------------------------------------------------
OpenWindow(0, 1000, 190, 320, 320, "PING plot", #PB_Window_SystemMenu)
CreateImage(0, 320, 320)
StartDrawing(WindowOutput(0))
ImageGadget(0, 0, 0, 320, 320, ImageID(0))

OpenConsole() ; Open a window to display the program status
EnableGraphicalConsole(1) ; Open graphics mode for the ConsoleLocate command below
ConsoleTitle ("Matt's - COM test program") ; Set the window title


;---------------------------[ Main program loop ]----------------------------------------------------
MAIN:
Box(0,0,320,320,RGB(0,0,0)) ;clear the screen
;---------------------------[ Input servo position ]-------------------------------------------------
If OpenConsole()
Print("Start angle? ")
SVA = Val(Input())
EndIf

x=0
buff=100
For x=SVA To SVA+100
buff=100
StartDrawing(WindowOutput(0))
Plot(160+buff*Cos(x*3.14159/180), 160+buff*Sin(x*3.14159/180), RGB(255, 255, 255))
PrintN(Str(x)+" ")
Next x

If WaitWindowEvent() = #PB_Event_CloseWindow ; If the user has pressed on the window close button
Goto endit
EndIf

Delay(2000)
ClearConsole()
Goto MAIN


;---------------------------[ Exit To system ]-------------------------------------------------------
endit:
StopDrawing()
CloseConsole()
End



What I hoped to accomplish is open a text window to print data and accept inputs. I also want a graphics window purely for plotting data real-time. I DON'T want to draw the whole screen then flip pages to view it like all the examples I'm seeing.

This stupid little program inputs a starting angle from the user on the text screen. Then it plots an arc from the starting point, plus 100 degrees, then waits.....clears the screen by drawing a black box over everything and returns to accept another input.

It sort of works. It'll plot the first arc then it flakes out. Sometimes it clears the screen, sometimes not, sometimes it will plot the second arc you enter, sometimes not..... and NEVER the third time :(

Help?? Please?? I'm very frustrated.

Re: Text and 2D graphics window

Posted: Fri Feb 25, 2011 8:20 am
by IdeasVacuum
It seems a very strange thing to do spiral72.

If you need that Graphics Window, drop the Console! No reason why you should not have an input box (string gadget or spin gadget) on the Window, or if the real estate cannot be spared use a Message Requester. Also, you initialise an Image Gadget but you are not drawing on an image?

Edit: Something like the Help example (you will need a white 500 x 400 bitmap image saved as 'C:\White500x400.bmp':

Code: Select all

#Window0   = 0
#BtnDraw   = 1
#BtnExit   = 2
#Input     = 3
#ImageGdg  = 4
#Image     = 5

Procedure DrawArc(iSVA)
;----------------------
   x = 0
buff = 100
               If StartDrawing(ImageOutput(#Image))

                           BackColor(RGB(255,255,255)) 
                          FrontColor(RGB(255,0,0))

                         DrawingMode(#PB_2DDrawing_Default)
                                 Box(1,1,498,398,RGB(255,255,255)) ; Clear previous drawing

                         DrawingMode(#PB_2DDrawing_Outlined)


                         For x = iSVA To (iSVA + 100)
                  
                                 Plot(160 + buff * Cos(x * 3.14159/180), 160 + buff * Sin(x * 3.14159/180))
                         Next
                  
                         DrawText(5,380,"Arc @ " + Str(iSVA))

                         StopDrawing()
                         SetGadgetState(#ImageGdg,ImageID(#Image))
               EndIf

EndProcedure

Procedure ShowForm()
;-------------------

  If OpenWindow(#Window0, 0, 0, 600, 500, "Draw Something",  #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered)

                ButtonGadget(#BtnDraw,   0,  0,  60,  24, "Draw", #PB_Button_Default)
                                                          ;Min-Max
                  SpinGadget(#Input,    65,  0,  50,  24, 100, 300, #PB_Spin_Numeric)
                ButtonGadget(#BtnExit, 540,  0,  60,  24, "Exit")


                  If (LoadImage(#Image,"C:\White500x400.bmp"))

                          ImageGadget(#ImageGdg, 50, 50, 500, 400, ImageID(#Image))

                  EndIf
                 

               GadgetToolTip(#BtnDraw, "Draw an arc")
               GadgetToolTip(#BtnExit, "Exit App")
             SetGadgetState (#Input, 100) : SetGadgetText(#Input, "100") ;initial value

  EndIf

EndProcedure

; #### EVENTS ####

ShowForm()

Repeat ; Start of the event loop

            iEvent = WaitWindowEvent(1)
         iGadgetID = EventGadget()

         If iEvent = #PB_Event_Gadget

               If     iGadgetID = #BtnDraw

                                     iUserInputVal.i = GetGadgetState(#Input)
                                     DrawArc(iUserInputVal)

               ElseIf iGadgetID = #BtnExit

                                     iEvent = #PB_Event_CloseWindow

               EndIf

         EndIf

Until iEvent = #PB_Event_CloseWindow ; End of the event loop

End
One advantage of drawing on an image is that it is easy to save.

Re: Text and 2D graphics window

Posted: Fri Feb 25, 2011 12:47 pm
by Trond
If you have a window opened, you need to have an event loop running. This means you can't use Input() from the same thread, since this function blocks until the user inputs something (essentially stalling the event loop).

Re: Text and 2D graphics window

Posted: Fri Feb 25, 2011 9:18 pm
by Nituvious
Is this what you are looking for?

Code: Select all

;---------------------------[ Initial setup ]--------------------------------------------------------
OpenWindow(0, 1000, 190, 320, 320, "PING plot", #PB_Window_SystemMenu)
CreateImage(0, 320, 320)

ImageGadget(0, 0, 0, 320, 320, ImageID(0))

OpenConsole() ; Open a window to display the program status
EnableGraphicalConsole(1) ; Open graphics mode for the ConsoleLocate command below
ConsoleTitle ("Matt's - COM test program") ; Set the window title


;---------------------------[ Main program loop ]----------------------------------------------------
MAIN:

StartDrawing(WindowOutput(0))
Box(0,0,320,320,RGB(0,0,0)) ;clear the screen

StopDrawing()
;---------------------------[ Input servo position ]-------------------------------------------------
If OpenConsole()
Print("Start angle? ")
SVA = Val(Input())
EndIf

x=0
buff=100
For x=SVA To SVA+100
buff=100
StartDrawing(WindowOutput(0))
Plot(160+buff*Cos(x*3.14159/180), 160+buff*Sin(x*3.14159/180), RGB(255, 255, 255))
PrintN(Str(x)+" ")
StopDrawing()
Next x

If WaitWindowEvent() = #PB_Event_CloseWindow ; If the user has pressed on the window close button
Goto endit
EndIf

Delay(2000)
ClearConsole()
Goto MAIN


;---------------------------[ Exit To system ]-------------------------------------------------------
endit:
;StopDrawing() 
CloseConsole()
End

[Edit] SORRY! I just reread your whole post again. You would probably want to use threads for this task. It would give you the asynchronous'ness you want.

Re: Text and 2D graphics window

Posted: Sat Feb 26, 2011 12:46 am
by Zach
In Purebasic help, look at the Keyboard link under 2D Games & Multimedia Libraries

Also under the General Libraries section, Console -> look up Inkey / Rawkey instead of Input. Since you are using the Console library, basing your input parsing routine on the Inkey() command should work better for you, as it will not halt the rest of the program.

Re: Text and 2D graphics window

Posted: Sun Feb 27, 2011 5:10 am
by spiral72
Thank you for the suggestions. I wanted to let you know I'm still interested, just working a LOT since posting this. I hope to get back to it tomorrow night.

Re: Text and 2D graphics window

Posted: Sun Feb 27, 2011 3:39 pm
by spiral72
Trond wrote:If you have a window opened, you need to have an event loop running. This means you can't use Input() from the same thread, since this function blocks until the user inputs something (essentially stalling the event loop).
I understand that the Input() stalls program execution. I DON'T understand why is this undesirable in a program with a window opened in the "Same thread" as you say.

Re: Text and 2D graphics window

Posted: Sun Feb 27, 2011 4:01 pm
by spiral72
IdeasVacuum wrote:It seems a very strange thing to do spiral72.

If you need that Graphics Window, drop the Console! No reason why you should not have an input box (string gadget or spin gadget) on the Window, or if the real estate cannot be spared use a Message Requester. Also, you initialise an Image Gadget but you are not drawing on an image?

Edit: Something like the Help example (you will need a white 500 x 400 bitmap image saved as 'C:\White500x400.bmp':

CODE.............

One advantage of drawing on an image is that it is easy to save.
Well your program works perfectly and looks good. It's not quite what I had intended but all the functions are there. I'll use it until I know better :)

The only thing I don't like is the "image drawing" THAT was the sole reason (in my mind) for the two windows: Text / Graphical windows. See once all this stuff is done, there are about 20 milliseconds between each point that is plotted on the arc. The data is not just dumb calculated points, but sonar data. So if I plot 100 data points, that's 20ms * 100 or 2 seconds to complete the plot, then "Blink" it's transferred to the screen via Image.... It'll work that way, just not what I hope to get out of it.

THANK YOU for your time!

Re: Text and 2D graphics window

Posted: Sun Feb 27, 2011 4:05 pm
by spiral72
Zach wrote:In Purebasic help, look at the Keyboard link under 2D Games & Multimedia Libraries

Also under the General Libraries section, Console -> look up Inkey / Rawkey instead of Input. Since you are using the Console library, basing your input parsing routine on the Inkey() command should work better for you, as it will not halt the rest of the program.
Thank you. I'll look that up..... I'll have to figure out a way to inkey() the digits into a number.... Surely I can figure that out :)

Re: Text and 2D graphics window

Posted: Sun Feb 27, 2011 6:40 pm
by Zach
I won't join the debate about the best way to implement your programs User Interface, but here is a terse example of capturing input.
It's pretty basic, and doesn't convert the string into an integer, but it should give you an idea of capturing input without blocking the rest of your program from executing other tasks (if needed)

Code: Select all

OpenConsole()
Global InputString$


Repeat
  
  
  KeyPressed$ = Inkey()  
  If KeyPressed$ <> ""
    InputString$ = InputString$ + KeyPressed$
  EndIf
  
Until KeyPressed$ = Chr(13)

Print(InputString$)
Delay (5000)
hit some keys on the keyboard and then hit Enter (code 13) which will echo what you keys you hit to the console and then close it after 5 seconds.

Although if I understand what you want to do... Take some input, and use it to plot an arc. That is pretty basic and procedural. I don't think non blocking input is really needed in this case. So all the previous comments may be much ado about nothing.

Re: Text and 2D graphics window

Posted: Sun Feb 27, 2011 7:51 pm
by IdeasVacuum
Ah, I see. Well, instead of drawing on an Image, you can draw on the Window.
In the modified example below, I have included an image gadget so that the drawing area is seen immediately, but the gadget is disabled and the arc is being drawn directly on the Window:

Code: Select all

#Window0   = 0
#BtnDraw   = 1
#BtnExit   = 2
#Input     = 3
#ImageGdg  = 4
#Image     = 5

Procedure DrawArc(iSVA)
;----------------------
   x = 0
buff = 100
               If StartDrawing(WindowOutput(#Window0))

                           BackColor(RGB(255,255,255)) 
                          FrontColor(RGB(255,0,0))

                         DrawingMode(#PB_2DDrawing_Default)
                                 Box(50,50,500,400,RGB(255,255,255)) ; Clear previous drawing

                         DrawingMode(#PB_2DDrawing_Outlined)

                         For x = iSVA To (iSVA + 100)
                  
                                 Plot(160 + buff * Cos(x * 3.14159/180), 160 + buff * Sin(x * 3.14159/180))
                         Next
                  
                         DrawText(50,430,"Arc @ " + Str(iSVA))

                         StopDrawing()
               EndIf

EndProcedure

Procedure ShowForm()
;-------------------

  If OpenWindow(#Window0, 0, 0, 600, 500, "Draw Something",  #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered)

                ButtonGadget(#BtnDraw,   0,  0,  60,  24, "Draw", #PB_Button_Default)
                                                          ;Min-Max
                  SpinGadget(#Input,    65,  0,  50,  24, 100, 300, #PB_Spin_Numeric)
                ButtonGadget(#BtnExit, 540,  0,  60,  24, "Exit")


                  If (LoadImage(#Image,"C:\White500x400.bmp"))

                          ImageGadget(#ImageGdg, 50, 50, 500, 400, ImageID(#Image))
                        DisableGadget(#ImageGdg,1)

                  EndIf
                 

               GadgetToolTip(#BtnDraw, "Draw an arc")
               GadgetToolTip(#BtnExit, "Exit App")
             SetGadgetState (#Input, 100) : SetGadgetText(#Input, "100") ;initial value

  EndIf

EndProcedure

; #### EVENTS ####

ShowForm()

Repeat ; Start of the event loop

            iEvent = WaitWindowEvent(1)
         iGadgetID = EventGadget()

         If iEvent = #PB_Event_Gadget

               If     iGadgetID = #BtnDraw

                                     iUserInputVal.i = GetGadgetState(#Input)
                                     DrawArc(iUserInputVal)

               ElseIf iGadgetID = #BtnExit

                                     iEvent = #PB_Event_CloseWindow

               EndIf

         EndIf

Until iEvent = #PB_Event_CloseWindow ; End of the event loop

End

Re: Text and 2D graphics window

Posted: Mon Feb 28, 2011 4:53 pm
by spiral72
WHAT!?? Wow, thank you! I didn't expect ya would give me the inkey code and make the direct to window change for me!

Thank you so much. That's awesome.


Hopefully you think my reasons are valid for the direct draw..... I'm not trying to be hard to get along with :)

I should be able to try this tomorrow night. Tonight is more work on the house for the wife!

Re: Text and 2D graphics window

Posted: Mon Feb 28, 2011 4:59 pm
by IdeasVacuum
My wife said to me "what are you doing today?". I replied "nothing". She said "That's what you did yesterday". I said "yes, but I haven't finished yet!"

Re: Text and 2D graphics window

Posted: Mon Feb 28, 2011 7:16 pm
by Zach
Two Scotsmen are standing on opposite hills and one says to the other, "Are ya shearin' sheep?" And the other replies, "No, Get yer'own!"

Re: Text and 2D graphics window

Posted: Mon Feb 28, 2011 9:24 pm
by spiral72
Zach wrote:Two Scotsmen are standing on opposite hills and one says to the other, "Are ya shearin' sheep?" And the other replies, "No, Get yer'own!"

WHAT? AAAAAHAHAHA! Thanks, I needed that after this typical Monday.