C++ SDL不';t画图

C++ SDL不';t画图,c++,sdl,sprite,render,draw,C++,Sdl,Sprite,Render,Draw,这是我的主要.cpp #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, c

这是我的主要.cpp

#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”
#包括
#包括
#包括“SDL/SDL_混合器.h”
#未定义主
SDL_表面*m_屏幕;
SDL_表面*bg_图像;
int main(int argc,char*argv[]){
SDL_事件evt;
bool p_running=true;
//初始化SDL
SDL_Init(SDL_Init_EVERYTHING);
SDL_启用Unicode(SDL_启用);
TTF_Init();
//设置标题栏文本
SDL_WM_SetCaption(“test1”,NULL);
//创建屏幕表面
m_pScreen=SDL_设置视频模式(500、400、32、SDL_DOUBLEBUF);
bg_image=NULL;
bg_image=Sprite::Load(“images/bg.png”);
Sprite::绘制(m_屏幕,bg_图像,0,0);
同时(p_运行)
{
SDL_WaitEvent(&evt);
if(evt.type==SDL\u QUIT)
p_running=false;
}
SDL_退出();
系统(“暂停”);
返回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
\ifndef\u SPRITE\H_
#定义精灵_
#包括
#包括
雪碧
{
公众:
雪碧();
静态SDL_表面*荷载(字符*文件);
静态布尔图(SDL_表面*dest,SDL_表面*src,int x,int y);
静态布尔图(SDL_表面*dest,SDL_表面*src,int x,int y,int x2,
整数y2,整数宽度,整数高度);
};
//建造师
Sprite::Sprite(){}
SDL_表面*精灵::加载(字符*文件)
{
SDL_表面*温度=零;
SDL_曲面*优化=空;
如果((temp=IMG_加载(文件))==NULL)
{
返回NULL;
}
优化=SDL_显示格式Alpha(温度);
SDL_自由表面(温度);
//Uint32 colorkey=SDL_MapRGB(优化->格式,0xFF,0x80,0xC0);//0xFF,0x04,0xC1);
//SDL_SetColorKey(优化,SDL_RLEACCEL | SDL_SRCCorKey,colorkey);
收益优化;
}
画图(SDL_表面*dest,SDL_表面*src,int x,int y)
{
if(dest==NULL | | src==NULL)
{
返回false;
}
直接销毁;
分解x=x;
分解y=y;
SDL_BlitSurface(src、NULL、dest和dest);
返回true;
}
#恩迪夫
我已经创建了一个名为images的文件夹,在里面我添加了一个名为bg.png 500x400的文件,带有一些黑色背景和一些白色线条,所以我可以将其设置为我的背景


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

您忘记了
SDL\u Flip()


您的ide可能会更改应用程序的工作目录,在这种情况下,请尝试在完整路径上打开映像。

通常,您希望主循环清除屏幕,绘制精灵,然后翻转缓冲区。这可以很容易地用您已有的代码完成,只需稍微更改一下循环:

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

}

如果进行此更改,请记住,应该在循环之前删除Draw调用。

是否检查了
SDL\u SetVideoMode
是否成功?@jrok我想没有,我该怎么做?我想既然窗口成功打开,就不应该有任何错误了……宾果!这解决了问题,我不必使用完整路径!谢谢@emartel的答案更正确。使用它的代码来避免进一步的问题。
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

}