-
Notifications
You must be signed in to change notification settings - Fork 0
/
BPlayerControl.cpp
88 lines (76 loc) · 2.07 KB
/
BPlayerControl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "BPlayerControl.h"
BPlayerControl::BPlayerControl()
{
//allocate memory for texture
player_Board = new BTexture;
//sets starting position of board
bBoardPosition.x = getGametableWidth() / 2;
bBoardPosition.y = getGametableHeightEnd() - getBoardHeight();
//set path for board media
board_Media = "Breakout_Media/player_board.png";
//load board texture
if (!loadBoardMedia())
{
std::cout << "Failed to load Board Texture\n";
}
}
BPlayerControl::~BPlayerControl()
{
//Clear texture if it exists
if (player_Board != NULL)
{
player_Board->clearTexture();
delete player_Board;
player_Board = NULL;
bBoardPosition = { 0, 0 };
}
}
SDL_Point BPlayerControl::getBoardPosition()
{
return bBoardPosition;
}
void BPlayerControl::handleEvent(SDL_Event* e)
{
if (e->type == SDL_MOUSEMOTION)
{
//Get mouse position
int x, y;
SDL_GetMouseState(&x, &y);
//Check if mouse is in area of game and move board left/right
switch (e->type)
{
case SDL_MOUSEMOTION:
//if mouse is left of board
if (bBoardPosition.x > getGametableWidthStart() && x < bBoardPosition.x)
{
bBoardPosition.x -= 10;
break;
}
//if mouse is right of board
else if (x > bBoardPosition.x + getBoardWidth() && bBoardPosition.x < (getGametableWidthEnd() - getBoardWidth()))
{
bBoardPosition.x += 10;
break;
}
default:
break;
}
}
}
void BPlayerControl::renderPlayerBoard()
{
//render player board texture in BTexture class
player_Board->renderTexture(bBoardPosition.x, bBoardPosition.y);
}
bool BPlayerControl::loadBoardMedia()
{
//Loading success flag
bool success = true;
//load Player Board image
if (!player_Board->loadFromFile(board_Media))
{
std::cout << "Failed to load Player_Board texture!\n";
success = false;
}
return success;
}