2012-11-11 32 views
0

这是我的main.cppSDL不画图像

#include <cstdlib> 
#include <iostream> 

#include "Sprite.h" 
#include <SDL/SDL.h> 
#include <SDL/SDL_ttf.h> 
#include "SDL/SDL_mixer.h" 

#undef main 

SDL_Surface* m_pScreen; 
SDL_Surface* bg_image; 

int main(int argc, char* argv[]){ 

     SDL_Event evt; 
     bool p_running = true; 

     // initialize SDL 
     SDL_Init(SDL_INIT_EVERYTHING); 
     SDL_EnableUNICODE(SDL_ENABLE); 
     TTF_Init(); 

     // set the title bar text 
     SDL_WM_SetCaption("test1", NULL); 

     // create the screen surface 
     m_pScreen = SDL_SetVideoMode(500, 400, 32, SDL_DOUBLEBUF); 

     bg_image = NULL; 

     bg_image = Sprite::Load("images/bg.png"); 

     Sprite::Draw(m_pScreen, bg_image, 0, 0); 


     while(p_running) 
     { 
      SDL_WaitEvent(&evt); 
      if(evt.type == SDL_QUIT) 
      p_running = false; 
     } 

     SDL_Quit(); 

     system("pause"); 
     return 0; 

} 

Sprite.h:

#ifndef _SPRITE_H_ 
#define _SPRITE_H_ 

#include <SDL/SDL.h> 
#include <SDL/SDL_image.h> 

class Sprite 
{ 
    public: 

      Sprite(); 

      static SDL_Surface* Load(char* pFile); 

      static bool Draw(SDL_Surface* dest, SDL_Surface* src, int x, int y); 

      static bool Draw(SDL_Surface* dest, SDL_Surface* src, int x, int y, int x2, 
       int y2, int width, int height); 
}; 



// constructor 
Sprite::Sprite() {} 

SDL_Surface* Sprite::Load(char* File) 
{ 
    SDL_Surface* temp = NULL; 
    SDL_Surface* optimized = NULL; 

    if((temp = IMG_Load(File)) == NULL) 
    { 
      return NULL; 
    } 

    optimized = SDL_DisplayFormatAlpha(temp); 

    SDL_FreeSurface(temp); 

    //Uint32 colorkey = SDL_MapRGB(optimized->format, 0xFF, 0x80, 0xC0);//0xFF, 0x04, 0xC1); 
    //SDL_SetColorKey(optimized, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey); 

    return optimized; 
} 

bool Sprite::Draw(SDL_Surface* dest, SDL_Surface* src, int x, int y) 
{ 
     if(dest == NULL || src == NULL) 
     { 
        return false; 
     } 

     SDL_Rect destR; 

     destR.x = x; 
     destR.y = y; 

     SDL_BlitSurface(src, NULL, dest, &destR); 

     return true; 
} 




#endif 

我创建了一个文件夹,名为图像和里面我ahve增加了一个名为BG文件。 PNG 500X400有一些黑色的背景和一些白线,所以我可以设置,作为我的背景...

问题是,我总是得到一个黑色的屏幕,而不是我试图加载的图像

+0

您是否检查过“SDL_SetVideoMode”是否成功? – jrok

+0

@jrok我猜不,我该怎么做?我认为自从窗口打开成功应该不会有任何错误... – fxuser

回答

1

你忘了SDL_Flip()

while(p_running) 
{ 
    SDL_WaitEvent(&evt); 
    if(evt.type == SDL_QUIT) 
    p_running = false; 
    SDL_Flip(m_pScreen); 
    ^^^^^^^^^^^^^^^^^^^^ 
} 

,可能你的IDE改变应用程序的工作目录,在这种情况下,尝试打开一个完整路径上的图像。

+0

宾果!解决了这个问题,我没有必要使用完整的路径!谢谢 – fxuser

+0

@emartel的答案更正确。使用它的代码来进一步避免问题。 – tehten

1

通常,你希望你的主循环清除屏幕,绘制精灵,然后翻转缓冲区。这可以通过简单地改变你的循环一点很容易与你已有的代码来完成:

while(p_running) 
{ 
    SDL_PollEvent(&evt); // Poll for new events 
    if(evt.type == SDL_QUIT) 
    { 
    p_running = false; 
    break; 
    } 

    SDL_FillRect(m_pScreen, NULL, 0); // Clear the screen 

    // Perform "game" logic here, like moving the sprite 

    Sprite::Draw(m_pScreen, bg_image, 0, 0); // Draw the sprite 

    SDL_Flip(m_pScreen); // Flip the buffers 

} 

如果你做到这一点的变化,记住,你应该删除循环之前绘制调用。