11 Replies - 847 Views - Last Post: 22 January 2013 - 01:32 PM Rate Topic: -----

#1 CountBale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 22-January 13

Images dont display in SDL, program returns 0

Posted 22 January 2013 - 04:58 AM

No image functions that I use in SDL seem to work, the program runs and ends without an error and none of the checks I put into my code show up anything (my STDout file is empty).

The simplest example is this section in my initialization function:

#include "SDLinit.h"


bool SDLinit()
{

    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;
    }
    screenSurface = SDL_SetVideoMode( screenWidth, screenHeight, screenBPP, SDL_SWSURFACE );
    if( screenSurface == NULL )
    {
        return false;
    }

    SDL_WM_SetCaption( "Game", NULL );

    SDL_Surface* icon = SDL_LoadBMP("icon.bmp");
    SDL_WM_SetIcon(icon, NULL);

    return true;
}



But none of my other functions work either. If necessary I can try uploading the source code but I have a fair bit of code over several files and I dont want to just post a huge wall of text. Can anyone help me out here? I have included the files in the same folder as my .exe and they are definitely the right file type.

Is This A Good Question/Topic? 0
  • +

Replies To: Images dont display in SDL, program returns 0

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1000
  • View blog
  • Posts: 4,181
  • Joined: 14-February 08

Re: Images dont display in SDL, program returns 0

Posted 22 January 2013 - 05:01 AM

Well, you are loading the image but you are not displaying it. You will not see the image unless you blit it as a surface.
Was This Post Helpful? 0
  • +
  • -

#3 CountBale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 22-January 13

Re: Images dont display in SDL, program returns 0

Posted 22 January 2013 - 05:08 AM

View Poststayscrisp, on 22 January 2013 - 05:01 AM, said:

Well, you are loading the image but you are not displaying it. You will not see the image unless you blit it as a surface.


I'm not sure about the SDL_WM_SetIcon command but my understanding was that it just set the window icon? I may be wrong about that but either way I have blitted my images in other functions using this blitting function:

#include "applySurface.h"

void applySurface( SDL_Surface* sourceImage, SDL_Surface* destination, int x, int y )
{
    SDL_Rect position;
    position.x = x;
    position.y = y;
    SDL_BlitSurface(sourceImage, NULL, destination, &position );
}



If it helps I am using a college computer where I dont have admin privileges if that could have an effect.
Was This Post Helpful? 0
  • +
  • -

#4 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1000
  • View blog
  • Posts: 4,181
  • Joined: 14-February 08

Re: Images dont display in SDL, program returns 0

Posted 22 January 2013 - 05:13 AM

If you wish to use the image as an icon you must call this before you call SDL_SetVideoMode. Also if you are using windows, the icon must be 32x32.
Was This Post Helpful? 1
  • +
  • -

#5 CountBale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 22-January 13

Re: Images dont display in SDL, program returns 0

Posted 22 January 2013 - 05:20 AM

View Poststayscrisp, on 22 January 2013 - 05:13 AM, said:

If you wish to use the image as an icon you must call this before you call SDL_SetVideoMode. Also if you are using windows, the icon must be 32x32.

Ah okay, it was 24x24 so I will fix that. Any idea why my other files dont display? They are all in .png format so I have used IMG_Load with SDL_Image, I load them all using these functions:

loadImage:
#include "loadImage.h"

SDL_Surface *loadImage( std::string filename )
{
    SDL_Surface* loadedImage = NULL;
    SDL_Surface* optimizedImage = NULL;
    loadedImage = IMG_Load( filename.c_str() );
    if( loadedImage != NULL )
    {
        optimizedImage = SDL_DisplayFormat(loadedImage);
        SDL_FreeSurface( loadedImage );
    }

    return optimizedImage;
}


and
loadFiles:

#include "loadFiles.h"

extern SDL_Surface *newGameImage;
extern SDL_Surface *loadGameImage;
extern SDL_Surface *settingsImage;
extern SDL_Surface *highScoresImage;
extern SDL_Surface *quitGameImage;
extern SDL_Surface *menuBackgroundImage;

extern TTF_Font *font;

extern Mix_Music *menuMusic;
extern Mix_Music *franceMusic;
extern Mix_Music *germanyMusic;
extern Mix_Music *russiaMusic;
extern Mix_Music *turkeyMusic;
extern Mix_Music *indiaMusic;
extern Mix_Music *chinaMusic;

extern Mix_Chunk *playerDeath;
extern Mix_Chunk *trainWhistle;
extern Mix_Chunk *buttonPress;
extern Mix_Chunk *eventSound;

bool loadFiles()
{
    //Load the menu images
    newGameImage = loadImage("newgame.png");
    if (newGameImage == NULL)
    {
        printf("File newGame failed to load");
        return false;
    }
    loadGameImage = loadImage("loadgame.png");
    if (loadGameImage == NULL)
    {
        printf("File loadGame failed to load");
        return false;
    }
    settingsImage = loadImage("settings.png");
    if (settingsImage == NULL)
    {
        printf("File settings failed to load");
        return false;
    }
    highScoresImage = loadImage("highscores.png");
    if (highScoresImage == NULL)
    {
        printf("File highScores failed to load");
        return false;
    }
    quitGameImage = loadImage("quitgame.png");
    if (quitGameImage == NULL)
    {
        printf("File quitGame failed to load");
        return false;
    }
    menuBackgroundImage = loadImage("background.png");
    if (menuBackgroundImage == NULL)
    {
        printf("File menuBackground failed to load");
        return false;
    }

    //Load the font
    font = TTF_OpenFont( "ElsiSwashCaps.ttf", 12 );
    if (font == NULL)
    {
        printf("File font failed to load");
        return false;
    }

    //Load the music
    menuMusic = Mix_LoadMUS( "menuMusic.wav" );
    if (menuMusic == NULL)
    {
        printf("File menuMusic failed to load");
        return false;
    }
    franceMusic = Mix_LoadMUS( "franceMusic.wav" );
    if (franceMusic == NULL)
    {
        printf("File franceMusic failed to load");
        return false;
    }
    germanyMusic = Mix_LoadMUS( "germanyMusic.wav" );
    if (germanyMusic == NULL)
    {
        printf("File germanyMusic failed to load");
        return false;
    }
    russiaMusic = Mix_LoadMUS( "russiaMusic.wav" );
    if (russiaMusic == NULL)
    {
        printf("File russiaMusic failed to load");
        return false;
    }
    turkeyMusic = Mix_LoadMUS( "turkeyMusic.wav" );
    if (turkeyMusic == NULL)
    {
        printf("File turkeyMusic failed to load");
        return false;
    }
    indiaMusic = Mix_LoadMUS( "indiaMusic.wav" );
    if (indiaMusic == NULL)
    {
        printf("File indiaMusic failed to load");
        return false;
    }
    chinaMusic = Mix_LoadMUS( "chinaMusic.wav" );
    if (chinaMusic == NULL)
    {
        printf("File chinaMusic failed to load");
        return false;
    }

    //Load the sound effects
    playerDeath = Mix_LoadWAV( "playerDeath.wav" );
    if (playerDeath == NULL)
    {
        printf("File playerDeath failed to load");
        return false;
    }
    trainWhistle = Mix_LoadWAV( "trainWhistle.wav" );
    if (trainWhistle == NULL)
    {
        printf("File trainWhistle failed to load");
        return false;
    }
    buttonPress = Mix_LoadWAV( "buttonPress.wav" );
    if (buttonPress== NULL)
    {
        printf("File buttonPress failed to load");
        return false;
    }
    eventSound = Mix_LoadWAV( "eventSound.wav" );
    if (eventSound == NULL)
    {
        printf("File eventSound failed to load");
        return false;
    }

    return true;
}


Was This Post Helpful? 0
  • +
  • -

#6 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1000
  • View blog
  • Posts: 4,181
  • Joined: 14-February 08

Re: Images dont display in SDL, program returns 0

Posted 22 January 2013 - 05:25 AM

Please post your drawing code and where you call it.
Was This Post Helpful? 0
  • +
  • -

#7 CountBale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 22-January 13

Re: Images dont display in SDL, program returns 0

Posted 22 January 2013 - 05:30 AM

View Poststayscrisp, on 22 January 2013 - 05:25 AM, said:

Please post your drawing code and where you call it.


Okay, this might be quite a long post but here goes...

drawing code:

#include "applySurface.h"

void applySurface( SDL_Surface* sourceImage, SDL_Surface* destination, int x, int y )
{
    SDL_Rect position;
    position.x = x;
    position.y = y;
    SDL_BlitSurface(sourceImage, NULL, destination, &position );
}



only place I have called it so far:

#include "menu.h"

extern SDL_Surface *screenSurface;
extern SDL_Surface *newGameImage;
extern SDL_Surface *loadGameImage;
extern SDL_Surface *settingsImage;
extern SDL_Surface *highScoresImage;
extern SDL_Surface *quitGameImage;
extern SDL_Surface *menuBackgroundImage;
extern SDL_Surface *menuBackgroundImage;

extern const int screenWidth;
extern const int screenHeight;
extern const int buttonWidth;
extern const int buttonHeight;

int menuDisplay()
{
    SDL_Surface* newGameButton = NULL;
    SDL_Surface* loadGameButton = NULL;
    SDL_Surface* settingsButton = NULL;
    SDL_Surface* highScoresButton = NULL;
    SDL_Surface* quitGameButton = NULL;
    SDL_Surface* menuBackground = NULL;

    int buttonX = ((screenWidth/2)-(buttonWidth/2));

    Button newGame(buttonX, 68, buttonHeight, buttonWidth, newGameImage);
    Button loadGame(buttonX, 218, buttonHeight, buttonWidth, loadGameImage);
    Button settings(buttonX, 368, buttonHeight, buttonWidth, settingsImage);
    Button highScores(buttonX, 518, buttonHeight, buttonWidth, highScoresImage);
    Button quitGame(buttonX, 668, buttonHeight, buttonWidth, quitGameImage);

    applySurface(menuBackgroundImage, menuBackground, 0, 0);
    applySurface(newGameImage, newGameButton, buttonX, 68);
    applySurface(loadGameImage, loadGameButton, buttonX, 218);
    applySurface(settingsImage, settingsButton, buttonX, 368);
    applySurface(highScoresImage, highScoresButton, buttonX, 518);
    applySurface(quitGameImage, quitGameButton, buttonX, 668);

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



    return 0;
}




and just for good measure, my button class:

#ifndef button_h
#define button_h
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>

class Button
{
    int x, y, h, w;
    SDL_Surface *buttonImage;
    public:
    Button( int x, int y, int h, int w, SDL_Surface *buttonImage);
    bool isPressed(SDL_Event *event);


};

#endif



Oh, also I changed my icon to 32x32 but it still doesn't display.
Was This Post Helpful? 0
  • +
  • -

#8 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1000
  • View blog
  • Posts: 4,181
  • Joined: 14-February 08

Re: Images dont display in SDL, program returns 0

Posted 22 January 2013 - 06:12 AM

Just looking through your code now.

Quote

Oh, also I changed my icon to 32x32 but it still doesn't display.


Have you moved it to before you call SDL_SetVideoMode?

Do you have a loop to display your objects because if you are clearing the window as you should be then they will be removed and then never displayed again. I'm guessing you don't as your loading of objects is in the same menuDisplay function as your drawing. You cannot do that every frame.

Also, your globals are making my eyes bleed :(
Was This Post Helpful? 0
  • +
  • -

#9 CountBale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 22-January 13

Re: Images dont display in SDL, program returns 0

Posted 22 January 2013 - 06:28 AM

View Poststayscrisp, on 22 January 2013 - 06:12 AM, said:

Quote

Oh, also I changed my icon to 32x32 but it still doesn't display.


Have you moved it to before you call SDL_SetVideoMode?

Yep.

Quote

Do you have a loop to display your objects because if you are clearing the window as you should be then they will be removed and then never displayed again. I'm guessing you don't as your loading of objects is in the same menuDisplay function as your drawing. You cannot do that every frame.


I'm not sure what you mean, the files are loaded before the program even enters the main loop, none of them load in the menuDisplay function. I think I may have found the issue though, the surfaces I was blitting to weren't ever being displayed. Doesn't solve the icon problem though.

Quote

Also, your globals are making my eyes bleed :(/>


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

#10 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1000
  • View blog
  • Posts: 4,181
  • Joined: 14-February 08

Re: Images dont display in SDL, program returns 0

Posted 22 January 2013 - 06:37 AM

I'm going to recommend that you look at my tutorials, they are linked in my signature.
Was This Post Helpful? 0
  • +
  • -

#11 CountBale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 22-January 13

Re: Images dont display in SDL, program returns 0

Posted 22 January 2013 - 11:04 AM

I managed to fix the issue, thank you for your help anyway though.
Was This Post Helpful? 0
  • +
  • -

#12 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1000
  • View blog
  • Posts: 4,181
  • Joined: 14-February 08

Re: Images dont display in SDL, program returns 0

Posted 22 January 2013 - 01:32 PM

Nice one. I still think you should read the tutorials :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1