forked from pioneerspacesim/pioneer
/
FaceVideoLink.cpp
240 lines (190 loc) · 6.13 KB
/
FaceVideoLink.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "FaceVideoLink.h"
#include "Lang.h"
#include "Pi.h"
#include "LuaNameGen.h"
#include "graphics/Material.h"
#include "graphics/Renderer.h"
#include "graphics/VertexArray.h"
#include "graphics/TextureBuilder.h"
#include "FileSystem.h"
#include "SDLWrappers.h"
using namespace Graphics;
#define FACE_WIDTH 295
#define FACE_HEIGHT 285
// XXX these shouldn't really be hardcoded. it'd be much nicer to poke through
// the facegen/ dir and figure out what we've got available. that or some
// config file
#define MAX_HEAD 20
#define MAX_EYES 20
#define MAX_NOSE 20
#define MAX_MOUTH 20
#define MAX_HAIR 20
#define MAX_CLOTHES 20
#define MAX_ARMOUR 3
#define MAX_ACCESSORIES 16
#define MAX_BACKGROUND 18
static void _blit_image(SDL_Surface *s, const char *filename, int xoff, int yoff)
{
SDLSurfacePtr is = LoadSurfaceFromFile(filename);
// XXX what should this do if the image couldn't be loaded?
if (! is) { return; }
SDL_Rect destrec = { 0, 0, 0, 0 };
destrec.x = ((FACE_WIDTH-is->w-1)/2)+xoff;
destrec.y = yoff;
SDL_BlitSurface(is.Get(), NULL, s, &destrec);
}
static void _blit_image(const SDLSurfacePtr &s, const char *filename, int xoff, int yoff)
{
_blit_image(s.Get(), filename, xoff, yoff);
}
FaceVideoLink::FaceVideoLink(float w, float h, Uint32 flags, Uint32 seed,
const std::string &name, const std::string &title) : VideoLink(w, h)
{
m_created = SDL_GetTicks();
m_message = new Gui::ToolTip(Lang::VID_LINK_ESTABLISHED);
if (!seed) seed = time(NULL);
MTRand rand(seed);
m_flags = flags;
m_seed = seed;
int race = rand.Int32(0,2);
int gender;
switch (flags & GENDER_MASK) {
case GENDER_MALE:
gender = 0;
break;
case GENDER_FEMALE:
gender = 1;
break;
case GENDER_RAND:
default:
gender = rand.Int32(0,1);
break;
}
std::string charname = name;
if (charname.empty())
charname = Pi::luaNameGen->FullName((gender != 0), rand);
m_characterInfo = new CharacterInfoText(w * 0.8f, h * 0.15f, charname, title);
int head = rand.Int32(0,MAX_HEAD);
int eyes = rand.Int32(0,MAX_EYES);
int nose = rand.Int32(0,MAX_NOSE);
int mouth = rand.Int32(0,MAX_MOUTH);
int hair = rand.Int32(0,MAX_HAIR);
int clothes = rand.Int32(0,MAX_CLOTHES);
int armour = rand.Int32(0,MAX_ARMOUR);
int accessories = rand.Int32(0,MAX_ACCESSORIES);
int background = rand.Int32(0,MAX_BACKGROUND);
char filename[1024];
SDLSurfacePtr faceim = SDLSurfacePtr::WrapNew(SDL_CreateRGBSurface(SDL_SWSURFACE, FACE_WIDTH, FACE_HEIGHT, 24, 0xff, 0xff00, 0xff0000, 0));
snprintf(filename, sizeof(filename), "facegen/backgrounds/background_%d.png", background);
_blit_image(faceim, filename, 0, 0);
snprintf(filename, sizeof(filename), "facegen/race_%d/head/head_%d_%d.png", race, gender, head);
_blit_image(faceim, filename, 0, 0);
if (!(flags & ARMOUR)) {
snprintf(filename, sizeof(filename), "facegen/clothes/cloth_%d_%d.png", gender, clothes);
_blit_image(faceim, filename, 0, 135);
}
snprintf(filename, sizeof(filename), "facegen/race_%d/eyes/eyes_%d_%d.png", race, gender, eyes);
_blit_image(faceim, filename, 0, 41);
snprintf(filename, sizeof(filename), "facegen/race_%d/nose/nose_%d_%d.png", race, gender, nose);
_blit_image(faceim, filename, 1, 89);
snprintf(filename, sizeof(filename), "facegen/race_%d/mouth/mouth_%d_%d.png", race, gender, mouth);
_blit_image(faceim, filename, 0, 155);
if (!(flags & ARMOUR)) {
snprintf(filename, sizeof(filename), "facegen/accessories/acc_%d.png", accessories);
if (rand.Int32(0,1)>0) _blit_image(faceim, filename, 0, 0);
snprintf(filename, sizeof(filename), "facegen/race_%d/hair/hair_%d_%d.png", race, gender, hair);
_blit_image(faceim, filename, 0, 0);
}
else {
snprintf(filename, sizeof(filename), "facegen/clothes/armour_%d.png", armour);
_blit_image(faceim, filename, 0, 0);
}
m_quad.Reset(new Gui::TexturedQuad(Graphics::TextureBuilder(faceim, Graphics::LINEAR_CLAMP, true, true).CreateTexture(Gui::Screen::GetRenderer())));
}
FaceVideoLink::~FaceVideoLink() {
delete m_message;
delete m_characterInfo;
}
void FaceVideoLink::Draw() {
float size[2];
GetSize(size);
Uint32 now = SDL_GetTicks();
if (now - m_created < 1500) {
glBegin(GL_QUADS);
glColor3f(0,0,0);
glVertex2f(0,0);
glVertex2f(0,size[1]);
glVertex2f(size[0],size[1]);
glVertex2f(size[0],0);
glEnd();
m_message->SetText(Lang::VID_CONNECTING);
DrawMessage();
return;
}
m_quad->Draw(Gui::Screen::GetRenderer(), vector2f(0.0f), vector2f(size[0],size[1]));
glPushMatrix();
glTranslatef(0.f, size[1]- size[1] * 0.16f, 0.f);
m_characterInfo->Draw();
glPopMatrix();
}
void FaceVideoLink::DrawMessage() {
float size[2];
GetSize(size);
float msgSize[2];
m_message->GetSize(msgSize);
glPushMatrix();
glTranslatef(size[0]*0.5f-msgSize[0]*0.5f, size[1]-msgSize[1]*1.5f, 0);
m_message->Draw();
glPopMatrix();
}
CharacterInfoText::CharacterInfoText(float w, float h,
const std::string &name, const std::string &title) :
Gui::Fixed(w, h),
m_characterName(name),
m_characterTitle(title),
m_width(w),
m_height(h)
{
if (m_characterTitle.empty())
h = h/1.5f;
SetSize(w, h);
m_background = new Gui::Gradient(w, h, Color(0.1f, 0.1f, 0.1f, 0.8f),
Color(0.f, 0.f, 0.1f, 0.f), Gui::Gradient::HORIZONTAL);
Gui::Screen::PushFont("OverlayFont");
m_nameLabel = new Gui::Label(m_characterName);
m_titleLabel = new Gui::Label(m_characterTitle);
Gui::Screen::PopFont();
Add(m_background, 0.f, 0.f);
Add(m_nameLabel, 25.f, 5.f);
Add(m_titleLabel, 25.f, m_height/2);
}
CharacterInfoText::~CharacterInfoText()
{
}
void CharacterInfoText::SetCharacterName(const std::string &name)
{
m_characterName = name;
m_nameLabel->SetText(name);
UpdateAllChildSizes();
}
void CharacterInfoText::SetCharacterTitle(const std::string &title)
{
m_characterTitle = title;
m_titleLabel->SetText(title);
UpdateAllChildSizes();
}
void CharacterInfoText::GetSizeRequested(float size[2])
{
size[0] = m_width;
size[1] = m_height;
}
void CharacterInfoText::Draw()
{
if (m_characterName.empty() && m_characterTitle.empty()) return;
for (std::list<widget_pos>::iterator i = m_children.begin(); i != m_children.end(); ++i) {
glPushMatrix();
glTranslatef((*i).pos[0], (*i).pos[1], 0);
(*i).w->Draw();
glPopMatrix();
}
}