-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.cpp
98 lines (89 loc) · 3.1 KB
/
menu.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
89
90
91
92
93
94
95
96
97
#include<sstream>
#include "menu.h"
Menu::Menu(SDL_Renderer* rend) :
renderer(rend),
gdata(Gamedata::getInstance()),
hudFrame( {gdata.getXmlInt("menu/loc/x"),
gdata.getXmlInt("menu/loc/y"),
gdata.getXmlInt("menu/width"),
gdata.getXmlInt("menu/height")}
),
backColor({static_cast<Uint8>(gdata.getXmlInt("menu/backColor/r")),
static_cast<Uint8>(gdata.getXmlInt("menu/backColor/g")),
static_cast<Uint8>(gdata.getXmlInt("menu/backColor/b")),
static_cast<Uint8>(gdata.getXmlInt("menu/backColor/a"))}
),
menuColor({static_cast<Uint8>(gdata.getXmlInt("menu/color/r")),
static_cast<Uint8>(gdata.getXmlInt("menu/color/g")),
static_cast<Uint8>(gdata.getXmlInt("menu/color/b")),
static_cast<Uint8>(gdata.getXmlInt("menu/color/a"))}
),
clock( Clock::getInstance() ),
io( IoMod::getInstance() ),
optionLoc( { gdata.getXmlInt("menu/optionLoc/x"),
gdata.getXmlInt("menu/optionLoc/y")}
),
clicks( {Sprite("clickOff"), Sprite("clickOn")} ),
currentClick(0),
currentOption(0),
spaces(gdata.getXmlInt("menu/spaces")),
startClickX(optionLoc[0]-spaces),
startClickY(optionLoc[1]+spaces),
clickX(startClickX),
clickY(startClickY)
{
int noOfOptions = gdata.getXmlInt("menu/noOfOptions");
std::stringstream strm;
for (int i = 0; i < noOfOptions; ++i)
{
strm.clear();
strm.str("");
strm << i;
std::string option("menu/option"+strm.str());
options.push_back(gdata.getXmlStr(option));
}
}
void Menu::incrIcon() {
clickY += spaces;
if ( clickY > static_cast<int>(options.size())*spaces+optionLoc[1]) {
clickY = startClickY;
currentOption = 0;
}
else ++currentOption;
}
void Menu::decrIcon() {
clickY -= spaces;
if ( clickY < spaces+optionLoc[1]) {
clickY = startClickY+2*spaces;
currentOption = options.size()-1;
}
else --currentOption;
}
void Menu::drawBackground() const {
// First set the blend mode so that alpha blending will work;
// the default blend mode is SDL_BLENDMODE_NONE!
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
// Set the hud background color:
SDL_SetRenderDrawColor( renderer, backColor.r, backColor.g,
backColor.b, backColor.a );
// Draw the filled rectangle:
SDL_RenderFillRect( renderer, &hudFrame );
// Set the color for the Menu outline:
SDL_Rect menuFrame = {hudFrame.x+50, hudFrame.y+40,
hudFrame.w-100, hudFrame.h-100};
SDL_SetRenderDrawColor( renderer, menuColor.r,
menuColor.g, menuColor.b, menuColor.a );
SDL_RenderFillRect( renderer, &menuFrame );
}
void Menu::draw() const {
drawBackground();
io.writeText("Options Menu", hudFrame.x+350, hudFrame.y+60);
int space = spaces;
for ( const std::string& option : options ) {
io.writeText(option, optionLoc[0], optionLoc[1]+space);
space += spaces;
}
// We have to draw the clickOn & clickOff relative to the screen,
// and we don't want to offset by the location of the viewprot:
clicks[currentClick].getImage()->draw(0, 0, clickX, clickY);
}