2013-06-12 30 views
-1

所以我在學習SDL的時候正在構建一個迷你引擎的過程中,當我碰到緩衝區並翻轉它時,我一直遇到問題。不管我做什麼,你的屏幕都是黑色的。你們能幫我出去嗎?SDL沒有讓我的圖像變成緩衝區

我有兩個類:圖形類,和系統類,一起工作blit圖像。 Graphics類具有一個通用函數,System利用這些函數。系統有一個Graphics對象作爲它的一個可變參數,所有的圖形渲染都是通過它進行的。我哪裏錯了?屏幕沒有我的blit圖像到緩衝可言:(

//Graphics.h

#ifndef GRAPHICS_H 
#define GRAPHICS_H 

#include "SDL/SDL.h" 
#include <string> 

class Graphics 
{ 
    public: 
     Graphics(); 
     void loadImages(SDL_Surface* image, std::string imageName); 
     void drawImages(SDL_Surface* image, SDL_Rect crop, SDL_Rect Pos); 
     SDL_Surface* ImgLoad(std::string filename); 
     void flip(); 
     void cleanUp(SDL_Surface* image); 

     SDL_Surface* background; 
     SDL_Surface* buffer; 

     friend class System; 

    private: 


     SDL_Rect backgrdCrop; 
     SDL_Rect backgrdPos; 
}; 

#endif // GRAPHICS_H 

//Graphics.cpp

#include "Graphics.h" 
#include "SDL/SDL.h" 
#include "SDL/SDL_mixer.h" 
#include "SDL/SDL_image.h" 
#include <string> 

Graphics::Graphics() 
{ 
    buffer = NULL; 
    background = NULL; 

    backgrdCrop = {0, 33, 32, 33}; 
} 

void Graphics::loadImages(SDL_Surface* image, std::string imageName){ 
    image = ImgLoad(imageName); 

    Uint32 colorkey = SDL_MapRGB(image->format, 0xFF, 0 , 0xFF); 
    SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey); 
} 

void Graphics::drawImages(SDL_Surface* image, SDL_Rect crop, SDL_Rect Pos) 
{ 
    SDL_BlitSurface(image, &crop, buffer, &Pos); 
} 

void Graphics::flip(){ 
    SDL_Flip(buffer); 
} 

void Graphics::cleanUp(SDL_Surface* image){ 
    SDL_FreeSurface(image); 
} 

SDL_Surface* Graphics::ImgLoad(std::string filename) 
{ 
    SDL_Surface* loadedImage = NULL; 
    SDL_Surface* optImage = NULL; 

    loadedImage = IMG_Load(filename.c_str()); 
    optImage = SDL_DisplayFormat(loadedImage); 

    SDL_FreeSurface(loadedImage); 

    return optImage; 
} 

//System.h

#ifndef SYSTEM_H 
#define SYSTEM_H 
#include "SDL/SDL.h" 
#include "Graphics.h" 
#include "Input.h" 

class System 
{ 
    public: 
     System(); 
     void init(bool fullscreen, int width, int height); 
     void input(); 
     void draw(); 
     bool isDone(); 
     void quit(); 
     void flip(); 
     Graphics g; 

    private: 
     bool done; 

     Input inp; 
     SDL_Surface* back; 
}; 

#endif // SYSTEM_H 

// system.cpp

#include <iostream> 
#include "System.h" 
#include "Graphics.h" 
#include "SDL/SDL.h" 
#include "Input.h" 
#include "SDL/SDL_mixer.h" 

System::System() 
{ 
    done = 0; 
} 

void System::init(bool fullscreen, int width, int height) 
{ 

    SDL_Init(SDL_INIT_EVERYTHING); 
    Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096); 

    if(fullscreen == 0){ 

     g.buffer = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE | SDL_FULLSCREEN); 

    } else { 

     g.buffer = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE); 
    } 

    g.loadImages(g.background, "swift.jpg"); 
    SDL_WM_SetCaption("Picking Sticks", NULL); 
} 

void System::input() 
{ 
    SDL_Rect basic = {0,0,0,0}; 

     inp.Update(); 
     g.backgrdPos = basic; 

} 

void System::draw() 
{ 
     SDL_Rect basic = {0,0,0,0}; 
     SDL_Rect sprite = {0, 33, 32, 33}; 

     SDL_BlitSurface(g.background, &basic, g.buffer, &sprite); 

} 

void System::flip() 
{ 
    g.flip(); 
} 

bool System::isDone() 
{ 
    return done; 
} 

void System::quit() 
{ 
    g.cleanUp(g.background); 
    SDL_Quit(); 
} 

//main.cpp

#include <iostream> 
#include "System.h" 
#include "Graphics.h" 
#include "SDL/SDL.h" 
#include "SDL/SDL_image.h" 
#include "SDL/SDL_mixer.h" 
#include "Input.h" 
#include <string> 

using namespace std; 

SDL_Event event; 

SDL_Surface* temp = NULL; 

string name = "swift.jpg"; 


int main(int argc, char *args[]) 
{ 
    System sys; 
    sys.init(1, 640, 480); 

    while(sys.isDone() == 0) 
    { 
     if(SDL_PollEvent(&event)) 
     { 
      if(event.type == SDL_QUIT) 
      { 
       sys.quit(); 

       return 0; 

      } 
     } 

     sys.input(); 

     sys.draw(); 

     sys.flip(); 
    } 

} 

回答

0

您在void System::draw()有一個語義錯誤。

當你聲明SDL_Rect basic = {0,0,0,0};如果您檢查一下

int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect); 

功能不你會發現basic RECT作爲SDL_Rect *srcrect你設置其值爲0。。這將使g.background的寬度和高度等於0。沒有任何東西會被畫出來。

如果要繪製整個g.background,請將NULL作爲SDL_Rect *srcrect參數。