Sign in to follow this  
frogtag

OpenGL Dev C++ File Structure Query

Recommended Posts

frogtag    121
I'm currently writing a simple engine program using OpenGL in Dev C++. For purposes of simplicity, i've got a main file (main.cpp) which uses a function within the file to draw a 3rd perspective person on the screen. I use a header file (character.h) to create class definitions to link to a character file (character.cpp) which does a lot of maths to make the character walk within a simple landscape (also a function within main.cpp). What i want to know or do is move the function for drawing the character into character.cpp, is this possible? i've tried it but i get lots of errors. does the function have to stay in main.cpp or can it be in character.cpp and called in main.cpp?

Share this post


Link to post
Share on other sites
dimebolt    440
It's hard to tell what's wrong without code and the actual error messages, but if you draw the character using OpenGL you'll also need to include the OpenGL headers in character.cpp. Also, if the drawing code requires certain functions from main.cpp, you should also make those accessable somehow (through a main.h or, more likely, by moving those functions to character.cpp). For an indepth explaination of all considerations with using multiple files, read this excellent article.

Tom

Share this post


Link to post
Share on other sites
frogtag    121
My files look roughly like follows....

MAIN.CPP
--------

#include files, etc...

...

draw_character(x, y, z); // character function is called

...

void draw_character(x, y, z)
{
glTranslate(x, y, z);
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd();
}

CHARACTER.H
-----------
...

Class declarations...

...

CHARACTER.CPP
-------------
#include "character.h"

...

void Class definitions...

...

What i want to do is move draw_character to character.cpp as a function(??? i presume you can do this?) and then call it in main.cpp using class_name::draw_character(). do you have to declare the function in character.h as you do classes?

Share this post


Link to post
Share on other sites
BorisTSR    165
make Character class declaration look something like below, with whatever else you have there currently

character.h

class Character
{
public:
void draw_character(x, y, z); //won't compile, x,y,z need types
}




character.cpp

void Character::draw_character(x, y, z) //again, x,y,z need types
{
glTranslate(x, y, z);
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd();
}




is that what you wanted?

Share this post


Link to post
Share on other sites
dimebolt    440
Please post code using source tags as described here.

Quote:
Original post by frogtag
My files look roughly like follows....
What i want to do is move draw_character to character.cpp as a function(??? i presume you can do this?) and then call it in main.cpp using class_name::draw_character(). do you have to declare the function in character.h as you do classes?


Yes that function must be defined in your class definition, which should be in character.h. It should look a bit like this:

character.h:
class Character
{
protected:
// put character variables and internally used member functions here...
public:
Character(); // constructor
~Character(); // destructor

// put all accessable member functions here:
void DrawCharacter();
}; // don't forget the ; or you'll get plenty of errors in character.cpp and main.cpp





character.cpp:
// put all includes required by character here:
#include "character.h"
#include <"GL/gl.h">
#include <"whateveritisyouneed.h">

// function implementations here:
Chracter::Character()
{...};
Chracter::~Character()
{...};

void Chracter::DrawCharacter()
{
glBegin();
...
glEnd();
};

// etc




Note, that I edited my original post with a helpful link.

Tom

EDIT: lol, Boris had similar ideas :)

[Edited by - dimebolt on October 5, 2005 10:17:25 AM]

Share this post


Link to post
Share on other sites
frogtag    121
Yeah it looks like what i've got but in character.cpp i'm using...

void draw_character(float x, float y, float z)
{
...
}

and use ...

Character::draw_character(1.0f, 1.0f, 1.0f)

in main.cpp. it might be that i don't use the Character:: in character.cpp before hand. i'll give it a go.

Thanks




Share this post


Link to post
Share on other sites
dimebolt    440
Quote:
Original post by frogtag
Yeah it looks like what i've got but in character.cpp i'm using...

void draw_character(float x, float y, float z)
{
...
}



Yes, if you forget the Character:: in that line it will not define draw_character as part of the class. Also, in main.cpp you shouldn't call draw_character through the class namespace (Character::draw_character(...)), but using a Chracter object like this:

Character player;
player.draw_character(x, y, z);


This also shows that in your naming the 'character' part has become superfluous. Since you already know the 'player' variable is a Character, you could simply name the function 'draw()' or, even better, 'draw_at_position()':

Character player;
player.draw_at_position(x, y, z);

As you can see, it now almost reads like a sentence :) To quote one of my former programming teachers: "good code should read like a book"...

Tom

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