SDL with C++ hello world

Hi, I've been following http://lazyfoo.net/SDL_tutorials/lesson01/index2.php to create a simple C++ program, display an image file. But for me it isn't working. I have placed hello.bmp in the correct directory and haev copied the code exactly, but the image does not display when I run it. I set it up to record error messages, and stdout.txt read "SDL_Init failed: SDL_UpperBlit: passed a NULL surface". Please can anyone tell my what I am doing wrong?
Post the code.
The code was in the link I gave, but here it is anyway:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <SDL/SDL.h>

using namespace std;

int main(int argc, char *argv[])
{
    SDL_Surface *hello;
    SDL_Surface *screen;
    SDL_Init(SDL_INIT_EVERYTHING);
    screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
    hello = SDL_LoadBMP("hello.bmp");
    SDL_BlitSurface(hello, NULL, screen, NULL);
    SDL_Flip(screen);
    SDL_Delay(2000);
    SDL_FreeSurface(hello);
    printf("SDL_Init failed: %s\n", SDL_GetError());
    SDL_Quit();
    return 0;
}
You must have mis-copied something. I downloaded the source (at the bottom of the page):

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
/*This source code copyrighted by Lazy Foo' Productions (2004-2009) and may not
be redestributed without written permission.*/

//Include SDL functions and datatypes
#include "SDL/SDL.h"

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

    //Start SDL
    SDL_Init( SDL_INIT_EVERYTHING );

    //Set up screen
    screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );

    //Load image
    hello = SDL_LoadBMP( "hello.bmp" );

    //Apply image to screen
    SDL_BlitSurface( hello, NULL, screen, NULL );

    //Update Screen
    SDL_Flip( screen );

    //Pause
    SDL_Delay( 2000 );

    //Free the loaded image
    SDL_FreeSurface( hello );

    //Quit SDL
    SDL_Quit();

    return 0;
}


Although, I recommend using SFML ( http://www.sfml-dev.org/ ) Much better documentation, much easier to use, and much faster.

EDIT: Took out the closing parenthesis from the link
Last edited on
I think it might of been a mistake on my side when I compiled it using "Compile", then "Run" because I clicked "Compile and Run" and it worked fine! Thankyou for helping anyway :)
Topic archived. No new replies allowed.