Newbie's first try

Hi,

I’m making my first attempt to learn the SDL. ?All I want to do is put up a
background image as a test. ?All indications are that everything works (No
errors returned) except that the screen just goes black. ?Any help would be
appreciated.

Thanks

Here is the code:

/***************************************************************************
? ? ? ? ? ? ? ? ? ? ? ? ? main.cpp ?- ?description
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?-------------------
? ? begin ? ? ? ? ? ? ? ?: Tue Dec 17 11:07:10 PST 2002
? ? copyright ? ? ? ? ? ?: © 2002 by Eric L. Damron
? ? email ? ? ? ? ? ? ? ?: @Eric_Damron
?***************************************************************************/

/***************************************************************************
?* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *
?* ? This program is free software; you can redistribute it and/or modify ?*
?* ? it under the terms of the GNU General Public License as published by ?*
?* ? the Free Software Foundation; either version 2 of the License, or ? ? *
?* ? (at your option) any later version. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *
?* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *
?***************************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <iostream.h>
#include <stdlib.h>
#include <SDL/SDL.h>

int ?initVideo(SDL_Surface *screen)
{

? /* Initialize SDL’s video system and check for errors */
? if (SDL_Init(SDL_INIT_VIDEO) != 0) {
? ? printf(“Unable to initialize SDL: %s \n”, SDL_GetError());
? ? return 1;
? }

? /* Make sure SDL_Quit gets called when the program exits! */
? atexit(SDL_Quit);

? /* Attempt to set a 1024x768 hicolor video mode */
? screen = SDL_SetVideoMode(1024, 768, 16, SDL_FULLSCREEN);
? if (screen == NULL) {
? ? printf(“Unable to set video mode: %s\n”, SDL_GetError());
? ? return 1;
? }

? /* If we got this far, everything worked */
? return 0;
}

int loadSplash(SDL_Surface *screen)
?{
? SDL_Surface *splashScreen;
? SDL_Rect src, dest;

? splashScreen =
SDL_LoadBMP("/home/eric/Projects/KDevelop/fuzzy/graphics/SplashScreen.bmp");

? if (splashScreen == NULL) {
? ? printf(“Unable to load bitmap.\n”);
? ? ?return 1;
? }

? src.x = 0;
? src.y = 0;
? src.w = splashScreen->w;
? src.h = splashScreen->h;
? dest.x = 0;
? dest.y = 0;
? dest.w = splashScreen->w;
? dest.h = splashScreen->h;

? SDL_BlitSurface(splashScreen, &src, screen, &dest);
? SDL_UpdateRect(screen, 0, 0, 0, 0);

? SDL_Delay(3000);

? SDL_FreeSurface(splashScreen);
? return 0;
}

int main(int argc, char *argv[])
{
? SDL_Surface *screen;

? initVideo(screen);
? loadSplash(screen);
? return 0;
}

Add this after SDL_UpdateRect(…);

SDL_Flip(screen);

-shawnOn Sat, 2002-12-21 at 02:48, Eric Damron wrote:

Hi,

I’m making my first attempt to learn the SDL. All I want to do is put up a
background image as a test. All indications are that everything works (No
errors returned) except that the screen just goes black. Any help would be
appreciated.

In your initVideo function you either need to pass the pointer to the
screen by reference.

ie:

in the main function make the line:
initVideo(screen);
to
initVideo(&screen);

and modify the initVideo function accordingly.
ie, the declaration with be:
int initVideo(SDL_Surface **screen)
and replace all instances of screen with *screen

or (more cleanly) have the initVideo return the pointer to screen, instead
of modifying the parameter.

In any case, what’s happening is that you’re modifying the pointer to
screen in local scope, so when the function returns you’re losing the
change of modifying what the pointer was pointing to (if you can follow
that =)

If it makes it easier to understand, it’s no different to:

void A( int b );
{
b = 5;
}

int main( int argc, char argv[] )
{
int b = 3;
A(b);
printf("%i\n", b); /
prints 3 */
return 0;
}

Hope I’m in some way clear in explaining what’s wrong =)

Regards,

Julian.On Sat, 21 Dec 2002 9:48 pm NZDT, Eric Damron wrote:

Hi,

I’m making my first attempt to learn the SDL. All I want to do is put
up a background image as a test. All indications are that everything
works (No errors returned) except that the screen just goes black. Any
help would be appreciated.


A real patriot is the fellow who gets a parking ticket and rejoices
that the system works.

I’m making my first attempt to learn the SDL. All I want to do is put
up a background image as a test. All indications are that everything
works (No errors returned) except that the screen just goes black. Any
help would be appreciated.

My guess is you think you’re storing a pointer to the screen surface in
main, but it actually contains garbage, so loadSplash fails to blit due to
an invalid surface. initVideo(SDL_Surface *screen) has a local copy of the
screen variable. When you store the result of SDL_SetVideoMode() it’s
storing it in the local copy, and not in the one that lives in main().
Try using a pointer to a pointer instead, or make screen global.–
Eric Lund
http://egotron.com