Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Working in console, showing in window  (Read 4831 times)

0 Members and 2 Guests are viewing this topic.

mickes27

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Working in console, showing in window
« on: June 10, 2017, 11:39:16 pm »
Hello,

I just started to learn SFML and have to do a project for my university. The task is to work with console and SFML window at the same time, using object orientated C++. For example I want to have menu in my console:

1. Draw circle
2. Draw rectangle
...

And when I type 1 in console, the SFML window updates and show circle. I don't know how to achieve that, because in every course of SFML I saw while loop for displaying window. It prevents from using console, unless I close the window. I need them both opened. I have two classes:

Scheduler -> while loop for taking numbers from console
SFMLWind -> for displaying the primitive in window

But I totaly don't know how to connect them to work simultanously. Can you show me the way? What I need to learn?

EDIT:

I've heard about threds - do I need them here?
« Last Edit: June 11, 2017, 12:03:23 am by mickes27 »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Working in console, showing in window
« Reply #1 on: June 11, 2017, 11:39:33 am »
The problem I can see here is that if you use the console's "cin"-type input, you block the thread from being able to process the window's events. Yes, using a separate thread could fix this but multiple threads are quite convoluted.

I would suggest, instead, to use the window's events to capture the keypresses and process them yourself. You can still output to the console.

You could capture real-time key presses to output to the console instead of using the SFML window's events.

Another option would be to create two windows. One for the graphics and one for the "console". The console window could be another SFML window but look and act similarly to a console window whilst also allowing access to its events.
If you want to be able to show a console in an SFML window, you could consider Selba Ward's Console Screen.

Otherwise, you may have to use multiple threads. If you haven't used threads before, this will take a lot of work and time.
http://www.cplusplus.com/reference/thread/thread/
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

mickes27

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Working in console, showing in window
« Reply #2 on: June 11, 2017, 11:51:24 am »
The problem is that it has to be "system console" as itself, not other windows which looks like that. I have to have the general program in console and the window has to be only for graphic display, so I think I can't use SFML window as main loop of my program. I think I will use threads then. Thanks for replay

PS. I am usind PDCurse for console, not cout/cin
« Last Edit: June 11, 2017, 11:53:59 am by mickes27 »

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Re: Working in console, showing in window
« Reply #3 on: June 11, 2017, 12:40:10 pm »
Use kbhit() and getch() functions from PDCurses, you won't have to wait for input from console, this way you can simply check if there's any.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Working in console, showing in window
« Reply #4 on: June 11, 2017, 12:41:16 pm »
As long as PDCurse doesn't block the main thread, you can still process the SFML window and therefore not need multiple threads.

p.s. Looks like korczurekk beat me to that...
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

mickes27

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Working in console, showing in window
« Reply #5 on: June 11, 2017, 07:01:03 pm »
So using getch and kbhit I would be able to write in console, then work on this data and send them to window display? Can you give me small example?

 

anything