SDL绘图程序是冻结
SDL drawing program is Freezing
我不知道为什么我的程序在画完第六个正方形后就冻结了。这是令人沮丧的。我的意思是,它只是一遍又一遍地重复同样的代码,为什么它可以执行前6次,而不是第7次?:/
下面是我的代码:#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<SDL.h>
using namespace std;
int main (int ,char**)
{
SDL_Init(SDL_INIT_EVERYTHING);
int ProcN=0;
int PageN;
int TProc=0;
int TPage;
int FrameN=0;
srand(time(NULL));
SDL_Surface *screen;
screen=SDL_SetVideoMode(860,540,32,SDL_SWSURFACE);
Uint32 white=SDL_MapRGB(screen->format,0xff,0xff,0xff);
Uint32 green=SDL_MapRGB(screen->format,0x22,0xff,0x22);
Uint32 yellow=SDL_MapRGB(screen->format,0xff,0xbb,0x11);
bool isrunning=true;
const int fps=40;
Uint32 start;
Uint8 *keys = SDL_GetKeyState(NULL);
SDL_FillRect(screen,&screen->clip_rect,white);
SDL_Rect rect[20];
int ff=0;
for(int i=0;i<4;i++)
{
for(int k=0;k<5;k++)
{
rect[ff].x=100+(k)*100+(k)*10;
rect[ff].y=100+(i)*10+(i)*100;
rect[ff].w=100;
rect[ff].h=100;
SDL_FillRect(screen,&rect[ff],yellow);
printf("%dt%dt%dn",ff,rect[ff].x,rect[ff].y);
ff++;
SDL_Flip(screen);
SDL_Delay(1000);
}
}
SDL_Flip(screen);
}
这是因为您不处理SDL事件。SDL是相当低级的,需要手工完成。通常,您希望在绘图循环所在的位置足够频繁地处理它们,以便您可以移动窗口并接收键盘和鼠标事件。要修复代码,请将SDL_Delay(1000);
行替换为:
int cnt = 0;
while(++cnt < 1000)
{
SDL_Event event;
SDL_PollEvent( &event );
SDL_Delay(1);
}
相关文章:
- WIN32:C++,为什么在WM_CLOSE上调用Messagebox函数程序正在冻结
- 程序在对mouse_event的 Windows API 调用中冻结
- C++(SFML?激活某些内容x秒钟,而不会冻结整个程序
- 并行 Python-C++ 程序冻结(内存?
- 如何在不冻结程序的主线程的情况下等待条形码读取器输入
- QT应用程序冻结
- Linux 套接字程序只是冻结而没有错误
- 如何提高四叉树代码的性能以防止程序冻结
- C++线程冻结程序
- 使用 cin 读取大型输入时程序冻结
- QT应用程序接口冻结
- QT QThread::is在Pi上运行冻结程序
- 如何在不冻结线程/应用程序的情况下减慢方法执行速度
- 在线程中复制文件以防止冻结应用程序
- 在C++中读取文件时应用程序冻结
- Qt程序每次都冻结
- QThread::msleep() 冻结程序
- 即使使用另一个线程,启动while循环也会冻结程序
- 为什么在单独的线程中使用信号量会冻结程序?
- 处理文件冻结程序