-
Notifications
You must be signed in to change notification settings - Fork 0
/
TitleScreen.cpp
142 lines (113 loc) · 3.86 KB
/
TitleScreen.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "TitleScreen.h"
#include "Game.h"
#include "Invader.h"
#include "SDLColors.h"
#include "StlUtil.h"
#include "Texture.h"
TitleScreen::TitleScreen( const string &titleText ) {
RectF windowRectangle = sdl->getWindowRectangle();
RectF titleBox = windowRectangle;
titleBox.size *= Vector2f( .75, .33 );
Vector2f titleCenter = sdl->getWindowSize() * Vector2f( .5, .25 );
titleBox.setCenter( titleCenter );
titleSprite = Sprite::Text( titleText, titleBox, White );
titleBox.pos += titleBox.size * .01;
titleShadow = Sprite::Text( titleText, titleBox, White );
RectF pushKeyBox;
pushKeyBox.pos = windowRectangle.size * Vector2f( .65, .8 );
pushKeyBox.size = windowRectangle.size * Vector2f( .25, .1 );
pushKey = Sprite::Text( "Push Enter or Space", pushKeyBox, White );
// Need the title texture to add anim frames using it
shared_ptr<Texture> titleTexture = titleSprite->animation.getCurrentFrame().tex;
// create a random color cycle animation
for( int i = 0; i < 15; i++ ) {
titleSprite->addAnimationFrame( titleTexture, SDL_RandomSolidColor(), .05 );
}
// init the menu items
menuItemSize = sdl->getWindowSize() * Vector2f( .15, .15 );
startMenuPos = titleCenter;
startMenuPos.y += titleBox.size.y / 2;
addMenuItem( "Play" );
addMenuItem( "Test" );
// Init the pointer
Vector2f pointerPos = startMenuPos;
pointerPos.x = getMenuPointerX();
pointer = std::make_shared<Sprite>( RectF( pointerPos, Vector2f( getMenuPointerSize() ) ), AnimationId::MenuPointer );
// Bonus:
Vector2f eaSpriteSize = windowRectangle.size.min() * .1;
Vector2f eSpritePos = titleBox.pos;
eSpritePos.x -= eaSpriteSize.x;
auto e = std::make_shared<Invader>( RectF( eSpritePos, eaSpriteSize ), AnimationId::InvaderE );
// Adjust the tilt of these sprites
for( auto&& frame : e->animation.frames ) {
frame.angle = -35;
}
auto a = std::make_shared<Invader>( RectF( eSpritePos + Vector2f( 35, -25 ), eaSpriteSize ), AnimationId::InvaderA );
for( auto&& frame : a->animation.frames ) {
frame.angle = -35;
}
invaders.push_back( e );
invaders.push_back( a );
}
void TitleScreen::update( float t ) {
titleShadow->update( t );
titleSprite->update( t );
pushKey->update( t );
for( auto menuItem : menuItems ) {
menuItem->update( t );
}
pointer->update( t );
for( auto invader : invaders ) {
invader->update( t );
}
}
void TitleScreen::draw() {
titleShadow->draw();
titleSprite->draw();
pushKey->draw();
for( auto menuItem : menuItems ) {
menuItem->draw();
}
pointer->draw();
for( auto invader : invaders ) {
invader->draw();
}
}
GameState TitleScreen::getLaunchState() {
if( !pointerIndex ) {
return GameState::Running;
}
else {
return GameState::Test;
}
}
void TitleScreen::addMenuItem( const string &menuItemText ) {
Vector2f nextMenuItemPos = startMenuPos;
nextMenuItemPos.y += menuItems.size() * menuItemSize.y;
RectF menuItemBox( nextMenuItemPos, menuItemSize );
menuItemBox.pos.x -= menuItemSize.x / 2;
shared_ptr<Sprite> menuItemSprite = Sprite::Text( menuItemText, menuItemBox, White );
menuItems.push_back( menuItemSprite );
}
float TitleScreen::getMenuPointerSize() const {
// The menu pointer is the same size as the HEIGHT of a menu items
return menuItemSize.y;
}
float TitleScreen::getMenuPointerX() const {
// The menu pointer
return startMenuPos.x - getMenuPointerSize()/2 - menuItemSize.x;
}
void TitleScreen::pointerUp() {
decycleArrayIndex( pointerIndex, menuItems.size() );
updatePointerPosition();
}
void TitleScreen::pointerDown() {
cycleArrayIndex( pointerIndex, menuItems.size() );
updatePointerPosition();
}
void TitleScreen::updatePointerPosition() {
Vector2f pointerPos = startMenuPos;
pointerPos.x = getMenuPointerX();
pointerPos.y += menuItemSize.y * pointerIndex;
pointer->setPos( pointerPos );
}