c++ - SDL不会绘制图像

标签 c++ sdl sprite render draw

这是我的main.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:

#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


我创建了一个名为images的文件夹,并在其中添加了一个名为bg.png 500x400的文件,该文件带有一些黑色背景和一些白色线条,因此我可以将其设置为我的背景...

问题是我总是黑屏,而不是我尝试加载的图像

最佳答案

您忘记了SDL_Flip()

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


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

关于c++ - SDL不会绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13333181/

相关文章:

c++ - header 中的值在定义后包含时设置为NULL

c++ - SDL OpenGL 纹理丢失

php - svg 文件 phpstorm 的自动化 CSS Sprite

Java 2D "tank"游戏 : Shooting 'missile' from rotated sprite doesn't work

c++ - 为什么 snprintf 在打印单个数字时始终比 ostringstream 快 2 倍?

c++ - 写入多维 vector 线程安全的子元素中的元素吗?

c++ - 全局变量类 c++

android - Andengine - 在 Sprites 上/在 Sprites 上绘制文本

c++ - unordered_map - {{key,value},{key,value}} 语法无效

c++ - 为什么存在符号时为什么 “symbol lookup error”, “undefined symbol”