简体   繁体   English

C ++和SDL:显示图像有问题吗?

[英]C++ and SDL: Problem with showing image?

For some reason SDL refuses to render an image. 由于某些原因,SDL拒绝渲染图像。 I don't see why and it's really bogging down my progress on a 2d game i'm developing. 我不知道为什么,这真的使我在开发2D游戏方面的进展陷入停滞。 Everything is linked properly and such. 一切都正确链接,等等。 Here's my code: 这是我的代码:

//main.cpp
#include "main.h"

void game::createWindow(const int SCREEN_W, const int SCREEN_H, const char* SCREEN_NAME)
{
 buffer = SDL_SetVideoMode(SCREEN_W, SCREEN_H, 0, NULL);
 SDL_WM_SetCaption(SCREEN_NAME, NULL);
}

void game::enterLoop()
{
 while(Running == true)
 {
  SDL_BlitSurface(zombie, NULL, buffer, NULL);
  SDL_Flip(buffer);

  while(SDL_PollEvent(&gameEvent))
  {
   if(gameEvent.type == SDL_QUIT)
   {
    Running = false;
   }
  }
 }
}

void game::loadContent()
{
 zombie = SDL_LoadBMP("zombie.bmp");
}

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

 SDL_Init(SDL_INIT_EVERYTHING);
 gameObj.createWindow(960, 600, "uShootZombies");
 gameObj.loadContent();
 gameObj.enterLoop();

 SDL_Quit();

 return 0;
}

//main.h
#include <SDL.h>
#include <fstream>
#include <string>

using namespace std;

class game
{
 public:
 void createWindow(const int SCREEN_W, const int SCREEN_H, const char* SCREEN_NAME);
 void enterLoop();
 void loadContent();

 game()
 {
  Running = true;
 }

 ~game()
 {
  SDL_FreeSurface(buffer);

  SDL_FreeSurface(background);
  SDL_FreeSurface(player);
  SDL_FreeSurface(zombie);
 }

 private:
 SDL_Surface* buffer;

 SDL_Surface* background;
 SDL_Surface* player;
 SDL_Surface* zombie;

 SDL_Event gameEvent;
 bool Running;
};NU

I just copied all of your code to use in code::blocks and it works fine. 我只是复制了您的所有代码以在code :: blocks中使用,并且工作正常。 Of course I was using my own .bmp file which I named "zombie.bmp" 当然,我使用的是自己的.bmp文件,我将其命名为“ zombie.bmp”

Are you sure your .bmp file is ok? 您确定您的.bmp文件正常吗?

Be aware that if you originally save it as a .jpeg file or something like that, then simply renamed it to .bmp, it won't work (And it won't throw an error either) 请注意,如果您最初将其另存为.jpeg文件或类似名称,然后将其重命名为.bmp,它将无法正常工作(也不会引发错误)

It must be originally saved as a bmp. 它必须最初保存为bmp。

That is all I can think of. 这就是我能想到的。

It seems that Sour Lemon has already solved your problem, but I still thought it would be worth pointing out that the above code doesn't perform any checks to make sure that your zombie image was actually loaded correctly. 看来Sour Lemon已经解决了您的问题,但我仍然值得指出的是,上述代码并未执行任何检查来确保您的僵尸图像已正确加载。

You should be doing something like this: 您应该这样做:

void game::loadContent()
{
    zombie = SDL_LoadBMP("zombie.bmp");
    if (zombie == NULL) {
        ReportError(SDL_GetError());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM