Jump to content
  • Advertisement
Sign in to follow this  
bronxbomber92

SDL images

This topic is 4205 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I just started porting my own 2D game from a different language, and I decided to use SDL for simplicity, then maybe change it over using OpenGL another time. But right now, the images won't load.... I just get a black screen... Can anyone help? Thanks
#include "SDL.h"
#include "SDL_image/SDL_image.h"
#include <OpenGL/gl.h>
#include <math.h>
#include <stdio.h>	
#include <stdlib.h>
#include <strings.h>

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

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

//The event structure
SDL_Event event;

/*class PLAYER { 
public:*/
	SDL_Surface *imageLeft = NULL;
	SDL_Surface *imageRight = NULL;
	int x = 220;
	int y = 10;
//}; 

//PLAYER player;

SDL_Surface *load_image( char *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 );
    
    //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 )
{
    //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 );
}
	
	
/*void InitGL();
void Display();

void InitGL() {
	//glClearColor( 0, 0, 0, 0 );
	
	glEnable(GL_TEXTURE_2D);						// Enable Texture Mapping ( NEW )
	glShadeModel(GL_SMOOTH);						// Enable Smooth Shading
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);					// Black Background
	glClearDepth(1.0f);							// Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);						// Enables Depth Testing
	glEnable (GL_LIGHTING); //enable the lighting
	glEnable (GL_LIGHT0); //enable LIGHT0, our Diffuse Light
	glDepthFunc(GL_LEQUAL);							// The Type Of Depth Testing To Do
	//glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	
    
}

void Display() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);			// Clear Screen And Depth Buffer
	glLoadIdentity();							// Reset The Current Matrix
	glTranslatef(0.0f,0.0f,-5.0f);						// Move Into The Screen 5 Units
	glPushMatrix();

	

	
	SDL_GL_SwapBuffers();
}	*/
   
int main(int argc, char *argv[])
{
    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 in error in setting up the screen
    if( screen == NULL )
    {
        return 1;      //Set the window caption
	}
		
	SDL_WM_SetCaption( "Super Monkey Poop Fight", NULL ); 
	
	// Load Images
	/*player.*/imageLeft = load_image( "thing.app/Contents/Resources/images/monkey1standl.png" );
	/*player.*/imageRight = load_image( "thing.app/Contents/Resources/images/monkey1standr.png" );
    background = load_image( "thing.app/Contents/Resources/images/background.png" );
	
	while(1) {
		
		//If there's an event to handle
        if( SDL_PollEvent( &event ) )
        {
            //If a key was pressed
            if( event.type == SDL_KEYDOWN )
            {
				//Set the proper text
                switch( event.key.keysym.sym )
                {
                    case SDLK_LEFT:
						/*player.*/x -= 4;
						apply_surface( /*player.*/x, /*player.*/y, /*player.*/imageLeft, screen );
						break;
                    case SDLK_RIGHT:
						/*player.*/x += 4;
						apply_surface( /*player.*/x, /*player.*/y, /*player.*/imageRight, screen );
						break; 
					case SDLK_q:
						//Free the image
						SDL_FreeSurface( imageLeft );
						SDL_FreeSurface( imageRight );
						SDL_FreeSurface( background );
						
						//Quit SDL
						SDL_Quit(); 	
						break;
                }
            }
            
        }
		
		//Apply the background to the screen
		apply_surface( 0, 0, background, screen );

		SDL_Flip( screen );
	}
	
	return 0;
	
}
edit - It may look messy because I commented out the OpenGL stuff, and the class... Sorry

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by bronxbomber92
Hey, I just started porting my own 2D game from a different language, and I decided to use SDL for simplicity, then maybe change it over using OpenGL another time. But right now, the images won't load.... I just get a black screen... Can anyone help? Thanks

#include "SDL.h"
#include "SDL_image/SDL_image.h"
#include <OpenGL/gl.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

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

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

//The event structure
SDL_Event event;

/*class PLAYER {
public:*/

SDL_Surface *imageLeft = NULL;
SDL_Surface *imageRight = NULL;
int x = 220;
int y = 10;
//};

//PLAYER player;

SDL_Surface *load_image( char *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 );

//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 )
{
//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 );
}


/*void InitGL();
void Display();

void InitGL() {
//glClearColor( 0, 0, 0, 0 );

glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glEnable (GL_LIGHTING); //enable the lighting
glEnable (GL_LIGHT0); //enable LIGHT0, our Diffuse Light
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
//glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

}

void Display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Matrix
glTranslatef(0.0f,0.0f,-5.0f); // Move Into The Screen 5 Units
glPushMatrix();




SDL_GL_SwapBuffers();
} */


int main(int argc, char *argv[])
{
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 in error in setting up the screen
if( screen == NULL )
{
return 1; //Set the window caption
}

SDL_WM_SetCaption( "Super Monkey Poop Fight", NULL );

// Load Images
/*player.*/imageLeft = load_image( "thing.app/Contents/Resources/images/monkey1standl.png" );
/*player.*/imageRight = load_image( "thing.app/Contents/Resources/images/monkey1standr.png" );
background = load_image( "thing.app/Contents/Resources/images/background.png" );

while(1) {

//If there's an event to handle
if( SDL_PollEvent( &event ) )
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Set the proper text
switch( event.key.keysym.sym )
{
case SDLK_LEFT:
/*player.*/x -= 4;
apply_surface( /*player.*/x, /*player.*/y, /*player.*/imageLeft, screen );
break;
case SDLK_RIGHT:
/*player.*/x += 4;
apply_surface( /*player.*/x, /*player.*/y, /*player.*/imageRight, screen );
break;
case SDLK_q:
//Free the image
SDL_FreeSurface( imageLeft );
SDL_FreeSurface( imageRight );
SDL_FreeSurface( background );

//Quit SDL
SDL_Quit();
break;
}
}

}

//Apply the background to the screen
apply_surface( 0, 0, background, screen );

SDL_Flip( screen );
}

return 0;

}




Here it is using the "source" tags instead of "code" tags. Just a note: when you are ready to switch to OpenGL you'll need to switch all of your blit functions from SDL to OpenGL also. It is good that you are using the apply_surface() function.

It looks rather odd that your quit statement tries to close SDL from inside the while loop. Remember that your break instruction only breaks from the switch statement not from the loop. You should put a boolean flag in your while statement so it will stop looping when you quit.

I'm not sure if I helped you any but try to make your indents line up so you can tell what's inside of what. Another thing you might try is to put fprintf(stderr, "error message"); statements in your quit areas so you'll have a record of what caused your program to quit and why it didn't close the screen when it quit. Note that stderr is redirected by SDL to a file called stderr.log or something similar so your program will have to have quit before you can read the error message.

Share this post


Link to post
Share on other sites
Hi, is the problem the image won't load, or it does load but don't show up when you blit it to the screen?

If you cannot load the image (i.e. load_image() returns NULL) try these things:

Check and make sure the paths are correct.
Try absolute paths.
Try "//dir1//dir2//image.png" instead of "/dir1/dir2/image.png" .

If the image loads correctly but does not show up on screen when you blit them try these things:

Re:SDL_PollEvent Question and SDL event loop
Change
if( SDL_PollEvent( &event ) ) to while( SDL_PollEvent( &event ) )

Re:SDL Event Frenzy and Handling Input in SDL
Do you best not to handle events in the event loop.

Make sure the background pointer is not NULL.


Good Luck

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Try replacing
#include "SDL_image/SDL_image.h"

by
#include


Hope it will help

Share this post


Link to post
Share on other sites
Try using the string returned by IMG_GetError() to see why IMG_Load() returns NULL, if that is the problem.

If that turns out not to be the problem, check the return value of SDL_BlitSurface. It should be >= 0 if no error occured (IIRC)

Share this post


Link to post
Share on other sites
Quote:
Original post by silverphyre673
Are the dimensions of your textures multiples of two? If they aren't, they won't get displayed.


This is SDL using software blitting; power-of-two doesn't matter for SDL, only OpenGL/DirectX/other hardware graphics things.

Oh wait, you said 'multiples of two'? I don't remember anything about that with SDL (or anything else for that matter), so I'm assuming that you meant 'powers of two.'

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!