gpt4 book ai didi

c++ - 在 SDL 中渲染静止图像

转载 作者:太空狗 更新时间:2023-10-29 23:16:59 25 4
gpt4 key购买 nike

我目前正在学习 SDL,并且正在尝试创建一个 Pacman 游戏。我正在尝试逐步进行,以免陷入大量代码的困境。

到目前为止,我已经创建了一个空白窗口并在其上渲染了 Pacman 图像。我可以按箭头键并在窗口周围移动 Pacman。我设置了它,因此 Pacman 图像存储为 SDL_Texture,我使用 RenderCopy 将其 blit 到窗口。每次用户按下箭头时,我都会移动图像的坐标并重新渲染整个图像。这很好用。然而,现在我想在屏幕上放一些点让吃 bean 人吃。如果我加载一个点图像并将其存储为新纹理以与 Pacman 一起 blit 到屏幕,但是,每次我移动 Pacman 时,点都会闪烁,因为它正在被删除并与 Pacman 一起重新渲染。

我的问题是,如何避免这种“闪烁”?我能以某种方式只重新渲染吃 bean 人而不重新渲染屏幕的其余部分吗?还是有另一种方法可以做到这一点?我想当我稍后在后台创建迷宫时也会遇到同样的问题。如何制作静态背景,每次重新渲染时都不会闪烁?

到目前为止,下面是我的代码。如果那里有任何格式错误的代码,请原谅我。正如我所说,我才刚刚开始学习 SDL(对 C++ 也很陌生),所以如果有任何明显的“你永远不应该那样做!”里面有一些东西,我很感激任何人指出它:)

#include <iostream>
#include <SDL2/SDL.h>
using namespace std;

const int WINDOW_HEIGHT = 480;
const int WINDOW_WIDTH = 640;
const int MOVE_WIDTH = 10;

int main(int argc, const char * argv[])
{
SDL_Window* mainWindow = NULL; //To hold the main window
SDL_Renderer* renderer = NULL; //To hold the renderer
SDL_Rect targetRect; //Rectangle to which pacman image will be drawn
SDL_Surface* bmpSurface = NULL; //To hold bmp image
SDL_Texture* bmpTexture = NULL; //To hold bmp image

//Initialize SDL and check for errors
if ( SDL_Init(SDL_INIT_EVERYTHING) != 0 )
{
cout << "ERROR: could not initialize SDL." << endl;
}

//Create a window
mainWindow = SDL_CreateWindow("BAM", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, 0);

if (mainWindow == NULL)
{
cout << "ERROR: could not initialize mainWindow." << endl;
}

//Initialize renderer
renderer = SDL_CreateRenderer(mainWindow, -1, SDL_RENDERER_ACCELERATED);

//Load image and store in an SDL_Surface
bmpSurface = SDL_LoadBMP("/Users/billgrenard/Desktop/Programs/SDL/SDL_KeyPresses/SDL_KeyPresses/pacman_closed.bmp");
if ( bmpSurface == NULL )
{
cout << "ERROR: could not load bmp file." << endl;
}

//Convert surface to texture for rendering
bmpTexture = SDL_CreateTextureFromSurface(renderer, bmpSurface);
if ( bmpTexture == NULL )
{
cout << "ERROR: could not convert bmp surface." << endl;
}

SDL_FreeSurface(bmpSurface);

//Define rectangle where pacman image is to be blitted
targetRect.w = 30;
targetRect.h = 30;
targetRect.x = (WINDOW_WIDTH/2) - (targetRect.w/2);
targetRect.y = (WINDOW_HEIGHT/2) - (targetRect.h/2);


//Main game loop
while (1)
{
SDL_Event e;
if (SDL_PollEvent(&e))
{
//Quit when user x's out the window
if (e.type == SDL_QUIT)
{
break;
}

//If user presses a key enter switch statement
else if( e.type == SDL_KEYDOWN )
{
switch ( e.key.keysym.sym ) {
//If user presses up arrow and the resulting move is inside the window, then move the Pacman's position
case SDLK_UP:
if ( targetRect.y - MOVE_WIDTH > 0 )
{
targetRect.y -= MOVE_WIDTH;
}

break;

//If user presses down arrow and the resulting move is inside the window, then move the Pacman's position
case SDLK_DOWN:
if ( targetRect.y + MOVE_WIDTH < (WINDOW_HEIGHT - targetRect.w) )
{
targetRect.y += MOVE_WIDTH;
}

break;

//If user presses right arrow and the resulting move is inside the window, then move the Pacman's position
case SDLK_RIGHT:
if ( targetRect.x + MOVE_WIDTH < (WINDOW_WIDTH - targetRect.w) )
{
targetRect.x += MOVE_WIDTH;
}

break;

//If user presses left arrow and the resulting move is inside the window, then move the Pacman's position
case SDLK_LEFT:
if ( targetRect.x - MOVE_WIDTH > 0 )
{
targetRect.x -= MOVE_WIDTH;
}

break;

default:
break;
}
}
}

SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, bmpTexture, NULL, &targetRect);
SDL_RenderPresent(renderer);
}

SDL_DestroyWindow(mainWindow);
SDL_DestroyTexture(bmpTexture);
SDL_DestroyRenderer(renderer);
SDL_Quit();

return 0;
}

编辑:为了回答 raser 的评论,这里是我找到 PollEvent 示例的链接:http://wiki.libsdl.org/SDL_CreateRenderer?highlight=%28%5CbCategoryAPI%5Cb%29%7C%28SDLFunctionTemplate%29

最佳答案

上述用于渲染 Pacman 的方法实际上可以很好地将点渲染到屏幕上。只需将点的图像存储在新纹理中,创建一个 SDL_Rect 来保存该纹理,然后使用 SDL_CreateRenderer 将点图像与吃 bean 人一起渲染到屏幕上。只要您没有使用 SDL_Delay 之类的东西来减慢帧速率,您就应该注意到渲染之间没有闪烁的点。

关于c++ - 在 SDL 中渲染静止图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21058880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com