-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbadtest2.cpp
343 lines (310 loc) · 9.69 KB
/
badtest2.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include "badtest2.h"
#include "ui_badtest2.h"
/* 0~5476 共5477帧 25FPS */
BadTest2::BadTest2(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::BadTest2)
{
ui->setupUi(this);
mode = 3;
//初始化一堆按钮
for (int i=0;i<(80*60);i++)
{
button[i] = new QPushButton(this);
button[i]->setVisible(false);
}
//初始化所有画面
frames = new char[5477*80*60];
//打表
for (int f=0;f<5477;f++)
{
QImage temp(QString(":BA/BadApple/") + QString::number(f+1) + QString(".bmp"));
for (int y=0;y<60;y++)
{
for (int x=0;x<80;x++)
{
if (QColor(temp.pixel(x,y)).lightness() > 128)
frames[f*4800+y*80+x] = 1;
else
frames[f*4800+y*80+x] = 0;
}
}
}
curTime = 0;
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(timerUpdate()));
timer->setInterval(30);
BASS_Init(-1, 44100, 0, 0, NULL);
nowPlay = BASS_StreamCreateFile(false,QString("BadApple.mp3").toStdWString().c_str(),0,0,BASS_UNICODE | BASS_STREAM_PRESCAN);
timer->start();
}
void BadTest2::arrangement()
{
//重排按钮
//行优先方式
/* 像这样(对应一维数组下标)
0,1,2,3,4,5,6,7,8,9……
80,82,83,84……
……
……4798,4799
*/
for (int i = 0; i < 4800; i++)
{
button[i]->setGeometry(i%80*10, (i/80)%60*10, 10, 10);
}
}
inline void BadTest2::buttonDraw()
{
//绘制当期帧
int frame = (int)(BASS_ChannelBytes2Seconds(nowPlay,BASS_ChannelGetPosition(nowPlay, BASS_POS_BYTE))*25.0);
if (frame > 5476 || frame < 0)
{
frame = 0;
}
buttonDraw(frame);
}
inline void BadTest2::buttonDraw(int frame)
{
int curFrame[80*60];
//copy当期帧画面
for (int i=0; i<4800; i++)
{
curFrame[i] = frames[frame*4800+i];
}
if (mode == 0)
{
//原始方式
int totalOfButton = 0;
for (int y = 0; y < 60; y++)
{
for (int x = 0; x < 80; x++)
{
if (curFrame[y*80+x])
{
button[y*80+x]->setVisible(true);
totalOfButton++;
}
else
{
button[y*80+x]->setVisible(false);
}
}
}
ui->label->setText(QString("按钮数:") + QString::number(totalOfButton));
}
else
{
//装逼方式
//当前操作的按钮编号
int buttonID = 0;
switch (mode) {
case 1:
for (int y = 0; y < 60; y++)
{
for (int x = 0; x < 80; x++)
{
if (curFrame[y*80+x])
{
//此处有像素点
int lenght = 1;
for (int i = x+1; i < 80; i++)
{
if (curFrame[y*80+i])
lenght++;
else
break;
}
//clearRect(curFrame, x, y, lenght, 1);
button[buttonID]->setGeometry(x*10,y*10,10*lenght,10);
button[buttonID++]->setVisible(true);
x+=lenght-1;
}
}
}
break;
case 2:
//搜寻正方形
for (int y = 0; y < 60; y++)
{
for (int x = 0; x < 80; x++)
{
//求解出的边长
int side = 1;
//搜索到画面非空白内容
if (curFrame[y*80+x])
{
//求当前允许最大边长
int maxSide = (80-x)<(60-y)?(80-x):(60-y);//最小值1(在边框上),最大值60
//搜索最大正方形
for (int sideAdd = 1; sideAdd < maxSide; sideAdd++)
{
bool testXOK = true;
//测试横向底边
for (int xLen = 0; xLen <= sideAdd; xLen++)
{
//发现非1像素
if(curFrame[(y+sideAdd)*80+(x+xLen)] == 0)
{
testXOK = false;
break;//停止横向搜索
}
}
//如果横向测试通过,测试纵向
bool testYOK = true;
if (testXOK)
{
for (int yLen = 0; yLen <= sideAdd; yLen++)
{
//发现非1像素
if(curFrame[(y+yLen)*80+(x+sideAdd)] == 0)
{
testYOK = false;
break;//停止纵向搜索
}
}
}
if (testXOK && testYOK)
{
side++;//增加边长
}
else
{
break;//无更大正方形,结束算法
}
}
clearRect(curFrame, x, y, side, side);
//操作界面控件
button[buttonID]->setGeometry(x*10,y*10,side*10,side*10);
button[buttonID++]->setVisible(true);
}
}
}
break;
case 3:
//算法2
for (int side = 60; side > 0; side--)
{
//搜索全画面中存在的边长为side的正方形 最大60 最小1
for (int y = 0; y <= 60-side; y++)
{
for (int x = 0; x <= 80-side; x++)
{
//遍历所有可搜索区域
bool isOk = true;//记录该区域是否存在正方形
//测试所有像素
for (int i = 0; i < side; i++)
{
for (int j = 0; j < side; j++)
{
if (curFrame[(y+i)*80+(x+j)] == 0)
{
//测试失败
isOk = false;
goto FUCK;//跳出两层循环
}
}
}
FUCK:
if (isOk)
{
clearRect(curFrame, x, y, side, side);
//操作界面控件
button[buttonID]->setGeometry(x*10,y*10,side*10,side*10);
button[buttonID++]->setVisible(true);
}
}
}
}
break;
default:
break;
}
ui->label->setText(QString("按钮数:") + QString::number(buttonID));
//隐藏剩余按钮
for (int i=buttonID; i<(80*60); i++)
{
button[i]->setVisible(false);
}
}
}
//清空一块区域
inline void BadTest2::clearRect(int *array, int x, int y, int width, int height)
{
for (int i=0; i<width; i++)
{
for (int j=0; j<height; j++)
{
array[(y+j)*80+(x+i)] = 0;
}
}
}
void BadTest2::timerUpdate()
{
//计算当前帧
int newTime = 0;
//if (BASS_ChannelIsActive(nowPlay) == BASS_ACTIVE_PLAYING)
newTime = (int)(BASS_ChannelBytes2Seconds(nowPlay,BASS_ChannelGetPosition(nowPlay, BASS_POS_BYTE))*25.0);
//同一帧
if (newTime == curTime)
return;
curTime = newTime;
if (curTime > 5476 || curTime < 0)
{
curTime = 0;
//timer->stop();
}
//---------
buttonDraw(curTime);
ui->playingProgress->setValue((int)(BASS_ChannelBytes2Seconds(nowPlay,BASS_ChannelGetPosition(nowPlay, BASS_POS_BYTE))*25.0));
}
void BadTest2::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setBrush(Qt::SolidPattern);
painter.drawRect(0,0,800,600);
//painter.setPen(QColor(Qt::white));
//painter.setBrush(QColor(Qt::white));
}
BadTest2::~BadTest2()
{
delete ui;
}
void BadTest2::on_algorithm0_clicked()
{
arrangement();
this->mode = 0;
buttonDraw();
}
void BadTest2::on_algorithm1_clicked()
{
this->mode = 1;
buttonDraw();
}
void BadTest2::on_algorithm2_clicked()
{
this->mode = 2;
buttonDraw();
}
void BadTest2::on_algorithm3_clicked()
{
this->mode = 3;
buttonDraw();
}
void BadTest2::on_playButton_clicked()
{
BASS_ChannelPlay(nowPlay, false);
}
void BadTest2::on_pauseButton_clicked()
{
BASS_ChannelPause(nowPlay);
}
void BadTest2::on_playingProgress_valueChanged(int value)
{
//换算到帧
int pos = (int)(BASS_ChannelBytes2Seconds(nowPlay,BASS_ChannelGetPosition(nowPlay, BASS_POS_BYTE))*25.0);
if (qAbs(pos - value) > 4)
{
//帧换算到位置
BASS_ChannelSetPosition(nowPlay, value*BASS_ChannelGetLength(nowPlay, BASS_POS_BYTE)/5476, BASS_POS_BYTE);
}
}