Sign in to follow this  
NUCLEAR RABBIT

Beginning SDL Problem

Recommended Posts

NUCLEAR RABBIT    318
Hello, I tried making something with SDL, Im getting no errors, but when I run the program, a window pops up and just goes away and it doesnt show the image. Can anyone help me find what I am doing wrong?
#include "SDL/SDL.h"
#include <string>

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

SDL_Surface * screen = NULL;
SDL_Surface * message = NULL;
SDL_Surface * background = NULL;

void CleanUp();
bool Init();
bool LoadFiles();
SDL_Surface * LoadImage(const std::string & file_name);
void ApplyImage(int x, int y, SDL_Surface * image, SDL_Surface * screen);

int main( int argc, char* args[] )
{
	if(Init() == false)
	{
		return 1;
	}

	if(LoadFiles() == false)
	{
		return 1;
	}

	ApplyImage(0,0,background,screen);
	ApplyImage(0,0,message,screen);

	if(SDL_Flip(screen) == -1)
	{
		return 1;
	}

	CleanUp();

	return 0;
}

//____________________________________________________________________________________________________________________

// Function Definitions

void CleanUp()
{
	SDL_Delay(5000);
	SDL_FreeSurface(background);
	SDL_FreeSurface(message);
	SDL_Quit();
}

bool Init()
{
	if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
	{
		return false;
	}

	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE);

	if(screen == NULL)
	{
		return false;
	}

	SDL_WM_SetCaption("ADDING AN IMAGE TO THE SCREEN!", NULL);

	return true;
}

bool LoadFiles()
{
	background = LoadImage("background.bmp");
	message = LoadImage("hello_world.bmp");

	if (background == NULL || message == NULL)
	{
		return false;
	}

	return true;
}

SDL_Surface * LoadImage(const std::string & file_name)
{
	SDL_Surface * loaded_image = NULL;
	SDL_Surface * opt_image = NULL;

	loaded_image = SDL_LoadBMP(file_name.c_str());

	if(loaded_image != NULL)
	{
		opt_image = SDL_DisplayFormat(loaded_image);

		SDL_FreeSurface(loaded_image);
	}

	return opt_image;
}

void ApplyImage(int x, int y, SDL_Surface * image, SDL_Surface * screen)
{
	SDL_Rect offset;

	offset.x = x;
	offset.y = y;

	SDL_BlitSurface(image, NULL, screen, &offset);
}

Share this post


Link to post
Share on other sites
deadstar    536
Quote:
Original post by NUCLEAR RABBIT
Hello,

I tried making something with SDL, Im getting no errors, but when I run the program, a window pops up and just goes away and it doesnt show the image. Can anyone help me find what I am doing wrong?

*** Source Snippet Removed ***


That's what your code does - inits SDL, loads the files, draws the images, flips the screen then calls cleanup and finishes :)

You need a 'main loop'.

Share this post


Link to post
Share on other sites
monkey4sale    127
lol, you're lucky, I can compile just fine, but when i try to execute the program nothing happens....

#include <SDL\SDL.h>
#include <string>

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

SDL_Surface *message = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
//Make a temporary rectangle to hold the offsets
SDL_Rect offset;

//Give the offsets to the rectangle
offset.x = x;
offset.y = y;

//Blit the surface
SDL_BlitSurface( source, NULL, destination, &offset );
}

SDL_Surface *load_image( std::string filename )
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;

loadedImage = SDL_LoadBMP( filename.c_str() );

if( loadedImage != NULL )
{
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
}
return optimizedImage;
}


int main( int argc, char* args[] )
{
//Start SDL
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1);
{
return 1;
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( screen == NULL )
{
return 1;
}
//Set the window caption
SDL_WM_SetCaption( "Hello World", NULL );
//Load the images
message = load_image( "hello_world.bmp" );
background = load_image( "background.bmp" );
//Apply the background to the screen
apply_surface( 0, 0, background, screen );
//Apply the message to the screen
apply_surface( 180, 140, message, screen );
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
//Wait 20 seconds
SDL_Delay( 20000 );
//Free the surfaces
SDL_FreeSurface( message );
SDL_FreeSurface( background );

//Quit SDL
SDL_Quit();

//Return
return 0;
}

Share this post


Link to post
Share on other sites
NUCLEAR RABBIT    318
Quote:
Original post by deadstar
Quote:
Original post by NUCLEAR RABBIT
Hello,

I tried making something with SDL, Im getting no errors, but when I run the program, a window pops up and just goes away and it doesnt show the image. Can anyone help me find what I am doing wrong?

*** Source Snippet Removed ***


That's what your code does - inits SDL, loads the files, draws the images, flips the screen then calls cleanup and finishes :)

You need a 'main loop'.


haha yah, but in the CleanUp(), i put a delay function, which delays the window for 5 seconds so I can see the images before it closes.

(it only stays up for like a split second)

Share this post


Link to post
Share on other sites
deadstar    536
Ah I did notice after I posted.

I wouldn't rely on that SDL_delay() too much.

Best advice I can give is to output error messages in all your error checking. Use 'cout' or 'printf', or if you're using Windows then include windows.h and use a message box:


MessageBox(NULL, "Error initialising SDL", "Error", MB_OK);



And that Main Loop still comes highly recommended.

Share this post


Link to post
Share on other sites
NUCLEAR RABBIT    318
Quote:
Original post by deadstar

And that Main Loop still comes highly recommended.


yah, i know. im just trying to get an image on the screen for right now [smile]

ill try the messagebox to find the error :D

thanx!

Share this post


Link to post
Share on other sites
NUCLEAR RABBIT    318
one problem with the message box, i get this error when i put that in:

1>c:\users\owner\documents\visual studio 2008\projects\sdl lesson 2\sdl lesson 2\main.cpp(68) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [5]' to 'LPCWSTR'


if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
MessageBox(NULL, "INIT", "ERROR", MB_OK);
return false;
}


(I added <windows.h>)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this