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