2012-04-09 108 views
0

我正在開發一款遊戲,而我似乎無法畫出屏幕。當我調用SDL_BlitSurface時,沒有任何反應。但是,它不返回錯誤代碼。我編寫了一個類來處理圖像精靈,當我用調試器進行掃描時,我發現它正在返回表面的非空值,以便傳輸到主視頻表面。但是,當我打電話給我的繪圖功能時,它什麼都不做。我已經確認像素值完全不會改變表面。我究竟做錯了什麼?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); 
} 

最後,這裏是紐約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; 
} 

回答

0

您需要使用每幀功能SDL_Flip()才能使視頻表面的更改可見。該函數的參數是一個指向你窗口的SDL_Surface的指針。

+0

SDL_Flip(buffer)是System :: refresh_bottom()的一部分。我很抱歉如果我沒有說清楚。 – 2012-04-09 23:29:35

0

好吧,我想通了。我忘記初始化my_direction,因此精靈正在裁剪一個無意義的位置。我最終得到了一張32x32的黑色圖像,然後我將其拍攝到黑色表面上,使其不可見。