Intermediate les08 separate .cpp & .h files

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
User avatar
bobloblaw
Posts: 100
Joined: April 15th, 2013, 3:05 am
Location: Earth, USA, California, SF Bay Area
Contact:

Intermediate les08 separate .cpp & .h files

Post by bobloblaw » June 2nd, 2013, 8:07 pm

Re: Intermediate lesson 08
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;
};
Surface.cpp:

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 );
			}
		}
	}
}

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: Intermediate les08 separate .cpp & .h files

Post by LuisR14 » June 2nd, 2013, 10:21 pm

btw, Gdiplus::GdiplusStartup requires a matching Gdiplus::GdiplusShutdown function :)
just wanted to point that out since i watched the tut vids
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

Syncan
Posts: 58
Joined: May 13th, 2013, 2:09 pm
Location: Holland

Re: Intermediate les08 separate .cpp & .h files

Post by Syncan » June 3rd, 2013, 10:28 am

LuisR14 wrote:btw, Gdiplus::GdiplusStartup requires a matching Gdiplus::GdiplusShutdown function :)
just wanted to point that out since i watched the tut vids
I am not yet at the intermediate lessons ( lesson 14 now ) but are you saying that lesson 8 intermediate is lacking a GdiplusShutdown function, or is that shutdown function only missing in the code above in this topic?

Probably I just do not understand what you are saying :shock:

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: Intermediate les08 separate .cpp & .h files

Post by LuisR14 » June 3rd, 2013, 3:08 pm

just pointing it out since the chilli never added it in any of the lessons :)
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

Post Reply