Hey,
Glad you asked this question! I’ve been tinkering with SDL for a week or
two now and have been
trying to figure out an elegant way to create a splash screen with it. My
initial cut, which was one of the
responses to your post, was to create a program that first launched the
splash screen, then the main game loop.
This is a ugly hack though, and I’d hoped for a better way, i.e., to keep
it all within the same program executable.
This may in fact be possible still, as I’m not a strong (C) programmer 
My code is attached/included if interested (main.c).
Also, when I use the gtv_center_window function (Linux FAQ) to center my
splash window, the window starts out in the
bottom right corner of my screen for a split second, then jogs to the
center. Anyone have an idea why this is, and/or how
to correct this behaviour?
Further, I found that you need SDL1.1.3 or higher in order to use this
example (gtv_center_window) function. I had .9 installed, and my program
kept complaining about XMoveWindow being undefined, (even though it is
anXLib function), until I upgraded to 1.1.3 of SDL.
Anyway, thanks alot!
James Caple
PH-Neutre wrote:
hello,
I’d like to create a splash screen where I’ll put “loading datas…” and
so on. I need to create a first window for this splash and then, when
all the stuff is ready, i need to create another one for the application
/ game. Then I’ll kill the first window.
but… i looked at the documentation but i couldn’t see what to use.
Maybe the threads ?
Thanks in advance,
PH.
-------------- next part --------------
/*
- Utility function to center an X11 Window
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <SDL.h>
#include <SDL_timer.h>
#include “SDL_syswm.h”
//Sint32 X, Y;
Uint32 pixel;
Uint8 *bits, bpp;
center_window(SDL_Surface *screen)
{
SDL_SysWMinfo info;
SDL_VERSION(&info.version);
if (SDL_GetWMInfo(&info) > 0 ) {
int x, y;
int w, h;
#ifdef unix
if (info.subsystem == SDL_SYSWM_X11) {
info.info.x11.lock_func();
w = DisplayWidth(info.info.x11.display, DefaultScreen(info.info.x11.display));
h = DisplayHeight(info.info.x11.display, DefaultScreen(info.info.x11.display));
x = (w - screen->w)/2;
y = (h - screen->h)/2;
XMoveWindow(info.info.x11.display, info.info.x11.wmwindow, x, y);
info.info.x11.unlock_func();
}
#else#
#endif
}
}
/*
- Draw stuff to the screen
*/
void Draw( SDL_Surface *screen, Sint32 X, Sint32 Y ) {
pixel = SDL_MapRGB(screen->format, 0xFF, 0xFF, 0x00);
if (SDL_MUSTLOCK(screen)) {
if (SDL_LockSurface(screen)<0)
return;
}
bpp = screen->format->BytesPerPixel;
//X = screen->w/2;
//Y = screen->h/2;
bits = ((Uint8 )screen->pixels)+Yscreen->pitch+X*bpp;
switch(bpp) {
case 1:
*((Uint8 *)(bits)) = (Uint8)pixel;
break;
case 2:
*((Uint16 *)(bits)) = (Uint16)pixel;
break;
case 3: {
Uint8 r,g,b;
r = (pixel>>screen->format->Rshift)&0xFF;
g = (pixel>>screen->format->Gshift)&0xFF;
b = (pixel>>screen->format->Bshift)&0xFF;
*((bits)+screen->format->Rshift/8) = r;
*((bits)+screen->format->Gshift/8) = g;
*((bits)+screen->format->Bshift/8) = b;
}
break;
case 4:
*((Uint32 *)(bits)) = (Uint32)pixel;
break;
}
if (SDL_MUSTLOCK(screen)) {
SDL_UnlockSurface(screen);
}
SDL_UpdateRect(screen, X, Y, 1, 1);
}
int FilterEvents(const SDL_Event *event) {
static int boycott = 1;
if ( (event->type == SDL_QUIT) && boycott ) {
printf(“Quit event filtered out – try again.\n”);
boycott = 0;
return (0);
}
if ( event->type == SDL_MOUSEMOTION ) {
printf(“Mouse moved to (%d,%d)\n”, event->motion.x, event->motion.y);
return(0);
}
return(1);
}
void ComplainAndExit(void) {
fprintf(stderr, “Problem: %s\n”, SDL_GetError());
exit(1);
}
-------------- next part --------------
/*
- Kludgy way to display splash screen and then the main program
*/
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
main() {
pid_t pid, w;
int i, status;
if ( (pid = fork()) == 0 ) {
// Run splash for a few seconds
system("./splash.c");
}
while((w = wait(&status)) && w != -1) {}
if ( (pid - fork()) == 0) {
// When splash is done, run main game loop
system("./main_tmd");
}
while((w = wait(&status)) && w != -1) {}
exit(0);
}
-------------- next part --------------
/*
- tmd.c
- Theater Missile Defender
- By James Caple
- 4/18/2000
- Learn C/C++ by hacking-together a simple game using SDL because
- Java sucks!!
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <SDL.h>
#include <SDL_timer.h>
#include “WMUtils.h”
int main(int argc, char *argv) {
SDL_PixelFormat fmt;
SDL_Surface *screen, *locked;
SDL_Surface *imagebmp, *image;
SDL_Rect dstrect;
int i;
Uint8 *buffer;
SDL_Event event;
Uint32 u_Pause = 4000;
/* Initialize vide */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, “Couldn’t initialize SDL: %s\n”, SDL_GetError());
exit (1);
}
atexit(SDL_Quit);
/* Load BMP file */
imagebmp = SDL_LoadBMP(“back.bmp”);
if ( imagebmp == NULL ) {
ComplainAndExit();
}
SDL_EventState(SDL_KEYDOWN, SDL_IGNORE);
SDL_EventState(SDL_KEYUP, SDL_IGNORE);
SDL_SetEventFilter(FilterEvents);
//screen = SDL_SetVideoMode(500, 640, 8, SDL_SWSURFACE|SDL_FULLSCREEN);
screen = SDL_SetVideoMode(500, 640, 8, SDL_SWSURFACE);
// Try to center window. Use gtv_center_window function from SDL Linux examples
// Function is in WMUtils.h
center_window( screen );
if ( screen == NULL ) {
//fprintf(stderr, “Couldn’t set 640x480x8 video mode: %s\n”, SDL_GetError());
//exit (1);
ComplainAndExit();
}
//SDL_Delay(5000);
/* set video colormap */
if ( imagebmp->format->palette != NULL ) {
SDL_SetColors(screen, imagebmp->format->palette->colors, 0, imagebmp->format->palette->ncolors);
}
/* Convert the image to the video format (maps colors) */
image = SDL_DisplayFormat(imagebmp);
SDL_FreeSurface(imagebmp);
if ( image == NULL ) {
ComplainAndExit();
}
/* Draw bands of color on the raw surface */
if ( SDL_MUSTLOCK(screen) ) {
if ( SDL_LockSurface(screen) < 0)
ComplainAndExit();
}
buffer=(Uint8 ) screen->pixels;
for ( i=0; ih; i++) {
memset (buffer, (i255)/screen->h, screen->w*screen->format->BytesPerPixel);
buffer += screen->pitch;
}
if ( SDL_MUSTLOCK(screen) ) {
SDL_UnlockSurface(screen);
}
/* Blit the image to the center of the screen */
dstrect.x = (screen->w-image->w)/2;
dstrect.y = (screen->h-image->h)/2;
dstrect.w = image->w;
dstrect.h = image->h;
if ( SDL_BlitSurface(image, NULL, screen, &dstrect) < 0 ) {
SDL_FreeSurface(image);
ComplainAndExit();
}
SDL_FreeSurface(image);
/* Update the screen */
SDL_UpdateRects(screen, 1, &dstrect);
/* Event handling stuff /
/******
while ( SDL_WaitEvent(&event) >= 0 ) {
switch (event.type) {
case SDL_ACTIVEEVENT: {
if ( event.active.state & SDL_APPACTIVE ) {
if ( event.active.gain ) {
printf(“App activated\n”);
} else {
printf(“App iconified\n”);
}
}
}
break;
case SDL_MOUSEBUTTONDOWN: {
Uint8 *keys;
keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_ESCAPE] == SDL_PRESSED ) {
printf("Bye bye...\n");
exit(0);
}
printf("Mouse button pressed\n");
}
break;
case SDL_QUIT: {
printf("Quit requested, quitting.\n");
exit(0);
}
break;
}
}
*********/
//run();
/* Wait a few seconds then kick-off main game frame(s) */
SDL_Delay( u_Pause );
//system(“./main_tmd”);
exit(0);
/* This should never happen */
printf(“SDL_WaitEvent error: %s\n”, SDL_GetError());
exit(1);
}