Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C/C++

C/C++ C/C++ Discussion

Reply
 
Thread Tools Display Modes
Old 07-21-2008, 11:22 AM   #1 (permalink)
Forum Expert
 
Join Date: Jan 2008
Posts: 286
Send a message via AIM to Kitchen_ Send a message via MSN to Kitchen_
Default [Tutorial] C++ Callbacks

I'll be explaining how to create and use callbacks.

First, I'll start by explaining what a callback is. A callback is basically a pointer to a function that executes when a certain event

happens. This could be a button click, a window resize, or anything else. For this example, I'll be creating a button class that has an OnCreate callback.

The first thing to do is create the button, here is what I have:

Button.h
Code:
#pragma once

class Button
{
public:
    //
    // Fields
    //
    int X;
    int Y;
    int Width;
    int Height;
    
    //
    // Functions
    //
    Button(int x, int y, int w, int h);
};
Button.cpp
Code:
#include "Button.h"

Button::Button(int x, int y, int w, int h)
{
    X = x;
    Y = y;
    Width = w;
    Height = h;
}
This is the basic outline for a button. Now, lets create a new instance of it:

Main.cpp
Code:
#include <stdlib.h>
#include <stdio.h>
#include "Button.h"

int main(int argc, char* argv[])
{
    Button* button1 = new Button(5, 5, 100, 26);

    printf("button1 created\n");

    // So we can see the output. This isn't needed in Visual 
    // Studio when you start without debugging.
    system("PAUSE");

    return 0;
}
This is pretty basic so far, nothing special happens. Now lets add an event, for when it's created:

Button.h
Code:
#pragma once

class Button
{
public:
    //
    // Fields
    //
    int X;
    int Y;
    int Width;
    int Height;
    // Our callback
    void (*OnCreateCallback)(void);
    
    //
    // Functions
    //
    Button(int x, int y, int w, int h, void (*callback)(void));
    void OnCreate();
};
Button.cpp
Code:
#include "Button.h"

Button::Button(int x, int y, int w, int h, void (*callback)(void))
{
    X = x;
    Y = y;
    Width = w;
    Height = h;
    OnCreateCallback = callback;

    // Trigger the callback
    OnCreate();
}

void Button::OnCreate()
{
    // Make sure the callback isn't null
    if(OnCreateCallback != 0)
    {
        OnCreateCallback();
    }    
}
Main.cpp
Code:
#include <stdlib.h>
#include <stdio.h>
#include "Button.h"

Button* button1;

void Button1Created()
{
    printf("button1 created\n");
}

int main(int argc, char* argv[])
{
    // When we call the constructor, we're passing the
    // name of the function that handles the OnCreate
    // event. If you pass 0, the event won't get called
    button1 = new Button(5, 5, 100, 26, Button1Created);

    // So we can see the output. This isn't needed in Visual 
    // Studio when you start without debugging.
    system("PAUSE");

    return 0;
}
And that is basically how you add a callback in C++. I hope this was understandable and that it makes sense!

Attached is the Visual Studio project.
Attached Files
File Type: rar C++ Callbacks.rar (2.3 KB, 4 views)
__________________
Age of Conan Hacks + more to come
Visit www . injectsoft . com

Last edited by Kitchen_; 07-21-2008 at 11:26 AM.
Kitchen_ is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5