使用SDL2渲染精灵

时间:2018-01-04 00:59:47

标签: c++

我对游戏开发非常陌生。我试图用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();
}

任何输入都表示赞赏。谢谢。

1 个答案:

答案 0 :(得分:0)

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

但我发现了两件事:

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

2) 永远不会调用SDL_Quit();,因为您上面的一行会使用return 0;退出主函数 =&GT;你应该换掉这些线!

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