Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


SDL very basic code


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Raman Gupta   Members   -  Reputation: 100

Like
0Likes
Like

Posted 18 March 2012 - 09:41 AM

Hi! everybody.
The code below should create a window with image.bmp as its background image.But when I run it creates a window without the image.I tried everything,my image is in the same folder of my project and main.cpp. The code is exactly same from a youtube tutorial and works fine for many people.Please tell what's wrong in this.
Thanks in advance.

#include<SDL/SDL.h>

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

bool gamerunning = true;
SDL_Event event;

void apply_surface(int x,int y,SDL_Surface *source,SDL_Surface *destination){
SDL_Rect rect;
rect.x=x;
rect.y=y;
SDL_BlitSurface(source,NULL,destination,&rect);
}
int main(int argc,char *argv[]){

SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption("Apply Images",NULL);

screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
background=SDL_LoadBMP("image.bmp");




//Game Loop
while(gamerunning){
if(SDL_PollEvent(&event)){
if(event.type==SDL_QUIT)
gamerunning=false;
}
//Apply surface
apply_surface(0,0,background,screen);

//Update Screen
SDL_Flip(screen);
}




SDL_Quit();
return 0;
}

Sponsor:

#2 rip-off   Moderators   -  Reputation: 8515

Like
0Likes
Like

Posted 18 March 2012 - 11:50 AM

There are a few things you can try. Easiest is to check if the image has been correctly loaded:
background = SDL_LoadBMP("image.bmp");
if(!background)
{
    // Write the error somewhere.
    // In C (requires <stdio.h>)
    printf("Failed to load image.bmp: %s\n", SDL_GetError());
    // In C++ (requires <iostream>)
    std::cout << "Failed to load image.bmp: " << SDL_GetError() << '\n';    
}
By implementing this and looking at the output, you should know if the file loaded correctly. If not, consider hard coding a full path to the file (remember to correctly escape the path if using windows back slashes). Or download a bitmap file you know is correct. It is unlikely - but your file might be corrupt. It might be a 32 bit bitmap, which has patchy support. It might look like "image.bmp" in Windows Explorer, but unless you've set the option to always show file extensions, the file might actually be called image.bmp.bmp (or even image.bmp.png).

You should add such error checking to all SDL functions, including SDL_Init() and SDL_SetVideoMode(). Even SDL_BlitSurface() can return an error. Better to be aware of these errors when they occur ather than spending time chasing phantom bugs later in your program.

If it is loading correctly, but no displaying, you should note that your rendering logic is tied to your input loop. That is, at least one "event" must occur before the screen will be drawn. That said, I would expect a SDL_VIDEOEXPOSE event to be delivered to your application during the first loop, so this shouldn't be an issue.

#3 Raman Gupta   Members   -  Reputation: 100

Like
0Likes
Like

Posted 18 March 2012 - 09:20 PM

@rip off
I have put the error check you have given but still nothing is displayed.The image that I am trying to load is created by me in paint,I have just filled the background with a colour in paint and saved it with the name image.bmp .
I didn't understand the SDL_VIDEOEXPOSE event.

#4 rip-off   Moderators   -  Reputation: 8515

Like
0Likes
Like

Posted 19 March 2012 - 12:16 PM

It would help if you post your current code, so we can check if your new error checking/handling is correct.

I have put the error check you have given but still nothing is displayed

Can you verify that the program definitely doesn't enter the error handling code? Rather than, say, the output merely going somewhere you aren't checking. There are a couple of ways of doing this.

  • Step through the program in the debugger, and see whether the error branch is executed.
  • Output a message in an "else" clause. If you see no output then maybe you have more work to do in your error handling.
  • Throw in a platform specific notification, for example a MessageBox() call on Windows.

The image that I am trying to load is created by me in paint,I have just filled the background with a colour in paint and saved it with the name image.bmp

Should work. Another likely problem is that your programs "working directory" isn't where you expect it to be. You said that your image is in the same folder as your source code. Depending on which IDE / toolchain you are using, that will not be where relative paths will be resolved against.

I didn't understand the SDL_VIDEOEXPOSE event.

The event indicates that the video surface has been "exposed". For instance, if the window was hidden and then the user clicks on the entry in the task bar to bring the window back. Or if another window went in front of your program's one. It generally doesn't matter for games, because they often re-draw the entire screen every game loop.

However, if you were making a turn based or other non-active simulation game (e.g. chess, minesweeper), you might only draw the window when something changes. In this case you need to be notified when the stuff you have drawn becomes invalidated due to external events.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS