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("%d\t%d\t%d\n",ff,rect[ff].x,rect[ff].y);
ff++;
SDL_Flip(screen);
SDL_Delay(1000);
}
}
SDL_Flip(screen);
}
这是因为您没有处理 SDL 事件。 SDL 级别很低,需要手动完成。一般来说,您希望在绘图循环所在的位置足够频繁地处理它们,以便您可以移动 window 并可以接收键盘和鼠标事件。
要修复您的代码,请将 SDL_Delay(1000);
行替换为:
int cnt = 0;
while(++cnt < 1000)
{
SDL_Event event;
SDL_PollEvent( &event );
SDL_Delay(1);
}
我不确定为什么我的程序在绘制完第六个正方形后就死机了。真令人沮丧。我的意思是它只是一遍又一遍地重复相同的代码,为什么它在前 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("%d\t%d\t%d\n",ff,rect[ff].x,rect[ff].y);
ff++;
SDL_Flip(screen);
SDL_Delay(1000);
}
}
SDL_Flip(screen);
}
这是因为您没有处理 SDL 事件。 SDL 级别很低,需要手动完成。一般来说,您希望在绘图循环所在的位置足够频繁地处理它们,以便您可以移动 window 并可以接收键盘和鼠标事件。
要修复您的代码,请将 SDL_Delay(1000);
行替换为:
int cnt = 0;
while(++cnt < 1000)
{
SDL_Event event;
SDL_PollEvent( &event );
SDL_Delay(1);
}