Sign in to follow this  
CableGuy

Libs, headers and external libs.

Recommended Posts

CableGuy    1335
Hi. I have created a library to use FreeType and I have a problem:
class FontResource
{
	friend class Renderer;
public:
	FontResource();
	
	bool Load(const string &name);		
	bool Load(const string &name,int size);	
private:
	bool AddChar(char c);
	vector<Glyph> glyph;
	FontPage *page;
	FT_Face face;
	int size;
};

As you can see I need to store FT_Face which is part of FreeType, which forces the users of my code also to include the FreeType headers. One way around it that I came up with is to store a void* and cast it in the source but that's too much hacky... Any ideas how to sort this out? Thanks.

Share this post


Link to post
Share on other sites
Aardvajk    13207
Pimpl.

Basic idea is, using a different example:

lib.h

class Texture
{
private:
class Imp; Imp *i; // forward declaration, can only use pointers and references

public:
Texture();
~Texture();
};





lib.cpp

#include <d3d9.h>

class Texture::Imp
{
public:
Imp() : Ptr(0) { }
~Imp(){ if(Ptr) Ptr->Release(); }

IDirect3DTexture9 *Ptr;
}

Texture::Texture()
{
i=new Imp(); i->Ptr=CreateTextureSomehow();
}

Texture::~Texture()
{
delete i;
}




This way, users including lib.h do not need to include, or even be aware of, d3d9.h or have any of its symbols dumped into global scope. Indeed you could change the implementation to use OpenGL and you would only have to recompile the library file and relink the project that uses it.

The link at the top will explain in far more detail.

Share this post


Link to post
Share on other sites
Kylotan    9860
Or you can use a pointer to an FT_Face. You don't need to include the FreeType header for that, but you do need to do some extra memory management for it.

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