Results 1 to 2 of 2

Thread: Question about keybindings

  1. #1

    Question Question about keybindings

    Hello.

    I'm using the basic2d example as the base for my code as it's the simplest one.
    The code is a mess for I'm in the phase of "trying to access the clanlibs functionality".

    So in the following code I'm trying to capture the key-presses of ASDW and process them as movement. The code compiles and actually works, however it is "jerky". The movement isn't stable, it slows down and then regains speed. As the functionality is very basic, I must be doing something wrong.

    It could be my system? But I don't know...
    Specs: CL2.3, Ubuntu 12.04


    Code:
    #include "precomp.h"
    #include "keytest.h"
    
    // The start of the Application
    int keytest::start(const std::vector<CL_String> &args)
    {
    	quit = false;
    
    	// Set the window
    	CL_DisplayWindowDescription desc;
    	desc.set_title("keytest");
    	desc.set_size(CL_Size(800, 600), true);
    	desc.set_allow_resize(true);
    
    	CL_DisplayWindow window(desc);
    
    	// Connect the Window close event
    	CL_Slot slot_quit = window.sig_window_close().connect(this, &keytest::on_window_close);
    
    	// Connect a keyboard handler to on_key_up()
    	CL_Slot slot_input_up = (window.get_ic().get_keyboard()).sig_key_up().connect(this, &keytest::on_input_up);
    
    	// Get the graphic context
    	CL_GraphicContext gc = window.get_gc();
    	CL_Font font(gc, "tahoma", 20);
    
    	// LOAD RESOURCES
    	CL_ResourceManager resources("Resources/resources.xml");
     	CL_Sprite sprite1(gc, "teapot", &resources);
     	CL_Sprite sprite2(gc, "ixwin", &resources);
     	CL_Sprite sprite3(gc, "teapot", &resources);
    
    	// Load images into set coordinates
     	CL_Sprite grid_A1(gc, "tile_water", &resources);
     	CL_Sprite grid_A2(gc, "tile_water", &resources);
     	CL_Sprite grid_A3(gc, "tile_water", &resources);
     	CL_Sprite grid_B1(gc, "tile_water", &resources);
     	CL_Sprite grid_B2(gc, "tile_grass", &resources);
     	CL_Sprite grid_B3(gc, "tile_water", &resources);
     	CL_Sprite grid_C1(gc, "tile_water", &resources);
     	CL_Sprite grid_C2(gc, "tile_water", &resources);
     	CL_Sprite grid_C3(gc, "tile_water", &resources);
    
    
    	unsigned int last_time = CL_System::get_time();
    
    	float playerX = -10.0f;
    	float playerY = 0.0f;
    
    	CL_InputDevice &keyboard = window.get_ic().get_keyboard();
    	// Run until someone presses escape
    	while (!quit)
    	{
    		unsigned int current_time = CL_System::get_time();
    		float time_delta_ms = static_cast<float> (current_time - last_time);
    		last_time = current_time;
    
    		// background
    		gc.clear(CL_Colorf(0.0f,0.0f,0.0f));
    
    		// Draw the bg-images (3x3 grid)
    		grid_A1.draw(gc,(float) playerX+0,			(float) playerY+0);
    		grid_A2.draw(gc,(float) playerX+20.0f,		(float) playerY+0);
    		grid_A3.draw(gc,(float) playerX+40.0f,		(float) playerY+0);
    
    		grid_B1.draw(gc,(float) playerX+0,			(float) playerY+20.0f);
    		grid_B2.draw(gc,(float) playerX+20.0f,		(float) playerY+20.0f);
    		grid_B3.draw(gc,(float) playerX+40.0f,		(float) playerY+20.0f);
    
    		grid_C1.draw(gc,(float) playerX+0,			(float) playerY+40.0f);
    		grid_C2.draw(gc,(float) playerX+20.0f,		(float) playerY+40.0f);
    		grid_C3.draw(gc,(float) playerX+40.0f,		(float) playerY+40.0f);
    
    		sprite1.update();
    		sprite1.draw(gc, 400, 300);
    
    		// Here is the keyboard presses
    		if( keyboard.get_keycode(CL_KEY_A))
    			playerX -= 2.0f;
    		if( keyboard.get_keycode(CL_KEY_D)) 
    			playerX += 2.0f;
    		if( keyboard.get_keycode(CL_KEY_W))
    			playerY -= 2.0f;
    		if( keyboard.get_keycode(CL_KEY_S))
    			playerY += 2.0f;
    
    		// Flip the display, showing on the screen what we have drawed
    		// since last call to flip()
    		window.flip(1);
    
    		// This call processes user input and other events
    		CL_KeepAlive::process(0);
    		CL_System::sleep(10);
    	}
    
    	return 0;
    }
    
    // A key was pressed
    void keytest::on_input_up(const CL_InputEvent &key, const CL_InputState &state)
    {
    	if(key.id == CL_KEY_ESCAPE)
    	{
    		quit = true;
    	}
    }
    
    
    // The window was closed
    void keytest::on_window_close()
    {
    	quit = true;
    }

  2. #2
    ClanLib Developer
    Join Date
    May 2007
    Posts
    1,824

    Default

    Try changing:
    "playerX -= 2.0f;"
    to
    "playerX -= time_delta_ms * 0.2f" <--- You may need to change this value

    Also remove the CL_System::sleep(10), since you have a window.flip(1), it's not required.

Similar Threads

  1. A question
    By xTarZii in forum Suggestion Box
    Replies: 0
    Last Post: 04-27-2013, 02:30 PM
  2. Gui question
    By Ockonal in forum Official ClanLib SDK Forums
    Replies: 22
    Last Post: 11-28-2010, 06:52 PM

Bookmarks

Bookmarks

Posting Permissions

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