PDA

View Full Version : User input does not react



Cr3a70r
09-26-2013, 03:34 PM
Hi everybody!
So, according to topic im having some problems on receiving UI.
In a class representing an Intro state i do the following:


clan::Slot slot_input_up = window->get_ic().get_keyboard().sig_key_up().connect(this, &IntroState::onInputUp);

Method IntroState::onInputUp


void onInputUp(const clan::InputEvent &key)
{
if (key.id == clan::keycode_q)
{
std::string title;
title = "Intro State: ";
title += "Exit";
window->set_title(title);
st_man->quit();
}
}


IDE: NetBeans 7.3.1
OS: Fedora 3.10.11-200.fc19.x86_64

Thank you in advice!

sphair
09-26-2013, 06:52 PM
Make sure clan::Slot slot_input_up does not go out of scope.

Cr3a70r
09-26-2013, 07:20 PM
How can it be done ?

Cr3a70r
09-26-2013, 07:50 PM
It seems that


clan::Slot slot_input_down = (window->get_ic().get_keyboard()).sig_key_down().connect(th is, &IntroState::onInputDown);

does nothing. Cuz when i set the BP to onInputDown it never hits the function.
When i declare a clan::Slot i thought strange that i never need to refer it to some event receiver.
As i understand the connect function has to set some event receiver for this.
But i never initialized any event receivers.

Also


clan::Slot slot_quit = window->sig_window_close().connect(this, &IntroState::onWindowClose);

It never hits the function onWindowClose.
It seems that arguments i pass is incorrect.
I did NOT find ANY actual documentation/comments on ANY functionality. Only obvious comments like "here we declare a variable"

rombust
09-26-2013, 08:21 PM
It is important clan::Slot stays in scope. From your code snippet, you cannot tell.


clan::Slot slot_quit = window->sig_window_close().connect(this, &IntroState::onWindowClose);

The "slot_quit" contains the information "the callback IntroState::onWindowClose will be called on sig_window_close()".

Thus when slot_quit goes out of scope (when the destructor is called), the callback will not be called.

Yes, the documentation does not make it clear. I'm sure it used to. http://clanlib.org/documentation/3.0/Core.html "Signals and callbacks" is all that I can find.

Note, some developers don't like having to keep multiple clan::Slots around. It's personal choice. The alternative it using clan::SlotContainer, that can contain multiple slots.

If still unsure, run the Examples/Display/Basic2D example. ( https://github.com/sphair/ClanLib/blob/master/Examples/Display/Basic2D/Sources/basic2d.cpp )

And of course, your main loop must call clan::KeepAlive::process(0);

Cr3a70r
09-26-2013, 08:25 PM
Well i know that clan is built with smart pointers. i know a little mistake in my code ive done before ive got to know about smart pointers in clan lib.
But still, take a look at my State please:



/*
* File: IntroState.h
* Author: cr3a70r
*
* Created on 25 Сентябрь 2013 г., 20:23
*/

#ifndef INTROSTATE_H
#define INTROSTATE_H
#include "Kernel.h"
#include "Context.h"
#include "Precompilled.h"
using namespace Game;

class IntroState : public IState
{
public:

void initialize()
{
window = CContext::Instance()->window;
window->set_title("IntroState");

st_man = CContext::Instance()->stateManager;
// Connect the Window close event
clan::Slot slot_quit = window->sig_window_close().connect(this, &IntroState::onWindowClose);

// Connect a keyboard handler to on_key_up()
clan::Slot slot_input_down = (window->get_ic().get_keyboard()).sig_key_down().connect(th is, &IntroState::onInputDown);
// Load a sprite from a png-file
canvas = new clan::Canvas(*window);
spr_logo = new clan::Image(*canvas, "Resources/logo.png");

}

void cleanup()
{

}

void pause()
{

}

void resume()
{

}

void update()
{
clan::GameTime game_time;
game_time.update();

// This call processes user input and other events
clan::KeepAlive::process(1);
}

void render()
{
canvas->clear(clan::Colorf(0.0f, 0.0f, 0.2f));
clan::Size canvas_size = canvas->get_size();
// Show the logo image.
spr_logo->draw(*canvas, canvas_size.width - spr_logo->get_width(), canvas_size.height - spr_logo->get_height());

window->flip(1);

}

~IntroState()
{
delete spr_logo;
delete canvas;
}

void onWindowClose()
{

}

void onInputDown(const clan::InputEvent &key)
{
if (key.id == clan::keycode_q)
{
std::string title;
title = "Intro State: ";
title += "Exit";
window->set_title(title);
st_man->quit();
}
}
protected:
clan::DisplayWindow *window;
clan::Image *spr_logo;
clan::Canvas *canvas;
Game::StateManager *st_man;
};



#endif /* INTROSTATE_H */




BTW: i wrote it using the basic2d example.
Just added a simple StateManager
Thank you

rombust
09-26-2013, 08:51 PM
Yeah error is as Sphair and myself stated.

clan::Slot's are going out of scope.

Change


clan::Slot slot_quit = window->sig_window_close().connect(this, &IntroState::onWindowClose);
clan::Slot slot_input_down = (window->get_ic().get_keyboard()).sig_key_down().connect(th is, &IntroState::onInputDown);


To


slot_quit = window->sig_window_close().connect(this, &IntroState::onWindowClose);
slot_input_down = (window->get_ic().get_keyboard()).sig_key_down().connect(th is, &IntroState::onInputDown);


And add:


protected:
clan::DisplayWindow *window;
clan::Image *spr_logo;
clan::Canvas *canvas;
Game::StateManager *st_man;
clan::Slot slot_quit; <---- This and
clan::Slot slot_input; <---- That

Judas
09-26-2013, 09:04 PM
The official documentation for the slots can be found here:

http://clanlib.org/documentation/3.0/TemplateCallbacks.html

If it is confusing or needs additional clarification, the text is part of the ClanLib git repository and can be found in the following file: ClanLib-3.0/Documentation/Overview/template_callbacks.h

We accept git pull requests.

Cr3a70r
09-27-2013, 04:54 AM
Damn that was some kind of easy if i kept in mind that clanlib uses smart pointers..
Thank you!