下面分别给出了starting()函数和running()函数,starting()是总控制函数,running()是在starting()中的一个线程,问题出在running()函数中。
该代码属于一个用EasyX做的尼尔机械纪元E结局的项目,链接:https://github.com/YNG2020/Nier-Automata-Ending-E
/**
* @brief 开始界面总控制函数
*/
void starting()
{
initgraph(GRAPH_WIDTH,GRAPH_HEIGHT);
music().open_music(_T("nonsense.mp3"));
music().play_music();
thread t1(running); // 将粒子效果放到多线程中
while (true)
{
Sleep(100);
if (IS_QUIT_MUSIC)
{
music().pause_music();
music().quit_music(); // 退出音乐
IS_QUIT_THREAD = true; // 退出粒子效果线程
t1.join();
setrop2(R2_COPYPEN);
EndBatchDraw(); // 结束批量绘制
break;
}
}
}
/**
* @brief 开始界面特效控制函数
*/
void running()
{
IMAGE bk_img;
IMAGE img(win_wid, win_hei);
DWORD* imgBuffer = GetImageBuffer(&img);
int sleep_time = 50;
int time_count = 0;
const TCHAR* bk_name = _T("mynierbk.png");
loadimage(&bk_img, bk_name, win_wid, win_hei);
time_t loopTime1, loopTime2;
char ch = '\0';
BeginBatchDraw();
Particle particles[Particle::NUM_PARTICLE];
RandBlock randblocks[RandBlock::NUM_ALL_RANDBLOCK];
Menu menu[5] = {
Menu(_T("Press Any Button"), static_cast<int>((win_hei - 24) * 0.68f)),
Menu(_T("New Game"), static_cast<int>((win_hei - 24) * 0.68f)),
Menu(_T("Setting"), static_cast<int>((win_hei - 24) * 0.68f + 42)),
Menu(_T("Exit Game"), static_cast<int>((win_hei - 24) * 0.68f + 84)),
Menu(_T("Loading..."), static_cast<int>((win_hei - 24) * 0.68f))
};
Gear gear = Gear(static_cast<float>(466 * x_ratio), static_cast<float>(333 * y_ratio), 0.75f * x_ratio);
while (true)
{
loopTime1 = clock();
################################# 就是这里,程序无法在我初始化的窗口中响应键盘输入
if (_kbhit()) {
ch = _getch();
}
#################################
putimage(0, 0, &bk_img); // 放置背景图
Menu::transfer_interface(menu, win_wid, win_hei, ch); // 菜单转换接口
Particle::show_particle(imgBuffer, &img, win_wid, win_hei, particles); // 放置渲染粒子
RandBlock::show_randblock(randblocks); // 放置随机马赛克
gear.show_gear(); // 放置齿轮
loopTime2 = clock();
time_count = static_cast<int>(loopTime2 - loopTime1);
Sleep(sleep_time - time_count > 0 ? sleep_time - time_count : 0); // 如果延时不足sleep_time,强行拉到sleep_time,否则就直接过
FlushBatchDraw();
ch = '\0';
if (IS_QUIT_THREAD)
break;
}
}