-
Notifications
You must be signed in to change notification settings - Fork 1
/
gamebackground.cpp
119 lines (96 loc) · 2.3 KB
/
gamebackground.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
#include "gamebackground.h"
#include "gamewidget.h"
#include "gameprofile.h"
#include <QDir>
GameBackground *gameBackground = 0;
enum BgBlend { BB_NONE, BB_ALPHA, BB_BLOCKS };
struct BgPrivate
{
QString bgPath;
QStringList bgList;
int currentBgIdx, transBgIdx;
QPixmap currentBg, transBg;
int frame;
BgBlend blend;
BgPrivate() : blend(BB_ALPHA)
{
}
void init()
{
frame = 0;
currentBgIdx = transBgIdx = -1;
bgPath = GameWidget::getResourcePath() + "schemes/" + gameProfile->currentTheme();
QStringList ext; ext << "bg*.png" << "bg*.jpg";
bgList = QDir(bgPath).entryList(ext);
// read current and trans
if (bgList.isEmpty())
return;
currentBgIdx = qrand() % bgList.count();
currentBg = QPixmap(bgPath + "/" + bgList.at(currentBgIdx));
loadTransBg();
}
void loadTransBg()
{
if (bgList.isEmpty())
return;
if (bgList.count() == 1)
transBgIdx = 0;
else
do {
transBgIdx = qrand() % bgList.count();
} while (currentBgIdx == transBgIdx);
transBg = QPixmap(bgPath + "/" + bgList.at(transBgIdx));
}
void draw(QPainter &p, const QRect &r)
{
p.setOpacity(1);
p.drawPixmap(r, currentBg);
if (++frame < 100)
return;
switch (blend)
{
case BB_ALPHA:
if (frame < 480)
return;
if (frame >= 480 && frame < 500)
{
p.setOpacity((double)(frame-480)/20.0);
p.drawPixmap(0,0,transBg);
return;
}
break;
case BB_BLOCKS:
if (frame < 500)
{
//p.setOpacity(1);
for (int i = 0; i < 10; i++)
{
QPainter p1(¤tBg);
int x = qrand() % 64;
int y = qrand() % 48;
p1.drawPixmap(x*16,y*16, transBg, x*16,y*16,16,16);
return;
}
}
break;
default: ;
}
p.drawPixmap(0,0,transBg);
frame = 0;
currentBg = transBg;
currentBgIdx = transBgIdx;
loadTransBg();
}
};
GameBackground::GameBackground()
{
priv = new BgPrivate();
}
void GameBackground::readProfile(PlayerInfo */*pl*/)
{
priv->init();
}
void GameBackground::draw(QPainter &p, int /*frame*/, const QRect &r)
{
priv->draw(p, r);
}