-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
156 lines (132 loc) · 4.99 KB
/
main.cpp
File metadata and controls
156 lines (132 loc) · 4.99 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
154
155
156
#pragma comment(lib,"Box2D.lib")
#pragma comment(lib,"SDL2.lib")
#pragma comment(lib,"SDL2main.lib")
#pragma comment(lib,"SDL2_image.lib")
#pragma comment(lib,"glu32.lib")
#pragma comment(lib,"opengl32.lib")
#include <windows.h>
#include <gl\GLU.h>
#include <iostream>
#include <random>
#include <string>
#include <vector>
#include <map>
#include <Box2D/Box2D.h>
extern "C" {
#include <SDL.h>
#include <SDL_main.h>
#include <SDL_image.h>
#include <SDL_opengl.h>
}
#include "level.h"
#include "StInput.h"
#include "StGameLogic.h"
#include "StDrawLogic.h"
#include "SharedResource.h"
#define FRAMES_PER_SECOND 60
void InitLevelObjects( Level & lvl,
std::unique_ptr<stSharedRes>& gameUnits,
b2World & world) {
SDL_Rect tileSize = lvl.GetTileSize();
//ñîçäà¸ì box2d ñòàòè÷åñêèå îáúåêòû ïî äàííûì èç óðîâíÿ
std::vector<Object> block = lvl.GetObjects("block");
for(int i = 0; i < block.size(); i++)
{
b2BodyDef bodyDef;
bodyDef.type = b2_staticBody;
bodyDef.position.Set(block[i].rect.x + tileSize.x / 2 * (block[i].rect.w / tileSize.x - 1),
block[i].rect.y + tileSize.y / 2 * (block[i].rect.h / tileSize.y - 1));
b2Body* body = world.CreateBody(&bodyDef);
b2PolygonShape shape;
shape.SetAsBox(block[i].rect.w / 2, block[i].rect.h / 2);
body->CreateFixture(&shape,1.0f);
}
//ñîçäà¸ì box2d äèíàìè÷åñêèå îáúåêòû ïî äàííûì èç óðîâíÿ (ìîíåòû)
gameUnits->coin = lvl.GetObjects("coin");
for(int i = 0; i < gameUnits->coin.size(); i++)
{
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(gameUnits->coin[i].rect.x + tileSize.x / 2 * (gameUnits->coin[i].rect.w / tileSize.x - 1),
gameUnits->coin[i].rect.y + tileSize.y / 2 * (gameUnits->coin[i].rect.h / tileSize.y - 1));
bodyDef.fixedRotation = true;
b2Body* body = world.CreateBody(&bodyDef);
b2PolygonShape shape;
shape.SetAsBox(gameUnits->coin[i].rect.w / 2, gameUnits->coin[i].rect.h / 2);
body->CreateFixture(&shape,1.0f);
gameUnits->coinBody.push_back(body);
}
//ñîçäà¸ì box2d äèíàìè÷åñêèå îáúåêòû ïî äàííûì èç óðîâíÿ (âðàãè)
int x = gameUnits->coinBody[0]->GetPosition().x;
gameUnits->enemy = lvl.GetObjects("enemy");
for(int i = 0; i < gameUnits->enemy.size(); i++)
{
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(gameUnits->enemy[i].rect.x +
tileSize.x / 2 * (gameUnits->enemy[i].rect.w / tileSize.x - 1),
gameUnits->enemy[i].rect.y + tileSize.y / 2 * (gameUnits->enemy[i].rect.h / tileSize.y - 1));
bodyDef.fixedRotation = true;
b2Body* body = world.CreateBody(&bodyDef);
b2PolygonShape shape;
shape.SetAsBox(gameUnits->enemy[i].rect.w / 2, gameUnits->enemy[i].rect.h / 2);
body->CreateFixture(&shape,1.0f);
gameUnits->enemyBody.push_back(body);
}
//ñîçäà¸ì èãðîêà
gameUnits->player = lvl.GetFirstObject("player");
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(gameUnits->player.rect.x, gameUnits->player.rect.y);
bodyDef.fixedRotation = true;
gameUnits->playerBody = world.CreateBody(&bodyDef);
b2PolygonShape shape;
shape.SetAsBox(gameUnits->player.rect.w / 2, gameUnits->player.rect.h / 2);
b2FixtureDef fixtureDef;
fixtureDef.shape = &shape;
fixtureDef.density = 10.0f; fixtureDef.friction = 0.3f;
gameUnits->playerBody->CreateFixture(&fixtureDef);
}
int main(int argv, char * argc[])
{
srand(SDL_GetTicks());
std::unique_ptr<stSharedRes> gameUnits(new stSharedRes());
Level lvl;
lvl.LoadFromFile("resources/platformer.tmx");
b2Vec2 gravity(0.0f, 10.0f);
b2World world(gravity);
//ñîçäà¸ì âñå íåîáõîäèìûå ñòðóêòóðû èãðîâîãî ìèðà èç ôàéëà
InitLevelObjects(lvl, gameUnits, world);
bool isOpen = true;
Uint32 t = SDL_GetTicks();
Uint32 framesPS = 0;
std::shared_ptr<StInput> InputObject(new StInput());
std::shared_ptr<StGameLogic> gameLogic(new StGameLogic());
std::shared_ptr<StDrawLogic> drawLogic(new StDrawLogic(&lvl.GetImagepath()));
drawLogic->PrepareBackground(&lvl);
const float STEP_TIME = 1.0f/60.0f;
while(isOpen)
{
Uint32 loopTick = 0;
isOpen = InputObject->ProcessInput(gameUnits->playerBody);
//std::cout<< "Process input " << SDL_GetTicks() - t << std::endl;
world.Step(STEP_TIME, 8, 1);
//std::cout<< "World step " << SDL_GetTicks() - t << std::endl;
gameLogic->HandleCollisions(gameUnits);
//std::cout<< "Handle collision " << SDL_GetTicks() - t << std::endl;
drawLogic->DrawBackground();
//std::cout<< "Draw background " << SDL_GetTicks() - t << std::endl;
drawLogic->DrawTiles(gameUnits);
//std::cout<< "Draw tiles " << SDL_GetTicks() - t << std::endl;
gameLogic->Step(gameUnits);
//std::cout<< "World step " << SDL_GetTicks() - t << std::endl;
++framesPS;
if (SDL_GetTicks() - t >= 1000) {
std::cout<< "Frames per second " << framesPS << std::endl;
t = SDL_GetTicks();
framesPS = 0;
}
}
SDL_Quit();
return 0;
}