-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
executable file
·91 lines (82 loc) · 1.92 KB
/
game.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
#include "base/with_mass.h"
#include "base/creator.h"
#include "player.h"
#include "game.h"
#include "show.h"
#include <SDL/SDL_gfxPrimitives.h>
#include <SDL/SDL_image.h>
Game::Game(): App("Tiles Adventure", 480, 360),
bg_timer(10)
{
IMG_Init(IMG_INIT_PNG);
sprites.add(new Player(&map, 60, 50, keys, "Virtual Guy"));
bg = new Background;
sprites.add(new Fps(Fps::DEFAULT, "fonts/Supercell-Magic_5"));
Map::camera.x = screen->w*.5 - 100;
Map::camera.y = screen->h*.5 + 10;
Map::camera.w = 200;
Map::camera.h = 120;
ObjectCreator::addToParameters(sprites[0], "Player");
map.load_objects();
}
Game::~Game()
{
delete bg; bg = NULL;
}
void Game::manage_events()
{
if (keys[SDLK_KP8])
Map::camera.y -= 2;
else if (keys[SDLK_KP2])
Map::camera.y += 2;
if (keys[SDLK_KP4])
Map::camera.x -= 2;
else if (keys[SDLK_KP6])
Map::camera.x += 2;
}
void Game::update_events()
{
App::update_events();
if (event.type == SDL_KEYDOWN)
{
switch (event.key.keysym.sym)
{
case SDLK_SPACE:
pause();
break;
case SDLK_b:
With_mass::showBoundingBox = !With_mass::showBoundingBox;
break;
default: ;
}
}
}
void Game::draw()
{
bg->draw(screen);
// rectangleColor(screen, camera.x, camera.y, camera.x+camera.w, camera.y+camera.h, 0xff);
map.draw(screen);
sprites.draw(screen);
SDL_Flip(screen);
}
void Game::update()
{
if (paused)
return;
sprites.update();
if (Show::instance)
return;
bg->update();
map.update();
map.center_on(sprites[0] , Map::camera);
if (bg_timer.out())
{
bg_timer.restart();
change_background();
}
}
void Game::change_background()
{
delete bg;
bg = new Background;
}