Results 1 to 3 of 3

Thread: Blocking keyboard input

  1. #1

    Default Blocking keyboard input

    Hi everyone!

    I've just read through most of the overviews, and to get used to ClanLib, decided to start modifying an example program I got from somewhere. All it does it draw a pretty colour.

    However, using the default loop
    while (!CL_Keyboard::get_keycode(CL_KEY_ESCAPE)){
    	CL_Display::clear(CL_Color(100, 0, 50));
    causes 100% usage of my CPU. Since I'm looking at roguelike programming, this isn't really acceptable. This is what I modified my program to:

    #include <ClanLib/gl.h>
    #include <ClanLib/core.h>
    #include <ClanLib/application.h>
    #include <ClanLib/display.h>
    class MyApp : public CL_ClanApplication
    	CL_EventTrigger tr;
    	void kdsignal(const CL_InputEvent &t){
    		std::cout << "Setting flag.\n";
    	virtual int main(int argc, char **argv)
    		CL_ConsoleWindow console("Console");
    		std::cout << "Application started." << std::endl;
    			CL_SetupCore setup_core;
    			CL_SetupDisplay setup_display;
    			CL_SetupGL setup_gl;
    			CL_DisplayWindow window("FontTest", 1200, 200);
    			CL_Slot keyPressSlot = CL_Keyboard::sig_key_down().connect(this, &MyApp::kdsignal);
    			while (!CL_Keyboard::get_keycode(CL_KEY_ESCAPE))
    				std::cout << "here\n";
    				tr.wait(); std::cout << "wait\n";
    				tr.reset(); std::cout << "reset\n";
    				CL_Display::clear(CL_Color(100, 0, 50));
    				std::cout << "Frame end.\n" << std::endl;
    		catch (CL_Error err){
    			std::cout << err.message.c_str() << std::endl;
    		return 0;
    } app;
    The idea here was that I would block before each redraw, waiting for user input. The way I found to unblock was by using the CL_Keyboard::sig_key_down() signal - I attached it to a slot that calls a function that only sets the flag, supposedly unblocking the wait() call on the main loop.

    However, it seems that it doesn't happen. If you block, you block. No signals get called.

    Am I doing something wrong, or do I need to use a separate thread for input?


  2. #2
    Administrator Seth's Avatar
    Join Date
    Jul 2002


    Clanlib needs to have CL_System::keep_alive() running all the time to handle system/input messages.

    However, I think you can get what you want done by adding a Sleep(1); to the loop and only DRAWING (your clear screen and flip stuff) when something actually changes. So what should happen is even though you're running the loop very quickly, it's still 0.05% of your total computers system time.

    You can sort of emulate the 'feel' of DOS/command line style non-event based programming (if that's what you're after) by writing your own GetKeyPress() function that basically DOES just run a similar loop until a key is pressed.
    Seth A. Robinson
    Robinson Technologies

  3. #3


    Thank you very much for the reply!

    I guess I'm just not used to non-blocking input calls. Even in an event-based architecture, if there's no event, there might be no reason for you to use up CPU. It wasn't really about the feel, but that's a good idea anyway. I'll give it a go!

    Thank you again

Similar Threads

  1. How to use gui without .run() blocking method ?
    By hubadu in forum Official ClanLib SDK Forums
    Replies: 3
    Last Post: 05-16-2007, 07:48 PM
  2. Mouse Input
    By Soudeus in forum Official ClanLib SDK Forums
    Replies: 2
    Last Post: 04-25-2007, 01:57 PM
  3. Slots and Keyboard
    By azizmandar in forum Official ClanLib SDK Forums
    Replies: 2
    Last Post: 01-03-2007, 03:19 AM
  4. keyboard controlls??
    By Gexy in forum Funeral Quest
    Replies: 3
    Last Post: 01-31-2006, 05:36 PM
  5. picking up items using keyboard
    By rpgnut in forum Dink Smallwood HD
    Replies: 3
    Last Post: 01-31-2005, 04:16 PM



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts