Skip to content

Commit 0ccd15d

Browse files
committed
avatars are now shown in the profile viewer, fix some memory leaks
1 parent e16ce6a commit 0ccd15d

7 files changed

Lines changed: 58 additions & 16 deletions

File tree

src/graphics/VideoBuffer.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ VideoBuffer::VideoBuffer(int width, int height):
1313

1414
VideoBuffer::~VideoBuffer()
1515
{
16-
delete vid;
16+
delete[] vid;
1717
}
1818

1919
void VideoBuffer::Clear()
@@ -275,4 +275,18 @@ int VideoBuffer::DrawText(int x, int y, const char *s, int r, int g, int b, int
275275
}
276276
return x;
277277
}
278+
279+
void VideoBuffer::DrawImage(pixel *img, int x, int y, int w, int h)
280+
{
281+
for (int j = 0; j < h; j++)
282+
for (int i = 0; i < w; i++)
283+
{
284+
int r = PIXR(*img);
285+
int g = PIXG(*img);
286+
int b = PIXB(*img);
287+
DrawPixel(x+i, y+j, r, g, b, 255);
288+
img++;
289+
}
290+
}
291+
278292
#endif

src/graphics/VideoBuffer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class VideoBuffer
2525
int DrawChar(int x, int y, unsigned char c, int r, int g, int b, int a);
2626
int DrawText(int x, int y, const char *s, int r, int g, int b, int a);
2727

28+
void DrawImage(pixel *image, int x, int y, int w, int h);
29+
2830
pixel* GetVid() { return vid; }
2931
};
3032

src/gui/profile/ProfileViewer.cpp

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,28 @@
99
#include "game/Download.h"
1010

1111
ProfileViewer::ProfileViewer(std::string profileName):
12-
Window_(Point(CENTERED, CENTERED), Point(200, 300)),
12+
Window_(Point(CENTERED, CENTERED), Point(250, 300)),
1313
name(profileName),
14+
avatar(NULL),
1415
ageLabel(NULL),
1516
websiteLabel(NULL),
1617
biographyLabel(NULL)
1718
{
18-
profileInfoDownload = new Download("http://" SERVER "/User.json?Name=" + profileName);
19+
profileInfoDownload = new Download("http://" SERVER "/User.json?Name=" + name);
1920
//profileInfoDownload->AuthHeaders();
2021
profileInfoDownload->Start();
2122

22-
usernameLabel = new Label(Point(8, 7), Point(Label::AUTOSIZE, Label::AUTOSIZE), profileName);
23+
avatarDownload = new Download("http://" STATICSERVER "/avatars/" + name + ".pti");
24+
avatarDownload->Start();
25+
26+
usernameLabel = new Label(Point(8, 7), Point(Label::AUTOSIZE, Label::AUTOSIZE), name);
2327
this->AddComponent(usernameLabel);
2428
MainLoop();
2529
}
2630

2731
ProfileViewer::~ProfileViewer()
2832
{
29-
delete usernameLabel;
30-
delete ageLabel;
31-
delete websiteLabel;
32-
delete biographyLabel;
33+
free(avatar);
3334
}
3435

3536
// To be removed later when there is a main engine loop for the entire game
@@ -45,8 +46,7 @@ void ProfileViewer::OnTick(float dt)
4546
{
4647
if (profileInfoDownload && profileInfoDownload->CheckDone())
4748
{
48-
int length, status;
49-
char *data = profileInfoDownload->Finish(&length, &status);
49+
char *data = profileInfoDownload->Finish(NULL, NULL);
5050

5151
json::Object parsed = ParseJSON(data);
5252

@@ -55,20 +55,41 @@ void ProfileViewer::OnTick(float dt)
5555
converter << ((json::Number)parsed["User"]["Age"]).Value();
5656
std::string age = converter.str();
5757

58-
ageLabel = new Label(Point(30, 19), Point(Label::AUTOSIZE, Label::AUTOSIZE), age, true);
59-
websiteLabel = new Label(Point(50, 31), Point(Label::AUTOSIZE, Label::AUTOSIZE), ((json::String)parsed["User"]["Website"]).Value(), true);
60-
biographyLabel = new Label(Point(8, 43), Point(180, Label::AUTOSIZE), ((json::String)parsed["User"]["Biography"]).Value(), true);
58+
ageLabel = new Label(Point(30, 19), Point(Label::AUTOSIZE, Label::AUTOSIZE), age);
59+
websiteLabel = new Label(Point(50, 31), Point(Label::AUTOSIZE, Label::AUTOSIZE), ((json::String)parsed["User"]["Website"]).Value());
60+
biographyLabel = new Label(Point(8, 43), Point(230, Label::AUTOSIZE), ((json::String)parsed["User"]["Biography"]).Value(), true);
6161
this->AddComponent(ageLabel);
6262
this->AddComponent(websiteLabel);
6363
this->AddComponent(biographyLabel);
6464

65-
delete data;
65+
free(data);
6666
profileInfoDownload = NULL;
6767
}
68+
69+
if (avatarDownload && avatarDownload->CheckDone())
70+
{
71+
int length;
72+
char *data = avatarDownload->Finish(&length, NULL);
73+
if (data)
74+
{
75+
int w, h;
76+
avatar = ptif_unpack(data, length, &w, &h);
77+
if (w != 40 || h != 40)
78+
{
79+
free(avatar);
80+
avatar = NULL;
81+
}
82+
}
83+
84+
free(data);
85+
avatarDownload = NULL;
86+
}
6887
}
6988

7089
void ProfileViewer::OnDraw(VideoBuffer *buf)
7190
{
91+
if (avatar)
92+
buf->DrawImage(avatar, 200, 10, 40, 40);
7293
buf->DrawText(10, 22, "Age:", 175, 175, 175, 255);
7394
buf->DrawText(10, 34, "Website:", 175, 175, 175, 255);
7495
buf->DrawText(10, 46, "Biography:", 175, 175, 175, 255);

src/gui/profile/ProfileViewer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44

55
#include <string>
66
#include "interface/Window.h"
7+
#include "graphics.h"
78

89
class Download;
910
class Label;
1011
class ProfileViewer : public Window_
1112
{
1213
std::string name;
1314
Download *profileInfoDownload;
15+
Download *avatarDownload;
16+
pixel *avatar;
1417

1518
Label *usernameLabel, *ageLabel, *websiteLabel, *biographyLabel;
1619
void MainLoop();

src/interface/Engine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,13 @@ void Engine::ShowWindow(Window_ *window)
186186
windows.push(window);
187187
if (top == NULL)
188188
top = window;
189+
fillrect(vid_buf, -1, -1, XRES+BARSIZE+1, YRES+MENUSIZE+1, 0, 0, 0, 100);
189190
}
190191

191192
void Engine::CloseWindow(Window_ *window)
192193
{
193194
if (window == windows.top())
194195
{
195-
delete windows.top();
196196
windows.pop();
197197
if (windows.size())
198198
top = windows.top();

src/interface/Window.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Window_::Window_(Point position_, Point size_):
1111
size(size_),
1212
Components(NULL),
1313
focused(NULL),
14+
clicked(NULL),
1415
toDelete(false)
1516
{
1617
if (position.X == CENTERED)

src/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2882,7 +2882,8 @@ int main(int argc, char *argv[])
28822882
#ifdef NEWINTERFACE
28832883
if (x <= XRES+BARSIZE-(510-399))
28842884
{
2885-
new ProfileViewer(svf_user);
2885+
ProfileViewer *temp = new ProfileViewer(svf_user);
2886+
delete temp;
28862887
}
28872888
else
28882889
#endif

0 commit comments

Comments
 (0)