Search code examples
c++loadingsdlbmp

C++ and SDL: Problem with showing image?


For some reason SDL refuses to render an image. I don't see why and it's really bogging down my progress on a 2d game i'm developing. 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

Solution

  • I just copied all of your code to use in code::blocks and it works fine. Of course I was using my own .bmp file which I named "zombie.bmp"

    Are you sure your .bmp file is ok?

    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)

    It must be originally saved as a bmp.

    That is all I can think of.