-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSDLGame.cpp
More file actions
121 lines (92 loc) · 2.54 KB
/
SDLGame.cpp
File metadata and controls
121 lines (92 loc) · 2.54 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
#include "SDLGame.h"
#include <time.h>
#include <iostream>
#include "Messages_defs.h"
SDLGame::SDLGame(string windowTitle, int width, int height) :
windowTitle_(windowTitle), width_(width), height_(height) {
initSDL();
initResources();
}
SDLGame::~SDLGame() {
closeResources();
closeSDL();
}
void SDLGame::initSDL() {
SDL_Init(SDL_INIT_EVERYTHING);
// Create window .
window_ = SDL_CreateWindow(windowTitle_.c_str(),
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, width_, height_, SDL_WINDOW_SHOWN);
// Create the renderer
renderer_ = SDL_CreateRenderer(window_, -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
// Clear screen (background color).
SDL_SetRenderDrawColor(renderer_, 0, 100, 100, 255); // Dark grey.
SDL_RenderClear(renderer_);
SDL_RenderPresent(renderer_);
// hide cursor by default
SDL_ShowCursor(0);
}
void SDLGame::closeSDL() {
SDL_DestroyRenderer(renderer_);
renderer_ = nullptr;
SDL_DestroyWindow(window_);
window_ = nullptr;
SDL_Quit();
}
void SDLGame::initResources() {
fonts_.init();
textures_.init();
audio_.init();
services_.setTextures(&textures_);
services_.setAudios(&audio_);
services_.setFonts(&fonts_);
services_.setRandomGenerator(&random_);
for (auto &image : Resources::images_) {
textures_.loadFromImg(image.id, renderer_, image.fileName);
}
for (auto &font : Resources::fonts_) {
fonts_.loadFont(font.id, font.fileName, font.size);
}
for (auto &txtmsg : Resources::messages_) {
textures_.loadFromText(txtmsg.id, renderer_, txtmsg.msg,
*fonts_.getFont(txtmsg.fontId), txtmsg.color);
}
for (auto &sound : Resources::sounds_) {
audio_.loadSound(sound.id, sound.fileName);
}
for (auto &music : Resources::musics_) {
cout << music.id << music.fileName << endl;
audio_.loadMusic(music.id, music.fileName);
}
}
void SDLGame::closeResources() {
}
SDL_Window* SDLGame::getWindow() const {
return window_;
}
SDL_Renderer* SDLGame::getRenderer() const {
return renderer_;
}
const ServiceLocator* SDLGame::getServiceLocator() const {
return &services_;
}
int SDLGame::getWindowWidth() const {
return width_;
}
int SDLGame::getWindowHeight() const {
return height_;
}
void SDLGame::addObserver(Observer* o) {
observers_.push_back(o);
}
void SDLGame::send(const void* senderObj, const msg::Message& msg) {
for (Observer* o : observers_) {
if (senderObj != o) {
if (msg.destination_ == msg::Broadcast // we send to everyone, even to the one from whom we received the message!
|| msg.destination_ == o->getId()) {
o->receive(senderObj,msg);
}
}
}
}