【发布时间】:2014-01-10 15:26:14
【问题描述】:
我正在尝试制作 Puyo Puyo 游戏。这是一个谜题。这意味着许多相同的精灵将同时出现。我要渲染四种类型的精灵。但是,当第一个完成下降时,下一个精灵闪烁并且游戏开始滞后。
我已经阅读了有关将掉落的精灵分组到一个纹理的信息,因此它们可以作为单个纹理工作,我认为使用 LockTexture 可能会起作用。但是我不知道怎么用。
这是我的代码:
//While application is running
while( !quit )
{
//Get objects from list (Deque).
puyo* p=list_puyo.at(c_list);
p->move=true;
getSpriteClip(p);
//printf(c_list+": y:"+p.y);
//Characters that haven't reached bottom continue to fall.
if(p->move &&!p->bottom)
fallingPuyo(p); //Every 512 miliseconds, the character will fall 32 pixels.
//Handle events on queue
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
}
}
//Set texture based on current keystate
const Uint8* currentKeyStates = SDL_GetKeyboardState( NULL );
if(!p->bottom && p->move)
{
if( currentKeyStates[ SDL_SCANCODE_DOWN ] )
{
if(!collisionCheck(false,false,p))
{
pressedKey++;
if(pressedKey>128)
{
p->y+=32;
pressedKey=0;
}
}
}
else if( currentKeyStates[ SDL_SCANCODE_LEFT ] )
{
if(!collisionCheck(true,false,p))
{
pressedKey++;
if(pressedKey>128)
{
p->x-=32;
pressedKey=0;
}
}
}
else if( currentKeyStates[ SDL_SCANCODE_RIGHT ] )
{
if(!collisionCheck(false,true,p))
{
pressedKey++;
if(pressedKey>128)
{
p->x+=32;
pressedKey=0;
}
}
}
}
//Clear screen
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
gBackgroundTexture.render(0,0); //window background
gBoardTexture.render(SCREEN_WIDTH/4+30,0); //board background
gSpriteSheetTexture.render( p->x, p->y, p->gSpriteClips); //puyos render
//Update screen
SDL_RenderPresent( gRenderer );
//Return puyo to list
list_puyo.at(c_list)=p;
//Increment list counter by one.
c_list++;
//return counter to 0.
if(c_list>=list_puyo.size())
c_list=0;
}
}
}
【问题讨论】:
-
等等,它只是在一个跌倒后才开始闪烁?还是在添加第二个之后?如果你没有双缓冲,你可能想要这样做,因为闪烁通常是没有双缓冲的症状。
-
您是在为列表的一次迭代执行所有这些操作,还是我错误地解释了您的代码?
-
不,@olevegard,如果列表计数器变得大于列表本身,会发生什么情况,计数器将返回零。我正在浏览列表中的所有对象。
-
@BWG,我如何使用双缓冲?
-
@ElAramu 好吧,您可能已经在使用双缓冲了。我没有使用 SDL 图形。但是我在您的代码中看到了其他一些奇怪的东西。例如,这整件事就是你的主循环。然而,每个循环你只能对一个 puyo 采取行动。然后,您将其“返回”到列表中。这是不必要的,因为
p是一个指针,而不是一个实例,所以返回它只会将列表中的指针设置回原来的样子。
标签: c++ textures rendering sdl sprite