/
setupgame.cpp
163 lines (149 loc) · 5.6 KB
/
setupgame.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
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
157
158
159
160
161
162
163
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <AL/alc.h>
#include <string>
#include <sstream>
#include <vector>
#include <memory>
#include <thread>
#include <functional>
#include "loadsound.hpp"
#include "loadtexture.hpp"
#include "setupgame.hpp"
#include "resourcemanager.hpp"
#include "readtostream.hpp"
#include "map.hpp"
#include "visibleobject.hpp"
#include "playgame.hpp"
#include "createbuffer.hpp"
struct Textures
{
Textures(const std::string file):filename(file){}
std::string filename;
std::vector<unsigned char> texVec;
unsigned int width;
unsigned int height;
};
struct Sounds
{
Sounds(const std::string file):filename(file){}
std::string filename;
unsigned int sound;
};
void CreateTexturesAndSound(std::vector<Textures> &textureVec, std::vector<Sounds> &soundVec);
bool SetupGame(ResourceManager &resourceManager, const unsigned int lives)
{
Map map;
CreateResources(resourceManager);
return PlayGame(map, resourceManager, lives);
}
void CreateResources(ResourceManager &resourceManager)
{
std::vector<Textures> texVec;
std::vector<Sounds> soundVec;
if(!resourceManager.GetTexture("geoff.png"))
texVec.push_back(Textures("geoff.png"));
if(!resourceManager.GetTexture("wolf.png"))
texVec.push_back(Textures("wolf.png"));
if(!resourceManager.GetTexture("bear.png"))
texVec.push_back(Textures("bear.png"));
if(!resourceManager.GetTexture("scenery.png"))
texVec.push_back(Textures("scenery.png"));
if(!resourceManager.GetTexture("scenery2.png"))
texVec.push_back(Textures("scenery2.png"));
if(!resourceManager.GetTexture("text.png"))
texVec.push_back(Textures("text.png"));
if(!resourceManager.GetTexture("snake.png"))
texVec.push_back(Textures("snake.png"));
if(!resourceManager.GetSound("bear.ogg"))
soundVec.push_back(Sounds("bear.ogg"));
if(!resourceManager.GetSound("chainsaw.ogg"))
soundVec.push_back(Sounds("chainsaw.ogg"));
if(!resourceManager.GetSound("chop.ogg"))
soundVec.push_back(Sounds("chop.ogg"));
if(!resourceManager.GetSound("death.ogg"))
soundVec.push_back(Sounds("death.ogg"));
if(!resourceManager.GetSound("snake.ogg"))
soundVec.push_back(Sounds("snake.ogg"));
if(!resourceManager.GetSound("wolfhowl.ogg"))
soundVec.push_back(Sounds("wolfhowl.ogg"));
if(!resourceManager.GetSound("woodpile.ogg"))
soundVec.push_back(Sounds("woodpile.ogg"));
resourceManager.GetOrCreateProgram("textured.vs", "textured.fs");
resourceManager.GetOrCreateProgram("character.vs", "textured.fs");
resourceManager.GetOrCreateProgram("colouredsquare.vs", "colouredsquare.fs");
std::shared_ptr<unsigned int> buffer(resourceManager.GetVao("buffer"));
if(!buffer)
{
buffer = std::make_shared<unsigned int>(CreateBuffer());
resourceManager.AddVao("buffer", buffer);
}
if(!resourceManager.GetVao("full vao"))
{
std::shared_ptr<unsigned int> vao(new unsigned int (CreateVAO(*buffer, 1)));
resourceManager.AddVao("full vao", vao);
}
if(!resourceManager.GetVao("vao top left"))
{
std::shared_ptr<unsigned int> vaoTopLeft(new unsigned int (CreateVAO(*buffer, 2)));
resourceManager.AddVao("vao top left", vaoTopLeft);
}
if(!resourceManager.GetVao("vao top right"))
{
std::shared_ptr<unsigned int> vaoTopRight(new unsigned int (CreateVAO(*buffer, 3)));
resourceManager.AddVao("vao top right", vaoTopRight);
}
if(!resourceManager.GetVao("vao bottom left"))
{
std::shared_ptr<unsigned int> vaoBottomLeft(new unsigned int (CreateVAO(*buffer, 4)));
resourceManager.AddVao("vao bottom left", vaoBottomLeft);
}
if(!resourceManager.GetVao("vao bottom right"))
{
std::shared_ptr<unsigned int> vaoBottomRight(new unsigned int (CreateVAO(*buffer, 5)));
resourceManager.AddVao("vao bottom right", vaoBottomRight);
}
if(!resourceManager.GetVao("vao plain colour"))
{
std::shared_ptr<unsigned int> vaoPlainColour(new unsigned int (SetupStandardVAO(*buffer)));
resourceManager.AddVao("vao plain colour", vaoPlainColour);
}
CreateTexturesAndSound(texVec, soundVec);
for(auto object(texVec.begin()); object != texVec.end(); ++object)
{
std::shared_ptr<unsigned int> ptr(
new unsigned int(CreateOpenGLTexture((*object).texVec, (*object).width,
(*object).height)));
resourceManager.AddTexture((*object).filename, ptr);
}
texVec.clear();
for(auto object(soundVec.begin()); object != soundVec.end(); ++object)
{
std::shared_ptr<unsigned int> ptr(
new unsigned int(object->sound));
resourceManager.AddSound((*object).filename, ptr);
}
soundVec.clear();
}
void CreateTexturesAndSound(std::vector<Textures> &textureVec,
std::vector<Sounds> &soundVec)
{
std::vector<std::thread> threadVec;
for(auto & object : textureVec)
{
threadVec.push_back(std::thread(TextureToVec, std::ref(object.filename),
std::ref(object.texVec), std::ref(object.width),
std::ref(object.height)));
}
ALCcontext * context(alcGetCurrentContext());
for(auto & object : soundVec)
{
threadVec.push_back(std::thread(SoundToVec, std::ref(object.filename),
std::ref(object.sound), context));
}
for(auto & thread : threadVec)
{
thread.join();
}
}