SDL按钮/事件停止工作

问题描述:

我一直在关注lazyfoo'Productions'教程,因为我创建了一个简单的游戏。我目前的任务是制作主菜单,这是我的问题所在。SDL按钮/事件停止工作

我对全局变量的热情,ERGO我不使用他们。我相信他们太难以准确地追踪和维护,但那不是重点。本教程使用屏幕的全局变量以及其他一些内容。我设法制定了一个按钮类,但它只能在一定的时间内(大约7秒)才能拒绝更改精灵。

我仍然可以正常退出,但按钮的质感将保持一致,那么它是在停止时间(如果鼠标在它,它会保持发光,反之亦然)。

这里是我的主线程,在那里我调用按钮的类函数:

//... 

while(quit == false) 
{ 
    if(SDL_PollEvent(&event)) 
    { 
     if(event.type == SDL_QUIT) 
     { 
      quit = true; 
     } 
     else if(event.type == SDL_MOUSEMOTION) 
     { 
      button_newgame.handle_events(event); 
     } 
    } 

    button_newgame.show(); 

    if(SDL_Flip(screen) == -1) 
    { 
     return 0; 
    } 
} 

而这里的按钮功能

Button::Button(int x, int y, int w, int h, int buttonnum, SDL_Surface *screen) 
{ 
box.x = x; 
box.y = y; 
box.w = w; 
box.h = h; 

m_screen = screen; 

switch(buttonnum) 
{ 
case 0: 
    buttonclipinactive.x = 0; 
    buttonclipinactive.y = 0; 
    buttonclipinactive.w = 240; 
    buttonclipinactive.h = 60; 

    buttonclipactive.x = 0; 
    //setting clips according to the button's identiffying number to find the correct sprite 
} 

void Button::handle_events(SDL_Event event) 
{ 
//The mouse offsets 
int mx = 0, my = 0; 

SDL_Delay(5); 

//Get the mouse offsets 
mx = event.motion.x; 
my = event.motion.y; 

//If the mouse is over the button 
if((mx > box.x) && (mx < box.x + box.w) && (my > box.y) && (my < box.y + box.h)) 
{ 
    //Set the button sprite 
    clip = &buttonclipactive; 
} 
//If not 
else 
{ 
    //Set the button sprite 
    clip = &buttonclipinactive; 
} 

SDL_Rect* Button::show() 
{ 
    SDL_Surface *mainmenuoptionsheet = load_image("images/mainmenuoptionsheet.bmp"); 

    apply_surface(box.x, box.y, mainmenuoptionsheet, m_screen, clip); 
} 

你叫Button::show()剔过在你的循环。但是这个功能究竟做了什么?

SDL_Surface *mainmenuoptionsheet = load_image("images/mainmenuoptionsheet.bmp"); 

所以,你正在加载bmp每一个滴答。你的程序停止显示可能是因为它(可预测地)耗尽内存使用,并停留在显示的最后精灵上。相反,在您的应用程序开始时,请将SDL_Surface指针加载ONCE,然后渲染每个帧。

作为附加的注释,请确保您使用的是较新的lazyfoo教程。他的旧的是过时的。

+0

我想我应该已经看到了。感谢您指出! –