Author Topic: Really, really basic libtcod question  (Read 8169 times)

Gr3yling

  • Rogueliker
  • ***
  • Posts: 168
  • Karma: +0/-0
    • View Profile
    • Email
Really, really basic libtcod question
« on: March 15, 2014, 02:07:40 AM »
EDIT:  Arrgh, I meant to post this in programming.  Sorry.

I'm trying to understand this code (which is from the roguebasin python + libtcod tutorial):

import libtcodpy as libtcod
 
#actual size of the window
SCREEN_WIDTH = 80
SCREEN_HEIGHT = 50
 
LIMIT_FPS = 20  #20 frames-per-second maximum
 
 
libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
 
libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/libtcod tutorial', False)
 
libtcod.sys_set_fps(LIMIT_FPS)
 
while not libtcod.console_is_window_closed():
 
    libtcod.console_set_default_foreground(0, libtcod.white)
 
    libtcod.console_put_char(0, 1, 1, '@', libtcod.BKGND_NONE)
 
    libtcod.console_flush()


If libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/libtcod tutorial', False) initializes the console, and libtcod.console_flush() displays the changes that I have made to it, why doesn't the console stay open in a script with just these two commands after libtcod.sys_set_fps(LIMIT_FPS)?  It looks almost like you have to use while not libtcod.console_is_window_closed(): to keep the console open, but I thought that line was essentially asking the question "is the console window open?" not giving python the command "keep the window open".  So, what is the bare minimum of commands necessary to keep the console window open and what the heck is the mechanism by which they work? 

Is python looping through the commands after while not libtcod.console_is_window_closed():  LIMIT_FPS times per second, or does the console just stay drawn until it gets a command to change what it displays and is flushed? 

Yes, I could ask this on the libtcod boards, but after a brief inspection of how complex the topics there are, I think I am too embarrassed to.
« Last Edit: March 15, 2014, 02:39:00 AM by Gr3yling »

Quendus

  • Rogueliker
  • ***
  • Posts: 447
  • Karma: +0/-0
  • $@ \in \{1,W\} \times \{1,H\}$
    • View Profile
    • Klein Roguelikes
Re: Really, really basic libtcod question
« Reply #1 on: March 15, 2014, 04:31:23 AM »
(Disclaimer: this is based on limited experience with libtcod 2 years ago and more extensive experience with SFML)

If libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/libtcod tutorial', False) initializes the console, and libtcod.console_flush() displays the changes that I have made to it, why doesn't the console stay open in a script with just these two commands after libtcod.sys_set_fps(LIMIT_FPS)?  It looks almost like you have to use while not libtcod.console_is_window_closed(): to keep the console open, but I thought that line was essentially asking the question "is the console window open?" not giving python the command "keep the window open".  So, what is the bare minimum of commands necessary to keep the console window open and what the heck is the mechanism by which they work?
There are two things that can close the libtcod window:
  • The Python script ends and everything is automatically destroyed
  • The libtcod window is deliberately closed.
In order to avoid the first possibility, you need an indefinitely running loop.
In order to end the script after the window is deliberately closed you need the loop to check whether the window has been closed.
Quote
Is python looping through the commands after while not libtcod.console_is_window_closed():  LIMIT_FPS times per second, or does the console just stay drawn until it gets a command to change what it displays and is flushed?
Libtcod, like most graphics libraries, runs like someone with a shopping list. It adds to a list of things to buy whenever it thinks of them (when you call put_char), and then takes the list to the shop and buys everything at once (when you call flush).
Setting the FPS limit is like deciding not to go to the shops more than once per week. If you call flush() less than a week after the last time, it will stop what it's doing, stand outside the shop until the week is over, and then start shopping. The extra delay in flush() means that if you put a call to flush() in a loop, it will effectively put a speed limit on the whole loop.
So the answer to both those questions is yes.

Gr3yling

  • Rogueliker
  • ***
  • Posts: 168
  • Karma: +0/-0
    • View Profile
    • Email
Re: Really, really basic libtcod question
« Reply #2 on: March 15, 2014, 04:37:44 PM »
Okay.  Thanks Quendus.  That was really helpful.