Welcome! Log In Create A New Profile

Advanced

SDL Help

Posted by g_man 
SDL Help
May 15, 2009 03:17AM
This may be the wrong catagory, but i'm having a problem with SDL.(for windows, not wii)
It compiles with no errors, but when i exicute it , it pops the hourglass mouse and when i click it stops responding.
I have set up the library's and header correctly

code:
#include 
#include "SDL/SDL.h" 
#include "SDL/SDL_image.h" 
#include 
#include 

using namespace std;

//Screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//The surfaces
SDL_Surface *map1 = NULL;
SDL_Surface *start = NULL;
SDL_Surface *screen = NULL;

SDL_Event event;


 
void sleep(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}

SDL_Surface *load_image( std::string filename )
{
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized image that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image using SDL_image
    loadedImage = IMG_Load( filename.c_str() );

    //If the image loaded
    if( loadedImage != NULL )
    {
        //Create an optimized image
        optimizedImage = SDL_DisplayFormat( loadedImage );

        //Free the old image
        SDL_FreeSurface( loadedImage );
    }

    //Return the optimized image
    return optimizedImage;
}

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

    //Get offsets
    offset.x = x;
    offset.y = y;

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

bool init()
{
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;
    }

    //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 false;
    }

    //Set the window caption
    SDL_WM_SetCaption( "Battle Link", NULL );

    //If everything initialized fine
    return true;
}

void clean_up()
{
    //Free the surface
    SDL_FreeSurface( map1 );

    //Quit SDL
    SDL_Quit();
}

int main( int argc, char* args[] ) { 
    //Initialize
    if( init() == false )
    {
		cout << "There has been an error in intializing the screen. Try again.\n";
        sleep(5);
		return 1;
    }
	

	
	map1 = load_image( "map1.bmp" );
	if(!map1) {
    printf("IMG_Load: %s\n", IMG_GetError());
		cout << "There has been an error intializing the file. Try again.";
		sleep(5);
		return 1;
	}
	start = load_image( "pics/start.jpg" );
	if(!start) {
    printf("IMG_Load: %s\n", IMG_GetError());
		cout << "There has been an error intializing the file. Try again.";
		sleep(5);
		return 1;
	}

	int stoploop = 0;
	int mousex;
	int mousey;
	while (1){
	if( event.type == SDL_MOUSEBUTTONDOWN) {
		if( event.button.button == SDL_BUTTON_LEFT) {
			mousex = event.button.x;
			mousey = event.button.y;
			
			//if mouse is over the button
			if( (mousex > 50) && (mousex < 200) && (mousey > 50) && (mousey < 250)) {
				break;
			}
		}
	}		
	apply_surface(50,50,start,screen);
	//Update screen
	if( SDL_Flip( screen) == -1) {
		return 1;
	}
	}
	
	
	//apply the surfaces to the screen
	apply_surface(0,48,map1,screen);
	
	//Update screen
	if( SDL_Flip( screen) == -1) {
		return 1;
	}
	
	SDL_Delay(5000);
	
	clean_up();
	
    return 0; 
}
Thanks
Re: SDL Help
May 15, 2009 09:33AM
I am not sure if SDL does it for you but try initializing fat yourself for file access
The sleep function is ugly, use SDL_Delay sleeps in milliseconds. Or use usleep which sleeps in microseconds.

As a last resort try changing
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
to
if( SDL_Init( SDL_INIT_EVERYTHING ^ SDL_INIT_CDROM ) == -1 )
Its been fixed but i dont know which version of sdl you have.
Re: SDL Help
May 15, 2009 04:16PM
thanks that fixed some of my issues, but it still won't change screens. the only reason i was using that sleep function was because i'm used to have a sleep function from C. I have the newest version of SDL, and if you think there is a better library than tell me. The goal is to make some kind of legend of zelda battle game.
Re: SDL Help
May 15, 2009 07:14PM
It's your loop.
I changed it to:
{
get input
handle input
draw
}
works
#include 
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include 


using namespace std;

//Screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//The surfaces
SDL_Surface *map1 = NULL;
SDL_Surface *start = NULL;
SDL_Surface *screen = NULL;

SDL_Event event;



void sleep(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}

SDL_Surface *load_image( std::string filename )
{
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized image that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image using SDL_image
    loadedImage = IMG_Load( filename.c_str() );

    //If the image loaded
    if( loadedImage != NULL )
    {
        //Create an optimized image
        optimizedImage = SDL_DisplayFormat( loadedImage );

        //Free the old image
        SDL_FreeSurface( loadedImage );
    }

    //Return the optimized image
    return optimizedImage;
}

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

    //Get offsets
    offset.x = x;
    offset.y = y;

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

bool init()
{
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;
    }

    //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 false;
    }

    //Set the window caption
    SDL_WM_SetCaption( "Battle Link", NULL );

    //If everything initialized fine
    return true;
}

void clean_up()
{
    //Free the surface
    SDL_FreeSurface( map1 );

    //Quit SDL
    SDL_Quit();
}

int main( int argc, char* args[] ) {
    //Initialize
    if( init() == false )
    {
		printf("There has been an error in intializing the screen. Try again.\n");
        sleep(5);
		return 1;
    }



	map1 = load_image( "map1.bmp" );
	if(!map1) {
    printf("IMG_Load: %s\n", IMG_GetError());
		printf("There has been an error intializing the file. Try again.");
		sleep(5);
		return 1;
	}
	start = load_image( "pics/start.jpg" );
	if(!start) {
    printf("IMG_Load: %s\n", IMG_GetError());
		printf("There has been an error intializing the file. Try again.");
		sleep(5);
		return 1;
	}

	int stoploop = 0;
	int mousex;
	int mousey;
	bool quit=false;
	while (!quit)
	{
        if( SDL_PollEvent( &event ) )
        {
	       if( event.type == SDL_MOUSEBUTTONDOWN)
           {
		      if( event.button.button == SDL_BUTTON_LEFT)
                {
			         mousex = event.button.x;
			         mousey = event.button.y;
                }
           }
        }
			//if mouse is over the button
			if( (mousex > 50) && (mousex < 400) && (mousey > 50) && (mousey < 450))
            {
                SDL_WM_SetCaption( "Button Clicked, Exiting", NULL );
                quit=true;
		    }
	apply_surface(50,50,start,screen);


	//apply the surfaces to the screen
	apply_surface(0,48,map1,screen);

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

	SDL_Delay(100);
}
	clean_up();

    return 0;
}

[lazyfoo.net]
Re: SDL Help
May 16, 2009 03:40AM
Thanks your code worked, i did have to mess with the loops a bit.

EDIT:
Ok I have another problem, I think i've narrowed it down to 2 main places

Here:
SDL_Surface *load_image( string filename )
{
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized image that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image using SDL_image
    loadedImage = IMG_Load( filename.c_str() );

    //If the image loaded
    if( loadedImage != NULL )
    {
        //Create an optimized image
        optimizedImage = SDL_DisplayFormat( loadedImage );

        //Free the old image
        SDL_FreeSurface( loadedImage );
    }

    //Return the optimized image
    return optimizedImage;
}
and Here:
class Linkman 
{
	public:
	Linkman();
	~Linkman();
	void SetX(int xx) {x = xx;}
	void SetY(int yy) {y = yy;}
	int GetX() {return x;}
	int GetY() {return y;}
	void SetFace(bool b) {faceup = b;}
	bool GetFace() {return faceup;}
	void SetLink(bool b) {whichlink = b;}
	bool GetLink() {return whichlink;}
	
	private:
	int x;
	int y;
	bool faceup;
	bool whichlink;
}

My errors:
expected init-declarator before * token
expected `,' or `;' before * token
In function `int SDL_main(int, char**)';
`load_image' undeclared
invalid use of member (did you forget the '&' ?)
invalid use of member (did you forget the '&' ?)
invalid use of member (did you forget the '&' ?)
invalid use of member (did you forget the '&' ?)
257 D:\MinGW\Battle Link\main.cpp cannot convert `SDL_Rect' to `SDL_Rect*' for argument `5' to `void apply_surface(int, int, SDL_Surface*, SDL_Surface*, SDL_Rect*)' `void
I had to copy these by hand
thanks



Edited 1 time(s). Last edit at 05/16/2009 05:05AM by g_man.
Re: SDL Help
May 16, 2009 08:15PM
Are you using an IDE or just compiling text files? I only get 1 error from compiling those two code snippets.
expected unqualified-id at end of input
expected `,' or `;' at end of input
That's from your Linkman class. You need to put a semicolon after the class definition.
class Linkman 
{
	public:
	Linkman();
	~Linkman();
	void SetX(int xx) {x = xx;}
	void SetY(int yy) {y = yy;}
	int GetX() {return x;}
	int GetY() {return y;}
	void SetFace(bool b) {faceup = b;}
	bool GetFace() {return faceup;}
	void SetLink(bool b) {whichlink = b;}
	bool GetLink() {return whichlink;}
	
	private:
	int x;
	int y;
	bool faceup;
	bool whichlink;
}; // <---------- there
Re: SDL Help
May 18, 2009 05:47AM
Thanks, i'm newish to C++ so i forget the little errors, i found out that the other ones were from me forgeting to add () after my functions, LOL
I fixed all of my errors, and I finally have it running :)
Sorry, only registered users may post in this forum.

Click here to login