使用 SDL2 渲染精灵

Rendering Sprites with SDL2

本文关键字:精灵 SDL2 使用      更新时间:2023-10-16

我对游戏开发非常陌生。我正在尝试使用 SDL 在窗口上移动精灵。我使用 http://gamedevgeek.com/tutorials/moving-sprites-with-sdl/作为参考,以帮助我了解 SDL。但是,这种 blit 方法不适用于 SDL2。我研究并发现我必须将表面转换为纹理并渲染它们,但我遇到了一些令人沮丧的困难。运行时,背景图像似乎渲染得很好,但精灵只出现在窗口的一角,移动时,似乎被背景覆盖了。这是代码:

#include <iostream>
#include "stdafx.h"
#include <SDL.h>
#include <SDL_image.h>
const int WIDTH = 900;
const int HEIGHT = 360;
const int SPRITE_SIZE = 256;
int main(int argc, char *argv[])
{
    SDL_Surface *imageSurface = NULL;
    SDL_Surface *windowSurface = NULL;
    SDL_Surface *temp = NULL;
    SDL_Surface *sprite = NULL;
    SDL_Surface *SDL_DisplayFormat(SDL_Surface *surface);
    SDL_Rect    rcSprite;
    SDL_Rect    gdSprite;
    SDL_Event windowEvent;
    SDL_Event   event;
    SDL_Renderer *renderer = NULL;
    SDL_Texture *texture;
    SDL_Texture *spriteTexture;
    const Uint8 *keystate;
    int colorkey;
    int count;
    int xPosition = 0;
    int yPosition = 0;
    int gameover = 0;

    SDL_Window *window = SDL_CreateWindow("ABDUCTO", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_ALLOW_HIGHDPI);
    windowSurface = SDL_GetWindowSurface(window);
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
    imageSurface = IMG_Load("farm.png");
    sprite= IMG_Load("sprite6.png");
    texture = SDL_CreateTextureFromSurface(renderer, imageSurface);
    spriteTexture = SDL_CreateTextureFromSurface(renderer, sprite);
    SDL_FreeSurface(sprite);
    SDL_FreeSurface(imageSurface);
    //rcSprite used as source rectangle, gdSprite as destination rectangle. Initialize them to the same position
    rcSprite.x = xPosition;
    rcSprite.y = yPosition;
    rcSprite.w = SPRITE_SIZE;
    rcSprite.h = SPRITE_SIZE;
    gdSprite.x = xPosition;
    gdSprite.y = yPosition;
    gdSprite.w = SPRITE_SIZE;
    gdSprite.h = SPRITE_SIZE;
    while (!gameover)
    {
        if (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
                case SDL_QUIT:
                    gameover = 1;
                    break;
            case SDL_KEYDOWN:
                switch (event.key.keysym.sym) 
                {
                    case SDLK_ESCAPE:
                    case SDLK_q:
                        gameover = 1;
                        break;
                }
                break;
            }
        }
        keystate = SDL_GetKeyboardState(NULL);
        // When key pressed, update the destination rectangle
        if (keystate[SDL_SCANCODE_LEFT]) {
            gdSprite.x -= 2;
        }
        if (keystate[SDL_SCANCODE_RIGHT]) {
            gdSprite.x += 2;
        }
        if (keystate[SDL_SCANCODE_UP]) {
            gdSprite.y -= 2;
        }
        if (keystate[SDL_SCANCODE_DOWN]) {
            gdSprite.y += 2;
        }
        if (gdSprite.x < 0) {
            gdSprite.x = 0;
        }
        else if (gdSprite.x > WIDTH - SPRITE_SIZE) {
            gdSprite.x = WIDTH - SPRITE_SIZE;
        }
        if(gdSprite.y < 0) {
            gdSprite.y = 0;
        }
        else if (gdSprite.y > HEIGHT - SPRITE_SIZE) {
            gdSprite.y = HEIGHT - SPRITE_SIZE;
        }
        //Render the window
        SDL_RenderClear(renderer);
        SDL_RenderCopy(renderer, texture, NULL, NULL);
        SDL_RenderCopy(renderer, spriteTexture, &rcSprite, &gdSprite);
        SDL_RenderPresent(renderer);
        //SDL_BlitSurface(imageSurface, NULL, windowSurface, NULL);
        //SDL_BlitSurface(sprite, NULL, imageSurface, &rcSprite);
        //SDL_UpdateWindowSurface(window);

        //update the source rectangle to move with the sprite??
        rcSprite.x = gdSprite.x;
        rcSprite.y = gdSprite.y;
    }

    SDL_DestroyTexture(spriteTexture);
    SDL_DestroyTexture(texture);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);

    return 0;
    SDL_Quit();
}

任何意见都值得赞赏。谢谢。

可能必须对图像的路径执行某些操作,或者在编译时将文件复制到资源中。您使用的是Windows,OSX还是Linux?您使用的是什么 IDE?

但我注意到两件事:

1(在SDL_CreateWindow之前,您应该初始化 SDL: SDL_Init(SDL_INIT_EVERYTHING);

2( SDL_Quit();永远不会被调用,因为上面的一行退出了主函数,return 0;=>你应该交换线条!

注意到更多:3(不要update the source rectangle to move with the sprite只需将整个精灵渲染到 gdSprite 位置: SDL_RenderCopy(renderer, sTexture, NULL, &gdSprite);