-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
153 lines (130 loc) · 4.36 KB
/
Copy pathgame.cpp
File metadata and controls
153 lines (130 loc) · 4.36 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "game.h"
GameManager::GameManager() : StopMainLoop(false), CenterX(0), CenterY(0)
{
cfg = SingletonConfig::config();
}
void GameManager::__CleanCurrentLevel()
{
for (std::vector<ObjectData*>::iterator it = CurrentLevel.begin(); it != CurrentLevel.end(); it++) {
delete *it;
}
}
void GameManager::__RenderCurrentLevel()
{
for (std::vector<ObjectData*>::iterator it = CurrentLevel.begin(); it != CurrentLevel.end(); it++) {
(*it)->Shift(CenterX, CenterY);
(*it)->Rendered = ((*it)->local.x) < render->Width && ((*it)->local.y) < render->Height && ((*it)->local.x + (*it)->local.w) > 0 && ((*it)->local.y + (*it)->local.h) > 0;
(*it)->Render(render->ren);
}
}
int GameManager::Initialize()
{
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
LogSDLError(std::cout, "SDL_Init");
return 1;
}
if ((IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) != IMG_INIT_PNG) {
LogSDLError(std::cout, "IMG_Init");
SDL_Quit();
return 1;
}
if(TTF_Init()==-1) {
LogSDLError(std::cout, "TTF_Init");
return 1;
}
window = new WindowManager;
if (window->Initialize()) {
LogError(std::cout, "Error initializing WindowManager.\n");
return 1;
}
render = new RenderManager;
if (render->Initialize(window->win)) {
LogError(std::cout, "Error initializing RenderManager.\n");
return 1;
}
assets = new AssetsManager;
if (assets->Initialize(render->ren)) {
LogError(std::cout, "Error initializing AssetsManager.\n");
return 1;
}
levels = new LevelsManager;
if (levels->Initialize()) {
LogError(std::cout, "Error initializing LevelsManager.\n");
return 1;
}
movement = new MovementManager;
if (movement->Initialize()) {
LogError(std::cout, "Error initializing MovementManager.\n");
return 1;
}
assets->PreLoadAssets();
levels->PreLoadLevels();
SingletonEvents::AddToTimer(movement->UpdateTimer, std::bind<void>([this](double dt, double elapsed) {
this->movement->SetToCenterInPixels(this->CenterX, this->CenterY);
this->CenterX -= this->render->Width / 2;
this->CenterY -= this->render->Height / 2;
}, _1, _2));
SingletonEvents::RegisterTimer(-1.0, std::bind<void>([this](double dt, double elapsed)
{
SDL_RenderClear(this->render->ren);
this->__RenderCurrentLevel();
this->RenderAsset("DefaultCursor", this->render->Width / 2, this->render->Height / 2);
SDL_RenderPresent(this->render->ren);
}, _1, _2));
SingletonEvents::Register(SDL_QUIT, std::bind<void>([this](SDL_Event &e)
{
this->StopMainLoop = true;
}, _1));
return 0;
}
int GameManager::RenderAsset(char const * name, uint32_t x, uint32_t y)
{
SDL_Rect dst;
dst.x = x;
dst.y = y;
AssetData * asset = assets->LoadAsset(name);
if (asset == NULL) return 1;
dst.w = (*asset->json)["Size"][2].asInt();
dst.h = (*asset->json)["Size"][3].asInt();
SDL_RenderCopy(render->ren, asset->tex == NULL ? SDL_CreateTextureFromSurface(render->ren,asset->surf) : asset->tex, NULL, &dst);
return 0;
}
int GameManager::LoadLevel(char const * name)
{
LevelData * level;
try {
level = levels->levels->at(name);
} catch (const std::out_of_range &e) {
LogError(std::cout, "Error loading level.\n");
LogError(std::cout, e.what());
return 1;
}
assets->UnloadAssets();
CurrentLevel.clear();
AssetData * asset;
ObjectData * obj;
movement->CreateWorld((*level->json)["Gravity"][0].asDouble(),(*level->json)["Gravity"][1].asDouble());
for (Json::ValueIterator it = (*level->json)["Assets"].begin(); it != (*level->json)["Assets"].end(); it++, asset = NULL) {
asset = assets->LoadAsset(it->asCString());
if (asset != NULL) {
for (Json::ValueIterator it2 = (*level->json)["Tiles"][it->asCString()].begin(); it2 != (*level->json)["Tiles"][it->asCString()].end(); it2++) {
obj = new ObjectData;
obj->Initialize(asset->tex, (*it2)[0].asInt(), (*it2)[1].asInt(), (*asset->json)["Size"][2].asUInt(), (*asset->json)["Size"][3].asUInt());
movement->AddStaticBody(obj->world);
CurrentLevel.push_back(obj);
}
}
}
b2Body * b = movement->AddDynamicBody((*level->json)["Spawn"][0].asInt(), (*level->json)["Spawn"][1].asInt(), 64, 64);
movement->SetFollowBody(b);
movement->SetControlBody(b);
return 0;
}
void GameManager::MainLoop()
{
SingletonEvents::StartTimer();
while(!StopMainLoop) {
SingletonEvents::Process();
SingletonEvents::ProcessTimers();
}
}