Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Event handler in c++

Reply
Thread Tools

Event handler in c++

 
 
pratik lala
Guest
Posts: n/a
 
      01-22-2012
Hey,
I am trying to create a game which requires the use of keyboard.The
game is about falling items and I have to collect the item using the
paddle .So I know that we can make the terminal to operate in raw mode
and make the paddle move using the keyboard input.But i am not sure if
it will work as efficiently as an event handler works in other
languages.
Is there any way that we can make it look like a REAL time event?
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-22-2012
On 22.01.2012 15:42, pratik lala wrote:
>
> I am trying to create a game which requires the use of keyboard.The
> game is about falling items and I have to collect the item using the
> paddle .So I know that we can make the terminal to operate in raw mode
> and make the paddle move using the keyboard input.But i am not sure if
> it will work as efficiently as an event handler works in other
> languages.


Other languages are generally implemented in either C or C++, or in some
language that ultimately is implemented in C or C++.

That means (logically) that whatever the other language does, you can do
it just as efficiently in C++.

And in general the problem is the opposite: can some other and otherwise
desirable language, do something as efficiently as C++?

It is rare indeed that some high level language beats C++ on efficiency.
When you understand what's going on, what overhead the typical language
implementation adds, then you know that any test putting C++ low on
efficiency must be flawed. Like an observation that claims the moon is
much larger than the sun is necessarily flawed.

The "language efficiency" thing for C++ is more a question of
/productivity/. If it involves much more work to do the thing
efficiently in C++ than to do it efficiently in e.g. C#, then likely
efficiency will suffer for C++. So your concern is not entirely
misplaced on general grounds, but it's based on an incorrect perception
of the issues.

However, be assured that regarding the concrete example, namely keyboard
input, there is no problem.

Keyboard input is so slow a thing, by several orders of magnitude
compared to the execution speed of current computers, that thinking
about efficiency here is meaningless.


> Is there any way that we can make it look like a REAL time event?


Yes. First define in more detail what you mean by "REAL time event".
Then make it look like that, or ask here about how to make it look like
that.


Cheers & hth.,

- Alf
 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      01-22-2012
"Alf P. Steinbach"

> On 22.01.2012 15:42, pratik lala wrote:
>>
>> I am trying to create a game which requires the use of keyboard.The
>> game is about falling items and I have to collect the item using the
>> paddle .So I know that we can make the terminal to operate in raw mode
>> and make the paddle move using the keyboard input.But i am not sure if
>> it will work as efficiently as an event handler works in other
>> languages.

>
> Other languages are generally implemented in either C or C++, or in some
> language that ultimately is implemented in C or C++.
>
> That means (logically) that whatever the other language does, you can do
> it just as efficiently in C++.
>
> And in general the problem is the opposite: can some other and otherwise
> desirable language, do something as efficiently as C++?
>
> It is rare indeed that some high level language beats C++ on efficiency.
> When you understand what's going on, what overhead the typical language
> implementation adds, then you know that any test putting C++ low on
> efficiency must be flawed. Like an observation that claims the moon is
> much larger than the sun is necessarily flawed.
>
> The "language efficiency" thing for C++ is more a question of
> /productivity/. If it involves much more work to do the thing efficiently
> in C++ than to do it efficiently in e.g. C#, then likely efficiency will
> suffer for C++. So your concern is not entirely misplaced on general
> grounds, but it's based on an incorrect perception of the issues.


IMO, the lack of co-routines is a serious impediment for some problems -
such as event simulation - in C++. For example, I don't see any way to
implement an event handler in as neatly and intuitively satisfying a way as
in the first incarnation of Simula.

Does anyone know, does Simula 67 have co-routines? I took a course in it but
the it was very superficial. and directed at the goal of using Simula 67 as
a general purpose language.


 
Reply With Quote
 
pratik lala
Guest
Posts: n/a
 
      01-23-2012
On Jan 22, 9:11*am, "Alf P. Steinbach" <alf.p.steinbach
(E-Mail Removed)> wrote:
> On 22.01.2012 15:42, pratik lala wrote:
>
>
>
> > I am trying to create a game which requires the use of keyboard.The
> > game is about falling items and I have to collect the item using the
> > paddle .So I know that we can make the terminal to operate in raw mode
> > and make the paddle move using the keyboard input.But i am not sure if
> > it will work as efficiently as an event handler works in other
> > languages.

>
> Other languages are generally implemented in either C or C++, or in some
> language that ultimately is implemented in C or C++.
>
> That means (logically) that whatever the other language does, you can do
> it just as efficiently in C++.
>
> And in general the problem is the opposite: can some other and otherwise
> desirable language, do something as efficiently as C++?
>
> It is rare indeed that some high level language beats C++ on efficiency.
> When you understand what's going on, what overhead the typical language
> implementation adds, then you know that any test putting C++ low on
> efficiency must be flawed. Like an observation that claims the moon is
> much larger than the sun is necessarily flawed.
>
> The "language efficiency" thing for C++ is more a question of
> /productivity/. If it involves much more work to do the thing
> efficiently in C++ than to do it efficiently in e.g. C#, then likely
> efficiency will suffer for C++. So your concern is not entirely
> misplaced on general grounds, but it's based on an incorrect perception
> of the issues.
>
> However, be assured that regarding the concrete example, namely keyboard
> input, there is no problem.
>
> Keyboard input is so slow a thing, by several orders of magnitude
> compared to the execution speed of current computers, that thinking
> about efficiency here is meaningless.
>
> > Is there any way that we can make it look like a REAL time event?

>
> Yes. First define in more detail what you mean by "REAL time event".
> Then make it look like that, or ask here about how to make it look like
> that.
>
> Cheers & hth.,
>
> - Alf


Well, the events are such that,i have to display the falling items
continously and also make the keyboard move.So, To display the matrix
continously, my displaymatrix will have to wait until the user gives
an input. I want to make it such that the items are falling
continously irrespective of the situation that the user is entering or
not entering the input
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-23-2012
On 23.01.2012 01:14, pratik lala wrote:
> On Jan 22, 9:11 am, "Alf P. Steinbach"<alf.p.steinbach
> (E-Mail Removed)> wrote:
>> On 22.01.2012 15:42, pratik lala wrote:
>>
>>> [snip]
>>> Is there any way that we can make it look like a REAL time event?

>>
>> Yes. First define in more detail what you mean by "REAL time event".
>> Then make it look like that, or ask here about how to make it look like
>> that.

>
> Well, the events are such that,i have to display the falling items
> continously and also make the keyboard move.So, To display the matrix
> continously, my displaymatrix will have to wait until the user gives
> an input. I want to make it such that the items are falling
> continously irrespective of the situation that the user is entering or
> not entering the input


OK, what you're asking for is simply non-blocking i/o.

In *nix that's part of raw mode. I don't remember the details (I think
last time I used that was in 1986). However, if I recall correctly that
functionality is part of the ncurses library -- check it out.

Anyway, on each timer tick, simply check whether there is any new
keyboard input. You can structure that to look as apparent events, or
whatever. But the crucial functionality at bottom, is non-blocking i/o.

Cheers & hth.,

- Alf
 
Reply With Quote
 
Christopher
Guest
Posts: n/a
 
      01-24-2012
On Jan 22, 10:56*am, "osmium" <(E-Mail Removed)> wrote:
> IMO, the lack of co-routines is a serious impediment for some problems -
> such as event simulation - *in C++. *For example, I don't see any wayto
> implement an event handler in as neatly and intuitively satisfying a way as
> in the first incarnation of Simula.


I use boost::asio::io_service and/or boost::bind for events on
occasion. You post some work to be done to the io_service (which runs
on a thread or threads) and it calls back your handler when it
completes. The "work" could be waiting for something to happen, such
as a network recv.

You can also use boost::condition_variable to wait for some state.

You'd need more details about said event and what triggers it.
 
Reply With Quote
 
Robin Tucker
Guest
Posts: n/a
 
      02-13-2012
I'm using a templated method to describe, dispatch and handle events
in an intuitive manner. For example, describing an event can be done
with something like this:

Event<void(std::shared_ptr<Observable>, double, double, double)>
MyEvent;

, for an event that takes 4 parameters (the "sender" and three
doubles). The handler looks like this:

void OnEventHandler(std::shared_ptr<Observable> sender, double x,
double y, double z)
{
// Handle the event.
}

And the event is dispatched in the following intuitive way:

MyEvent(shared_from_this(), 3, 2, 1);

It's type-safe and you can dispatch any number of parameters (up to 9
actually, in the absence of variadic templates in VS2010!). Anyway the
code and blog about it can be found here:

http://drrobsjournal.blogspot.com/

I hope this is useful to someone.

 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      02-14-2012
在 2012年2月13日星期一UTC+8下午7时52分44秒 ,Robin Tucker写道:
> I'm using a templated method to describe, dispatch and handle events
> in an intuitive manner. For example, describing an event can be done
> with something like this:
>
> Event<void(std::shared_ptr<Observable>, double, double, double)>
> MyEvent;
>
> , for an event that takes 4 parameters (the "sender" and three
> doubles). The handler looks like this:
>
> void OnEventHandler(std::shared_ptr<Observable> sender, double x,
> double y, double z)
> {
> // Handle the event.
> }
>
> And the event is dispatched in the following intuitive way:
>
> MyEvent(shared_from_this(), 3, 2, 1);
>
> It's type-safe and you can dispatch any number of parameters (up to 9
> actually, in the absence of variadic templates in VS2010!). Anyway the
> code and blog about it can be found here:
>
> http://drrobsjournal.blogspot.com/
>
> I hope this is useful to someone.


C++ is not very good to be usd as a language to
write device drives to be called by the OS.

People working on the linux kernal talked about this
problem for so long.

Unless functions in the C++ container and template libraries are incorporated into the OS kernal library
or system shared linked libraries, otherwise C++ compiled programs are too fat in the heap, too.

A device in the OS is an object that responds to messages from the OS or AP..


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      02-14-2012
On 02/14/12 11:14 PM, 88888 Dihedral wrote:
> 在 2012年2月13日星期一UTC+8下午7时52分44秒 ,Robin Tucker写道:
>> I'm using a templated method to describe, dispatch and handle events
>> in an intuitive manner. For example, describing an event can be done
>> with something like this:
>>
>> Event<void(std::shared_ptr<Observable>, double, double, double)>
>> MyEvent;
>>
>> , for an event that takes 4 parameters (the "sender" and three
>> doubles). The handler looks like this:
>>
>> void OnEventHandler(std::shared_ptr<Observable> sender, double x,
>> double y, double z)
>> {
>> // Handle the event.
>> }
>>
>> And the event is dispatched in the following intuitive way:
>>
>> MyEvent(shared_from_this(), 3, 2, 1);
>>
>> It's type-safe and you can dispatch any number of parameters (up to 9
>> actually, in the absence of variadic templates in VS2010!). Anyway the
>> code and blog about it can be found here:
>>
>> http://drrobsjournal.blogspot.com/
>>
>> I hope this is useful to someone.

>
> C++ is not very good to be usd as a language to
> write device drives to be called by the OS.


Yes it is, you just have to provide the OS hooks as extern "C" functions.

> People working on the linux kernal talked about this
> problem for so long.


They are somewhat bigoted.

> Unless functions in the C++ container and template libraries are incorporated into the OS kernal library
> or system shared linked libraries, otherwise C++ compiled programs are too fat in the heap, too.


"too fat in the heap" ?

In kernel space you have two choices:

1) Use C++ as a better C and avoid the parts that require runtime support.

2) Use a statically linked run time library.

I use one where resources are tight and 2 where they are not.

--
Ian Collins
 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      02-14-2012
在 2012年2月15日星期三UTC+8上午2时17分43秒 ,Ian Collins写道:
> On 02/14/12 11:14 PM, 88888 Dihedral wrote:
> > 在 2012年2月13日星期一UTC+8下午7时52分44秒 ,Robin Tucker写道:
> >> I'm using a templated method to describe, dispatch and handle events
> >> in an intuitive manner. For example, describing an event can be done
> >> with something like this:
> >>
> >> Event<void(std::shared_ptr<Observable>, double, double, double)>
> >> MyEvent;
> >>
> >> , for an event that takes 4 parameters (the "sender" and three
> >> doubles). The handler looks like this:
> >>
> >> void OnEventHandler(std::shared_ptr<Observable> sender, double x,
> >> double y, double z)
> >> {
> >> // Handle the event.
> >> }
> >>
> >> And the event is dispatched in the following intuitive way:
> >>
> >> MyEvent(shared_from_this(), 3, 2, 1);
> >>
> >> It's type-safe and you can dispatch any number of parameters (up to 9
> >> actually, in the absence of variadic templates in VS2010!). Anyway the
> >> code and blog about it can be found here:
> >>
> >> http://drrobsjournal.blogspot.com/
> >>
> >> I hope this is useful to someone.

> >
> > C++ is not very good to be usd as a language to
> > write device drives to be called by the OS.

>
> Yes it is, you just have to provide the OS hooks as extern "C" functions.
>
> > People working on the linux kernal talked about this
> > problem for so long.

>
> They are somewhat bigoted.
>
> > Unless functions in the C++ container and template libraries are incorporated into the OS kernal library
> > or system shared linked libraries, otherwise C++ compiled programs are too fat in the heap, too.

>
> "too fat in the heap" ?
>
> In kernel space you have two choices:
>
> 1) Use C++ as a better C and avoid the parts that require runtime support..
>
> 2) Use a statically linked run time library.
>
> I use one where resources are tight and 2 where they are not.
>
> --
> Ian Collins




在 2012年2月15日星期三UTC+8上午2时17分43秒 ,Ian Collins写道:
> On 02/14/12 11:14 PM, 88888 Dihedral wrote:
> > 在 2012年2月13日星期一UTC+8下午7时52分44秒 ,Robin Tucker写道:
> >> I'm using a templated method to describe, dispatch and handle events
> >> in an intuitive manner. For example, describing an event can be done
> >> with something like this:
> >>
> >> Event<void(std::shared_ptr<Observable>, double, double, double)>
> >> MyEvent;
> >>
> >> , for an event that takes 4 parameters (the "sender" and three
> >> doubles). The handler looks like this:
> >>
> >> void OnEventHandler(std::shared_ptr<Observable> sender, double x,
> >> double y, double z)
> >> {
> >> // Handle the event.
> >> }
> >>
> >> And the event is dispatched in the following intuitive way:
> >>
> >> MyEvent(shared_from_this(), 3, 2, 1);
> >>
> >> It's type-safe and you can dispatch any number of parameters (up to 9
> >> actually, in the absence of variadic templates in VS2010!). Anyway the
> >> code and blog about it can be found here:
> >>
> >> http://drrobsjournal.blogspot.com/
> >>
> >> I hope this is useful to someone.

> >
> > C++ is not very good to be usd as a language to
> > write device drives to be called by the OS.

>
> Yes it is, you just have to provide the OS hooks as extern "C" functions.
>
> > People working on the linux kernal talked about this
> > problem for so long.

>
> They are somewhat bigoted.
>


In the kernal space, the hardware supports and assembler
supports are required.

> > Unless functions in the C++ container and template libraries are incorporated into the OS kernal library
> > or system shared linked libraries, otherwise C++ compiled programs are too fat in the heap, too.

>
> "too fat in the heap" ?
>

For example, the hash or map library in C++
are unreasably large and some are just faking
to be a hash table not of O(1) when there is no hash collision at all.

A hash can be frozen at the compile time
is not well-distinguished from a map that can
be modified in the run time very often.

This took so long for C++ to implement
a true hash table.

> In kernel space you have two choices:
>
> 1) Use C++ as a better C and avoid the parts that require runtime support..
>
> 2) Use a statically linked run time library.
>
> I use one where resources are tight and 2 where they are not.
>
> --
> Ian Collins


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Event Handler that creates adds another event handler kaczmar2@gmail.com ASP .Net 1 02-22-2007 07:37 AM
problem in changing the actual event handler into our own event ashok.dhananjeyan@gmail.com Javascript 0 10-30-2006 02:32 PM
rendering Button inside Render() event, makes it loose its click event handler association sonic ASP .Net 1 01-07-2005 06:33 PM
How to recall add event from an Event handler?? RC ASP .Net Web Controls 1 01-06-2005 07:44 PM
Passing event from dynamically created element and event handler? Adi Javascript 2 02-23-2004 02:44 PM



Advertisments