Managing 2 windows (or window/console) without additional libs

General FreeBASIC programming questions.
Case
Posts: 2
Joined: Dec 09, 2017 22:10

Managing 2 windows (or window/console) without additional libs

Postby Case » Dec 09, 2017 22:37

I'm new to FreeBasic, but not BASIC or programming in general. I want to implement a score display for a sports event, i.e.:

  • A laptop with 2nd monitor attached
  • external monitor is facing audience, displaying current teams' names, how many goals each team scored, a countdown clock etc.
  • laptop screen facing operator, for manipulating team names and score, starting/stopping time etc.

I'd like to keep this as simple as possible, because it would be my first FB project, and my first programming project in a while. So, unless there's some example code out there, I'd like to stick to FreeBasic's internal command set and not use any additional libraries.

AFAIK, I can't open more than one window in FreeBasic, but I should be able to print text to the console in addition to the window I open. That would allow me to open a window in the desired resolution (that the user needs to move to the 2nd monitor manually) and use that for the official display that the audience sees. The operator in front of the laptop would see the console window instead, where very simple text output would show him the same information the audience sees, plus information on all the keyboard shortcuts for manipulating stuff. The console display would have to be updated about once per second, since it also has a countdown clock.

Is that a viable approach, or am I missing something?

The first problem I encountered is locating the cursors in the console window after I opened a window using ScreenRes - is that even possible? The plan was to print a whole bunch of text ("press X to change this, Y to change that...") initially but leave the first few lines empty, then only print team names, scores and time in those first few line every second.
St_W
Posts: 1495
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Managing 2 windows (or window/console) without additional libs

Postby St_W » Dec 10, 2017 14:45

FreeBasic has not been designed for using multiple windows and thus such scenarios always need some hacks to get it work, so I wouldn't suggest such a solution.

Have you thought about creating two applications, each rendering its own window, which communicate via some form of IPC (sockets, pipes, files, ...) ? After reading your description that sounds like a viable approach to me.
A bit off-topic here, but still worth a though: what about implementing it using HTML+Javascript (e.g. websockets) only. That way you'd simply need two browser windows and they even could be located on different PCs. The same applies btw if you choose a socket-based communication for the two FB applications.

Speaking of HTML: of course you could also write a presentation application that renders the presentation display using a FreeBasic graphics window and additionally implements a small webserver that provides a HTML interface to control the presentation display.
xlucas
Posts: 285
Joined: May 09, 2014 21:19
Location: Argentina

Re: Managing 2 windows (or window/console) without additional libs

Postby xlucas » Dec 21, 2017 18:57

Another solution would be to create a frameless, transparent window the size of the desktop (or two desktops), use a transparent background and draw the windows yourself. It can be a lot of work, but depending on what you want it for, maybe it's what you'd prefer to do.
JohnK
Posts: 279
Joined: Sep 01, 2005 5:20
Location: Earth, usually
Contact:

Re: Managing 2 windows (or window/console) without additional libs

Postby JohnK » Dec 22, 2017 3:11

I would agree with these comments. A hack in fb is likely to be more work than using a library. If you do decide to put some effort into this, i suggest looking at the demos using fltk. Another could be tinyDialog. For me it was almost plug and play simple. Like spend 30 mins on it, and it should start making sense.

GUI is totally about multiple windows and you can still draw your fb.image to these windows. Just my $0.02 worth.


'The simplest example:

Code: Select all

#include once "fltk-c.bi"

'setup FB graphics
ScreenRes 1,1,32,,&HFFFFFFFF
dim as ubyte ptr fb_img=ImageCreate(101,101)
dim as ubyte ptr fb_pixels
dim as integer   w,h,bpp,pitch
ImageInfo fb_img,w,h,bpp,pitch,fb_pixels

'FB Drawing, RGB is reversed for fltk!!
Line fb_img,(0,0)-(100,100),RGBA(0,0,0,0),BF
Circle fb_img,(50,50),50,RGB(255,255,  0),,,,F
Circle fb_img,(25,30),12,RGB(255,255,255),,,,F
Circle fb_img,(75,30),12,RGB(255,255,255),,,,F

'make one window for test score
Var Win = Fl_WindowNew(320,200, "Fl_Window")
Fl_BoxNew 20,40,300,100,"Hello, World!"

'make another for drawing
Var Win2 = Fl_Double_WindowNew(320,200, "Fl_Window")
var Win2box = Fl_BoxNew(w\2,0,w,h)

'transfer fb to fltk
var img = Fl_RGB_ImageNew(fb_pixels,w,h,bpp,pitch)
Fl_WidgetSetImage Win2box,img


Fl_WindowShow Win
FL_WindowShow Win2
Fl_Run


I also would add that FLTK drawing commands can be simpler than FB
dodicat
Posts: 6639
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Managing 2 windows (or window/console) without additional libs

Postby dodicat » Dec 23, 2017 1:27

For more than one screen you can create a freebasic .dll, and use it along with a graphics screen.
This will give two graphics screens on the desktop.
But slightly easier, a working console and a display graphics.
(WINDOWS)

Code: Select all

#include "windows.bi"
Declare Function timeBeginPeriod       Alias "timeBeginPeriod"(As Ulong=1) As Long
Declare Function timeEndPeriod         Alias "timeEndPeriod"  (As Ulong=1) As Long
#include "crt.bi"


#define locateC(x,y)  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),type(x,y))
#define clearScreen    system_("@cls||clear")



#macro consoleinput(stuff,variable)
Open Cons For Input As #2
consolePrint(stuff)
Input #2,variable
Close #2
#endmacro

#macro consoleprint(stuff)
Open Cons For Output As #1
Print #1, (stuff) & "  ";
Close #1
#endmacro


Dim Shared As Long MySpeed
dim shared as integer xres,yres

Function Regulate(Byval MyFps As Long,Byref fps As Long) As Long
    Static As Double timervalue,_lastsleeptime,t3,frames
    frames+=1
    If (Timer-t3)>=1 Then t3=Timer:fps=frames:frames=0
    Var sleeptime=_lastsleeptime+((1/myfps)-Timer+timervalue)*1000
    If sleeptime<1 Then sleeptime=1
    _lastsleeptime=sleeptime
    timervalue=Timer
    Return sleeptime
End Function

Sub Display()
    Screenset 1,0
    Static x As Long = 10
    Static y As Long = 10
    Static dx As Long = 1
    Static dy As Long = 1
    Static As Long fps,sleeptime
    static as ulong u
    static as ulongint Q
    dim as ulong h,l
    While 1
        screenset 1,0
        sleeptime=regulate(MySpeed,fps)
        x += dx : y += dy
        If x<10 Or x>=xres-10 Then dx = -dx
        If y<10 Or y>=yres-10 Then dy = -dy
       
        Line(0,0)-(xres,yres),0,bf 'clear the graphics
     
        Circle(x,y),10 'draw the ball
       
        draw string(20,300), "Framerate set at " &myspeed
        Draw String(20,20),"Framerate = " &fps
        Draw String(200,200),"Sleep " &sleeptime &" , 1"
        Flip
    timeBeginPeriod(1) 'one millisecond sleep for sleep 1
        Sleep sleeptime,1
    timeEndPeriod(1)
       
    Wend
End Sub


Sub inputsub  'console
    threadcall Display()
    Do
        clearscreen
       
        locateC(3,2)
        consoleprint ("Click console to make active")
         
       locateC(0,5)
       consoleinput ("Enter a new framerate or <return> to end ",MySpeed)
       
       
        If Myspeed=0 Then End
        sleep 1
    Loop
End Sub

'====================================
Screenres 500,600,,2
screeninfo xres,yres
width xres\8,yres\16 'maximize the graphics fonts.
screencontrol 100,500,200
SetWindowPos( GetConsoleWindow, HWND_TOPMOST, 0, 0,450,600,0)


MySpeed=400  '<   set initial framerate



inputsub



Sleep




 

Return to “General”

Who is online

Users browsing this forum: albert, AndyA and 10 guests