Jump to content
  • Advertisement

Archived

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

aboeing

object manager

This topic is 5381 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, im not really sure what the name of the thing im trying to do is, but basically I want to be able to have some overall object manager that creates other objects according to some global flag. For example:
Manager->Initialize();
Manager->SetAPI(OPEN_GL);
Texture *t = Manager->CreateTexture();
t->LoadBMP("helo.bmp");
Manager->Shutdown();
but this would mean for each creation function I would have to include all the different possible api implementations, and make massive switch functions, like so:
#include <opengl_texture.h>
#include <directx_texture.h>
#include <software_texture.h>

class Manager {
	CreateTexture() {
		switch (api) {
			case OPEN_GL:
				return (Texture *) new OpenGL_Texture;
			case DIRECTX:
				return (Texture *) new DirectX_Texture;
			case SOFTWARE:
				reutrn (Texture *) new Software_Texture;
		}
	}
};
So my question basically is, is there a better/nicer way to do this? [so that i dont need to build big switch statements - im using this on a project which will probably use more than 5 different api's, and it would get quite annoying..] Thanks! [edited by - aboeing on September 25, 2003 4:23:03 AM]

Share this post


Link to post
Share on other sites
Advertisement
Make a base class abstract graphics class then derive an opengl, directx, and software children from that base class and just define the functions differently for each. Then, store a pointer to the base type. When you switch APIs just call a templated "setAPI" funct with the type as the parameterization (or rather than templating you can load an instance of a child class from a dll). That function should be in charge of deleting (or signalling a release in some manner) of any previously allocated children, and then stores the address of the new object to the pointer. Then just interface with the pointer to the base class and dynamic function binding takes care of everything for you! Switch statements be gone!

Share this post


Link to post
Share on other sites

class Interface{
virtual void render(void);
}

class GL : public Interface{
void render(void){
//render with GL

};
}

class D3D : public Interface{
void render(void){
//render with D3D

};
}

class Engine{
Interface *interface;

void setAPI(int api){
if (api == GLAPI) interface = new GL;
if (api == D3DAPI) interface = new D3D;
}

void render(void){
interface->render();
}
}


i hope this example is clear

[edited by - uncutno on September 25, 2003 4:29:41 AM]

Share this post


Link to post
Share on other sites
uh, thanks for the help, but i dont really understand that,..

say i have this code:

class Interface{
virtual void render(void);
}

class Texture{
virtual void loadBMP(char *filename);
}

class GL : public Interface{
void render(void){
//render with GL

};
}
class D3D : public Interface{
void render(void){
//render with D3D

};
}
class GLTex:public Texture {
void loadBMP(char *filename) {
//load bmp with gl

}
}

class D3DTex:public Texture {
void loadBMP(char *filename) {
//load bmp with D3D

}
}


now how do i make a new texture?


class Engine{
Interface *interface;
Texture *texture;
void setAPI(int api){
if (api == GLAPI) interface = new GL;
if (api == D3DAPI) interface = new D3D;
}
void render(void){
interface->render(); //yipee

}
void loadBMP(char *fname) {
//** insert code to create correct texture**//

texture->LoadBMP(fname); //how do i know to create a d3d or gl texture here?

}
}


Share this post


Link to post
Share on other sites
put the createTexture method in your interface. Then you can override the virtual render and createTexture functions.


class Interface
{
virtual void render();
virtual Texture* createTexture();
}

class GL extends Interface
{
void render()
{
//render to openGL frame buffer

}

Texture* createTexture()
{
//create OGL texture

}
}

class DirectX extends Interface
{
void render()
{
//renders to DirectX frame buffer

}

Texture* createTexture()
{
//create DirectX texture

}
}




class Engine
{
Interface *interface;
Texture *texture;
void setAPI(int api)
{
if (api == GLAPI) interface = new GL;
if (api == D3DAPI) interface = new D3D;
}

void render(void)
{
interface->render(); //yipee

}

void loadBMP(char *fname)
{
Texture* texture = interface->createTexture();
texture->LoadBMP(fname);
}
}


I apologise for my mash of Java/C++


Share this post


Link to post
Share on other sites


//interfaces


class TextureInterface{
virtual void create(void);
virtual void loadBMP(char *fname);
}

class ApiInterface{
virtual void render(void);
virtual TextureInterface *newTexture(void);
}

//gl


//this way, all the GL texture code will be inside

//the GLTexture class

class GLTexture : public TextureInterface{

//store GL spesific texture data here


void create(void){
//create a gl texture

};
void loadBMP(char *fname){
//load a bmp into a GL texture

};
}

class GL : public ApiInterface{
void render(void){
//render with GL

}
virtual TextureInterface *newTexture(void){
return new GLTexture;
}
}

//d3d.. same as gl, but the d3d way


//engine


class Engine{
ApiInterface *interface;
TextureInterface *texture;
void setAPI(int api){
if (api == GLAPI) interface = new GL;
if (api == D3DAPI) interface = new D3D;
}

void doStuff(void){
texture = interface->newTexture();
texture->create();
texture->loadBMP(fname);
}

void render(void){
interface->render();
}
}


and so on...

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!