1
\$\begingroup\$

I have come across a very wierd problem. Here is my code:

#include <iostream>
#include "SDL.h"

using namespace std;

class MyGame
{
public:

    SDL_Surface* image;
    SDL_Surface* window;
    SDL_Event* e;

    void Start() 
    {
        SDL_Init(SDL_INIT_EVERYTHING);

        window = SDL_SetVideoMode(800,600,32,SDL_SWSURFACE);

        image = SDL_LoadBMP("Bitmap.bmp");
        SDL_BlitSurface(image,NULL,window,NULL);
        SDL_FreeSurface(image);

        SDL_Event evt;

        bool programrunning = true;
        while(programrunning)
        {
            SDL_WaitEvent(&evt);
            if(evt.type == SDL_QUIT) programrunning = false;
        }

        SDL_Quit();
    }

    void Update() 
    {
        SDL_Flip(window);
    }
};

int main(int argc,char* args[]) 
{
    MyGame obj;
    obj.Start();
    while(true) 
    {
        obj.Update();
    }

    return 0;
}

Whenever I run this code on Code::Blocks, no errors appear and everything works except for the bitmap. For some reason it doesn't display. However, I once tried replacing Bitmap.bmp with cb.bmp (the Code::Blocks logo that came with the IDE) and it rendered perfectly fine. Also, whenever I close the program Windows gives me this error: "2DGame.exe not responding", no matter what bitmap I use. This is one of the strangest coding problems I have come across ever, please tell me why Bitmap.bmp is being a jerk and how to fix it and also how to fix the error I get whenever I close my program. Thanks!

P.S: You guys asked for the image, here it is:

enter image description here

\$\endgroup\$
8
  • \$\begingroup\$ I've edited your question, and removed the part where you ask about the Code::Blocks SDL template, as that's not relevant to this particular issue you are having. The template is probably built to just quickly open a window and close immediately. Now, as for your actual question: could you supply the problematic Bitmap.bmp? It's really hard to guess what might be wrong with your particular bitmap. \$\endgroup\$
    – user35344
    Commented Jul 10, 2016 at 11:01
  • \$\begingroup\$ The Bitmap is a picture I downloaded off the internet. It is an image that I downloaded off the internet. It's roughly 250x250 in size. \$\endgroup\$ Commented Jul 11, 2016 at 16:54
  • \$\begingroup\$ I didn't ask for the size. I asked for the image. Could you supply it please? \$\endgroup\$
    – user35344
    Commented Jul 11, 2016 at 21:30
  • \$\begingroup\$ I supplied the image above \$\endgroup\$ Commented Jul 13, 2016 at 8:14
  • 2
    \$\begingroup\$ It looks like you running this using SDL 1.2? SDL_SetVideoMode was removed when moved up to SDL 2. Any reason why you're not using SDL 2? \$\endgroup\$ Commented Jul 13, 2016 at 8:29

2 Answers 2

1
\$\begingroup\$

As you haven't supplied the particular bitmap you are having trouble with, I cannot guess what is wrong with the bitmap. Your rendering code seems fine on that part.

When it comes to the program hanging, the cause is quite clear. When an SDL_QUIT event is received, you quit your loop that is waiting for that particular event, and call SDL_Quit();. However, the while loop inside your main still keeps trying to update the window. As of currently, your application can't be exit, because the while loop inside main is an infinite loop. All in all, your game loop seems a little ill-formed. You might want to try a simpler approach, where there is only one game loop, and where you use SDL_PollEvent for receiving events.

\$\endgroup\$
1
  • \$\begingroup\$ The Bitmap is a picture I downloaded off the internet. It is an image that I downloaded off the internet. It's roughly 250x250 in size. \$\endgroup\$ Commented Jul 11, 2016 at 16:54
1
\$\begingroup\$

SDL_LoadBMP can only load bitmap images (i.e. extension .bmp). Images from the internet are rarely of this format; they are usually formats like JPEG, PNG or GIF.

For that, you want to look at SDL_image instead. It's a helper library for SDL that handles loading various image types into SDL. It has a different loading function: IMG_Load.

And please, always check errors in your code. Your problem could have been caught earlier if you had checked the output of SDL_LoadBMP, which would have been null. Like most SDL functions, you can then get a description of the error using SDL_GetError.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .