-
CallBack Functions
I am playing around with Event handling and I am trying to make my own menu in a Win32 console. I have an event pump
but I am getting a little confused when it comes to event model vs callback model and how I would implement them.
I have a basic skeleton program that I am using to write test programs. Perhaps my design is flawed.
Code:
//EventPump.cpp
#include"EventPump.h"
void EventPump(bool(*ProgramMain)(HANDLE &,INPUT_RECORD &,DWORD &) )
{
HANDLE hStdout=GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hStdin=GetStdHandle(STD_INPUT_HANDLE);
INPUT_RECORD input_record;
SetConsoleMode(hStdin,ENABLE_MOUSE_INPUT);
DWORD EventsInBuffer;
DWORD EventsRead;
do
{
GetNumberOfConsoleInputEvents(hStdin,&EventsInBuffer);
if(EventsInBuffer>=1)
{
ReadConsoleInput(hStdin,&input_record,1,&EventsRead);
}
}while(ProgramMain(hStdout,input_record,EventsInBuffer) );
}
Code:
//EventHandler.cpp
#include "EventHandler.h"
#include "KeyMappings.h"
extern void (*FunctionCallback[161])(void);
bool EventHandler(HANDLE hStdout,INPUT_RECORD input_record)
{
switch(input_record.EventType)
{
case EVENT:
switch(input_record.Event)
{
case
break;
}
Code:
//ProgramMain.cpp
#include ProgramMain.h
bool ProgramMain(HANDLE &hStdout,INPUT_RECORD &input_record,DWORD &EventsInBuffer)
{
//Functions to be executed each cycle go here
return EventHandler(hStdout,input_record);
}
Code:
//main.cpp
#include "EventPump.h"
#include "EventHandler.h"
#include "KeyMappings.h"
#include <windows.h>
#include "Menus.h"
void (*FunctionCallback[161])(void);//?? not sure if this is write?
int main(void)
{
for(int i=0;i<161;i++)
FunctionCallback[i]=FunctionNULL;
EventPump(ProgramMain);
return 0;
}
I believe this is the event model. What I am trying to figure out is how I would implement callback functions in a callback model? Would I have to change my design or would i just build on this?
-
Under a callback design, you access the function via a function pointer. Under an event design, you signal an event. Depending on the programming language and OS, the message queue dispatches the message to the appropriate module.
Kuphryn
-
Thanks Kuphryn. I understand that a callback model uses function pointers. I am trying to understand the process so I am attempting to create a model. I have an event pump that reads in messages from the the keyboard and mouse. The event handler (a switch statement) basically filters the input and executes some function accoding to the event MOUSE_MOVED, or FROM_LEFT_1st_BUTTON_PRESSED, so do I just replace the functions in the switch statement with function pointers? DOH I don't create any new events. So I need to define my own events either with an enumeration or const. IE(const MY_EVENT=1).
Then write that back to the input buffer? is that right?
-
Yes, passing a function pointer with the event is one solution.
Kuphryn
-
ok I think I have a better understanding.
Scenerio: We have a file menu with selections for open save save as and quit;
Normally the menu will be closed be a single line at the top of the screen with the word File
the title of the menu.
Now the MOUSE_MOVED event would test for the location of the mouse to see if it is over the file menu.
Once the mouse is moved over the file menu then the MOUSE_MOVED event would load
the function_ptr for the FROM_LEFT_1ST_BUTTON_PRESSED to ppoint to the function that calls openmenu() so when you click the buttom it opens the Filemenu. If you move off from the File menu it swithes the function_ptr back.
After the menu is open then the MOUSE_MOVED event would test for the location of
each selection and repeat the above sequence of events. Wehen moved outside the area of the menu the menu would close automatically
Does this sound right?
[edit]
NeverMind When the person clicks on the menu it sends the location of the mouse so
i don't need to test continually in MOUSE_MOVED event.
I am trying to add a File menu to this project. I was making an ascii art editor to make maps for a text based console game. I thought it would be simple to add a menu but I am finding it frustrating. It is crude you press F1 to bring up the ascii symbols and colors. You just click on the symbol and left click on color for foreground and rightclick on color for background. I want to be able to save the finished artwork to a file and also to load so I thought I would add a menu.