midterm final iteration
user_2869209
c_cpp
3 years ago
10 kB
2
Indexable
#include "MyProject.h"
#include <Windowsx.h> // for GET__LPARAM macros
#include <SpriteBatch.h>
#include <d3d11.h>
#include <SimpleMath.h>
#include <DirectXColors.h>
#include <sstream>
#include "Collision2D.h"
using namespace DirectX;
using namespace DirectX::SimpleMath;
//Author: Parker Heil. 4363015.
//The purpose of this assignment is: To create brickbreak for the midterm assignment.
// Using a menu, functions, and deltatime.
//I've used assets from a different project so TECHNICALLY theyre unique! hahaha.
//Date: 2022/11/08.
float RandFloat() { return float(rand()) / float(RAND_MAX); }
//----------------------------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int nShowCmd)
{
MyProject application(hInstance); // Create the class variable
if (application.InitWindowsApp(L"BREAKOUT!", nShowCmd) == false) // Initialize the window, if all is well show and update it so it displays
{
return 0; // Error creating the window, terminate application
}
if (application.InitializeDirect3D())
{
application.SetDepthStencil(true); // Tell DirectX class to create and maintain a depth stencil buffer
application.InitializeTextures();
application.MessageLoop(); // Window has been successfully created, start the application message loop
}
return 0;
}
//----------------------------------------------------------------------------------------------
// Constructor
//----------------------------------------------------------------------------------------------
MyProject::MyProject(HINSTANCE hInstance)
: DirectXClass(hInstance)
{
mousePos = Vector2(clientWidth * 0.5f, clientHeight * 0.5f);
buttonDown = false;
spriteBatch = NULL;
currentSelectedIndex = -1;
message = L"Mouse was clicked here";
ClearColor = Color(DirectX::Colors::DarkGray.v);
velocity = Vector2(100, 100);
balls.SetVelocity(velocity, 0);
ByeBye = Vector2(2000, 2000);
}
//----------------------------------------------------------------------------------------------
// Destructor
//----------------------------------------------------------------------------------------------
MyProject::~MyProject()
{
if (spriteBatch)
{
delete spriteBatch;
spriteBatch = NULL;
}
if (states)
{
delete states;
states = NULL;
}
delete spriteBatch;
}
//----------------------------------------------------------------------------------------------
// Initialize any fonts we need to use here
//----------------------------------------------------------------------------------------------
void MyProject::InitializeTextures()
{
// initialize the sprite batch
spriteBatch = new DirectX::SpriteBatch(DeviceContext);
commonStates = new CommonStates(D3DDevice);
arial16.InitializeFont(D3DDevice, DeviceContext, L"..\\Font\\Arial16.spritefont");
buttonTexture.Load(D3DDevice, L"..\\Textures\\blueSheet.png");
brickTexture.Load(D3DDevice, L"..\\Textures\\blueSheet.png");
ball.Load(D3DDevice, L"..\\Textures\\ball.png");
paddleTexture.Load(D3DDevice, L"..\\Textures\\paddle.png");
//font.InitializeFont()
paddle.Initialize(&paddleTexture, Vector2(500, 700), 0.0f, 0.5f, Color(1, 1, 1),0);
balls.Initialize(&ball, Vector2(400, 300), 0, .010, Color(1, 1, 1), 0);
for (int i = 0; i < 2; i++)
{
buttonSprite[i].Initialize(&buttonTexture, Vector2(500, 300 + (i * buttonSpace)), 0, 1, Color(1, 1, 1), 0);
}
Vector2 brickPosition;
brickPosition.x = 110;
brickPosition.y = 60;
for (int i = 0; i < 15; i++)
{
brickSprite[i].Initialize(&brickTexture, brickPosition, 0, 1, Color(1, 1, 1), 0);
brickPosition.x += 200;
if (i == 4 || i == 9)
{
brickPosition.x = 110;
brickPosition.y += 60;
}
}
}
//----------------------------------------------------------------------------------------------
// Window message handler
//----------------------------------------------------------------------------------------------
LRESULT MyProject::ProcessWindowMessages(UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_MOUSEMOVE:
mousePos.x = (float)GET_X_LPARAM(lParam);
mousePos.y = (float)GET_Y_LPARAM(lParam);
return 0;
case WM_LBUTTONUP:
buttonDown = false;
mousePos.x = (float)GET_X_LPARAM(lParam);
mousePos.y = (float)GET_Y_LPARAM(lParam);
break;
case WM_LBUTTONDOWN:
buttonDown = true;
mousePos.x = (float)GET_X_LPARAM(lParam);
mousePos.y = (float)GET_Y_LPARAM(lParam);
OnMouseDown();
break;
case WM_KEYUP:
if (wParam >= '0' && wParam <= '4')
{
PresentInterval = wParam - '0';
}
if (wParam == VK_LEFT) { right = false; }
if (wParam == VK_RIGHT) { left = false; }
break;
case WM_KEYDOWN:
if (wParam == VK_LEFT) { right = true; }
if (wParam == VK_RIGHT) { left = true; }
break;
}
// let the base class handle remaining messages
return DirectXClass::ProcessWindowMessages(msg, wParam, lParam);
}
//----------------------------------------------------------------------------------------------
// Called by the render loop to render a single frame
//----------------------------------------------------------------------------------------------
void MyProject::Render(void)
{
std::wostringstream ws, ws1, ws2;
ws << score;
ws1 << L"Play";
ws2 << L"Quit";
spriteBatch->Begin(SpriteSortMode_BackToFront, commonStates->NonPremultiplied());
MenuFunction();
spriteBatch->End();
DirectXClass::Render();
ButtonFonts(ws1, ws2, ws);
}
void MyProject::MenuFunction()
{
for (int i = 0; i < 2; i++)
{
buttonSprite[i].Draw(spriteBatch);
}
if (gameStart)
{
for (int i = 0; i < 15; i++)
{
brickSprite[i].Draw(spriteBatch);
balls.Draw(spriteBatch);
paddle.Draw(spriteBatch);
}
}
}
void MyProject::ButtonFonts(std::wostringstream& ws1, std::wostringstream& ws2, std::wostringstream& ws)
{
if (!gameStart)
{
arial16.PrintMessage(475, 288 + 0 * buttonSpace, ws1.str(), FC_RED);
arial16.PrintMessage(475, 288 + 1 * buttonSpace, ws2.str(), FC_RED);
}
else
{
arial16.PrintMessage(75, 700, ws.str(), FC_RED);
}
}
//----------------------------------------------------------------------------------------------
// Called every frame to update objects.
// deltaTime: how much time in seconds has elapsed since the last frame
//----------------------------------------------------------------------------------------------
void MyProject::Update(float deltaTime)
{
gameFunction(deltaTime);
ButtonUpdate();
}
void MyProject::gameFunction(float deltaTime)
{
Vector2 paddlePosition = paddle.GetPosition();
if (right) { paddlePosition.x -= 3; paddle.SetPosition(paddlePosition); }
if (left) { paddlePosition.x += 3; paddle.SetPosition(paddlePosition); }
Vector2 ballVelocity = balls.GetVelocity();
Vector2 ballPosition = balls.GetPosition();
ballPosition += ballVelocity * deltaTime;
balls.SetPosition(ballPosition);
if (ballPosition.y > clientHeight) { ballVelocity.y = -ballVelocity.y; balls.SetVelocity(ballVelocity, 0); }
if (ballPosition.y < 0) { ballVelocity.y = -ballVelocity.y; balls.SetVelocity(ballVelocity, 0); }
if (ballPosition.x > clientWidth) { ballVelocity.x = -ballVelocity.x; balls.SetVelocity(ballVelocity, 0); }
if (ballPosition.x < 0) { ballVelocity.x = -ballVelocity.x; balls.SetVelocity(ballVelocity, 0); }
Box2D paddleCollider(paddle.GetPosition(), paddle.GetExtents());
Circle ballCollider;
ballCollider.center = balls.GetPosition();
ballCollider.radius = balls.GetWidth() * 0.25f;
if (Collision2D::BoxCircleCheck(paddleCollider, ballCollider))
{
balls.SetPosition(Collision2D::ReflectCircleBox(ballCollider, ballVelocity, deltaTime, paddleCollider));
balls.SetVelocity(ballVelocity, 0);
}
for (int o = 0; o < 15; o++)
{
Box2D boxCollider(brickSprite[o].GetPosition(), brickSprite[o].GetExtents());
Circle circleCollider;
circleCollider.center = balls.GetPosition();
circleCollider.radius = balls.GetWidth() * 0.25f;
if (Collision2D::BoxCircleCheck(boxCollider, circleCollider))
{
score++;
remaining--;
brickSprite[o].SetPosition(ByeBye);
balls.SetPosition(Collision2D::ReflectCircleBox(circleCollider, ballVelocity, deltaTime, boxCollider));
balls.SetVelocity(ballVelocity, 0);
}
}
if (brickSprite[7].GetPosition() == ByeBye)
{
paddle.SetScale(1.0f);
}
if (brickSprite[12].GetPosition() == ByeBye)
{
balls.SetVelocity(velocity * 1.5, 0);
}
if (brickSprite[1].GetPosition() == ByeBye)
{
balls.SetScale(0.02f + deltaTime);
}
if (remaining == 0)
{
gameStart = false;
}
}
void MyProject::ButtonUpdate()
{
for (int i = 0; i < 2; i++)
{
buttonSprite[i].SetTextureRegion(0, 0, 190, 49);
bool doesButtonContainMouse = buttonSprite[i].ContainsPoint(mousePos);
//If the mouse cursor is hovering over the button
if (doesButtonContainMouse || currentSelectedIndex == i)
{
buttonSprite[i].SetTextureRegion(190, 50, 380, 94);
buttonSprite[i].SetScale(1.1f);
buttonSprite[i].SetColor(Color(1.5, 1.5, 1.5));
//If the Mouse Button is Pressed Down
if (buttonDown && doesButtonContainMouse)
{
currentSelectedIndex = i;
switch (currentSelectedIndex)
{
case 0:
for (int i = 0; i < 2; i++)
{
buttonSprite[i].SetPosition(Vector2(2000, 2000));
}
gameStart = true;
for (int i = 0; i < 15; i++)
{
brickSprite[i].SetTextureRegion(0, 0, 190, 49);
}
break;
case 1:
PostQuitMessage(1);
break;
}
}
}
else
{
buttonSprite[i].SetColor(Color(1, 1, 1));
buttonSprite[i].SetScale(1);
}
}
}
//----------------------------------------------------------------------------------------------
// Called when the mouse is released
//----------------------------------------------------------------------------------------------
void MyProject::OnMouseDown()
{
// this is called when the left mouse button is clicked
// mouse position is stored in mousePos variable
}
Editor is loading...