-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGameRoot.cpp
More file actions
127 lines (93 loc) · 3.94 KB
/
GameRoot.cpp
File metadata and controls
127 lines (93 loc) · 3.94 KB
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
#include "Utility/package.hpp"
#include "package.h"
//---------------------------------------------------------------------------------
// Written by Terence J. Grant - tjgrant [at] tatewake [dot] com
// Find the full tutorial at: http://gamedev.tutsplus.com/series/
//----------------------------------------------------------------------------------
GameRoot* gApp = GameRoot::getInstance();
#define kScale 3.0f
void GameRoot::DrawRightAlignedString(const std::string& str, int32_t y)
{
int32_t textWidth = int32_t(Art::getInstance()->getFont().getTextSize(str).width * kScale);
mSpriteBatch->drawString(1, Art::getInstance()->getFont(), str, tPoint2f(mViewportSize.width - textWidth - 5, y), tColor4f(1,1,1,1),
0, tPoint2f(0,0), tVector2f(kScale));
}
GameRoot::GameRoot()
: mParticleManager(1024 * 20),
mViewportSize(1136, 640),
mSpriteBatch(NULL),
mGrid(NULL)
{}
tDimension2f GameRoot::getViewportSize()
{
return mViewportSize;
}
ParticleManager* GameRoot::getParticleManager()
{
return &mParticleManager;
}
Grid* GameRoot::getGrid()
{
return mGrid;
}
void GameRoot::onInitView()
{
Art::getInstance();
//Sound::getInstance();
const int maxGridPoints = 600;
tVector2f gridSpacing = tVector2f((float)sqrtf(mViewportSize.width * mViewportSize.height / maxGridPoints));
mGrid = new Grid(tRectf(0,0, mViewportSize), gridSpacing);
EntityManager::getInstance()->add(PlayerShip::getInstance());
//Sound::getInstance()->getMusic()->play(0, (uint32_t)-1);
mViewport = new tAutosizeViewport(mViewportSize);
mSpriteBatch = new tSpriteBatch;
mSpriteBatch->setProjection(tMatrix4x4f::ortho(0,mViewportSize.width, mViewportSize.height, 0));
glClearColor(0,0,0,1);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glHint(GL_GENERATE_MIPMAP_HINT, GL_DONT_CARE);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
}
void GameRoot::onRedrawView(float time)
{
char buf[80];
#pragma unused(time)
Input::getInstance()->update();
PlayerStatus::getInstance()->update();
EntityManager::getInstance()->update();
EnemySpawner::getInstance()->update();
mParticleManager.update();
mGrid->update();
EntityManager::getInstance()->draw(mSpriteBatch);
mGrid->draw(mSpriteBatch);
mParticleManager.draw(mSpriteBatch);
// Draw user interface
sprintf(buf, "Lives: %d", PlayerStatus::getInstance()->getLives());
mSpriteBatch->drawString(1, Art::getInstance()->getFont(), buf, tPoint2f(5,5), tColor4f(1,1,1,1),
0, tPoint2f(0,0), tVector2f(kScale));
sprintf(buf, "Score: %d", PlayerStatus::getInstance()->getScore());
DrawRightAlignedString(buf, 5);
sprintf(buf, "Multiplier: %d", PlayerStatus::getInstance()->getMultiplier());
DrawRightAlignedString(buf, 35);
mSpriteBatch->draw(0, Art::getInstance()->getPointer(), Input::getInstance()->getMousePosition(), tOptional<tRectf>());
VirtualGamepad::getInstance()->draw(mSpriteBatch);
if (PlayerStatus::getInstance()->getIsGameOver())
{
sprintf(buf, "Game Over\nYour Score: %d\nHigh Score: %d",
PlayerStatus::getInstance()->getScore(),
PlayerStatus::getInstance()->getHighScore());
tDimension2f textSize = Art::getInstance()->getFont().getTextSize(buf);
mSpriteBatch->drawString(1, Art::getInstance()->getFont(), buf, (mViewportSize - textSize) / 2, tColor4f(1,1,1,1),
0, tPoint2f(0,0), tVector2f(kScale));
}
//Run redraw logic here
mViewport->run();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
mSpriteBatch->end();
glFlush();
}