DrawBackground function

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
nbmatt
Posts: 10
Joined: January 2nd, 2013, 11:30 pm

DrawBackground function

Post by nbmatt » January 3rd, 2013, 9:26 pm

I am sure there will be some interest in this. I am contributing a DrawBackground function that will allow you to draw a background color to the game screen. Tired of seeing that same old black background? Then this is for you!

The first thing you will need to do is open the D3DGraphics.h Header file.

Below the other functions under "public:" add the following line:

Code: Select all

void DrawBackground(int width, int height, int r, int g, int b);
So now your header file should look something like this (yours may differ depending on any added or removed functions):

Code: Select all

#pragma once

#include <d3d9.h>

class D3DGraphics
{
public:
	D3DGraphics( HWND hWnd );
	~D3DGraphics();
	void PutPixel( int x,int y,int r,int g,int b );
	void DrawLine( int x1,int y1,int x2,int y2,int r,int g,int b );
	void DrawRectangle(int x, int y, int x2, int y2, int borderRed, int borderGreen, int borderBlue, int r, int g, int b);
	void DrawCircle( int cx,int cy,int radius,int r,int g,int b );
	void DrawDisc( int cx,int cy,int r,int rd,int g,int b );
	void DrawBackground(int width, int height, int r, int g, int b);
	void BeginFrame();
	void EndFrame();
private:
	IDirect3D9*			pDirect3D;
	IDirect3DDevice9*	pDevice;
	IDirect3DSurface9*	pBackBuffer;
	D3DLOCKED_RECT		backRect;
};

Now, open your D3DGraphics.cpp Source file. At the end of the file, add the following:

Code: Select all

void D3DGraphics::DrawBackground(int width, int height, int r, int g, int b)
{
	for(int i = 0; i < width; i++)
	{
		for(int j = 0; j < height; j++)
		{
			PutPixel(i, j, r, g, b);
		}
	}
}
All finished. You can save and recompile. You will now be able to draw a background to the screen.

To demonstrate a red background, open the Game.cpp Source file. Inside the ComposeFrame function where Chili's video tutorials have you typing the code, you can call the DrawBackground function. Since a background is usually the very bottom layer, DrawBackground should be the very first function you call before any additional drawing functions. If you were to call the DrawLine function first and then DrawBackground, the background would be drawn afterwards and thus, cover up your line.

To call the draw background function with red background:

Code: Select all

gfx.DrawBackground(800, 600, 255, 0, 0);
The parameters: screen width, screen height, red, green, blue

HippoSocks
Posts: 48
Joined: December 10th, 2012, 5:12 pm

Re: DrawBackground function

Post by HippoSocks » January 3rd, 2013, 11:35 pm

Chilli adds defines into the newer versions of the framework for the platformer.

D3DGraphics.h:

Code: Select all

#define SCREENWIDTH 800
#define SCREENHEIGHT 600
So I would say for the sake of ease, This would be better, because if you change the window size, the background will be one less thing to think about changing.

Code: Select all

void D3DGraphics::DrawBackgroundColor(  int r, int g, int b )
{
   for( int x = 0; x < SCREENWIDTH; x++ )
   {
        for( int y = 0; y < SCREENHEIGHT; y++ )
        {
              PutPixel( x, y, r, g, b );  // Good spot nbmatt ;)
        }
    }
}
Sadly there is a downside with this method of pooping out a background.
That function does loops 480000 per frame, meaning you will do serious damage to your frames per second once you start making a game that uses lots of loops and if statements, especially on computers with low specs.
Last edited by HippoSocks on January 4th, 2013, 2:32 am, edited 2 times in total.
-Socks

nbmatt
Posts: 10
Joined: January 2nd, 2013, 11:30 pm

Re: DrawBackground function

Post by nbmatt » January 4th, 2013, 1:41 am

Thanks for the update. In your remake, it would actually be PutPixel(x, y, r, g, b);

While there is a downside to that, it would not typically be any more harmful than drawing all of the game objects as shown in the tutorial videos. Unfortunately, that is the only way to place things onto the game canvas programmatically. The best way would be to create and load a bitmap with a paint program. But in these cases, we're talking about "beginners" and beginners should be shown all the possibilities in order to see how it works.

HippoSocks
Posts: 48
Joined: December 10th, 2012, 5:12 pm

Re: DrawBackground function

Post by HippoSocks » January 4th, 2013, 2:28 am

Thanks for spotting my little error ;)

I can see what you mean, for a beginner to use this with the unoptimized PutPixel then there game will poop all over the place, then explode.. A lot..

I don't think you realized what I meant, by it being harmful to the frames..

A game I am working on uses roughly this number of sprites..
480000 + ( 5 * (50*50) ) + 25 * 25 = 498750 pixels..

Background + 5 Power ups + Player = 498750.. Thats not even including the map, you see how these numbers add up right? since each pixel gets placed one by one..

( (50 * 50) * 5 ) * 38 = 475000
The map is 475000 pixels.. So the total is 973750 pixels,

Now lets add the enemy's:
3 * ( 25 * 25 ) = 1875
The total is: 975625

This leaves us only 24375 pixels away from reaching 1000000 pixels, My laptop starts dropping to about 40 frames at about that many.. But I still have game room by removing drawing the background first..

Because I'd draw 480000 of them pixels before the 520000 of the actual game.
And if I draw them last, I'll cover the rest of the sprites up. :lol:

Using a sprite is still as recourse heavy, I haven't worked out if its possible with the framework but isn't loading the background into a separate buffer from the back buffer, finding a way to print it but not clear it, somewhat be better? That way you don't print thousands of wasteful pixels onto the screen every frame.

Sorry for the lecture there, hope it doesn't seem rude, I'm just pointing out, that the method is quite wasteful.. But I do agree it is one of the only ways to draw a background. But not the best..
-Socks

nbmatt
Posts: 10
Joined: January 2nd, 2013, 11:30 pm

Re: DrawBackground function

Post by nbmatt » January 4th, 2013, 4:15 am

No need to be sorry. I appreciate a good debate. It helps me think about things. :)

You are correct. And as for your last question. It would be much more efficient to draw the background only once. I remember way back when I messed around with allegro library, you could technically draw the background outside of the game loop. I haven't played around with this framework enough yet to know enough about where the loop begins just yet. I will have a look though.

User avatar
codinitup
Posts: 112
Joined: June 27th, 2012, 7:43 am

Re: DrawBackground function

Post by codinitup » January 5th, 2013, 2:35 am

Well from what I saw in the origional post, you haven't heard about the clear function. In D3DGraphics, there is a function similar to this

Code: Select all

pDevice->Clear
and then there should be a parameter that says something like...

Code: Select all

D3DCOLOR_XRGB(0,0,0)
just change the color and it will clear the screen to a selected color.

I can't tell for sure, but from what I remember it is something like that.
Last edited by codinitup on January 6th, 2013, 2:39 am, edited 1 time in total.
MOOOOOO

User avatar
natox1986
Posts: 53
Joined: December 14th, 2012, 1:11 pm

Re: DrawBackground function

Post by natox1986 » January 5th, 2013, 11:40 am

No need to draw the PutPixel function all over the screen, this would mess up your FPS whenever you draw too many entities. Codinitup is right, in D3DGraphics.cpp is a Clear() function. This function takes several parameters. One of these parameters (D3DCOLOR_XRGB(x,x,x)) is used to change the default background color of the window.

Code: Select all

////////////////////////////////
// INSIDE D3DGraphics.cpp//
////////////////////////////////

void D3DGraphics::BeginFrame()
{
	HRESULT result;

	result = pDevice->Clear( 0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),0.0f,0 );
	assert( !FAILED( result ) );

	result = pBackBuffer->LockRect( &backRect,NULL,NULL );
	assert( !FAILED( result ) );
}

Like you already know, the 0,0,0 is black... 255,255,255 is white... and anything inbetween is valid!
So put on your pretty pants and start paintin' dat background!
Image

HippoSocks
Posts: 48
Joined: December 10th, 2012, 5:12 pm

Re: DrawBackground function

Post by HippoSocks » January 5th, 2013, 9:52 pm

nbmatt wrote:No need to be sorry. I appreciate a good debate. It helps me think about things. :).
Same here, But sometimes, a young forum roamer will take offence, and I'm not here to argue.
I'm just still hoping I got all that math correct, I was rather tired.. :lol:
I was just trying to make the point, drawing more than the amount of pixels the window's maximum, then you end up at loss.

Hmm, I forgot you could color the background with the clear function, That could be rather handy to remember.. :lol:
-Socks

Post Reply