SFML community forums

Help => Graphics => Topic started by: bobl on February 14, 2010, 04:13:13 pm

Title: blocking keypress
Post by: bobl on February 14, 2010, 04:13:13 pm
This is my first program built from the samples and source codes.
I'm pleased to see the facility for non-blocking keyboard input
but I would also like to single step through a frame at a time.

Can someone please show me how to modify this program so that you can  use eg the spacebar to step through the program a frame at a time.

Thank you in anticipation


Code: [Select]

//just testing display of set with capacity of 1000 that gets filled then emptied
//g++ text_opengl.cpp -lsfml-graphics -lsfml-window -lsfml-system -lGLU -lGL
#include <SFML/Graphics.hpp>
//#include <SFML/Graphics/Color.hpp>  //Red, this include already included by <SFML/Graphics.hpp>?
#include <iostream>
#include <string>              
#include <iomanip>              //setprecision
#include <sstream>              //stringstream
#include <math.h>              //log10
using namespace std;

string Set_string(int capacity, int items_in_set){
   int max_digits;
   int items_digits;
   stringstream ss;
   if (capacity > 0) {max_digits = ( (int) log10(capacity) + 1 );}
   else {max_digits = 0;}
   if (items_in_set > 0){items_digits = ( (int) log10(items_in_set) + 1 );}
   else {items_digits = 0;}
   int leading_0s = max_digits - items_digits;
   ss << string(leading_0s,'0') << items_in_set;
   return ss.str();
}

int Display_set(){
    sf::RenderWindow App(sf::VideoMode(800, 600), "SFML_App");

    sf::Font TextFont;
    if (!TextFont.LoadFromFile("/home/me/Desktop/bench/cheeseburger.ttf", 30)) {return 1;}

    const char * String = "AZERTYUIOPQSDFGHJKLMWXCVBNazertyuiopqsdfghjklmwxcvbn0123456789";
    sf::String Text(String, TextFont, 30.f);
   
    Text.SetColor(sf::Color::Red);  
    Text.SetX(50.f);
    Text.SetY(10.f);

    int capacity = 100, i=0, count_up=1;  //i simulates the qty of items in set with capacity for 1000 of them

    while ( App.IsOpened() ){

       //This is non-blocking!
       sf::Event Event;
       while ( App.GetEvent(Event) ){
          if (Event.Type == sf::Event::Closed) {App.Close();} //mouse click window close or alt/F4
          if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)) {App.Close();} //Esc key
       }

       App.Clear();
       Text.SetText(  Set_string(capacity,i)  );
       App.Draw(Text);
       App.Display();

       if (count_up){ if (i<capacity) i++; }
       else         { if (i>0) i--;    }
       if (i == capacity) {count_up=0;}
    }
   return 0;
}

int main(){
   Display_set();
   return 0;
}


Title: blocking keypress
Post by: bobl on February 14, 2010, 08:04:40 pm
BTW I tried this
ie putting the drawing under the control of the Esc key
but no go

Code: [Select]

    while ( App.IsOpened() ){
sf::Event Event;
while ( App.GetEvent(Event) ){
  if (Event.Type == sf::Event::Closed) {App.Close();} //mouse click window close or alt/F4
  if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)) { //Esc key
      App.Clear();
      Text.SetText(  Set_string(capacity,i)  );
      App.Draw(Text);
      App.Display();
      if (count_up){ if (i<capacity) i++; }
      else         { if (i>0) i--;    }
      if (i == capacity) {count_up=0;}
  }
        }
    }


I'm not sure why my code doesn't appear indented when I preview it
I am using code tags. Am I missing something??
Title: blocking keypress
Post by: Laurent on February 14, 2010, 08:56:44 pm
Quote
I'm not sure why my code doesn't appear indented when I preview it
I am using code tags. Am I missing something??

You have disabled BBCode in your profile.
Title: blocking keypress
Post by: bobl on February 14, 2010, 09:01:53 pm
Thanks very much

I notice that it's switched on now so thanks for enabling it in the code for my 1st post and turning it on now.

I've just revisited my 2nd post and it works fine.
I just forgot to show the 1st frame to start with and that threw me.