在SDL c ++中移动精灵

时间:2017-01-21 20:49:25

标签: c++ sdl sdl-2 motion

我想用箭头按钮在屏幕上移动一个精灵。但是,当旧的精灵仍然在屏幕上时,精灵会被渲染。所以我最终得到了很多点screenshot(我的精灵图像是一个点)。

dot.h

   //The dot that will move around on the screen
class Dot {
    public:
        //The dimensions of the dot
        SDL_Surface* dot;
        static const int DOT_WIDTH = 20;
        static const int DOT_HEIGHT = 20;
        int SCREEN_WIDTH;
        int SCREEN_HEIGHT;
    //Maximum axis velocity of the dot
    static const int DOT_VEL = 1;

    //Initializes the variables
    Dot(int SCREEN_WIDTH, int SCREEN_HEIGHT) {
        this->SCREEN_WIDTH = SCREEN_WIDTH;
        this->SCREEN_HEIGHT = SCREEN_HEIGHT;
        //Initialize the offsets
        mPosX = 0;
        mPosY = 0;

        //Initialize the velocity
        mVelX = 0;
        mVelY = 0;

        // render dot IMG
        this->dot = SDL::loadBMP("dot.bmp", false, false);

    }

    //Takes key presses and adjusts the dot's velocity
    void handleEvent( SDL_Event& e ) {
        //If a key was pressed
        if( e.type == SDL_KEYDOWN && e.key.repeat == 0 )
        {
            //Adjust the velocity
            switch( e.key.keysym.sym )
            {
                case SDLK_UP: mVelY -= DOT_VEL; break;
                case SDLK_DOWN: mVelY += DOT_VEL; break;
                case SDLK_LEFT: mVelX -= DOT_VEL; break;
                case SDLK_RIGHT: mVelX += DOT_VEL; break;
            }
        }
        //If a key was released
        else if( e.type == SDL_KEYUP && e.key.repeat == 0 )
        {
            //Adjust the velocity
            switch( e.key.keysym.sym )
            {
                case SDLK_UP: mVelY += DOT_VEL; break;
                case SDLK_DOWN: mVelY -= DOT_VEL; break;
                case SDLK_LEFT: mVelX += DOT_VEL; break;
                case SDLK_RIGHT: mVelX -= DOT_VEL; break;
            }
        }
    }

    //Moves the dot
    void move() {
        //Move the dot left or right
        mPosX += mVelX;

        //If the dot went too far to the left or right
        if( ( mPosX < 0 ) || ( mPosX + DOT_WIDTH > SCREEN_WIDTH ) )
        {
            //Move back
            mPosX -= mVelX;
        }

        //Move the dot up or down
        mPosY += mVelY;

        //If the dot went too far up or down
        if( ( mPosY < 0 ) || ( mPosY + DOT_HEIGHT > SCREEN_HEIGHT ) )
        {
            //Move back
            mPosY -= mVelY;
        }
    }

    //Shows the dot on the screen
    void render() {
        //Show the dot
        SDL_Rect dstrect = { mPosX, mPosY};
        SDL_BlitSurface(this->dot, NULL, SDL::screenSurface, &dstrect);

        // SDL_FreeSurface(this->dot);
    }

private:
    //The X and Y offsets of the dot
    int mPosX, mPosY;

    //The velocity of the dot
    int mVelX, mVelY;
};

我的main.cpp文件。它有一些对input.h和sdl.h的引用,但它们并不重要

的main.cpp

    //Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>
#include <iostream>

#include "Sdl.h"
#include "Dot.h"
#include "Input.h"
class Main {
public:
SDL_Event e;
Input* input;
Dot* dot;
//constructor
Main() {
    //Screen dimension constants
    int SCREEN_WIDTH = 640;
    int SCREEN_HEIGHT = 480;

    SDL::init("NHTV-Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    input = new Input();
    dot = new Dot(SCREEN_WIDTH, SCREEN_HEIGHT);
    this->loop();
}
loop() {
    //Main loop flag
    bool quit = false;

    //While application is running
    while( !input->quit ) {
        gameLoop();
    }
    // run when while loops ends
    this->quit();
}


gameLoop() {
    // SDL::blitSurface(this->dot);
    dot->move();
    if(input->movement) {
        dot->handleEvent(input->getEvent());
        dot->render();
    }
    input->loop();
}
quit() {
    // close sdl process
    SDL::close();
}
};
// arguments for cross-plafrom
int main( int argc, char* args[] ) {
    Main* main = new Main();

    return 0;
}

这是我的结果:screenshot。如何移动现有的精灵而不是创建新精灵?或删除旧的精灵?

1 个答案:

答案 0 :(得分:1)

简而言之,您需要在绘制每个帧之前清除屏幕。

我建议使用SDL_Renderer将图形绘制到后备缓冲区,这是一个隐藏的表面,而不是直接写入窗口表面。

在绘制到后备缓冲区之前,请使用SDL_RenderClear()功能清除后备缓冲区。或者,如果您知道将覆盖所有内容,只需保存此函数调用。

准备好显示后备缓冲区后,使用后备缓冲区切换当前帧缓冲区,即调用SDL_RenderPresent()