SDL

A quick flash to the screen!

Page 1 of 1

6 Replies - 1275 Views - Last Post: 24 November 2009 - 01:02 PM Rate Topic: -----

#1 chewii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 19-June 09

SDL

Posted 21 November 2009 - 06:56 AM

Hi there guys,

I'm very new to SDL and game programming in general and I'm having a bit of trouble.
My program compiles ok, but when it comes to running it, my window just flashes up for a second.

I have absolutely no idea where to start as I have read a few solutions, but only to find they have not worked for me.


class Initialization
{
	public:
		int initializeSDL();
		int loadFiles();
};

int Initialization::initializeSDL()
{
	if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
		return 1;

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

	if( screen = NULL )
		return 1;

	SDL_WM_SetCaption( "Game", NULL );
}

int Initialization::loadFiles()
{
	character = SDL_LoadBMP( "dot.bmp" );
	if(character == NULL )
		return 1;
}

void cleanUp()
{
	SDL_FreeSurface( character );
	SDL_FreeSurface( screen );

	SDL_Quit();
}

int main( int argc, char* args[] )
{
	Initialization init;
	init.initializeSDL();
	init.loadFiles();

	SDL_BlitSurface( character, NULL, screen, NULL );
	SDL_Flip( screen );

	SDL_Delay( 3000 );
	cleanUp();
	return 0;
}




As you can see I'm just having some practice with some classes in SDL too.

Both screen and character Surfaces have been declared as:


SDL_Surface* character = NULL;
SDL_Surface* screen = NULL;




I apologise for posting all of my code - I'm just really stumped.

Thanks in advance!

chewii.

Is This A Good Question/Topic? 0
  • +

Replies To: SDL

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: SDL

Posted 21 November 2009 - 01:03 PM

Hi

You most likely need to split up where you blit to the screen and flip into a separate function, call it display or something like that.

void Display()
{
	  SDL_BlitSurface( character, NULL, screen, NULL );

	  SDL_Flip( screen );
}



Also you will want to have a game loop that runs until the game quits, if you look at your main function it only blits to the screen once and then cleans everything up.

make a bool called running.
bool running;

//in your init function
running = true;

while(running = true)
{
	display();
	// update();
	// other stuff
}



then you can set a keyboard variable that calls a quit function, this can simply set running to false and then call your cleanup function

void Quit()
{
	running = false;
	cleanup();
}



Hope that helps :^:
Was This Post Helpful? 1
  • +
  • -

#3 chewii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 19-June 09

Re: SDL

Posted 21 November 2009 - 04:07 PM

Thanks for your reply!

Works great. Really appreciate it.

Chewii.
Was This Post Helpful? 0
  • +
  • -

#4 Hellbroth  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 190
  • Joined: 15-August 09

Re: SDL

Posted 21 November 2009 - 09:03 PM

If you like SDL check this website :

http://lazyfoo.net/S...rials/index.php


It's honestly realy good to learn almost everything about SDL and even many concepts of game programming.
Was This Post Helpful? 0
  • +
  • -

#5 chewii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 19-June 09

Re: SDL

Posted 24 November 2009 - 03:58 AM

http://lazyfoo.net/S...rials/index.php

That's where I have been learning! I instantly knew it was a good source.

Thanks again.

Chewii.
Was This Post Helpful? 0
  • +
  • -

#6 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: SDL

Posted 24 November 2009 - 04:20 AM

That site is best used as a reference when there is something you don't understand, it does not teach you how to program games using any kind of OOP or reusable code.

Check out my tutorials if you want to get started on a more reusable code path http://www.dreaminco...topic110460.htm
Was This Post Helpful? 0
  • +
  • -

#7 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 782
  • View blog
  • Posts: 7,610
  • Joined: 05-May 05

Re: SDL

Posted 24 November 2009 - 01:02 PM

That's a good point.

I was deciding between my next issue of Linux for Windows Game Programmers being about sound mixing in SDL (playing multiple sounds engine) or about writing re-usable code in C (using the Linux library model really helps). I might still do the sound mixing since it is overlooked often.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1