0

我正在开发一款游戏,但我似乎根本无法在屏幕上画画。当我调用 SDL_BlitSurface 时,什么也没有发生。但是,它不会返回错误代码。我编写了一个类来处理图像精灵,当我使用调试器单步执行时,我看到它正在返回非空值,以使表面闪烁到主视频表面。然而,当我调用我的绘图函数时,它什么也没做。我已经确认表面的像素值根本没有改变。我究竟做错了什么?

这是我的 main.cpp:

//Standard libs
#include <SDL.h>

//Our gaming base
#include "game_base.h"

//SDL redefines main as winmain, but this is a console application so we don't want that
#undef main

const int MAX_FPS = 80;

int main ( int argc, char* argv[] ) {
    //Initialization
    System game( false, 640, 480, "Platformer", MAX_FPS ); //Create a game that is not fullscreen with a resolution of 640x480
    SDL_Surface *buffer = NULL;
    Input *input = NULL;
    buffer = game.get_buffer();
    Image_Sprite *player_sprite = NULL;
    player_sprite = new Image_Sprite( "data/player.bmp", 1, 4 );
    SDL_Rect hitbox;
    hitbox.w = 32;
    hitbox.h = 32;
    hitbox.x = 0;
    hitbox.y = 0;
    Player player( 100, 100, hitbox, 1.0, RIGHT, player_sprite );
    //Main game loop
    while( !game.check_is_done() ) {
        game.refresh_top();
        player.draw( buffer );
        game.refresh_bottom();
    }

    //Cleanup
    delete input;
    delete player_sprite;
}

这是我正在使用的绘图功能:

void Character::draw( SDL_Surface *destination ) {
    SDL_Rect coordinates;
    SDL_Surface *sprite;
    sprite = my_sprite->get_frame( my_frame, my_direction );
    coordinates.x = (int)get_x();
    coordinates.y = (int)get_y();

    SDL_BlitSurface( sprite, NULL, destination, NULL );
}

最后,这里是 ny get_frame() 函数:

SDL_Surface* Image_Sprite::get_frame( int x, int y ) {
    SDL_PixelFormat *format;
    format = my_sprite->format;
    SDL_Surface *frame = SDL_CreateRGBSurface( my_sprite->flags, my_frame_width, my_frame_height, format->BitsPerPixel,
                                                    format->Rmask, format->Gmask, format->Bmask, format->Amask );
    SDL_Rect *frame_crop = new SDL_Rect;
    frame_crop->x = my_frame_width * x;
    frame_crop->y = my_frame_height * y;
    frame_crop->w = my_frame_width;
    frame_crop->h = my_frame_height;

    SDL_Rect *coordinates = new SDL_Rect;
    coordinates->x = 0;
    coordinates->y = 0;

    SDL_BlitSurface( my_sprite, frame_crop, frame, coordinates );
    delete frame_crop;
    return frame;
}
4

2 回答 2

0

好吧,我想通了。我忘记初始化 my_direction,所以精灵正在裁剪一个无意义的位置。最后我得到了一个 32x32 黑色的图像,然后我将其粘贴到黑色表面上,使其不可见。

于 2012-04-11T14:05:48.743 回答
0

您需要使用SDL_Flip()每帧功能来使对视频表面的更改可见。该函数的参数是指向窗口的 SDL_Surface 的指针。

于 2012-04-09T23:27:41.447 回答