View Full Version : Question about keybindings

06-27-2013, 10:27 PM

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". :whistling:

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

#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_size(CL_Size(800, 600), 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().conn ect(this, &keytest::on_input_up);

// Get the graphic context
CL_GraphicContext gc = window.get_gc();
CL_Font font(gc, "tahoma", 20);

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

// 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.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()

// This call processes user input and other events

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;

06-28-2013, 08:30 AM
Try changing:
"playerX -= 2.0f;"
"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.