Thread: CallBack Functions

  1. #1
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    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?
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    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

  3. #3
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    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?
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Yes, passing a function pointer with the event is one solution.

    Kuphryn

  5. #5
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    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.
    Last edited by manofsteel972; 11-04-2004 at 04:52 PM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 07:05 PM
  2. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  3. Replies: 1
    Last Post: 08-24-2008, 11:39 AM
  4. Callback Functions
    By valaris in forum C Programming
    Replies: 11
    Last Post: 07-31-2008, 09:20 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM