PDA

View Full Version : Getting mouse coordinates



DeusIX
10-07-2013, 03:13 PM
I'm trying to get mouse coordinates as an int. The following gives me ASCII-characters.

Here's the code:



clan::InputDevice &keyboard = window.get_ic().get_keyboard();
clan::InputDevice &mouse = window.get_ic().get_mouse();
clan::GameTime game_time;
while (!quit)
{ /* MAIN LOOP*/

game_time.update();
clan::Size canvas_size = canvas.get_size();

if( keyboard.get_keycode(clan::keycode_a) ) {playerX = playerX-3;}
if( keyboard.get_keycode(clan::keycode_d) ) {playerX = playerX+3;}
if( keyboard.get_keycode(clan::keycode_w) ) {playerY = playerY-3;}
if( keyboard.get_keycode(clan::keycode_s) ) {playerY = playerY+3;}

canvas.clear(clan::Colorf(0.0f,0.0f,0.2f)); // BG
spr_map.draw(canvas, 0, 0); // Draw map
for (int i = 1; i < 9; i++) {canvas.draw_line(0, gridSize*i, 600, gridSize*i,clan::Colorf(0.5f, 0.0f, 0.0f));}
for (int i = 1; i < 9; i++) {canvas.draw_line(gridSize*i, 0, gridSize*i, 600,clan::Colorf(0.5f, 0.0f, 0.0f));}

// The problem area
if( mouse.get_keycode(clan::mouse_left) ) {
Point position = mouse.get_position();
int sectorX = position.x;
text = sectorX ;
}

font.draw_text(canvas, 500, 32, text, clan::Colorf::white);

window.flip(1);
clan::KeepAlive::process(0); // This call processes user input and other events
}
return 0;
}



Now if this is added:

text = string_format("%1", position.x / 64);

I'll get the proper value, but as a string. Which means that while it displays properly, I can't do any calculations with it.
How do I make this work?

rombust
10-07-2013, 05:33 PM
clan::Point position = mouse.get_position();

Option 1)
clan::Point new_point = position;
new_point .x = postition.x / 64;
new_point .y = postition.y / 64;
std::string text1 = string_format("%1", new_point.x);
if (new_point.x > 1234) printf("Winner!");

Option 2)
int my_new_position = position.x / 64;
std::string text2 = string_format("%1", my_new_position );
if (my_new_position > 1234) printf("Winner!");

Option 3)
float my_floater_position = (float) position.x / 64.0f;
std::string text3 = string_format("%1", my_floater_position );
if (my_floater_position > 1234.0f) printf("Winner!");