Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Plugin System – an alternative to GetProcAddress and interfaces

, 25 May 2007
A powerful and extensible way of creating plugin-based applications
pluginsystem_src.zip
defmaker
defmaker.dsp
defmaker.dsw
DLL+EXE
Host.dsw
HostExe.dsp
BMPParser
BMPParser.dsp
Host
Host.dsp
Release
BMPParser.imp
Host.dll
HostExe.exe
TGAParser.imp
TGAParser
TGAParser.dsp
EXE
Host.dsp
Host.dsw
BMPParser
BMPParser.dsp
ImageParser
ImageParser.dsp
Release
BMPParser.imp
Host.exe
TGAParser.imp
TGAParser
TGAParser.dsp
Interface
Host.dsp
Host.dsw
BMPParser
BMPParser.dsp
Release
BMPParser.imp
Host.exe
TGAParser.imp
TGAParser
TGAParser.dsp
// TGAParser.cpp - a very simple TGA file parser
//
// Part of the Plugin System tutorial
//
///////////////////////////////////////////////////////////////////////////////

#include <windows.h>
#include <stdio.h>
#include "..\ImageParser.h"

// CTGAParser implements the IImageParser interface
class CTGAParser: public IImageParser
{
public:
	virtual HBITMAP ParseFile( const char *fname );
	virtual bool SupportsType( const char *type ) const;

private:
	HBITMAP CreateBitmap( int width, int height, void **data );
};

// Creates a bitmap with the specified size
HBITMAP CTGAParser::CreateBitmap( int width, int height, void **data )
{
	BITMAPINFO bmi={sizeof(BITMAPINFOHEADER),width,height,1,24,0,0,0,0,0,0};
	return CreateDIBSection(NULL,&bmi,DIB_RGB_COLORS,data,0,0);
}

// Parses a TGA file
HBITMAP CTGAParser::ParseFile( const char *fname )
{
	FILE *f=fopen(fname,"rb");
	if (!f) return NULL;

// Targa header structure
#pragma pack(push,1)
	struct TGAHeader
	{
		unsigned char idFieldLength;
		unsigned char colorMapType;
		unsigned char imageType;
		unsigned short firstColorMapEntry;
		unsigned short colorMapLength;
		unsigned char colorMapEntrySize;
		unsigned short xOrigin;
		unsigned short yOrigin;
		unsigned short width;
		unsigned short height;
		unsigned char bitsPerPixel;
		unsigned char descriptorBits;
	};
#pragma pack(pop)

	TGAHeader header;
	fread(&header,sizeof(header),1,f);

	int width=header.width;
	int height=header.height;

	if (header.imageType&8) {
		// compressed images are not supported
		fclose(f);
		return NULL;
	}
	if (header.bitsPerPixel!=24) {
		// only 24 bit images are supported
		fclose(f);
		return NULL;
	}

	// create the HBITMAP
	void *data;
	HBITMAP bitmap=CreateBitmap(width,(header.descriptorBits&32)?-height:height,&data);
	if (!bitmap) {
		fclose(f);
		return NULL;
	}

	// read the pixels
	int pitch=(width*3+3)&~3;
	char *ptr=(char*)data;
	fseek(f,ftell(f)+header.idFieldLength,SEEK_SET);
	for (int y=0;y<height;y++,ptr+=pitch)
		fread(ptr,width,3,f);

	fclose(f);
	return bitmap;
}

// Notifies the host that the plugin supports the TGA type
bool CTGAParser::SupportsType( const char *type ) const
{
	return stricmp(type,".tga")==0;
}

static CTGAParser g_TGAParser;

// The host calls this function to get access to the image parser
extern "C" __declspec(dllexport) IImageParser *GetParser( void ) { return &g_TGAParser; }

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ivo Beltchev
Software Developer (Senior)
United States United States
Ivo started programming in 1985 on an Apple ][ clone. He graduated from Sofia University, Bulgaria with a MSCS degree. Ivo has been working as a professional programmer for over 12 years, and as a professional game programmer for over 10. He is currently employed in Pandemic Studios, a video game company in Los Angeles, California.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 25 May 2007
Article Copyright 2007 by Ivo Beltchev
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid