The chilli man had a lot on his mind this episode and decided to put all the Surface code into a single .h file. Out of best code practice I took some initiative and broke it into it's separate .cpp & .h files. For anyone that might be interested I thought I'd post them here.
Cheers to all~
Surface.h:
Code: Select all
#pragma once
#include "D3DGraphics.h"
#include <string>
class Surface
{
public:
Surface( std::wstring& filename );
~Surface();
void Draw( int xoff, int yoff, D3DGraphics& gfx );
void DrawKeyed( int xoff, int yoff, D3DCOLOR key, D3DGraphics& gfx );
private:
unsigned int width;
unsigned int height;
D3DCOLOR* surface;
};
Code: Select all
#include "Surface.h"
#include <GdiPlus.h>
#include "D3DGraphics.h"
#include <string>
#pragma comment( lib,"gdiplus.lib" )
Surface::Surface( std::wstring& filename )
:
width( NULL ),
height( NULL ),
surface( NULL )
{
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
Gdiplus::GdiplusStartup( &gdiplusToken, &gdiplusStartupInput, NULL );
Gdiplus::Bitmap bitmap( filename.c_str() );
Gdiplus::Color pixel;
height = bitmap.GetHeight();
width = bitmap.GetWidth();
surface = new D3DCOLOR[ height * width ];
for( unsigned int y = 0; y < bitmap.GetHeight(); y++ )
{
for( unsigned int x = 0; x < bitmap.GetWidth(); x++ )
{
bitmap.GetPixel( x, y, &pixel );
surface[ x + y * bitmap.GetWidth() ] = D3DCOLOR_ARGB( pixel.GetA(), pixel.GetR(), pixel.GetG(), pixel.GetB() );
}
}
}
Surface::~Surface()
{
delete [] surface;
}
void Surface::Draw( int xoff, int yoff, D3DGraphics& gfx )
{
const int yStart = max( -yoff, 0 );
const int xStart = max( -xoff, 0 );
const int yEnd = min( SCREENHEIGHT - yoff, (int)height );
const int xEnd = min( SCREENWIDTH - xoff, (int)width );
for( int y = yStart; y < yEnd; y++ )
{
for( int x = xStart; x < xEnd; x++ )
{
gfx.PutPixel( x + xoff, y + yoff, surface[ x + y * width ] );
}
}
}
void Surface::DrawKeyed( int xoff, int yoff, D3DCOLOR key, D3DGraphics& gfx )
{
const int yStart = max( -yoff, 0 );
const int xStart = max( -xoff, 0 );
const int yEnd = min( SCREENHEIGHT - yoff, (int)height );
const int xEnd = min( SCREENWIDTH - xoff, (int)width );
for( int y = yStart; y < yEnd; y++ )
{
for( int x = xStart; x < xEnd; x++ )
{
D3DCOLOR c = surface[ x + y * width ];
if( c != key )
{
gfx.PutPixel( x + xoff, y + yoff, c );
}
}
}
}