• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Chrono1081

Why can't my class find these callback functions?

4 posts in this topic

Hi guys,

I've been working on OpenGL tutorials and now am moving what I'm learning into a class, but I ran into a problem. When I call glutDisplayFunc and glutReshapeFunc they can't find the callback functions (even though they exist). I'm really at a loss here and I either have a syntax error (even though everything works when I take it outside the class) or there is something going on under the hood that I am not aware of? Any help would be greatly appreciated :)

I'll admit I'm pretty rusty with classes but I can't seem to spot what I am doing wrong. Anyone have any idea?

[code]

//HEADER FILE
class OGLAppManager
{
public:
OGLAppManager();
void reshape(int width, int height);
void display(void);
private:
float rotationAngle;
};

//SOURCE FILE
#include "OGLAppManager.h"

OGLAppManager::OGLAppManager()
{
rotationAngle = 0;

//Set display properties
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); //Set up a display buffer, enable double buffering, blending and depth testing
glutInitWindowSize(1000, 1000); //Set the width and height of the window
glutInitWindowPosition(100, 100); //Set window position
glutCreateWindow("GSP 420"); //Set window caption

//Window Control Functions
glutDisplayFunc(display); //Tell glut to draw window based on function
glutReshapeFunc(reshape); //Tell glut to reshape the window when dragged
};

//Function to reshape the window when dragged
void OGLAppManager::reshape(int width, int height)
{
//Set viewport to window size
glViewport(0, 0, (GLsizei)width, (GLsizei)height);

// Switch to projection matrix to manipulate how scene is viewed
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); //Reset projection matrix to avoid artifacts

//Set FOV, + Near and Far clip planes
gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0);

//Switch back to modelview matrix
glMatrixMode(GL_MODELVIEW);
};

//Function to draw the scene
void OGLAppManager::display(void)
{
//Handle drawing
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //Clear background of window
glClear(GL_COLOR_BUFFER_BIT); //Clear the color buffer
glLoadIdentity(); //Load identity matrix to reset drawing locations

//Translate scene back
glTranslatef(0.0f, 0.0f, -6.0f);

//Draw a pyramid
glPushMatrix();
glRotatef(rotationAngle, 0.0, 1.0, 0.0);
glBegin(GL_TRIANGLES);

//SNIP
glutSwapBuffers(); //Flush buffers to the window

rotationAngle++; //Rotation angle for pyramid
};
[/code]
0

Share this post


Link to post
Share on other sites
A class member function will receive the this pointer of the class as its first (hidden) argument, so you're not passing in a 'void func(void)' but a 'void func(OGLAppManager*)'.

The this pointer won't be passed in as first hidden argument when you declare the function as static.




0

Share this post


Link to post
Share on other sites
Member functions are not normal functions, you cannot pass a pointer to a member function to something expecting a pointer to a function. To even refer to a member function, you must use &ClassName::functionName, unlike normal functions. There are many other caveats on member function pointers, but long story short glut cannot interact with c++ member functions. You'll need to write non-member functions. From these you may of course call member functions on any globally accessible object.

I'd recommend using SDL rather than GLUT if you can.
1

Share this post


Link to post
Share on other sites

Posted · Hidden

I tried this earlier before I posted:

[color=#008d00][color=#480083]glutDisplayFunc[/color][color=#000000](*[/color][color=#255b5f]display[/color][color=#000000]); [/color]//Tell glut to draw window based on function[/color]

[color=#008d00][color=#480083]glutReshapeFunc[/color][color=#000000](*[/color][color=#255b5f]reshape[/color][color=#000000]); [/color]//Tell glut to reshape the window when dragged[/color]

[color=#008d00]
[/color]

[color=#008d00][color=#000000][font=arial, verdana, tahoma, sans-serif] But when I run the program it crashes (although it compiles and links just fine.) Am I doing something wrong? None of my C++ books cover using callback functions in a class :([/font][/color][/color]

0

Share this post


Link to post
Thanks so much for the responses guys!

I did notice the pointer thing when I double clicked the error message (and just to try I put a * in front of the name which compiled and linked just fine, but crashed the program on startup :P

I will look into using SDL since I am fairly familiar with it although I've never mixed it with SDL. Are you able to just use the functions of each? (It looked that way when I saw it on Lazyfoo's site).
0

Share this post


Link to post
Share on other sites
You could do something like so :
[code]
class OGLAppManager{
public:
typedef void (*DisplayFunc)();
private:
DisplayFunc _display;
public:
OGLAppManager(): _display( _defaultDispFunc ) {}
OGLAppManager(const DiplayFunc& f) : _display(f) {}
void setDisplayFunc(const DisplayFunc& f){ _display = f; }
void initialize(){ /* all glut initialization here */ }
private:
static void _defaultDispFunc(){ /* default display function */ }
}

[/code]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0