简体   繁体   English

使用SDL2渲染精灵

[英]Rendering Sprites with SDL2

I am extremely new to game development. 我对游戏开发非常陌生。 I am attempting to move sprites on a window with SDL. 我正在尝试使用SDL在窗口上移动精灵。 I was using http://gamedevgeek.com/tutorials/moving-sprites-with-sdl/ as a reference for this to help me get a feel for SDL. 我使用http://gamedevgeek.com/tutorials/moving-sprites-with-sdl/作为参考,以帮助我了解SDL。 However, this method of blit doesn't work with SDL2. 但是,这种blit方法不适用于SDL2。 I researched and found that I must convert surfaces to textures and render those, but I am running into some frustrating difficulties. 经过研究,我发现必须将表面转换为纹理并进行渲染,但是我遇到了一些令人沮丧的难题。 When ran, the background image seems to render fine, but the sprite only appears in the corner of the window, and when moved, it seems to be overwritten by the background. 运行时,背景图像似乎可以很好地渲染,但是精灵仅显示在窗口的一角,而移动时,它似乎被背景覆盖。 Here is the code: 这是代码:

#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();
}

Any input is appreciated. 任何输入表示赞赏。 Thank you. 谢谢。

probably has to do something with the path of your images, or copying the files into the resources on compilation. 可能必须对图像的路径进行某些处理,或者将文件复制到编译时的资源中。 are you on windows, osx or linux? 您在Windows,OSX还是Linux上? What IDE are you using? 您正在使用什么IDE?

But i noticed two things: 但我注意到两件事:

1) Before the SDL_CreateWindow you should initialize SDL: SDL_Init(SDL_INIT_EVERYTHING); 1)在SDL_CreateWindow之前,您应该初始化SDL: SDL_Init(SDL_INIT_EVERYTHING);

2) SDL_Quit(); 2) SDL_Quit(); will never be called because one line above you quit the main function with return 0; 永远不会被调用,因为您上方的一行以return 0;退出了main函数return 0; => you should swap the lines! =>您应该换行!

noticed some more: 3) DON'T update the source rectangle to move with the sprite just render the whole sprite to the gdSprite loction: SDL_RenderCopy(renderer, sTexture, NULL, &gdSprite); 注意更多:3)不要update the source rectangle to move with the sprite只需将整个精灵渲染到gdSprite位置即可: SDL_RenderCopy(renderer, sTexture, NULL, &gdSprite);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM