SDL 2 blitting BMP文件

时间:2018-12-13 03:34:36

标签: c++ sdl-2 game-development

当前正在尝试通过SDL 2在窗口上显示BMP文件

这是我的main.cpp文件:

  `#include "pch.h"
   #include <iostream>
   #include<SDL.h>
   using namespace std;
   int main(int argc, char* args[])
   {
     bool run=true;
     sdl a;
     if (a.init() == false)
      {
        cout << "SDL not working" << endl;
        return 0;
      }
      else
        if (a.screendisplay() == false)
        {
            cout << "Enable to open the window" << endl;
            return 0;
        }
        else
             if (a.loadmedia() == false)
             {
             cout << "Unable to load the image" << endl << SDL_GetError()<<endl;
              return 0;
             }
             else
              a.imageprocessing();
          SDL_Delay(2000);
          a.quit();
    return 0;
}`.   

这是我的pch.H文件,上面的代码中已提到该文件:

class sdl
{
public:
    bool init();
    bool screendisplay();
    bool quit();
    bool loadmedia();
    bool imageprocessing();
private:
    SDL_Surface *gsurface = NULL;
    SDL_Window * Window = NULL;
    SDL_Surface * screenSurface = NULL;


};

Pch.cpp文件:

 #include "pch.h"
using namespace std;
bool sdl::init()
{
    bool sucess = true;
    if (SDL_Init(SDL_INIT_VIDEO) <0)
    {
        cout << "SDL not Working properply" << endl;
        return false;
    }
    else return true;
};
bool sdl::screendisplay()
{
    bool sucess;
    Window = SDL_CreateWindow("SDL Tutorial", 0, 0,640, 280, SDL_WINDOW_SHOWN);
    if (Window = NULL)
    {
        return sucess = false;
    }
    else
        screenSurface= SDL_GetWindowSurface(Window);
        return sucess = true;
};
bool sdl::loadmedia()
{
    bool success = true;
        gsurface = SDL_LoadBMP("asd.bmp");
        if (gsurface == NULL)
        {
            success = false;
        }
        return success;

};
bool sdl::imageprocessing()
{
    bool success = true;
    if (SDL_BlitSurface(gsurface, NULL, screenSurface, NULL) < 0)
    {
        return success = false;
    }
    else
    SDL_UpdateWindowSurface(Window);
    SDL_Delay(2000);
    return success;
};
bool sdl::quit()
{
    bool sucess;

    SDL_DestroyWindow(Window);
        SDL_Quit();
        return sucess = true;

};

当我运行该程序时,将出现窗口,但没有图像。我在这里做什么错了?

1 个答案:

答案 0 :(得分:1)

我认为问题出在您的screendisplay()函数中:

bool sdl::screendisplay()
{
    bool sucess;
    Window = SDL_CreateWindow("SDL Tutorial", 0, 0,640, 280, SDL_WINDOW_SHOWN);
    if (Window = NULL) // <- Right here
    {
        return sucess = false;
    }
    else
        screenSurface= SDL_GetWindowSurface(Window);
        return sucess = true;
};

您正在执行任务而不是进行比较。您应该打开编译器警告,以便它能够捕获到这一点。