开发者

Problems blitting surface in SDL from vector of SDL_Rect

开发者 https://www.devze.com 2023-03-30 16:29 出处:网络
#include \"main.h\" SDL_Surface* screen; //The screen Surface SDL_Surface* Snake_Body; //The snake surface (shape to draw)
#include "main.h"

SDL_Surface* screen; //The screen Surface
SDL_Surface* Snake_Body; //The snake surface (shape to draw)

SDL_Rect rectangle1; //Red border
SDL_Rect rectangle2; //Black Board

SDL_Event event;    //Keyboard handling
direction facing;

std::vector<SDL_Rect> snake (3); //Snake body locations



SDL_Surface *LoadSnake()
{
    SDL_Surface* temporary;

    Uint32 rmask,gmask,bmask,amask;

    #if SDL_BYTEORDER == SDL_BIG_ENDIAN
        rmask = 0xff000000;
        gmask = 0x00ff0000;
    开发者_运维百科    bmask = 0x0000ff00;
        amask = 0x000000ff;
    #else
        rmask = 0x000000ff;
        gmask = 0x0000ff00;
        bmask = 0x00ff0000;
        amask = 0xff000000;
    #endif

    temporary = SDL_CreateRGBSurface(SDL_SWSURFACE,5,5,32,rmask,gmask,bmask,amask); //Make an empty surface

    SDL_FillRect(temporary,NULL,SDL_MapRGB(screen->format,0,255,0)); //Fill it with color

    return temporary; //return it
}


bool init()
{
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1 )
        return false;

    screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE);

    if(!screen)
        return false;

    SDL_WM_SetCaption("Snake v0.1",NULL);

    Snake_Body = LoadSnake();

    rectangle1.x = 0;  //Red border
    rectangle1.y = 0;
    rectangle1.w = 500;
    rectangle1.h = 500;

    rectangle2.x = 25; //Black background
    rectangle2.y = 25;
    rectangle2.w = 450;
    rectangle2.h = 450;

    return true;
}

SDL_Surface *loadImage(const char* filename)
{
    SDL_Surface *loadedImage = NULL;
    SDL_Surface *optimizedImage = NULL;

    loadedImage = SDL_LoadBMP(filename);

    if (loadedImage)
    {
        optimizedImage = SDL_DisplayFormat(loadedImage);
        SDL_FreeSurface(loadedImage);
    }

    return optimizedImage;
}

void pollInput()
{
    while(SDL_PollEvent(&event))
    {
        if(event.type == SDL_QUIT)
        {
            end = true;
        }

        if(event.type == SDL_KEYUP)
        {
            switch(event.key.keysym.sym)
            {
            case SDLK_ESCAPE:
                end = true; break;
            case SDLK_UP:
                facing = up; break;
            case SDLK_DOWN:
                facing = down; break;
            case SDLK_RIGHT:
                facing = right; break;
            case SDLK_LEFT:
                facing = left; break;
            }
        }
    }
}

void Update()
{
    for(unsigned int i = 1; i < snake.size(); i++)
    {
        snake[i] = snake[i-1];
    }

    switch(facing)
    {
    case up:
        snake[0].y -= 5; break;
    case down:
        snake[0].y += 5; break;
    case left:
        snake[0].x -= 5; break;
    case right:
        snake[0].x += 5; break;
    }

}

void SetUp()
{
    snake[0].x = 250;
    snake[0].y = 250;


    snake[1].x = 250 + 15;
    snake[1].y = 250;

    snake[2].x = 250 + 15 + 15;
    snake[2].y = 250;
}

void Draw()
{
    unsigned short i;
    int x;

    SDL_FillRect(screen,&rectangle1,SDL_MapRGB(screen->format,255,0,0)); //Red Border

    SDL_FillRect(screen,&rectangle2,SDL_MapRGB(screen->format,0,0,0)); //Black Board

    for(i = 0; i < snake.size(); i++)
    {
        assert(SDL_BlitSurface(Snake_Body,NULL,screen,&snake[i]) == 0);
    }

    SDL_Flip(screen);
}

void Finish()
{
    SDL_FreeSurface(Snake_Body);

    SDL_Quit();
}

I have this code, it's a game of snake written in SDL I am working on, however, I seem to have a problem.

SDL draws the background just fine, but for some reason it won't draw the snake. I set up an assert over there to check if the SDL_BlitSurface function succeeded, and it returns a normal value (0), even though nothing shows up on screen.

I set up the texture of the snake node to be a single green rectangle, Snake_Body, and I coded the locations that it needs to be drawn in a std::vector container, so I can call the SDL_BlitSurface() function on each of the elements in the vector, I have tracked the values and they are changing correctly, but for some reason the image stil isn't drawn. I have also attempted to replace my SDL_BlitSurface() function with a SDL_FillRect() in the same position, but it doesn't change anything.

EDIT: I have added my main loop below this edit:

#include "main.h"

bool end = false;
Uint32 time;

int main(int argc,char* argv[])
{
    if(!init())
        return 1;

    SetUp();

    time = SDL_GetTicks();

    while(!end)
    {
        if(time+1000<SDL_GetTicks())
        {
            Update();
            time = SDL_GetTicks();
        }

        pollInput();
        Draw();
    }

    Finish();

    return 0;
}


You don't appear to be doing any bounds checking on the snake's position to make sure it stays on the screen. So when it moves off the screen, it stays off and never comes back. My guess is that you aren't regulating the framerate properly, or you aren't regulating it at all(I would need to see your main loop), so the snake moves off the screen so fast that you never even see it.

What happens if you don't call the Update function?


If you posted the whole code then there's a call to SetUp missing. This won't set up the snake and it is probably somewhere outside the screen as the vector contains garbage. Also, even though SDL_BlitSurface ignores dstrect width and height, it's a good practice to initialize these to some meaningful values.

0

精彩评论

暂无评论...
验证码 换一张
取 消