Programm works only with breakpoint.

Hello,

i'm pretty new to c++ and tried to play around a bit with SDL, but i got already stuck on my first application.

First of all, my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <stdio.h>
#include <string>
#include "SDL/SDL.h"
using namespace std;

void handle_sdl_error(string message) {
  printf("%s: %s\n", message.c_str(), SDL_GetError());
  getchar();
  SDL_Quit();
}

int main(int argc, char* args[]) {
  SDL_Surface* hello  = NULL;
  SDL_Surface* screen = NULL;
  SDL_Surface* image  = NULL;

  SDL_Init(SDL_INIT_EVERYTHING);

  screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);

  // Between here and ...
  if (screen == NULL) {
    handle_sdl_error("Could not set up screen");
    return -1;
  }

  hello  = SDL_LoadBMP("hello.bmp");
  if(hello == NULL) {
    handle_sdl_error("Could not load \"hello.bmp\" because");
    return -1;
  }

  image = SDL_DisplayFormat(hello);
  if(image == NULL) {
    handle_sdl_error("Could not load \"hello.bmp\" because");
    return -1;
  }
  SDL_FreeSurface(hello);

  SDL_BlitSurface(image, NULL, screen, NULL);

  SDL_Flip(screen);
  // ... and here

  cout << "Press Enter to quit." << endl;
  cin.ignore(80, '\n');

  SDL_FreeSurface(image);

  SDL_Quit();

  return 0;
}


If i just compile and run it, it just shows a black window.

But when i run it with gdb and set a breakpoint somewhere in the section i marked with my comments, no matter where, it will work and show me the loaded Bitmap.

To clarify this: I don't do anything in gdb, except setting the breakpoint, run the programm, continue on breakpoint, tada: BMP is shown.

Now i'm pretty curiuos how and why such a behavior is possible. Maybe some here can explain that to me. If you need any informations about my Environment, please ask.
Last edited on
SDL_Flip documentation wrote:
The SDL_DOUBLEBUF flag must have been passed to SDL_SetVideoMode, when setting the video mode for this function to perform hardware flipping.


http://sdl.beuc.net/sdl.wiki/SDL_UpdateRect
SDL_Flip documentation also wrote:
On hardware that doesn't support double-buffering or if SDL_SWSURFACE was set, this is equivalent to calling SDL_UpdateRect(screen, 0, 0, 0, 0)


But anyways, i also tried to replace the call to SDL_Flip with an call to SDL_UpdateRect, but that doesn't changed anything.


1
2
3
4
5
  [...]
  screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
  [...]
  SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);
  [...]


1
2
3
4
5
  [...]
  screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
  [...]
  SDL_Flip(screen);
  [...]


In both cases the behavior is the same as before, black screen without breakpoint, working as expected when setting a breakpoint.
Welp, I'm stumped.
Topic archived. No new replies allowed.