Homework Assignment Episode 9

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Natox
Posts: 51
Joined: January 10th, 2012, 2:51 pm
Location: the Netherlands
Contact:

Homework Assignment Episode 9

Post by Natox » January 23rd, 2012, 11:40 pm

DO NOT READ ANY FURTHER IF YOU DID NOT FINISH YOUR HOMEWORK YET

Okay, finally, after 1½ day of thinking, doubting, experimenting I finally figured out the homework assignment. This might sounds really stupid to you chili, but I actually thought it was hard to come up with the logic for this. Maybe it gets easier once you get more into the programming language.

Game.cpp:

Code: Select all

/****************************************************************************************** 
 *	Chili DirectX Framework Version 11.12.17											  *	
 *	Game.cpp																			  *
 *	Copyright 2011 PlanetChili.net														  *
 *																						  *
 *	This file is part of The Chili DirectX Framework.									  *
 *																						  *
 *	The Chili DirectX Framework is free software: you can redistribute it and/or modify	  *
 *	it under the terms of the GNU General Public License as published by				  *
 *	the Free Software Foundation, either version 3 of the License, or					  *
 *	(at your option) any later version.													  *
 *																						  *
 *	The Chili DirectX Framework is distributed in the hope that it will be useful,		  *
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of						  *
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the						  *
 *	GNU General Public License for more details.										  *
 *																						  *
 *	You should have received a copy of the GNU General Public License					  *
 *	along with The Chili DirectX Framework.  If not, see <http://www.gnu.org/licenses/>.  *
 ******************************************************************************************/
#include "Game.h"

Game::Game( HWND hWnd,const KeyboardServer& kServer )
:	gfx ( hWnd ), 
	kbd( kServer ),
	moveX( 375 ),
	moveY( 225 ),
	resizeX( 50 ),
	resizeY( 50 )
{}

void Game::Go()
{
	gfx.BeginFrame();
	ComposeFrame();
	gfx.EndFrame();
}

void Game::drawSquare()
{
	for( x = moveX ; x < moveX + resizeX ; x++){
		for ( y = moveY ; y < moveY + resizeY ; y++ )
		{ gfx.PutPixel(x,y,255,255,255);
		}
	}
}

void Game::ComposeFrame()
{
	// Local variables

	int movementSpeed = 1;
	int resizeSpeed = 1;

	// Draw Square Function

	drawSquare();
	
	// Clamp to screen code

	if( moveX < 1 )
	{
		moveX = 1;
	}
	if( moveX + resizeX > 800 )
	{
		moveX = 800 - resizeX;
	}
	if( moveY < 1 )
	{
		moveY = 1;
	}
	if( moveY + resizeY > 600 )
	{
		moveY = 600 - resizeY;
	}

	// Whole function moving

	if(!(kbd.SpaceIsPressed()) )
	{
		if(kbd.RightIsPressed() )
		{
			moveX = moveX + movementSpeed;
		}
		if(kbd.LeftIsPressed() )
		{
			moveX = moveX - movementSpeed;
		}
		if(kbd.UpIsPressed() )
		{
			moveY = moveY - movementSpeed;
		}
		if(kbd.DownIsPressed() )
		{
			moveY = moveY + movementSpeed;
		}
	} else if(kbd.SpaceIsPressed() )
	{
		if(kbd.LeftIsPressed() )
		{
			resizeX = resizeX - resizeSpeed;
		}
		if(kbd.UpIsPressed() )
		{
			resizeY = resizeY - resizeSpeed;
		}
		if(kbd.RightIsPressed() )
		{
			resizeX = resizeX + resizeSpeed;
		}
		if(kbd.DownIsPressed() )
		{
			resizeY = resizeY + resizeSpeed;
		}

	}

}
Game.h

Code: Select all

/****************************************************************************************** 
 *	Chili DirectX Framework Version 11.12.17											  *	
 *	Game.h																				  *
 *	Copyright 2011 PlanetChili.net														  *
 *																						  *
 *	This file is part of The Chili DirectX Framework.									  *
 *																						  *
 *	The Chili DirectX Framework is free software: you can redistribute it and/or modify	  *
 *	it under the terms of the GNU General Public License as published by				  *
 *	the Free Software Foundation, either version 3 of the License, or					  *
 *	(at your option) any later version.													  *
 *																						  *
 *	The Chili DirectX Framework is distributed in the hope that it will be useful,		  *
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of						  *
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the						  *
 *	GNU General Public License for more details.										  *
 *																						  *
 *	You should have received a copy of the GNU General Public License					  *
 *	along with The Chili DirectX Framework.  If not, see <http://www.gnu.org/licenses/>.  *
 ******************************************************************************************/
#pragma once

#include "D3DGraphics.h"
#include "Keyboard.h"

class Game
{
public:
	Game( HWND hWnd,const KeyboardServer& kServer );
	void Go();
private:

	// TODO: User functions go here

	void ComposeFrame();
	void drawSquare();

private:

	// TODO: User variables go here

	int x;
	int y;
	int moveX;
	int moveY;
	int resizeX;
	int resizeY;

	D3DGraphics gfx;
	KeyboardClient kbd;
};

So I made a function for the entity called 'drawSquare'.
Inside that function I created the square using nested 'for loops'.
The loops contain different variables that control movement and size.



Movement and size can not be modified simultaneously because I disabled movement while spacebar is pressed, in order to resize with the same keys. I tried to make them perform simultaneously, but it looks so damn ugly and causes fps to drop a little.



The movementSpeed and resizeSpeed are also customizable.
Plus I made it clamp to screen so it can't go off to cause errors.


What I learned from the clamping is that you actually have to define the clamping code BEFORE you define the movement of your square... if you do it the other way around, the program will crash when trying to leave the screen at a high velocity (if the speed is modified).

Is it good enough to pass your righteous judgement, Chili? ;)

PS - I'm gonna mess around with the colors now, but since that was an extra assignment, I wanted to get this stuff off of my chest first.
My Weblog: http://www.gamer-bay.com/
Keep track of my learning progress on the C++ programming language.

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Homework Assignment Episode 9

Post by chili » January 24th, 2012, 12:53 pm

Very good Natox! I would give this assigment an 'A'. :)

There are only two points that I think could be improved upon.

Firstly, I'm glad you went the route of creating a separate drawSquare() function for the routine. However, the loop variables should be declared inside the function since they do not need to persist over calls. You can declare them right inside the for loop initializer section.

Secondly, instead of having the function access the position and dimension variables directly, you should pass those values to the function as parameters. If you imagine that you have many rectangles, you can see right away why this would be a better solution. As things stand, you would need a separate function for each rectangle!

Overall, very good stuff Natox. Can't wait to see what you come up with for the bonus assignment. :)
Chili

Natox
Posts: 51
Joined: January 10th, 2012, 2:51 pm
Location: the Netherlands
Contact:

Re: Homework Assignment Episode 9

Post by Natox » January 24th, 2012, 2:21 pm

However, the loop variables should be declared inside the function since they do not need to persist over calls. You can declare them right inside the for loop initializer section.
You mean like this? PS, different variables since I started over.

Code: Select all

void Game::drawSquare()
{
	for( x = x2move = 10 ; x < x2move + x2size ; x++)
	{
		for( y = y2move = 10 ; y < y2move + y2size ; y++)
		{
			gfx.PutPixel(x,y,255,255,255);
		}
	}
}
----------------------------------------------------------------------------------------
Secondly, instead of having the function access the position and dimension variables directly, you should pass those values to the function as parameters.
Well the thing is, function parameters get me really confused. I imagine, that a function uses the parameters and passes the parameter values to all the same variables used inside the function.

But, whenever these function parameters need to be changed, they should be variables themself right? And I don't know how to do that, since the function called in the ComposeFrame section only takes parameter values right? It does not allow you to change these values as a user of the program.... or am I wrong and plain stupid? :lol:
My Weblog: http://www.gamer-bay.com/
Keep track of my learning progress on the C++ programming language.

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Homework Assignment Episode 9

Post by chili » January 25th, 2012, 2:38 am

Natox wrote:But, whenever these function parameters need to be changed, they should be variables themself right? And I don't know how to do that, since the function called in the ComposeFrame section only takes parameter values right? It does not allow you to change these values as a user of the program...
Head... explodes... :shock:

I'm not really sure what you mean by this Natox, so you're going to have to elaborate for me. I'm sure you have a misconception here somewhere, we just have to find out what it is exactly.

Here is how I would write the function:

Code: Select all

void Game::drawSquare( int x1,int y1,int width,int height )
{
   for( int xCount = x1; xCount < x1 + width ; xCount++)
   {
      for( int yCount = y1; yCount < y1 + height; yCount++)
      {
         gfx.PutPixel( xCount,yCount,255,255,255 );
      }
   }
}
You should declare 4 variables in the Game object; squareX, squareY, squareWidth, and squareHeight.

Then you draw it by calling: drawSquare( squareX,squareY,squareWidth,squareHeight );

Make sense? ;)
Chili

Natox
Posts: 51
Joined: January 10th, 2012, 2:51 pm
Location: the Netherlands
Contact:

Re: Homework Assignment Episode 9

Post by Natox » January 25th, 2012, 6:13 pm

Hey chili, thanks for the reply! It was a misconception indeed!
I thought it was impossible to call a function and put variable parameters in that call again!
So this was my logic:
drawSquare(40,40); and I thought this was impossible: drawSquare(x,y);.

So now that I know this, it makes it a WHOLE lot easier!!
The only question I have is... where do I initialize the width and height variables?
Those are also only needed by the function itself, but I can not initialize those values in the loop.
My Weblog: http://www.gamer-bay.com/
Keep track of my learning progress on the C++ programming language.

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Homework Assignment Episode 9

Post by chili » January 26th, 2012, 10:56 am

The variables width and height are parameter variables used inside the draw function. You don't really "initialize" parameter variables, because they start off initialized with the values passed to the function as soon as you enter the function.
Chili

Natox
Posts: 51
Joined: January 10th, 2012, 2:51 pm
Location: the Netherlands
Contact:

Re: Homework Assignment Episode 9

Post by Natox » January 26th, 2012, 11:45 am

Makes sense, never thought about that to be honest. Thank you!
I'll start from scratch again this evening. I'm working right now.
Mobile internet for the win!
My Weblog: http://www.gamer-bay.com/
Keep track of my learning progress on the C++ programming language.

Natox
Posts: 51
Joined: January 10th, 2012, 2:51 pm
Location: the Netherlands
Contact:

Re: Homework Assignment Episode 9

Post by Natox » January 26th, 2012, 6:50 pm

I'm sorry mate, but I just completely lost it. Nothing seems to work the proper way.
I am trying to get the square in the screen initiated in the forloop, but it just throws other variables from that function at a random position outside the screen. Kinda pisses me off.

You're telling me that it's better to initialize the function variables inside the function ye?
So that means (since all of the variables are used inside this function) that I shouldn't have any initial variables on the game constructor right?

So, the only way I can think of to initialize the function is by using the for-loop initializer area like this:
for( int xCount = x1 = 100 ; xCount < x1 + width ; xCount++).

Could you pop up Game.ccp and Game.h, so I can figure this shit out :(

Thanks mate
My Weblog: http://www.gamer-bay.com/
Keep track of my learning progress on the C++ programming language.

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Homework Assignment Episode 9

Post by chili » January 27th, 2012, 2:56 am

Natox wrote:for( int xCount = x1 = 100 ; xCount < x1 + width ; xCount++)
The red part is evil, kill it!!

What the red statement does is:

First, set the value of x1 to 100.
Second, set the value of xCount to the value of x1 (100).

If you do this, no matter what x1 is, it will always be set to 100 when you enter this loop. Therefore, the parameter passed to the function corresponding to x1 will become irrelevent!! :o

You need to separate (in your mind and in your code) the variables used in the function to draw a box and the variables used in the Game object to keep track of your specific box. You need to initialize your Game object variables in your constructor, then you update them in the ComposeFrame() function according to what keys are pressed, and finally you pass the values stored in the Game object variables to the draw function.
Natox wrote:Could you pop up Game.ccp and Game.h, so I can figure this shit out
If you mean that you want the complete files, I can do that, but not right now because I'm at work. :)
Chili

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Homework Assignment Episode 9

Post by chili » January 27th, 2012, 4:49 pm

Hope this helps:

Code: Select all

***************************************************************************************** 
 *	Chili DirectX Framework Version 11.12.17											  *	
 *	Game.cpp																			  *
 *	Copyright 2011 PlanetChili.net														  *
 *																						  *
 *	This file is part of The Chili DirectX Framework.									  *
 *																						  *
 *	The Chili DirectX Framework is free software: you can redistribute it and/or modify	  *
 *	it under the terms of the GNU General Public License as published by				  *
 *	the Free Software Foundation, either version 3 of the License, or					  *
 *	(at your option) any later version.													  *
 *																						  *
 *	The Chili DirectX Framework is distributed in the hope that it will be useful,		  *
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of						  *
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the						  *
 *	GNU General Public License for more details.										  *
 *																						  *
 *	You should have received a copy of the GNU General Public License					  *
 *	along with The Chili DirectX Framework.  If not, see <http://www.gnu.org/licenses/>.  *
 ******************************************************************************************/
#include "Game.h"

Game::Game( HWND hWnd,const KeyboardServer& kServer )
:	gfx ( hWnd ),
	kbd( kServer ),
	boxX( 100 ),
	boxY( 100 ),
	boxWidth( 100 ),
	boxHeight( 100 )
{}

void Game::Go()
{
	gfx.BeginFrame();
	ComposeFrame();
	gfx.EndFrame();
}

void Game::DrawBox( int x1,int y1,int width,int height )
{
	for( int x = x1; x < x1 + width; x++ )
	{
		for( int y = y1; y < y1 + height; y++ )
		{
			gfx.PutPixel( x,y,255,255,255 );
		}
	}
}

void Game::ComposeFrame()
{
	if( kbd.RightIsPressed() )
	{
		if( kbd.SpaceIsPressed() )
		{
			boxWidth++;
		}
		else
		{
			boxX++;
		}
	}
	if( kbd.LeftIsPressed() )
	{
		if( kbd.SpaceIsPressed() )
		{
			boxWidth--;
		}
		else
		{
			boxX--;
		}
	}
	if( kbd.DownIsPressed() )
	{
		if( kbd.SpaceIsPressed() )
		{
			boxHeight++;
		}
		else
		{
			boxY++;
		}
	}
	if( kbd.UpIsPressed() )
	{
		if( kbd.SpaceIsPressed() )
		{
			boxHeight--;
		}
		else
		{
			boxY--;
		}
	}

	DrawBox( boxX,boxY,boxWidth,boxHeight );
}

Code: Select all

/****************************************************************************************** 
 *	Chili DirectX Framework Version 11.12.17											  *	
 *	Game.h																				  *
 *	Copyright 2011 PlanetChili.net														  *
 *																						  *
 *	This file is part of The Chili DirectX Framework.									  *
 *																						  *
 *	The Chili DirectX Framework is free software: you can redistribute it and/or modify	  *
 *	it under the terms of the GNU General Public License as published by				  *
 *	the Free Software Foundation, either version 3 of the License, or					  *
 *	(at your option) any later version.													  *
 *																						  *
 *	The Chili DirectX Framework is distributed in the hope that it will be useful,		  *
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of						  *
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the						  *
 *	GNU General Public License for more details.										  *
 *																						  *
 *	You should have received a copy of the GNU General Public License					  *
 *	along with The Chili DirectX Framework.  If not, see <http://www.gnu.org/licenses/>.  *
 ******************************************************************************************/
#pragma once

#include "D3DGraphics.h"
#include "Keyboard.h"

class Game
{
public:
	Game( HWND hWnd,const KeyboardServer& kServer );
	void Go();
private:
	void ComposeFrame();
	/********************************/
	/*  User Functions              */

	void DrawBox( int x1,int y1,int width,int height );

	/********************************/
private:
	D3DGraphics gfx;
	KeyboardClient kbd;
	/********************************/
	/*  User Variables              */
	
	int boxX;
	int boxY;
	int boxWidth;
	int boxHeight;

	/********************************/
};
Chili

Post Reply