Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Vlion

OO concepts and implementations.

This topic is 6112 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everybody. During my work with OpenGL over the mast months, I`ve been using a fairly standard interface. The essentials are given here
    
#define numobjs 8

class cObj
{
bool on[objs];
int handle[numobjs];
void Init();

int initobj()
{
i=0;
while(!on[i])
{
if(i >= numobjs)
{
return -1;
}
i++;
break;
}
on[i] = true;
return handle[i];
}
//runs object if needs updating.

void Run();
};
  
Now, Im sure you can see the prob. It is _not_ self-contained. However, it is a quite functional system- trust me- I still have it in operation. A month ago or so, I was helping a guy get his OGL lights working, nothing much, y'know. And his code was just beuatiful Roughly, it looked like this:
  
class cObj
{
static void init()
objinit()
Run()
};
  
This code is obvoisly more elgegant. It takes away the limit, is self contained, etc. My question is... Are there other methods of designing your OO implementations to be cleaner, and easier to code than my version ? I would have already converted, except that I want to be sure that when my code goes into its final state, it is as good as I can get it. I came, I saw, I got programmers block. ~V'lion [edit: added source tags] Edited by - Magmai Kai Holmlor on December 18, 2001 2:58:23 AM

Share this post


Link to post
Share on other sites
Advertisement
Most of the improvements on OO code are in object-object interactions. It''s not clear what you''re trying to accomplish with the coed above... so I don''t know what to suggest.

...you do know that you can define methods outside of the class declaration in a seperate file right? So that all that''s in the header file (.h) is a simple description of what the object can do (how it does it goes in the source file .cpp). Is that what you meant?

Share this post


Link to post
Share on other sites
I use this code to manage textures, lights, and similar things.
I saw the code the guy sent to me, and it looked alot better written.
So what I`m wondering, is there a even better way to structure my code ?

I came, I saw, I got programmers block.
~V''''lion

Share this post


Link to post
Share on other sites
Yes.
Put implementation code in the .cpp files, instead of inlining it in that class declaration.

Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO

Share this post


Link to post
Share on other sites
Once again, exactly what is the purpose of the code?
is it to encapsulate objects and only needing to call run to update all of them?

or is it just to have them all safely tucked away at one place?

plz state your intentions and maybe we can help

Share this post


Link to post
Share on other sites
Hm. Ok.
Heres my cTexture code.


    
#define TEXNUM 8
class cTexture
{

//Tracks the enabled state.

bool tracker[TEXNUM];
unsigned int texture[TEXNUM];
AUX_RGBImageRec *TexImg[TEXNUM];

public:
//Sets up filename as texture num.

//Returns the handle to the texture.

unsigned int MakeTex(char *filename)
{
int i = 0;

//Finds the first availible texture

while(tracker[i] != false)
{
i++;
}

//checks for errors in handle

if(i > TEXNUM || i < 0)
{
return false;
}

//Loads the file, checks for errors.

if(!(TexImg[i] = auxDIBImageLoad(filename)))
{
PostQuitMessage(-1);
return false;
}

//generate the OGL tex handle. Not my handle.

glGenTextures(1,&texture[i]);

//initializes the texture.

glBindTexture(GL_TEXTURE_2D, texture[i]);

//sets texture values.

glTexImage2D(GL_TEXTURE_2D,
0,
3,
TexImg[i]->sizeX,
TexImg[i]->sizeY,
0,
GL_RGB,
GL_UNSIGNED_BYTE,
TexImg[i]->data);

gluBuild2DMipmaps (
GL_TEXTURE_2D,
3,
TexImg[i]->sizeX,
TexImg[i]->sizeY,
GL_RGB,
GL_UNSIGNED_BYTE,
TexImg[i]->data
);



tracker[i] = true;

return i;


}
//Stubbed in, will work it so that it frees up num

void UnMakeTex(int num)
{
}

//Taken from NeHe` s tutorials.

int Init()
{
glEnable(GL_TEXTURE_2D);
//This void * cast that gets multiplied by one is really weird.

//I don`t know why I found it in here...

memset(TexImg,0,sizeof(void *)*1);
memset(tracker,false,sizeof(tracker));

//Fix MIP LOD values- done

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
//I have decided that the default values for GL_TEXTURE_WRAP_S|T are satifactory.

return true;

}
//Binds num so that the next operations use it.

bool RunATex(int num)
{
if(num >= TEXNUM)
{
return false;
}
if (num < 0)
{
return false;
}
//Yes this tex is on

if(tracker[num] == false)
{
return false;
}
glBindTexture(GL_TEXTURE_2D, texture[num]);
return true;


}
};
cTexture TEX;


Thats my cTexture class.
Here is the code I helped out with.
Its a cLight class; i didnt post my cLight because mine is very large.
As you can see, this code is more elegant,where if you want something, you declare it, instead of my monolithic approach.

  
#ifndef _LIGHT
#define _LIGHT

// light.h

//

// class to encapsulate the camera


#include "CObject.h"
#include <gl/glut.h>

#define X 0
#define Y 1
#define Z 2

typedef GLfloat vertex4D[4];
typedef GLfloat vector[3];

class CLight : public CObject
{

public:
CLight();
~CLight();


//functions


public:

void SetPosition(GLfloat x, GLfloat y, GLfloat z, GLfloat point);
void SetAmbient(GLfloat x, GLfloat y, GLfloat z, GLfloat a);
void SetSpecular(GLfloat x, GLfloat y, GLfloat z, GLfloat a);
void SetDiffuse(GLfloat x, GLfloat y, GLfloat z, GLfloat a);

void Move(float x, float y, float z);
void Draw();
void Position();
void Ambient();
void Specular();
void Diffuse();
void On();
void Off();

protected:


//variables


public:

vertex4D m_Position;
vertex4D m_Ambient;
vertex4D m_Specular;
vertex4D m_Diffuse;


bool m_On;
bool m_Spotlight;
vector m_SpotDirection;
GLfloat m_SpotExponent;
GLfloat m_SpotCutoff;
GLfloat m_ConstantAttenuation;
GLfloat m_LinearAttenuation;
GLfloat m_QuadraticAttenuation;
static int number_of_lights;
int m_LightID;

protected:


};

#endif


So my question is: is there a more elegant way to structure my code ?

I came, I saw, I got programmers block.
~V'lion

[edit: changed code tags to source tags]

Edited by - Magmai Kai Holmlor on December 22, 2001 6:07:42 PM

Share this post


Link to post
Share on other sites
Its self-contained.
You simply declare the object and you have a light.
Whereas in my code, you would have to call TEX.MakeTex() to get a texture, and besides, I have an outside define in the code that actos on my class code.

I came, I saw, I got programmers block.
~V''''lion

Share this post


Link to post
Share on other sites
Yeah, sure... but really what is your idea behind having it that way?

You must have had an idea with your system, because just having to declare a light is well.. the simple way todo it

what are you trying to achive?

Share this post


Link to post
Share on other sites
Why did I write in that convoluted fashion ?
Mostly cause I was busily try to code without taking a step back and thinking about design.
What do I want to achieve ?
Reliable, clean, easy-to-understand, elegant code.
~V''lion


I came, I saw, I got programmers block.
~V''''lion

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!