mirror of
https://github.com/yawut/SDL.git
synced 2026-08-22 00:35:18 -05:00
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
This commit is contained in:
@@ -7,7 +7,7 @@ EXE = @EXE@
|
||||
CFLAGS = @CFLAGS@
|
||||
LIBS = @LIBS@
|
||||
|
||||
TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcdrom$(EXE) testcursor$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl$(EXE) testhread$(EXE) testiconv$(EXE) testjoystick$(EXE) testkeys$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testsem$(EXE) testsprite$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) threadwin$(EXE) torturethread$(EXE)
|
||||
TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcdrom$(EXE) testcursor$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl$(EXE) testhread$(EXE) testiconv$(EXE) testjoystick$(EXE) testkeys$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testsem$(EXE) testsprite$(EXE) testsprite2$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) testwm2$(EXE) threadwin$(EXE) torturethread$(EXE)
|
||||
|
||||
all: $(TARGETS)
|
||||
|
||||
@@ -83,6 +83,9 @@ testsem$(EXE): $(srcdir)/testsem.c
|
||||
testsprite$(EXE): $(srcdir)/testsprite.c
|
||||
$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
|
||||
|
||||
testsprite2$(EXE): $(srcdir)/testsprite2.c
|
||||
$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
|
||||
|
||||
testtimer$(EXE): $(srcdir)/testtimer.c
|
||||
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
|
||||
|
||||
@@ -98,6 +101,9 @@ testwin$(EXE): $(srcdir)/testwin.c
|
||||
testwm$(EXE): $(srcdir)/testwm.c
|
||||
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
|
||||
|
||||
testwm2$(EXE): $(srcdir)/testwm.c
|
||||
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
|
||||
|
||||
threadwin$(EXE): $(srcdir)/threadwin.c
|
||||
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
|
||||
|
||||
|
||||
226
test/checkkeys.c
226
test/checkkeys.c
@@ -11,136 +11,140 @@
|
||||
#include "SDL.h"
|
||||
|
||||
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
||||
static void quit(int rc)
|
||||
static void
|
||||
quit(int rc)
|
||||
{
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
}
|
||||
|
||||
static void print_modifiers(void)
|
||||
static void
|
||||
print_modifiers(void)
|
||||
{
|
||||
int mod;
|
||||
printf(" modifiers:");
|
||||
mod = SDL_GetModState();
|
||||
if(!mod) {
|
||||
printf(" (none)");
|
||||
return;
|
||||
}
|
||||
if(mod & KMOD_LSHIFT)
|
||||
printf(" LSHIFT");
|
||||
if(mod & KMOD_RSHIFT)
|
||||
printf(" RSHIFT");
|
||||
if(mod & KMOD_LCTRL)
|
||||
printf(" LCTRL");
|
||||
if(mod & KMOD_RCTRL)
|
||||
printf(" RCTRL");
|
||||
if(mod & KMOD_LALT)
|
||||
printf(" LALT");
|
||||
if(mod & KMOD_RALT)
|
||||
printf(" RALT");
|
||||
if(mod & KMOD_LMETA)
|
||||
printf(" LMETA");
|
||||
if(mod & KMOD_RMETA)
|
||||
printf(" RMETA");
|
||||
if(mod & KMOD_NUM)
|
||||
printf(" NUM");
|
||||
if(mod & KMOD_CAPS)
|
||||
printf(" CAPS");
|
||||
if(mod & KMOD_MODE)
|
||||
printf(" MODE");
|
||||
int mod;
|
||||
printf(" modifiers:");
|
||||
mod = SDL_GetModState();
|
||||
if (!mod) {
|
||||
printf(" (none)");
|
||||
return;
|
||||
}
|
||||
if (mod & KMOD_LSHIFT)
|
||||
printf(" LSHIFT");
|
||||
if (mod & KMOD_RSHIFT)
|
||||
printf(" RSHIFT");
|
||||
if (mod & KMOD_LCTRL)
|
||||
printf(" LCTRL");
|
||||
if (mod & KMOD_RCTRL)
|
||||
printf(" RCTRL");
|
||||
if (mod & KMOD_LALT)
|
||||
printf(" LALT");
|
||||
if (mod & KMOD_RALT)
|
||||
printf(" RALT");
|
||||
if (mod & KMOD_LMETA)
|
||||
printf(" LMETA");
|
||||
if (mod & KMOD_RMETA)
|
||||
printf(" RMETA");
|
||||
if (mod & KMOD_NUM)
|
||||
printf(" NUM");
|
||||
if (mod & KMOD_CAPS)
|
||||
printf(" CAPS");
|
||||
if (mod & KMOD_MODE)
|
||||
printf(" MODE");
|
||||
}
|
||||
|
||||
static void PrintKey(SDL_keysym *sym, int pressed)
|
||||
static void
|
||||
PrintKey(SDL_keysym * sym, int pressed)
|
||||
{
|
||||
/* Print the keycode, name and state */
|
||||
if ( sym->sym ) {
|
||||
printf("Key %s: %d-%s ", pressed ? "pressed" : "released",
|
||||
sym->sym, SDL_GetKeyName(sym->sym));
|
||||
} else {
|
||||
printf("Unknown Key (scancode = %d) %s ", sym->scancode,
|
||||
pressed ? "pressed" : "released");
|
||||
}
|
||||
/* Print the keycode, name and state */
|
||||
if (sym->sym) {
|
||||
printf("Key %s: %d-%s ", pressed ? "pressed" : "released",
|
||||
sym->sym, SDL_GetKeyName(sym->sym));
|
||||
} else {
|
||||
printf("Unknown Key (scancode = %d) %s ", sym->scancode,
|
||||
pressed ? "pressed" : "released");
|
||||
}
|
||||
|
||||
/* Print the translated character, if one exists */
|
||||
if ( sym->unicode ) {
|
||||
/* Is it a control-character? */
|
||||
if ( sym->unicode < ' ' ) {
|
||||
printf(" (^%c)", sym->unicode+'@');
|
||||
} else {
|
||||
/* Print the translated character, if one exists */
|
||||
if (sym->unicode) {
|
||||
/* Is it a control-character? */
|
||||
if (sym->unicode < ' ') {
|
||||
printf(" (^%c)", sym->unicode + '@');
|
||||
} else {
|
||||
#ifdef UNICODE
|
||||
printf(" (%c)", sym->unicode);
|
||||
printf(" (%c)", sym->unicode);
|
||||
#else
|
||||
/* This is a Latin-1 program, so only show 8-bits */
|
||||
if ( !(sym->unicode & 0xFF00) )
|
||||
printf(" (%c)", sym->unicode);
|
||||
else
|
||||
printf(" (0x%X)", sym->unicode);
|
||||
/* This is a Latin-1 program, so only show 8-bits */
|
||||
if (!(sym->unicode & 0xFF00))
|
||||
printf(" (%c)", sym->unicode);
|
||||
else
|
||||
printf(" (0x%X)", sym->unicode);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
print_modifiers();
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
print_modifiers();
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
SDL_Event event;
|
||||
int done;
|
||||
Uint32 videoflags;
|
||||
SDL_Event event;
|
||||
int done;
|
||||
Uint32 videoflags;
|
||||
|
||||
/* Initialize SDL */
|
||||
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
|
||||
return(1);
|
||||
}
|
||||
/* Initialize SDL */
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return (1);
|
||||
}
|
||||
|
||||
videoflags = SDL_SWSURFACE;
|
||||
while( argc > 1 ) {
|
||||
--argc;
|
||||
if ( argv[argc] && !strcmp(argv[argc], "-fullscreen") ) {
|
||||
videoflags |= SDL_FULLSCREEN;
|
||||
} else {
|
||||
fprintf(stderr, "Usage: %s [-fullscreen]\n", argv[0]);
|
||||
quit(1);
|
||||
}
|
||||
}
|
||||
videoflags = SDL_SWSURFACE;
|
||||
while (argc > 1) {
|
||||
--argc;
|
||||
if (argv[argc] && !strcmp(argv[argc], "-fullscreen")) {
|
||||
videoflags |= SDL_FULLSCREEN;
|
||||
} else {
|
||||
fprintf(stderr, "Usage: %s [-fullscreen]\n", argv[0]);
|
||||
quit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Set 640x480 video mode */
|
||||
if ( SDL_SetVideoMode(640, 480, 0, videoflags) == NULL ) {
|
||||
fprintf(stderr, "Couldn't set 640x480 video mode: %s\n",
|
||||
SDL_GetError());
|
||||
quit(2);
|
||||
}
|
||||
/* Set 640x480 video mode */
|
||||
if (SDL_SetVideoMode(640, 480, 0, videoflags) == NULL) {
|
||||
fprintf(stderr, "Couldn't set 640x480 video mode: %s\n",
|
||||
SDL_GetError());
|
||||
quit(2);
|
||||
}
|
||||
|
||||
/* Enable UNICODE translation for keyboard input */
|
||||
SDL_EnableUNICODE(1);
|
||||
/* Enable UNICODE translation for keyboard input */
|
||||
SDL_EnableUNICODE(1);
|
||||
|
||||
/* Enable auto repeat for keyboard input */
|
||||
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,
|
||||
SDL_DEFAULT_REPEAT_INTERVAL);
|
||||
/* Enable auto repeat for keyboard input */
|
||||
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,
|
||||
SDL_DEFAULT_REPEAT_INTERVAL);
|
||||
|
||||
/* Watch keystrokes */
|
||||
done = 0;
|
||||
while ( !done ) {
|
||||
/* Check for events */
|
||||
SDL_WaitEvent(&event);
|
||||
switch (event.type) {
|
||||
case SDL_KEYDOWN:
|
||||
PrintKey(&event.key.keysym, 1);
|
||||
break;
|
||||
case SDL_KEYUP:
|
||||
PrintKey(&event.key.keysym, 0);
|
||||
break;
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
/* Any button press quits the app... */
|
||||
case SDL_QUIT:
|
||||
done = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* Watch keystrokes */
|
||||
done = 0;
|
||||
while (!done) {
|
||||
/* Check for events */
|
||||
SDL_WaitEvent(&event);
|
||||
switch (event.type) {
|
||||
case SDL_KEYDOWN:
|
||||
PrintKey(&event.key.keysym, 1);
|
||||
break;
|
||||
case SDL_KEYUP:
|
||||
PrintKey(&event.key.keysym, 0);
|
||||
break;
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
/* Any button press quits the app... */
|
||||
case SDL_QUIT:
|
||||
done = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
SDL_Quit();
|
||||
return(0);
|
||||
SDL_Quit();
|
||||
return (0);
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ AC_SUBST(EXE)
|
||||
AC_SUBST(MATHLIB)
|
||||
|
||||
dnl Check for SDL
|
||||
SDL_VERSION=1.2.10
|
||||
SDL_VERSION=1.3.0
|
||||
AM_PATH_SDL($SDL_VERSION,
|
||||
:,
|
||||
AC_MSG_ERROR([*** SDL version $SDL_VERSION not found!])
|
||||
|
||||
421
test/graywin.c
421
test/graywin.c
@@ -8,250 +8,241 @@
|
||||
|
||||
#include "SDL.h"
|
||||
|
||||
#ifdef TEST_VGA16 /* Define this if you want to test VGA 16-color video modes */
|
||||
#ifdef TEST_VGA16 /* Define this if you want to test VGA 16-color video modes */
|
||||
#define NUM_COLORS 16
|
||||
#else
|
||||
#define NUM_COLORS 256
|
||||
#endif
|
||||
|
||||
/* Draw a randomly sized and colored box centered about (X,Y) */
|
||||
void DrawBox(SDL_Surface *screen, int X, int Y, int width, int height)
|
||||
void
|
||||
DrawBox(SDL_Surface * screen, int X, int Y, int width, int height)
|
||||
{
|
||||
static unsigned int seeded = 0;
|
||||
SDL_Rect area;
|
||||
Uint32 color;
|
||||
Uint32 randc;
|
||||
static unsigned int seeded = 0;
|
||||
SDL_Rect area;
|
||||
Uint32 color;
|
||||
Uint32 randc;
|
||||
|
||||
/* Seed the random number generator */
|
||||
if ( seeded == 0 ) {
|
||||
srand(time(NULL));
|
||||
seeded = 1;
|
||||
}
|
||||
/* Seed the random number generator */
|
||||
if (seeded == 0) {
|
||||
srand(time(NULL));
|
||||
seeded = 1;
|
||||
}
|
||||
|
||||
/* Get the bounds of the rectangle */
|
||||
area.w = (rand()%width);
|
||||
area.h = (rand()%height);
|
||||
area.x = X-(area.w/2);
|
||||
area.y = Y-(area.h/2);
|
||||
randc = (rand()%NUM_COLORS);
|
||||
/* Get the bounds of the rectangle */
|
||||
area.w = (rand() % width);
|
||||
area.h = (rand() % height);
|
||||
area.x = X - (area.w / 2);
|
||||
area.y = Y - (area.h / 2);
|
||||
randc = (rand() % NUM_COLORS);
|
||||
|
||||
if (screen->format->BytesPerPixel==1)
|
||||
{
|
||||
color = randc;
|
||||
}
|
||||
else
|
||||
{
|
||||
color = SDL_MapRGB(screen->format, randc, randc, randc);
|
||||
}
|
||||
if (screen->format->BytesPerPixel == 1) {
|
||||
color = randc;
|
||||
} else {
|
||||
color = SDL_MapRGB(screen->format, randc, randc, randc);
|
||||
}
|
||||
|
||||
/* Do it! */
|
||||
SDL_FillRect(screen, &area, color);
|
||||
if ( screen->flags & SDL_DOUBLEBUF ) {
|
||||
SDL_Flip(screen);
|
||||
} else {
|
||||
SDL_UpdateRects(screen, 1, &area);
|
||||
}
|
||||
/* Do it! */
|
||||
SDL_FillRect(screen, &area, color);
|
||||
if (screen->flags & SDL_DOUBLEBUF) {
|
||||
SDL_Flip(screen);
|
||||
} else {
|
||||
SDL_UpdateRects(screen, 1, &area);
|
||||
}
|
||||
}
|
||||
|
||||
void DrawBackground(SDL_Surface *screen)
|
||||
void
|
||||
DrawBackground(SDL_Surface * screen)
|
||||
{
|
||||
int i, j, k;
|
||||
Uint8 *buffer;
|
||||
Uint16 *buffer16;
|
||||
Uint16 color;
|
||||
Uint8 gradient;
|
||||
int i, j, k;
|
||||
Uint8 *buffer;
|
||||
Uint16 *buffer16;
|
||||
Uint16 color;
|
||||
Uint8 gradient;
|
||||
|
||||
/* Set the surface pixels and refresh! */
|
||||
/* Use two loops in case the surface is double-buffered (both sides) */
|
||||
/* Set the surface pixels and refresh! */
|
||||
/* Use two loops in case the surface is double-buffered (both sides) */
|
||||
|
||||
for ( j=0; j<2; ++j ) {
|
||||
if ( SDL_LockSurface(screen) < 0 ) {
|
||||
fprintf(stderr, "Couldn't lock display surface: %s\n",
|
||||
SDL_GetError());
|
||||
return;
|
||||
}
|
||||
buffer = (Uint8 *)screen->pixels;
|
||||
for (j = 0; j < 2; ++j) {
|
||||
if (SDL_LockSurface(screen) < 0) {
|
||||
fprintf(stderr, "Couldn't lock display surface: %s\n",
|
||||
SDL_GetError());
|
||||
return;
|
||||
}
|
||||
buffer = (Uint8 *) screen->pixels;
|
||||
|
||||
if (screen->format->BytesPerPixel!=2) {
|
||||
for ( i=0; i<screen->h; ++i ) {
|
||||
memset(buffer,(i*(NUM_COLORS-1))/screen->h, screen->w * screen->format->BytesPerPixel);
|
||||
buffer += screen->pitch;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for ( i=0; i<screen->h; ++i ) {
|
||||
gradient=((i*(NUM_COLORS-1))/screen->h);
|
||||
color = SDL_MapRGB(screen->format, gradient, gradient, gradient);
|
||||
buffer16=(Uint16*)buffer;
|
||||
for (k=0; k<screen->w; k++)
|
||||
{
|
||||
*(buffer16+k)=color;
|
||||
}
|
||||
buffer += screen->pitch;
|
||||
}
|
||||
if (screen->format->BytesPerPixel != 2) {
|
||||
for (i = 0; i < screen->h; ++i) {
|
||||
memset(buffer, (i * (NUM_COLORS - 1)) / screen->h,
|
||||
screen->w * screen->format->BytesPerPixel);
|
||||
buffer += screen->pitch;
|
||||
}
|
||||
} else {
|
||||
for (i = 0; i < screen->h; ++i) {
|
||||
gradient = ((i * (NUM_COLORS - 1)) / screen->h);
|
||||
color =
|
||||
SDL_MapRGB(screen->format, gradient, gradient, gradient);
|
||||
buffer16 = (Uint16 *) buffer;
|
||||
for (k = 0; k < screen->w; k++) {
|
||||
*(buffer16 + k) = color;
|
||||
}
|
||||
buffer += screen->pitch;
|
||||
}
|
||||
}
|
||||
|
||||
SDL_UnlockSurface(screen);
|
||||
if ( screen->flags & SDL_DOUBLEBUF ) {
|
||||
SDL_Flip(screen);
|
||||
} else {
|
||||
SDL_UpdateRect(screen, 0, 0, 0, 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
SDL_UnlockSurface(screen);
|
||||
if (screen->flags & SDL_DOUBLEBUF) {
|
||||
SDL_Flip(screen);
|
||||
} else {
|
||||
SDL_UpdateRect(screen, 0, 0, 0, 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SDL_Surface *CreateScreen(Uint16 w, Uint16 h, Uint8 bpp, Uint32 flags)
|
||||
SDL_Surface *
|
||||
CreateScreen(Uint16 w, Uint16 h, Uint8 bpp, Uint32 flags)
|
||||
{
|
||||
SDL_Surface *screen;
|
||||
int i;
|
||||
SDL_Color palette[NUM_COLORS];
|
||||
SDL_Surface *screen;
|
||||
int i;
|
||||
SDL_Color palette[NUM_COLORS];
|
||||
|
||||
/* Set the video mode */
|
||||
screen = SDL_SetVideoMode(w, h, bpp, flags);
|
||||
if ( screen == NULL ) {
|
||||
fprintf(stderr, "Couldn't set display mode: %s\n",
|
||||
SDL_GetError());
|
||||
return(NULL);
|
||||
}
|
||||
fprintf(stderr, "Screen is in %s mode\n",
|
||||
(screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
|
||||
/* Set the video mode */
|
||||
screen = SDL_SetVideoMode(w, h, bpp, flags);
|
||||
if (screen == NULL) {
|
||||
fprintf(stderr, "Couldn't set display mode: %s\n", SDL_GetError());
|
||||
return (NULL);
|
||||
}
|
||||
fprintf(stderr, "Screen is in %s mode\n",
|
||||
(screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
|
||||
|
||||
if (bpp==8) {
|
||||
/* Set a gray colormap, reverse order from white to black */
|
||||
for ( i=0; i<NUM_COLORS; ++i ) {
|
||||
palette[i].r = (NUM_COLORS-1)-i * (256 / NUM_COLORS);
|
||||
palette[i].g = (NUM_COLORS-1)-i * (256 / NUM_COLORS);
|
||||
palette[i].b = (NUM_COLORS-1)-i * (256 / NUM_COLORS);
|
||||
}
|
||||
SDL_SetColors(screen, palette, 0, NUM_COLORS);
|
||||
}
|
||||
if (bpp == 8) {
|
||||
/* Set a gray colormap, reverse order from white to black */
|
||||
for (i = 0; i < NUM_COLORS; ++i) {
|
||||
palette[i].r = (NUM_COLORS - 1) - i * (256 / NUM_COLORS);
|
||||
palette[i].g = (NUM_COLORS - 1) - i * (256 / NUM_COLORS);
|
||||
palette[i].b = (NUM_COLORS - 1) - i * (256 / NUM_COLORS);
|
||||
}
|
||||
SDL_SetColors(screen, palette, 0, NUM_COLORS);
|
||||
}
|
||||
|
||||
return(screen);
|
||||
return (screen);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
SDL_Surface *screen;
|
||||
Uint32 videoflags;
|
||||
int done;
|
||||
SDL_Event event;
|
||||
int width, height, bpp;
|
||||
SDL_Surface *screen;
|
||||
Uint32 videoflags;
|
||||
int done;
|
||||
SDL_Event event;
|
||||
int width, height, bpp;
|
||||
|
||||
/* Initialize SDL */
|
||||
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
|
||||
exit(1);
|
||||
}
|
||||
/* Initialize SDL */
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* See if we try to get a hardware colormap */
|
||||
width = 640;
|
||||
height = 480;
|
||||
bpp = 8;
|
||||
videoflags = SDL_SWSURFACE;
|
||||
while ( argc > 1 ) {
|
||||
--argc;
|
||||
if ( argv[argc-1] && (strcmp(argv[argc-1], "-width") == 0) ) {
|
||||
width = atoi(argv[argc]);
|
||||
--argc;
|
||||
} else
|
||||
if ( argv[argc-1] && (strcmp(argv[argc-1], "-height") == 0) ) {
|
||||
height = atoi(argv[argc]);
|
||||
--argc;
|
||||
} else
|
||||
if ( argv[argc-1] && (strcmp(argv[argc-1], "-bpp") == 0) ) {
|
||||
bpp = atoi(argv[argc]);
|
||||
--argc;
|
||||
} else
|
||||
if ( argv[argc] && (strcmp(argv[argc], "-hw") == 0) ) {
|
||||
videoflags |= SDL_HWSURFACE;
|
||||
} else
|
||||
if ( argv[argc] && (strcmp(argv[argc], "-hwpalette") == 0) ) {
|
||||
videoflags |= SDL_HWPALETTE;
|
||||
} else
|
||||
if ( argv[argc] && (strcmp(argv[argc], "-flip") == 0) ) {
|
||||
videoflags |= SDL_DOUBLEBUF;
|
||||
} else
|
||||
if ( argv[argc] && (strcmp(argv[argc], "-noframe") == 0) ) {
|
||||
videoflags |= SDL_NOFRAME;
|
||||
} else
|
||||
if ( argv[argc] && (strcmp(argv[argc], "-resize") == 0) ) {
|
||||
videoflags |= SDL_RESIZABLE;
|
||||
} else
|
||||
if ( argv[argc] && (strcmp(argv[argc], "-fullscreen") == 0) ) {
|
||||
videoflags |= SDL_FULLSCREEN;
|
||||
} else {
|
||||
fprintf(stderr, "Usage: %s [-width] [-height] [-bpp] [-hw] [-hwpalette] [-flip] [-noframe] [-fullscreen] [-resize]\n",
|
||||
argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
/* See if we try to get a hardware colormap */
|
||||
width = 640;
|
||||
height = 480;
|
||||
bpp = 8;
|
||||
videoflags = SDL_SWSURFACE;
|
||||
while (argc > 1) {
|
||||
--argc;
|
||||
if (argv[argc - 1] && (strcmp(argv[argc - 1], "-width") == 0)) {
|
||||
width = atoi(argv[argc]);
|
||||
--argc;
|
||||
} else if (argv[argc - 1]
|
||||
&& (strcmp(argv[argc - 1], "-height") == 0)) {
|
||||
height = atoi(argv[argc]);
|
||||
--argc;
|
||||
} else if (argv[argc - 1] && (strcmp(argv[argc - 1], "-bpp") == 0)) {
|
||||
bpp = atoi(argv[argc]);
|
||||
--argc;
|
||||
} else if (argv[argc] && (strcmp(argv[argc], "-hw") == 0)) {
|
||||
videoflags |= SDL_HWSURFACE;
|
||||
} else if (argv[argc] && (strcmp(argv[argc], "-hwpalette") == 0)) {
|
||||
videoflags |= SDL_HWPALETTE;
|
||||
} else if (argv[argc] && (strcmp(argv[argc], "-flip") == 0)) {
|
||||
videoflags |= SDL_DOUBLEBUF;
|
||||
} else if (argv[argc] && (strcmp(argv[argc], "-noframe") == 0)) {
|
||||
videoflags |= SDL_NOFRAME;
|
||||
} else if (argv[argc] && (strcmp(argv[argc], "-resize") == 0)) {
|
||||
videoflags |= SDL_RESIZABLE;
|
||||
} else if (argv[argc] && (strcmp(argv[argc], "-fullscreen") == 0)) {
|
||||
videoflags |= SDL_FULLSCREEN;
|
||||
} else {
|
||||
fprintf(stderr,
|
||||
"Usage: %s [-width] [-height] [-bpp] [-hw] [-hwpalette] [-flip] [-noframe] [-fullscreen] [-resize]\n",
|
||||
argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Set a video mode */
|
||||
screen = CreateScreen(width, height, bpp, videoflags);
|
||||
if ( screen == NULL ) {
|
||||
exit(2);
|
||||
}
|
||||
|
||||
DrawBackground(screen);
|
||||
|
||||
/* Wait for a keystroke */
|
||||
done = 0;
|
||||
while ( !done && SDL_WaitEvent(&event) ) {
|
||||
switch (event.type) {
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
DrawBox(screen, event.button.x, event.button.y, width, height);
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
/* Ignore ALT-TAB for windows */
|
||||
if ( (event.key.keysym.sym == SDLK_LALT) ||
|
||||
(event.key.keysym.sym == SDLK_TAB) ) {
|
||||
break;
|
||||
}
|
||||
/* Center the mouse on <SPACE> */
|
||||
if ( event.key.keysym.sym == SDLK_SPACE ) {
|
||||
SDL_WarpMouse(width/2, height/2);
|
||||
break;
|
||||
}
|
||||
/* Toggle fullscreen mode on <RETURN> */
|
||||
if ( event.key.keysym.sym == SDLK_RETURN ) {
|
||||
videoflags ^= SDL_FULLSCREEN;
|
||||
screen = CreateScreen(
|
||||
screen->w, screen->h,
|
||||
screen->format->BitsPerPixel,
|
||||
videoflags);
|
||||
if ( screen == NULL ) {
|
||||
fprintf(stderr,
|
||||
"Couldn't toggle fullscreen mode\n");
|
||||
done = 1;
|
||||
}
|
||||
DrawBackground(screen);
|
||||
break;
|
||||
}
|
||||
/* Any other key quits the application... */
|
||||
case SDL_QUIT:
|
||||
done = 1;
|
||||
break;
|
||||
case SDL_VIDEOEXPOSE:
|
||||
DrawBackground(screen);
|
||||
break;
|
||||
case SDL_VIDEORESIZE:
|
||||
screen = CreateScreen(
|
||||
event.resize.w, event.resize.h,
|
||||
screen->format->BitsPerPixel,
|
||||
videoflags);
|
||||
if ( screen == NULL ) {
|
||||
fprintf(stderr,
|
||||
"Couldn't resize video mode\n");
|
||||
done = 1;
|
||||
}
|
||||
DrawBackground(screen);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
SDL_Quit();
|
||||
return(0);
|
||||
/* Set a video mode */
|
||||
screen = CreateScreen(width, height, bpp, videoflags);
|
||||
if (screen == NULL) {
|
||||
exit(2);
|
||||
}
|
||||
|
||||
DrawBackground(screen);
|
||||
|
||||
/* Wait for a keystroke */
|
||||
done = 0;
|
||||
while (!done && SDL_WaitEvent(&event)) {
|
||||
switch (event.type) {
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
DrawBox(screen, event.button.x, event.button.y, width, height);
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
/* Ignore ALT-TAB for windows */
|
||||
if ((event.key.keysym.sym == SDLK_LALT) ||
|
||||
(event.key.keysym.sym == SDLK_TAB)) {
|
||||
break;
|
||||
}
|
||||
/* Center the mouse on <SPACE> */
|
||||
if (event.key.keysym.sym == SDLK_SPACE) {
|
||||
SDL_WarpMouse(width / 2, height / 2);
|
||||
break;
|
||||
}
|
||||
/* Toggle fullscreen mode on <RETURN> */
|
||||
if (event.key.keysym.sym == SDLK_RETURN) {
|
||||
videoflags ^= SDL_FULLSCREEN;
|
||||
screen = CreateScreen(screen->w, screen->h,
|
||||
screen->format->BitsPerPixel,
|
||||
videoflags);
|
||||
if (screen == NULL) {
|
||||
fprintf(stderr, "Couldn't toggle fullscreen mode\n");
|
||||
done = 1;
|
||||
}
|
||||
DrawBackground(screen);
|
||||
break;
|
||||
}
|
||||
/* Any other key quits the application... */
|
||||
case SDL_QUIT:
|
||||
done = 1;
|
||||
break;
|
||||
case SDL_VIDEOEXPOSE:
|
||||
DrawBackground(screen);
|
||||
break;
|
||||
case SDL_VIDEORESIZE:
|
||||
printf("Screen resized to %dx%d\n", event.resize.w,
|
||||
event.resize.h);
|
||||
screen =
|
||||
CreateScreen(event.resize.w, event.resize.h,
|
||||
screen->format->BitsPerPixel, videoflags);
|
||||
if (screen == NULL) {
|
||||
fprintf(stderr, "Couldn't resize video mode\n");
|
||||
done = 1;
|
||||
}
|
||||
DrawBackground(screen);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
SDL_Quit();
|
||||
return (0);
|
||||
}
|
||||
|
||||
150
test/loopwave.c
150
test/loopwave.c
@@ -16,99 +16,117 @@
|
||||
#include "SDL.h"
|
||||
#include "SDL_audio.h"
|
||||
|
||||
struct {
|
||||
SDL_AudioSpec spec;
|
||||
Uint8 *sound; /* Pointer to wave data */
|
||||
Uint32 soundlen; /* Length of wave data */
|
||||
int soundpos; /* Current play position */
|
||||
struct
|
||||
{
|
||||
SDL_AudioSpec spec;
|
||||
Uint8 *sound; /* Pointer to wave data */
|
||||
Uint32 soundlen; /* Length of wave data */
|
||||
int soundpos; /* Current play position */
|
||||
} wave;
|
||||
|
||||
|
||||
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
||||
static void quit(int rc)
|
||||
static void
|
||||
quit(int rc)
|
||||
{
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
}
|
||||
|
||||
|
||||
void SDLCALL fillerup(void *unused, Uint8 *stream, int len)
|
||||
void SDLCALL
|
||||
fillerup(void *unused, Uint8 * stream, int len)
|
||||
{
|
||||
Uint8 *waveptr;
|
||||
int waveleft;
|
||||
Uint8 *waveptr;
|
||||
int waveleft;
|
||||
|
||||
/* Set up the pointers */
|
||||
waveptr = wave.sound + wave.soundpos;
|
||||
waveleft = wave.soundlen - wave.soundpos;
|
||||
/* Set up the pointers */
|
||||
waveptr = wave.sound + wave.soundpos;
|
||||
waveleft = wave.soundlen - wave.soundpos;
|
||||
|
||||
/* Go! */
|
||||
while ( waveleft <= len ) {
|
||||
SDL_MixAudio(stream, waveptr, waveleft, SDL_MIX_MAXVOLUME);
|
||||
stream += waveleft;
|
||||
len -= waveleft;
|
||||
waveptr = wave.sound;
|
||||
waveleft = wave.soundlen;
|
||||
wave.soundpos = 0;
|
||||
}
|
||||
SDL_MixAudio(stream, waveptr, len, SDL_MIX_MAXVOLUME);
|
||||
wave.soundpos += len;
|
||||
/* Go! */
|
||||
while (waveleft <= len) {
|
||||
SDL_MixAudio(stream, waveptr, waveleft, SDL_MIX_MAXVOLUME);
|
||||
stream += waveleft;
|
||||
len -= waveleft;
|
||||
waveptr = wave.sound;
|
||||
waveleft = wave.soundlen;
|
||||
wave.soundpos = 0;
|
||||
}
|
||||
SDL_MixAudio(stream, waveptr, len, SDL_MIX_MAXVOLUME);
|
||||
wave.soundpos += len;
|
||||
}
|
||||
|
||||
static int done = 0;
|
||||
void poked(int sig)
|
||||
void
|
||||
poked(int sig)
|
||||
{
|
||||
done = 1;
|
||||
done = 1;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
char name[32];
|
||||
int i, n;
|
||||
|
||||
/* Load the SDL library */
|
||||
if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
|
||||
return(1);
|
||||
}
|
||||
if ( argv[1] == NULL ) {
|
||||
argv[1] = "sample.wav";
|
||||
}
|
||||
/* Load the wave file into memory */
|
||||
if ( SDL_LoadWAV(argv[1],
|
||||
&wave.spec, &wave.sound, &wave.soundlen) == NULL ) {
|
||||
fprintf(stderr, "Couldn't load %s: %s\n",
|
||||
argv[1], SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
/* Print available audio drivers */
|
||||
n = SDL_GetNumAudioDrivers();
|
||||
if (n == 0) {
|
||||
printf("No built-in audio drivers\n");
|
||||
} else {
|
||||
printf("Built-in audio drivers:");
|
||||
for (i = 0; i < n; ++i) {
|
||||
if (i > 0) {
|
||||
printf(",");
|
||||
}
|
||||
printf(" %s", SDL_GetAudioDriver(i));
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
wave.spec.callback = fillerup;
|
||||
/* Load the SDL library */
|
||||
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return (1);
|
||||
}
|
||||
if (argv[1] == NULL) {
|
||||
argv[1] = "sample.wav";
|
||||
}
|
||||
/* Load the wave file into memory */
|
||||
if (SDL_LoadWAV(argv[1], &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
|
||||
fprintf(stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
|
||||
wave.spec.callback = fillerup;
|
||||
#if HAVE_SIGNAL_H
|
||||
/* Set the signals */
|
||||
/* Set the signals */
|
||||
#ifdef SIGHUP
|
||||
signal(SIGHUP, poked);
|
||||
signal(SIGHUP, poked);
|
||||
#endif
|
||||
signal(SIGINT, poked);
|
||||
signal(SIGINT, poked);
|
||||
#ifdef SIGQUIT
|
||||
signal(SIGQUIT, poked);
|
||||
signal(SIGQUIT, poked);
|
||||
#endif
|
||||
signal(SIGTERM, poked);
|
||||
signal(SIGTERM, poked);
|
||||
#endif /* HAVE_SIGNAL_H */
|
||||
|
||||
/* Initialize fillerup() variables */
|
||||
if ( SDL_OpenAudio(&wave.spec, NULL) < 0 ) {
|
||||
fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
|
||||
SDL_FreeWAV(wave.sound);
|
||||
quit(2);
|
||||
}
|
||||
SDL_PauseAudio(0);
|
||||
/* Initialize fillerup() variables */
|
||||
if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
|
||||
fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
|
||||
SDL_FreeWAV(wave.sound);
|
||||
quit(2);
|
||||
}
|
||||
SDL_PauseAudio(0);
|
||||
|
||||
/* Let the audio run */
|
||||
printf("Using audio driver: %s\n", SDL_AudioDriverName(name, 32));
|
||||
while ( ! done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING) )
|
||||
SDL_Delay(1000);
|
||||
/* Let the audio run */
|
||||
printf("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
|
||||
while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
|
||||
SDL_Delay(1000);
|
||||
|
||||
/* Clean up on signal */
|
||||
SDL_CloseAudio();
|
||||
SDL_FreeWAV(wave.sound);
|
||||
SDL_Quit();
|
||||
return(0);
|
||||
/* Clean up on signal */
|
||||
SDL_CloseAudio();
|
||||
SDL_FreeWAV(wave.sound);
|
||||
SDL_Quit();
|
||||
return (0);
|
||||
}
|
||||
|
||||
852
test/testalpha.c
852
test/testalpha.c
@@ -10,523 +10,527 @@
|
||||
|
||||
#include "SDL.h"
|
||||
|
||||
#define FRAME_TICKS (1000/30) /* 30 frames/second */
|
||||
#define FRAME_TICKS (1000/30) /* 30 frames/second */
|
||||
|
||||
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
||||
static void quit(int rc)
|
||||
static void
|
||||
quit(int rc)
|
||||
{
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
}
|
||||
|
||||
/* Fill the screen with a gradient */
|
||||
static void FillBackground(SDL_Surface *screen)
|
||||
static void
|
||||
FillBackground(SDL_Surface * screen)
|
||||
{
|
||||
Uint8 *buffer;
|
||||
Uint16 *buffer16;
|
||||
Uint16 color;
|
||||
Uint8 gradient;
|
||||
int i, k;
|
||||
Uint8 *buffer;
|
||||
Uint16 *buffer16;
|
||||
Uint16 color;
|
||||
Uint8 gradient;
|
||||
int i, k;
|
||||
|
||||
/* Set the surface pixels and refresh! */
|
||||
if ( SDL_LockSurface(screen) < 0 ) {
|
||||
fprintf(stderr, "Couldn't lock the display surface: %s\n",
|
||||
SDL_GetError());
|
||||
quit(2);
|
||||
}
|
||||
buffer=(Uint8 *)screen->pixels;
|
||||
if (screen->format->BytesPerPixel!=2) {
|
||||
for ( i=0; i<screen->h; ++i ) {
|
||||
memset(buffer,(i*255)/screen->h, screen->w*screen->format->BytesPerPixel);
|
||||
buffer += screen->pitch;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for ( i=0; i<screen->h; ++i ) {
|
||||
gradient=((i*255)/screen->h);
|
||||
color = (Uint16)SDL_MapRGB(screen->format, gradient, gradient, gradient);
|
||||
buffer16=(Uint16*)buffer;
|
||||
for (k=0; k<screen->w; k++)
|
||||
{
|
||||
*(buffer16+k)=color;
|
||||
}
|
||||
buffer += screen->pitch;
|
||||
}
|
||||
/* Set the surface pixels and refresh! */
|
||||
if (SDL_LockSurface(screen) < 0) {
|
||||
fprintf(stderr, "Couldn't lock the display surface: %s\n",
|
||||
SDL_GetError());
|
||||
quit(2);
|
||||
}
|
||||
buffer = (Uint8 *) screen->pixels;
|
||||
if (screen->format->BytesPerPixel != 2) {
|
||||
for (i = 0; i < screen->h; ++i) {
|
||||
memset(buffer, (i * 255) / screen->h,
|
||||
screen->w * screen->format->BytesPerPixel);
|
||||
buffer += screen->pitch;
|
||||
}
|
||||
} else {
|
||||
for (i = 0; i < screen->h; ++i) {
|
||||
gradient = ((i * 255) / screen->h);
|
||||
color =
|
||||
(Uint16) SDL_MapRGB(screen->format, gradient, gradient,
|
||||
gradient);
|
||||
buffer16 = (Uint16 *) buffer;
|
||||
for (k = 0; k < screen->w; k++) {
|
||||
*(buffer16 + k) = color;
|
||||
}
|
||||
buffer += screen->pitch;
|
||||
}
|
||||
}
|
||||
|
||||
SDL_UnlockSurface(screen);
|
||||
SDL_UpdateRect(screen, 0, 0, 0, 0);
|
||||
SDL_UnlockSurface(screen);
|
||||
SDL_UpdateRect(screen, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
/* Create a "light" -- a yellowish surface with variable alpha */
|
||||
SDL_Surface *CreateLight(int radius)
|
||||
SDL_Surface *
|
||||
CreateLight(int radius)
|
||||
{
|
||||
Uint8 trans, alphamask;
|
||||
int range, addition;
|
||||
int xdist, ydist;
|
||||
Uint16 x, y;
|
||||
Uint16 skip;
|
||||
Uint32 pixel;
|
||||
SDL_Surface *light;
|
||||
Uint8 trans, alphamask;
|
||||
int range, addition;
|
||||
int xdist, ydist;
|
||||
Uint16 x, y;
|
||||
Uint16 skip;
|
||||
Uint32 pixel;
|
||||
SDL_Surface *light;
|
||||
|
||||
#ifdef LIGHT_16BIT
|
||||
Uint16 *buf;
|
||||
Uint16 *buf;
|
||||
|
||||
/* Create a 16 (4/4/4/4) bpp square with a full 4-bit alpha channel */
|
||||
/* Note: this isn't any faster than a 32 bit alpha surface */
|
||||
alphamask = 0x0000000F;
|
||||
light = SDL_CreateRGBSurface(SDL_SWSURFACE, 2*radius, 2*radius, 16,
|
||||
0x0000F000, 0x00000F00, 0x000000F0, alphamask);
|
||||
/* Create a 16 (4/4/4/4) bpp square with a full 4-bit alpha channel */
|
||||
/* Note: this isn't any faster than a 32 bit alpha surface */
|
||||
alphamask = 0x0000000F;
|
||||
light = SDL_CreateRGBSurface(SDL_SWSURFACE, 2 * radius, 2 * radius, 16,
|
||||
0x0000F000, 0x00000F00, 0x000000F0,
|
||||
alphamask);
|
||||
#else
|
||||
Uint32 *buf;
|
||||
Uint32 *buf;
|
||||
|
||||
/* Create a 32 (8/8/8/8) bpp square with a full 8-bit alpha channel */
|
||||
alphamask = 0x000000FF;
|
||||
light = SDL_CreateRGBSurface(SDL_SWSURFACE, 2*radius, 2*radius, 32,
|
||||
0xFF000000, 0x00FF0000, 0x0000FF00, alphamask);
|
||||
if ( light == NULL ) {
|
||||
fprintf(stderr, "Couldn't create light: %s\n", SDL_GetError());
|
||||
return(NULL);
|
||||
}
|
||||
/* Create a 32 (8/8/8/8) bpp square with a full 8-bit alpha channel */
|
||||
alphamask = 0x000000FF;
|
||||
light = SDL_CreateRGBSurface(SDL_SWSURFACE, 2 * radius, 2 * radius, 32,
|
||||
0xFF000000, 0x00FF0000, 0x0000FF00,
|
||||
alphamask);
|
||||
if (light == NULL) {
|
||||
fprintf(stderr, "Couldn't create light: %s\n", SDL_GetError());
|
||||
return (NULL);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Fill with a light yellow-orange color */
|
||||
skip = light->pitch-(light->w*light->format->BytesPerPixel);
|
||||
/* Fill with a light yellow-orange color */
|
||||
skip = light->pitch - (light->w * light->format->BytesPerPixel);
|
||||
#ifdef LIGHT_16BIT
|
||||
buf = (Uint16 *)light->pixels;
|
||||
buf = (Uint16 *) light->pixels;
|
||||
#else
|
||||
buf = (Uint32 *)light->pixels;
|
||||
buf = (Uint32 *) light->pixels;
|
||||
#endif
|
||||
/* Get a tranparent pixel value - we'll add alpha later */
|
||||
pixel = SDL_MapRGBA(light->format, 0xFF, 0xDD, 0x88, 0);
|
||||
for ( y=0; y<light->h; ++y ) {
|
||||
for ( x=0; x<light->w; ++x ) {
|
||||
*buf++ = pixel;
|
||||
}
|
||||
buf += skip; /* Almost always 0, but just in case... */
|
||||
}
|
||||
/* Get a tranparent pixel value - we'll add alpha later */
|
||||
pixel = SDL_MapRGBA(light->format, 0xFF, 0xDD, 0x88, 0);
|
||||
for (y = 0; y < light->h; ++y) {
|
||||
for (x = 0; x < light->w; ++x) {
|
||||
*buf++ = pixel;
|
||||
}
|
||||
buf += skip; /* Almost always 0, but just in case... */
|
||||
}
|
||||
|
||||
/* Calculate alpha values for the surface. */
|
||||
/* Calculate alpha values for the surface. */
|
||||
#ifdef LIGHT_16BIT
|
||||
buf = (Uint16 *)light->pixels;
|
||||
buf = (Uint16 *) light->pixels;
|
||||
#else
|
||||
buf = (Uint32 *)light->pixels;
|
||||
buf = (Uint32 *) light->pixels;
|
||||
#endif
|
||||
for ( y=0; y<light->h; ++y ) {
|
||||
for ( x=0; x<light->w; ++x ) {
|
||||
/* Slow distance formula (from center of light) */
|
||||
xdist = x-(light->w/2);
|
||||
ydist = y-(light->h/2);
|
||||
range = (int)sqrt(xdist*xdist+ydist*ydist);
|
||||
for (y = 0; y < light->h; ++y) {
|
||||
for (x = 0; x < light->w; ++x) {
|
||||
/* Slow distance formula (from center of light) */
|
||||
xdist = x - (light->w / 2);
|
||||
ydist = y - (light->h / 2);
|
||||
range = (int) sqrt(xdist * xdist + ydist * ydist);
|
||||
|
||||
/* Scale distance to range of transparency (0-255) */
|
||||
if ( range > radius ) {
|
||||
trans = alphamask;
|
||||
} else {
|
||||
/* Increasing transparency with distance */
|
||||
trans = (Uint8)((range*alphamask)/radius);
|
||||
/* Scale distance to range of transparency (0-255) */
|
||||
if (range > radius) {
|
||||
trans = alphamask;
|
||||
} else {
|
||||
/* Increasing transparency with distance */
|
||||
trans = (Uint8) ((range * alphamask) / radius);
|
||||
|
||||
/* Lights are very transparent */
|
||||
addition = (alphamask+1)/8;
|
||||
if ( (int)trans+addition > alphamask ) {
|
||||
trans = alphamask;
|
||||
} else {
|
||||
trans += addition;
|
||||
}
|
||||
}
|
||||
/* We set the alpha component as the right N bits */
|
||||
*buf++ |= (255-trans);
|
||||
}
|
||||
buf += skip; /* Almost always 0, but just in case... */
|
||||
}
|
||||
/* Enable RLE acceleration of this alpha surface */
|
||||
SDL_SetAlpha(light, SDL_SRCALPHA|SDL_RLEACCEL, 0);
|
||||
/* Lights are very transparent */
|
||||
addition = (alphamask + 1) / 8;
|
||||
if ((int) trans + addition > alphamask) {
|
||||
trans = alphamask;
|
||||
} else {
|
||||
trans += addition;
|
||||
}
|
||||
}
|
||||
/* We set the alpha component as the right N bits */
|
||||
*buf++ |= (255 - trans);
|
||||
}
|
||||
buf += skip; /* Almost always 0, but just in case... */
|
||||
}
|
||||
/* Enable RLE acceleration of this alpha surface */
|
||||
SDL_SetAlpha(light, SDL_SRCALPHA | SDL_RLEACCEL, 0);
|
||||
|
||||
/* We're done! */
|
||||
return(light);
|
||||
/* We're done! */
|
||||
return (light);
|
||||
}
|
||||
|
||||
static Uint32 flashes = 0;
|
||||
static Uint32 flashtime = 0;
|
||||
|
||||
void FlashLight(SDL_Surface *screen, SDL_Surface *light, int x, int y)
|
||||
void
|
||||
FlashLight(SDL_Surface * screen, SDL_Surface * light, int x, int y)
|
||||
{
|
||||
SDL_Rect position;
|
||||
Uint32 ticks1;
|
||||
Uint32 ticks2;
|
||||
SDL_Rect position;
|
||||
Uint32 ticks1;
|
||||
Uint32 ticks2;
|
||||
|
||||
/* Easy, center light */
|
||||
position.x = x-(light->w/2);
|
||||
position.y = y-(light->h/2);
|
||||
position.w = light->w;
|
||||
position.h = light->h;
|
||||
ticks1 = SDL_GetTicks();
|
||||
SDL_BlitSurface(light, NULL, screen, &position);
|
||||
ticks2 = SDL_GetTicks();
|
||||
SDL_UpdateRects(screen, 1, &position);
|
||||
++flashes;
|
||||
/* Easy, center light */
|
||||
position.x = x - (light->w / 2);
|
||||
position.y = y - (light->h / 2);
|
||||
position.w = light->w;
|
||||
position.h = light->h;
|
||||
ticks1 = SDL_GetTicks();
|
||||
SDL_BlitSurface(light, NULL, screen, &position);
|
||||
ticks2 = SDL_GetTicks();
|
||||
SDL_UpdateRects(screen, 1, &position);
|
||||
++flashes;
|
||||
|
||||
/* Update time spend doing alpha blitting */
|
||||
flashtime += (ticks2-ticks1);
|
||||
/* Update time spend doing alpha blitting */
|
||||
flashtime += (ticks2 - ticks1);
|
||||
}
|
||||
|
||||
static int sprite_visible = 0;
|
||||
static SDL_Surface *sprite;
|
||||
static SDL_Surface *backing;
|
||||
static SDL_Rect position;
|
||||
static int x_vel, y_vel;
|
||||
static int alpha_vel;
|
||||
static SDL_Rect position;
|
||||
static int x_vel, y_vel;
|
||||
static int alpha_vel;
|
||||
|
||||
int LoadSprite(SDL_Surface *screen, char *file)
|
||||
int
|
||||
LoadSprite(SDL_Surface * screen, char *file)
|
||||
{
|
||||
SDL_Surface *converted;
|
||||
SDL_Surface *converted;
|
||||
|
||||
/* Load the sprite image */
|
||||
sprite = SDL_LoadBMP(file);
|
||||
if ( sprite == NULL ) {
|
||||
fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
|
||||
return(-1);
|
||||
}
|
||||
/* Load the sprite image */
|
||||
sprite = SDL_LoadBMP(file);
|
||||
if (sprite == NULL) {
|
||||
fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* Set transparent pixel as the pixel at (0,0) */
|
||||
if ( sprite->format->palette ) {
|
||||
SDL_SetColorKey(sprite, SDL_SRCCOLORKEY,
|
||||
*(Uint8 *)sprite->pixels);
|
||||
}
|
||||
/* Set transparent pixel as the pixel at (0,0) */
|
||||
if (sprite->format->palette) {
|
||||
SDL_SetColorKey(sprite, SDL_SRCCOLORKEY, *(Uint8 *) sprite->pixels);
|
||||
}
|
||||
|
||||
/* Convert sprite to video format */
|
||||
converted = SDL_DisplayFormat(sprite);
|
||||
SDL_FreeSurface(sprite);
|
||||
if ( converted == NULL ) {
|
||||
fprintf(stderr, "Couldn't convert background: %s\n",
|
||||
SDL_GetError());
|
||||
return(-1);
|
||||
}
|
||||
sprite = converted;
|
||||
/* Convert sprite to video format */
|
||||
converted = SDL_DisplayFormat(sprite);
|
||||
SDL_FreeSurface(sprite);
|
||||
if (converted == NULL) {
|
||||
fprintf(stderr, "Couldn't convert background: %s\n", SDL_GetError());
|
||||
return (-1);
|
||||
}
|
||||
sprite = converted;
|
||||
|
||||
/* Create the background */
|
||||
backing = SDL_CreateRGBSurface(SDL_SWSURFACE, sprite->w, sprite->h, 8,
|
||||
0, 0, 0, 0);
|
||||
if ( backing == NULL ) {
|
||||
fprintf(stderr, "Couldn't create background: %s\n",
|
||||
SDL_GetError());
|
||||
SDL_FreeSurface(sprite);
|
||||
return(-1);
|
||||
}
|
||||
/* Create the background */
|
||||
backing = SDL_CreateRGBSurface(SDL_SWSURFACE, sprite->w, sprite->h, 8,
|
||||
0, 0, 0, 0);
|
||||
if (backing == NULL) {
|
||||
fprintf(stderr, "Couldn't create background: %s\n", SDL_GetError());
|
||||
SDL_FreeSurface(sprite);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* Convert background to video format */
|
||||
converted = SDL_DisplayFormat(backing);
|
||||
SDL_FreeSurface(backing);
|
||||
if ( converted == NULL ) {
|
||||
fprintf(stderr, "Couldn't convert background: %s\n",
|
||||
SDL_GetError());
|
||||
SDL_FreeSurface(sprite);
|
||||
return(-1);
|
||||
}
|
||||
backing = converted;
|
||||
/* Convert background to video format */
|
||||
converted = SDL_DisplayFormat(backing);
|
||||
SDL_FreeSurface(backing);
|
||||
if (converted == NULL) {
|
||||
fprintf(stderr, "Couldn't convert background: %s\n", SDL_GetError());
|
||||
SDL_FreeSurface(sprite);
|
||||
return (-1);
|
||||
}
|
||||
backing = converted;
|
||||
|
||||
/* Set the initial position of the sprite */
|
||||
position.x = (screen->w-sprite->w)/2;
|
||||
position.y = (screen->h-sprite->h)/2;
|
||||
position.w = sprite->w;
|
||||
position.h = sprite->h;
|
||||
x_vel = 0; y_vel = 0;
|
||||
alpha_vel = 1;
|
||||
/* Set the initial position of the sprite */
|
||||
position.x = (screen->w - sprite->w) / 2;
|
||||
position.y = (screen->h - sprite->h) / 2;
|
||||
position.w = sprite->w;
|
||||
position.h = sprite->h;
|
||||
x_vel = 0;
|
||||
y_vel = 0;
|
||||
alpha_vel = 1;
|
||||
|
||||
/* We're ready to roll. :) */
|
||||
return(0);
|
||||
/* We're ready to roll. :) */
|
||||
return (0);
|
||||
}
|
||||
|
||||
void AttractSprite(Uint16 x, Uint16 y)
|
||||
void
|
||||
AttractSprite(Uint16 x, Uint16 y)
|
||||
{
|
||||
x_vel = ((int)x-position.x)/10;
|
||||
y_vel = ((int)y-position.y)/10;
|
||||
x_vel = ((int) x - position.x) / 10;
|
||||
y_vel = ((int) y - position.y) / 10;
|
||||
}
|
||||
|
||||
void MoveSprite(SDL_Surface *screen, SDL_Surface *light)
|
||||
void
|
||||
MoveSprite(SDL_Surface * screen, SDL_Surface * light)
|
||||
{
|
||||
SDL_Rect updates[2];
|
||||
int alpha;
|
||||
SDL_Rect updates[2];
|
||||
int alpha;
|
||||
|
||||
/* Erase the sprite if it was visible */
|
||||
if ( sprite_visible ) {
|
||||
updates[0] = position;
|
||||
SDL_BlitSurface(backing, NULL, screen, &updates[0]);
|
||||
} else {
|
||||
updates[0].x = 0; updates[0].y = 0;
|
||||
updates[0].w = 0; updates[0].h = 0;
|
||||
sprite_visible = 1;
|
||||
}
|
||||
/* Erase the sprite if it was visible */
|
||||
if (sprite_visible) {
|
||||
updates[0] = position;
|
||||
SDL_BlitSurface(backing, NULL, screen, &updates[0]);
|
||||
} else {
|
||||
updates[0].x = 0;
|
||||
updates[0].y = 0;
|
||||
updates[0].w = 0;
|
||||
updates[0].h = 0;
|
||||
sprite_visible = 1;
|
||||
}
|
||||
|
||||
/* Since the sprite is off the screen, we can do other drawing
|
||||
without being overwritten by the saved area behind the sprite.
|
||||
*/
|
||||
if ( light != NULL ) {
|
||||
int x, y;
|
||||
/* Since the sprite is off the screen, we can do other drawing
|
||||
without being overwritten by the saved area behind the sprite.
|
||||
*/
|
||||
if (light != NULL) {
|
||||
int x, y;
|
||||
|
||||
SDL_GetMouseState(&x, &y);
|
||||
FlashLight(screen, light, x, y);
|
||||
}
|
||||
|
||||
/* Move the sprite, bounce at the wall */
|
||||
position.x += x_vel;
|
||||
if ( (position.x < 0) || (position.x >= screen->w) ) {
|
||||
x_vel = -x_vel;
|
||||
position.x += x_vel;
|
||||
}
|
||||
position.y += y_vel;
|
||||
if ( (position.y < 0) || (position.y >= screen->h) ) {
|
||||
y_vel = -y_vel;
|
||||
position.y += y_vel;
|
||||
}
|
||||
SDL_GetMouseState(&x, &y);
|
||||
FlashLight(screen, light, x, y);
|
||||
}
|
||||
|
||||
/* Update transparency (fade in and out) */
|
||||
alpha = sprite->format->alpha;
|
||||
if ( (alpha+alpha_vel) < 0 ) {
|
||||
alpha_vel = -alpha_vel;
|
||||
} else
|
||||
if ( (alpha+alpha_vel) > 255 ) {
|
||||
alpha_vel = -alpha_vel;
|
||||
}
|
||||
SDL_SetAlpha(sprite, SDL_SRCALPHA, (Uint8)(alpha+alpha_vel));
|
||||
/* Move the sprite, bounce at the wall */
|
||||
position.x += x_vel;
|
||||
if ((position.x < 0) || (position.x >= screen->w)) {
|
||||
x_vel = -x_vel;
|
||||
position.x += x_vel;
|
||||
}
|
||||
position.y += y_vel;
|
||||
if ((position.y < 0) || (position.y >= screen->h)) {
|
||||
y_vel = -y_vel;
|
||||
position.y += y_vel;
|
||||
}
|
||||
|
||||
/* Save the area behind the sprite */
|
||||
updates[1] = position;
|
||||
SDL_BlitSurface(screen, &updates[1], backing, NULL);
|
||||
|
||||
/* Blit the sprite onto the screen */
|
||||
updates[1] = position;
|
||||
SDL_BlitSurface(sprite, NULL, screen, &updates[1]);
|
||||
/* Update transparency (fade in and out) */
|
||||
alpha = sprite->format->alpha;
|
||||
if ((alpha + alpha_vel) < 0) {
|
||||
alpha_vel = -alpha_vel;
|
||||
} else if ((alpha + alpha_vel) > 255) {
|
||||
alpha_vel = -alpha_vel;
|
||||
}
|
||||
SDL_SetAlpha(sprite, SDL_SRCALPHA, (Uint8) (alpha + alpha_vel));
|
||||
|
||||
/* Make it so! */
|
||||
SDL_UpdateRects(screen, 2, updates);
|
||||
/* Save the area behind the sprite */
|
||||
updates[1] = position;
|
||||
SDL_BlitSurface(screen, &updates[1], backing, NULL);
|
||||
|
||||
/* Blit the sprite onto the screen */
|
||||
updates[1] = position;
|
||||
SDL_BlitSurface(sprite, NULL, screen, &updates[1]);
|
||||
|
||||
/* Make it so! */
|
||||
SDL_UpdateRects(screen, 2, updates);
|
||||
}
|
||||
|
||||
void WarpSprite(SDL_Surface *screen, int x, int y)
|
||||
void
|
||||
WarpSprite(SDL_Surface * screen, int x, int y)
|
||||
{
|
||||
SDL_Rect updates[2];
|
||||
SDL_Rect updates[2];
|
||||
|
||||
/* Erase, move, Draw, update */
|
||||
updates[0] = position;
|
||||
SDL_BlitSurface(backing, NULL, screen, &updates[0]);
|
||||
position.x = x-sprite->w/2; /* Center about X */
|
||||
position.y = y-sprite->h/2; /* Center about Y */
|
||||
updates[1] = position;
|
||||
SDL_BlitSurface(screen, &updates[1], backing, NULL);
|
||||
updates[1] = position;
|
||||
SDL_BlitSurface(sprite, NULL, screen, &updates[1]);
|
||||
SDL_UpdateRects(screen, 2, updates);
|
||||
/* Erase, move, Draw, update */
|
||||
updates[0] = position;
|
||||
SDL_BlitSurface(backing, NULL, screen, &updates[0]);
|
||||
position.x = x - sprite->w / 2; /* Center about X */
|
||||
position.y = y - sprite->h / 2; /* Center about Y */
|
||||
updates[1] = position;
|
||||
SDL_BlitSurface(screen, &updates[1], backing, NULL);
|
||||
updates[1] = position;
|
||||
SDL_BlitSurface(sprite, NULL, screen, &updates[1]);
|
||||
SDL_UpdateRects(screen, 2, updates);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
const SDL_VideoInfo *info;
|
||||
SDL_Surface *screen;
|
||||
int w, h;
|
||||
Uint8 video_bpp;
|
||||
Uint32 videoflags;
|
||||
int i, done;
|
||||
SDL_Event event;
|
||||
SDL_Surface *light;
|
||||
int mouse_pressed;
|
||||
Uint32 ticks, lastticks;
|
||||
const SDL_VideoInfo *info;
|
||||
SDL_Surface *screen;
|
||||
int w, h;
|
||||
Uint8 video_bpp;
|
||||
Uint32 videoflags;
|
||||
int i, done;
|
||||
SDL_Event event;
|
||||
SDL_Surface *light;
|
||||
int mouse_pressed;
|
||||
Uint32 ticks, lastticks;
|
||||
|
||||
|
||||
/* Initialize SDL */
|
||||
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
|
||||
return(1);
|
||||
}
|
||||
/* Initialize SDL */
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return (1);
|
||||
}
|
||||
|
||||
/* Alpha blending doesn't work well at 8-bit color */
|
||||
/* Alpha blending doesn't work well at 8-bit color */
|
||||
#ifdef _WIN32_WCE
|
||||
/* Pocket PC */
|
||||
w = 240;
|
||||
h = 320;
|
||||
/* Pocket PC */
|
||||
w = 240;
|
||||
h = 320;
|
||||
#else
|
||||
w = 640;
|
||||
h = 480;
|
||||
w = 640;
|
||||
h = 480;
|
||||
#endif
|
||||
info = SDL_GetVideoInfo();
|
||||
if ( info->vfmt->BitsPerPixel > 8 ) {
|
||||
video_bpp = info->vfmt->BitsPerPixel;
|
||||
} else {
|
||||
video_bpp = 16;
|
||||
info = SDL_GetVideoInfo();
|
||||
if (info->vfmt->BitsPerPixel > 8) {
|
||||
video_bpp = info->vfmt->BitsPerPixel;
|
||||
} else {
|
||||
video_bpp = 16;
|
||||
fprintf(stderr, "forced 16 bpp mode\n");
|
||||
}
|
||||
videoflags = SDL_SWSURFACE;
|
||||
for (i = 1; argv[i]; ++i) {
|
||||
if (strcmp(argv[i], "-bpp") == 0) {
|
||||
video_bpp = atoi(argv[++i]);
|
||||
if (video_bpp <= 8) {
|
||||
video_bpp = 16;
|
||||
fprintf(stderr, "forced 16 bpp mode\n");
|
||||
}
|
||||
videoflags = SDL_SWSURFACE;
|
||||
for ( i = 1; argv[i]; ++i ) {
|
||||
if ( strcmp(argv[i], "-bpp") == 0 ) {
|
||||
video_bpp = atoi(argv[++i]);
|
||||
if (video_bpp<=8) {
|
||||
video_bpp=16;
|
||||
fprintf(stderr, "forced 16 bpp mode\n");
|
||||
}
|
||||
} else
|
||||
if ( strcmp(argv[i], "-hw") == 0 ) {
|
||||
videoflags |= SDL_HWSURFACE;
|
||||
} else
|
||||
if ( strcmp(argv[i], "-warp") == 0 ) {
|
||||
videoflags |= SDL_HWPALETTE;
|
||||
} else
|
||||
if ( strcmp(argv[i], "-width") == 0 && argv[i+1] ) {
|
||||
w = atoi(argv[++i]);
|
||||
} else
|
||||
if ( strcmp(argv[i], "-height") == 0 && argv[i+1] ) {
|
||||
h = atoi(argv[++i]);
|
||||
} else
|
||||
if ( strcmp(argv[i], "-resize") == 0 ) {
|
||||
videoflags |= SDL_RESIZABLE;
|
||||
} else
|
||||
if ( strcmp(argv[i], "-noframe") == 0 ) {
|
||||
videoflags |= SDL_NOFRAME;
|
||||
} else
|
||||
if ( strcmp(argv[i], "-fullscreen") == 0 ) {
|
||||
videoflags |= SDL_FULLSCREEN;
|
||||
} else {
|
||||
fprintf(stderr,
|
||||
"Usage: %s [-width N] [-height N] [-bpp N] [-warp] [-hw] [-fullscreen]\n",
|
||||
argv[0]);
|
||||
quit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (strcmp(argv[i], "-hw") == 0) {
|
||||
videoflags |= SDL_HWSURFACE;
|
||||
} else if (strcmp(argv[i], "-warp") == 0) {
|
||||
videoflags |= SDL_HWPALETTE;
|
||||
} else if (strcmp(argv[i], "-width") == 0 && argv[i + 1]) {
|
||||
w = atoi(argv[++i]);
|
||||
} else if (strcmp(argv[i], "-height") == 0 && argv[i + 1]) {
|
||||
h = atoi(argv[++i]);
|
||||
} else if (strcmp(argv[i], "-resize") == 0) {
|
||||
videoflags |= SDL_RESIZABLE;
|
||||
} else if (strcmp(argv[i], "-noframe") == 0) {
|
||||
videoflags |= SDL_NOFRAME;
|
||||
} else if (strcmp(argv[i], "-fullscreen") == 0) {
|
||||
videoflags |= SDL_FULLSCREEN;
|
||||
} else {
|
||||
fprintf(stderr,
|
||||
"Usage: %s [-width N] [-height N] [-bpp N] [-warp] [-hw] [-fullscreen]\n",
|
||||
argv[0]);
|
||||
quit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Set video mode */
|
||||
if ( (screen=SDL_SetVideoMode(w,h,video_bpp,videoflags)) == NULL ) {
|
||||
fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
|
||||
w, h, video_bpp, SDL_GetError());
|
||||
quit(2);
|
||||
}
|
||||
FillBackground(screen);
|
||||
/* Set video mode */
|
||||
if ((screen = SDL_SetVideoMode(w, h, video_bpp, videoflags)) == NULL) {
|
||||
fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
|
||||
w, h, video_bpp, SDL_GetError());
|
||||
quit(2);
|
||||
}
|
||||
FillBackground(screen);
|
||||
|
||||
/* Create the light */
|
||||
light = CreateLight(82);
|
||||
if ( light == NULL ) {
|
||||
quit(1);
|
||||
}
|
||||
/* Create the light */
|
||||
light = CreateLight(82);
|
||||
if (light == NULL) {
|
||||
quit(1);
|
||||
}
|
||||
|
||||
/* Load the sprite */
|
||||
if ( LoadSprite(screen, "icon.bmp") < 0 ) {
|
||||
SDL_FreeSurface(light);
|
||||
quit(1);
|
||||
}
|
||||
/* Load the sprite */
|
||||
if (LoadSprite(screen, "icon.bmp") < 0) {
|
||||
SDL_FreeSurface(light);
|
||||
quit(1);
|
||||
}
|
||||
|
||||
/* Print out information about our surfaces */
|
||||
printf("Screen is at %d bits per pixel\n",screen->format->BitsPerPixel);
|
||||
if ( (screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
|
||||
printf("Screen is in video memory\n");
|
||||
} else {
|
||||
printf("Screen is in system memory\n");
|
||||
}
|
||||
if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
|
||||
printf("Screen has double-buffering enabled\n");
|
||||
}
|
||||
if ( (sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
|
||||
printf("Sprite is in video memory\n");
|
||||
} else {
|
||||
printf("Sprite is in system memory\n");
|
||||
}
|
||||
/* Print out information about our surfaces */
|
||||
printf("Screen is at %d bits per pixel\n", screen->format->BitsPerPixel);
|
||||
if ((screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
|
||||
printf("Screen is in video memory\n");
|
||||
} else {
|
||||
printf("Screen is in system memory\n");
|
||||
}
|
||||
if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
|
||||
printf("Screen has double-buffering enabled\n");
|
||||
}
|
||||
if ((sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
|
||||
printf("Sprite is in video memory\n");
|
||||
} else {
|
||||
printf("Sprite is in system memory\n");
|
||||
}
|
||||
|
||||
/* Run a sample blit to trigger blit acceleration */
|
||||
MoveSprite(screen, NULL);
|
||||
if ( (sprite->flags & SDL_HWACCEL) == SDL_HWACCEL ) {
|
||||
printf("Sprite blit uses hardware alpha acceleration\n");
|
||||
} else {
|
||||
printf("Sprite blit dosn't uses hardware alpha acceleration\n");
|
||||
}
|
||||
/* Run a sample blit to trigger blit acceleration */
|
||||
MoveSprite(screen, NULL);
|
||||
if ((sprite->flags & SDL_HWACCEL) == SDL_HWACCEL) {
|
||||
printf("Sprite blit uses hardware alpha acceleration\n");
|
||||
} else {
|
||||
printf("Sprite blit dosn't uses hardware alpha acceleration\n");
|
||||
}
|
||||
|
||||
/* Set a clipping rectangle to clip the outside edge of the screen */
|
||||
{ SDL_Rect clip;
|
||||
clip.x = 32;
|
||||
clip.y = 32;
|
||||
clip.w = screen->w-(2*32);
|
||||
clip.h = screen->h-(2*32);
|
||||
SDL_SetClipRect(screen, &clip);
|
||||
}
|
||||
/* Set a clipping rectangle to clip the outside edge of the screen */
|
||||
{
|
||||
SDL_Rect clip;
|
||||
clip.x = 32;
|
||||
clip.y = 32;
|
||||
clip.w = screen->w - (2 * 32);
|
||||
clip.h = screen->h - (2 * 32);
|
||||
SDL_SetClipRect(screen, &clip);
|
||||
}
|
||||
|
||||
/* Wait for a keystroke */
|
||||
lastticks = SDL_GetTicks();
|
||||
done = 0;
|
||||
mouse_pressed = 0;
|
||||
while ( !done ) {
|
||||
/* Update the frame -- move the sprite */
|
||||
if ( mouse_pressed ) {
|
||||
MoveSprite(screen, light);
|
||||
mouse_pressed = 0;
|
||||
} else {
|
||||
MoveSprite(screen, NULL);
|
||||
}
|
||||
/* Wait for a keystroke */
|
||||
lastticks = SDL_GetTicks();
|
||||
done = 0;
|
||||
mouse_pressed = 0;
|
||||
while (!done) {
|
||||
/* Update the frame -- move the sprite */
|
||||
if (mouse_pressed) {
|
||||
MoveSprite(screen, light);
|
||||
mouse_pressed = 0;
|
||||
} else {
|
||||
MoveSprite(screen, NULL);
|
||||
}
|
||||
|
||||
/* Slow down the loop to 30 frames/second */
|
||||
ticks = SDL_GetTicks();
|
||||
if ( (ticks-lastticks) < FRAME_TICKS ) {
|
||||
/* Slow down the loop to 30 frames/second */
|
||||
ticks = SDL_GetTicks();
|
||||
if ((ticks - lastticks) < FRAME_TICKS) {
|
||||
#ifdef CHECK_SLEEP_GRANULARITY
|
||||
fprintf(stderr, "Sleeping %d ticks\n", FRAME_TICKS-(ticks-lastticks));
|
||||
fprintf(stderr, "Sleeping %d ticks\n",
|
||||
FRAME_TICKS - (ticks - lastticks));
|
||||
#endif
|
||||
SDL_Delay(FRAME_TICKS-(ticks-lastticks));
|
||||
SDL_Delay(FRAME_TICKS - (ticks - lastticks));
|
||||
#ifdef CHECK_SLEEP_GRANULARITY
|
||||
fprintf(stderr, "Slept %d ticks\n", (SDL_GetTicks()-ticks));
|
||||
fprintf(stderr, "Slept %d ticks\n", (SDL_GetTicks() - ticks));
|
||||
#endif
|
||||
}
|
||||
lastticks = ticks;
|
||||
}
|
||||
lastticks = ticks;
|
||||
|
||||
/* Check for events */
|
||||
while ( SDL_PollEvent(&event) ) {
|
||||
switch (event.type) {
|
||||
case SDL_VIDEORESIZE:
|
||||
screen = SDL_SetVideoMode(event.resize.w, event.resize.h, video_bpp, videoflags);
|
||||
if ( screen ) {
|
||||
FillBackground(screen);
|
||||
}
|
||||
break;
|
||||
/* Attract sprite while mouse is held down */
|
||||
case SDL_MOUSEMOTION:
|
||||
if (event.motion.state != 0) {
|
||||
AttractSprite(event.motion.x,
|
||||
event.motion.y);
|
||||
mouse_pressed = 1;
|
||||
}
|
||||
break;
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
if ( event.button.button == 1 ) {
|
||||
AttractSprite(event.button.x,
|
||||
event.button.y);
|
||||
mouse_pressed = 1;
|
||||
} else {
|
||||
SDL_Rect area;
|
||||
/* Check for events */
|
||||
while (SDL_PollEvent(&event)) {
|
||||
switch (event.type) {
|
||||
case SDL_VIDEORESIZE:
|
||||
screen =
|
||||
SDL_SetVideoMode(event.resize.w, event.resize.h,
|
||||
video_bpp, videoflags);
|
||||
if (screen) {
|
||||
FillBackground(screen);
|
||||
}
|
||||
break;
|
||||
/* Attract sprite while mouse is held down */
|
||||
case SDL_MOUSEMOTION:
|
||||
if (event.motion.state != 0) {
|
||||
AttractSprite(event.motion.x, event.motion.y);
|
||||
mouse_pressed = 1;
|
||||
}
|
||||
break;
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
if (event.button.button == 1) {
|
||||
AttractSprite(event.button.x, event.button.y);
|
||||
mouse_pressed = 1;
|
||||
} else {
|
||||
SDL_Rect area;
|
||||
|
||||
area.x = event.button.x-16;
|
||||
area.y = event.button.y-16;
|
||||
area.w = 32;
|
||||
area.h = 32;
|
||||
SDL_FillRect(screen, &area, 0);
|
||||
SDL_UpdateRects(screen,1,&area);
|
||||
}
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
if ( event.key.keysym.sym == SDLK_ESCAPE ) {
|
||||
done = 1;
|
||||
}
|
||||
break;
|
||||
case SDL_QUIT:
|
||||
done = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
SDL_FreeSurface(light);
|
||||
SDL_FreeSurface(sprite);
|
||||
SDL_FreeSurface(backing);
|
||||
area.x = event.button.x - 16;
|
||||
area.y = event.button.y - 16;
|
||||
area.w = 32;
|
||||
area.h = 32;
|
||||
SDL_FillRect(screen, &area, 0);
|
||||
SDL_UpdateRects(screen, 1, &area);
|
||||
}
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
if (event.key.keysym.sym == SDLK_ESCAPE) {
|
||||
done = 1;
|
||||
}
|
||||
break;
|
||||
case SDL_QUIT:
|
||||
done = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
SDL_FreeSurface(light);
|
||||
SDL_FreeSurface(sprite);
|
||||
SDL_FreeSurface(backing);
|
||||
|
||||
/* Print out some timing information */
|
||||
if ( flashes > 0 ) {
|
||||
printf("%d alpha blits, ~%4.4f ms per blit\n",
|
||||
flashes, (float)flashtime/flashes);
|
||||
}
|
||||
/* Print out some timing information */
|
||||
if (flashes > 0) {
|
||||
printf("%d alpha blits, ~%4.4f ms per blit\n",
|
||||
flashes, (float) flashtime / flashes);
|
||||
}
|
||||
|
||||
SDL_Quit();
|
||||
return(0);
|
||||
SDL_Quit();
|
||||
return (0);
|
||||
}
|
||||
|
||||
@@ -9,176 +9,174 @@
|
||||
#include "picture.xbm"
|
||||
|
||||
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
||||
static void quit(int rc)
|
||||
static void
|
||||
quit(int rc)
|
||||
{
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
}
|
||||
|
||||
SDL_Surface *LoadXBM(SDL_Surface *screen, int w, int h, Uint8 *bits)
|
||||
SDL_Surface *
|
||||
LoadXBM(SDL_Surface * screen, int w, int h, Uint8 * bits)
|
||||
{
|
||||
SDL_Surface *bitmap;
|
||||
Uint8 *line;
|
||||
SDL_Surface *bitmap;
|
||||
Uint8 *line;
|
||||
|
||||
/* Allocate the bitmap */
|
||||
bitmap = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 1, 0, 0, 0, 0);
|
||||
if ( bitmap == NULL ) {
|
||||
fprintf(stderr, "Couldn't allocate bitmap: %s\n",
|
||||
SDL_GetError());
|
||||
return(NULL);
|
||||
}
|
||||
/* Allocate the bitmap */
|
||||
bitmap = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 1, 0, 0, 0, 0);
|
||||
if (bitmap == NULL) {
|
||||
fprintf(stderr, "Couldn't allocate bitmap: %s\n", SDL_GetError());
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/* Copy the pixels */
|
||||
line = (Uint8 *)bitmap->pixels;
|
||||
w = (w+7)/8;
|
||||
while ( h-- ) {
|
||||
memcpy(line, bits, w);
|
||||
/* X11 Bitmap images have the bits reversed */
|
||||
{ int i, j; Uint8 *buf, byte;
|
||||
for ( buf=line, i=0; i<w; ++i, ++buf ) {
|
||||
byte = *buf;
|
||||
*buf = 0;
|
||||
for ( j=7; j>=0; --j ) {
|
||||
*buf |= (byte&0x01)<<j;
|
||||
byte >>= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
line += bitmap->pitch;
|
||||
bits += w;
|
||||
}
|
||||
return(bitmap);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
SDL_Surface *screen;
|
||||
SDL_Surface *bitmap;
|
||||
Uint8 video_bpp;
|
||||
Uint32 videoflags;
|
||||
Uint8 *buffer;
|
||||
int i, k, done;
|
||||
SDL_Event event;
|
||||
Uint16 *buffer16;
|
||||
Uint16 color;
|
||||
Uint8 gradient;
|
||||
SDL_Color palette[256];
|
||||
|
||||
|
||||
/* Initialize SDL */
|
||||
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
|
||||
return(1);
|
||||
}
|
||||
|
||||
video_bpp = 0;
|
||||
videoflags = SDL_SWSURFACE;
|
||||
while ( argc > 1 ) {
|
||||
--argc;
|
||||
if ( strcmp(argv[argc-1], "-bpp") == 0 ) {
|
||||
video_bpp = atoi(argv[argc]);
|
||||
--argc;
|
||||
} else
|
||||
if ( strcmp(argv[argc], "-warp") == 0 ) {
|
||||
videoflags |= SDL_HWPALETTE;
|
||||
} else
|
||||
if ( strcmp(argv[argc], "-hw") == 0 ) {
|
||||
videoflags |= SDL_HWSURFACE;
|
||||
} else
|
||||
if ( strcmp(argv[argc], "-fullscreen") == 0 ) {
|
||||
videoflags |= SDL_FULLSCREEN;
|
||||
} else {
|
||||
fprintf(stderr,
|
||||
"Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
|
||||
argv[0]);
|
||||
quit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Set 640x480 video mode */
|
||||
if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) {
|
||||
fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
|
||||
video_bpp, SDL_GetError());
|
||||
quit(2);
|
||||
}
|
||||
|
||||
if (video_bpp==8) {
|
||||
/* Set a gray colormap, reverse order from white to black */
|
||||
for ( i=0; i<256; ++i ) {
|
||||
palette[i].r = 255-i;
|
||||
palette[i].g = 255-i;
|
||||
palette[i].b = 255-i;
|
||||
}
|
||||
SDL_SetColors(screen, palette, 0, 256);
|
||||
}
|
||||
|
||||
/* Set the surface pixels and refresh! */
|
||||
if ( SDL_LockSurface(screen) < 0 ) {
|
||||
fprintf(stderr, "Couldn't lock the display surface: %s\n",
|
||||
SDL_GetError());
|
||||
quit(2);
|
||||
}
|
||||
buffer=(Uint8 *)screen->pixels;
|
||||
if (screen->format->BytesPerPixel!=2) {
|
||||
for ( i=0; i<screen->h; ++i ) {
|
||||
memset(buffer,(i*255)/screen->h, screen->pitch);
|
||||
buffer += screen->pitch;
|
||||
}
|
||||
}
|
||||
else
|
||||
/* Copy the pixels */
|
||||
line = (Uint8 *) bitmap->pixels;
|
||||
w = (w + 7) / 8;
|
||||
while (h--) {
|
||||
memcpy(line, bits, w);
|
||||
/* X11 Bitmap images have the bits reversed */
|
||||
{
|
||||
for ( i=0; i<screen->h; ++i ) {
|
||||
gradient=((i*255)/screen->h);
|
||||
color = SDL_MapRGB(screen->format, gradient, gradient, gradient);
|
||||
buffer16=(Uint16*)buffer;
|
||||
for (k=0; k<screen->w; k++)
|
||||
{
|
||||
*(buffer16+k)=color;
|
||||
}
|
||||
buffer += screen->pitch;
|
||||
}
|
||||
int i, j;
|
||||
Uint8 *buf, byte;
|
||||
for (buf = line, i = 0; i < w; ++i, ++buf) {
|
||||
byte = *buf;
|
||||
*buf = 0;
|
||||
for (j = 7; j >= 0; --j) {
|
||||
*buf |= (byte & 0x01) << j;
|
||||
byte >>= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
SDL_UnlockSurface(screen);
|
||||
SDL_UpdateRect(screen, 0, 0, 0, 0);
|
||||
|
||||
/* Load the bitmap */
|
||||
bitmap = LoadXBM(screen, picture_width, picture_height,
|
||||
(Uint8 *)picture_bits);
|
||||
if ( bitmap == NULL ) {
|
||||
quit(1);
|
||||
}
|
||||
|
||||
/* Wait for a keystroke */
|
||||
done = 0;
|
||||
while ( !done ) {
|
||||
/* Check for events */
|
||||
while ( SDL_PollEvent(&event) ) {
|
||||
switch (event.type) {
|
||||
case SDL_MOUSEBUTTONDOWN: {
|
||||
SDL_Rect dst;
|
||||
|
||||
dst.x = event.button.x - bitmap->w/2;
|
||||
dst.y = event.button.y - bitmap->h/2;
|
||||
dst.w = bitmap->w;
|
||||
dst.h = bitmap->h;
|
||||
SDL_BlitSurface(bitmap, NULL,
|
||||
screen, &dst);
|
||||
SDL_UpdateRects(screen,1,&dst);
|
||||
}
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
/* Any key press quits the app... */
|
||||
done = 1;
|
||||
break;
|
||||
case SDL_QUIT:
|
||||
done = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
SDL_FreeSurface(bitmap);
|
||||
SDL_Quit();
|
||||
return(0);
|
||||
line += bitmap->pitch;
|
||||
bits += w;
|
||||
}
|
||||
return (bitmap);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
SDL_Surface *screen;
|
||||
SDL_Surface *bitmap;
|
||||
Uint8 video_bpp;
|
||||
Uint32 videoflags;
|
||||
Uint8 *buffer;
|
||||
int i, k, done;
|
||||
SDL_Event event;
|
||||
Uint16 *buffer16;
|
||||
Uint16 color;
|
||||
Uint8 gradient;
|
||||
SDL_Color palette[256];
|
||||
|
||||
|
||||
/* Initialize SDL */
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return (1);
|
||||
}
|
||||
|
||||
video_bpp = 0;
|
||||
videoflags = SDL_SWSURFACE;
|
||||
while (argc > 1) {
|
||||
--argc;
|
||||
if (strcmp(argv[argc - 1], "-bpp") == 0) {
|
||||
video_bpp = atoi(argv[argc]);
|
||||
--argc;
|
||||
} else if (strcmp(argv[argc], "-warp") == 0) {
|
||||
videoflags |= SDL_HWPALETTE;
|
||||
} else if (strcmp(argv[argc], "-hw") == 0) {
|
||||
videoflags |= SDL_HWSURFACE;
|
||||
} else if (strcmp(argv[argc], "-fullscreen") == 0) {
|
||||
videoflags |= SDL_FULLSCREEN;
|
||||
} else {
|
||||
fprintf(stderr,
|
||||
"Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
|
||||
argv[0]);
|
||||
quit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Set 640x480 video mode */
|
||||
if ((screen = SDL_SetVideoMode(640, 480, video_bpp, videoflags)) == NULL) {
|
||||
fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
|
||||
video_bpp, SDL_GetError());
|
||||
quit(2);
|
||||
}
|
||||
|
||||
if (video_bpp == 8) {
|
||||
/* Set a gray colormap, reverse order from white to black */
|
||||
for (i = 0; i < 256; ++i) {
|
||||
palette[i].r = 255 - i;
|
||||
palette[i].g = 255 - i;
|
||||
palette[i].b = 255 - i;
|
||||
}
|
||||
SDL_SetColors(screen, palette, 0, 256);
|
||||
}
|
||||
|
||||
/* Set the surface pixels and refresh! */
|
||||
if (SDL_LockSurface(screen) < 0) {
|
||||
fprintf(stderr, "Couldn't lock the display surface: %s\n",
|
||||
SDL_GetError());
|
||||
quit(2);
|
||||
}
|
||||
buffer = (Uint8 *) screen->pixels;
|
||||
if (screen->format->BytesPerPixel != 2) {
|
||||
for (i = 0; i < screen->h; ++i) {
|
||||
memset(buffer, (i * 255) / screen->h, screen->pitch);
|
||||
buffer += screen->pitch;
|
||||
}
|
||||
} else {
|
||||
for (i = 0; i < screen->h; ++i) {
|
||||
gradient = ((i * 255) / screen->h);
|
||||
color = SDL_MapRGB(screen->format, gradient, gradient, gradient);
|
||||
buffer16 = (Uint16 *) buffer;
|
||||
for (k = 0; k < screen->w; k++) {
|
||||
*(buffer16 + k) = color;
|
||||
}
|
||||
buffer += screen->pitch;
|
||||
}
|
||||
}
|
||||
SDL_UnlockSurface(screen);
|
||||
SDL_UpdateRect(screen, 0, 0, 0, 0);
|
||||
|
||||
/* Load the bitmap */
|
||||
bitmap = LoadXBM(screen, picture_width, picture_height,
|
||||
(Uint8 *) picture_bits);
|
||||
if (bitmap == NULL) {
|
||||
quit(1);
|
||||
}
|
||||
|
||||
/* Wait for a keystroke */
|
||||
done = 0;
|
||||
while (!done) {
|
||||
/* Check for events */
|
||||
while (SDL_PollEvent(&event)) {
|
||||
switch (event.type) {
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
{
|
||||
SDL_Rect dst;
|
||||
|
||||
dst.x = event.button.x - bitmap->w / 2;
|
||||
dst.y = event.button.y - bitmap->h / 2;
|
||||
dst.w = bitmap->w;
|
||||
dst.h = bitmap->h;
|
||||
SDL_BlitSurface(bitmap, NULL, screen, &dst);
|
||||
SDL_UpdateRects(screen, 1, &dst);
|
||||
}
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
/* Any key press quits the app... */
|
||||
done = 1;
|
||||
break;
|
||||
case SDL_QUIT:
|
||||
done = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
SDL_FreeSurface(bitmap);
|
||||
SDL_Quit();
|
||||
return (0);
|
||||
}
|
||||
|
||||
@@ -15,27 +15,30 @@ static SDL_Surface *src = NULL;
|
||||
static int testSeconds = 10;
|
||||
|
||||
|
||||
static int percent(int val, int total)
|
||||
static int
|
||||
percent(int val, int total)
|
||||
{
|
||||
return((int) ((((float) val) / ((float) total)) * 100.0f));
|
||||
return ((int) ((((float) val) / ((float) total)) * 100.0f));
|
||||
}
|
||||
|
||||
static int randRange(int lo, int hi)
|
||||
static int
|
||||
randRange(int lo, int hi)
|
||||
{
|
||||
return(lo + (int) (((double) hi)*rand()/(RAND_MAX+1.0)));
|
||||
return (lo + (int) (((double) hi) * rand() / (RAND_MAX + 1.0)));
|
||||
}
|
||||
|
||||
static void copy_trunc_str(char *str, size_t strsize, const char *flagstr)
|
||||
static void
|
||||
copy_trunc_str(char *str, size_t strsize, const char *flagstr)
|
||||
{
|
||||
if ( (strlen(str) + strlen(flagstr)) >= (strsize - 1) )
|
||||
if ((strlen(str) + strlen(flagstr)) >= (strsize - 1))
|
||||
strcpy(str + (strsize - 5), " ...");
|
||||
else
|
||||
strcat(str, flagstr);
|
||||
}
|
||||
|
||||
static void __append_sdl_surface_flag(SDL_Surface *_surface, char *str,
|
||||
size_t strsize, Uint32 flag,
|
||||
const char *flagstr)
|
||||
static void
|
||||
__append_sdl_surface_flag(SDL_Surface * _surface, char *str,
|
||||
size_t strsize, Uint32 flag, const char *flagstr)
|
||||
{
|
||||
if (_surface->flags & flag)
|
||||
copy_trunc_str(str, strsize, flagstr);
|
||||
@@ -45,22 +48,28 @@ static void __append_sdl_surface_flag(SDL_Surface *_surface, char *str,
|
||||
#define append_sdl_surface_flag(a, b, c, fl) __append_sdl_surface_flag(a, b, c, fl, " " #fl)
|
||||
#define print_tf_state(str, val) printf("%s: {%s}\n", str, (val) ? "true" : "false" )
|
||||
|
||||
static void output_videoinfo_details(void)
|
||||
static void
|
||||
output_videoinfo_details(void)
|
||||
{
|
||||
const SDL_VideoInfo *info = SDL_GetVideoInfo();
|
||||
printf("SDL_GetVideoInfo():\n");
|
||||
if (info == NULL)
|
||||
printf(" (null.)\n");
|
||||
else
|
||||
{
|
||||
else {
|
||||
print_tf_state(" hardware surface available", info->hw_available);
|
||||
print_tf_state(" window manager available", info->wm_available);
|
||||
print_tf_state(" accelerated hardware->hardware blits", info->blit_hw);
|
||||
print_tf_state(" accelerated hardware->hardware colorkey blits", info->blit_hw_CC);
|
||||
print_tf_state(" accelerated hardware->hardware alpha blits", info->blit_hw_A);
|
||||
print_tf_state(" accelerated software->hardware blits", info->blit_sw);
|
||||
print_tf_state(" accelerated software->hardware colorkey blits", info->blit_sw_CC);
|
||||
print_tf_state(" accelerated software->hardware alpha blits", info->blit_sw_A);
|
||||
print_tf_state(" accelerated hardware->hardware blits",
|
||||
info->blit_hw);
|
||||
print_tf_state(" accelerated hardware->hardware colorkey blits",
|
||||
info->blit_hw_CC);
|
||||
print_tf_state(" accelerated hardware->hardware alpha blits",
|
||||
info->blit_hw_A);
|
||||
print_tf_state(" accelerated software->hardware blits",
|
||||
info->blit_sw);
|
||||
print_tf_state(" accelerated software->hardware colorkey blits",
|
||||
info->blit_sw_CC);
|
||||
print_tf_state(" accelerated software->hardware alpha blits",
|
||||
info->blit_sw_A);
|
||||
print_tf_state(" accelerated color fills", info->blit_fill);
|
||||
printf(" video memory: (%d)\n", info->video_mem);
|
||||
}
|
||||
@@ -68,63 +77,58 @@ static void output_videoinfo_details(void)
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static void output_surface_details(const char *name, SDL_Surface *surface)
|
||||
static void
|
||||
output_surface_details(const char *name, SDL_Surface * surface)
|
||||
{
|
||||
printf("Details for %s:\n", name);
|
||||
|
||||
if (surface == NULL)
|
||||
{
|
||||
if (surface == NULL) {
|
||||
printf("-WARNING- You've got a NULL surface!");
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
char f[256];
|
||||
printf(" width : %d\n", surface->w);
|
||||
printf(" height : %d\n", surface->h);
|
||||
printf(" depth : %d bits per pixel\n", surface->format->BitsPerPixel);
|
||||
printf(" depth : %d bits per pixel\n",
|
||||
surface->format->BitsPerPixel);
|
||||
printf(" pitch : %d\n", (int) surface->pitch);
|
||||
printf(" alpha : %d\n", (int) surface->format->alpha);
|
||||
printf(" colorkey : 0x%X\n", (unsigned int) surface->format->colorkey);
|
||||
printf(" colorkey : 0x%X\n",
|
||||
(unsigned int) surface->format->colorkey);
|
||||
|
||||
printf(" red bits : 0x%08X mask, %d shift, %d loss\n",
|
||||
(int) surface->format->Rmask,
|
||||
(int) surface->format->Rshift,
|
||||
(int) surface->format->Rloss);
|
||||
(int) surface->format->Rmask,
|
||||
(int) surface->format->Rshift, (int) surface->format->Rloss);
|
||||
printf(" green bits : 0x%08X mask, %d shift, %d loss\n",
|
||||
(int) surface->format->Gmask,
|
||||
(int) surface->format->Gshift,
|
||||
(int) surface->format->Gloss);
|
||||
(int) surface->format->Gmask,
|
||||
(int) surface->format->Gshift, (int) surface->format->Gloss);
|
||||
printf(" blue bits : 0x%08X mask, %d shift, %d loss\n",
|
||||
(int) surface->format->Bmask,
|
||||
(int) surface->format->Bshift,
|
||||
(int) surface->format->Bloss);
|
||||
(int) surface->format->Bmask,
|
||||
(int) surface->format->Bshift, (int) surface->format->Bloss);
|
||||
printf(" alpha bits : 0x%08X mask, %d shift, %d loss\n",
|
||||
(int) surface->format->Amask,
|
||||
(int) surface->format->Ashift,
|
||||
(int) surface->format->Aloss);
|
||||
(int) surface->format->Amask,
|
||||
(int) surface->format->Ashift, (int) surface->format->Aloss);
|
||||
|
||||
f[0] = '\0';
|
||||
|
||||
/*append_sdl_surface_flag(surface, f, sizeof (f), SDL_SWSURFACE);*/
|
||||
/*append_sdl_surface_flag(surface, f, sizeof (f), SDL_SWSURFACE); */
|
||||
if ((surface->flags & SDL_HWSURFACE) == 0)
|
||||
copy_trunc_str(f, sizeof (f), " SDL_SWSURFACE");
|
||||
copy_trunc_str(f, sizeof(f), " SDL_SWSURFACE");
|
||||
|
||||
append_sdl_surface_flag(surface, f, sizeof (f), SDL_HWSURFACE);
|
||||
append_sdl_surface_flag(surface, f, sizeof (f), SDL_ASYNCBLIT);
|
||||
append_sdl_surface_flag(surface, f, sizeof (f), SDL_ANYFORMAT);
|
||||
append_sdl_surface_flag(surface, f, sizeof (f), SDL_HWPALETTE);
|
||||
append_sdl_surface_flag(surface, f, sizeof (f), SDL_DOUBLEBUF);
|
||||
append_sdl_surface_flag(surface, f, sizeof (f), SDL_FULLSCREEN);
|
||||
append_sdl_surface_flag(surface, f, sizeof (f), SDL_OPENGL);
|
||||
append_sdl_surface_flag(surface, f, sizeof (f), SDL_OPENGLBLIT);
|
||||
append_sdl_surface_flag(surface, f, sizeof (f), SDL_RESIZABLE);
|
||||
append_sdl_surface_flag(surface, f, sizeof (f), SDL_NOFRAME);
|
||||
append_sdl_surface_flag(surface, f, sizeof (f), SDL_HWACCEL);
|
||||
append_sdl_surface_flag(surface, f, sizeof (f), SDL_SRCCOLORKEY);
|
||||
append_sdl_surface_flag(surface, f, sizeof (f), SDL_RLEACCELOK);
|
||||
append_sdl_surface_flag(surface, f, sizeof (f), SDL_RLEACCEL);
|
||||
append_sdl_surface_flag(surface, f, sizeof (f), SDL_SRCALPHA);
|
||||
append_sdl_surface_flag(surface, f, sizeof (f), SDL_PREALLOC);
|
||||
append_sdl_surface_flag(surface, f, sizeof(f), SDL_HWSURFACE);
|
||||
append_sdl_surface_flag(surface, f, sizeof(f), SDL_ASYNCBLIT);
|
||||
append_sdl_surface_flag(surface, f, sizeof(f), SDL_ANYFORMAT);
|
||||
append_sdl_surface_flag(surface, f, sizeof(f), SDL_HWPALETTE);
|
||||
append_sdl_surface_flag(surface, f, sizeof(f), SDL_DOUBLEBUF);
|
||||
append_sdl_surface_flag(surface, f, sizeof(f), SDL_FULLSCREEN);
|
||||
append_sdl_surface_flag(surface, f, sizeof(f), SDL_OPENGL);
|
||||
append_sdl_surface_flag(surface, f, sizeof(f), SDL_RESIZABLE);
|
||||
append_sdl_surface_flag(surface, f, sizeof(f), SDL_NOFRAME);
|
||||
append_sdl_surface_flag(surface, f, sizeof(f), SDL_HWACCEL);
|
||||
append_sdl_surface_flag(surface, f, sizeof(f), SDL_SRCCOLORKEY);
|
||||
append_sdl_surface_flag(surface, f, sizeof(f), SDL_RLEACCELOK);
|
||||
append_sdl_surface_flag(surface, f, sizeof(f), SDL_RLEACCEL);
|
||||
append_sdl_surface_flag(surface, f, sizeof(f), SDL_SRCALPHA);
|
||||
append_sdl_surface_flag(surface, f, sizeof(f), SDL_PREALLOC);
|
||||
|
||||
if (f[0] == '\0')
|
||||
strcpy(f, " (none)");
|
||||
@@ -135,14 +139,16 @@ static void output_surface_details(const char *name, SDL_Surface *surface)
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static void output_details(void)
|
||||
static void
|
||||
output_details(void)
|
||||
{
|
||||
output_videoinfo_details();
|
||||
output_surface_details("Source Surface", src);
|
||||
output_surface_details("Destination Surface", dest);
|
||||
}
|
||||
|
||||
static Uint32 blit(SDL_Surface *dst, SDL_Surface *src, int x, int y)
|
||||
static Uint32
|
||||
blit(SDL_Surface * dst, SDL_Surface * src, int x, int y)
|
||||
{
|
||||
Uint32 start = 0;
|
||||
SDL_Rect srcRect;
|
||||
@@ -152,39 +158,41 @@ static Uint32 blit(SDL_Surface *dst, SDL_Surface *src, int x, int y)
|
||||
srcRect.y = 0;
|
||||
dstRect.x = x;
|
||||
dstRect.y = y;
|
||||
dstRect.w = srcRect.w = src->w; /* SDL will clip as appropriate. */
|
||||
dstRect.w = srcRect.w = src->w; /* SDL will clip as appropriate. */
|
||||
dstRect.h = srcRect.h = src->h;
|
||||
|
||||
start = SDL_GetTicks();
|
||||
SDL_BlitSurface(src, &srcRect, dst, &dstRect);
|
||||
return(SDL_GetTicks() - start);
|
||||
return (SDL_GetTicks() - start);
|
||||
}
|
||||
|
||||
static void blitCentered(SDL_Surface *dst, SDL_Surface *src)
|
||||
static void
|
||||
blitCentered(SDL_Surface * dst, SDL_Surface * src)
|
||||
{
|
||||
int x = (dst->w - src->w) / 2;
|
||||
int y = (dst->h - src->h) / 2;
|
||||
blit(dst, src, x, y);
|
||||
}
|
||||
|
||||
static int atoi_hex(const char *str)
|
||||
static int
|
||||
atoi_hex(const char *str)
|
||||
{
|
||||
if (str == NULL)
|
||||
return 0;
|
||||
|
||||
if (strlen(str) > 2)
|
||||
{
|
||||
if (strlen(str) > 2) {
|
||||
int retval = 0;
|
||||
if ((str[0] == '0') && (str[1] == 'x'))
|
||||
sscanf(str + 2, "%X", &retval);
|
||||
return(retval);
|
||||
return (retval);
|
||||
}
|
||||
|
||||
return(atoi(str));
|
||||
return (atoi(str));
|
||||
}
|
||||
|
||||
|
||||
static int setup_test(int argc, char **argv)
|
||||
static int
|
||||
setup_test(int argc, char **argv)
|
||||
{
|
||||
const char *dumpfile = NULL;
|
||||
SDL_Surface *bmp = NULL;
|
||||
@@ -213,8 +221,7 @@ static int setup_test(int argc, char **argv)
|
||||
int screenSurface = 0;
|
||||
int i = 0;
|
||||
|
||||
for (i = 1; i < argc; i++)
|
||||
{
|
||||
for (i = 1; i < argc; i++) {
|
||||
const char *arg = argv[i];
|
||||
|
||||
if (strcmp(arg, "--dstbpp") == 0)
|
||||
@@ -256,63 +263,59 @@ static int setup_test(int argc, char **argv)
|
||||
else if (strcmp(arg, "--dumpfile") == 0)
|
||||
dumpfile = argv[++i];
|
||||
/* !!! FIXME: set colorkey. */
|
||||
else if (0) /* !!! FIXME: we handle some commandlines elsewhere now */
|
||||
{
|
||||
else if (0) { /* !!! FIXME: we handle some commandlines elsewhere now */
|
||||
fprintf(stderr, "Unknown commandline option: %s\n", arg);
|
||||
return(0);
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
|
||||
if (SDL_Init(SDL_INIT_VIDEO) == -1)
|
||||
{
|
||||
if (SDL_Init(SDL_INIT_VIDEO) == -1) {
|
||||
fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
|
||||
return(0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
bmp = SDL_LoadBMP("sample.bmp");
|
||||
if (bmp == NULL)
|
||||
{
|
||||
if (bmp == NULL) {
|
||||
fprintf(stderr, "SDL_LoadBMP failed: %s\n", SDL_GetError());
|
||||
SDL_Quit();
|
||||
return(0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if ((dstflags & SDL_HWSURFACE) == 0) dstflags |= SDL_SWSURFACE;
|
||||
if ((srcflags & SDL_HWSURFACE) == 0) srcflags |= SDL_SWSURFACE;
|
||||
if ((dstflags & SDL_HWSURFACE) == 0)
|
||||
dstflags |= SDL_SWSURFACE;
|
||||
if ((srcflags & SDL_HWSURFACE) == 0)
|
||||
srcflags |= SDL_SWSURFACE;
|
||||
|
||||
if (screenSurface)
|
||||
dest = SDL_SetVideoMode(dstw, dsth, dstbpp, dstflags);
|
||||
else
|
||||
{
|
||||
else {
|
||||
dest = SDL_CreateRGBSurface(dstflags, dstw, dsth, dstbpp,
|
||||
dstrmask, dstgmask, dstbmask, dstamask);
|
||||
}
|
||||
|
||||
if (dest == NULL)
|
||||
{
|
||||
if (dest == NULL) {
|
||||
fprintf(stderr, "dest surface creation failed: %s\n", SDL_GetError());
|
||||
SDL_Quit();
|
||||
return(0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
src = SDL_CreateRGBSurface(srcflags, srcw, srch, srcbpp,
|
||||
srcrmask, srcgmask, srcbmask, srcamask);
|
||||
if (src == NULL)
|
||||
{
|
||||
if (src == NULL) {
|
||||
fprintf(stderr, "src surface creation failed: %s\n", SDL_GetError());
|
||||
SDL_Quit();
|
||||
return(0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* handle alpha settings... */
|
||||
srcalphaflags = (src->flags&SDL_SRCALPHA) | (src->flags&SDL_RLEACCEL);
|
||||
dstalphaflags = (dest->flags&SDL_SRCALPHA) | (dest->flags&SDL_RLEACCEL);
|
||||
srcalphaflags = (src->flags & SDL_SRCALPHA) | (src->flags & SDL_RLEACCEL);
|
||||
dstalphaflags =
|
||||
(dest->flags & SDL_SRCALPHA) | (dest->flags & SDL_RLEACCEL);
|
||||
origsrcalphaflags = srcalphaflags;
|
||||
origdstalphaflags = dstalphaflags;
|
||||
srcalpha = src->format->alpha;
|
||||
dstalpha = dest->format->alpha;
|
||||
for (i = 1; i < argc; i++)
|
||||
{
|
||||
for (i = 1; i < argc; i++) {
|
||||
const char *arg = argv[i];
|
||||
|
||||
if (strcmp(arg, "--srcalpha") == 0)
|
||||
@@ -336,9 +339,11 @@ static int setup_test(int argc, char **argv)
|
||||
else if (strcmp(arg, "--dstnorleaccel") == 0)
|
||||
dstalphaflags &= ~SDL_RLEACCEL;
|
||||
}
|
||||
if ((dstalphaflags != origdstalphaflags) || (dstalpha != dest->format->alpha))
|
||||
if ((dstalphaflags != origdstalphaflags)
|
||||
|| (dstalpha != dest->format->alpha))
|
||||
SDL_SetAlpha(dest, dstalphaflags, (Uint8) dstalpha);
|
||||
if ((srcalphaflags != origsrcalphaflags) || (srcalpha != src->format->alpha))
|
||||
if ((srcalphaflags != origsrcalphaflags)
|
||||
|| (srcalpha != src->format->alpha))
|
||||
SDL_SetAlpha(src, srcalphaflags, (Uint8) srcalpha);
|
||||
|
||||
/* set some sane defaults so we can see if the blit code is broken... */
|
||||
@@ -349,15 +354,16 @@ static int setup_test(int argc, char **argv)
|
||||
SDL_FreeSurface(bmp);
|
||||
|
||||
if (dumpfile)
|
||||
SDL_SaveBMP(src, dumpfile); /* make sure initial convert is sane. */
|
||||
SDL_SaveBMP(src, dumpfile); /* make sure initial convert is sane. */
|
||||
|
||||
output_details();
|
||||
|
||||
return(1);
|
||||
return (1);
|
||||
}
|
||||
|
||||
|
||||
static void test_blit_speed(void)
|
||||
static void
|
||||
test_blit_speed(void)
|
||||
{
|
||||
Uint32 clearColor = SDL_MapRGB(dest->format, 0, 0, 0);
|
||||
Uint32 iterations = 0;
|
||||
@@ -376,45 +382,43 @@ static void test_blit_speed(void)
|
||||
now = SDL_GetTicks();
|
||||
end = now + testms;
|
||||
|
||||
do
|
||||
{
|
||||
do {
|
||||
/* pump the event queue occasionally to keep OS happy... */
|
||||
if (now - last > 1000)
|
||||
{
|
||||
if (now - last > 1000) {
|
||||
last = now;
|
||||
while (SDL_PollEvent(&event)) { /* no-op. */ }
|
||||
while (SDL_PollEvent(&event)) { /* no-op. */
|
||||
}
|
||||
}
|
||||
|
||||
iterations++;
|
||||
elasped += blit(dest, src, randRange(0, wmax), randRange(0, hmax));
|
||||
if (isScreen)
|
||||
{
|
||||
SDL_Flip(dest); /* show it! */
|
||||
SDL_FillRect(dest, NULL, clearColor); /* blank it for next time! */
|
||||
if (isScreen) {
|
||||
SDL_Flip(dest); /* show it! */
|
||||
SDL_FillRect(dest, NULL, clearColor); /* blank it for next time! */
|
||||
}
|
||||
|
||||
now = SDL_GetTicks();
|
||||
} while (now < end);
|
||||
}
|
||||
while (now < end);
|
||||
|
||||
printf("Non-blitting crap accounted for %d percent of this run.\n",
|
||||
percent(testms - elasped, testms));
|
||||
percent(testms - elasped, testms));
|
||||
|
||||
printf("%d blits took %d ms (%d fps).\n",
|
||||
(int) iterations,
|
||||
(int) elasped,
|
||||
(int) (((float)iterations) / (((float)elasped) / 1000.0f)));
|
||||
(int) iterations,
|
||||
(int) elasped,
|
||||
(int) (((float) iterations) / (((float) elasped) / 1000.0f)));
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
int initialized = setup_test(argc, argv);
|
||||
if (initialized)
|
||||
{
|
||||
if (initialized) {
|
||||
test_blit_speed();
|
||||
SDL_Quit();
|
||||
}
|
||||
return(!initialized);
|
||||
return (!initialized);
|
||||
}
|
||||
|
||||
/* end of testblitspeed.c ... */
|
||||
|
||||
|
||||
344
test/testcdrom.c
344
test/testcdrom.c
@@ -9,201 +9,195 @@
|
||||
#include "SDL.h"
|
||||
|
||||
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
||||
static void quit(int rc)
|
||||
static void
|
||||
quit(int rc)
|
||||
{
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
}
|
||||
|
||||
static void PrintStatus(int driveindex, SDL_CD *cdrom)
|
||||
static void
|
||||
PrintStatus(int driveindex, SDL_CD * cdrom)
|
||||
{
|
||||
CDstatus status;
|
||||
char *status_str;
|
||||
CDstatus status;
|
||||
char *status_str;
|
||||
|
||||
status = SDL_CDStatus(cdrom);
|
||||
switch (status) {
|
||||
case CD_TRAYEMPTY:
|
||||
status_str = "tray empty";
|
||||
break;
|
||||
case CD_STOPPED:
|
||||
status_str = "stopped";
|
||||
break;
|
||||
case CD_PLAYING:
|
||||
status_str = "playing";
|
||||
break;
|
||||
case CD_PAUSED:
|
||||
status_str = "paused";
|
||||
break;
|
||||
case CD_ERROR:
|
||||
status_str = "error state";
|
||||
break;
|
||||
}
|
||||
printf("Drive %d status: %s\n", driveindex, status_str);
|
||||
if ( status >= CD_PLAYING ) {
|
||||
int m, s, f;
|
||||
FRAMES_TO_MSF(cdrom->cur_frame, &m, &s, &f);
|
||||
printf("Currently playing track %d, %d:%2.2d\n",
|
||||
cdrom->track[cdrom->cur_track].id, m, s);
|
||||
}
|
||||
status = SDL_CDStatus(cdrom);
|
||||
switch (status) {
|
||||
case CD_TRAYEMPTY:
|
||||
status_str = "tray empty";
|
||||
break;
|
||||
case CD_STOPPED:
|
||||
status_str = "stopped";
|
||||
break;
|
||||
case CD_PLAYING:
|
||||
status_str = "playing";
|
||||
break;
|
||||
case CD_PAUSED:
|
||||
status_str = "paused";
|
||||
break;
|
||||
case CD_ERROR:
|
||||
status_str = "error state";
|
||||
break;
|
||||
}
|
||||
printf("Drive %d status: %s\n", driveindex, status_str);
|
||||
if (status >= CD_PLAYING) {
|
||||
int m, s, f;
|
||||
FRAMES_TO_MSF(cdrom->cur_frame, &m, &s, &f);
|
||||
printf("Currently playing track %d, %d:%2.2d\n",
|
||||
cdrom->track[cdrom->cur_track].id, m, s);
|
||||
}
|
||||
}
|
||||
|
||||
static void ListTracks(SDL_CD *cdrom)
|
||||
static void
|
||||
ListTracks(SDL_CD * cdrom)
|
||||
{
|
||||
int i;
|
||||
int m, s, f;
|
||||
char* trtype;
|
||||
int i;
|
||||
int m, s, f;
|
||||
char *trtype;
|
||||
|
||||
SDL_CDStatus(cdrom);
|
||||
printf("Drive tracks: %d\n", cdrom->numtracks);
|
||||
for ( i=0; i<cdrom->numtracks; ++i ) {
|
||||
FRAMES_TO_MSF(cdrom->track[i].length, &m, &s, &f);
|
||||
if ( f > 0 )
|
||||
++s;
|
||||
switch(cdrom->track[i].type)
|
||||
{
|
||||
case SDL_AUDIO_TRACK:
|
||||
trtype="audio";
|
||||
break;
|
||||
case SDL_DATA_TRACK:
|
||||
trtype="data";
|
||||
break;
|
||||
default:
|
||||
trtype="unknown";
|
||||
break;
|
||||
}
|
||||
printf("\tTrack (index %d) %d: %d:%2.2d / %d [%s track]\n", i,
|
||||
cdrom->track[i].id, m, s, cdrom->track[i].length, trtype);
|
||||
}
|
||||
SDL_CDStatus(cdrom);
|
||||
printf("Drive tracks: %d\n", cdrom->numtracks);
|
||||
for (i = 0; i < cdrom->numtracks; ++i) {
|
||||
FRAMES_TO_MSF(cdrom->track[i].length, &m, &s, &f);
|
||||
if (f > 0)
|
||||
++s;
|
||||
switch (cdrom->track[i].type) {
|
||||
case SDL_AUDIO_TRACK:
|
||||
trtype = "audio";
|
||||
break;
|
||||
case SDL_DATA_TRACK:
|
||||
trtype = "data";
|
||||
break;
|
||||
default:
|
||||
trtype = "unknown";
|
||||
break;
|
||||
}
|
||||
printf("\tTrack (index %d) %d: %d:%2.2d / %d [%s track]\n", i,
|
||||
cdrom->track[i].id, m, s, cdrom->track[i].length, trtype);
|
||||
}
|
||||
}
|
||||
|
||||
static void PrintUsage(char *argv0)
|
||||
static void
|
||||
PrintUsage(char *argv0)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s [drive#] [command] [command] ...\n", argv0);
|
||||
fprintf(stderr, "Where 'command' is one of:\n");
|
||||
fprintf(stderr, " -status\n");
|
||||
fprintf(stderr, " -list\n");
|
||||
fprintf(stderr, " -play [first_track] [first_frame] [num_tracks] [num_frames]\n");
|
||||
fprintf(stderr, " -pause\n");
|
||||
fprintf(stderr, " -resume\n");
|
||||
fprintf(stderr, " -stop\n");
|
||||
fprintf(stderr, " -eject\n");
|
||||
fprintf(stderr, " -sleep <milliseconds>\n");
|
||||
fprintf(stderr, "Usage: %s [drive#] [command] [command] ...\n", argv0);
|
||||
fprintf(stderr, "Where 'command' is one of:\n");
|
||||
fprintf(stderr, " -status\n");
|
||||
fprintf(stderr, " -list\n");
|
||||
fprintf(stderr,
|
||||
" -play [first_track] [first_frame] [num_tracks] [num_frames]\n");
|
||||
fprintf(stderr, " -pause\n");
|
||||
fprintf(stderr, " -resume\n");
|
||||
fprintf(stderr, " -stop\n");
|
||||
fprintf(stderr, " -eject\n");
|
||||
fprintf(stderr, " -sleep <milliseconds>\n");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int drive;
|
||||
int i;
|
||||
SDL_CD *cdrom;
|
||||
int drive;
|
||||
int i;
|
||||
SDL_CD *cdrom;
|
||||
|
||||
/* Initialize SDL first */
|
||||
if ( SDL_Init(SDL_INIT_CDROM) < 0 ) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
|
||||
return(1);
|
||||
}
|
||||
/* Initialize SDL first */
|
||||
if (SDL_Init(SDL_INIT_CDROM) < 0) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return (1);
|
||||
}
|
||||
|
||||
/* Find out how many CD-ROM drives are connected to the system */
|
||||
if ( SDL_CDNumDrives() == 0 ) {
|
||||
printf("No CD-ROM devices detected\n");
|
||||
quit(0);
|
||||
}
|
||||
printf("Drives available: %d\n", SDL_CDNumDrives());
|
||||
for ( i=0; i<SDL_CDNumDrives(); ++i ) {
|
||||
printf("Drive %d: \"%s\"\n", i, SDL_CDName(i));
|
||||
}
|
||||
/* Find out how many CD-ROM drives are connected to the system */
|
||||
if (SDL_CDNumDrives() == 0) {
|
||||
printf("No CD-ROM devices detected\n");
|
||||
quit(0);
|
||||
}
|
||||
printf("Drives available: %d\n", SDL_CDNumDrives());
|
||||
for (i = 0; i < SDL_CDNumDrives(); ++i) {
|
||||
printf("Drive %d: \"%s\"\n", i, SDL_CDName(i));
|
||||
}
|
||||
|
||||
/* Open the CD-ROM */
|
||||
drive = 0;
|
||||
i=1;
|
||||
if ( argv[i] && isdigit(argv[i][0]) ) {
|
||||
drive = atoi(argv[i++]);
|
||||
}
|
||||
cdrom = SDL_CDOpen(drive);
|
||||
if ( cdrom == NULL ) {
|
||||
fprintf(stderr, "Couldn't open drive %d: %s\n", drive,
|
||||
SDL_GetError());
|
||||
quit(2);
|
||||
}
|
||||
/* Open the CD-ROM */
|
||||
drive = 0;
|
||||
i = 1;
|
||||
if (argv[i] && isdigit(argv[i][0])) {
|
||||
drive = atoi(argv[i++]);
|
||||
}
|
||||
cdrom = SDL_CDOpen(drive);
|
||||
if (cdrom == NULL) {
|
||||
fprintf(stderr, "Couldn't open drive %d: %s\n", drive,
|
||||
SDL_GetError());
|
||||
quit(2);
|
||||
}
|
||||
#ifdef TEST_NULLCD
|
||||
cdrom = NULL;
|
||||
cdrom = NULL;
|
||||
#endif
|
||||
|
||||
/* Find out which function to perform */
|
||||
for ( ; argv[i]; ++i ) {
|
||||
if ( strcmp(argv[i], "-status") == 0 ) {
|
||||
/* PrintStatus(drive, cdrom); */
|
||||
} else
|
||||
if ( strcmp(argv[i], "-list") == 0 ) {
|
||||
ListTracks(cdrom);
|
||||
} else
|
||||
if ( strcmp(argv[i], "-play") == 0 ) {
|
||||
int strack, sframe;
|
||||
int ntrack, nframe;
|
||||
|
||||
strack = 0;
|
||||
if ( argv[i+1] && isdigit(argv[i+1][0]) ) {
|
||||
strack = atoi(argv[++i]);
|
||||
}
|
||||
sframe = 0;
|
||||
if ( argv[i+1] && isdigit(argv[i+1][0]) ) {
|
||||
sframe = atoi(argv[++i]);
|
||||
}
|
||||
ntrack = 0;
|
||||
if ( argv[i+1] && isdigit(argv[i+1][0]) ) {
|
||||
ntrack = atoi(argv[++i]);
|
||||
}
|
||||
nframe = 0;
|
||||
if ( argv[i+1] && isdigit(argv[i+1][0]) ) {
|
||||
nframe = atoi(argv[++i]);
|
||||
}
|
||||
if ( CD_INDRIVE(SDL_CDStatus(cdrom)) ) {
|
||||
if ( SDL_CDPlayTracks(cdrom, strack, sframe,
|
||||
ntrack, nframe) < 0 ) {
|
||||
fprintf(stderr,
|
||||
"Couldn't play tracks %d/%d for %d/%d: %s\n",
|
||||
strack, sframe, ntrack, nframe, SDL_GetError());
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "No CD in drive!\n");
|
||||
}
|
||||
} else
|
||||
if ( strcmp(argv[i], "-pause") == 0 ) {
|
||||
if ( SDL_CDPause(cdrom) < 0 ) {
|
||||
fprintf(stderr, "Couldn't pause CD: %s\n",
|
||||
SDL_GetError());
|
||||
}
|
||||
} else
|
||||
if ( strcmp(argv[i], "-resume") == 0 ) {
|
||||
if ( SDL_CDResume(cdrom) < 0 ) {
|
||||
fprintf(stderr, "Couldn't resume CD: %s\n",
|
||||
SDL_GetError());
|
||||
}
|
||||
} else
|
||||
if ( strcmp(argv[i], "-stop") == 0 ) {
|
||||
if ( SDL_CDStop(cdrom) < 0 ) {
|
||||
fprintf(stderr, "Couldn't eject CD: %s\n",
|
||||
SDL_GetError());
|
||||
}
|
||||
} else
|
||||
if ( strcmp(argv[i], "-eject") == 0 ) {
|
||||
if ( SDL_CDEject(cdrom) < 0 ) {
|
||||
fprintf(stderr, "Couldn't eject CD: %s\n",
|
||||
SDL_GetError());
|
||||
}
|
||||
} else
|
||||
if ( (strcmp(argv[i], "-sleep") == 0) &&
|
||||
(argv[i+1] && isdigit(argv[i+1][0])) ) {
|
||||
SDL_Delay(atoi(argv[++i]));
|
||||
printf("Delayed %d milliseconds\n", atoi(argv[i]));
|
||||
} else {
|
||||
PrintUsage(argv[0]);
|
||||
SDL_CDClose(cdrom);
|
||||
quit(1);
|
||||
}
|
||||
}
|
||||
PrintStatus(drive, cdrom);
|
||||
SDL_CDClose(cdrom);
|
||||
SDL_Quit();
|
||||
/* Find out which function to perform */
|
||||
for (; argv[i]; ++i) {
|
||||
if (strcmp(argv[i], "-status") == 0) {
|
||||
/* PrintStatus(drive, cdrom); */
|
||||
} else if (strcmp(argv[i], "-list") == 0) {
|
||||
ListTracks(cdrom);
|
||||
} else if (strcmp(argv[i], "-play") == 0) {
|
||||
int strack, sframe;
|
||||
int ntrack, nframe;
|
||||
|
||||
return(0);
|
||||
strack = 0;
|
||||
if (argv[i + 1] && isdigit(argv[i + 1][0])) {
|
||||
strack = atoi(argv[++i]);
|
||||
}
|
||||
sframe = 0;
|
||||
if (argv[i + 1] && isdigit(argv[i + 1][0])) {
|
||||
sframe = atoi(argv[++i]);
|
||||
}
|
||||
ntrack = 0;
|
||||
if (argv[i + 1] && isdigit(argv[i + 1][0])) {
|
||||
ntrack = atoi(argv[++i]);
|
||||
}
|
||||
nframe = 0;
|
||||
if (argv[i + 1] && isdigit(argv[i + 1][0])) {
|
||||
nframe = atoi(argv[++i]);
|
||||
}
|
||||
if (CD_INDRIVE(SDL_CDStatus(cdrom))) {
|
||||
if (SDL_CDPlayTracks(cdrom, strack, sframe,
|
||||
ntrack, nframe) < 0) {
|
||||
fprintf(stderr,
|
||||
"Couldn't play tracks %d/%d for %d/%d: %s\n",
|
||||
strack, sframe, ntrack, nframe, SDL_GetError());
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "No CD in drive!\n");
|
||||
}
|
||||
} else if (strcmp(argv[i], "-pause") == 0) {
|
||||
if (SDL_CDPause(cdrom) < 0) {
|
||||
fprintf(stderr, "Couldn't pause CD: %s\n", SDL_GetError());
|
||||
}
|
||||
} else if (strcmp(argv[i], "-resume") == 0) {
|
||||
if (SDL_CDResume(cdrom) < 0) {
|
||||
fprintf(stderr, "Couldn't resume CD: %s\n", SDL_GetError());
|
||||
}
|
||||
} else if (strcmp(argv[i], "-stop") == 0) {
|
||||
if (SDL_CDStop(cdrom) < 0) {
|
||||
fprintf(stderr, "Couldn't eject CD: %s\n", SDL_GetError());
|
||||
}
|
||||
} else if (strcmp(argv[i], "-eject") == 0) {
|
||||
if (SDL_CDEject(cdrom) < 0) {
|
||||
fprintf(stderr, "Couldn't eject CD: %s\n", SDL_GetError());
|
||||
}
|
||||
} else if ((strcmp(argv[i], "-sleep") == 0) &&
|
||||
(argv[i + 1] && isdigit(argv[i + 1][0]))) {
|
||||
SDL_Delay(atoi(argv[++i]));
|
||||
printf("Delayed %d milliseconds\n", atoi(argv[i]));
|
||||
} else {
|
||||
PrintUsage(argv[0]);
|
||||
SDL_CDClose(cdrom);
|
||||
quit(1);
|
||||
}
|
||||
}
|
||||
PrintStatus(drive, cdrom);
|
||||
SDL_CDClose(cdrom);
|
||||
SDL_Quit();
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
@@ -11,205 +11,213 @@
|
||||
(swap left and right for different endianness)
|
||||
*/
|
||||
|
||||
Uint16 cursor_data[16]={
|
||||
0xffff,
|
||||
0xffff,
|
||||
0xffff,
|
||||
0xffff,
|
||||
Uint16 cursor_data[16] = {
|
||||
0xffff,
|
||||
0xffff,
|
||||
0xffff,
|
||||
0xffff,
|
||||
|
||||
0xffff,
|
||||
0xffff,
|
||||
0xffff,
|
||||
0xffff,
|
||||
0xffff,
|
||||
0xffff,
|
||||
0xffff,
|
||||
0xffff,
|
||||
|
||||
0x0000,
|
||||
0x0000,
|
||||
0x0000,
|
||||
0x0000,
|
||||
0x0000,
|
||||
0x0000,
|
||||
0x0000,
|
||||
0x0000,
|
||||
|
||||
0x0000,
|
||||
0x0000,
|
||||
0x0000,
|
||||
0x0000
|
||||
0x0000,
|
||||
0x0000,
|
||||
0x0000,
|
||||
0x0000
|
||||
};
|
||||
|
||||
Uint16 cursor_mask[16]={
|
||||
0xff00,
|
||||
0xff00,
|
||||
0xff00,
|
||||
0xff00,
|
||||
Uint16 cursor_mask[16] = {
|
||||
0xff00,
|
||||
0xff00,
|
||||
0xff00,
|
||||
0xff00,
|
||||
|
||||
0xff00,
|
||||
0xff00,
|
||||
0xff00,
|
||||
0xff00,
|
||||
0xff00,
|
||||
0xff00,
|
||||
0xff00,
|
||||
0xff00,
|
||||
|
||||
0xff00,
|
||||
0xff00,
|
||||
0xff00,
|
||||
0xff00,
|
||||
0xff00,
|
||||
0xff00,
|
||||
0xff00,
|
||||
0xff00,
|
||||
|
||||
0xff00,
|
||||
0xff00,
|
||||
0xff00,
|
||||
0xff00
|
||||
0xff00,
|
||||
0xff00,
|
||||
0xff00,
|
||||
0xff00
|
||||
};
|
||||
|
||||
/* another test cursor: smaller than 16x16, and with an odd height */
|
||||
|
||||
Uint8 small_cursor_data[11] = { 0x00, 0x18, 0x08, 0x38, 0x44, 0x54, 0x44, 0x38, 0x20, 0x20, 0x00 };
|
||||
Uint8 small_cursor_mask[11] = { 0x3C, 0x3C, 0x3C, 0x7C, 0xC6, 0xC6, 0xC6, 0x7C, 0x78, 0x70, 0x70 };
|
||||
Uint8 small_cursor_data[11] =
|
||||
{ 0x00, 0x18, 0x08, 0x38, 0x44, 0x54, 0x44, 0x38, 0x20, 0x20, 0x00 };
|
||||
Uint8 small_cursor_mask[11] =
|
||||
{ 0x3C, 0x3C, 0x3C, 0x7C, 0xC6, 0xC6, 0xC6, 0x7C, 0x78, 0x70, 0x70 };
|
||||
|
||||
/* XPM */
|
||||
static const char *arrow[] = {
|
||||
/* width height num_colors chars_per_pixel */
|
||||
" 32 32 3 1",
|
||||
/* colors */
|
||||
"X c #000000",
|
||||
". c #ffffff",
|
||||
" c None",
|
||||
/* pixels */
|
||||
"X ",
|
||||
"XX ",
|
||||
"X.X ",
|
||||
"X..X ",
|
||||
"X...X ",
|
||||
"X....X ",
|
||||
"X.....X ",
|
||||
"X......X ",
|
||||
"X.......X ",
|
||||
"X........X ",
|
||||
"X.....XXXXX ",
|
||||
"X..X..X ",
|
||||
"X.X X..X ",
|
||||
"XX X..X ",
|
||||
"X X..X ",
|
||||
" X..X ",
|
||||
" X..X ",
|
||||
" X..X ",
|
||||
" XX ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
"0,0"
|
||||
/* width height num_colors chars_per_pixel */
|
||||
" 32 32 3 1",
|
||||
/* colors */
|
||||
"X c #000000",
|
||||
". c #ffffff",
|
||||
" c None",
|
||||
/* pixels */
|
||||
"X ",
|
||||
"XX ",
|
||||
"X.X ",
|
||||
"X..X ",
|
||||
"X...X ",
|
||||
"X....X ",
|
||||
"X.....X ",
|
||||
"X......X ",
|
||||
"X.......X ",
|
||||
"X........X ",
|
||||
"X.....XXXXX ",
|
||||
"X..X..X ",
|
||||
"X.X X..X ",
|
||||
"XX X..X ",
|
||||
"X X..X ",
|
||||
" X..X ",
|
||||
" X..X ",
|
||||
" X..X ",
|
||||
" XX ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
"0,0"
|
||||
};
|
||||
|
||||
static SDL_Cursor *create_arrow_cursor()
|
||||
static SDL_Cursor *
|
||||
create_arrow_cursor()
|
||||
{
|
||||
int i, row, col;
|
||||
Uint8 data[4*32];
|
||||
Uint8 mask[4*32];
|
||||
int hot_x, hot_y;
|
||||
int i, row, col;
|
||||
Uint8 data[4 * 32];
|
||||
Uint8 mask[4 * 32];
|
||||
int hot_x, hot_y;
|
||||
|
||||
i = -1;
|
||||
for ( row=0; row<32; ++row ) {
|
||||
for ( col=0; col<32; ++col ) {
|
||||
if ( col % 8 ) {
|
||||
data[i] <<= 1;
|
||||
mask[i] <<= 1;
|
||||
} else {
|
||||
++i;
|
||||
data[i] = mask[i] = 0;
|
||||
}
|
||||
switch (arrow[4+row][col]) {
|
||||
case 'X':
|
||||
data[i] |= 0x01;
|
||||
mask[i] |= 0x01;
|
||||
break;
|
||||
case '.':
|
||||
mask[i] |= 0x01;
|
||||
break;
|
||||
case ' ':
|
||||
break;
|
||||
}
|
||||
i = -1;
|
||||
for (row = 0; row < 32; ++row) {
|
||||
for (col = 0; col < 32; ++col) {
|
||||
if (col % 8) {
|
||||
data[i] <<= 1;
|
||||
mask[i] <<= 1;
|
||||
} else {
|
||||
++i;
|
||||
data[i] = mask[i] = 0;
|
||||
}
|
||||
switch (arrow[4 + row][col]) {
|
||||
case 'X':
|
||||
data[i] |= 0x01;
|
||||
mask[i] |= 0x01;
|
||||
break;
|
||||
case '.':
|
||||
mask[i] |= 0x01;
|
||||
break;
|
||||
case ' ':
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
sscanf(arrow[4+row], "%d,%d", &hot_x, &hot_y);
|
||||
return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
|
||||
sscanf(arrow[4 + row], "%d,%d", &hot_x, &hot_y);
|
||||
return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
SDL_Surface *screen;
|
||||
SDL_bool quit = SDL_FALSE, first_time = SDL_TRUE;
|
||||
SDL_Cursor *cursor[3];
|
||||
int current;
|
||||
SDL_Surface *screen;
|
||||
SDL_bool quit = SDL_FALSE, first_time = SDL_TRUE;
|
||||
SDL_Cursor *cursor[3];
|
||||
int current;
|
||||
|
||||
/* Load the SDL library */
|
||||
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
|
||||
return(1);
|
||||
}
|
||||
/* Load the SDL library */
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return (1);
|
||||
}
|
||||
|
||||
screen = SDL_SetVideoMode(320,200,8,SDL_ANYFORMAT);
|
||||
if (screen==NULL) {
|
||||
fprintf(stderr, "Couldn't initialize video mode: %s\n",SDL_GetError());
|
||||
return(1);
|
||||
}
|
||||
screen = SDL_SetVideoMode(320, 200, 8, SDL_ANYFORMAT);
|
||||
if (screen == NULL) {
|
||||
fprintf(stderr, "Couldn't initialize video mode: %s\n",
|
||||
SDL_GetError());
|
||||
return (1);
|
||||
}
|
||||
|
||||
SDL_FillRect(screen, NULL, 0x664422);
|
||||
SDL_FillRect(screen, NULL, 0x664422);
|
||||
|
||||
cursor[0] = SDL_CreateCursor((Uint8 *)cursor_data, (Uint8 *)cursor_mask,
|
||||
16, 16, 8, 8);
|
||||
if (cursor[0]==NULL) {
|
||||
fprintf(stderr, "Couldn't initialize test cursor: %s\n",SDL_GetError());
|
||||
SDL_Quit();
|
||||
return(1);
|
||||
}
|
||||
cursor[1] = create_arrow_cursor();
|
||||
if (cursor[1]==NULL) {
|
||||
fprintf(stderr, "Couldn't initialize arrow cursor: %s\n",SDL_GetError());
|
||||
SDL_FreeCursor(cursor[0]);
|
||||
SDL_Quit();
|
||||
return(1);
|
||||
}
|
||||
cursor[2] = SDL_CreateCursor(small_cursor_data, small_cursor_mask,
|
||||
8, 11, 3, 5);
|
||||
if (cursor[2]==NULL) {
|
||||
fprintf(stderr, "Couldn't initialize test cursor: %s\n",SDL_GetError());
|
||||
SDL_Quit();
|
||||
return(1);
|
||||
}
|
||||
cursor[0] = SDL_CreateCursor((Uint8 *) cursor_data, (Uint8 *) cursor_mask,
|
||||
16, 16, 8, 8);
|
||||
if (cursor[0] == NULL) {
|
||||
fprintf(stderr, "Couldn't initialize test cursor: %s\n",
|
||||
SDL_GetError());
|
||||
SDL_Quit();
|
||||
return (1);
|
||||
}
|
||||
cursor[1] = create_arrow_cursor();
|
||||
if (cursor[1] == NULL) {
|
||||
fprintf(stderr, "Couldn't initialize arrow cursor: %s\n",
|
||||
SDL_GetError());
|
||||
SDL_FreeCursor(cursor[0]);
|
||||
SDL_Quit();
|
||||
return (1);
|
||||
}
|
||||
cursor[2] = SDL_CreateCursor(small_cursor_data, small_cursor_mask,
|
||||
8, 11, 3, 5);
|
||||
if (cursor[2] == NULL) {
|
||||
fprintf(stderr, "Couldn't initialize test cursor: %s\n",
|
||||
SDL_GetError());
|
||||
SDL_Quit();
|
||||
return (1);
|
||||
}
|
||||
|
||||
current = 0;
|
||||
SDL_SetCursor(cursor[current]);
|
||||
current = 0;
|
||||
SDL_SetCursor(cursor[current]);
|
||||
|
||||
while (!quit) {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
switch(event.type) {
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
current = (current + 1)%3;
|
||||
SDL_SetCursor(cursor[current]);
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
if (event.key.keysym.sym == SDLK_ESCAPE) {
|
||||
quit = SDL_TRUE;
|
||||
}
|
||||
break;
|
||||
case SDL_QUIT:
|
||||
quit = SDL_TRUE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
SDL_Flip(screen);
|
||||
SDL_Delay(1);
|
||||
}
|
||||
while (!quit) {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
switch (event.type) {
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
current = (current + 1) % 3;
|
||||
SDL_SetCursor(cursor[current]);
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
if (event.key.keysym.sym == SDLK_ESCAPE) {
|
||||
quit = SDL_TRUE;
|
||||
}
|
||||
break;
|
||||
case SDL_QUIT:
|
||||
quit = SDL_TRUE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
SDL_Flip(screen);
|
||||
SDL_Delay(1);
|
||||
}
|
||||
|
||||
SDL_FreeCursor(cursor[0]);
|
||||
SDL_FreeCursor(cursor[1]);
|
||||
SDL_FreeCursor(cursor[0]);
|
||||
SDL_FreeCursor(cursor[1]);
|
||||
|
||||
SDL_Quit();
|
||||
return(0);
|
||||
SDL_Quit();
|
||||
return (0);
|
||||
}
|
||||
|
||||
287
test/testdyngl.c
287
test/testdyngl.c
@@ -28,182 +28,177 @@
|
||||
#include "SDL_opengl.h"
|
||||
|
||||
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
||||
static void quit(int rc)
|
||||
static void
|
||||
quit(int rc)
|
||||
{
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
}
|
||||
|
||||
void* get_funcaddr(const char* p)
|
||||
void *
|
||||
get_funcaddr(const char *p)
|
||||
{
|
||||
void* f=SDL_GL_GetProcAddress(p);
|
||||
if (f)
|
||||
{
|
||||
return f;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Unable to get function pointer for %s\n",p);
|
||||
quit(1);
|
||||
}
|
||||
return NULL;
|
||||
void *f = SDL_GL_GetProcAddress(p);
|
||||
if (f) {
|
||||
return f;
|
||||
} else {
|
||||
printf("Unable to get function pointer for %s\n", p);
|
||||
quit(1);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
typedef struct
|
||||
{
|
||||
void(APIENTRY*glBegin)(GLenum);
|
||||
void(APIENTRY*glEnd)();
|
||||
void(APIENTRY*glVertex3f)(GLfloat, GLfloat, GLfloat);
|
||||
void(APIENTRY*glClearColor)(GLfloat, GLfloat, GLfloat, GLfloat);
|
||||
void(APIENTRY*glClear)(GLbitfield);
|
||||
void(APIENTRY*glDisable)(GLenum);
|
||||
void(APIENTRY*glEnable)(GLenum);
|
||||
void(APIENTRY*glColor4ub)(GLubyte,GLubyte,GLubyte,GLubyte);
|
||||
void(APIENTRY*glPointSize)(GLfloat);
|
||||
void(APIENTRY*glHint)(GLenum,GLenum);
|
||||
void(APIENTRY*glBlendFunc)(GLenum,GLenum);
|
||||
void(APIENTRY*glMatrixMode)(GLenum);
|
||||
void(APIENTRY*glLoadIdentity)();
|
||||
void(APIENTRY*glOrtho)(GLdouble,GLdouble,GLdouble,GLdouble,GLdouble,GLdouble);
|
||||
void(APIENTRY*glRotatef)(GLfloat,GLfloat,GLfloat,GLfloat);
|
||||
void(APIENTRY*glViewport)(GLint,GLint,GLsizei,GLsizei);
|
||||
void(APIENTRY*glFogf)(GLenum,GLfloat);
|
||||
void (APIENTRY * glBegin) (GLenum);
|
||||
void (APIENTRY * glEnd) ();
|
||||
void (APIENTRY * glVertex3f) (GLfloat, GLfloat, GLfloat);
|
||||
void (APIENTRY * glClearColor) (GLfloat, GLfloat, GLfloat, GLfloat);
|
||||
void (APIENTRY * glClear) (GLbitfield);
|
||||
void (APIENTRY * glDisable) (GLenum);
|
||||
void (APIENTRY * glEnable) (GLenum);
|
||||
void (APIENTRY * glColor4ub) (GLubyte, GLubyte, GLubyte, GLubyte);
|
||||
void (APIENTRY * glPointSize) (GLfloat);
|
||||
void (APIENTRY * glHint) (GLenum, GLenum);
|
||||
void (APIENTRY * glBlendFunc) (GLenum, GLenum);
|
||||
void (APIENTRY * glMatrixMode) (GLenum);
|
||||
void (APIENTRY * glLoadIdentity) ();
|
||||
void (APIENTRY * glOrtho) (GLdouble, GLdouble, GLdouble, GLdouble,
|
||||
GLdouble, GLdouble);
|
||||
void (APIENTRY * glRotatef) (GLfloat, GLfloat, GLfloat, GLfloat);
|
||||
void (APIENTRY * glViewport) (GLint, GLint, GLsizei, GLsizei);
|
||||
void (APIENTRY * glFogf) (GLenum, GLfloat);
|
||||
}
|
||||
glfuncs;
|
||||
|
||||
void init_glfuncs(glfuncs* f)
|
||||
void
|
||||
init_glfuncs(glfuncs * f)
|
||||
{
|
||||
f->glBegin=get_funcaddr("glBegin");
|
||||
f->glEnd=get_funcaddr("glEnd");
|
||||
f->glVertex3f=get_funcaddr("glVertex3f");
|
||||
f->glClearColor=get_funcaddr("glClearColor");
|
||||
f->glClear=get_funcaddr("glClear");
|
||||
f->glDisable=get_funcaddr("glDisable");
|
||||
f->glEnable=get_funcaddr("glEnable");
|
||||
f->glColor4ub=get_funcaddr("glColor4ub");
|
||||
f->glPointSize=get_funcaddr("glPointSize");
|
||||
f->glHint=get_funcaddr("glHint");
|
||||
f->glBlendFunc=get_funcaddr("glBlendFunc");
|
||||
f->glMatrixMode=get_funcaddr("glMatrixMode");
|
||||
f->glLoadIdentity=get_funcaddr("glLoadIdentity");
|
||||
f->glOrtho=get_funcaddr("glOrtho");
|
||||
f->glRotatef=get_funcaddr("glRotatef");
|
||||
f->glViewport=get_funcaddr("glViewport");
|
||||
f->glFogf=get_funcaddr("glFogf");
|
||||
f->glBegin = get_funcaddr("glBegin");
|
||||
f->glEnd = get_funcaddr("glEnd");
|
||||
f->glVertex3f = get_funcaddr("glVertex3f");
|
||||
f->glClearColor = get_funcaddr("glClearColor");
|
||||
f->glClear = get_funcaddr("glClear");
|
||||
f->glDisable = get_funcaddr("glDisable");
|
||||
f->glEnable = get_funcaddr("glEnable");
|
||||
f->glColor4ub = get_funcaddr("glColor4ub");
|
||||
f->glPointSize = get_funcaddr("glPointSize");
|
||||
f->glHint = get_funcaddr("glHint");
|
||||
f->glBlendFunc = get_funcaddr("glBlendFunc");
|
||||
f->glMatrixMode = get_funcaddr("glMatrixMode");
|
||||
f->glLoadIdentity = get_funcaddr("glLoadIdentity");
|
||||
f->glOrtho = get_funcaddr("glOrtho");
|
||||
f->glRotatef = get_funcaddr("glRotatef");
|
||||
f->glViewport = get_funcaddr("glViewport");
|
||||
f->glFogf = get_funcaddr("glFogf");
|
||||
}
|
||||
|
||||
#define NB_PIXELS 1000
|
||||
|
||||
int main(int argc,char *argv[])
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
glfuncs f;
|
||||
int i;
|
||||
SDL_Event event;
|
||||
int done=0;
|
||||
GLfloat pixels[NB_PIXELS*3];
|
||||
const char *gl_library = NULL; /* Use the default GL library */
|
||||
glfuncs f;
|
||||
int i;
|
||||
SDL_Event event;
|
||||
int done = 0;
|
||||
GLfloat pixels[NB_PIXELS * 3];
|
||||
const char *gl_library = NULL; /* Use the default GL library */
|
||||
|
||||
if (argv[1]) {
|
||||
gl_library = argv[1];
|
||||
}
|
||||
|
||||
if (SDL_Init(SDL_INIT_VIDEO)<0)
|
||||
{
|
||||
printf("Unable to init SDL : %s\n",SDL_GetError());
|
||||
return(1);
|
||||
}
|
||||
if (argv[1]) {
|
||||
gl_library = argv[1];
|
||||
}
|
||||
|
||||
if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1)<0)
|
||||
{
|
||||
printf("Unable to set GL attribute : %s\n",SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
|
||||
if (SDL_GL_LoadLibrary(gl_library)<0)
|
||||
{
|
||||
printf("Unable to dynamically open GL lib : %s\n",SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
printf("Unable to init SDL : %s\n", SDL_GetError());
|
||||
return (1);
|
||||
}
|
||||
|
||||
if (SDL_SetVideoMode(640,480,0,SDL_OPENGL)==NULL)
|
||||
{
|
||||
printf("Unable to open video mode : %s\n",SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) < 0) {
|
||||
printf("Unable to set GL attribute : %s\n", SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
|
||||
/* Set the window manager title bar */
|
||||
SDL_WM_SetCaption( "SDL Dynamic OpenGL Loading Test", "testdyngl" );
|
||||
if (SDL_GL_LoadLibrary(gl_library) < 0) {
|
||||
printf("Unable to dynamically open GL lib : %s\n", SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
|
||||
init_glfuncs(&f);
|
||||
if (SDL_SetVideoMode(640, 480, 0, SDL_OPENGL) == NULL) {
|
||||
printf("Unable to open video mode : %s\n", SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
|
||||
for(i=0;i<NB_PIXELS;i++)
|
||||
{
|
||||
pixels[3*i]=rand()%250-125;
|
||||
pixels[3*i+1]=rand()%250-125;
|
||||
pixels[3*i+2]=rand()%250-125;
|
||||
}
|
||||
|
||||
f.glViewport(0,0,640,480);
|
||||
|
||||
f.glMatrixMode(GL_PROJECTION);
|
||||
f.glLoadIdentity();
|
||||
f.glOrtho(-100,100,-100,100,-500,500);
|
||||
|
||||
f.glMatrixMode(GL_MODELVIEW);
|
||||
f.glLoadIdentity();
|
||||
|
||||
f.glEnable(GL_DEPTH_TEST);
|
||||
f.glDisable(GL_TEXTURE_2D);
|
||||
f.glEnable(GL_BLEND);
|
||||
f.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
f.glClearColor(0.0f,0.0f,0.0f,0.0f);
|
||||
f.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
f.glEnable(GL_POINT_SMOOTH);
|
||||
f.glHint(GL_POINT_SMOOTH_HINT,GL_NICEST);
|
||||
f.glPointSize(5.0f);
|
||||
f.glEnable(GL_FOG);
|
||||
f.glFogf(GL_FOG_START,-500);
|
||||
f.glFogf(GL_FOG_END,500);
|
||||
f.glFogf(GL_FOG_DENSITY,0.005);
|
||||
|
||||
do
|
||||
{
|
||||
f.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
f.glRotatef(2.0,1.0,1.0,1.0);
|
||||
f.glRotatef(1.0,0.0,1.0,1.0);
|
||||
|
||||
f.glColor4ub(255,255,255,255);
|
||||
f.glBegin(GL_POINTS);
|
||||
for(i=0;i<NB_PIXELS;i++)
|
||||
{
|
||||
f.glVertex3f(pixels[3*i],pixels[3*i+1],pixels[3*i+2]);
|
||||
}
|
||||
f.glEnd();
|
||||
SDL_GL_SwapBuffers();
|
||||
/* Set the window manager title bar */
|
||||
SDL_WM_SetCaption("SDL Dynamic OpenGL Loading Test", "testdyngl");
|
||||
|
||||
while(SDL_PollEvent(&event))
|
||||
{
|
||||
if(event.type & SDL_KEYDOWN)
|
||||
done=1;
|
||||
}
|
||||
init_glfuncs(&f);
|
||||
|
||||
SDL_Delay(20);
|
||||
}
|
||||
while(!done);
|
||||
|
||||
SDL_Quit();
|
||||
return 0;
|
||||
for (i = 0; i < NB_PIXELS; i++) {
|
||||
pixels[3 * i] = rand() % 250 - 125;
|
||||
pixels[3 * i + 1] = rand() % 250 - 125;
|
||||
pixels[3 * i + 2] = rand() % 250 - 125;
|
||||
}
|
||||
|
||||
f.glViewport(0, 0, 640, 480);
|
||||
|
||||
f.glMatrixMode(GL_PROJECTION);
|
||||
f.glLoadIdentity();
|
||||
f.glOrtho(-100, 100, -100, 100, -500, 500);
|
||||
|
||||
f.glMatrixMode(GL_MODELVIEW);
|
||||
f.glLoadIdentity();
|
||||
|
||||
f.glEnable(GL_DEPTH_TEST);
|
||||
f.glDisable(GL_TEXTURE_2D);
|
||||
f.glEnable(GL_BLEND);
|
||||
f.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
f.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
f.glEnable(GL_POINT_SMOOTH);
|
||||
f.glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
|
||||
f.glPointSize(5.0f);
|
||||
f.glEnable(GL_FOG);
|
||||
f.glFogf(GL_FOG_START, -500);
|
||||
f.glFogf(GL_FOG_END, 500);
|
||||
f.glFogf(GL_FOG_DENSITY, 0.005);
|
||||
|
||||
do {
|
||||
f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
f.glRotatef(2.0, 1.0, 1.0, 1.0);
|
||||
f.glRotatef(1.0, 0.0, 1.0, 1.0);
|
||||
|
||||
f.glColor4ub(255, 255, 255, 255);
|
||||
f.glBegin(GL_POINTS);
|
||||
for (i = 0; i < NB_PIXELS; i++) {
|
||||
f.glVertex3f(pixels[3 * i], pixels[3 * i + 1], pixels[3 * i + 2]);
|
||||
}
|
||||
f.glEnd();
|
||||
SDL_GL_SwapBuffers();
|
||||
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type & SDL_KEYDOWN)
|
||||
done = 1;
|
||||
}
|
||||
|
||||
SDL_Delay(20);
|
||||
}
|
||||
while (!done);
|
||||
|
||||
SDL_Quit();
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else /* HAVE_OPENGL */
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
printf("No OpenGL support on this system\n");
|
||||
return 1;
|
||||
printf("No OpenGL support on this system\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
#endif /* HAVE_OPENGL */
|
||||
|
||||
@@ -11,51 +11,54 @@
|
||||
static int alive = 0;
|
||||
|
||||
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
||||
static void quit(int rc)
|
||||
static void
|
||||
quit(int rc)
|
||||
{
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
}
|
||||
|
||||
int SDLCALL ThreadFunc(void *data)
|
||||
int SDLCALL
|
||||
ThreadFunc(void *data)
|
||||
{
|
||||
/* Set the child thread error string */
|
||||
SDL_SetError("Thread %s (%d) had a problem: %s",
|
||||
(char *)data, SDL_ThreadID(), "nevermind");
|
||||
while ( alive ) {
|
||||
printf("Thread '%s' is alive!\n", (char *)data);
|
||||
SDL_Delay(1*1000);
|
||||
}
|
||||
printf("Child thread error string: %s\n", SDL_GetError());
|
||||
return(0);
|
||||
/* Set the child thread error string */
|
||||
SDL_SetError("Thread %s (%d) had a problem: %s",
|
||||
(char *) data, SDL_ThreadID(), "nevermind");
|
||||
while (alive) {
|
||||
printf("Thread '%s' is alive!\n", (char *) data);
|
||||
SDL_Delay(1 * 1000);
|
||||
}
|
||||
printf("Child thread error string: %s\n", SDL_GetError());
|
||||
return (0);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
SDL_Thread *thread;
|
||||
SDL_Thread *thread;
|
||||
|
||||
/* Load the SDL library */
|
||||
if ( SDL_Init(0) < 0 ) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
|
||||
return(1);
|
||||
}
|
||||
/* Load the SDL library */
|
||||
if (SDL_Init(0) < 0) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return (1);
|
||||
}
|
||||
|
||||
/* Set the error value for the main thread */
|
||||
SDL_SetError("No worries");
|
||||
/* Set the error value for the main thread */
|
||||
SDL_SetError("No worries");
|
||||
|
||||
alive = 1;
|
||||
thread = SDL_CreateThread(ThreadFunc, "#1");
|
||||
if ( thread == NULL ) {
|
||||
fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
SDL_Delay(5*1000);
|
||||
printf("Waiting for thread #1\n");
|
||||
alive = 0;
|
||||
SDL_WaitThread(thread, NULL);
|
||||
alive = 1;
|
||||
thread = SDL_CreateThread(ThreadFunc, "#1");
|
||||
if (thread == NULL) {
|
||||
fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
SDL_Delay(5 * 1000);
|
||||
printf("Waiting for thread #1\n");
|
||||
alive = 0;
|
||||
SDL_WaitThread(thread, NULL);
|
||||
|
||||
printf("Main thread error string: %s\n", SDL_GetError());
|
||||
printf("Main thread error string: %s\n", SDL_GetError());
|
||||
|
||||
SDL_Quit();
|
||||
return(0);
|
||||
SDL_Quit();
|
||||
return (0);
|
||||
}
|
||||
|
||||
346
test/testfile.c
346
test/testfile.c
@@ -8,172 +8,254 @@
|
||||
#include "SDL_endian.h"
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/* WARNING ! those 2 files will be destroyed by this test program */
|
||||
#define FBASENAME1 "sdldata1" /* this file will be created during tests */
|
||||
#define FBASENAME2 "sdldata2" /* this file should not exists before starting test */
|
||||
#define FBASENAME1 "sdldata1" /* this file will be created during tests */
|
||||
#define FBASENAME2 "sdldata2" /* this file should not exists before starting test */
|
||||
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL ((void *)0)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
static void cleanup( void ) {
|
||||
static void
|
||||
cleanup(void)
|
||||
{
|
||||
|
||||
unlink(FBASENAME1);
|
||||
unlink(FBASENAME2);
|
||||
unlink(FBASENAME1);
|
||||
unlink(FBASENAME2);
|
||||
}
|
||||
|
||||
static void rwops_error_quit( unsigned line, SDL_RWops *rwops) {
|
||||
|
||||
printf("testfile.c(%d): failed\n",line);
|
||||
if (rwops) {
|
||||
rwops->close(rwops); /* This calls SDL_FreeRW(rwops); */
|
||||
}
|
||||
cleanup();
|
||||
exit(1); /* quit with rwops error (test failed) */
|
||||
static void
|
||||
rwops_error_quit(unsigned line, SDL_RWops * rwops)
|
||||
{
|
||||
|
||||
printf("testfile.c(%d): failed\n", line);
|
||||
if (rwops) {
|
||||
rwops->close(rwops); /* This calls SDL_FreeRW(rwops); */
|
||||
}
|
||||
cleanup();
|
||||
exit(1); /* quit with rwops error (test failed) */
|
||||
}
|
||||
|
||||
#define RWOP_ERR_QUIT(x) rwops_error_quit( __LINE__, (x) )
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
SDL_RWops *rwops = NULL;
|
||||
char test_buf[30];
|
||||
|
||||
cleanup();
|
||||
SDL_RWops *rwops = NULL;
|
||||
char test_buf[30];
|
||||
|
||||
cleanup();
|
||||
|
||||
/* test 1 : basic argument test: all those calls to SDL_RWFromFile should fail */
|
||||
|
||||
rwops = SDL_RWFromFile(NULL,NULL);
|
||||
if (rwops) RWOP_ERR_QUIT(rwops);
|
||||
rwops = SDL_RWFromFile(NULL,"ab+");
|
||||
if (rwops) RWOP_ERR_QUIT(rwops);
|
||||
rwops = SDL_RWFromFile(NULL,"sldfkjsldkfj");
|
||||
if (rwops) RWOP_ERR_QUIT(rwops);
|
||||
rwops = SDL_RWFromFile("something","");
|
||||
if (rwops) RWOP_ERR_QUIT(rwops);
|
||||
rwops = SDL_RWFromFile("something",NULL);
|
||||
if (rwops) RWOP_ERR_QUIT(rwops);
|
||||
printf("test1 OK\n");
|
||||
|
||||
rwops = SDL_RWFromFile(NULL, NULL);
|
||||
if (rwops)
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
rwops = SDL_RWFromFile(NULL, "ab+");
|
||||
if (rwops)
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
rwops = SDL_RWFromFile(NULL, "sldfkjsldkfj");
|
||||
if (rwops)
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
rwops = SDL_RWFromFile("something", "");
|
||||
if (rwops)
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
rwops = SDL_RWFromFile("something", NULL);
|
||||
if (rwops)
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
printf("test1 OK\n");
|
||||
|
||||
/* test 2 : check that inexistant file is not successfully opened/created when required */
|
||||
/* modes : r, r+ implie that file MUST exist
|
||||
modes : a, a+, w, w+ checks that it succeeds (file may not exists)
|
||||
|
||||
*/
|
||||
rwops = SDL_RWFromFile(FBASENAME2,"rb"); /* this file doesn't exist that call must fail */
|
||||
if (rwops) RWOP_ERR_QUIT(rwops);
|
||||
rwops = SDL_RWFromFile(FBASENAME2,"rb+"); /* this file doesn't exist that call must fail */
|
||||
if (rwops) RWOP_ERR_QUIT(rwops);
|
||||
rwops = SDL_RWFromFile(FBASENAME2,"wb");
|
||||
if (!rwops) RWOP_ERR_QUIT(rwops);
|
||||
rwops->close(rwops); unlink(FBASENAME2);
|
||||
rwops = SDL_RWFromFile(FBASENAME2,"wb+");
|
||||
if (!rwops) RWOP_ERR_QUIT(rwops);
|
||||
rwops->close(rwops); unlink(FBASENAME2);
|
||||
rwops = SDL_RWFromFile(FBASENAME2,"ab");
|
||||
if (!rwops) RWOP_ERR_QUIT(rwops);
|
||||
rwops->close(rwops); unlink(FBASENAME2);
|
||||
rwops = SDL_RWFromFile(FBASENAME2,"ab+");
|
||||
if (!rwops) RWOP_ERR_QUIT(rwops);
|
||||
rwops->close(rwops); unlink(FBASENAME2);
|
||||
printf("test2 OK\n");
|
||||
rwops = SDL_RWFromFile(FBASENAME2, "rb"); /* this file doesn't exist that call must fail */
|
||||
if (rwops)
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
rwops = SDL_RWFromFile(FBASENAME2, "rb+"); /* this file doesn't exist that call must fail */
|
||||
if (rwops)
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
rwops = SDL_RWFromFile(FBASENAME2, "wb");
|
||||
if (!rwops)
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
rwops->close(rwops);
|
||||
unlink(FBASENAME2);
|
||||
rwops = SDL_RWFromFile(FBASENAME2, "wb+");
|
||||
if (!rwops)
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
rwops->close(rwops);
|
||||
unlink(FBASENAME2);
|
||||
rwops = SDL_RWFromFile(FBASENAME2, "ab");
|
||||
if (!rwops)
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
rwops->close(rwops);
|
||||
unlink(FBASENAME2);
|
||||
rwops = SDL_RWFromFile(FBASENAME2, "ab+");
|
||||
if (!rwops)
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
rwops->close(rwops);
|
||||
unlink(FBASENAME2);
|
||||
printf("test2 OK\n");
|
||||
|
||||
/* test 3 : creation, writing , reading, seeking,
|
||||
test : w mode, r mode, w+ mode
|
||||
*/
|
||||
rwops = SDL_RWFromFile(FBASENAME1,"wb"); /* write only */
|
||||
if (!rwops) RWOP_ERR_QUIT(rwops);
|
||||
if (1 != rwops->write(rwops,"1234567890",10,1) ) RWOP_ERR_QUIT(rwops);
|
||||
if (10 != rwops->write(rwops,"1234567890",1,10) ) RWOP_ERR_QUIT(rwops);
|
||||
if (7 != rwops->write(rwops,"1234567",1,7) ) RWOP_ERR_QUIT(rwops);
|
||||
if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops);
|
||||
if (0!=rwops->read(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops); /* we are in write only mode */
|
||||
rwops->close(rwops);
|
||||
rwops = SDL_RWFromFile(FBASENAME1, "wb"); /* write only */
|
||||
if (!rwops)
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (1 != rwops->write(rwops, "1234567890", 10, 1))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (10 != rwops->write(rwops, "1234567890", 1, 10))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (7 != rwops->write(rwops, "1234567", 1, 7))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (0 != rwops->read(rwops, test_buf, 1, 1))
|
||||
RWOP_ERR_QUIT(rwops); /* we are in write only mode */
|
||||
rwops->close(rwops);
|
||||
|
||||
rwops = SDL_RWFromFile(FBASENAME1,"rb"); /* read mode, file must exists */
|
||||
if (!rwops) RWOP_ERR_QUIT(rwops);
|
||||
if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops);
|
||||
if (20!=rwops->seek(rwops,-7,RW_SEEK_END)) RWOP_ERR_QUIT(rwops);
|
||||
if (7!=rwops->read(rwops,test_buf,1,7)) RWOP_ERR_QUIT(rwops);
|
||||
if (SDL_memcmp(test_buf,"1234567",7)) RWOP_ERR_QUIT(rwops);
|
||||
if (0!=rwops->read(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops);
|
||||
if (0!=rwops->read(rwops,test_buf,10,100)) RWOP_ERR_QUIT(rwops);
|
||||
if (0!=rwops->seek(rwops,-27,RW_SEEK_CUR)) RWOP_ERR_QUIT(rwops);
|
||||
if (2!=rwops->read(rwops,test_buf,10,3)) RWOP_ERR_QUIT(rwops);
|
||||
if (SDL_memcmp(test_buf,"12345678901234567890",20)) RWOP_ERR_QUIT(rwops);
|
||||
if (0!=rwops->write(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops); /* readonly mode */
|
||||
rwops->close(rwops);
|
||||
rwops = SDL_RWFromFile(FBASENAME1, "rb"); /* read mode, file must exists */
|
||||
if (!rwops)
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (7 != rwops->read(rwops, test_buf, 1, 7))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (SDL_memcmp(test_buf, "1234567", 7))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (0 != rwops->read(rwops, test_buf, 1, 1))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (0 != rwops->read(rwops, test_buf, 10, 100))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (2 != rwops->read(rwops, test_buf, 10, 3))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (SDL_memcmp(test_buf, "12345678901234567890", 20))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (0 != rwops->write(rwops, test_buf, 1, 1))
|
||||
RWOP_ERR_QUIT(rwops); /* readonly mode */
|
||||
rwops->close(rwops);
|
||||
|
||||
/* test 3: same with w+ mode */
|
||||
rwops = SDL_RWFromFile(FBASENAME1,"wb+"); /* write + read + truncation */
|
||||
if (!rwops) RWOP_ERR_QUIT(rwops);
|
||||
if (1 != rwops->write(rwops,"1234567890",10,1) ) RWOP_ERR_QUIT(rwops);
|
||||
if (10 != rwops->write(rwops,"1234567890",1,10) ) RWOP_ERR_QUIT(rwops);
|
||||
if (7 != rwops->write(rwops,"1234567",1,7) ) RWOP_ERR_QUIT(rwops);
|
||||
if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops);
|
||||
if (1!=rwops->read(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops); /* we are in read/write mode */
|
||||
if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops);
|
||||
if (20!=rwops->seek(rwops,-7,RW_SEEK_END)) RWOP_ERR_QUIT(rwops);
|
||||
if (7!=rwops->read(rwops,test_buf,1,7)) RWOP_ERR_QUIT(rwops);
|
||||
if (SDL_memcmp(test_buf,"1234567",7)) RWOP_ERR_QUIT(rwops);
|
||||
if (0!=rwops->read(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops);
|
||||
if (0!=rwops->read(rwops,test_buf,10,100)) RWOP_ERR_QUIT(rwops);
|
||||
if (0!=rwops->seek(rwops,-27,RW_SEEK_CUR)) RWOP_ERR_QUIT(rwops);
|
||||
if (2!=rwops->read(rwops,test_buf,10,3)) RWOP_ERR_QUIT(rwops);
|
||||
if (SDL_memcmp(test_buf,"12345678901234567890",20)) RWOP_ERR_QUIT(rwops);
|
||||
rwops->close(rwops);
|
||||
printf("test3 OK\n");
|
||||
rwops = SDL_RWFromFile(FBASENAME1, "wb+"); /* write + read + truncation */
|
||||
if (!rwops)
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (1 != rwops->write(rwops, "1234567890", 10, 1))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (10 != rwops->write(rwops, "1234567890", 1, 10))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (7 != rwops->write(rwops, "1234567", 1, 7))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (1 != rwops->read(rwops, test_buf, 1, 1))
|
||||
RWOP_ERR_QUIT(rwops); /* we are in read/write mode */
|
||||
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (7 != rwops->read(rwops, test_buf, 1, 7))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (SDL_memcmp(test_buf, "1234567", 7))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (0 != rwops->read(rwops, test_buf, 1, 1))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (0 != rwops->read(rwops, test_buf, 10, 100))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (2 != rwops->read(rwops, test_buf, 10, 3))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (SDL_memcmp(test_buf, "12345678901234567890", 20))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
rwops->close(rwops);
|
||||
printf("test3 OK\n");
|
||||
|
||||
/* test 4: same in r+ mode */
|
||||
rwops = SDL_RWFromFile(FBASENAME1,"rb+"); /* write + read + file must exists, no truncation */
|
||||
if (!rwops) RWOP_ERR_QUIT(rwops);
|
||||
if (1 != rwops->write(rwops,"1234567890",10,1) ) RWOP_ERR_QUIT(rwops);
|
||||
if (10 != rwops->write(rwops,"1234567890",1,10) ) RWOP_ERR_QUIT(rwops);
|
||||
if (7 != rwops->write(rwops,"1234567",1,7) ) RWOP_ERR_QUIT(rwops);
|
||||
if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops);
|
||||
if (1!=rwops->read(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops); /* we are in read/write mode */
|
||||
if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops);
|
||||
if (20!=rwops->seek(rwops,-7,RW_SEEK_END)) RWOP_ERR_QUIT(rwops);
|
||||
if (7!=rwops->read(rwops,test_buf,1,7)) RWOP_ERR_QUIT(rwops);
|
||||
if (SDL_memcmp(test_buf,"1234567",7)) RWOP_ERR_QUIT(rwops);
|
||||
if (0!=rwops->read(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops);
|
||||
if (0!=rwops->read(rwops,test_buf,10,100)) RWOP_ERR_QUIT(rwops);
|
||||
if (0!=rwops->seek(rwops,-27,RW_SEEK_CUR)) RWOP_ERR_QUIT(rwops);
|
||||
if (2!=rwops->read(rwops,test_buf,10,3)) RWOP_ERR_QUIT(rwops);
|
||||
if (SDL_memcmp(test_buf,"12345678901234567890",20)) RWOP_ERR_QUIT(rwops);
|
||||
rwops->close(rwops);
|
||||
printf("test4 OK\n");
|
||||
rwops = SDL_RWFromFile(FBASENAME1, "rb+"); /* write + read + file must exists, no truncation */
|
||||
if (!rwops)
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (1 != rwops->write(rwops, "1234567890", 10, 1))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (10 != rwops->write(rwops, "1234567890", 1, 10))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (7 != rwops->write(rwops, "1234567", 1, 7))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (1 != rwops->read(rwops, test_buf, 1, 1))
|
||||
RWOP_ERR_QUIT(rwops); /* we are in read/write mode */
|
||||
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (7 != rwops->read(rwops, test_buf, 1, 7))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (SDL_memcmp(test_buf, "1234567", 7))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (0 != rwops->read(rwops, test_buf, 1, 1))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (0 != rwops->read(rwops, test_buf, 10, 100))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (2 != rwops->read(rwops, test_buf, 10, 3))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (SDL_memcmp(test_buf, "12345678901234567890", 20))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
rwops->close(rwops);
|
||||
printf("test4 OK\n");
|
||||
|
||||
/* test5 : append mode */
|
||||
rwops = SDL_RWFromFile(FBASENAME1,"ab+"); /* write + read + append */
|
||||
if (!rwops) RWOP_ERR_QUIT(rwops);
|
||||
if (1 != rwops->write(rwops,"1234567890",10,1) ) RWOP_ERR_QUIT(rwops);
|
||||
if (10 != rwops->write(rwops,"1234567890",1,10) ) RWOP_ERR_QUIT(rwops);
|
||||
if (7 != rwops->write(rwops,"1234567",1,7) ) RWOP_ERR_QUIT(rwops);
|
||||
if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops);
|
||||
|
||||
if (1!=rwops->read(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops);
|
||||
if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops);
|
||||
|
||||
if (20+27!=rwops->seek(rwops,-7,RW_SEEK_END)) RWOP_ERR_QUIT(rwops);
|
||||
if (7!=rwops->read(rwops,test_buf,1,7)) RWOP_ERR_QUIT(rwops);
|
||||
if (SDL_memcmp(test_buf,"1234567",7)) RWOP_ERR_QUIT(rwops);
|
||||
if (0!=rwops->read(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops);
|
||||
if (0!=rwops->read(rwops,test_buf,10,100)) RWOP_ERR_QUIT(rwops);
|
||||
|
||||
if (27!=rwops->seek(rwops,-27,RW_SEEK_CUR)) RWOP_ERR_QUIT(rwops);
|
||||
|
||||
if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops);
|
||||
if (3!=rwops->read(rwops,test_buf,10,3)) RWOP_ERR_QUIT(rwops);
|
||||
if (SDL_memcmp(test_buf,"123456789012345678901234567123",30))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
rwops->close(rwops);
|
||||
printf("test5 OK\n");
|
||||
cleanup();
|
||||
return 0; /* all ok */
|
||||
rwops = SDL_RWFromFile(FBASENAME1, "ab+"); /* write + read + append */
|
||||
if (!rwops)
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (1 != rwops->write(rwops, "1234567890", 10, 1))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (10 != rwops->write(rwops, "1234567890", 1, 10))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (7 != rwops->write(rwops, "1234567", 1, 7))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
|
||||
if (1 != rwops->read(rwops, test_buf, 1, 1))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
|
||||
if (20 + 27 != rwops->seek(rwops, -7, RW_SEEK_END))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (7 != rwops->read(rwops, test_buf, 1, 7))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (SDL_memcmp(test_buf, "1234567", 7))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (0 != rwops->read(rwops, test_buf, 1, 1))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (0 != rwops->read(rwops, test_buf, 10, 100))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
|
||||
if (27 != rwops->seek(rwops, -27, RW_SEEK_CUR))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
|
||||
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (3 != rwops->read(rwops, test_buf, 10, 3))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
if (SDL_memcmp(test_buf, "123456789012345678901234567123", 30))
|
||||
RWOP_ERR_QUIT(rwops);
|
||||
rwops->close(rwops);
|
||||
printf("test5 OK\n");
|
||||
cleanup();
|
||||
return 0; /* all ok */
|
||||
}
|
||||
|
||||
311
test/testgamma.c
311
test/testgamma.c
@@ -9,189 +9,186 @@
|
||||
#include "SDL.h"
|
||||
|
||||
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
||||
static void quit(int rc)
|
||||
static void
|
||||
quit(int rc)
|
||||
{
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
}
|
||||
|
||||
/* Turn a normal gamma value into an appropriate gamma ramp */
|
||||
void CalculateGamma(double gamma, Uint16 *ramp)
|
||||
void
|
||||
CalculateGamma(double gamma, Uint16 * ramp)
|
||||
{
|
||||
int i, value;
|
||||
int i, value;
|
||||
|
||||
gamma = 1.0 / gamma;
|
||||
for ( i=0; i<256; ++i ) {
|
||||
value = (int)(pow((double)i/256.0, gamma)*65535.0 + 0.5);
|
||||
if ( value > 65535 ) {
|
||||
value = 65535;
|
||||
}
|
||||
ramp[i] = (Uint16)value;
|
||||
}
|
||||
gamma = 1.0 / gamma;
|
||||
for (i = 0; i < 256; ++i) {
|
||||
value = (int) (pow((double) i / 256.0, gamma) * 65535.0 + 0.5);
|
||||
if (value > 65535) {
|
||||
value = 65535;
|
||||
}
|
||||
ramp[i] = (Uint16) value;
|
||||
}
|
||||
}
|
||||
|
||||
/* This can be used as a general routine for all of the test programs */
|
||||
int get_video_args(char *argv[], int *w, int *h, int *bpp, Uint32 *flags)
|
||||
int
|
||||
get_video_args(char *argv[], int *w, int *h, int *bpp, Uint32 * flags)
|
||||
{
|
||||
int i;
|
||||
int i;
|
||||
|
||||
*w = 640;
|
||||
*h = 480;
|
||||
*bpp = 0;
|
||||
*flags = SDL_SWSURFACE;
|
||||
*w = 640;
|
||||
*h = 480;
|
||||
*bpp = 0;
|
||||
*flags = SDL_SWSURFACE;
|
||||
|
||||
for ( i=1; argv[i]; ++i ) {
|
||||
if ( strcmp(argv[i], "-width") == 0 ) {
|
||||
if ( argv[i+1] ) {
|
||||
*w = atoi(argv[++i]);
|
||||
}
|
||||
} else
|
||||
if ( strcmp(argv[i], "-height") == 0 ) {
|
||||
if ( argv[i+1] ) {
|
||||
*h = atoi(argv[++i]);
|
||||
}
|
||||
} else
|
||||
if ( strcmp(argv[i], "-bpp") == 0 ) {
|
||||
if ( argv[i+1] ) {
|
||||
*bpp = atoi(argv[++i]);
|
||||
}
|
||||
} else
|
||||
if ( strcmp(argv[i], "-fullscreen") == 0 ) {
|
||||
*flags |= SDL_FULLSCREEN;
|
||||
} else
|
||||
if ( strcmp(argv[i], "-hw") == 0 ) {
|
||||
*flags |= SDL_HWSURFACE;
|
||||
} else
|
||||
if ( strcmp(argv[i], "-hwpalette") == 0 ) {
|
||||
*flags |= SDL_HWPALETTE;
|
||||
} else
|
||||
break;
|
||||
}
|
||||
return i;
|
||||
for (i = 1; argv[i]; ++i) {
|
||||
if (strcmp(argv[i], "-width") == 0) {
|
||||
if (argv[i + 1]) {
|
||||
*w = atoi(argv[++i]);
|
||||
}
|
||||
} else if (strcmp(argv[i], "-height") == 0) {
|
||||
if (argv[i + 1]) {
|
||||
*h = atoi(argv[++i]);
|
||||
}
|
||||
} else if (strcmp(argv[i], "-bpp") == 0) {
|
||||
if (argv[i + 1]) {
|
||||
*bpp = atoi(argv[++i]);
|
||||
}
|
||||
} else if (strcmp(argv[i], "-fullscreen") == 0) {
|
||||
*flags |= SDL_FULLSCREEN;
|
||||
} else if (strcmp(argv[i], "-hw") == 0) {
|
||||
*flags |= SDL_HWSURFACE;
|
||||
} else if (strcmp(argv[i], "-hwpalette") == 0) {
|
||||
*flags |= SDL_HWPALETTE;
|
||||
} else
|
||||
break;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
SDL_Surface *screen;
|
||||
SDL_Surface *image;
|
||||
float gamma;
|
||||
int i;
|
||||
int w, h, bpp;
|
||||
Uint32 flags;
|
||||
Uint16 ramp[256];
|
||||
Uint16 red_ramp[256];
|
||||
Uint32 then, timeout;
|
||||
SDL_Surface *screen;
|
||||
SDL_Surface *image;
|
||||
float gamma;
|
||||
int i;
|
||||
int w, h, bpp;
|
||||
Uint32 flags;
|
||||
Uint16 ramp[256];
|
||||
Uint16 red_ramp[256];
|
||||
Uint32 then, timeout;
|
||||
|
||||
/* Check command line arguments */
|
||||
argv += get_video_args(argv, &w, &h, &bpp, &flags);
|
||||
/* Check command line arguments */
|
||||
argv += get_video_args(argv, &w, &h, &bpp, &flags);
|
||||
|
||||
/* Initialize SDL */
|
||||
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
|
||||
fprintf(stderr,
|
||||
"Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return(1);
|
||||
}
|
||||
/* Initialize SDL */
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return (1);
|
||||
}
|
||||
|
||||
/* Initialize the display, always use hardware palette */
|
||||
screen = SDL_SetVideoMode(w, h, bpp, flags | SDL_HWPALETTE);
|
||||
if ( screen == NULL ) {
|
||||
fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
|
||||
w, h, SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
/* Initialize the display, always use hardware palette */
|
||||
screen = SDL_SetVideoMode(w, h, bpp, flags | SDL_HWPALETTE);
|
||||
if (screen == NULL) {
|
||||
fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
|
||||
w, h, SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
|
||||
/* Set the window manager title bar */
|
||||
SDL_WM_SetCaption("SDL gamma test", "testgamma");
|
||||
/* Set the window manager title bar */
|
||||
SDL_WM_SetCaption("SDL gamma test", "testgamma");
|
||||
|
||||
/* Set the desired gamma, if any */
|
||||
gamma = 1.0f;
|
||||
if ( *argv ) {
|
||||
gamma = (float)atof(*argv);
|
||||
}
|
||||
if ( SDL_SetGamma(gamma, gamma, gamma) < 0 ) {
|
||||
fprintf(stderr, "Unable to set gamma: %s\n", SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
|
||||
#if 0 /* This isn't supported. Integrating the gamma ramps isn't exact */
|
||||
/* See what gamma was actually set */
|
||||
float real[3];
|
||||
if ( SDL_GetGamma(&real[0], &real[1], &real[2]) < 0 ) {
|
||||
printf("Couldn't get gamma: %s\n", SDL_GetError());
|
||||
} else {
|
||||
printf("Set gamma values: R=%2.2f, G=%2.2f, B=%2.2f\n",
|
||||
real[0], real[1], real[2]);
|
||||
}
|
||||
/* Set the desired gamma, if any */
|
||||
gamma = 1.0f;
|
||||
if (*argv) {
|
||||
gamma = (float) atof(*argv);
|
||||
}
|
||||
if (SDL_SetGamma(gamma, gamma, gamma) < 0) {
|
||||
fprintf(stderr, "Unable to set gamma: %s\n", SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
#if 0 /* This isn't supported. Integrating the gamma ramps isn't exact */
|
||||
/* See what gamma was actually set */
|
||||
float real[3];
|
||||
if (SDL_GetGamma(&real[0], &real[1], &real[2]) < 0) {
|
||||
printf("Couldn't get gamma: %s\n", SDL_GetError());
|
||||
} else {
|
||||
printf("Set gamma values: R=%2.2f, G=%2.2f, B=%2.2f\n",
|
||||
real[0], real[1], real[2]);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Do all the drawing work */
|
||||
image = SDL_LoadBMP("sample.bmp");
|
||||
if ( image ) {
|
||||
SDL_Rect dst;
|
||||
/* Do all the drawing work */
|
||||
image = SDL_LoadBMP("sample.bmp");
|
||||
if (image) {
|
||||
SDL_Rect dst;
|
||||
|
||||
dst.x = (screen->w - image->w)/2;
|
||||
dst.y = (screen->h - image->h)/2;
|
||||
dst.w = image->w;
|
||||
dst.h = image->h;
|
||||
SDL_BlitSurface(image, NULL, screen, &dst);
|
||||
SDL_UpdateRects(screen, 1, &dst);
|
||||
}
|
||||
dst.x = (screen->w - image->w) / 2;
|
||||
dst.y = (screen->h - image->h) / 2;
|
||||
dst.w = image->w;
|
||||
dst.h = image->h;
|
||||
SDL_BlitSurface(image, NULL, screen, &dst);
|
||||
SDL_UpdateRects(screen, 1, &dst);
|
||||
}
|
||||
|
||||
/* Wait a bit, handling events */
|
||||
then = SDL_GetTicks();
|
||||
timeout = (5*1000);
|
||||
while ( (SDL_GetTicks()-then) < timeout ) {
|
||||
SDL_Event event;
|
||||
/* Wait a bit, handling events */
|
||||
then = SDL_GetTicks();
|
||||
timeout = (5 * 1000);
|
||||
while ((SDL_GetTicks() - then) < timeout) {
|
||||
SDL_Event event;
|
||||
|
||||
while ( SDL_PollEvent(&event) ) {
|
||||
switch (event.type) {
|
||||
case SDL_QUIT: /* Quit now */
|
||||
timeout = 0;
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
switch (event.key.keysym.sym) {
|
||||
case SDLK_SPACE: /* Go longer.. */
|
||||
timeout += (5*1000);
|
||||
break;
|
||||
case SDLK_UP:
|
||||
gamma += 0.2f;
|
||||
SDL_SetGamma(gamma, gamma, gamma);
|
||||
break;
|
||||
case SDLK_DOWN:
|
||||
gamma -= 0.2f;
|
||||
SDL_SetGamma(gamma, gamma, gamma);
|
||||
break;
|
||||
case SDLK_ESCAPE:
|
||||
timeout = 0;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
while (SDL_PollEvent(&event)) {
|
||||
switch (event.type) {
|
||||
case SDL_QUIT: /* Quit now */
|
||||
timeout = 0;
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
switch (event.key.keysym.sym) {
|
||||
case SDLK_SPACE: /* Go longer.. */
|
||||
timeout += (5 * 1000);
|
||||
break;
|
||||
case SDLK_UP:
|
||||
gamma += 0.2f;
|
||||
SDL_SetGamma(gamma, gamma, gamma);
|
||||
break;
|
||||
case SDLK_DOWN:
|
||||
gamma -= 0.2f;
|
||||
SDL_SetGamma(gamma, gamma, gamma);
|
||||
break;
|
||||
case SDLK_ESCAPE:
|
||||
timeout = 0;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Perform a gamma flash to red using color ramps */
|
||||
while ( gamma < 10.0 ) {
|
||||
/* Increase the red gamma and decrease everything else... */
|
||||
gamma += 0.1f;
|
||||
CalculateGamma(gamma, red_ramp);
|
||||
CalculateGamma(1.0/gamma, ramp);
|
||||
SDL_SetGammaRamp(red_ramp, ramp, ramp);
|
||||
}
|
||||
/* Finish completely red */
|
||||
memset(red_ramp, 255, sizeof(red_ramp));
|
||||
memset(ramp, 0, sizeof(ramp));
|
||||
SDL_SetGammaRamp(red_ramp, ramp, ramp);
|
||||
/* Perform a gamma flash to red using color ramps */
|
||||
while (gamma < 10.0) {
|
||||
/* Increase the red gamma and decrease everything else... */
|
||||
gamma += 0.1f;
|
||||
CalculateGamma(gamma, red_ramp);
|
||||
CalculateGamma(1.0 / gamma, ramp);
|
||||
SDL_SetGammaRamp(red_ramp, ramp, ramp);
|
||||
}
|
||||
/* Finish completely red */
|
||||
memset(red_ramp, 255, sizeof(red_ramp));
|
||||
memset(ramp, 0, sizeof(ramp));
|
||||
SDL_SetGammaRamp(red_ramp, ramp, ramp);
|
||||
|
||||
/* Now fade out to black */
|
||||
for ( i=(red_ramp[0] >> 8); i >= 0; --i ) {
|
||||
memset(red_ramp, i, sizeof(red_ramp));
|
||||
SDL_SetGammaRamp(red_ramp, NULL, NULL);
|
||||
}
|
||||
SDL_Delay(1*1000);
|
||||
/* Now fade out to black */
|
||||
for (i = (red_ramp[0] >> 8); i >= 0; --i) {
|
||||
memset(red_ramp, i, sizeof(red_ramp));
|
||||
SDL_SetGammaRamp(red_ramp, NULL, NULL);
|
||||
}
|
||||
SDL_Delay(1 * 1000);
|
||||
|
||||
SDL_Quit();
|
||||
return(0);
|
||||
SDL_Quit();
|
||||
return (0);
|
||||
}
|
||||
|
||||
1376
test/testgl.c
1376
test/testgl.c
File diff suppressed because it is too large
Load Diff
110
test/testhread.c
110
test/testhread.c
@@ -11,72 +11,76 @@
|
||||
static int alive = 0;
|
||||
|
||||
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
||||
static void quit(int rc)
|
||||
static void
|
||||
quit(int rc)
|
||||
{
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
}
|
||||
|
||||
int SDLCALL ThreadFunc(void *data)
|
||||
int SDLCALL
|
||||
ThreadFunc(void *data)
|
||||
{
|
||||
printf("Started thread %s: My thread id is %u\n",
|
||||
(char *)data, SDL_ThreadID());
|
||||
while ( alive ) {
|
||||
printf("Thread '%s' is alive!\n", (char *)data);
|
||||
SDL_Delay(1*1000);
|
||||
}
|
||||
printf("Thread '%s' exiting!\n", (char *)data);
|
||||
return(0);
|
||||
printf("Started thread %s: My thread id is %u\n",
|
||||
(char *) data, SDL_ThreadID());
|
||||
while (alive) {
|
||||
printf("Thread '%s' is alive!\n", (char *) data);
|
||||
SDL_Delay(1 * 1000);
|
||||
}
|
||||
printf("Thread '%s' exiting!\n", (char *) data);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void killed(int sig)
|
||||
static void
|
||||
killed(int sig)
|
||||
{
|
||||
printf("Killed with SIGTERM, waiting 5 seconds to exit\n");
|
||||
SDL_Delay(5*1000);
|
||||
alive = 0;
|
||||
quit(0);
|
||||
printf("Killed with SIGTERM, waiting 5 seconds to exit\n");
|
||||
SDL_Delay(5 * 1000);
|
||||
alive = 0;
|
||||
quit(0);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
SDL_Thread *thread;
|
||||
SDL_Thread *thread;
|
||||
|
||||
/* Load the SDL library */
|
||||
if ( SDL_Init(0) < 0 ) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
|
||||
return(1);
|
||||
}
|
||||
/* Load the SDL library */
|
||||
if (SDL_Init(0) < 0) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return (1);
|
||||
}
|
||||
|
||||
alive = 1;
|
||||
thread = SDL_CreateThread(ThreadFunc, "#1");
|
||||
if ( thread == NULL ) {
|
||||
fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
SDL_Delay(5*1000);
|
||||
printf("Waiting for thread #1\n");
|
||||
alive = 0;
|
||||
SDL_WaitThread(thread, NULL);
|
||||
alive = 1;
|
||||
thread = SDL_CreateThread(ThreadFunc, "#1");
|
||||
if (thread == NULL) {
|
||||
fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
SDL_Delay(5 * 1000);
|
||||
printf("Waiting for thread #1\n");
|
||||
alive = 0;
|
||||
SDL_WaitThread(thread, NULL);
|
||||
|
||||
alive = 1;
|
||||
thread = SDL_CreateThread(ThreadFunc, "#2");
|
||||
if ( thread == NULL ) {
|
||||
fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
SDL_Delay(5*1000);
|
||||
printf("Killing thread #2\n");
|
||||
SDL_KillThread(thread);
|
||||
alive = 1;
|
||||
thread = SDL_CreateThread(ThreadFunc, "#2");
|
||||
if (thread == NULL) {
|
||||
fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
SDL_Delay(5 * 1000);
|
||||
printf("Killing thread #2\n");
|
||||
SDL_KillThread(thread);
|
||||
|
||||
alive = 1;
|
||||
signal(SIGTERM, killed);
|
||||
thread = SDL_CreateThread(ThreadFunc, "#3");
|
||||
if ( thread == NULL ) {
|
||||
fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
raise(SIGTERM);
|
||||
alive = 1;
|
||||
signal(SIGTERM, killed);
|
||||
thread = SDL_CreateThread(ThreadFunc, "#3");
|
||||
if (thread == NULL) {
|
||||
fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
raise(SIGTERM);
|
||||
|
||||
SDL_Quit(); /* Never reached */
|
||||
return(0); /* Never reached */
|
||||
SDL_Quit(); /* Never reached */
|
||||
return (0); /* Never reached */
|
||||
}
|
||||
|
||||
118
test/testiconv.c
118
test/testiconv.c
@@ -3,67 +3,71 @@
|
||||
|
||||
#include "SDL.h"
|
||||
|
||||
static size_t widelen(char *data)
|
||||
static size_t
|
||||
widelen(char *data)
|
||||
{
|
||||
size_t len = 0;
|
||||
Uint32 *p = (Uint32 *)data;
|
||||
while(*p++) {
|
||||
++len;
|
||||
}
|
||||
return len;
|
||||
size_t len = 0;
|
||||
Uint32 *p = (Uint32 *) data;
|
||||
while (*p++) {
|
||||
++len;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
const char * formats[] = {
|
||||
"UTF8",
|
||||
"UTF-8",
|
||||
"UTF16BE",
|
||||
"UTF-16BE",
|
||||
"UTF16LE",
|
||||
"UTF-16LE",
|
||||
"UTF32BE",
|
||||
"UTF-32BE",
|
||||
"UTF32LE",
|
||||
"UTF-32LE",
|
||||
"UCS4",
|
||||
"UCS-4",
|
||||
};
|
||||
char buffer[BUFSIZ];
|
||||
char *ucs4;
|
||||
char *test[2];
|
||||
int i, index = 0;
|
||||
FILE *file;
|
||||
int errors = 0;
|
||||
const char *formats[] = {
|
||||
"UTF8",
|
||||
"UTF-8",
|
||||
"UTF16BE",
|
||||
"UTF-16BE",
|
||||
"UTF16LE",
|
||||
"UTF-16LE",
|
||||
"UTF32BE",
|
||||
"UTF-32BE",
|
||||
"UTF32LE",
|
||||
"UTF-32LE",
|
||||
"UCS4",
|
||||
"UCS-4",
|
||||
};
|
||||
char buffer[BUFSIZ];
|
||||
char *ucs4;
|
||||
char *test[2];
|
||||
int i, index = 0;
|
||||
FILE *file;
|
||||
int errors = 0;
|
||||
|
||||
if ( !argv[1] ) {
|
||||
argv[1] = "utf8.txt";
|
||||
}
|
||||
file = fopen(argv[1], "rb");
|
||||
if ( !file ) {
|
||||
fprintf(stderr, "Unable to open %s\n", argv[1]);
|
||||
return (1);
|
||||
}
|
||||
if (!argv[1]) {
|
||||
argv[1] = "utf8.txt";
|
||||
}
|
||||
file = fopen(argv[1], "rb");
|
||||
if (!file) {
|
||||
fprintf(stderr, "Unable to open %s\n", argv[1]);
|
||||
return (1);
|
||||
}
|
||||
|
||||
while ( fgets(buffer, sizeof(buffer), file) ) {
|
||||
/* Convert to UCS-4 */
|
||||
size_t len;
|
||||
ucs4 = SDL_iconv_string("UCS-4", "UTF-8", buffer, SDL_strlen(buffer)+1);
|
||||
len = (widelen(ucs4)+1)*4;
|
||||
for ( i = 0; i < SDL_arraysize(formats); ++i ) {
|
||||
test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len);
|
||||
test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len);
|
||||
if ( SDL_memcmp(test[1], ucs4, len) != 0 ) {
|
||||
fprintf(stderr, "FAIL: %s\n", formats[i]);
|
||||
++errors;
|
||||
}
|
||||
SDL_free(test[0]);
|
||||
SDL_free(test[1]);
|
||||
}
|
||||
test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
|
||||
SDL_free(ucs4);
|
||||
fputs(test[0], stdout);
|
||||
SDL_free(test[0]);
|
||||
}
|
||||
return (errors ? errors + 1 : 0);
|
||||
while (fgets(buffer, sizeof(buffer), file)) {
|
||||
/* Convert to UCS-4 */
|
||||
size_t len;
|
||||
ucs4 =
|
||||
SDL_iconv_string("UCS-4", "UTF-8", buffer,
|
||||
SDL_strlen(buffer) + 1);
|
||||
len = (widelen(ucs4) + 1) * 4;
|
||||
for (i = 0; i < SDL_arraysize(formats); ++i) {
|
||||
test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len);
|
||||
test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len);
|
||||
if (SDL_memcmp(test[1], ucs4, len) != 0) {
|
||||
fprintf(stderr, "FAIL: %s\n", formats[i]);
|
||||
++errors;
|
||||
}
|
||||
SDL_free(test[0]);
|
||||
SDL_free(test[1]);
|
||||
}
|
||||
test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
|
||||
SDL_free(ucs4);
|
||||
fputs(test[0], stdout);
|
||||
SDL_free(test[0]);
|
||||
}
|
||||
return (errors ? errors + 1 : 0);
|
||||
}
|
||||
|
||||
@@ -10,169 +10,167 @@
|
||||
#define SCREEN_WIDTH 640
|
||||
#define SCREEN_HEIGHT 480
|
||||
|
||||
void WatchJoystick(SDL_Joystick *joystick)
|
||||
void
|
||||
WatchJoystick(SDL_Joystick * joystick)
|
||||
{
|
||||
SDL_Surface *screen;
|
||||
const char *name;
|
||||
int i, done;
|
||||
SDL_Event event;
|
||||
int x, y, draw;
|
||||
SDL_Rect axis_area[2];
|
||||
SDL_Surface *screen;
|
||||
const char *name;
|
||||
int i, done;
|
||||
SDL_Event event;
|
||||
int x, y, draw;
|
||||
SDL_Rect axis_area[6][2];
|
||||
|
||||
/* Set a video mode to display joystick axis position */
|
||||
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0);
|
||||
if ( screen == NULL ) {
|
||||
fprintf(stderr, "Couldn't set video mode: %s\n",SDL_GetError());
|
||||
return;
|
||||
}
|
||||
/* Set a video mode to display joystick axis position */
|
||||
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0);
|
||||
if (screen == NULL) {
|
||||
fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
|
||||
return;
|
||||
}
|
||||
|
||||
/* Print info about the joystick we are watching */
|
||||
name = SDL_JoystickName(SDL_JoystickIndex(joystick));
|
||||
printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick),
|
||||
name ? name : "Unknown Joystick");
|
||||
printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
|
||||
SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
|
||||
SDL_JoystickNumBalls(joystick),SDL_JoystickNumButtons(joystick));
|
||||
/* Print info about the joystick we are watching */
|
||||
name = SDL_JoystickName(SDL_JoystickIndex(joystick));
|
||||
printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick),
|
||||
name ? name : "Unknown Joystick");
|
||||
printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
|
||||
SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
|
||||
SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
|
||||
|
||||
/* Initialize drawing rectangles */
|
||||
memset(axis_area, 0, (sizeof axis_area));
|
||||
draw = 0;
|
||||
/* Initialize drawing rectangles */
|
||||
memset(axis_area, 0, (sizeof axis_area));
|
||||
draw = 0;
|
||||
|
||||
/* Loop, getting joystick events! */
|
||||
done = 0;
|
||||
while ( ! done ) {
|
||||
while ( SDL_PollEvent(&event) ) {
|
||||
switch (event.type) {
|
||||
case SDL_JOYAXISMOTION:
|
||||
printf("Joystick %d axis %d value: %d\n",
|
||||
event.jaxis.which,
|
||||
event.jaxis.axis,
|
||||
event.jaxis.value);
|
||||
break;
|
||||
case SDL_JOYHATMOTION:
|
||||
printf("Joystick %d hat %d value:",
|
||||
event.jhat.which,
|
||||
event.jhat.hat);
|
||||
if ( event.jhat.value == SDL_HAT_CENTERED )
|
||||
printf(" centered");
|
||||
if ( event.jhat.value & SDL_HAT_UP )
|
||||
printf(" up");
|
||||
if ( event.jhat.value & SDL_HAT_RIGHT )
|
||||
printf(" right");
|
||||
if ( event.jhat.value & SDL_HAT_DOWN )
|
||||
printf(" down");
|
||||
if ( event.jhat.value & SDL_HAT_LEFT )
|
||||
printf(" left");
|
||||
printf("\n");
|
||||
break;
|
||||
case SDL_JOYBALLMOTION:
|
||||
printf("Joystick %d ball %d delta: (%d,%d)\n",
|
||||
event.jball.which,
|
||||
event.jball.ball,
|
||||
event.jball.xrel,
|
||||
event.jball.yrel);
|
||||
break;
|
||||
case SDL_JOYBUTTONDOWN:
|
||||
printf("Joystick %d button %d down\n",
|
||||
event.jbutton.which,
|
||||
event.jbutton.button);
|
||||
break;
|
||||
case SDL_JOYBUTTONUP:
|
||||
printf("Joystick %d button %d up\n",
|
||||
event.jbutton.which,
|
||||
event.jbutton.button);
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
if ( event.key.keysym.sym != SDLK_ESCAPE ) {
|
||||
break;
|
||||
}
|
||||
/* Fall through to signal quit */
|
||||
case SDL_QUIT:
|
||||
done = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* Update visual joystick state */
|
||||
for ( i=0; i<SDL_JoystickNumButtons(joystick); ++i ) {
|
||||
SDL_Rect area;
|
||||
/* Loop, getting joystick events! */
|
||||
done = 0;
|
||||
while (!done) {
|
||||
while (SDL_PollEvent(&event)) {
|
||||
switch (event.type) {
|
||||
case SDL_JOYAXISMOTION:
|
||||
printf("Joystick %d axis %d value: %d\n",
|
||||
event.jaxis.which,
|
||||
event.jaxis.axis, event.jaxis.value);
|
||||
break;
|
||||
case SDL_JOYHATMOTION:
|
||||
printf("Joystick %d hat %d value:",
|
||||
event.jhat.which, event.jhat.hat);
|
||||
if (event.jhat.value == SDL_HAT_CENTERED)
|
||||
printf(" centered");
|
||||
if (event.jhat.value & SDL_HAT_UP)
|
||||
printf(" up");
|
||||
if (event.jhat.value & SDL_HAT_RIGHT)
|
||||
printf(" right");
|
||||
if (event.jhat.value & SDL_HAT_DOWN)
|
||||
printf(" down");
|
||||
if (event.jhat.value & SDL_HAT_LEFT)
|
||||
printf(" left");
|
||||
printf("\n");
|
||||
break;
|
||||
case SDL_JOYBALLMOTION:
|
||||
printf("Joystick %d ball %d delta: (%d,%d)\n",
|
||||
event.jball.which,
|
||||
event.jball.ball, event.jball.xrel, event.jball.yrel);
|
||||
break;
|
||||
case SDL_JOYBUTTONDOWN:
|
||||
printf("Joystick %d button %d down\n",
|
||||
event.jbutton.which, event.jbutton.button);
|
||||
break;
|
||||
case SDL_JOYBUTTONUP:
|
||||
printf("Joystick %d button %d up\n",
|
||||
event.jbutton.which, event.jbutton.button);
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
if (event.key.keysym.sym != SDLK_ESCAPE) {
|
||||
break;
|
||||
}
|
||||
/* Fall through to signal quit */
|
||||
case SDL_QUIT:
|
||||
done = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* Update visual joystick state */
|
||||
for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
|
||||
SDL_Rect area;
|
||||
|
||||
area.x = i*34;
|
||||
area.y = SCREEN_HEIGHT-34;
|
||||
area.w = 32;
|
||||
area.h = 32;
|
||||
if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
|
||||
SDL_FillRect(screen, &area, 0xFFFF);
|
||||
} else {
|
||||
SDL_FillRect(screen, &area, 0x0000);
|
||||
}
|
||||
SDL_UpdateRects(screen, 1, &area);
|
||||
}
|
||||
area.x = i * 34;
|
||||
area.y = SCREEN_HEIGHT - 34;
|
||||
area.w = 32;
|
||||
area.h = 32;
|
||||
if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
|
||||
SDL_FillRect(screen, &area, 0xFFFF);
|
||||
} else {
|
||||
SDL_FillRect(screen, &area, 0x0000);
|
||||
}
|
||||
SDL_UpdateRects(screen, 1, &area);
|
||||
}
|
||||
|
||||
/* Erase previous axes */
|
||||
SDL_FillRect(screen, &axis_area[draw], 0x0000);
|
||||
for (i = 0;
|
||||
i < SDL_JoystickNumAxes(joystick) / 2
|
||||
&& i < SDL_arraysize(axis_area); ++i) {
|
||||
/* Erase previous axes */
|
||||
SDL_FillRect(screen, &axis_area[i][draw], 0x0000);
|
||||
|
||||
/* Draw the X/Y axis */
|
||||
draw = !draw;
|
||||
x = (((int)SDL_JoystickGetAxis(joystick, 0))+32768);
|
||||
x *= SCREEN_WIDTH;
|
||||
x /= 65535;
|
||||
if ( x < 0 ) {
|
||||
x = 0;
|
||||
} else
|
||||
if ( x > (SCREEN_WIDTH-16) ) {
|
||||
x = SCREEN_WIDTH-16;
|
||||
}
|
||||
y = (((int)SDL_JoystickGetAxis(joystick, 1))+32768);
|
||||
y *= SCREEN_HEIGHT;
|
||||
y /= 65535;
|
||||
if ( y < 0 ) {
|
||||
y = 0;
|
||||
} else
|
||||
if ( y > (SCREEN_HEIGHT-16) ) {
|
||||
y = SCREEN_HEIGHT-16;
|
||||
}
|
||||
axis_area[draw].x = (Sint16)x;
|
||||
axis_area[draw].y = (Sint16)y;
|
||||
axis_area[draw].w = 16;
|
||||
axis_area[draw].h = 16;
|
||||
SDL_FillRect(screen, &axis_area[draw], 0xFFFF);
|
||||
/* Draw the X/Y axis */
|
||||
draw = !draw;
|
||||
x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768);
|
||||
x *= SCREEN_WIDTH;
|
||||
x /= 65535;
|
||||
if (x < 0) {
|
||||
x = 0;
|
||||
} else if (x > (SCREEN_WIDTH - 16)) {
|
||||
x = SCREEN_WIDTH - 16;
|
||||
}
|
||||
y = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 1)) + 32768);
|
||||
y *= SCREEN_HEIGHT;
|
||||
y /= 65535;
|
||||
if (y < 0) {
|
||||
y = 0;
|
||||
} else if (y > (SCREEN_HEIGHT - 16)) {
|
||||
y = SCREEN_HEIGHT - 16;
|
||||
}
|
||||
axis_area[i][draw].x = (Sint16) x;
|
||||
axis_area[i][draw].y = (Sint16) y;
|
||||
axis_area[i][draw].w = 16;
|
||||
axis_area[i][draw].h = 16;
|
||||
SDL_FillRect(screen, &axis_area[i][draw], 0xFFFF);
|
||||
|
||||
SDL_UpdateRects(screen, 2, axis_area);
|
||||
}
|
||||
SDL_UpdateRects(screen, 2, axis_area[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
const char *name;
|
||||
int i;
|
||||
SDL_Joystick *joystick;
|
||||
const char *name;
|
||||
int i;
|
||||
SDL_Joystick *joystick;
|
||||
|
||||
/* Initialize SDL (Note: video is required to start event loop) */
|
||||
if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK) < 0 ) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
|
||||
exit(1);
|
||||
}
|
||||
/* Initialize SDL (Note: video is required to start event loop) */
|
||||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Print information about the joysticks */
|
||||
printf("There are %d joysticks attached\n", SDL_NumJoysticks());
|
||||
for ( i=0; i<SDL_NumJoysticks(); ++i ) {
|
||||
name = SDL_JoystickName(i);
|
||||
printf("Joystick %d: %s\n",i,name ? name : "Unknown Joystick");
|
||||
}
|
||||
/* Print information about the joysticks */
|
||||
printf("There are %d joysticks attached\n", SDL_NumJoysticks());
|
||||
for (i = 0; i < SDL_NumJoysticks(); ++i) {
|
||||
name = SDL_JoystickName(i);
|
||||
printf("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
|
||||
}
|
||||
|
||||
if ( argv[1] ) {
|
||||
joystick = SDL_JoystickOpen(atoi(argv[1]));
|
||||
if ( joystick == NULL ) {
|
||||
printf("Couldn't open joystick %d: %s\n", atoi(argv[1]),
|
||||
SDL_GetError());
|
||||
} else {
|
||||
WatchJoystick(joystick);
|
||||
SDL_JoystickClose(joystick);
|
||||
}
|
||||
}
|
||||
SDL_QuitSubSystem(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK);
|
||||
if (argv[1]) {
|
||||
joystick = SDL_JoystickOpen(atoi(argv[1]));
|
||||
if (joystick == NULL) {
|
||||
printf("Couldn't open joystick %d: %s\n", atoi(argv[1]),
|
||||
SDL_GetError());
|
||||
} else {
|
||||
WatchJoystick(joystick);
|
||||
SDL_JoystickClose(joystick);
|
||||
}
|
||||
}
|
||||
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
|
||||
|
||||
return(0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
@@ -8,18 +8,18 @@
|
||||
|
||||
#include "SDL.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
SDLKey key;
|
||||
SDLKey key;
|
||||
|
||||
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n",
|
||||
SDL_GetError());
|
||||
exit(1);
|
||||
}
|
||||
for ( key=SDLK_FIRST; key<SDLK_LAST; ++key ) {
|
||||
printf("Key #%d, \"%s\"\n", key, SDL_GetKeyName(key));
|
||||
}
|
||||
SDL_Quit();
|
||||
return(0);
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
exit(1);
|
||||
}
|
||||
for (key = SDLK_FIRST; key < SDLK_LAST; ++key) {
|
||||
printf("Key #%d, \"%s\"\n", key, SDL_GetKeyName(key));
|
||||
}
|
||||
SDL_Quit();
|
||||
return (0);
|
||||
}
|
||||
|
||||
154
test/testlock.c
154
test/testlock.c
@@ -19,84 +19,92 @@ static volatile int doterminate = 0;
|
||||
* SDL_Quit() shouldn't be used with atexit() directly because
|
||||
* calling conventions may differ...
|
||||
*/
|
||||
static void SDL_Quit_Wrapper(void)
|
||||
static void
|
||||
SDL_Quit_Wrapper(void)
|
||||
{
|
||||
SDL_Quit();
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
void printid(void)
|
||||
void
|
||||
printid(void)
|
||||
{
|
||||
printf("Process %u: exiting\n", SDL_ThreadID());
|
||||
}
|
||||
|
||||
void terminate(int sig)
|
||||
{
|
||||
signal(SIGINT, terminate);
|
||||
doterminate = 1;
|
||||
}
|
||||
void closemutex(int sig)
|
||||
{
|
||||
Uint32 id = SDL_ThreadID();
|
||||
int i;
|
||||
printf("Process %u: Cleaning up...\n", id == mainthread ? 0 : id);
|
||||
for ( i=0; i<6; ++i )
|
||||
SDL_KillThread(threads[i]);
|
||||
SDL_DestroyMutex(mutex);
|
||||
exit(sig);
|
||||
}
|
||||
int SDLCALL Run(void *data)
|
||||
{
|
||||
if ( SDL_ThreadID() == mainthread )
|
||||
signal(SIGTERM, closemutex);
|
||||
while ( 1 ) {
|
||||
printf("Process %u ready to work\n", SDL_ThreadID());
|
||||
if ( SDL_mutexP(mutex) < 0 ) {
|
||||
fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError());
|
||||
exit(1);
|
||||
}
|
||||
printf("Process %u, working!\n", SDL_ThreadID());
|
||||
SDL_Delay(1*1000);
|
||||
printf("Process %u, done!\n", SDL_ThreadID());
|
||||
if ( SDL_mutexV(mutex) < 0 ) {
|
||||
fprintf(stderr, "Couldn't unlock mutex: %s", SDL_GetError());
|
||||
exit(1);
|
||||
}
|
||||
/* If this sleep isn't done, then threads may starve */
|
||||
SDL_Delay(10);
|
||||
if (SDL_ThreadID() == mainthread && doterminate) {
|
||||
printf("Process %u: raising SIGTERM\n", SDL_ThreadID());
|
||||
raise(SIGTERM);
|
||||
}
|
||||
}
|
||||
return(0);
|
||||
printf("Process %u: exiting\n", SDL_ThreadID());
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
void
|
||||
terminate(int sig)
|
||||
{
|
||||
int i;
|
||||
int maxproc = 6;
|
||||
|
||||
/* Load the SDL library */
|
||||
if ( SDL_Init(0) < 0 ) {
|
||||
fprintf(stderr, "%s\n", SDL_GetError());
|
||||
exit(1);
|
||||
}
|
||||
atexit(SDL_Quit_Wrapper);
|
||||
|
||||
if ( (mutex=SDL_CreateMutex()) == NULL ) {
|
||||
fprintf(stderr, "Couldn't create mutex: %s\n", SDL_GetError());
|
||||
exit(1);
|
||||
}
|
||||
|
||||
mainthread = SDL_ThreadID();
|
||||
printf("Main thread: %u\n", mainthread);
|
||||
atexit(printid);
|
||||
for ( i=0; i<maxproc; ++i ) {
|
||||
if ( (threads[i]=SDL_CreateThread(Run, NULL)) == NULL )
|
||||
fprintf(stderr, "Couldn't create thread!\n");
|
||||
}
|
||||
signal(SIGINT, terminate);
|
||||
Run(NULL);
|
||||
|
||||
return(0); /* Never reached */
|
||||
signal(SIGINT, terminate);
|
||||
doterminate = 1;
|
||||
}
|
||||
|
||||
void
|
||||
closemutex(int sig)
|
||||
{
|
||||
Uint32 id = SDL_ThreadID();
|
||||
int i;
|
||||
printf("Process %u: Cleaning up...\n", id == mainthread ? 0 : id);
|
||||
for (i = 0; i < 6; ++i)
|
||||
SDL_KillThread(threads[i]);
|
||||
SDL_DestroyMutex(mutex);
|
||||
exit(sig);
|
||||
}
|
||||
|
||||
int SDLCALL
|
||||
Run(void *data)
|
||||
{
|
||||
if (SDL_ThreadID() == mainthread)
|
||||
signal(SIGTERM, closemutex);
|
||||
while (1) {
|
||||
printf("Process %u ready to work\n", SDL_ThreadID());
|
||||
if (SDL_mutexP(mutex) < 0) {
|
||||
fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError());
|
||||
exit(1);
|
||||
}
|
||||
printf("Process %u, working!\n", SDL_ThreadID());
|
||||
SDL_Delay(1 * 1000);
|
||||
printf("Process %u, done!\n", SDL_ThreadID());
|
||||
if (SDL_mutexV(mutex) < 0) {
|
||||
fprintf(stderr, "Couldn't unlock mutex: %s", SDL_GetError());
|
||||
exit(1);
|
||||
}
|
||||
/* If this sleep isn't done, then threads may starve */
|
||||
SDL_Delay(10);
|
||||
if (SDL_ThreadID() == mainthread && doterminate) {
|
||||
printf("Process %u: raising SIGTERM\n", SDL_ThreadID());
|
||||
raise(SIGTERM);
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
int maxproc = 6;
|
||||
|
||||
/* Load the SDL library */
|
||||
if (SDL_Init(0) < 0) {
|
||||
fprintf(stderr, "%s\n", SDL_GetError());
|
||||
exit(1);
|
||||
}
|
||||
atexit(SDL_Quit_Wrapper);
|
||||
|
||||
if ((mutex = SDL_CreateMutex()) == NULL) {
|
||||
fprintf(stderr, "Couldn't create mutex: %s\n", SDL_GetError());
|
||||
exit(1);
|
||||
}
|
||||
|
||||
mainthread = SDL_ThreadID();
|
||||
printf("Main thread: %u\n", mainthread);
|
||||
atexit(printid);
|
||||
for (i = 0; i < maxproc; ++i) {
|
||||
if ((threads[i] = SDL_CreateThread(Run, NULL)) == NULL)
|
||||
fprintf(stderr, "Couldn't create thread!\n");
|
||||
}
|
||||
signal(SIGINT, terminate);
|
||||
Run(NULL);
|
||||
|
||||
return (0); /* Never reached */
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -36,91 +36,96 @@
|
||||
* in Gimp, saving in PPM ascii format and formatting with Emacs macros.
|
||||
*/
|
||||
static SDL_Color wavemap[] = {
|
||||
{0,2,103}, {0,7,110}, {0,13,117}, {0,19,125},
|
||||
{0,25,133}, {0,31,141}, {0,37,150}, {0,43,158},
|
||||
{0,49,166}, {0,55,174}, {0,61,182}, {0,67,190},
|
||||
{0,73,198}, {0,79,206}, {0,86,214}, {0,96,220},
|
||||
{5,105,224}, {12,112,226}, {19,120,227}, {26,128,229},
|
||||
{33,135,230}, {40,143,232}, {47,150,234}, {54,158,236},
|
||||
{61,165,238}, {68,173,239}, {75,180,241}, {82,188,242},
|
||||
{89,195,244}, {96,203,246}, {103,210,248}, {112,218,250},
|
||||
{124,224,250}, {135,226,251}, {146,229,251}, {156,231,252},
|
||||
{167,233,252}, {178,236,252}, {189,238,252}, {200,240,252},
|
||||
{211,242,252}, {222,244,252}, {233,247,252}, {242,249,252},
|
||||
{237,250,252}, {209,251,252}, {174,251,252}, {138,252,252},
|
||||
{102,251,252}, {63,250,252}, {24,243,252}, {7,225,252},
|
||||
{4,203,252}, {3,181,252}, {2,158,252}, {1,136,251},
|
||||
{0,111,248}, {0,82,234}, {0,63,213}, {0,50,192},
|
||||
{0,39,172}, {0,28,152}, {0,17,132}, {0,7,114}
|
||||
{0, 2, 103}, {0, 7, 110}, {0, 13, 117}, {0, 19, 125},
|
||||
{0, 25, 133}, {0, 31, 141}, {0, 37, 150}, {0, 43, 158},
|
||||
{0, 49, 166}, {0, 55, 174}, {0, 61, 182}, {0, 67, 190},
|
||||
{0, 73, 198}, {0, 79, 206}, {0, 86, 214}, {0, 96, 220},
|
||||
{5, 105, 224}, {12, 112, 226}, {19, 120, 227}, {26, 128, 229},
|
||||
{33, 135, 230}, {40, 143, 232}, {47, 150, 234}, {54, 158, 236},
|
||||
{61, 165, 238}, {68, 173, 239}, {75, 180, 241}, {82, 188, 242},
|
||||
{89, 195, 244}, {96, 203, 246}, {103, 210, 248}, {112, 218, 250},
|
||||
{124, 224, 250}, {135, 226, 251}, {146, 229, 251}, {156, 231, 252},
|
||||
{167, 233, 252}, {178, 236, 252}, {189, 238, 252}, {200, 240, 252},
|
||||
{211, 242, 252}, {222, 244, 252}, {233, 247, 252}, {242, 249, 252},
|
||||
{237, 250, 252}, {209, 251, 252}, {174, 251, 252}, {138, 252, 252},
|
||||
{102, 251, 252}, {63, 250, 252}, {24, 243, 252}, {7, 225, 252},
|
||||
{4, 203, 252}, {3, 181, 252}, {2, 158, 252}, {1, 136, 251},
|
||||
{0, 111, 248}, {0, 82, 234}, {0, 63, 213}, {0, 50, 192},
|
||||
{0, 39, 172}, {0, 28, 152}, {0, 17, 132}, {0, 7, 114}
|
||||
};
|
||||
|
||||
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
||||
static void quit(int rc)
|
||||
static void
|
||||
quit(int rc)
|
||||
{
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
}
|
||||
|
||||
static void sdlerr(char *when)
|
||||
static void
|
||||
sdlerr(char *when)
|
||||
{
|
||||
fprintf(stderr, "SDL error: %s: %s\n", when, SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
|
||||
/* create a background surface */
|
||||
static SDL_Surface *make_bg(SDL_Surface *screen, int startcol)
|
||||
static SDL_Surface *
|
||||
make_bg(SDL_Surface * screen, int startcol)
|
||||
{
|
||||
int i;
|
||||
SDL_Surface *bg = SDL_CreateRGBSurface(SDL_SWSURFACE, screen->w, screen->h,
|
||||
8, 0, 0, 0, 0);
|
||||
if(!bg)
|
||||
sdlerr("creating background surface");
|
||||
SDL_Surface *bg =
|
||||
SDL_CreateRGBSurface(SDL_SWSURFACE, screen->w, screen->h,
|
||||
8, 0, 0, 0, 0);
|
||||
if (!bg)
|
||||
sdlerr("creating background surface");
|
||||
|
||||
/* set the palette to the logical screen palette so that blits
|
||||
won't be translated */
|
||||
SDL_SetColors(bg, screen->format->palette->colors, 0, 256);
|
||||
|
||||
/* Make a wavy background pattern using colours 0-63 */
|
||||
if(SDL_LockSurface(bg) < 0)
|
||||
sdlerr("locking background");
|
||||
for(i = 0; i < SCRH; i++) {
|
||||
Uint8 *p = (Uint8 *)bg->pixels + i * bg->pitch;
|
||||
int j, d;
|
||||
d = 0;
|
||||
for(j = 0; j < SCRW; j++) {
|
||||
int v = MAX(d, -2);
|
||||
v = MIN(v, 2);
|
||||
if(i > 0)
|
||||
v += p[-bg->pitch] + 65 - startcol;
|
||||
p[j] = startcol + (v & 63);
|
||||
d += ((rand() >> 3) % 3) - 1;
|
||||
}
|
||||
if (SDL_LockSurface(bg) < 0)
|
||||
sdlerr("locking background");
|
||||
for (i = 0; i < SCRH; i++) {
|
||||
Uint8 *p = (Uint8 *) bg->pixels + i * bg->pitch;
|
||||
int j, d;
|
||||
d = 0;
|
||||
for (j = 0; j < SCRW; j++) {
|
||||
int v = MAX(d, -2);
|
||||
v = MIN(v, 2);
|
||||
if (i > 0)
|
||||
v += p[-bg->pitch] + 65 - startcol;
|
||||
p[j] = startcol + (v & 63);
|
||||
d += ((rand() >> 3) % 3) - 1;
|
||||
}
|
||||
}
|
||||
SDL_UnlockSurface(bg);
|
||||
return(bg);
|
||||
return (bg);
|
||||
}
|
||||
|
||||
/*
|
||||
* Return a surface flipped horisontally. Only works for 8bpp;
|
||||
* extension to arbitrary bitness is left as an exercise for the reader.
|
||||
*/
|
||||
static SDL_Surface *hflip(SDL_Surface *s)
|
||||
static SDL_Surface *
|
||||
hflip(SDL_Surface * s)
|
||||
{
|
||||
int i;
|
||||
SDL_Surface *z = SDL_CreateRGBSurface(SDL_SWSURFACE, s->w, s->h, 8,
|
||||
0, 0, 0, 0);
|
||||
0, 0, 0, 0);
|
||||
/* copy palette */
|
||||
SDL_SetColors(z, s->format->palette->colors,
|
||||
0, s->format->palette->ncolors);
|
||||
if(SDL_LockSurface(s) < 0 || SDL_LockSurface(z) < 0)
|
||||
sdlerr("locking flip images");
|
||||
0, s->format->palette->ncolors);
|
||||
if (SDL_LockSurface(s) < 0 || SDL_LockSurface(z) < 0)
|
||||
sdlerr("locking flip images");
|
||||
|
||||
for(i = 0; i < s->h; i++) {
|
||||
int j;
|
||||
Uint8 *from = (Uint8 *)s->pixels + i * s->pitch;
|
||||
Uint8 *to = (Uint8 *)z->pixels + i * z->pitch + s->w - 1;
|
||||
for(j = 0; j < s->w; j++)
|
||||
to[-j] = from[j];
|
||||
for (i = 0; i < s->h; i++) {
|
||||
int j;
|
||||
Uint8 *from = (Uint8 *) s->pixels + i * s->pitch;
|
||||
Uint8 *to = (Uint8 *) z->pixels + i * z->pitch + s->w - 1;
|
||||
for (j = 0; j < s->w; j++)
|
||||
to[-j] = from[j];
|
||||
}
|
||||
|
||||
SDL_UnlockSurface(z);
|
||||
@@ -128,7 +133,8 @@ static SDL_Surface *hflip(SDL_Surface *s)
|
||||
return z;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
SDL_Color cmap[256];
|
||||
SDL_Surface *screen;
|
||||
@@ -143,47 +149,49 @@ int main(int argc, char **argv)
|
||||
int gamma_fade = 0;
|
||||
int gamma_ramp = 0;
|
||||
|
||||
if(SDL_Init(SDL_INIT_VIDEO) < 0)
|
||||
sdlerr("initialising SDL");
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0)
|
||||
sdlerr("initialising SDL");
|
||||
|
||||
while(--argc) {
|
||||
++argv;
|
||||
if(strcmp(*argv, "-hw") == 0)
|
||||
vidflags |= SDL_HWSURFACE;
|
||||
else if(strcmp(*argv, "-fullscreen") == 0)
|
||||
vidflags |= SDL_FULLSCREEN;
|
||||
else if(strcmp(*argv, "-nofade") == 0)
|
||||
fade_max = 1;
|
||||
else if(strcmp(*argv, "-gamma") == 0)
|
||||
gamma_fade = 1;
|
||||
else if(strcmp(*argv, "-gammaramp") == 0)
|
||||
gamma_ramp = 1;
|
||||
else {
|
||||
fprintf(stderr,
|
||||
"usage: testpalette "
|
||||
" [-hw] [-fullscreen] [-nofade] [-gamma] [-gammaramp]\n");
|
||||
quit(1);
|
||||
}
|
||||
while (--argc) {
|
||||
++argv;
|
||||
if (strcmp(*argv, "-hw") == 0)
|
||||
vidflags |= SDL_HWSURFACE;
|
||||
else if (strcmp(*argv, "-fullscreen") == 0)
|
||||
vidflags |= SDL_FULLSCREEN;
|
||||
else if (strcmp(*argv, "-nofade") == 0)
|
||||
fade_max = 1;
|
||||
else if (strcmp(*argv, "-gamma") == 0)
|
||||
gamma_fade = 1;
|
||||
else if (strcmp(*argv, "-gammaramp") == 0)
|
||||
gamma_ramp = 1;
|
||||
else {
|
||||
fprintf(stderr,
|
||||
"usage: testpalette "
|
||||
" [-hw] [-fullscreen] [-nofade] [-gamma] [-gammaramp]\n");
|
||||
quit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Ask explicitly for 8bpp and a hardware palette */
|
||||
if((screen = SDL_SetVideoMode(SCRW, SCRH, 8, vidflags | SDL_HWPALETTE)) == NULL) {
|
||||
fprintf(stderr, "error setting %dx%d 8bpp indexed mode: %s\n",
|
||||
SCRW, SCRH, SDL_GetError());
|
||||
quit(1);
|
||||
if ((screen =
|
||||
SDL_SetVideoMode(SCRW, SCRH, 8, vidflags | SDL_HWPALETTE)) == NULL) {
|
||||
fprintf(stderr, "error setting %dx%d 8bpp indexed mode: %s\n",
|
||||
SCRW, SCRH, SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
|
||||
if (vidflags & SDL_FULLSCREEN) SDL_ShowCursor (SDL_FALSE);
|
||||
if (vidflags & SDL_FULLSCREEN)
|
||||
SDL_ShowCursor(SDL_FALSE);
|
||||
|
||||
if((boat[0] = SDL_LoadBMP("sail.bmp")) == NULL)
|
||||
sdlerr("loading sail.bmp");
|
||||
if ((boat[0] = SDL_LoadBMP("sail.bmp")) == NULL)
|
||||
sdlerr("loading sail.bmp");
|
||||
/* We've chosen magenta (#ff00ff) as colour key for the boat */
|
||||
SDL_SetColorKey(boat[0], SDL_SRCCOLORKEY | SDL_RLEACCEL,
|
||||
SDL_MapRGB(boat[0]->format, 0xff, 0x00, 0xff));
|
||||
SDL_MapRGB(boat[0]->format, 0xff, 0x00, 0xff));
|
||||
boatcols = boat[0]->format->palette->ncolors;
|
||||
boat[1] = hflip(boat[0]);
|
||||
SDL_SetColorKey(boat[1], SDL_SRCCOLORKEY | SDL_RLEACCEL,
|
||||
SDL_MapRGB(boat[1]->format, 0xff, 0x00, 0xff));
|
||||
SDL_MapRGB(boat[1]->format, 0xff, 0x00, 0xff));
|
||||
|
||||
/*
|
||||
* First set the physical screen palette to black, so the user won't
|
||||
@@ -196,11 +204,11 @@ int main(int argc, char **argv)
|
||||
* Proper palette management is important when playing games with the
|
||||
* colormap. We have divided the palette as follows:
|
||||
*
|
||||
* index 0..(boatcols-1): used for the boat
|
||||
* index boatcols..(boatcols+63): used for the waves
|
||||
* index 0..(boatcols-1): used for the boat
|
||||
* index boatcols..(boatcols+63): used for the waves
|
||||
*/
|
||||
SDL_SetPalette(screen, SDL_LOGPAL,
|
||||
boat[0]->format->palette->colors, 0, boatcols);
|
||||
boat[0]->format->palette->colors, 0, boatcols);
|
||||
SDL_SetPalette(screen, SDL_LOGPAL, wavemap, boatcols, 64);
|
||||
|
||||
/*
|
||||
@@ -212,18 +220,18 @@ int main(int argc, char **argv)
|
||||
/* save the index of the red colour for later */
|
||||
red = SDL_MapRGB(screen->format, 0xff, 0x00, 0x00);
|
||||
|
||||
bg = make_bg(screen, boatcols); /* make a nice wavy background surface */
|
||||
bg = make_bg(screen, boatcols); /* make a nice wavy background surface */
|
||||
|
||||
/* initial screen contents */
|
||||
if(SDL_BlitSurface(bg, NULL, screen, NULL) < 0)
|
||||
sdlerr("blitting background to screen");
|
||||
SDL_Flip(screen); /* actually put the background on screen */
|
||||
if (SDL_BlitSurface(bg, NULL, screen, NULL) < 0)
|
||||
sdlerr("blitting background to screen");
|
||||
SDL_Flip(screen); /* actually put the background on screen */
|
||||
|
||||
/* determine initial boat placements */
|
||||
for(i = 0; i < NBOATS; i++) {
|
||||
boatx[i] = (rand() % (SCRW + boat[0]->w)) - boat[0]->w;
|
||||
boaty[i] = i * (SCRH - boat[0]->h) / (NBOATS - 1);
|
||||
boatdir[i] = ((rand() >> 5) & 1) * 2 - 1;
|
||||
for (i = 0; i < NBOATS; i++) {
|
||||
boatx[i] = (rand() % (SCRW + boat[0]->w)) - boat[0]->w;
|
||||
boaty[i] = i * (SCRH - boat[0]->h) / (NBOATS - 1);
|
||||
boatdir[i] = ((rand() >> 5) & 1) * 2 - 1;
|
||||
}
|
||||
|
||||
start = SDL_GetTicks();
|
||||
@@ -231,112 +239,113 @@ int main(int argc, char **argv)
|
||||
fade_dir = 1;
|
||||
fade_level = 0;
|
||||
do {
|
||||
SDL_Event e;
|
||||
SDL_Rect updates[NBOATS];
|
||||
SDL_Rect r;
|
||||
int redphase;
|
||||
SDL_Event e;
|
||||
SDL_Rect updates[NBOATS];
|
||||
SDL_Rect r;
|
||||
int redphase;
|
||||
|
||||
/* A small event loop: just exit on any key or mouse button event */
|
||||
while(SDL_PollEvent(&e)) {
|
||||
if(e.type == SDL_KEYDOWN || e.type == SDL_QUIT
|
||||
|| e.type == SDL_MOUSEBUTTONDOWN) {
|
||||
if(fade_dir < 0)
|
||||
fade_level = 0;
|
||||
fade_dir = -1;
|
||||
}
|
||||
}
|
||||
/* A small event loop: just exit on any key or mouse button event */
|
||||
while (SDL_PollEvent(&e)) {
|
||||
if (e.type == SDL_KEYDOWN || e.type == SDL_QUIT
|
||||
|| e.type == SDL_MOUSEBUTTONDOWN) {
|
||||
if (fade_dir < 0)
|
||||
fade_level = 0;
|
||||
fade_dir = -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* move boats */
|
||||
for(i = 0; i < NBOATS; i++) {
|
||||
int old_x = boatx[i];
|
||||
/* update boat position */
|
||||
boatx[i] += boatdir[i] * SPEED;
|
||||
if(boatx[i] <= -boat[0]->w || boatx[i] >= SCRW)
|
||||
boatdir[i] = -boatdir[i];
|
||||
/* move boats */
|
||||
for (i = 0; i < NBOATS; i++) {
|
||||
int old_x = boatx[i];
|
||||
/* update boat position */
|
||||
boatx[i] += boatdir[i] * SPEED;
|
||||
if (boatx[i] <= -boat[0]->w || boatx[i] >= SCRW)
|
||||
boatdir[i] = -boatdir[i];
|
||||
|
||||
/* paint over the old boat position */
|
||||
r.x = old_x;
|
||||
r.y = boaty[i];
|
||||
r.w = boat[0]->w;
|
||||
r.h = boat[0]->h;
|
||||
if(SDL_BlitSurface(bg, &r, screen, &r) < 0)
|
||||
sdlerr("blitting background");
|
||||
/* paint over the old boat position */
|
||||
r.x = old_x;
|
||||
r.y = boaty[i];
|
||||
r.w = boat[0]->w;
|
||||
r.h = boat[0]->h;
|
||||
if (SDL_BlitSurface(bg, &r, screen, &r) < 0)
|
||||
sdlerr("blitting background");
|
||||
|
||||
/* construct update rectangle (bounding box of old and new pos) */
|
||||
updates[i].x = MIN(old_x, boatx[i]);
|
||||
updates[i].y = boaty[i];
|
||||
updates[i].w = boat[0]->w + SPEED;
|
||||
updates[i].h = boat[0]->h;
|
||||
/* clip update rectangle to screen */
|
||||
if(updates[i].x < 0) {
|
||||
updates[i].w += updates[i].x;
|
||||
updates[i].x = 0;
|
||||
}
|
||||
if(updates[i].x + updates[i].w > SCRW)
|
||||
updates[i].w = SCRW - updates[i].x;
|
||||
}
|
||||
/* construct update rectangle (bounding box of old and new pos) */
|
||||
updates[i].x = MIN(old_x, boatx[i]);
|
||||
updates[i].y = boaty[i];
|
||||
updates[i].w = boat[0]->w + SPEED;
|
||||
updates[i].h = boat[0]->h;
|
||||
/* clip update rectangle to screen */
|
||||
if (updates[i].x < 0) {
|
||||
updates[i].w += updates[i].x;
|
||||
updates[i].x = 0;
|
||||
}
|
||||
if (updates[i].x + updates[i].w > SCRW)
|
||||
updates[i].w = SCRW - updates[i].x;
|
||||
}
|
||||
|
||||
for(i = 0; i < NBOATS; i++) {
|
||||
/* paint boat on new position */
|
||||
r.x = boatx[i];
|
||||
r.y = boaty[i];
|
||||
if(SDL_BlitSurface(boat[(boatdir[i] + 1) / 2], NULL,
|
||||
screen, &r) < 0)
|
||||
sdlerr("blitting boat");
|
||||
}
|
||||
for (i = 0; i < NBOATS; i++) {
|
||||
/* paint boat on new position */
|
||||
r.x = boatx[i];
|
||||
r.y = boaty[i];
|
||||
if (SDL_BlitSurface(boat[(boatdir[i] + 1) / 2], NULL,
|
||||
screen, &r) < 0)
|
||||
sdlerr("blitting boat");
|
||||
}
|
||||
|
||||
/* cycle wave palette */
|
||||
for(i = 0; i < 64; i++)
|
||||
cmap[boatcols + ((i + frames) & 63)] = wavemap[i];
|
||||
/* cycle wave palette */
|
||||
for (i = 0; i < 64; i++)
|
||||
cmap[boatcols + ((i + frames) & 63)] = wavemap[i];
|
||||
|
||||
if(fade_dir) {
|
||||
/* Fade the entire palette in/out */
|
||||
fade_level += fade_dir;
|
||||
if (fade_dir) {
|
||||
/* Fade the entire palette in/out */
|
||||
fade_level += fade_dir;
|
||||
|
||||
if(gamma_fade) {
|
||||
/* Fade linearly in gamma level (lousy) */
|
||||
float level = (float)fade_level / fade_max;
|
||||
if(SDL_SetGamma(level, level, level) < 0)
|
||||
sdlerr("setting gamma");
|
||||
if (gamma_fade) {
|
||||
/* Fade linearly in gamma level (lousy) */
|
||||
float level = (float) fade_level / fade_max;
|
||||
if (SDL_SetGamma(level, level, level) < 0)
|
||||
sdlerr("setting gamma");
|
||||
|
||||
} else if(gamma_ramp) {
|
||||
/* Fade using gamma ramp (better) */
|
||||
Uint16 ramp[256];
|
||||
for(i = 0; i < 256; i++)
|
||||
ramp[i] = (i * fade_level / fade_max) << 8;
|
||||
if(SDL_SetGammaRamp(ramp, ramp, ramp) < 0)
|
||||
sdlerr("setting gamma ramp");
|
||||
} else if (gamma_ramp) {
|
||||
/* Fade using gamma ramp (better) */
|
||||
Uint16 ramp[256];
|
||||
for (i = 0; i < 256; i++)
|
||||
ramp[i] = (i * fade_level / fade_max) << 8;
|
||||
if (SDL_SetGammaRamp(ramp, ramp, ramp) < 0)
|
||||
sdlerr("setting gamma ramp");
|
||||
|
||||
} else {
|
||||
/* Fade using direct palette manipulation (best) */
|
||||
memcpy(cmap, screen->format->palette->colors,
|
||||
boatcols * sizeof(SDL_Color));
|
||||
for(i = 0; i < boatcols + 64; i++) {
|
||||
cmap[i].r = cmap[i].r * fade_level / fade_max;
|
||||
cmap[i].g = cmap[i].g * fade_level / fade_max;
|
||||
cmap[i].b = cmap[i].b * fade_level / fade_max;
|
||||
}
|
||||
}
|
||||
if(fade_level == fade_max)
|
||||
fade_dir = 0;
|
||||
}
|
||||
} else {
|
||||
/* Fade using direct palette manipulation (best) */
|
||||
memcpy(cmap, screen->format->palette->colors,
|
||||
boatcols * sizeof(SDL_Color));
|
||||
for (i = 0; i < boatcols + 64; i++) {
|
||||
cmap[i].r = cmap[i].r * fade_level / fade_max;
|
||||
cmap[i].g = cmap[i].g * fade_level / fade_max;
|
||||
cmap[i].b = cmap[i].b * fade_level / fade_max;
|
||||
}
|
||||
}
|
||||
if (fade_level == fade_max)
|
||||
fade_dir = 0;
|
||||
}
|
||||
|
||||
/* pulse the red colour (done after the fade, for a night effect) */
|
||||
redphase = frames % 64;
|
||||
cmap[red].r = (int)(255 * sin(redphase * M_PI / 63));
|
||||
/* pulse the red colour (done after the fade, for a night effect) */
|
||||
redphase = frames % 64;
|
||||
cmap[red].r = (int) (255 * sin(redphase * M_PI / 63));
|
||||
|
||||
SDL_SetPalette(screen, SDL_PHYSPAL, cmap, 0, boatcols + 64);
|
||||
SDL_SetPalette(screen, SDL_PHYSPAL, cmap, 0, boatcols + 64);
|
||||
|
||||
/* update changed areas of the screen */
|
||||
SDL_UpdateRects(screen, NBOATS, updates);
|
||||
frames++;
|
||||
} while(fade_level > 0);
|
||||
/* update changed areas of the screen */
|
||||
SDL_UpdateRects(screen, NBOATS, updates);
|
||||
frames++;
|
||||
}
|
||||
while (fade_level > 0);
|
||||
|
||||
printf("%d frames, %.2f fps\n",
|
||||
frames, 1000.0 * frames / (SDL_GetTicks() - start));
|
||||
frames, 1000.0 * frames / (SDL_GetTicks() - start));
|
||||
|
||||
if (vidflags & SDL_FULLSCREEN) SDL_ShowCursor (SDL_TRUE);
|
||||
if (vidflags & SDL_FULLSCREEN)
|
||||
SDL_ShowCursor(SDL_TRUE);
|
||||
SDL_Quit();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,200 +10,206 @@
|
||||
* compare them directly, so we push it through a function to keep the
|
||||
* compiler quiet. --ryan.
|
||||
*/
|
||||
static int badsize(size_t sizeoftype, size_t hardcodetype)
|
||||
static int
|
||||
badsize(size_t sizeoftype, size_t hardcodetype)
|
||||
{
|
||||
return sizeoftype != hardcodetype;
|
||||
}
|
||||
|
||||
int TestTypes(SDL_bool verbose)
|
||||
int
|
||||
TestTypes(SDL_bool verbose)
|
||||
{
|
||||
int error = 0;
|
||||
int error = 0;
|
||||
|
||||
if ( badsize(sizeof(Uint8), 1) ) {
|
||||
if ( verbose )
|
||||
printf("sizeof(Uint8) != 1, instead = %ul\n",
|
||||
sizeof(Uint8));
|
||||
++error;
|
||||
}
|
||||
if ( badsize(sizeof(Uint16), 2) ) {
|
||||
if ( verbose )
|
||||
printf("sizeof(Uint16) != 2, instead = %ul\n",
|
||||
sizeof(Uint16));
|
||||
++error;
|
||||
}
|
||||
if ( badsize(sizeof(Uint32), 4) ) {
|
||||
if ( verbose )
|
||||
printf("sizeof(Uint32) != 4, instead = %ul\n",
|
||||
sizeof(Uint32));
|
||||
++error;
|
||||
}
|
||||
if (badsize(sizeof(Uint8), 1)) {
|
||||
if (verbose)
|
||||
printf("sizeof(Uint8) != 1, instead = %ul\n", sizeof(Uint8));
|
||||
++error;
|
||||
}
|
||||
if (badsize(sizeof(Uint16), 2)) {
|
||||
if (verbose)
|
||||
printf("sizeof(Uint16) != 2, instead = %ul\n", sizeof(Uint16));
|
||||
++error;
|
||||
}
|
||||
if (badsize(sizeof(Uint32), 4)) {
|
||||
if (verbose)
|
||||
printf("sizeof(Uint32) != 4, instead = %ul\n", sizeof(Uint32));
|
||||
++error;
|
||||
}
|
||||
#ifdef SDL_HAS_64BIT_TYPE
|
||||
if ( badsize(sizeof(Uint64), 8) ) {
|
||||
if ( verbose )
|
||||
printf("sizeof(Uint64) != 8, instead = %ul\n",
|
||||
sizeof(Uint64));
|
||||
++error;
|
||||
}
|
||||
if (badsize(sizeof(Uint64), 8)) {
|
||||
if (verbose)
|
||||
printf("sizeof(Uint64) != 8, instead = %ul\n", sizeof(Uint64));
|
||||
++error;
|
||||
}
|
||||
#else
|
||||
if ( verbose ) {
|
||||
printf("WARNING: No 64-bit datatype on this platform\n");
|
||||
}
|
||||
if (verbose) {
|
||||
printf("WARNING: No 64-bit datatype on this platform\n");
|
||||
}
|
||||
#endif
|
||||
if ( verbose && !error )
|
||||
printf("All data types are the expected size.\n");
|
||||
if (verbose && !error)
|
||||
printf("All data types are the expected size.\n");
|
||||
|
||||
return( error ? 1 : 0 );
|
||||
return (error ? 1 : 0);
|
||||
}
|
||||
|
||||
int TestEndian(SDL_bool verbose)
|
||||
int
|
||||
TestEndian(SDL_bool verbose)
|
||||
{
|
||||
int error = 0;
|
||||
Uint16 value = 0x1234;
|
||||
int real_byteorder;
|
||||
Uint16 value16 = 0xCDAB;
|
||||
Uint16 swapped16 = 0xABCD;
|
||||
Uint32 value32 = 0xEFBEADDE;
|
||||
Uint32 swapped32 = 0xDEADBEEF;
|
||||
int error = 0;
|
||||
Uint16 value = 0x1234;
|
||||
int real_byteorder;
|
||||
Uint16 value16 = 0xCDAB;
|
||||
Uint16 swapped16 = 0xABCD;
|
||||
Uint32 value32 = 0xEFBEADDE;
|
||||
Uint32 swapped32 = 0xDEADBEEF;
|
||||
#ifdef SDL_HAS_64BIT_TYPE
|
||||
Uint64 value64, swapped64;
|
||||
value64 = 0xEFBEADDE;
|
||||
value64 <<= 32;
|
||||
value64 |= 0xCDAB3412;
|
||||
swapped64 = 0x1234ABCD;
|
||||
swapped64 <<= 32;
|
||||
swapped64 |= 0xDEADBEEF;
|
||||
Uint64 value64, swapped64;
|
||||
value64 = 0xEFBEADDE;
|
||||
value64 <<= 32;
|
||||
value64 |= 0xCDAB3412;
|
||||
swapped64 = 0x1234ABCD;
|
||||
swapped64 <<= 32;
|
||||
swapped64 |= 0xDEADBEEF;
|
||||
#endif
|
||||
|
||||
if ( verbose ) {
|
||||
printf("Detected a %s endian machine.\n",
|
||||
(SDL_BYTEORDER == SDL_LIL_ENDIAN) ? "little" : "big");
|
||||
}
|
||||
if ( (*((char *)&value) >> 4) == 0x1 ) {
|
||||
real_byteorder = SDL_BIG_ENDIAN;
|
||||
} else {
|
||||
real_byteorder = SDL_LIL_ENDIAN;
|
||||
}
|
||||
if ( real_byteorder != SDL_BYTEORDER ) {
|
||||
if ( verbose ) {
|
||||
printf("Actually a %s endian machine!\n",
|
||||
(real_byteorder == SDL_LIL_ENDIAN) ? "little" : "big");
|
||||
}
|
||||
++error;
|
||||
}
|
||||
if ( verbose ) {
|
||||
printf("Value 16 = 0x%X, swapped = 0x%X\n", value16, SDL_Swap16(value16));
|
||||
}
|
||||
if ( SDL_Swap16(value16) != swapped16 ) {
|
||||
if ( verbose ) {
|
||||
printf("16 bit value swapped incorrectly!\n");
|
||||
}
|
||||
++error;
|
||||
}
|
||||
if ( verbose ) {
|
||||
printf("Value 32 = 0x%X, swapped = 0x%X\n", value32, SDL_Swap32(value32));
|
||||
}
|
||||
if ( SDL_Swap32(value32) != swapped32 ) {
|
||||
if ( verbose ) {
|
||||
printf("32 bit value swapped incorrectly!\n");
|
||||
}
|
||||
++error;
|
||||
}
|
||||
if (verbose) {
|
||||
printf("Detected a %s endian machine.\n",
|
||||
(SDL_BYTEORDER == SDL_LIL_ENDIAN) ? "little" : "big");
|
||||
}
|
||||
if ((*((char *) &value) >> 4) == 0x1) {
|
||||
real_byteorder = SDL_BIG_ENDIAN;
|
||||
} else {
|
||||
real_byteorder = SDL_LIL_ENDIAN;
|
||||
}
|
||||
if (real_byteorder != SDL_BYTEORDER) {
|
||||
if (verbose) {
|
||||
printf("Actually a %s endian machine!\n",
|
||||
(real_byteorder == SDL_LIL_ENDIAN) ? "little" : "big");
|
||||
}
|
||||
++error;
|
||||
}
|
||||
if (verbose) {
|
||||
printf("Value 16 = 0x%X, swapped = 0x%X\n", value16,
|
||||
SDL_Swap16(value16));
|
||||
}
|
||||
if (SDL_Swap16(value16) != swapped16) {
|
||||
if (verbose) {
|
||||
printf("16 bit value swapped incorrectly!\n");
|
||||
}
|
||||
++error;
|
||||
}
|
||||
if (verbose) {
|
||||
printf("Value 32 = 0x%X, swapped = 0x%X\n", value32,
|
||||
SDL_Swap32(value32));
|
||||
}
|
||||
if (SDL_Swap32(value32) != swapped32) {
|
||||
if (verbose) {
|
||||
printf("32 bit value swapped incorrectly!\n");
|
||||
}
|
||||
++error;
|
||||
}
|
||||
#ifdef SDL_HAS_64BIT_TYPE
|
||||
if ( verbose ) {
|
||||
if (verbose) {
|
||||
#ifdef _MSC_VER
|
||||
printf("Value 64 = 0x%I64X, swapped = 0x%I64X\n", value64, SDL_Swap64(value64));
|
||||
printf("Value 64 = 0x%I64X, swapped = 0x%I64X\n", value64,
|
||||
SDL_Swap64(value64));
|
||||
#else
|
||||
printf("Value 64 = 0x%llX, swapped = 0x%llX\n", value64, SDL_Swap64(value64));
|
||||
printf("Value 64 = 0x%llX, swapped = 0x%llX\n", value64,
|
||||
SDL_Swap64(value64));
|
||||
#endif
|
||||
}
|
||||
if ( SDL_Swap64(value64) != swapped64 ) {
|
||||
if ( verbose ) {
|
||||
printf("64 bit value swapped incorrectly!\n");
|
||||
}
|
||||
++error;
|
||||
}
|
||||
}
|
||||
if (SDL_Swap64(value64) != swapped64) {
|
||||
if (verbose) {
|
||||
printf("64 bit value swapped incorrectly!\n");
|
||||
}
|
||||
++error;
|
||||
}
|
||||
#endif
|
||||
return( error ? 1 : 0 );
|
||||
return (error ? 1 : 0);
|
||||
}
|
||||
|
||||
|
||||
int TestCPUInfo(SDL_bool verbose)
|
||||
int
|
||||
TestCPUInfo(SDL_bool verbose)
|
||||
{
|
||||
if ( verbose ) {
|
||||
printf("RDTSC %s\n", SDL_HasRDTSC() ? "detected" : "not detected");
|
||||
printf("MMX %s\n", SDL_HasMMX() ? "detected" : "not detected");
|
||||
printf("MMX Ext %s\n", SDL_HasMMXExt() ? "detected" : "not detected");
|
||||
printf("3DNow %s\n", SDL_Has3DNow() ? "detected" : "not detected");
|
||||
printf("3DNow Ext %s\n", SDL_Has3DNowExt() ? "detected" : "not detected");
|
||||
printf("SSE %s\n", SDL_HasSSE() ? "detected" : "not detected");
|
||||
printf("SSE2 %s\n", SDL_HasSSE2() ? "detected" : "not detected");
|
||||
printf("AltiVec %s\n", SDL_HasAltiVec() ? "detected" : "not detected");
|
||||
}
|
||||
return(0);
|
||||
if (verbose) {
|
||||
printf("RDTSC %s\n", SDL_HasRDTSC()? "detected" : "not detected");
|
||||
printf("MMX %s\n", SDL_HasMMX()? "detected" : "not detected");
|
||||
printf("MMX Ext %s\n", SDL_HasMMXExt()? "detected" : "not detected");
|
||||
printf("3DNow %s\n", SDL_Has3DNow()? "detected" : "not detected");
|
||||
printf("3DNow Ext %s\n",
|
||||
SDL_Has3DNowExt()? "detected" : "not detected");
|
||||
printf("SSE %s\n", SDL_HasSSE()? "detected" : "not detected");
|
||||
printf("SSE2 %s\n", SDL_HasSSE2()? "detected" : "not detected");
|
||||
printf("AltiVec %s\n", SDL_HasAltiVec()? "detected" : "not detected");
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
SDL_bool verbose = SDL_TRUE;
|
||||
int status = 0;
|
||||
SDL_bool verbose = SDL_TRUE;
|
||||
int status = 0;
|
||||
|
||||
if ( argv[1] && (SDL_strcmp(argv[1], "-q") == 0) ) {
|
||||
verbose = SDL_FALSE;
|
||||
}
|
||||
if ( verbose ) {
|
||||
printf("This system is running %s\n",
|
||||
if (argv[1] && (SDL_strcmp(argv[1], "-q") == 0)) {
|
||||
verbose = SDL_FALSE;
|
||||
}
|
||||
if (verbose) {
|
||||
printf("This system is running %s\n",
|
||||
#if __AIX__
|
||||
"AIX"
|
||||
"AIX"
|
||||
#elif __AMIGA__
|
||||
"AmigaOS"
|
||||
"AmigaOS"
|
||||
#elif __BEOS__
|
||||
"BeOS"
|
||||
"BeOS"
|
||||
#elif __BSDI__
|
||||
"BSDI"
|
||||
"BSDI"
|
||||
#elif __DREAMCAST__
|
||||
"Dreamcast"
|
||||
"Dreamcast"
|
||||
#elif __FREEBSD__
|
||||
"FreeBSD"
|
||||
"FreeBSD"
|
||||
#elif __HPUX__
|
||||
"HP-UX"
|
||||
"HP-UX"
|
||||
#elif __IRIX__
|
||||
"Irix"
|
||||
"Irix"
|
||||
#elif __LINUX__
|
||||
"Linux"
|
||||
"Linux"
|
||||
#elif __MINT__
|
||||
"Atari MiNT"
|
||||
"Atari MiNT"
|
||||
#elif __MACOS__
|
||||
"MacOS Classic"
|
||||
"MacOS Classic"
|
||||
#elif __MACOSX__
|
||||
"Mac OS X"
|
||||
"Mac OS X"
|
||||
#elif __NETBSD__
|
||||
"NetBSD"
|
||||
"NetBSD"
|
||||
#elif __OPENBSD__
|
||||
"OpenBSD"
|
||||
"OpenBSD"
|
||||
#elif __OS2__
|
||||
"OS/2"
|
||||
"OS/2"
|
||||
#elif __OSF__
|
||||
"OSF/1"
|
||||
"OSF/1"
|
||||
#elif __QNXNTO__
|
||||
"QNX Neutrino"
|
||||
"QNX Neutrino"
|
||||
#elif __RISCOS__
|
||||
"RISC OS"
|
||||
"RISC OS"
|
||||
#elif __SOLARIS__
|
||||
"Solaris"
|
||||
"Solaris"
|
||||
#elif __WIN32__
|
||||
#ifdef _WIN32_WCE
|
||||
"Windows CE"
|
||||
"Windows CE"
|
||||
#else
|
||||
"Windows"
|
||||
"Windows"
|
||||
#endif
|
||||
#else
|
||||
"an unknown operating system! (see SDL_platform.h)"
|
||||
"an unknown operating system! (see SDL_platform.h)"
|
||||
#endif
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
status += TestTypes(verbose);
|
||||
status += TestEndian(verbose);
|
||||
status += TestCPUInfo(verbose);
|
||||
return status;
|
||||
status += TestTypes(verbose);
|
||||
status += TestEndian(verbose);
|
||||
status += TestCPUInfo(verbose);
|
||||
return status;
|
||||
}
|
||||
|
||||
108
test/testsem.c
108
test/testsem.c
@@ -13,66 +13,74 @@
|
||||
static SDL_sem *sem;
|
||||
int alive = 1;
|
||||
|
||||
int SDLCALL ThreadFunc(void *data)
|
||||
int SDLCALL
|
||||
ThreadFunc(void *data)
|
||||
{
|
||||
int threadnum = (int)(uintptr_t)data;
|
||||
while ( alive ) {
|
||||
SDL_SemWait(sem);
|
||||
fprintf(stderr, "Thread number %d has got the semaphore (value = %d)!\n", threadnum, SDL_SemValue(sem));
|
||||
SDL_Delay(200);
|
||||
SDL_SemPost(sem);
|
||||
fprintf(stderr, "Thread number %d has released the semaphore (value = %d)!\n", threadnum, SDL_SemValue(sem));
|
||||
SDL_Delay(1); /* For the scheduler */
|
||||
}
|
||||
printf("Thread number %d exiting.\n", threadnum);
|
||||
return 0;
|
||||
int threadnum = (int) (uintptr_t) data;
|
||||
while (alive) {
|
||||
SDL_SemWait(sem);
|
||||
fprintf(stderr,
|
||||
"Thread number %d has got the semaphore (value = %d)!\n",
|
||||
threadnum, SDL_SemValue(sem));
|
||||
SDL_Delay(200);
|
||||
SDL_SemPost(sem);
|
||||
fprintf(stderr,
|
||||
"Thread number %d has released the semaphore (value = %d)!\n",
|
||||
threadnum, SDL_SemValue(sem));
|
||||
SDL_Delay(1); /* For the scheduler */
|
||||
}
|
||||
printf("Thread number %d exiting.\n", threadnum);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void killed(int sig)
|
||||
static void
|
||||
killed(int sig)
|
||||
{
|
||||
alive = 0;
|
||||
alive = 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
SDL_Thread *threads[NUM_THREADS];
|
||||
uintptr_t i;
|
||||
int init_sem;
|
||||
SDL_Thread *threads[NUM_THREADS];
|
||||
uintptr_t i;
|
||||
int init_sem;
|
||||
|
||||
if(argc < 2) {
|
||||
fprintf(stderr,"Usage: %s init_value\n", argv[0]);
|
||||
return(1);
|
||||
}
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "Usage: %s init_value\n", argv[0]);
|
||||
return (1);
|
||||
}
|
||||
|
||||
/* Load the SDL library */
|
||||
if ( SDL_Init(0) < 0 ) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
|
||||
return(1);
|
||||
}
|
||||
signal(SIGTERM, killed);
|
||||
signal(SIGINT, killed);
|
||||
|
||||
init_sem = atoi(argv[1]);
|
||||
sem = SDL_CreateSemaphore(init_sem);
|
||||
|
||||
printf("Running %d threads, semaphore value = %d\n", NUM_THREADS, init_sem);
|
||||
/* Create all the threads */
|
||||
for( i = 0; i < NUM_THREADS; ++i ) {
|
||||
threads[i] = SDL_CreateThread(ThreadFunc, (void*)i);
|
||||
}
|
||||
/* Load the SDL library */
|
||||
if (SDL_Init(0) < 0) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return (1);
|
||||
}
|
||||
signal(SIGTERM, killed);
|
||||
signal(SIGINT, killed);
|
||||
|
||||
/* Wait 10 seconds */
|
||||
SDL_Delay(10 * 1000);
|
||||
init_sem = atoi(argv[1]);
|
||||
sem = SDL_CreateSemaphore(init_sem);
|
||||
|
||||
/* Wait for all threads to finish */
|
||||
printf("Waiting for threads to finish\n");
|
||||
alive = 0;
|
||||
for( i = 0; i < NUM_THREADS; ++i ) {
|
||||
SDL_WaitThread(threads[i], NULL);
|
||||
}
|
||||
printf("Finished waiting for threads\n");
|
||||
printf("Running %d threads, semaphore value = %d\n", NUM_THREADS,
|
||||
init_sem);
|
||||
/* Create all the threads */
|
||||
for (i = 0; i < NUM_THREADS; ++i) {
|
||||
threads[i] = SDL_CreateThread(ThreadFunc, (void *) i);
|
||||
}
|
||||
|
||||
SDL_DestroySemaphore(sem);
|
||||
SDL_Quit();
|
||||
return(0);
|
||||
/* Wait 10 seconds */
|
||||
SDL_Delay(10 * 1000);
|
||||
|
||||
/* Wait for all threads to finish */
|
||||
printf("Waiting for threads to finish\n");
|
||||
alive = 0;
|
||||
for (i = 0; i < NUM_THREADS; ++i) {
|
||||
SDL_WaitThread(threads[i], NULL);
|
||||
}
|
||||
printf("Finished waiting for threads\n");
|
||||
|
||||
SDL_DestroySemaphore(sem);
|
||||
SDL_Quit();
|
||||
return (0);
|
||||
}
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
/* Simple program: Move N sprites around on the screen as fast as possible */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "SDL.h"
|
||||
@@ -22,302 +18,300 @@ int debug_flip;
|
||||
Uint16 sprite_w, sprite_h;
|
||||
|
||||
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
||||
static void quit(int rc)
|
||||
static void
|
||||
quit(int rc)
|
||||
{
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
}
|
||||
|
||||
int LoadSprite(char *file)
|
||||
int
|
||||
LoadSprite(char *file)
|
||||
{
|
||||
SDL_Surface *temp;
|
||||
SDL_Surface *temp;
|
||||
|
||||
/* Load the sprite image */
|
||||
sprite = SDL_LoadBMP(file);
|
||||
if ( sprite == NULL ) {
|
||||
fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
|
||||
return(-1);
|
||||
}
|
||||
/* Load the sprite image */
|
||||
sprite = SDL_LoadBMP(file);
|
||||
if (sprite == NULL) {
|
||||
fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* Set transparent pixel as the pixel at (0,0) */
|
||||
if ( sprite->format->palette ) {
|
||||
SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY|SDL_RLEACCEL),
|
||||
*(Uint8 *)sprite->pixels);
|
||||
}
|
||||
/* Set transparent pixel as the pixel at (0,0) */
|
||||
if (sprite->format->palette) {
|
||||
SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY | SDL_RLEACCEL),
|
||||
*(Uint8 *) sprite->pixels);
|
||||
}
|
||||
|
||||
/* Convert sprite to video format */
|
||||
temp = SDL_DisplayFormat(sprite);
|
||||
SDL_FreeSurface(sprite);
|
||||
if ( temp == NULL ) {
|
||||
fprintf(stderr, "Couldn't convert background: %s\n",
|
||||
SDL_GetError());
|
||||
return(-1);
|
||||
}
|
||||
sprite = temp;
|
||||
/* Convert sprite to video format */
|
||||
temp = SDL_DisplayFormat(sprite);
|
||||
SDL_FreeSurface(sprite);
|
||||
if (temp == NULL) {
|
||||
fprintf(stderr, "Couldn't convert background: %s\n", SDL_GetError());
|
||||
return (-1);
|
||||
}
|
||||
sprite = temp;
|
||||
|
||||
/* We're ready to roll. :) */
|
||||
return(0);
|
||||
/* We're ready to roll. :) */
|
||||
return (0);
|
||||
}
|
||||
|
||||
void MoveSprites(SDL_Surface *screen, Uint32 background)
|
||||
void
|
||||
MoveSprites(SDL_Surface * screen, Uint32 background)
|
||||
{
|
||||
int i, nupdates;
|
||||
SDL_Rect area, *position, *velocity;
|
||||
int i, nupdates;
|
||||
SDL_Rect area, *position, *velocity;
|
||||
|
||||
nupdates = 0;
|
||||
/* Erase all the sprites if necessary */
|
||||
if ( sprites_visible ) {
|
||||
SDL_FillRect(screen, NULL, background);
|
||||
}
|
||||
nupdates = 0;
|
||||
/* Erase all the sprites if necessary */
|
||||
if (sprites_visible) {
|
||||
SDL_FillRect(screen, NULL, background);
|
||||
}
|
||||
|
||||
/* Move the sprite, bounce at the wall, and draw */
|
||||
for ( i=0; i<numsprites; ++i ) {
|
||||
position = &positions[i];
|
||||
velocity = &velocities[i];
|
||||
position->x += velocity->x;
|
||||
if ( (position->x < 0) || (position->x >= (screen->w - sprite_w)) ) {
|
||||
velocity->x = -velocity->x;
|
||||
position->x += velocity->x;
|
||||
}
|
||||
position->y += velocity->y;
|
||||
if ( (position->y < 0) || (position->y >= (screen->h - sprite_w)) ) {
|
||||
velocity->y = -velocity->y;
|
||||
position->y += velocity->y;
|
||||
}
|
||||
/* Move the sprite, bounce at the wall, and draw */
|
||||
for (i = 0; i < numsprites; ++i) {
|
||||
position = &positions[i];
|
||||
velocity = &velocities[i];
|
||||
position->x += velocity->x;
|
||||
if ((position->x < 0) || (position->x >= (screen->w - sprite_w))) {
|
||||
velocity->x = -velocity->x;
|
||||
position->x += velocity->x;
|
||||
}
|
||||
position->y += velocity->y;
|
||||
if ((position->y < 0) || (position->y >= (screen->h - sprite_w))) {
|
||||
velocity->y = -velocity->y;
|
||||
position->y += velocity->y;
|
||||
}
|
||||
|
||||
/* Blit the sprite onto the screen */
|
||||
area = *position;
|
||||
SDL_BlitSurface(sprite, NULL, screen, &area);
|
||||
sprite_rects[nupdates++] = area;
|
||||
}
|
||||
/* Blit the sprite onto the screen */
|
||||
area = *position;
|
||||
SDL_BlitSurface(sprite, NULL, screen, &area);
|
||||
sprite_rects[nupdates++] = area;
|
||||
}
|
||||
|
||||
if (debug_flip) {
|
||||
if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
|
||||
static int t = 0;
|
||||
if (debug_flip) {
|
||||
if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
|
||||
static int t = 0;
|
||||
|
||||
Uint32 color = SDL_MapRGB (screen->format, 255, 0, 0);
|
||||
SDL_Rect r;
|
||||
r.x = (sin((float)t * 2 * 3.1459) + 1.0) / 2.0 * (screen->w-20);
|
||||
r.y = 0;
|
||||
r.w = 20;
|
||||
r.h = screen->h;
|
||||
Uint32 color = SDL_MapRGB(screen->format, 255, 0, 0);
|
||||
SDL_Rect r;
|
||||
r.x =
|
||||
(sin((float) t * 2 * 3.1459) + 1.0) / 2.0 * (screen->w - 20);
|
||||
r.y = 0;
|
||||
r.w = 20;
|
||||
r.h = screen->h;
|
||||
|
||||
SDL_FillRect (screen, &r, color);
|
||||
t+=2;
|
||||
}
|
||||
}
|
||||
SDL_FillRect(screen, &r, color);
|
||||
t += 2;
|
||||
}
|
||||
}
|
||||
|
||||
/* Update the screen! */
|
||||
if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
|
||||
SDL_Flip(screen);
|
||||
} else {
|
||||
SDL_UpdateRects(screen, nupdates, sprite_rects);
|
||||
}
|
||||
sprites_visible = 1;
|
||||
/* Update the screen! */
|
||||
if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
|
||||
SDL_Flip(screen);
|
||||
} else {
|
||||
SDL_UpdateRects(screen, nupdates, sprite_rects);
|
||||
}
|
||||
sprites_visible = 1;
|
||||
}
|
||||
|
||||
/* This is a way of telling whether or not to use hardware surfaces */
|
||||
Uint32 FastestFlags(Uint32 flags, int width, int height, int bpp)
|
||||
Uint32
|
||||
FastestFlags(Uint32 flags, int width, int height, int bpp)
|
||||
{
|
||||
const SDL_VideoInfo *info;
|
||||
const SDL_VideoInfo *info;
|
||||
|
||||
/* Hardware acceleration is only used in fullscreen mode */
|
||||
flags |= SDL_FULLSCREEN;
|
||||
/* Hardware acceleration is only used in fullscreen mode */
|
||||
flags |= SDL_FULLSCREEN;
|
||||
|
||||
/* Check for various video capabilities */
|
||||
info = SDL_GetVideoInfo();
|
||||
if ( info->blit_hw_CC && info->blit_fill ) {
|
||||
/* We use accelerated colorkeying and color filling */
|
||||
flags |= SDL_HWSURFACE;
|
||||
}
|
||||
/* If we have enough video memory, and will use accelerated
|
||||
blits directly to it, then use page flipping.
|
||||
*/
|
||||
if ( (flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
|
||||
/* Direct hardware blitting without double-buffering
|
||||
causes really bad flickering.
|
||||
*/
|
||||
if ( info->video_mem*1024 > (height*width*bpp/8) ) {
|
||||
flags |= SDL_DOUBLEBUF;
|
||||
} else {
|
||||
flags &= ~SDL_HWSURFACE;
|
||||
}
|
||||
}
|
||||
/* Check for various video capabilities */
|
||||
info = SDL_GetVideoInfo();
|
||||
if (info->blit_hw_CC && info->blit_fill) {
|
||||
/* We use accelerated colorkeying and color filling */
|
||||
flags |= SDL_HWSURFACE;
|
||||
}
|
||||
/* If we have enough video memory, and will use accelerated
|
||||
blits directly to it, then use page flipping.
|
||||
*/
|
||||
if ((flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
|
||||
/* Direct hardware blitting without double-buffering
|
||||
causes really bad flickering.
|
||||
*/
|
||||
if (info->video_mem * 1024 > (height * width * bpp / 8)) {
|
||||
flags |= SDL_DOUBLEBUF;
|
||||
} else {
|
||||
flags &= ~SDL_HWSURFACE;
|
||||
}
|
||||
}
|
||||
|
||||
/* Return the flags */
|
||||
return(flags);
|
||||
/* Return the flags */
|
||||
return (flags);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
SDL_Surface *screen;
|
||||
Uint8 *mem;
|
||||
int width, height;
|
||||
Uint8 video_bpp;
|
||||
Uint32 videoflags;
|
||||
Uint32 background;
|
||||
int i, done;
|
||||
SDL_Event event;
|
||||
Uint32 then, now, frames;
|
||||
SDL_Surface *screen;
|
||||
Uint8 *mem;
|
||||
int width, height;
|
||||
Uint8 video_bpp;
|
||||
Uint32 videoflags;
|
||||
Uint32 background;
|
||||
int i, done;
|
||||
SDL_Event event;
|
||||
Uint32 then, now, frames;
|
||||
|
||||
/* Initialize SDL */
|
||||
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
|
||||
return(1);
|
||||
}
|
||||
/* Initialize SDL */
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return (1);
|
||||
}
|
||||
|
||||
numsprites = NUM_SPRITES;
|
||||
videoflags = SDL_SWSURFACE|SDL_ANYFORMAT;
|
||||
width = 640;
|
||||
height = 480;
|
||||
video_bpp = 8;
|
||||
debug_flip = 0;
|
||||
while ( argc > 1 ) {
|
||||
--argc;
|
||||
if ( strcmp(argv[argc-1], "-width") == 0 ) {
|
||||
width = atoi(argv[argc]);
|
||||
--argc;
|
||||
} else
|
||||
if ( strcmp(argv[argc-1], "-height") == 0 ) {
|
||||
height = atoi(argv[argc]);
|
||||
--argc;
|
||||
} else
|
||||
if ( strcmp(argv[argc-1], "-bpp") == 0 ) {
|
||||
video_bpp = atoi(argv[argc]);
|
||||
videoflags &= ~SDL_ANYFORMAT;
|
||||
--argc;
|
||||
} else
|
||||
if ( strcmp(argv[argc], "-fast") == 0 ) {
|
||||
videoflags = FastestFlags(videoflags, width, height, video_bpp);
|
||||
} else
|
||||
if ( strcmp(argv[argc], "-hw") == 0 ) {
|
||||
videoflags ^= SDL_HWSURFACE;
|
||||
} else
|
||||
if ( strcmp(argv[argc], "-flip") == 0 ) {
|
||||
videoflags ^= SDL_DOUBLEBUF;
|
||||
} else
|
||||
if ( strcmp(argv[argc], "-debugflip") == 0 ) {
|
||||
debug_flip ^= 1;
|
||||
} else
|
||||
if ( strcmp(argv[argc], "-fullscreen") == 0 ) {
|
||||
videoflags ^= SDL_FULLSCREEN;
|
||||
} else
|
||||
if ( isdigit(argv[argc][0]) ) {
|
||||
numsprites = atoi(argv[argc]);
|
||||
} else {
|
||||
fprintf(stderr,
|
||||
"Usage: %s [-bpp N] [-hw] [-flip] [-fast] [-fullscreen] [numsprites]\n",
|
||||
argv[0]);
|
||||
quit(1);
|
||||
}
|
||||
}
|
||||
numsprites = NUM_SPRITES;
|
||||
videoflags = SDL_SWSURFACE | SDL_ANYFORMAT;
|
||||
width = 640;
|
||||
height = 480;
|
||||
video_bpp = 8;
|
||||
debug_flip = 0;
|
||||
while (argc > 1) {
|
||||
--argc;
|
||||
if (strcmp(argv[argc - 1], "-width") == 0) {
|
||||
width = atoi(argv[argc]);
|
||||
--argc;
|
||||
} else if (strcmp(argv[argc - 1], "-height") == 0) {
|
||||
height = atoi(argv[argc]);
|
||||
--argc;
|
||||
} else if (strcmp(argv[argc - 1], "-bpp") == 0) {
|
||||
video_bpp = atoi(argv[argc]);
|
||||
videoflags &= ~SDL_ANYFORMAT;
|
||||
--argc;
|
||||
} else if (strcmp(argv[argc], "-fast") == 0) {
|
||||
videoflags = FastestFlags(videoflags, width, height, video_bpp);
|
||||
} else if (strcmp(argv[argc], "-hw") == 0) {
|
||||
videoflags ^= SDL_HWSURFACE;
|
||||
} else if (strcmp(argv[argc], "-flip") == 0) {
|
||||
videoflags ^= SDL_DOUBLEBUF;
|
||||
} else if (strcmp(argv[argc], "-debugflip") == 0) {
|
||||
debug_flip ^= 1;
|
||||
} else if (strcmp(argv[argc], "-fullscreen") == 0) {
|
||||
videoflags ^= SDL_FULLSCREEN;
|
||||
} else if (isdigit(argv[argc][0])) {
|
||||
numsprites = atoi(argv[argc]);
|
||||
} else {
|
||||
fprintf(stderr,
|
||||
"Usage: %s [-bpp N] [-hw] [-flip] [-fast] [-fullscreen] [numsprites]\n",
|
||||
argv[0]);
|
||||
quit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Set video mode */
|
||||
screen = SDL_SetVideoMode(width, height, video_bpp, videoflags);
|
||||
if ( ! screen ) {
|
||||
fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
|
||||
width, height, SDL_GetError());
|
||||
quit(2);
|
||||
}
|
||||
/* Set video mode */
|
||||
screen = SDL_SetVideoMode(width, height, video_bpp, videoflags);
|
||||
if (!screen) {
|
||||
fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
|
||||
width, height, SDL_GetError());
|
||||
quit(2);
|
||||
}
|
||||
|
||||
/* Load the sprite */
|
||||
if ( LoadSprite("icon.bmp") < 0 ) {
|
||||
quit(1);
|
||||
}
|
||||
/* Load the sprite */
|
||||
if (LoadSprite("icon.bmp") < 0) {
|
||||
quit(1);
|
||||
}
|
||||
|
||||
/* Allocate memory for the sprite info */
|
||||
mem = (Uint8 *)malloc(4*sizeof(SDL_Rect)*numsprites);
|
||||
if ( mem == NULL ) {
|
||||
SDL_FreeSurface(sprite);
|
||||
fprintf(stderr, "Out of memory!\n");
|
||||
quit(2);
|
||||
}
|
||||
sprite_rects = (SDL_Rect *)mem;
|
||||
positions = sprite_rects;
|
||||
sprite_rects += numsprites;
|
||||
velocities = sprite_rects;
|
||||
sprite_rects += numsprites;
|
||||
sprite_w = sprite->w;
|
||||
sprite_h = sprite->h;
|
||||
srand(time(NULL));
|
||||
for ( i=0; i<numsprites; ++i ) {
|
||||
positions[i].x = rand()%(screen->w - sprite_w);
|
||||
positions[i].y = rand()%(screen->h - sprite_h);
|
||||
positions[i].w = sprite->w;
|
||||
positions[i].h = sprite->h;
|
||||
velocities[i].x = 0;
|
||||
velocities[i].y = 0;
|
||||
while ( ! velocities[i].x && ! velocities[i].y ) {
|
||||
velocities[i].x = (rand()%(MAX_SPEED*2+1))-MAX_SPEED;
|
||||
velocities[i].y = (rand()%(MAX_SPEED*2+1))-MAX_SPEED;
|
||||
}
|
||||
}
|
||||
background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
|
||||
/* Allocate memory for the sprite info */
|
||||
mem = (Uint8 *) malloc(4 * sizeof(SDL_Rect) * numsprites);
|
||||
if (mem == NULL) {
|
||||
SDL_FreeSurface(sprite);
|
||||
fprintf(stderr, "Out of memory!\n");
|
||||
quit(2);
|
||||
}
|
||||
sprite_rects = (SDL_Rect *) mem;
|
||||
positions = sprite_rects;
|
||||
sprite_rects += numsprites;
|
||||
velocities = sprite_rects;
|
||||
sprite_rects += numsprites;
|
||||
sprite_w = sprite->w;
|
||||
sprite_h = sprite->h;
|
||||
srand(time(NULL));
|
||||
for (i = 0; i < numsprites; ++i) {
|
||||
positions[i].x = rand() % (screen->w - sprite_w);
|
||||
positions[i].y = rand() % (screen->h - sprite_h);
|
||||
positions[i].w = sprite->w;
|
||||
positions[i].h = sprite->h;
|
||||
velocities[i].x = 0;
|
||||
velocities[i].y = 0;
|
||||
while (!velocities[i].x && !velocities[i].y) {
|
||||
velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
|
||||
velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
|
||||
}
|
||||
}
|
||||
background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
|
||||
|
||||
/* Print out information about our surfaces */
|
||||
printf("Screen is at %d bits per pixel\n",screen->format->BitsPerPixel);
|
||||
if ( (screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
|
||||
printf("Screen is in video memory\n");
|
||||
} else {
|
||||
printf("Screen is in system memory\n");
|
||||
}
|
||||
if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
|
||||
printf("Screen has double-buffering enabled\n");
|
||||
}
|
||||
if ( (sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
|
||||
printf("Sprite is in video memory\n");
|
||||
} else {
|
||||
printf("Sprite is in system memory\n");
|
||||
}
|
||||
/* Run a sample blit to trigger blit acceleration */
|
||||
{ SDL_Rect dst;
|
||||
dst.x = 0;
|
||||
dst.y = 0;
|
||||
dst.w = sprite->w;
|
||||
dst.h = sprite->h;
|
||||
SDL_BlitSurface(sprite, NULL, screen, &dst);
|
||||
SDL_FillRect(screen, &dst, background);
|
||||
}
|
||||
if ( (sprite->flags & SDL_HWACCEL) == SDL_HWACCEL ) {
|
||||
printf("Sprite blit uses hardware acceleration\n");
|
||||
}
|
||||
if ( (sprite->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) {
|
||||
printf("Sprite blit uses RLE acceleration\n");
|
||||
}
|
||||
/* Print out information about our surfaces */
|
||||
printf("Screen is at %d bits per pixel\n", screen->format->BitsPerPixel);
|
||||
if ((screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
|
||||
printf("Screen is in video memory\n");
|
||||
} else {
|
||||
printf("Screen is in system memory\n");
|
||||
}
|
||||
if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
|
||||
printf("Screen has double-buffering enabled\n");
|
||||
}
|
||||
if ((sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
|
||||
printf("Sprite is in video memory\n");
|
||||
} else {
|
||||
printf("Sprite is in system memory\n");
|
||||
}
|
||||
/* Run a sample blit to trigger blit acceleration */
|
||||
{
|
||||
SDL_Rect dst;
|
||||
dst.x = 0;
|
||||
dst.y = 0;
|
||||
dst.w = sprite->w;
|
||||
dst.h = sprite->h;
|
||||
SDL_BlitSurface(sprite, NULL, screen, &dst);
|
||||
SDL_FillRect(screen, &dst, background);
|
||||
}
|
||||
if ((sprite->flags & SDL_HWACCEL) == SDL_HWACCEL) {
|
||||
printf("Sprite blit uses hardware acceleration\n");
|
||||
}
|
||||
if ((sprite->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
|
||||
printf("Sprite blit uses RLE acceleration\n");
|
||||
}
|
||||
|
||||
/* Loop, blitting sprites and waiting for a keystroke */
|
||||
frames = 0;
|
||||
then = SDL_GetTicks();
|
||||
done = 0;
|
||||
sprites_visible = 0;
|
||||
while ( !done ) {
|
||||
/* Check for events */
|
||||
++frames;
|
||||
while ( SDL_PollEvent(&event) ) {
|
||||
switch (event.type) {
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
SDL_WarpMouse(screen->w/2, screen->h/2);
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
/* Any keypress quits the app... */
|
||||
case SDL_QUIT:
|
||||
done = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
MoveSprites(screen, background);
|
||||
}
|
||||
SDL_FreeSurface(sprite);
|
||||
free(mem);
|
||||
/* Loop, blitting sprites and waiting for a keystroke */
|
||||
frames = 0;
|
||||
then = SDL_GetTicks();
|
||||
done = 0;
|
||||
sprites_visible = 0;
|
||||
while (!done) {
|
||||
/* Check for events */
|
||||
++frames;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
switch (event.type) {
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
SDL_WarpMouse(screen->w / 2, screen->h / 2);
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
/* Any keypress quits the app... */
|
||||
case SDL_QUIT:
|
||||
done = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
MoveSprites(screen, background);
|
||||
}
|
||||
SDL_FreeSurface(sprite);
|
||||
free(mem);
|
||||
|
||||
/* Print out some timing information */
|
||||
now = SDL_GetTicks();
|
||||
if ( now > then ) {
|
||||
printf("%2.2f frames per second\n",
|
||||
((double)frames*1000)/(now-then));
|
||||
}
|
||||
SDL_Quit();
|
||||
return(0);
|
||||
/* Print out some timing information */
|
||||
now = SDL_GetTicks();
|
||||
if (now > then) {
|
||||
printf("%2.2f frames per second\n",
|
||||
((double) frames * 1000) / (now - then));
|
||||
}
|
||||
SDL_Quit();
|
||||
return (0);
|
||||
}
|
||||
|
||||
262
test/testsprite2.c
Normal file
262
test/testsprite2.c
Normal file
@@ -0,0 +1,262 @@
|
||||
/* Simple program: Move N sprites around on the screen as fast as possible */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "SDL.h"
|
||||
|
||||
#define NUM_WINDOWS 4
|
||||
#define WINDOW_W 640
|
||||
#define WINDOW_H 480
|
||||
#define NUM_SPRITES 100
|
||||
#define MAX_SPEED 1
|
||||
#define BACKGROUND 0x00FFFFFF
|
||||
|
||||
static int num_windows;
|
||||
static int num_sprites;
|
||||
static SDL_WindowID *windows;
|
||||
static SDL_TextureID *sprites;
|
||||
static SDL_Rect *positions;
|
||||
static SDL_Rect *velocities;
|
||||
static int sprite_w, sprite_h;
|
||||
|
||||
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
||||
static void
|
||||
quit(int rc)
|
||||
{
|
||||
if (windows) {
|
||||
SDL_free(windows);
|
||||
}
|
||||
if (sprites) {
|
||||
SDL_free(sprites);
|
||||
}
|
||||
if (positions) {
|
||||
SDL_free(positions);
|
||||
}
|
||||
if (velocities) {
|
||||
SDL_free(velocities);
|
||||
}
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
}
|
||||
|
||||
int
|
||||
LoadSprite(char *file)
|
||||
{
|
||||
int i;
|
||||
SDL_Surface *temp;
|
||||
|
||||
/* Load the sprite image */
|
||||
temp = SDL_LoadBMP(file);
|
||||
if (temp == NULL) {
|
||||
fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
|
||||
return (-1);
|
||||
}
|
||||
sprite_w = temp->w;
|
||||
sprite_h = temp->h;
|
||||
|
||||
/* Set transparent pixel as the pixel at (0,0) */
|
||||
if (temp->format->palette) {
|
||||
SDL_SetColorKey(temp, SDL_SRCCOLORKEY, *(Uint8 *) temp->pixels);
|
||||
}
|
||||
|
||||
/* Create textures from the image */
|
||||
for (i = 0; i < num_windows; ++i) {
|
||||
SDL_SelectRenderer(windows[i]);
|
||||
sprites[i] =
|
||||
SDL_CreateTextureFromSurface(0, SDL_TextureAccess_Remote, temp);
|
||||
if (!sprites[i]) {
|
||||
fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
|
||||
SDL_FreeSurface(temp);
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
SDL_FreeSurface(temp);
|
||||
|
||||
/* We're ready to roll. :) */
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
MoveSprites(SDL_WindowID window, SDL_TextureID sprite)
|
||||
{
|
||||
int i, n;
|
||||
int window_w, window_h;
|
||||
SDL_Rect area, *position, *velocity;
|
||||
|
||||
SDL_SelectRenderer(window);
|
||||
|
||||
/* Query the sizes */
|
||||
SDL_GetWindowSize(window, &window_w, &window_h);
|
||||
|
||||
/* Move the sprite, bounce at the wall, and draw */
|
||||
n = 0;
|
||||
for (i = 0; i < num_sprites; ++i) {
|
||||
position = &positions[i];
|
||||
SDL_RenderFill(position, BACKGROUND);
|
||||
}
|
||||
for (i = 0; i < num_sprites; ++i) {
|
||||
position = &positions[i];
|
||||
velocity = &velocities[i];
|
||||
position->x += velocity->x;
|
||||
if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
|
||||
velocity->x = -velocity->x;
|
||||
position->x += velocity->x;
|
||||
}
|
||||
position->y += velocity->y;
|
||||
if ((position->y < 0) || (position->y >= (window_h - sprite_w))) {
|
||||
velocity->y = -velocity->y;
|
||||
position->y += velocity->y;
|
||||
}
|
||||
|
||||
/* Blit the sprite onto the screen */
|
||||
SDL_RenderCopy(sprite, NULL, position, SDL_TextureBlendMode_Mask,
|
||||
SDL_TextureScaleMode_None);
|
||||
}
|
||||
|
||||
/* Update the screen! */
|
||||
SDL_RenderPresent();
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int window_w, window_h;
|
||||
Uint32 window_flags = SDL_WINDOW_SHOWN;
|
||||
SDL_DisplayMode *mode, fullscreen_mode;
|
||||
int i, done;
|
||||
SDL_Event event;
|
||||
Uint32 then, now, frames;
|
||||
|
||||
/* Initialize SDL */
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return (1);
|
||||
}
|
||||
|
||||
num_windows = NUM_WINDOWS;
|
||||
num_sprites = NUM_SPRITES;
|
||||
window_w = WINDOW_W;
|
||||
window_h = WINDOW_H;
|
||||
while (argc > 1) {
|
||||
if (strcmp(argv[argc - 1], "-width") == 0) {
|
||||
window_w = atoi(argv[argc]);
|
||||
--argc;
|
||||
} else if (strcmp(argv[argc - 1], "-height") == 0) {
|
||||
window_h = atoi(argv[argc]);
|
||||
--argc;
|
||||
} else if (strcmp(argv[argc - 1], "-fullscreen") == 0) {
|
||||
num_windows = 1;
|
||||
window_flags |= SDL_WINDOW_FULLSCREEN;
|
||||
--argc;
|
||||
} else if (isdigit(argv[argc][0])) {
|
||||
num_sprites = atoi(argv[argc]);
|
||||
} else {
|
||||
fprintf(stderr,
|
||||
"Usage: %s [-width] [-height] [numsprites]\n", argv[0]);
|
||||
quit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (window_flags & SDL_WINDOW_FULLSCREEN) {
|
||||
SDL_zero(fullscreen_mode);
|
||||
fullscreen_mode.w = window_w;
|
||||
fullscreen_mode.h = window_h;
|
||||
SDL_SetFullscreenDisplayMode(&fullscreen_mode);
|
||||
}
|
||||
|
||||
/* Create the windows, initialize the renderers, and load the textures */
|
||||
windows = (SDL_WindowID *) SDL_malloc(num_windows * sizeof(*windows));
|
||||
sprites = (SDL_TextureID *) SDL_malloc(num_windows * sizeof(*sprites));
|
||||
if (!windows || !sprites) {
|
||||
fprintf(stderr, "Out of memory!\n");
|
||||
quit(2);
|
||||
}
|
||||
for (i = 0; i < num_windows; ++i) {
|
||||
char title[32];
|
||||
|
||||
SDL_snprintf(title, sizeof(title), "testsprite %d", i + 1);
|
||||
windows[i] =
|
||||
SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED,
|
||||
SDL_WINDOWPOS_UNDEFINED, window_w, window_h,
|
||||
window_flags);
|
||||
if (!windows[i]) {
|
||||
fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
|
||||
quit(2);
|
||||
}
|
||||
|
||||
if (SDL_CreateRenderer(windows[i], -1, 0) < 0) {
|
||||
fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
|
||||
quit(2);
|
||||
}
|
||||
SDL_RenderFill(NULL, BACKGROUND);
|
||||
}
|
||||
if (LoadSprite("icon.bmp") < 0) {
|
||||
quit(2);
|
||||
}
|
||||
|
||||
/* Allocate memory for the sprite info */
|
||||
positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
|
||||
velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
|
||||
if (!positions || !velocities) {
|
||||
fprintf(stderr, "Out of memory!\n");
|
||||
quit(2);
|
||||
}
|
||||
srand(time(NULL));
|
||||
for (i = 0; i < num_sprites; ++i) {
|
||||
positions[i].x = rand() % (window_w - sprite_w);
|
||||
positions[i].y = rand() % (window_h - sprite_h);
|
||||
positions[i].w = sprite_w;
|
||||
positions[i].h = sprite_h;
|
||||
velocities[i].x = 0;
|
||||
velocities[i].y = 0;
|
||||
while (!velocities[i].x && !velocities[i].y) {
|
||||
velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
|
||||
velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
|
||||
}
|
||||
}
|
||||
|
||||
/* Loop, blitting sprites and waiting for a keystroke */
|
||||
frames = 0;
|
||||
then = SDL_GetTicks();
|
||||
done = 0;
|
||||
while (!done) {
|
||||
/* Check for events */
|
||||
++frames;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
switch (event.type) {
|
||||
case SDL_WINDOWEVENT:
|
||||
switch (event.window.event) {
|
||||
case SDL_WINDOWEVENT_EXPOSED:
|
||||
SDL_SelectRenderer(event.window.windowID);
|
||||
SDL_RenderFill(NULL, BACKGROUND);
|
||||
break;
|
||||
case SDL_WINDOWEVENT_CLOSE:
|
||||
done = 1;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
/* Any keypress quits the app... */
|
||||
case SDL_QUIT:
|
||||
done = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < num_windows; ++i) {
|
||||
MoveSprites(windows[i], sprites[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/* Print out some timing information */
|
||||
now = SDL_GetTicks();
|
||||
if (now > then) {
|
||||
printf("%2.2f frames per second\n",
|
||||
((double) frames * 1000) / (now - then));
|
||||
}
|
||||
quit(0);
|
||||
}
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
115
test/testtimer.c
115
test/testtimer.c
@@ -12,76 +12,79 @@
|
||||
|
||||
static int ticks = 0;
|
||||
|
||||
static Uint32 SDLCALL ticktock(Uint32 interval)
|
||||
static Uint32 SDLCALL
|
||||
ticktock(Uint32 interval)
|
||||
{
|
||||
++ticks;
|
||||
return(interval);
|
||||
++ticks;
|
||||
return (interval);
|
||||
}
|
||||
|
||||
static Uint32 SDLCALL callback(Uint32 interval, void *param)
|
||||
static Uint32 SDLCALL
|
||||
callback(Uint32 interval, void *param)
|
||||
{
|
||||
printf("Timer %d : param = %d\n", interval, (int)(uintptr_t)param);
|
||||
return interval;
|
||||
printf("Timer %d : param = %d\n", interval, (int) (uintptr_t) param);
|
||||
return interval;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int desired;
|
||||
SDL_TimerID t1, t2, t3;
|
||||
int desired;
|
||||
SDL_TimerID t1, t2, t3;
|
||||
|
||||
if ( SDL_Init(SDL_INIT_TIMER) < 0 ) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return(1);
|
||||
}
|
||||
if (SDL_Init(SDL_INIT_TIMER) < 0) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return (1);
|
||||
}
|
||||
|
||||
/* Start the timer */
|
||||
desired = 0;
|
||||
if ( argv[1] ) {
|
||||
desired = atoi(argv[1]);
|
||||
}
|
||||
if ( desired == 0 ) {
|
||||
desired = DEFAULT_RESOLUTION;
|
||||
}
|
||||
SDL_SetTimer(desired, ticktock);
|
||||
/* Start the timer */
|
||||
desired = 0;
|
||||
if (argv[1]) {
|
||||
desired = atoi(argv[1]);
|
||||
}
|
||||
if (desired == 0) {
|
||||
desired = DEFAULT_RESOLUTION;
|
||||
}
|
||||
SDL_SetTimer(desired, ticktock);
|
||||
|
||||
/* Wait 10 seconds */
|
||||
printf("Waiting 10 seconds\n");
|
||||
SDL_Delay(10*1000);
|
||||
/* Wait 10 seconds */
|
||||
printf("Waiting 10 seconds\n");
|
||||
SDL_Delay(10 * 1000);
|
||||
|
||||
/* Stop the timer */
|
||||
SDL_SetTimer(0, NULL);
|
||||
/* Stop the timer */
|
||||
SDL_SetTimer(0, NULL);
|
||||
|
||||
/* Print the results */
|
||||
if ( ticks ) {
|
||||
fprintf(stderr,
|
||||
"Timer resolution: desired = %d ms, actual = %f ms\n",
|
||||
desired, (double)(10*1000)/ticks);
|
||||
}
|
||||
|
||||
/* Test multiple timers */
|
||||
printf("Testing multiple timers...\n");
|
||||
t1 = SDL_AddTimer(100, callback, (void*)1);
|
||||
if(!t1)
|
||||
fprintf(stderr,"Could not create timer 1: %s\n", SDL_GetError());
|
||||
t2 = SDL_AddTimer(50, callback, (void*)2);
|
||||
if(!t2)
|
||||
fprintf(stderr,"Could not create timer 2: %s\n", SDL_GetError());
|
||||
t3 = SDL_AddTimer(233, callback, (void*)3);
|
||||
if(!t3)
|
||||
fprintf(stderr,"Could not create timer 3: %s\n", SDL_GetError());
|
||||
|
||||
/* Wait 10 seconds */
|
||||
printf("Waiting 10 seconds\n");
|
||||
SDL_Delay(10*1000);
|
||||
/* Print the results */
|
||||
if (ticks) {
|
||||
fprintf(stderr,
|
||||
"Timer resolution: desired = %d ms, actual = %f ms\n",
|
||||
desired, (double) (10 * 1000) / ticks);
|
||||
}
|
||||
|
||||
printf("Removing timer 1 and waiting 5 more seconds\n");
|
||||
SDL_RemoveTimer(t1);
|
||||
/* Test multiple timers */
|
||||
printf("Testing multiple timers...\n");
|
||||
t1 = SDL_AddTimer(100, callback, (void *) 1);
|
||||
if (!t1)
|
||||
fprintf(stderr, "Could not create timer 1: %s\n", SDL_GetError());
|
||||
t2 = SDL_AddTimer(50, callback, (void *) 2);
|
||||
if (!t2)
|
||||
fprintf(stderr, "Could not create timer 2: %s\n", SDL_GetError());
|
||||
t3 = SDL_AddTimer(233, callback, (void *) 3);
|
||||
if (!t3)
|
||||
fprintf(stderr, "Could not create timer 3: %s\n", SDL_GetError());
|
||||
|
||||
SDL_Delay(5*1000);
|
||||
/* Wait 10 seconds */
|
||||
printf("Waiting 10 seconds\n");
|
||||
SDL_Delay(10 * 1000);
|
||||
|
||||
SDL_RemoveTimer(t2);
|
||||
SDL_RemoveTimer(t3);
|
||||
printf("Removing timer 1 and waiting 5 more seconds\n");
|
||||
SDL_RemoveTimer(t1);
|
||||
|
||||
SDL_Quit();
|
||||
return(0);
|
||||
SDL_Delay(5 * 1000);
|
||||
|
||||
SDL_RemoveTimer(t2);
|
||||
SDL_RemoveTimer(t3);
|
||||
|
||||
SDL_Quit();
|
||||
return (0);
|
||||
}
|
||||
|
||||
@@ -8,30 +8,30 @@
|
||||
|
||||
#include "SDL.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
SDL_version compiled;
|
||||
SDL_version compiled;
|
||||
|
||||
/* Initialize SDL */
|
||||
if ( SDL_Init(0) < 0 ) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
|
||||
exit(1);
|
||||
}
|
||||
/* Initialize SDL */
|
||||
if (SDL_Init(0) < 0) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
exit(1);
|
||||
}
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "SDL initialized\n");
|
||||
fprintf(stderr, "SDL initialized\n");
|
||||
#endif
|
||||
#if SDL_VERSION_ATLEAST(1, 2, 0)
|
||||
printf("Compiled with SDL 1.2 or newer\n");
|
||||
printf("Compiled with SDL 1.2 or newer\n");
|
||||
#else
|
||||
printf("Compiled with SDL older than 1.2\n");
|
||||
printf("Compiled with SDL older than 1.2\n");
|
||||
#endif
|
||||
SDL_VERSION(&compiled);
|
||||
printf("Compiled version: %d.%d.%d\n",
|
||||
compiled.major, compiled.minor, compiled.patch);
|
||||
printf("Linked version: %d.%d.%d\n",
|
||||
SDL_Linked_Version()->major,
|
||||
SDL_Linked_Version()->minor,
|
||||
SDL_Linked_Version()->patch);
|
||||
SDL_Quit();
|
||||
return(0);
|
||||
SDL_VERSION(&compiled);
|
||||
printf("Compiled version: %d.%d.%d\n",
|
||||
compiled.major, compiled.minor, compiled.patch);
|
||||
printf("Linked version: %d.%d.%d\n",
|
||||
SDL_Linked_Version()->major,
|
||||
SDL_Linked_Version()->minor, SDL_Linked_Version()->patch);
|
||||
SDL_Quit();
|
||||
return (0);
|
||||
}
|
||||
|
||||
@@ -15,451 +15,517 @@
|
||||
SDL_SRCCOLORKEY | SDL_SRCALPHA | SDL_RLEACCEL | \
|
||||
SDL_RLEACCELOK)
|
||||
|
||||
void PrintFlags(Uint32 flags)
|
||||
#if 0
|
||||
void
|
||||
PrintFlags(Uint32 flags)
|
||||
{
|
||||
printf("0x%8.8x", (flags & FLAG_MASK));
|
||||
if ( flags & SDL_HWSURFACE ) {
|
||||
printf(" SDL_HWSURFACE");
|
||||
} else {
|
||||
printf(" SDL_SWSURFACE");
|
||||
}
|
||||
if ( flags & SDL_FULLSCREEN ) {
|
||||
printf(" | SDL_FULLSCREEN");
|
||||
}
|
||||
if ( flags & SDL_DOUBLEBUF ) {
|
||||
printf(" | SDL_DOUBLEBUF");
|
||||
}
|
||||
if ( flags & SDL_SRCCOLORKEY ) {
|
||||
printf(" | SDL_SRCCOLORKEY");
|
||||
}
|
||||
if ( flags & SDL_SRCALPHA ) {
|
||||
printf(" | SDL_SRCALPHA");
|
||||
}
|
||||
if ( flags & SDL_RLEACCEL ) {
|
||||
printf(" | SDL_RLEACCEL");
|
||||
}
|
||||
if ( flags & SDL_RLEACCELOK ) {
|
||||
printf(" | SDL_RLEACCELOK");
|
||||
}
|
||||
printf("0x%8.8x", (flags & FLAG_MASK));
|
||||
if (flags & SDL_HWSURFACE) {
|
||||
printf(" SDL_HWSURFACE");
|
||||
} else {
|
||||
printf(" SDL_SWSURFACE");
|
||||
}
|
||||
if (flags & SDL_FULLSCREEN) {
|
||||
printf(" | SDL_FULLSCREEN");
|
||||
}
|
||||
if (flags & SDL_DOUBLEBUF) {
|
||||
printf(" | SDL_DOUBLEBUF");
|
||||
}
|
||||
if (flags & SDL_SRCCOLORKEY) {
|
||||
printf(" | SDL_SRCCOLORKEY");
|
||||
}
|
||||
if (flags & SDL_SRCALPHA) {
|
||||
printf(" | SDL_SRCALPHA");
|
||||
}
|
||||
if (flags & SDL_RLEACCEL) {
|
||||
printf(" | SDL_RLEACCEL");
|
||||
}
|
||||
if (flags & SDL_RLEACCELOK) {
|
||||
printf(" | SDL_RLEACCELOK");
|
||||
}
|
||||
}
|
||||
|
||||
int RunBlitTests(SDL_Surface *screen, SDL_Surface *bmp, int blitcount)
|
||||
int
|
||||
RunBlitTests(SDL_Surface * screen, SDL_Surface * bmp, int blitcount)
|
||||
{
|
||||
int i, j;
|
||||
int maxx;
|
||||
int maxy;
|
||||
SDL_Rect dst;
|
||||
int i, j;
|
||||
int maxx;
|
||||
int maxy;
|
||||
SDL_Rect dst;
|
||||
|
||||
maxx = (int)screen->w - bmp->w + 1;
|
||||
maxy = (int)screen->h - bmp->h + 1;
|
||||
for ( i = 0; i < NUM_UPDATES; ++i ) {
|
||||
for ( j = 0; j < blitcount; ++j ) {
|
||||
if ( maxx ) {
|
||||
dst.x = rand() % maxx;
|
||||
} else {
|
||||
dst.x = 0;
|
||||
}
|
||||
if ( maxy ) {
|
||||
dst.y = rand() % maxy;
|
||||
} else {
|
||||
dst.y = 0;
|
||||
}
|
||||
dst.w = bmp->w;
|
||||
dst.h = bmp->h;
|
||||
SDL_BlitSurface(bmp, NULL, screen, &dst);
|
||||
}
|
||||
SDL_Flip(screen);
|
||||
}
|
||||
maxx = (int) screen->w - bmp->w + 1;
|
||||
maxy = (int) screen->h - bmp->h + 1;
|
||||
for (i = 0; i < NUM_UPDATES; ++i) {
|
||||
for (j = 0; j < blitcount; ++j) {
|
||||
if (maxx) {
|
||||
dst.x = rand() % maxx;
|
||||
} else {
|
||||
dst.x = 0;
|
||||
}
|
||||
if (maxy) {
|
||||
dst.y = rand() % maxy;
|
||||
} else {
|
||||
dst.y = 0;
|
||||
}
|
||||
dst.w = bmp->w;
|
||||
dst.h = bmp->h;
|
||||
SDL_BlitSurface(bmp, NULL, screen, &dst);
|
||||
}
|
||||
SDL_Flip(screen);
|
||||
}
|
||||
|
||||
return i;
|
||||
return i;
|
||||
}
|
||||
|
||||
int RunModeTests(SDL_Surface *screen)
|
||||
int
|
||||
RunModeTests(SDL_Surface * screen)
|
||||
{
|
||||
Uint32 then, now;
|
||||
Uint32 frames;
|
||||
float seconds;
|
||||
int i;
|
||||
Uint8 r, g, b;
|
||||
SDL_Surface *bmp, *bmpcc, *tmp;
|
||||
SDL_Event event;
|
||||
Uint32 then, now;
|
||||
Uint32 frames;
|
||||
float seconds;
|
||||
int i;
|
||||
Uint8 r, g, b;
|
||||
SDL_Surface *bmp, *bmpcc, *tmp;
|
||||
SDL_Event event;
|
||||
|
||||
while ( SDL_PollEvent(&event) ) {
|
||||
if ( event.type == SDL_KEYDOWN )
|
||||
return 0;
|
||||
}
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_KEYDOWN)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* First test fills and screen update speed */
|
||||
printf("Running color fill and fullscreen update test\n");
|
||||
then = SDL_GetTicks();
|
||||
frames = 0;
|
||||
for ( i = 0; i < 256; ++i ) {
|
||||
r = i;
|
||||
g = 0;
|
||||
b = 0;
|
||||
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
|
||||
SDL_Flip(screen);
|
||||
++frames;
|
||||
}
|
||||
for ( i = 0; i < 256; ++i ) {
|
||||
r = 0;
|
||||
g = i;
|
||||
b = 0;
|
||||
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
|
||||
SDL_Flip(screen);
|
||||
++frames;
|
||||
}
|
||||
for ( i = 0; i < 256; ++i ) {
|
||||
r = 0;
|
||||
g = 0;
|
||||
b = i;
|
||||
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
|
||||
SDL_Flip(screen);
|
||||
++frames;
|
||||
}
|
||||
now = SDL_GetTicks();
|
||||
seconds = (float)(now - then) / 1000.0f;
|
||||
if ( seconds > 0.0f ) {
|
||||
printf("%d fills and flips in %2.2f seconds, %2.2f FPS\n", frames, seconds, (float)frames / seconds);
|
||||
} else {
|
||||
printf("%d fills and flips in zero seconds!n", frames);
|
||||
}
|
||||
/* First test fills and screen update speed */
|
||||
printf("Running color fill and fullscreen update test\n");
|
||||
then = SDL_GetTicks();
|
||||
frames = 0;
|
||||
for (i = 0; i < 256; ++i) {
|
||||
r = i;
|
||||
g = 0;
|
||||
b = 0;
|
||||
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
|
||||
SDL_Flip(screen);
|
||||
++frames;
|
||||
}
|
||||
for (i = 0; i < 256; ++i) {
|
||||
r = 0;
|
||||
g = i;
|
||||
b = 0;
|
||||
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
|
||||
SDL_Flip(screen);
|
||||
++frames;
|
||||
}
|
||||
for (i = 0; i < 256; ++i) {
|
||||
r = 0;
|
||||
g = 0;
|
||||
b = i;
|
||||
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
|
||||
SDL_Flip(screen);
|
||||
++frames;
|
||||
}
|
||||
now = SDL_GetTicks();
|
||||
seconds = (float) (now - then) / 1000.0f;
|
||||
if (seconds > 0.0f) {
|
||||
printf("%d fills and flips in %2.2f seconds, %2.2f FPS\n", frames,
|
||||
seconds, (float) frames / seconds);
|
||||
} else {
|
||||
printf("%d fills and flips in zero seconds!n", frames);
|
||||
}
|
||||
|
||||
/* clear the screen after fill test */
|
||||
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
|
||||
SDL_Flip(screen);
|
||||
/* clear the screen after fill test */
|
||||
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
|
||||
SDL_Flip(screen);
|
||||
|
||||
while ( SDL_PollEvent(&event) ) {
|
||||
if ( event.type == SDL_KEYDOWN )
|
||||
return 0;
|
||||
}
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_KEYDOWN)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* run the generic blit test */
|
||||
bmp = SDL_LoadBMP("sample.bmp");
|
||||
if ( ! bmp ) {
|
||||
printf("Couldn't load sample.bmp: %s\n", SDL_GetError());
|
||||
return 0;
|
||||
}
|
||||
printf("Running freshly loaded blit test: %dx%d at %d bpp, flags: ",
|
||||
bmp->w, bmp->h, bmp->format->BitsPerPixel);
|
||||
PrintFlags(bmp->flags);
|
||||
printf("\n");
|
||||
then = SDL_GetTicks();
|
||||
frames = RunBlitTests(screen, bmp, NUM_BLITS);
|
||||
now = SDL_GetTicks();
|
||||
seconds = (float)(now - then) / 1000.0f;
|
||||
if ( seconds > 0.0f ) {
|
||||
printf("%d blits / %d updates in %2.2f seconds, %2.2f FPS\n", NUM_BLITS*frames, frames, seconds, (float)frames / seconds);
|
||||
} else {
|
||||
printf("%d blits / %d updates in zero seconds!\n", NUM_BLITS*frames, frames);
|
||||
}
|
||||
/* run the generic blit test */
|
||||
bmp = SDL_LoadBMP("sample.bmp");
|
||||
if (!bmp) {
|
||||
printf("Couldn't load sample.bmp: %s\n", SDL_GetError());
|
||||
return 0;
|
||||
}
|
||||
printf("Running freshly loaded blit test: %dx%d at %d bpp, flags: ",
|
||||
bmp->w, bmp->h, bmp->format->BitsPerPixel);
|
||||
PrintFlags(bmp->flags);
|
||||
printf("\n");
|
||||
then = SDL_GetTicks();
|
||||
frames = RunBlitTests(screen, bmp, NUM_BLITS);
|
||||
now = SDL_GetTicks();
|
||||
seconds = (float) (now - then) / 1000.0f;
|
||||
if (seconds > 0.0f) {
|
||||
printf("%d blits / %d updates in %2.2f seconds, %2.2f FPS\n",
|
||||
NUM_BLITS * frames, frames, seconds, (float) frames / seconds);
|
||||
} else {
|
||||
printf("%d blits / %d updates in zero seconds!\n",
|
||||
NUM_BLITS * frames, frames);
|
||||
}
|
||||
|
||||
/* clear the screen after blit test */
|
||||
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
|
||||
SDL_Flip(screen);
|
||||
/* clear the screen after blit test */
|
||||
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
|
||||
SDL_Flip(screen);
|
||||
|
||||
while ( SDL_PollEvent(&event) ) {
|
||||
if ( event.type == SDL_KEYDOWN )
|
||||
return 0;
|
||||
}
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_KEYDOWN)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* run the colorkeyed blit test */
|
||||
bmpcc = SDL_LoadBMP("sample.bmp");
|
||||
if ( ! bmpcc ) {
|
||||
printf("Couldn't load sample.bmp: %s\n", SDL_GetError());
|
||||
return 0;
|
||||
}
|
||||
printf("Running freshly loaded cc blit test: %dx%d at %d bpp, flags: ",
|
||||
bmpcc->w, bmpcc->h, bmpcc->format->BitsPerPixel);
|
||||
SDL_SetColorKey(bmpcc, SDL_SRCCOLORKEY | SDL_RLEACCEL, *(Uint8 *)bmpcc->pixels);
|
||||
/* run the colorkeyed blit test */
|
||||
bmpcc = SDL_LoadBMP("sample.bmp");
|
||||
if (!bmpcc) {
|
||||
printf("Couldn't load sample.bmp: %s\n", SDL_GetError());
|
||||
return 0;
|
||||
}
|
||||
printf("Running freshly loaded cc blit test: %dx%d at %d bpp, flags: ",
|
||||
bmpcc->w, bmpcc->h, bmpcc->format->BitsPerPixel);
|
||||
SDL_SetColorKey(bmpcc, SDL_SRCCOLORKEY | SDL_RLEACCEL,
|
||||
*(Uint8 *) bmpcc->pixels);
|
||||
|
||||
PrintFlags(bmpcc->flags);
|
||||
printf("\n");
|
||||
then = SDL_GetTicks();
|
||||
frames = RunBlitTests(screen, bmpcc, NUM_BLITS);
|
||||
now = SDL_GetTicks();
|
||||
seconds = (float)(now - then) / 1000.0f;
|
||||
if ( seconds > 0.0f ) {
|
||||
printf("%d cc blits / %d updates in %2.2f seconds, %2.2f FPS\n", NUM_BLITS*frames, frames, seconds, (float)frames / seconds);
|
||||
} else {
|
||||
printf("%d cc blits / %d updates in zero seconds!\n", NUM_BLITS*frames, frames);
|
||||
}
|
||||
PrintFlags(bmpcc->flags);
|
||||
printf("\n");
|
||||
then = SDL_GetTicks();
|
||||
frames = RunBlitTests(screen, bmpcc, NUM_BLITS);
|
||||
now = SDL_GetTicks();
|
||||
seconds = (float) (now - then) / 1000.0f;
|
||||
if (seconds > 0.0f) {
|
||||
printf("%d cc blits / %d updates in %2.2f seconds, %2.2f FPS\n",
|
||||
NUM_BLITS * frames, frames, seconds, (float) frames / seconds);
|
||||
} else {
|
||||
printf("%d cc blits / %d updates in zero seconds!\n",
|
||||
NUM_BLITS * frames, frames);
|
||||
}
|
||||
|
||||
/* clear the screen after cc blit test */
|
||||
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
|
||||
SDL_Flip(screen);
|
||||
/* clear the screen after cc blit test */
|
||||
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
|
||||
SDL_Flip(screen);
|
||||
|
||||
while ( SDL_PollEvent(&event) ) {
|
||||
if ( event.type == SDL_KEYDOWN )
|
||||
return 0;
|
||||
}
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_KEYDOWN)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* run the generic blit test */
|
||||
tmp = bmp;
|
||||
bmp = SDL_DisplayFormat(bmp);
|
||||
SDL_FreeSurface(tmp);
|
||||
if ( ! bmp ) {
|
||||
printf("Couldn't convert sample.bmp: %s\n", SDL_GetError());
|
||||
return 0;
|
||||
}
|
||||
printf("Running display format blit test: %dx%d at %d bpp, flags: ",
|
||||
bmp->w, bmp->h, bmp->format->BitsPerPixel);
|
||||
PrintFlags(bmp->flags);
|
||||
printf("\n");
|
||||
then = SDL_GetTicks();
|
||||
frames = RunBlitTests(screen, bmp, NUM_BLITS);
|
||||
now = SDL_GetTicks();
|
||||
seconds = (float)(now - then) / 1000.0f;
|
||||
if ( seconds > 0.0f ) {
|
||||
printf("%d blits / %d updates in %2.2f seconds, %2.2f FPS\n", NUM_BLITS*frames, frames, seconds, (float)frames / seconds);
|
||||
} else {
|
||||
printf("%d blits / %d updates in zero seconds!\n", NUM_BLITS*frames, frames);
|
||||
}
|
||||
/* run the generic blit test */
|
||||
tmp = bmp;
|
||||
bmp = SDL_DisplayFormat(bmp);
|
||||
SDL_FreeSurface(tmp);
|
||||
if (!bmp) {
|
||||
printf("Couldn't convert sample.bmp: %s\n", SDL_GetError());
|
||||
return 0;
|
||||
}
|
||||
printf("Running display format blit test: %dx%d at %d bpp, flags: ",
|
||||
bmp->w, bmp->h, bmp->format->BitsPerPixel);
|
||||
PrintFlags(bmp->flags);
|
||||
printf("\n");
|
||||
then = SDL_GetTicks();
|
||||
frames = RunBlitTests(screen, bmp, NUM_BLITS);
|
||||
now = SDL_GetTicks();
|
||||
seconds = (float) (now - then) / 1000.0f;
|
||||
if (seconds > 0.0f) {
|
||||
printf("%d blits / %d updates in %2.2f seconds, %2.2f FPS\n",
|
||||
NUM_BLITS * frames, frames, seconds, (float) frames / seconds);
|
||||
} else {
|
||||
printf("%d blits / %d updates in zero seconds!\n",
|
||||
NUM_BLITS * frames, frames);
|
||||
}
|
||||
|
||||
/* clear the screen after blit test */
|
||||
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
|
||||
SDL_Flip(screen);
|
||||
/* clear the screen after blit test */
|
||||
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
|
||||
SDL_Flip(screen);
|
||||
|
||||
while ( SDL_PollEvent(&event) ) {
|
||||
if ( event.type == SDL_KEYDOWN )
|
||||
return 0;
|
||||
}
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_KEYDOWN)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* run the colorkeyed blit test */
|
||||
tmp = bmpcc;
|
||||
bmpcc = SDL_DisplayFormat(bmpcc);
|
||||
SDL_FreeSurface(tmp);
|
||||
if ( ! bmpcc ) {
|
||||
printf("Couldn't convert sample.bmp: %s\n", SDL_GetError());
|
||||
return 0;
|
||||
}
|
||||
printf("Running display format cc blit test: %dx%d at %d bpp, flags: ",
|
||||
bmpcc->w, bmpcc->h, bmpcc->format->BitsPerPixel);
|
||||
PrintFlags(bmpcc->flags);
|
||||
printf("\n");
|
||||
then = SDL_GetTicks();
|
||||
frames = RunBlitTests(screen, bmpcc, NUM_BLITS);
|
||||
now = SDL_GetTicks();
|
||||
seconds = (float)(now - then) / 1000.0f;
|
||||
if ( seconds > 0.0f ) {
|
||||
printf("%d cc blits / %d updates in %2.2f seconds, %2.2f FPS\n", NUM_BLITS*frames, frames, seconds, (float)frames / seconds);
|
||||
} else {
|
||||
printf("%d cc blits / %d updates in zero seconds!\n", NUM_BLITS*frames, frames);
|
||||
}
|
||||
/* run the colorkeyed blit test */
|
||||
tmp = bmpcc;
|
||||
bmpcc = SDL_DisplayFormat(bmpcc);
|
||||
SDL_FreeSurface(tmp);
|
||||
if (!bmpcc) {
|
||||
printf("Couldn't convert sample.bmp: %s\n", SDL_GetError());
|
||||
return 0;
|
||||
}
|
||||
printf("Running display format cc blit test: %dx%d at %d bpp, flags: ",
|
||||
bmpcc->w, bmpcc->h, bmpcc->format->BitsPerPixel);
|
||||
PrintFlags(bmpcc->flags);
|
||||
printf("\n");
|
||||
then = SDL_GetTicks();
|
||||
frames = RunBlitTests(screen, bmpcc, NUM_BLITS);
|
||||
now = SDL_GetTicks();
|
||||
seconds = (float) (now - then) / 1000.0f;
|
||||
if (seconds > 0.0f) {
|
||||
printf("%d cc blits / %d updates in %2.2f seconds, %2.2f FPS\n",
|
||||
NUM_BLITS * frames, frames, seconds, (float) frames / seconds);
|
||||
} else {
|
||||
printf("%d cc blits / %d updates in zero seconds!\n",
|
||||
NUM_BLITS * frames, frames);
|
||||
}
|
||||
|
||||
/* clear the screen after cc blit test */
|
||||
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
|
||||
SDL_Flip(screen);
|
||||
/* clear the screen after cc blit test */
|
||||
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
|
||||
SDL_Flip(screen);
|
||||
|
||||
while ( SDL_PollEvent(&event) ) {
|
||||
if ( event.type == SDL_KEYDOWN )
|
||||
return 0;
|
||||
}
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_KEYDOWN)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* run the alpha blit test only if screen bpp>8 */
|
||||
if (bmp->format->BitsPerPixel>8)
|
||||
/* run the alpha blit test only if screen bpp>8 */
|
||||
if (bmp->format->BitsPerPixel > 8) {
|
||||
SDL_FreeSurface(bmp);
|
||||
bmp = SDL_LoadBMP("sample.bmp");
|
||||
SDL_SetAlpha(bmp, SDL_SRCALPHA, 85); /* 85 - 33% alpha */
|
||||
tmp = bmp;
|
||||
bmp = SDL_DisplayFormat(bmp);
|
||||
SDL_FreeSurface(tmp);
|
||||
if (!bmp) {
|
||||
printf("Couldn't convert sample.bmp: %s\n", SDL_GetError());
|
||||
return 0;
|
||||
}
|
||||
printf
|
||||
("Running display format alpha blit test: %dx%d at %d bpp, flags: ",
|
||||
bmp->w, bmp->h, bmp->format->BitsPerPixel);
|
||||
PrintFlags(bmp->flags);
|
||||
printf("\n");
|
||||
then = SDL_GetTicks();
|
||||
frames = RunBlitTests(screen, bmp, NUM_BLITS);
|
||||
now = SDL_GetTicks();
|
||||
seconds = (float) (now - then) / 1000.0f;
|
||||
if (seconds > 0.0f) {
|
||||
printf
|
||||
("%d alpha blits / %d updates in %2.2f seconds, %2.2f FPS\n",
|
||||
NUM_BLITS * frames, frames, seconds,
|
||||
(float) frames / seconds);
|
||||
} else {
|
||||
printf("%d alpha blits / %d updates in zero seconds!\n",
|
||||
NUM_BLITS * frames, frames);
|
||||
}
|
||||
}
|
||||
|
||||
/* clear the screen after alpha blit test */
|
||||
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
|
||||
SDL_Flip(screen);
|
||||
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_KEYDOWN)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* run the cc+alpha blit test only if screen bpp>8 */
|
||||
if (bmp->format->BitsPerPixel > 8) {
|
||||
SDL_FreeSurface(bmpcc);
|
||||
bmpcc = SDL_LoadBMP("sample.bmp");
|
||||
SDL_SetAlpha(bmpcc, SDL_SRCALPHA, 85); /* 85 - 33% alpha */
|
||||
SDL_SetColorKey(bmpcc, SDL_SRCCOLORKEY | SDL_RLEACCEL,
|
||||
*(Uint8 *) bmpcc->pixels);
|
||||
tmp = bmpcc;
|
||||
bmpcc = SDL_DisplayFormat(bmpcc);
|
||||
SDL_FreeSurface(tmp);
|
||||
if (!bmpcc) {
|
||||
printf("Couldn't convert sample.bmp: %s\n", SDL_GetError());
|
||||
return 0;
|
||||
}
|
||||
printf
|
||||
("Running display format cc+alpha blit test: %dx%d at %d bpp, flags: ",
|
||||
bmpcc->w, bmpcc->h, bmpcc->format->BitsPerPixel);
|
||||
PrintFlags(bmpcc->flags);
|
||||
printf("\n");
|
||||
then = SDL_GetTicks();
|
||||
frames = RunBlitTests(screen, bmpcc, NUM_BLITS);
|
||||
now = SDL_GetTicks();
|
||||
seconds = (float) (now - then) / 1000.0f;
|
||||
if (seconds > 0.0f) {
|
||||
printf
|
||||
("%d cc+alpha blits / %d updates in %2.2f seconds, %2.2f FPS\n",
|
||||
NUM_BLITS * frames, frames, seconds,
|
||||
(float) frames / seconds);
|
||||
} else {
|
||||
printf("%d cc+alpha blits / %d updates in zero seconds!\n",
|
||||
NUM_BLITS * frames, frames);
|
||||
}
|
||||
}
|
||||
|
||||
SDL_FreeSurface(bmpcc);
|
||||
SDL_FreeSurface(bmp);
|
||||
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_KEYDOWN)
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void
|
||||
RunVideoTests()
|
||||
{
|
||||
static const struct
|
||||
{
|
||||
int w, h, bpp;
|
||||
} mode_list[] = {
|
||||
{
|
||||
SDL_FreeSurface(bmp);
|
||||
bmp = SDL_LoadBMP("sample.bmp");
|
||||
SDL_SetAlpha(bmp, SDL_SRCALPHA, 85); /* 85 - 33% alpha */
|
||||
tmp = bmp;
|
||||
bmp = SDL_DisplayFormat(bmp);
|
||||
SDL_FreeSurface(tmp);
|
||||
if ( ! bmp ) {
|
||||
printf("Couldn't convert sample.bmp: %s\n", SDL_GetError());
|
||||
return 0;
|
||||
}
|
||||
printf("Running display format alpha blit test: %dx%d at %d bpp, flags: ",
|
||||
bmp->w, bmp->h, bmp->format->BitsPerPixel);
|
||||
PrintFlags(bmp->flags);
|
||||
printf("\n");
|
||||
then = SDL_GetTicks();
|
||||
frames = RunBlitTests(screen, bmp, NUM_BLITS);
|
||||
now = SDL_GetTicks();
|
||||
seconds = (float)(now - then) / 1000.0f;
|
||||
if ( seconds > 0.0f ) {
|
||||
printf("%d alpha blits / %d updates in %2.2f seconds, %2.2f FPS\n", NUM_BLITS*frames, frames, seconds, (float)frames / seconds);
|
||||
} else {
|
||||
printf("%d alpha blits / %d updates in zero seconds!\n", NUM_BLITS*frames, frames);
|
||||
}
|
||||
}
|
||||
640, 480, 8}, {
|
||||
640, 480, 16}, {
|
||||
640, 480, 32}, {
|
||||
800, 600, 8}, {
|
||||
800, 600, 16}, {
|
||||
800, 600, 32}, {
|
||||
1024, 768, 8}, {
|
||||
1024, 768, 16}, {
|
||||
1024, 768, 32}
|
||||
};
|
||||
static const Uint32 flags[] = {
|
||||
(SDL_SWSURFACE),
|
||||
(SDL_SWSURFACE | SDL_FULLSCREEN),
|
||||
(SDL_HWSURFACE | SDL_FULLSCREEN),
|
||||
(SDL_HWSURFACE | SDL_FULLSCREEN | SDL_DOUBLEBUF)
|
||||
};
|
||||
int i, j;
|
||||
SDL_Surface *screen;
|
||||
|
||||
/* clear the screen after alpha blit test */
|
||||
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
|
||||
SDL_Flip(screen);
|
||||
|
||||
while ( SDL_PollEvent(&event) ) {
|
||||
if ( event.type == SDL_KEYDOWN )
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* run the cc+alpha blit test only if screen bpp>8 */
|
||||
if (bmp->format->BitsPerPixel>8)
|
||||
{
|
||||
SDL_FreeSurface(bmpcc);
|
||||
bmpcc = SDL_LoadBMP("sample.bmp");
|
||||
SDL_SetAlpha(bmpcc, SDL_SRCALPHA, 85); /* 85 - 33% alpha */
|
||||
SDL_SetColorKey(bmpcc, SDL_SRCCOLORKEY | SDL_RLEACCEL, *(Uint8 *)bmpcc->pixels);
|
||||
tmp = bmpcc;
|
||||
bmpcc = SDL_DisplayFormat(bmpcc);
|
||||
SDL_FreeSurface(tmp);
|
||||
if ( ! bmpcc ) {
|
||||
printf("Couldn't convert sample.bmp: %s\n", SDL_GetError());
|
||||
return 0;
|
||||
}
|
||||
printf("Running display format cc+alpha blit test: %dx%d at %d bpp, flags: ",
|
||||
bmpcc->w, bmpcc->h, bmpcc->format->BitsPerPixel);
|
||||
PrintFlags(bmpcc->flags);
|
||||
printf("\n");
|
||||
then = SDL_GetTicks();
|
||||
frames = RunBlitTests(screen, bmpcc, NUM_BLITS);
|
||||
now = SDL_GetTicks();
|
||||
seconds = (float)(now - then) / 1000.0f;
|
||||
if ( seconds > 0.0f ) {
|
||||
printf("%d cc+alpha blits / %d updates in %2.2f seconds, %2.2f FPS\n", NUM_BLITS*frames, frames, seconds, (float)frames / seconds);
|
||||
} else {
|
||||
printf("%d cc+alpha blits / %d updates in zero seconds!\n", NUM_BLITS*frames, frames);
|
||||
}
|
||||
}
|
||||
|
||||
SDL_FreeSurface(bmpcc);
|
||||
SDL_FreeSurface(bmp);
|
||||
|
||||
while ( SDL_PollEvent(&event) ) {
|
||||
if ( event.type == SDL_KEYDOWN )
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
/* Test out several different video mode combinations */
|
||||
SDL_WM_SetCaption("SDL Video Benchmark", "vidtest");
|
||||
SDL_ShowCursor(0);
|
||||
for (i = 0; i < SDL_TABLESIZE(mode_list); ++i) {
|
||||
for (j = 0; j < SDL_TABLESIZE(flags); ++j) {
|
||||
printf("===================================\n");
|
||||
printf("Setting video mode: %dx%d at %d bpp, flags: ",
|
||||
mode_list[i].w, mode_list[i].h, mode_list[i].bpp);
|
||||
PrintFlags(flags[j]);
|
||||
printf("\n");
|
||||
screen = SDL_SetVideoMode(mode_list[i].w,
|
||||
mode_list[i].h,
|
||||
mode_list[i].bpp, flags[j]);
|
||||
if (!screen) {
|
||||
printf("Setting video mode failed: %s\n", SDL_GetError());
|
||||
continue;
|
||||
}
|
||||
if ((screen->flags & FLAG_MASK) != flags[j]) {
|
||||
printf("Flags didn't match: ");
|
||||
PrintFlags(screen->flags);
|
||||
printf("\n");
|
||||
continue;
|
||||
}
|
||||
if (!RunModeTests(screen)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void RunVideoTests()
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
static const struct {
|
||||
int w, h, bpp;
|
||||
} mode_list[] = {
|
||||
{ 640, 480, 8 }, { 640, 480, 16 }, { 640, 480, 32 },
|
||||
{ 800, 600, 8 }, { 800, 600, 16 }, { 800, 600, 32 },
|
||||
{ 1024, 768, 8 }, { 1024, 768, 16 }, { 1024, 768, 32 }
|
||||
};
|
||||
static const Uint32 flags[] = {
|
||||
(SDL_SWSURFACE),
|
||||
(SDL_SWSURFACE | SDL_FULLSCREEN),
|
||||
(SDL_HWSURFACE | SDL_FULLSCREEN),
|
||||
(SDL_HWSURFACE | SDL_FULLSCREEN | SDL_DOUBLEBUF)
|
||||
};
|
||||
int i, j;
|
||||
SDL_Surface *screen;
|
||||
const SDL_VideoInfo *info;
|
||||
int i, d, n;
|
||||
const char *driver;
|
||||
const SDL_DisplayMode *mode;
|
||||
int bpp;
|
||||
Uint32 Rmask, Gmask, Bmask, Amask;
|
||||
int nmodes;
|
||||
|
||||
/* Test out several different video mode combinations */
|
||||
SDL_WM_SetCaption("SDL Video Benchmark", "vidtest");
|
||||
SDL_ShowCursor(0);
|
||||
for ( i = 0; i < SDL_TABLESIZE(mode_list); ++i ) {
|
||||
for ( j = 0; j < SDL_TABLESIZE(flags); ++j ) {
|
||||
printf("===================================\n");
|
||||
printf("Setting video mode: %dx%d at %d bpp, flags: ",
|
||||
mode_list[i].w,
|
||||
mode_list[i].h,
|
||||
mode_list[i].bpp);
|
||||
PrintFlags(flags[j]);
|
||||
printf("\n");
|
||||
screen = SDL_SetVideoMode(mode_list[i].w,
|
||||
mode_list[i].h,
|
||||
mode_list[i].bpp,
|
||||
flags[j]);
|
||||
if ( ! screen ) {
|
||||
printf("Setting video mode failed: %s\n", SDL_GetError());
|
||||
continue;
|
||||
}
|
||||
if ( (screen->flags & FLAG_MASK) != flags[j] ) {
|
||||
printf("Flags didn't match: ");
|
||||
PrintFlags(screen->flags);
|
||||
printf("\n");
|
||||
continue;
|
||||
}
|
||||
if ( ! RunModeTests(screen) ) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
const SDL_VideoInfo *info;
|
||||
int i;
|
||||
SDL_Rect **modes;
|
||||
char driver[128];
|
||||
|
||||
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
|
||||
fprintf(stderr,
|
||||
"Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
exit(1);
|
||||
}
|
||||
if ( SDL_VideoDriverName(driver, sizeof(driver)) ) {
|
||||
printf("Video driver: %s\n", driver);
|
||||
}
|
||||
info = SDL_GetVideoInfo();
|
||||
printf(
|
||||
"Current display: %dx%d, %d bits-per-pixel\n",
|
||||
info->current_w, info->current_h, info->vfmt->BitsPerPixel);
|
||||
if ( info->vfmt->palette == NULL ) {
|
||||
printf(" Red Mask = 0x%.8x\n", info->vfmt->Rmask);
|
||||
printf(" Green Mask = 0x%.8x\n", info->vfmt->Gmask);
|
||||
printf(" Blue Mask = 0x%.8x\n", info->vfmt->Bmask);
|
||||
}
|
||||
/* Print available fullscreen video modes */
|
||||
modes = SDL_ListModes(NULL, SDL_FULLSCREEN);
|
||||
if ( modes == (SDL_Rect **)0 ) {
|
||||
printf("No available fullscreen video modes\n");
|
||||
} else
|
||||
if ( modes == (SDL_Rect **)-1 ) {
|
||||
printf("No special fullscreen video modes\n");
|
||||
} else {
|
||||
printf("Fullscreen video modes:\n");
|
||||
for ( i=0; modes[i]; ++i ) {
|
||||
printf("\t%dx%dx%d\n", modes[i]->w, modes[i]->h, info->vfmt->BitsPerPixel);
|
||||
}
|
||||
}
|
||||
if ( info->wm_available ) {
|
||||
printf("A window manager is available\n");
|
||||
}
|
||||
if ( info->hw_available ) {
|
||||
printf("Hardware surfaces are available (%dK video memory)\n",
|
||||
info->video_mem);
|
||||
}
|
||||
if ( info->blit_hw ) {
|
||||
printf(
|
||||
"Copy blits between hardware surfaces are accelerated\n");
|
||||
}
|
||||
if ( info->blit_hw_CC ) {
|
||||
printf(
|
||||
"Colorkey blits between hardware surfaces are accelerated\n");
|
||||
}
|
||||
if ( info->blit_hw_A ) {
|
||||
printf(
|
||||
"Alpha blits between hardware surfaces are accelerated\n");
|
||||
}
|
||||
if ( info->blit_sw ) {
|
||||
printf(
|
||||
"Copy blits from software surfaces to hardware surfaces are accelerated\n");
|
||||
}
|
||||
if ( info->blit_sw_CC ) {
|
||||
printf(
|
||||
"Colorkey blits from software surfaces to hardware surfaces are accelerated\n");
|
||||
}
|
||||
if ( info->blit_sw_A ) {
|
||||
printf(
|
||||
"Alpha blits from software surfaces to hardware surfaces are accelerated\n");
|
||||
}
|
||||
if ( info->blit_fill ) {
|
||||
printf(
|
||||
"Color fills on hardware surfaces are accelerated\n");
|
||||
}
|
||||
|
||||
if ( argv[1] && (strcmp(argv[1], "-benchmark") == 0) ) {
|
||||
RunVideoTests();
|
||||
}
|
||||
|
||||
SDL_Quit();
|
||||
return(0);
|
||||
/* Print available video drivers */
|
||||
n = SDL_GetNumVideoDrivers();
|
||||
if (n == 0) {
|
||||
printf("No built-in video drivers\n");
|
||||
} else {
|
||||
printf("Built-in video drivers:");
|
||||
for (i = 0; i < n; ++i) {
|
||||
if (i > 0) {
|
||||
printf(",");
|
||||
}
|
||||
printf(" %s", SDL_GetVideoDriver(i));
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
exit(1);
|
||||
}
|
||||
driver = SDL_GetCurrentVideoDriver();
|
||||
if (driver) {
|
||||
printf("Video driver: %s\n", driver);
|
||||
}
|
||||
printf("Number of displays: %d\n", SDL_GetNumVideoDisplays());
|
||||
for (d = 0; d < SDL_GetNumVideoDisplays(); ++d) {
|
||||
printf("Display %d:\n", d);
|
||||
SDL_SelectVideoDisplay(d);
|
||||
|
||||
mode = SDL_GetDesktopDisplayMode();
|
||||
SDL_PixelFormatEnumToMasks(mode->format, &bpp, &Rmask, &Gmask, &Bmask,
|
||||
&Amask);
|
||||
printf(" Current mode: %dx%d@%dHz, %d bits-per-pixel\n", mode->w,
|
||||
mode->h, mode->refresh_rate, bpp);
|
||||
if (Rmask || Gmask || Bmask) {
|
||||
printf(" Red Mask = 0x%.8x\n", Rmask);
|
||||
printf(" Green Mask = 0x%.8x\n", Gmask);
|
||||
printf(" Blue Mask = 0x%.8x\n", Bmask);
|
||||
if (Amask)
|
||||
printf(" Alpha Mask = 0x%.8x\n", Amask);
|
||||
}
|
||||
|
||||
/* Print available fullscreen video modes */
|
||||
nmodes = SDL_GetNumDisplayModes();
|
||||
if (nmodes == 0) {
|
||||
printf("No available fullscreen video modes\n");
|
||||
} else {
|
||||
printf(" Fullscreen video modes:\n");
|
||||
for (i = 0; i < nmodes; ++i) {
|
||||
mode = SDL_GetDisplayMode(i);
|
||||
SDL_PixelFormatEnumToMasks(mode->format, &bpp, &Rmask,
|
||||
&Gmask, &Bmask, &Amask);
|
||||
printf(" Mode %d: %dx%d@%dHz, %d bits-per-pixel\n", i,
|
||||
mode->w, mode->h, mode->refresh_rate, bpp);
|
||||
if (Rmask || Gmask || Bmask) {
|
||||
printf(" Red Mask = 0x%.8x\n", Rmask);
|
||||
printf(" Green Mask = 0x%.8x\n", Gmask);
|
||||
printf(" Blue Mask = 0x%.8x\n", Bmask);
|
||||
if (Amask)
|
||||
printf(" Alpha Mask = 0x%.8x\n", Amask);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
info = SDL_GetVideoInfo();
|
||||
if (info->wm_available) {
|
||||
printf("A window manager is available\n");
|
||||
}
|
||||
if (info->hw_available) {
|
||||
printf("Hardware surfaces are available (%dK video memory)\n",
|
||||
info->video_mem);
|
||||
}
|
||||
if (info->blit_hw) {
|
||||
printf("Copy blits between hardware surfaces are accelerated\n");
|
||||
}
|
||||
if (info->blit_hw_CC) {
|
||||
printf("Colorkey blits between hardware surfaces are accelerated\n");
|
||||
}
|
||||
if (info->blit_hw_A) {
|
||||
printf("Alpha blits between hardware surfaces are accelerated\n");
|
||||
}
|
||||
if (info->blit_sw) {
|
||||
printf
|
||||
("Copy blits from software surfaces to hardware surfaces are accelerated\n");
|
||||
}
|
||||
if (info->blit_sw_CC) {
|
||||
printf
|
||||
("Colorkey blits from software surfaces to hardware surfaces are accelerated\n");
|
||||
}
|
||||
if (info->blit_sw_A) {
|
||||
printf
|
||||
("Alpha blits from software surfaces to hardware surfaces are accelerated\n");
|
||||
}
|
||||
if (info->blit_fill) {
|
||||
printf("Color fills on hardware surfaces are accelerated\n");
|
||||
}
|
||||
#if 0
|
||||
if (argv[1] && (strcmp(argv[1], "-benchmark") == 0)) {
|
||||
RunVideoTests();
|
||||
}
|
||||
#endif
|
||||
|
||||
SDL_Quit();
|
||||
return (0);
|
||||
}
|
||||
|
||||
634
test/testwin.c
634
test/testwin.c
@@ -12,366 +12,344 @@
|
||||
#include "SDL.h"
|
||||
|
||||
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
||||
static void quit(int rc)
|
||||
static void
|
||||
quit(int rc)
|
||||
{
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
}
|
||||
|
||||
void DrawPict(SDL_Surface *screen, char *bmpfile,
|
||||
int speedy, int flip, int nofade)
|
||||
void
|
||||
DrawPict(SDL_Surface * screen, char *bmpfile,
|
||||
int speedy, int flip, int nofade)
|
||||
{
|
||||
SDL_Surface *picture;
|
||||
SDL_Rect dest, update;
|
||||
int i, centered;
|
||||
int ncolors;
|
||||
SDL_Color *colors, *cmap;
|
||||
SDL_Surface *picture;
|
||||
SDL_Rect dest, update;
|
||||
int i, centered;
|
||||
int ncolors;
|
||||
SDL_Color *colors, *cmap;
|
||||
|
||||
/* Load the image into a surface */
|
||||
if ( bmpfile == NULL ) {
|
||||
bmpfile = "sample.bmp"; /* Sample image */
|
||||
}
|
||||
fprintf(stderr, "Loading picture: %s\n", bmpfile);
|
||||
picture = SDL_LoadBMP(bmpfile);
|
||||
if ( picture == NULL ) {
|
||||
fprintf(stderr, "Couldn't load %s: %s\n", bmpfile,
|
||||
SDL_GetError());
|
||||
return;
|
||||
}
|
||||
/* Load the image into a surface */
|
||||
if (bmpfile == NULL) {
|
||||
bmpfile = "sample.bmp"; /* Sample image */
|
||||
}
|
||||
fprintf(stderr, "Loading picture: %s\n", bmpfile);
|
||||
picture = SDL_LoadBMP(bmpfile);
|
||||
if (picture == NULL) {
|
||||
fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError());
|
||||
return;
|
||||
}
|
||||
|
||||
/* Set the display colors -- on a hicolor display this is a no-op */
|
||||
if ( picture->format->palette ) {
|
||||
ncolors = picture->format->palette->ncolors;
|
||||
colors = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
|
||||
cmap = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
|
||||
memcpy(colors, picture->format->palette->colors,
|
||||
ncolors*sizeof(SDL_Color));
|
||||
} else {
|
||||
int r, g, b;
|
||||
/* Set the display colors -- on a hicolor display this is a no-op */
|
||||
if (picture->format->palette) {
|
||||
ncolors = picture->format->palette->ncolors;
|
||||
colors = (SDL_Color *) malloc(ncolors * sizeof(SDL_Color));
|
||||
cmap = (SDL_Color *) malloc(ncolors * sizeof(SDL_Color));
|
||||
memcpy(colors, picture->format->palette->colors,
|
||||
ncolors * sizeof(SDL_Color));
|
||||
} else {
|
||||
int r, g, b;
|
||||
|
||||
/* Allocate 256 color palette */
|
||||
ncolors = 256;
|
||||
colors = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
|
||||
cmap = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
|
||||
/* Allocate 256 color palette */
|
||||
ncolors = 256;
|
||||
colors = (SDL_Color *) malloc(ncolors * sizeof(SDL_Color));
|
||||
cmap = (SDL_Color *) malloc(ncolors * sizeof(SDL_Color));
|
||||
|
||||
/* Set a 3,3,2 color cube */
|
||||
for ( r=0; r<8; ++r ) {
|
||||
for ( g=0; g<8; ++g ) {
|
||||
for ( b=0; b<4; ++b ) {
|
||||
i = ((r<<5)|(g<<2)|b);
|
||||
colors[i].r = r<<5;
|
||||
colors[i].g = g<<5;
|
||||
colors[i].b = b<<6;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
NOTICE("testwin: setting colors\n");
|
||||
if ( ! SDL_SetColors(screen, colors, 0, ncolors) &&
|
||||
(screen->format->palette != NULL) ) {
|
||||
fprintf(stderr,
|
||||
"Warning: Couldn't set all of the colors, but SDL will map the image\n"
|
||||
" (colormap fading will suffer - try the -warp option)\n"
|
||||
);
|
||||
}
|
||||
/* Set a 3,3,2 color cube */
|
||||
for (r = 0; r < 8; ++r) {
|
||||
for (g = 0; g < 8; ++g) {
|
||||
for (b = 0; b < 4; ++b) {
|
||||
i = ((r << 5) | (g << 2) | b);
|
||||
colors[i].r = r << 5;
|
||||
colors[i].g = g << 5;
|
||||
colors[i].b = b << 6;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
NOTICE("testwin: setting colors\n");
|
||||
if (!SDL_SetColors(screen, colors, 0, ncolors) &&
|
||||
(screen->format->palette != NULL)) {
|
||||
fprintf(stderr,
|
||||
"Warning: Couldn't set all of the colors, but SDL will map the image\n"
|
||||
" (colormap fading will suffer - try the -warp option)\n");
|
||||
}
|
||||
|
||||
/* Set the screen to black (not really necessary) */
|
||||
if ( SDL_LockSurface(screen) == 0 ) {
|
||||
Uint32 black;
|
||||
Uint8 *pixels;
|
||||
/* Set the screen to black (not really necessary) */
|
||||
if (SDL_LockSurface(screen) == 0) {
|
||||
Uint32 black;
|
||||
Uint8 *pixels;
|
||||
|
||||
black = SDL_MapRGB(screen->format, 0, 0, 0);
|
||||
pixels = (Uint8 *)screen->pixels;
|
||||
for ( i=0; i<screen->h; ++i ) {
|
||||
memset(pixels, black,
|
||||
screen->w*screen->format->BytesPerPixel);
|
||||
pixels += screen->pitch;
|
||||
}
|
||||
SDL_UnlockSurface(screen);
|
||||
SDL_UpdateRect(screen, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
/* Display the picture */
|
||||
if ( speedy ) {
|
||||
SDL_Surface *displayfmt;
|
||||
black = SDL_MapRGB(screen->format, 0, 0, 0);
|
||||
pixels = (Uint8 *) screen->pixels;
|
||||
for (i = 0; i < screen->h; ++i) {
|
||||
memset(pixels, black, screen->w * screen->format->BytesPerPixel);
|
||||
pixels += screen->pitch;
|
||||
}
|
||||
SDL_UnlockSurface(screen);
|
||||
SDL_UpdateRect(screen, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
fprintf(stderr, "Converting picture\n");
|
||||
displayfmt = SDL_DisplayFormat(picture);
|
||||
if ( displayfmt == NULL ) {
|
||||
fprintf(stderr,
|
||||
"Couldn't convert image: %s\n", SDL_GetError());
|
||||
goto done;
|
||||
}
|
||||
SDL_FreeSurface(picture);
|
||||
picture = displayfmt;
|
||||
}
|
||||
printf("(image surface located in %s memory)\n",
|
||||
(picture->flags&SDL_HWSURFACE) ? "video" : "system");
|
||||
centered = (screen->w - picture->w)/2;
|
||||
if ( centered < 0 ) {
|
||||
centered = 0;
|
||||
}
|
||||
dest.y = (screen->h - picture->h)/2;
|
||||
dest.w = picture->w;
|
||||
dest.h = picture->h;
|
||||
NOTICE("testwin: moving image\n");
|
||||
for ( i=0; i<=centered; ++i ) {
|
||||
dest.x = i;
|
||||
update = dest;
|
||||
if ( SDL_BlitSurface(picture, NULL, screen, &update) < 0 ) {
|
||||
fprintf(stderr, "Blit failed: %s\n", SDL_GetError());
|
||||
break;
|
||||
}
|
||||
if ( flip ) {
|
||||
SDL_Flip(screen);
|
||||
} else {
|
||||
SDL_UpdateRects(screen, 1, &update);
|
||||
}
|
||||
}
|
||||
/* Display the picture */
|
||||
if (speedy) {
|
||||
SDL_Surface *displayfmt;
|
||||
|
||||
fprintf(stderr, "Converting picture\n");
|
||||
displayfmt = SDL_DisplayFormat(picture);
|
||||
if (displayfmt == NULL) {
|
||||
fprintf(stderr, "Couldn't convert image: %s\n", SDL_GetError());
|
||||
goto done;
|
||||
}
|
||||
SDL_FreeSurface(picture);
|
||||
picture = displayfmt;
|
||||
}
|
||||
printf("(image surface located in %s memory)\n",
|
||||
(picture->flags & SDL_HWSURFACE) ? "video" : "system");
|
||||
centered = (screen->w - picture->w) / 2;
|
||||
if (centered < 0) {
|
||||
centered = 0;
|
||||
}
|
||||
dest.y = (screen->h - picture->h) / 2;
|
||||
dest.w = picture->w;
|
||||
dest.h = picture->h;
|
||||
NOTICE("testwin: moving image\n");
|
||||
for (i = 0; i <= centered; ++i) {
|
||||
dest.x = i;
|
||||
update = dest;
|
||||
if (SDL_BlitSurface(picture, NULL, screen, &update) < 0) {
|
||||
fprintf(stderr, "Blit failed: %s\n", SDL_GetError());
|
||||
break;
|
||||
}
|
||||
if (flip) {
|
||||
SDL_Flip(screen);
|
||||
} else {
|
||||
SDL_UpdateRects(screen, 1, &update);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef SCREENSHOT
|
||||
if ( SDL_SaveBMP(screen, "screen.bmp") < 0 )
|
||||
printf("Couldn't save screen: %s\n", SDL_GetError());
|
||||
if (SDL_SaveBMP(screen, "screen.bmp") < 0)
|
||||
printf("Couldn't save screen: %s\n", SDL_GetError());
|
||||
#endif
|
||||
|
||||
#ifndef BENCHMARK_SDL
|
||||
/* Let it sit there for a while */
|
||||
SDL_Delay(5*1000);
|
||||
/* Let it sit there for a while */
|
||||
SDL_Delay(5 * 1000);
|
||||
#endif
|
||||
/* Fade the colormap */
|
||||
if ( ! nofade ) {
|
||||
int maxstep;
|
||||
SDL_Color final;
|
||||
SDL_Color palcolors[256];
|
||||
struct {
|
||||
Sint16 r, g, b;
|
||||
} cdist[256];
|
||||
/* Fade the colormap */
|
||||
if (!nofade) {
|
||||
int maxstep;
|
||||
SDL_Color final;
|
||||
SDL_Color palcolors[256];
|
||||
struct
|
||||
{
|
||||
Sint16 r, g, b;
|
||||
} cdist[256];
|
||||
|
||||
NOTICE("testwin: fading out...\n");
|
||||
memcpy(cmap, colors, ncolors*sizeof(SDL_Color));
|
||||
maxstep = 32-1;
|
||||
final.r = 0xFF;
|
||||
final.g = 0x00;
|
||||
final.b = 0x00;
|
||||
memcpy(palcolors, colors, ncolors*sizeof(SDL_Color));
|
||||
for ( i=0; i<ncolors; ++i ) {
|
||||
cdist[i].r = final.r-palcolors[i].r;
|
||||
cdist[i].g = final.g-palcolors[i].g;
|
||||
cdist[i].b = final.b-palcolors[i].b;
|
||||
}
|
||||
for ( i=0; i<=maxstep/2; ++i ) { /* halfway fade */
|
||||
int c;
|
||||
for ( c=0; c<ncolors; ++c ) {
|
||||
colors[c].r =
|
||||
palcolors[c].r+((cdist[c].r*i))/maxstep;
|
||||
colors[c].g =
|
||||
palcolors[c].g+((cdist[c].g*i))/maxstep;
|
||||
colors[c].b =
|
||||
palcolors[c].b+((cdist[c].b*i))/maxstep;
|
||||
}
|
||||
SDL_SetColors(screen, colors, 0, ncolors);
|
||||
SDL_Delay(1);
|
||||
}
|
||||
final.r = 0x00;
|
||||
final.g = 0x00;
|
||||
final.b = 0x00;
|
||||
memcpy(palcolors, colors, ncolors*sizeof(SDL_Color));
|
||||
for ( i=0; i<ncolors; ++i ) {
|
||||
cdist[i].r = final.r-palcolors[i].r;
|
||||
cdist[i].g = final.g-palcolors[i].g;
|
||||
cdist[i].b = final.b-palcolors[i].b;
|
||||
}
|
||||
maxstep /= 2;
|
||||
for ( i=0; i<=maxstep; ++i ) { /* finish fade out */
|
||||
int c;
|
||||
for ( c=0; c<ncolors; ++c ) {
|
||||
colors[c].r =
|
||||
palcolors[c].r+((cdist[c].r*i))/maxstep;
|
||||
colors[c].g =
|
||||
palcolors[c].g+((cdist[c].g*i))/maxstep;
|
||||
colors[c].b =
|
||||
palcolors[c].b+((cdist[c].b*i))/maxstep;
|
||||
}
|
||||
SDL_SetColors(screen, colors, 0, ncolors);
|
||||
SDL_Delay(1);
|
||||
}
|
||||
for ( i=0; i<ncolors; ++i ) {
|
||||
colors[i].r = final.r;
|
||||
colors[i].g = final.g;
|
||||
colors[i].b = final.b;
|
||||
}
|
||||
SDL_SetColors(screen, colors, 0, ncolors);
|
||||
NOTICE("testwin: fading in...\n");
|
||||
memcpy(palcolors, colors, ncolors*sizeof(SDL_Color));
|
||||
for ( i=0; i<ncolors; ++i ) {
|
||||
cdist[i].r = cmap[i].r-palcolors[i].r;
|
||||
cdist[i].g = cmap[i].g-palcolors[i].g;
|
||||
cdist[i].b = cmap[i].b-palcolors[i].b;
|
||||
}
|
||||
for ( i=0; i<=maxstep; ++i ) { /* 32 step fade in */
|
||||
int c;
|
||||
for ( c=0; c<ncolors; ++c ) {
|
||||
colors[c].r =
|
||||
palcolors[c].r+((cdist[c].r*i))/maxstep;
|
||||
colors[c].g =
|
||||
palcolors[c].g+((cdist[c].g*i))/maxstep;
|
||||
colors[c].b =
|
||||
palcolors[c].b+((cdist[c].b*i))/maxstep;
|
||||
}
|
||||
SDL_SetColors(screen, colors, 0, ncolors);
|
||||
SDL_Delay(1);
|
||||
}
|
||||
NOTICE("testwin: fading over\n");
|
||||
}
|
||||
|
||||
done:
|
||||
/* Free the picture and return */
|
||||
SDL_FreeSurface(picture);
|
||||
free(colors); free(cmap);
|
||||
return;
|
||||
NOTICE("testwin: fading out...\n");
|
||||
memcpy(cmap, colors, ncolors * sizeof(SDL_Color));
|
||||
maxstep = 32 - 1;
|
||||
final.r = 0xFF;
|
||||
final.g = 0x00;
|
||||
final.b = 0x00;
|
||||
memcpy(palcolors, colors, ncolors * sizeof(SDL_Color));
|
||||
for (i = 0; i < ncolors; ++i) {
|
||||
cdist[i].r = final.r - palcolors[i].r;
|
||||
cdist[i].g = final.g - palcolors[i].g;
|
||||
cdist[i].b = final.b - palcolors[i].b;
|
||||
}
|
||||
for (i = 0; i <= maxstep / 2; ++i) { /* halfway fade */
|
||||
int c;
|
||||
for (c = 0; c < ncolors; ++c) {
|
||||
colors[c].r = palcolors[c].r + ((cdist[c].r * i)) / maxstep;
|
||||
colors[c].g = palcolors[c].g + ((cdist[c].g * i)) / maxstep;
|
||||
colors[c].b = palcolors[c].b + ((cdist[c].b * i)) / maxstep;
|
||||
}
|
||||
SDL_SetColors(screen, colors, 0, ncolors);
|
||||
SDL_Delay(1);
|
||||
}
|
||||
final.r = 0x00;
|
||||
final.g = 0x00;
|
||||
final.b = 0x00;
|
||||
memcpy(palcolors, colors, ncolors * sizeof(SDL_Color));
|
||||
for (i = 0; i < ncolors; ++i) {
|
||||
cdist[i].r = final.r - palcolors[i].r;
|
||||
cdist[i].g = final.g - palcolors[i].g;
|
||||
cdist[i].b = final.b - palcolors[i].b;
|
||||
}
|
||||
maxstep /= 2;
|
||||
for (i = 0; i <= maxstep; ++i) { /* finish fade out */
|
||||
int c;
|
||||
for (c = 0; c < ncolors; ++c) {
|
||||
colors[c].r = palcolors[c].r + ((cdist[c].r * i)) / maxstep;
|
||||
colors[c].g = palcolors[c].g + ((cdist[c].g * i)) / maxstep;
|
||||
colors[c].b = palcolors[c].b + ((cdist[c].b * i)) / maxstep;
|
||||
}
|
||||
SDL_SetColors(screen, colors, 0, ncolors);
|
||||
SDL_Delay(1);
|
||||
}
|
||||
for (i = 0; i < ncolors; ++i) {
|
||||
colors[i].r = final.r;
|
||||
colors[i].g = final.g;
|
||||
colors[i].b = final.b;
|
||||
}
|
||||
SDL_SetColors(screen, colors, 0, ncolors);
|
||||
NOTICE("testwin: fading in...\n");
|
||||
memcpy(palcolors, colors, ncolors * sizeof(SDL_Color));
|
||||
for (i = 0; i < ncolors; ++i) {
|
||||
cdist[i].r = cmap[i].r - palcolors[i].r;
|
||||
cdist[i].g = cmap[i].g - palcolors[i].g;
|
||||
cdist[i].b = cmap[i].b - palcolors[i].b;
|
||||
}
|
||||
for (i = 0; i <= maxstep; ++i) { /* 32 step fade in */
|
||||
int c;
|
||||
for (c = 0; c < ncolors; ++c) {
|
||||
colors[c].r = palcolors[c].r + ((cdist[c].r * i)) / maxstep;
|
||||
colors[c].g = palcolors[c].g + ((cdist[c].g * i)) / maxstep;
|
||||
colors[c].b = palcolors[c].b + ((cdist[c].b * i)) / maxstep;
|
||||
}
|
||||
SDL_SetColors(screen, colors, 0, ncolors);
|
||||
SDL_Delay(1);
|
||||
}
|
||||
NOTICE("testwin: fading over\n");
|
||||
}
|
||||
|
||||
done:
|
||||
/* Free the picture and return */
|
||||
SDL_FreeSurface(picture);
|
||||
free(colors);
|
||||
free(cmap);
|
||||
return;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
SDL_Surface *screen;
|
||||
/* Options */
|
||||
int speedy, flip, nofade;
|
||||
int delay;
|
||||
int w, h;
|
||||
int desired_bpp;
|
||||
Uint32 video_flags;
|
||||
SDL_Surface *screen;
|
||||
/* Options */
|
||||
int speedy, flip, nofade;
|
||||
int delay;
|
||||
int w, h;
|
||||
int desired_bpp;
|
||||
Uint32 video_flags;
|
||||
#ifdef BENCHMARK_SDL
|
||||
Uint32 then, now;
|
||||
Uint32 then, now;
|
||||
#endif
|
||||
/* Set default options and check command-line */
|
||||
speedy = 0;
|
||||
flip = 0;
|
||||
nofade = 0;
|
||||
delay = 1;
|
||||
/* Set default options and check command-line */
|
||||
speedy = 0;
|
||||
flip = 0;
|
||||
nofade = 0;
|
||||
delay = 1;
|
||||
|
||||
#ifdef _WIN32_WCE
|
||||
w = 240;
|
||||
h = 320;
|
||||
desired_bpp = 8;
|
||||
video_flags = SDL_FULLSCREEN;
|
||||
w = 240;
|
||||
h = 320;
|
||||
desired_bpp = 8;
|
||||
video_flags = SDL_FULLSCREEN;
|
||||
#else
|
||||
w = 640;
|
||||
h = 480;
|
||||
desired_bpp = 0;
|
||||
video_flags = 0;
|
||||
w = 640;
|
||||
h = 480;
|
||||
desired_bpp = 0;
|
||||
video_flags = 0;
|
||||
#endif
|
||||
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
|
||||
fprintf(stderr,
|
||||
"Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return(1);
|
||||
}
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return (1);
|
||||
}
|
||||
|
||||
while ( argc > 1 ) {
|
||||
if ( strcmp(argv[1], "-speedy") == 0 ) {
|
||||
speedy = 1;
|
||||
argv += 1;
|
||||
argc -= 1;
|
||||
} else
|
||||
if ( strcmp(argv[1], "-nofade") == 0 ) {
|
||||
nofade = 1;
|
||||
argv += 1;
|
||||
argc -= 1;
|
||||
} else
|
||||
if ( strcmp(argv[1], "-delay") == 0 ) {
|
||||
if ( argv[2] ) {
|
||||
delay = atoi(argv[2]);
|
||||
argv += 2;
|
||||
argc -= 2;
|
||||
} else {
|
||||
fprintf(stderr,
|
||||
"The -delay option requires an argument\n");
|
||||
quit(1);
|
||||
}
|
||||
} else
|
||||
if ( strcmp(argv[1], "-width") == 0 ) {
|
||||
if ( argv[2] && ((w = atoi(argv[2])) > 0) ) {
|
||||
argv += 2;
|
||||
argc -= 2;
|
||||
} else {
|
||||
fprintf(stderr,
|
||||
"The -width option requires an argument\n");
|
||||
quit(1);
|
||||
}
|
||||
} else
|
||||
if ( strcmp(argv[1], "-height") == 0 ) {
|
||||
if ( argv[2] && ((h = atoi(argv[2])) > 0) ) {
|
||||
argv += 2;
|
||||
argc -= 2;
|
||||
} else {
|
||||
fprintf(stderr,
|
||||
"The -height option requires an argument\n");
|
||||
quit(1);
|
||||
}
|
||||
} else
|
||||
if ( strcmp(argv[1], "-bpp") == 0 ) {
|
||||
if ( argv[2] ) {
|
||||
desired_bpp = atoi(argv[2]);
|
||||
argv += 2;
|
||||
argc -= 2;
|
||||
} else {
|
||||
fprintf(stderr,
|
||||
"The -bpp option requires an argument\n");
|
||||
quit(1);
|
||||
}
|
||||
} else
|
||||
if ( strcmp(argv[1], "-warp") == 0 ) {
|
||||
video_flags |= SDL_HWPALETTE;
|
||||
argv += 1;
|
||||
argc -= 1;
|
||||
} else
|
||||
if ( strcmp(argv[1], "-hw") == 0 ) {
|
||||
video_flags |= SDL_HWSURFACE;
|
||||
argv += 1;
|
||||
argc -= 1;
|
||||
} else
|
||||
if ( strcmp(argv[1], "-flip") == 0 ) {
|
||||
video_flags |= SDL_DOUBLEBUF;
|
||||
argv += 1;
|
||||
argc -= 1;
|
||||
} else
|
||||
if ( strcmp(argv[1], "-fullscreen") == 0 ) {
|
||||
video_flags |= SDL_FULLSCREEN;
|
||||
argv += 1;
|
||||
argc -= 1;
|
||||
} else
|
||||
break;
|
||||
}
|
||||
while (argc > 1) {
|
||||
if (strcmp(argv[1], "-speedy") == 0) {
|
||||
speedy = 1;
|
||||
argv += 1;
|
||||
argc -= 1;
|
||||
} else if (strcmp(argv[1], "-nofade") == 0) {
|
||||
nofade = 1;
|
||||
argv += 1;
|
||||
argc -= 1;
|
||||
} else if (strcmp(argv[1], "-delay") == 0) {
|
||||
if (argv[2]) {
|
||||
delay = atoi(argv[2]);
|
||||
argv += 2;
|
||||
argc -= 2;
|
||||
} else {
|
||||
fprintf(stderr, "The -delay option requires an argument\n");
|
||||
quit(1);
|
||||
}
|
||||
} else if (strcmp(argv[1], "-width") == 0) {
|
||||
if (argv[2] && ((w = atoi(argv[2])) > 0)) {
|
||||
argv += 2;
|
||||
argc -= 2;
|
||||
} else {
|
||||
fprintf(stderr, "The -width option requires an argument\n");
|
||||
quit(1);
|
||||
}
|
||||
} else if (strcmp(argv[1], "-height") == 0) {
|
||||
if (argv[2] && ((h = atoi(argv[2])) > 0)) {
|
||||
argv += 2;
|
||||
argc -= 2;
|
||||
} else {
|
||||
fprintf(stderr, "The -height option requires an argument\n");
|
||||
quit(1);
|
||||
}
|
||||
} else if (strcmp(argv[1], "-bpp") == 0) {
|
||||
if (argv[2]) {
|
||||
desired_bpp = atoi(argv[2]);
|
||||
argv += 2;
|
||||
argc -= 2;
|
||||
} else {
|
||||
fprintf(stderr, "The -bpp option requires an argument\n");
|
||||
quit(1);
|
||||
}
|
||||
} else if (strcmp(argv[1], "-warp") == 0) {
|
||||
video_flags |= SDL_HWPALETTE;
|
||||
argv += 1;
|
||||
argc -= 1;
|
||||
} else if (strcmp(argv[1], "-hw") == 0) {
|
||||
video_flags |= SDL_HWSURFACE;
|
||||
argv += 1;
|
||||
argc -= 1;
|
||||
} else if (strcmp(argv[1], "-flip") == 0) {
|
||||
video_flags |= SDL_DOUBLEBUF;
|
||||
argv += 1;
|
||||
argc -= 1;
|
||||
} else if (strcmp(argv[1], "-fullscreen") == 0) {
|
||||
video_flags |= SDL_FULLSCREEN;
|
||||
argv += 1;
|
||||
argc -= 1;
|
||||
} else
|
||||
break;
|
||||
}
|
||||
|
||||
/* Initialize the display */
|
||||
screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
|
||||
if ( screen == NULL ) {
|
||||
fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
|
||||
w, h, desired_bpp, SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
printf("Set%s %dx%dx%d mode\n",
|
||||
screen->flags & SDL_FULLSCREEN ? " fullscreen" : "",
|
||||
screen->w, screen->h, screen->format->BitsPerPixel);
|
||||
printf("(video surface located in %s memory)\n",
|
||||
(screen->flags&SDL_HWSURFACE) ? "video" : "system");
|
||||
if ( screen->flags & SDL_DOUBLEBUF ) {
|
||||
printf("Double-buffering enabled\n");
|
||||
flip = 1;
|
||||
}
|
||||
/* Initialize the display */
|
||||
screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
|
||||
if (screen == NULL) {
|
||||
fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
|
||||
w, h, desired_bpp, SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
printf("Set%s %dx%dx%d mode\n",
|
||||
screen->flags & SDL_FULLSCREEN ? " fullscreen" : "",
|
||||
screen->w, screen->h, screen->format->BitsPerPixel);
|
||||
printf("(video surface located in %s memory)\n",
|
||||
(screen->flags & SDL_HWSURFACE) ? "video" : "system");
|
||||
if (screen->flags & SDL_DOUBLEBUF) {
|
||||
printf("Double-buffering enabled\n");
|
||||
flip = 1;
|
||||
}
|
||||
|
||||
/* Set the window manager title bar */
|
||||
SDL_WM_SetCaption("SDL test window", "testwin");
|
||||
/* Set the window manager title bar */
|
||||
SDL_WM_SetCaption("SDL test window", "testwin");
|
||||
|
||||
/* Do all the drawing work */
|
||||
/* Do all the drawing work */
|
||||
#ifdef BENCHMARK_SDL
|
||||
then = SDL_GetTicks();
|
||||
DrawPict(screen, argv[1], speedy, flip, nofade);
|
||||
now = SDL_GetTicks();
|
||||
printf("Time: %d milliseconds\n", now-then);
|
||||
then = SDL_GetTicks();
|
||||
DrawPict(screen, argv[1], speedy, flip, nofade);
|
||||
now = SDL_GetTicks();
|
||||
printf("Time: %d milliseconds\n", now - then);
|
||||
#else
|
||||
DrawPict(screen, argv[1], speedy, flip, nofade);
|
||||
DrawPict(screen, argv[1], speedy, flip, nofade);
|
||||
#endif
|
||||
SDL_Delay(delay*1000);
|
||||
SDL_Quit();
|
||||
return(0);
|
||||
SDL_Delay(delay * 1000);
|
||||
SDL_Quit();
|
||||
return (0);
|
||||
}
|
||||
|
||||
607
test/testwm.c
607
test/testwm.c
@@ -10,367 +10,374 @@
|
||||
/* Is the cursor visible? */
|
||||
static int visible = 1;
|
||||
|
||||
static Uint8 video_bpp;
|
||||
static Uint8 video_bpp;
|
||||
static Uint32 video_flags;
|
||||
|
||||
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
||||
static void quit(int rc)
|
||||
static void
|
||||
quit(int rc)
|
||||
{
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
}
|
||||
|
||||
int SetVideoMode(int w, int h)
|
||||
int
|
||||
SetVideoMode(int w, int h)
|
||||
{
|
||||
SDL_Surface *screen;
|
||||
int i;
|
||||
Uint8 *buffer;
|
||||
SDL_Color palette[256];
|
||||
SDL_Surface *screen;
|
||||
int i;
|
||||
Uint8 *buffer;
|
||||
SDL_Color palette[256];
|
||||
|
||||
screen = SDL_SetVideoMode(w, h, video_bpp, video_flags);
|
||||
if ( screen == NULL ) {
|
||||
fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
|
||||
w, h, video_bpp, SDL_GetError());
|
||||
return(-1);
|
||||
}
|
||||
printf("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ?
|
||||
"fullscreen" : "windowed");
|
||||
screen = SDL_SetVideoMode(w, h, video_bpp, video_flags);
|
||||
if (screen == NULL) {
|
||||
fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
|
||||
w, h, video_bpp, SDL_GetError());
|
||||
return (-1);
|
||||
}
|
||||
printf("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ?
|
||||
"fullscreen" : "windowed");
|
||||
|
||||
/* Set the surface pixels and refresh! */
|
||||
for ( i=0; i<256; ++i ) {
|
||||
palette[i].r = 255-i;
|
||||
palette[i].g = 255-i;
|
||||
palette[i].b = 255-i;
|
||||
}
|
||||
SDL_SetColors(screen, palette, 0, 256);
|
||||
if ( SDL_LockSurface(screen) < 0 ) {
|
||||
fprintf(stderr, "Couldn't lock display surface: %s\n",
|
||||
SDL_GetError());
|
||||
return(-1);
|
||||
}
|
||||
buffer = (Uint8 *)screen->pixels;
|
||||
for ( i=0; i<screen->h; ++i ) {
|
||||
memset(buffer,(i*255)/screen->h,
|
||||
screen->w*screen->format->BytesPerPixel);
|
||||
buffer += screen->pitch;
|
||||
}
|
||||
SDL_UnlockSurface(screen);
|
||||
SDL_UpdateRect(screen, 0, 0, 0, 0);
|
||||
/* Set the surface pixels and refresh! */
|
||||
for (i = 0; i < 256; ++i) {
|
||||
palette[i].r = 255 - i;
|
||||
palette[i].g = 255 - i;
|
||||
palette[i].b = 255 - i;
|
||||
}
|
||||
SDL_SetColors(screen, palette, 0, 256);
|
||||
if (SDL_LockSurface(screen) < 0) {
|
||||
fprintf(stderr, "Couldn't lock display surface: %s\n",
|
||||
SDL_GetError());
|
||||
return (-1);
|
||||
}
|
||||
buffer = (Uint8 *) screen->pixels;
|
||||
for (i = 0; i < screen->h; ++i) {
|
||||
memset(buffer, (i * 255) / screen->h,
|
||||
screen->w * screen->format->BytesPerPixel);
|
||||
buffer += screen->pitch;
|
||||
}
|
||||
SDL_UnlockSurface(screen);
|
||||
SDL_UpdateRect(screen, 0, 0, 0, 0);
|
||||
|
||||
return(0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
SDL_Surface *LoadIconSurface(char *file, Uint8 **maskp)
|
||||
SDL_Surface *
|
||||
LoadIconSurface(char *file, Uint8 ** maskp)
|
||||
{
|
||||
SDL_Surface *icon;
|
||||
Uint8 *pixels;
|
||||
Uint8 *mask;
|
||||
int mlen, i, j;
|
||||
SDL_Surface *icon;
|
||||
Uint8 *pixels;
|
||||
Uint8 *mask;
|
||||
int mlen, i, j;
|
||||
|
||||
*maskp = NULL;
|
||||
*maskp = NULL;
|
||||
|
||||
/* Load the icon surface */
|
||||
icon = SDL_LoadBMP(file);
|
||||
if ( icon == NULL ) {
|
||||
fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
|
||||
return(NULL);
|
||||
}
|
||||
/* Load the icon surface */
|
||||
icon = SDL_LoadBMP(file);
|
||||
if (icon == NULL) {
|
||||
fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/* Check width and height
|
||||
if ( (icon->w%8) != 0 ) {
|
||||
fprintf(stderr, "Icon width must be a multiple of 8!\n");
|
||||
SDL_FreeSurface(icon);
|
||||
return(NULL);
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
if ( icon->format->palette == NULL ) {
|
||||
fprintf(stderr, "Icon must have a palette!\n");
|
||||
SDL_FreeSurface(icon);
|
||||
return(NULL);
|
||||
}
|
||||
/* Check width and height
|
||||
if ( (icon->w%8) != 0 ) {
|
||||
fprintf(stderr, "Icon width must be a multiple of 8!\n");
|
||||
SDL_FreeSurface(icon);
|
||||
return(NULL);
|
||||
}
|
||||
*/
|
||||
|
||||
/* Set the colorkey */
|
||||
SDL_SetColorKey(icon, SDL_SRCCOLORKEY, *((Uint8 *)icon->pixels));
|
||||
|
||||
/* Create the mask */
|
||||
pixels = (Uint8 *)icon->pixels;
|
||||
printf("Transparent pixel: (%d,%d,%d)\n",
|
||||
icon->format->palette->colors[*pixels].r,
|
||||
icon->format->palette->colors[*pixels].g,
|
||||
icon->format->palette->colors[*pixels].b);
|
||||
mlen = (icon->w*icon->h + 7) / 8;
|
||||
mask = (Uint8 *)malloc(mlen);
|
||||
if ( mask == NULL ) {
|
||||
fprintf(stderr, "Out of memory!\n");
|
||||
SDL_FreeSurface(icon);
|
||||
return(NULL);
|
||||
}
|
||||
memset(mask, 0, mlen);
|
||||
for ( i=0; i < icon->h; i++)
|
||||
for (j=0; j < icon->w; j++) {
|
||||
if (icon->format->palette == NULL) {
|
||||
fprintf(stderr, "Icon must have a palette!\n");
|
||||
SDL_FreeSurface(icon);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/* Set the colorkey */
|
||||
SDL_SetColorKey(icon, SDL_SRCCOLORKEY, *((Uint8 *) icon->pixels));
|
||||
|
||||
/* Create the mask */
|
||||
pixels = (Uint8 *) icon->pixels;
|
||||
printf("Transparent pixel: (%d,%d,%d)\n",
|
||||
icon->format->palette->colors[*pixels].r,
|
||||
icon->format->palette->colors[*pixels].g,
|
||||
icon->format->palette->colors[*pixels].b);
|
||||
mlen = (icon->w * icon->h + 7) / 8;
|
||||
mask = (Uint8 *) malloc(mlen);
|
||||
if (mask == NULL) {
|
||||
fprintf(stderr, "Out of memory!\n");
|
||||
SDL_FreeSurface(icon);
|
||||
return (NULL);
|
||||
}
|
||||
memset(mask, 0, mlen);
|
||||
for (i = 0; i < icon->h; i++)
|
||||
for (j = 0; j < icon->w; j++) {
|
||||
int pindex = i * icon->pitch + j;
|
||||
int mindex = i * icon->w + j;
|
||||
if ( pixels[pindex] != *pixels )
|
||||
mask[mindex>>3] |= 1 << (7 - (mindex & 7));
|
||||
if (pixels[pindex] != *pixels)
|
||||
mask[mindex >> 3] |= 1 << (7 - (mindex & 7));
|
||||
}
|
||||
*maskp = mask;
|
||||
return(icon);
|
||||
*maskp = mask;
|
||||
return (icon);
|
||||
}
|
||||
|
||||
void HotKey_ToggleFullScreen(void)
|
||||
void
|
||||
HotKey_ToggleFullScreen(void)
|
||||
{
|
||||
SDL_Surface *screen;
|
||||
SDL_Surface *screen;
|
||||
|
||||
screen = SDL_GetVideoSurface();
|
||||
if ( SDL_WM_ToggleFullScreen(screen) ) {
|
||||
printf("Toggled fullscreen mode - now %s\n",
|
||||
(screen->flags&SDL_FULLSCREEN) ? "fullscreen" : "windowed");
|
||||
} else {
|
||||
printf("Unable to toggle fullscreen mode\n");
|
||||
video_flags ^= SDL_FULLSCREEN;
|
||||
SetVideoMode(screen->w, screen->h);
|
||||
}
|
||||
screen = SDL_GetVideoSurface();
|
||||
if (SDL_WM_ToggleFullScreen(screen)) {
|
||||
printf("Toggled fullscreen mode - now %s\n",
|
||||
(screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
|
||||
} else {
|
||||
printf("Unable to toggle fullscreen mode\n");
|
||||
video_flags ^= SDL_FULLSCREEN;
|
||||
SetVideoMode(screen->w, screen->h);
|
||||
}
|
||||
}
|
||||
|
||||
void HotKey_ToggleGrab(void)
|
||||
void
|
||||
HotKey_ToggleGrab(void)
|
||||
{
|
||||
SDL_GrabMode mode;
|
||||
SDL_GrabMode mode;
|
||||
|
||||
printf("Ctrl-G: toggling input grab!\n");
|
||||
mode = SDL_WM_GrabInput(SDL_GRAB_QUERY);
|
||||
if ( mode == SDL_GRAB_ON ) {
|
||||
printf("Grab was on\n");
|
||||
} else {
|
||||
printf("Grab was off\n");
|
||||
}
|
||||
mode = SDL_WM_GrabInput(mode ? SDL_GRAB_OFF : SDL_GRAB_ON);
|
||||
if ( mode == SDL_GRAB_ON ) {
|
||||
printf("Grab is now on\n");
|
||||
} else {
|
||||
printf("Grab is now off\n");
|
||||
}
|
||||
printf("Ctrl-G: toggling input grab!\n");
|
||||
mode = SDL_WM_GrabInput(SDL_GRAB_QUERY);
|
||||
if (mode == SDL_GRAB_ON) {
|
||||
printf("Grab was on\n");
|
||||
} else {
|
||||
printf("Grab was off\n");
|
||||
}
|
||||
mode = SDL_WM_GrabInput(mode ? SDL_GRAB_OFF : SDL_GRAB_ON);
|
||||
if (mode == SDL_GRAB_ON) {
|
||||
printf("Grab is now on\n");
|
||||
} else {
|
||||
printf("Grab is now off\n");
|
||||
}
|
||||
}
|
||||
|
||||
void HotKey_Iconify(void)
|
||||
void
|
||||
HotKey_Iconify(void)
|
||||
{
|
||||
printf("Ctrl-Z: iconifying window!\n");
|
||||
SDL_WM_IconifyWindow();
|
||||
printf("Ctrl-Z: iconifying window!\n");
|
||||
SDL_WM_IconifyWindow();
|
||||
}
|
||||
|
||||
void HotKey_Quit(void)
|
||||
void
|
||||
HotKey_Quit(void)
|
||||
{
|
||||
SDL_Event event;
|
||||
SDL_Event event;
|
||||
|
||||
printf("Posting internal quit request\n");
|
||||
event.type = SDL_USEREVENT;
|
||||
SDL_PushEvent(&event);
|
||||
printf("Posting internal quit request\n");
|
||||
event.type = SDL_USEREVENT;
|
||||
SDL_PushEvent(&event);
|
||||
}
|
||||
|
||||
int SDLCALL FilterEvents(const SDL_Event *event)
|
||||
static int SDLCALL(*old_filterfunc) (void *, SDL_Event *);
|
||||
static void *old_filterdata;
|
||||
|
||||
int SDLCALL
|
||||
FilterEvents(void *userdata, SDL_Event * event)
|
||||
{
|
||||
static int reallyquit = 0;
|
||||
static int reallyquit = 0;
|
||||
|
||||
switch (event->type) {
|
||||
if (old_filterfunc) {
|
||||
old_filterfunc(old_filterdata, event);
|
||||
}
|
||||
|
||||
case SDL_ACTIVEEVENT:
|
||||
/* See what happened */
|
||||
printf("App %s ",
|
||||
event->active.gain ? "gained" : "lost");
|
||||
if ( event->active.state & SDL_APPACTIVE )
|
||||
printf("active ");
|
||||
if ( event->active.state & SDL_APPINPUTFOCUS )
|
||||
printf("input ");
|
||||
if ( event->active.state & SDL_APPMOUSEFOCUS )
|
||||
printf("mouse ");
|
||||
printf("focus\n");
|
||||
switch (event->type) {
|
||||
|
||||
/* See if we are iconified or restored */
|
||||
if ( event->active.state & SDL_APPACTIVE ) {
|
||||
printf("App has been %s\n",
|
||||
event->active.gain ?
|
||||
"restored" : "iconified");
|
||||
}
|
||||
return(0);
|
||||
case SDL_ACTIVEEVENT:
|
||||
/* See what happened */
|
||||
printf("App %s ", event->active.gain ? "gained" : "lost");
|
||||
if (event->active.state & SDL_APPACTIVE)
|
||||
printf("active ");
|
||||
if (event->active.state & SDL_APPINPUTFOCUS)
|
||||
printf("input ");
|
||||
if (event->active.state & SDL_APPMOUSEFOCUS)
|
||||
printf("mouse ");
|
||||
printf("focus\n");
|
||||
|
||||
/* We want to toggle visibility on buttonpress */
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
if ( event->button.state == SDL_PRESSED ) {
|
||||
visible = !visible;
|
||||
SDL_ShowCursor(visible);
|
||||
}
|
||||
printf("Mouse button %d has been %s\n",
|
||||
event->button.button,
|
||||
(event->button.state == SDL_PRESSED) ?
|
||||
"pressed" : "released");
|
||||
return(0);
|
||||
/* See if we are iconified or restored */
|
||||
if (event->active.state & SDL_APPACTIVE) {
|
||||
printf("App has been %s\n",
|
||||
event->active.gain ? "restored" : "iconified");
|
||||
}
|
||||
return (0);
|
||||
|
||||
/* Show relative mouse motion */
|
||||
case SDL_MOUSEMOTION:
|
||||
/* We want to toggle visibility on buttonpress */
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
if (event->button.state == SDL_PRESSED) {
|
||||
visible = !visible;
|
||||
SDL_ShowCursor(visible);
|
||||
}
|
||||
printf("Mouse button %d has been %s\n",
|
||||
event->button.button,
|
||||
(event->button.state == SDL_PRESSED) ? "pressed" : "released");
|
||||
return (0);
|
||||
|
||||
/* Show relative mouse motion */
|
||||
case SDL_MOUSEMOTION:
|
||||
#if 0
|
||||
printf("Mouse motion: {%d,%d} (%d,%d)\n",
|
||||
event->motion.x, event->motion.y,
|
||||
event->motion.xrel, event->motion.yrel);
|
||||
printf("Mouse motion: {%d,%d} (%d,%d)\n",
|
||||
event->motion.x, event->motion.y,
|
||||
event->motion.xrel, event->motion.yrel);
|
||||
#endif
|
||||
return(0);
|
||||
return (0);
|
||||
|
||||
case SDL_KEYDOWN:
|
||||
if ( event->key.keysym.sym == SDLK_ESCAPE ) {
|
||||
HotKey_Quit();
|
||||
}
|
||||
if ( (event->key.keysym.sym == SDLK_g) &&
|
||||
(event->key.keysym.mod & KMOD_CTRL) ) {
|
||||
HotKey_ToggleGrab();
|
||||
}
|
||||
if ( (event->key.keysym.sym == SDLK_z) &&
|
||||
(event->key.keysym.mod & KMOD_CTRL) ) {
|
||||
HotKey_Iconify();
|
||||
}
|
||||
if ( (event->key.keysym.sym == SDLK_RETURN) &&
|
||||
(event->key.keysym.mod & KMOD_ALT) ) {
|
||||
HotKey_ToggleFullScreen();
|
||||
}
|
||||
return(0);
|
||||
case SDL_KEYDOWN:
|
||||
if (event->key.keysym.sym == SDLK_ESCAPE) {
|
||||
HotKey_Quit();
|
||||
}
|
||||
if ((event->key.keysym.sym == SDLK_g) &&
|
||||
(event->key.keysym.mod & KMOD_CTRL)) {
|
||||
HotKey_ToggleGrab();
|
||||
}
|
||||
if ((event->key.keysym.sym == SDLK_z) &&
|
||||
(event->key.keysym.mod & KMOD_CTRL)) {
|
||||
HotKey_Iconify();
|
||||
}
|
||||
if ((event->key.keysym.sym == SDLK_RETURN) &&
|
||||
(event->key.keysym.mod & KMOD_ALT)) {
|
||||
HotKey_ToggleFullScreen();
|
||||
}
|
||||
return (0);
|
||||
|
||||
/* Pass the video resize event through .. */
|
||||
case SDL_VIDEORESIZE:
|
||||
return(1);
|
||||
/* Pass the video resize event through .. */
|
||||
case SDL_VIDEORESIZE:
|
||||
return (1);
|
||||
|
||||
/* This is important! Queue it if we want to quit. */
|
||||
case SDL_QUIT:
|
||||
if ( ! reallyquit ) {
|
||||
reallyquit = 1;
|
||||
printf("Quit requested\n");
|
||||
return(0);
|
||||
}
|
||||
printf("Quit demanded\n");
|
||||
return(1);
|
||||
/* This is important! Queue it if we want to quit. */
|
||||
case SDL_QUIT:
|
||||
if (!reallyquit) {
|
||||
reallyquit = 1;
|
||||
printf("Quit requested\n");
|
||||
return (0);
|
||||
}
|
||||
printf("Quit demanded\n");
|
||||
return (1);
|
||||
|
||||
/* This will never happen because events queued directly
|
||||
to the event queue are not filtered.
|
||||
*/
|
||||
case SDL_USEREVENT:
|
||||
return(1);
|
||||
/* This will never happen because events queued directly
|
||||
to the event queue are not filtered.
|
||||
*/
|
||||
case SDL_USEREVENT:
|
||||
return (1);
|
||||
|
||||
/* Drop all other events */
|
||||
default:
|
||||
return(0);
|
||||
}
|
||||
/* Drop all other events */
|
||||
default:
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
SDL_Event event;
|
||||
char *title;
|
||||
SDL_Surface *icon;
|
||||
Uint8 *icon_mask;
|
||||
int parsed;
|
||||
int w, h;
|
||||
SDL_Event event;
|
||||
char *title;
|
||||
SDL_Surface *icon;
|
||||
Uint8 *icon_mask;
|
||||
int parsed;
|
||||
int w, h;
|
||||
|
||||
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
|
||||
fprintf(stderr,
|
||||
"Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return(1);
|
||||
}
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return (1);
|
||||
}
|
||||
|
||||
/* Check command line arguments */
|
||||
w = 640;
|
||||
h = 480;
|
||||
video_bpp = 8;
|
||||
video_flags = SDL_SWSURFACE;
|
||||
parsed = 1;
|
||||
while ( parsed ) {
|
||||
if ( (argc >= 2) && (strcmp(argv[1], "-fullscreen") == 0) ) {
|
||||
video_flags |= SDL_FULLSCREEN;
|
||||
argc -= 1;
|
||||
argv += 1;
|
||||
} else
|
||||
if ( (argc >= 2) && (strcmp(argv[1], "-resize") == 0) ) {
|
||||
video_flags |= SDL_RESIZABLE;
|
||||
argc -= 1;
|
||||
argv += 1;
|
||||
} else
|
||||
if ( (argc >= 2) && (strcmp(argv[1], "-noframe") == 0) ) {
|
||||
video_flags |= SDL_NOFRAME;
|
||||
argc -= 1;
|
||||
argv += 1;
|
||||
} else
|
||||
if ( (argc >= 3) && (strcmp(argv[1], "-width") == 0) ) {
|
||||
w = atoi(argv[2]);
|
||||
argc -= 2;
|
||||
argv += 2;
|
||||
} else
|
||||
if ( (argc >= 3) && (strcmp(argv[1], "-height") == 0) ) {
|
||||
h = atoi(argv[2]);
|
||||
argc -= 2;
|
||||
argv += 2;
|
||||
} else
|
||||
if ( (argc >= 3) && (strcmp(argv[1], "-bpp") == 0) ) {
|
||||
video_bpp = atoi(argv[2]);
|
||||
argc -= 2;
|
||||
argv += 2;
|
||||
} else {
|
||||
parsed = 0;
|
||||
}
|
||||
}
|
||||
/* Check command line arguments */
|
||||
w = 640;
|
||||
h = 480;
|
||||
video_bpp = 8;
|
||||
video_flags = SDL_SWSURFACE;
|
||||
parsed = 1;
|
||||
while (parsed) {
|
||||
if ((argc >= 2) && (strcmp(argv[1], "-fullscreen") == 0)) {
|
||||
video_flags |= SDL_FULLSCREEN;
|
||||
argc -= 1;
|
||||
argv += 1;
|
||||
} else if ((argc >= 2) && (strcmp(argv[1], "-resize") == 0)) {
|
||||
video_flags |= SDL_RESIZABLE;
|
||||
argc -= 1;
|
||||
argv += 1;
|
||||
} else if ((argc >= 2) && (strcmp(argv[1], "-noframe") == 0)) {
|
||||
video_flags |= SDL_NOFRAME;
|
||||
argc -= 1;
|
||||
argv += 1;
|
||||
} else if ((argc >= 3) && (strcmp(argv[1], "-width") == 0)) {
|
||||
w = atoi(argv[2]);
|
||||
argc -= 2;
|
||||
argv += 2;
|
||||
} else if ((argc >= 3) && (strcmp(argv[1], "-height") == 0)) {
|
||||
h = atoi(argv[2]);
|
||||
argc -= 2;
|
||||
argv += 2;
|
||||
} else if ((argc >= 3) && (strcmp(argv[1], "-bpp") == 0)) {
|
||||
video_bpp = atoi(argv[2]);
|
||||
argc -= 2;
|
||||
argv += 2;
|
||||
} else {
|
||||
parsed = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Set the icon -- this must be done before the first mode set */
|
||||
icon = LoadIconSurface("icon.bmp", &icon_mask);
|
||||
if ( icon != NULL ) {
|
||||
SDL_WM_SetIcon(icon, icon_mask);
|
||||
}
|
||||
if ( icon_mask != NULL )
|
||||
free(icon_mask);
|
||||
/* Set the icon -- this must be done before the first mode set */
|
||||
icon = LoadIconSurface("icon.bmp", &icon_mask);
|
||||
if (icon != NULL) {
|
||||
SDL_WM_SetIcon(icon, icon_mask);
|
||||
}
|
||||
if (icon_mask != NULL)
|
||||
free(icon_mask);
|
||||
|
||||
/* Set the title bar */
|
||||
if ( argv[1] == NULL )
|
||||
title = "Testing 1.. 2.. 3...";
|
||||
else
|
||||
title = argv[1];
|
||||
SDL_WM_SetCaption(title, "testwm");
|
||||
/* Set the title bar */
|
||||
if (argv[1] == NULL)
|
||||
title = "Testing 1.. 2.. 3...";
|
||||
else
|
||||
title = argv[1];
|
||||
SDL_WM_SetCaption(title, "testwm");
|
||||
|
||||
/* See if it's really set */
|
||||
SDL_WM_GetCaption(&title, NULL);
|
||||
if ( title )
|
||||
printf("Title was set to: %s\n", title);
|
||||
else
|
||||
printf("No window title was set!\n");
|
||||
/* See if it's really set */
|
||||
SDL_WM_GetCaption(&title, NULL);
|
||||
if (title)
|
||||
printf("Title was set to: %s\n", title);
|
||||
else
|
||||
printf("No window title was set!\n");
|
||||
|
||||
/* Initialize the display */
|
||||
if ( SetVideoMode(w, h) < 0 ) {
|
||||
quit(1);
|
||||
}
|
||||
/* Initialize the display */
|
||||
if (SetVideoMode(w, h) < 0) {
|
||||
quit(1);
|
||||
}
|
||||
|
||||
/* Set an event filter that discards everything but QUIT */
|
||||
SDL_SetEventFilter(FilterEvents);
|
||||
/* Set an event filter that discards everything but QUIT */
|
||||
SDL_GetEventFilter(&old_filterfunc, &old_filterdata);
|
||||
SDL_SetEventFilter(FilterEvents, NULL);
|
||||
|
||||
/* Ignore key up events, they don't even get filtered */
|
||||
SDL_EventState(SDL_KEYUP, SDL_IGNORE);
|
||||
/* Ignore key up events, they don't even get filtered */
|
||||
SDL_EventState(SDL_KEYUP, SDL_IGNORE);
|
||||
|
||||
/* Loop, waiting for QUIT */
|
||||
while ( SDL_WaitEvent(&event) ) {
|
||||
switch (event.type) {
|
||||
case SDL_VIDEORESIZE:
|
||||
printf("Got a resize event: %dx%d\n",
|
||||
event.resize.w, event.resize.h);
|
||||
SetVideoMode(event.resize.w, event.resize.h);
|
||||
break;
|
||||
case SDL_USEREVENT:
|
||||
printf("Handling internal quit request\n");
|
||||
/* Fall through to the quit handler */
|
||||
case SDL_QUIT:
|
||||
printf("Bye bye..\n");
|
||||
quit(0);
|
||||
default:
|
||||
/* This should never happen */
|
||||
printf("Warning: Event %d wasn't filtered\n",
|
||||
event.type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
printf("SDL_WaitEvent() error: %s\n", SDL_GetError());
|
||||
SDL_Quit();
|
||||
return(255);
|
||||
/* Loop, waiting for QUIT */
|
||||
while (SDL_WaitEvent(&event)) {
|
||||
switch (event.type) {
|
||||
case SDL_VIDEORESIZE:
|
||||
printf("Got a resize event: %dx%d\n",
|
||||
event.resize.w, event.resize.h);
|
||||
SetVideoMode(event.resize.w, event.resize.h);
|
||||
break;
|
||||
case SDL_USEREVENT:
|
||||
printf("Handling internal quit request\n");
|
||||
/* Fall through to the quit handler */
|
||||
case SDL_QUIT:
|
||||
printf("Bye bye..\n");
|
||||
quit(0);
|
||||
default:
|
||||
/* This should never happen */
|
||||
printf("Warning: Event %d wasn't filtered\n", event.type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
printf("SDL_WaitEvent() error: %s\n", SDL_GetError());
|
||||
SDL_Quit();
|
||||
return (255);
|
||||
}
|
||||
|
||||
99
test/testwm2.c
Normal file
99
test/testwm2.c
Normal file
@@ -0,0 +1,99 @@
|
||||
/* Simple program: Move N sprites around on the screen as fast as possible */
|
||||
|
||||
#include "SDL.h"
|
||||
|
||||
#define NUM_WINDOWS 2
|
||||
#define WINDOW_W 640
|
||||
#define WINDOW_H 480
|
||||
|
||||
static int num_windows;
|
||||
static SDL_WindowID *windows;
|
||||
|
||||
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
||||
static void
|
||||
quit(int rc)
|
||||
{
|
||||
if (windows) {
|
||||
SDL_free(windows);
|
||||
}
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int window_w, window_h;
|
||||
int i, done;
|
||||
SDL_Event event;
|
||||
|
||||
/* Initialize SDL */
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return (1);
|
||||
}
|
||||
|
||||
num_windows = NUM_WINDOWS;
|
||||
window_w = WINDOW_W;
|
||||
window_h = WINDOW_H;
|
||||
while (argc > 1) {
|
||||
if (strcmp(argv[argc - 1], "-width") == 0) {
|
||||
window_w = atoi(argv[argc]);
|
||||
--argc;
|
||||
} else if (strcmp(argv[argc - 1], "-height") == 0) {
|
||||
window_h = atoi(argv[argc]);
|
||||
--argc;
|
||||
} else {
|
||||
fprintf(stderr, "Usage: %s [-width] [-height]\n", argv[0]);
|
||||
quit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Create the windows */
|
||||
windows = (SDL_WindowID *) SDL_malloc(num_windows * sizeof(*windows));
|
||||
if (!windows) {
|
||||
fprintf(stderr, "Out of memory!\n");
|
||||
quit(2);
|
||||
}
|
||||
for (i = 0; i < num_windows; ++i) {
|
||||
char title[32];
|
||||
int x, y;
|
||||
|
||||
SDL_snprintf(title, sizeof(title), "testwm %d", i + 1);
|
||||
if (i == 0) {
|
||||
x = SDL_WINDOWPOS_CENTERED;
|
||||
y = SDL_WINDOWPOS_CENTERED;
|
||||
} else {
|
||||
x = SDL_WINDOWPOS_UNDEFINED;
|
||||
y = SDL_WINDOWPOS_UNDEFINED;
|
||||
}
|
||||
windows[i] =
|
||||
SDL_CreateWindow(title, x, y, window_w, window_h,
|
||||
SDL_WINDOW_SHOWN);
|
||||
if (!windows[i]) {
|
||||
fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
|
||||
quit(2);
|
||||
}
|
||||
}
|
||||
|
||||
/* Loop, blitting sprites and waiting for a keystroke */
|
||||
done = 0;
|
||||
while (!done) {
|
||||
/* Check for events */
|
||||
while (SDL_PollEvent(&event)) {
|
||||
switch (event.type) {
|
||||
case SDL_KEYDOWN:
|
||||
/* Any keypress quits the app... */
|
||||
case SDL_QUIT:
|
||||
done = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
quit(0);
|
||||
}
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
556
test/threadwin.c
556
test/threadwin.c
@@ -15,323 +15,327 @@ static int done = 0;
|
||||
static int visible = 1;
|
||||
|
||||
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
||||
static void quit(int rc)
|
||||
static void
|
||||
quit(int rc)
|
||||
{
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
}
|
||||
|
||||
SDL_Surface *LoadIconSurface(char *file, Uint8 **maskp)
|
||||
SDL_Surface *
|
||||
LoadIconSurface(char *file, Uint8 ** maskp)
|
||||
{
|
||||
SDL_Surface *icon;
|
||||
Uint8 *pixels;
|
||||
Uint8 *mask;
|
||||
int mlen, i;
|
||||
SDL_Surface *icon;
|
||||
Uint8 *pixels;
|
||||
Uint8 *mask;
|
||||
int mlen, i;
|
||||
|
||||
*maskp = NULL;
|
||||
*maskp = NULL;
|
||||
|
||||
/* Load the icon surface */
|
||||
icon = SDL_LoadBMP(file);
|
||||
if ( icon == NULL ) {
|
||||
fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
|
||||
return(NULL);
|
||||
}
|
||||
/* Load the icon surface */
|
||||
icon = SDL_LoadBMP(file);
|
||||
if (icon == NULL) {
|
||||
fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/* Check width and height */
|
||||
if ( (icon->w%8) != 0 ) {
|
||||
fprintf(stderr, "Icon width must be a multiple of 8!\n");
|
||||
SDL_FreeSurface(icon);
|
||||
return(NULL);
|
||||
}
|
||||
if ( icon->format->palette == NULL ) {
|
||||
fprintf(stderr, "Icon must have a palette!\n");
|
||||
SDL_FreeSurface(icon);
|
||||
return(NULL);
|
||||
}
|
||||
/* Check width and height */
|
||||
if ((icon->w % 8) != 0) {
|
||||
fprintf(stderr, "Icon width must be a multiple of 8!\n");
|
||||
SDL_FreeSurface(icon);
|
||||
return (NULL);
|
||||
}
|
||||
if (icon->format->palette == NULL) {
|
||||
fprintf(stderr, "Icon must have a palette!\n");
|
||||
SDL_FreeSurface(icon);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/* Set the colorkey */
|
||||
SDL_SetColorKey(icon, SDL_SRCCOLORKEY, *((Uint8 *)icon->pixels));
|
||||
/* Set the colorkey */
|
||||
SDL_SetColorKey(icon, SDL_SRCCOLORKEY, *((Uint8 *) icon->pixels));
|
||||
|
||||
/* Create the mask */
|
||||
pixels = (Uint8 *)icon->pixels;
|
||||
printf("Transparent pixel: (%d,%d,%d)\n",
|
||||
icon->format->palette->colors[*pixels].r,
|
||||
icon->format->palette->colors[*pixels].g,
|
||||
icon->format->palette->colors[*pixels].b);
|
||||
mlen = icon->w*icon->h;
|
||||
mask = (Uint8 *)malloc(mlen/8);
|
||||
if ( mask == NULL ) {
|
||||
fprintf(stderr, "Out of memory!\n");
|
||||
SDL_FreeSurface(icon);
|
||||
return(NULL);
|
||||
}
|
||||
memset(mask, 0, mlen/8);
|
||||
for ( i=0; i<mlen; ) {
|
||||
if ( pixels[i] != *pixels )
|
||||
mask[i/8] |= 0x01;
|
||||
++i;
|
||||
if ( (i%8) != 0 )
|
||||
mask[i/8] <<= 1;
|
||||
}
|
||||
*maskp = mask;
|
||||
return(icon);
|
||||
/* Create the mask */
|
||||
pixels = (Uint8 *) icon->pixels;
|
||||
printf("Transparent pixel: (%d,%d,%d)\n",
|
||||
icon->format->palette->colors[*pixels].r,
|
||||
icon->format->palette->colors[*pixels].g,
|
||||
icon->format->palette->colors[*pixels].b);
|
||||
mlen = icon->w * icon->h;
|
||||
mask = (Uint8 *) malloc(mlen / 8);
|
||||
if (mask == NULL) {
|
||||
fprintf(stderr, "Out of memory!\n");
|
||||
SDL_FreeSurface(icon);
|
||||
return (NULL);
|
||||
}
|
||||
memset(mask, 0, mlen / 8);
|
||||
for (i = 0; i < mlen;) {
|
||||
if (pixels[i] != *pixels)
|
||||
mask[i / 8] |= 0x01;
|
||||
++i;
|
||||
if ((i % 8) != 0)
|
||||
mask[i / 8] <<= 1;
|
||||
}
|
||||
*maskp = mask;
|
||||
return (icon);
|
||||
}
|
||||
|
||||
int SDLCALL FilterEvents(const SDL_Event *event)
|
||||
int SDLCALL
|
||||
FilterEvents(void *userdata, SDL_Event * event)
|
||||
{
|
||||
static int reallyquit = 0;
|
||||
static int reallyquit = 0;
|
||||
|
||||
switch (event->type) {
|
||||
switch (event->type) {
|
||||
|
||||
case SDL_ACTIVEEVENT:
|
||||
/* See what happened */
|
||||
printf("App %s ",
|
||||
event->active.gain ? "gained" : "lost");
|
||||
if ( event->active.state & SDL_APPACTIVE )
|
||||
printf("active ");
|
||||
if ( event->active.state & SDL_APPMOUSEFOCUS )
|
||||
printf("mouse ");
|
||||
if ( event->active.state & SDL_APPINPUTFOCUS )
|
||||
printf("input ");
|
||||
printf("focus\n");
|
||||
case SDL_ACTIVEEVENT:
|
||||
/* See what happened */
|
||||
printf("App %s ", event->active.gain ? "gained" : "lost");
|
||||
if (event->active.state & SDL_APPACTIVE)
|
||||
printf("active ");
|
||||
if (event->active.state & SDL_APPMOUSEFOCUS)
|
||||
printf("mouse ");
|
||||
if (event->active.state & SDL_APPINPUTFOCUS)
|
||||
printf("input ");
|
||||
printf("focus\n");
|
||||
|
||||
/* See if we are iconified or restored */
|
||||
if ( event->active.state & SDL_APPACTIVE ) {
|
||||
printf("App has been %s\n",
|
||||
event->active.gain ?
|
||||
"restored" : "iconified");
|
||||
}
|
||||
return(0);
|
||||
/* See if we are iconified or restored */
|
||||
if (event->active.state & SDL_APPACTIVE) {
|
||||
printf("App has been %s\n",
|
||||
event->active.gain ? "restored" : "iconified");
|
||||
}
|
||||
return (0);
|
||||
|
||||
/* This is important! Queue it if we want to quit. */
|
||||
case SDL_QUIT:
|
||||
if ( ! reallyquit ) {
|
||||
reallyquit = 1;
|
||||
printf("Quit requested\n");
|
||||
return(0);
|
||||
}
|
||||
printf("Quit demanded\n");
|
||||
return(1);
|
||||
/* This is important! Queue it if we want to quit. */
|
||||
case SDL_QUIT:
|
||||
if (!reallyquit) {
|
||||
reallyquit = 1;
|
||||
printf("Quit requested\n");
|
||||
return (0);
|
||||
}
|
||||
printf("Quit demanded\n");
|
||||
return (1);
|
||||
|
||||
/* Mouse and keyboard events go to threads */
|
||||
case SDL_MOUSEMOTION:
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
case SDL_KEYDOWN:
|
||||
case SDL_KEYUP:
|
||||
return(1);
|
||||
/* Mouse and keyboard events go to threads */
|
||||
case SDL_MOUSEMOTION:
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
case SDL_KEYDOWN:
|
||||
case SDL_KEYUP:
|
||||
return (1);
|
||||
|
||||
/* Drop all other events */
|
||||
default:
|
||||
return(0);
|
||||
}
|
||||
/* Drop all other events */
|
||||
default:
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
|
||||
int SDLCALL HandleMouse(void *unused)
|
||||
int SDLCALL
|
||||
HandleMouse(void *unused)
|
||||
{
|
||||
SDL_Event events[10];
|
||||
int i, found;
|
||||
Uint32 mask;
|
||||
SDL_Event events[10];
|
||||
int i, found;
|
||||
Uint32 mask;
|
||||
|
||||
/* Handle mouse events here */
|
||||
mask = (SDL_MOUSEMOTIONMASK|SDL_MOUSEBUTTONDOWNMASK|SDL_MOUSEBUTTONUPMASK);
|
||||
while ( ! done ) {
|
||||
found = SDL_PeepEvents(events, 10, SDL_GETEVENT, mask);
|
||||
for ( i=0; i<found; ++i ) {
|
||||
switch(events[i].type) {
|
||||
/* We want to toggle visibility on buttonpress */
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
if ( events[i].button.state == SDL_PRESSED ) {
|
||||
visible = !visible;
|
||||
SDL_ShowCursor(visible);
|
||||
}
|
||||
printf("Mouse button %d has been %s\n",
|
||||
events[i].button.button,
|
||||
(events[i].button.state == SDL_PRESSED) ?
|
||||
"pressed" : "released");
|
||||
break;
|
||||
/* Show relative mouse motion */
|
||||
case SDL_MOUSEMOTION:
|
||||
printf("Mouse relative motion: {%d,%d}\n",
|
||||
events[i].motion.xrel, events[i].motion.yrel);
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* Give up some CPU to allow events to arrive */
|
||||
SDL_Delay(20);
|
||||
}
|
||||
return(0);
|
||||
/* Handle mouse events here */
|
||||
mask =
|
||||
(SDL_MOUSEMOTIONMASK | SDL_MOUSEBUTTONDOWNMASK |
|
||||
SDL_MOUSEBUTTONUPMASK);
|
||||
while (!done) {
|
||||
found = SDL_PeepEvents(events, 10, SDL_GETEVENT, mask);
|
||||
for (i = 0; i < found; ++i) {
|
||||
switch (events[i].type) {
|
||||
/* We want to toggle visibility on buttonpress */
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
if (events[i].button.state == SDL_PRESSED) {
|
||||
visible = !visible;
|
||||
SDL_ShowCursor(visible);
|
||||
}
|
||||
printf("Mouse button %d has been %s\n",
|
||||
events[i].button.button,
|
||||
(events[i].button.state == SDL_PRESSED) ?
|
||||
"pressed" : "released");
|
||||
break;
|
||||
/* Show relative mouse motion */
|
||||
case SDL_MOUSEMOTION:
|
||||
printf("Mouse relative motion: {%d,%d}\n",
|
||||
events[i].motion.xrel, events[i].motion.yrel);
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* Give up some CPU to allow events to arrive */
|
||||
SDL_Delay(20);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int SDLCALL HandleKeyboard(void *unused)
|
||||
int SDLCALL
|
||||
HandleKeyboard(void *unused)
|
||||
{
|
||||
SDL_Event events[10];
|
||||
int i, found;
|
||||
Uint32 mask;
|
||||
SDL_Event events[10];
|
||||
int i, found;
|
||||
Uint32 mask;
|
||||
|
||||
/* Handle mouse events here */
|
||||
mask = (SDL_KEYDOWNMASK|SDL_KEYUPMASK);
|
||||
while ( ! done ) {
|
||||
found = SDL_PeepEvents(events, 10, SDL_GETEVENT, mask);
|
||||
for ( i=0; i<found; ++i ) {
|
||||
switch(events[i].type) {
|
||||
/* We want to toggle visibility on buttonpress */
|
||||
case SDL_KEYDOWN:
|
||||
case SDL_KEYUP:
|
||||
printf("Key '%c' has been %s\n",
|
||||
events[i].key.keysym.unicode,
|
||||
(events[i].key.state == SDL_PRESSED) ?
|
||||
"pressed" : "released");
|
||||
/* Handle mouse events here */
|
||||
mask = (SDL_KEYDOWNMASK | SDL_KEYUPMASK);
|
||||
while (!done) {
|
||||
found = SDL_PeepEvents(events, 10, SDL_GETEVENT, mask);
|
||||
for (i = 0; i < found; ++i) {
|
||||
switch (events[i].type) {
|
||||
/* We want to toggle visibility on buttonpress */
|
||||
case SDL_KEYDOWN:
|
||||
case SDL_KEYUP:
|
||||
printf("Key '%c' has been %s\n",
|
||||
events[i].key.keysym.unicode,
|
||||
(events[i].key.state == SDL_PRESSED) ?
|
||||
"pressed" : "released");
|
||||
|
||||
/* Allow hitting <ESC> to quit the app */
|
||||
if ( events[i].key.keysym.sym == SDLK_ESCAPE ) {
|
||||
done = 1;
|
||||
}
|
||||
/* Allow hitting <ESC> to quit the app */
|
||||
if (events[i].key.keysym.sym == SDLK_ESCAPE) {
|
||||
done = 1;
|
||||
}
|
||||
|
||||
/* skip events now that aren't KEYUPs... */
|
||||
if (events[i].key.state == SDL_PRESSED)
|
||||
break;
|
||||
/* skip events now that aren't KEYUPs... */
|
||||
if (events[i].key.state == SDL_PRESSED)
|
||||
break;
|
||||
|
||||
if ( events[i].key.keysym.sym == SDLK_f ) {
|
||||
int rc = 0;
|
||||
printf("attempting to toggle fullscreen...\n");
|
||||
rc = SDL_WM_ToggleFullScreen(SDL_GetVideoSurface());
|
||||
printf("SDL_WM_ToggleFullScreen returned %d.\n", rc);
|
||||
}
|
||||
if (events[i].key.keysym.sym == SDLK_f) {
|
||||
int rc = 0;
|
||||
printf("attempting to toggle fullscreen...\n");
|
||||
rc = SDL_WM_ToggleFullScreen(SDL_GetVideoSurface());
|
||||
printf("SDL_WM_ToggleFullScreen returned %d.\n", rc);
|
||||
}
|
||||
|
||||
if ( events[i].key.keysym.sym == SDLK_g ) {
|
||||
SDL_GrabMode m;
|
||||
m = SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON ?
|
||||
SDL_GRAB_OFF : SDL_GRAB_ON;
|
||||
printf("attempting to toggle input grab to %s...\n",
|
||||
m == SDL_GRAB_ON ? "ON" : "OFF");
|
||||
SDL_WM_GrabInput(m);
|
||||
printf("attempt finished.\n");
|
||||
}
|
||||
if (events[i].key.keysym.sym == SDLK_g) {
|
||||
SDL_GrabMode m;
|
||||
m = SDL_WM_GrabInput(SDL_GRAB_QUERY) ==
|
||||
SDL_GRAB_ON ? SDL_GRAB_OFF : SDL_GRAB_ON;
|
||||
printf
|
||||
("attempting to toggle input grab to %s...\n",
|
||||
m == SDL_GRAB_ON ? "ON" : "OFF");
|
||||
SDL_WM_GrabInput(m);
|
||||
printf("attempt finished.\n");
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* Give up some CPU to allow events to arrive */
|
||||
SDL_Delay(20);
|
||||
}
|
||||
return(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* Give up some CPU to allow events to arrive */
|
||||
SDL_Delay(20);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
SDL_Surface *screen;
|
||||
SDL_Surface *icon;
|
||||
Uint8 *icon_mask;
|
||||
int i, parsed;
|
||||
Uint8 *buffer;
|
||||
SDL_Color palette[256];
|
||||
Uint32 init_flags;
|
||||
Uint8 video_bpp;
|
||||
Uint32 video_flags;
|
||||
SDL_Thread *mouse_thread;
|
||||
SDL_Thread *keybd_thread;
|
||||
SDL_Surface *screen;
|
||||
SDL_Surface *icon;
|
||||
Uint8 *icon_mask;
|
||||
int i, parsed;
|
||||
Uint8 *buffer;
|
||||
SDL_Color palette[256];
|
||||
Uint32 init_flags;
|
||||
Uint8 video_bpp;
|
||||
Uint32 video_flags;
|
||||
SDL_Thread *mouse_thread;
|
||||
SDL_Thread *keybd_thread;
|
||||
|
||||
/* Set the options, based on command line arguments */
|
||||
init_flags = SDL_INIT_VIDEO;
|
||||
video_bpp = 8;
|
||||
video_flags = SDL_SWSURFACE;
|
||||
parsed = 1;
|
||||
while ( parsed ) {
|
||||
/* If the threaded option is enabled, and the SDL library hasn't
|
||||
been compiled with threaded events enabled, then the mouse and
|
||||
keyboard won't respond.
|
||||
*/
|
||||
if ( (argc >= 2) && (strcmp(argv[1], "-threaded") == 0) ) {
|
||||
init_flags |= SDL_INIT_EVENTTHREAD;
|
||||
argc -= 1;
|
||||
argv += 1;
|
||||
printf("Running with threaded events\n");
|
||||
} else
|
||||
if ( (argc >= 2) && (strcmp(argv[1], "-fullscreen") == 0) ) {
|
||||
video_flags |= SDL_FULLSCREEN;
|
||||
argc -= 1;
|
||||
argv += 1;
|
||||
} else
|
||||
if ( (argc >= 3) && (strcmp(argv[1], "-bpp") == 0) ) {
|
||||
video_bpp = atoi(argv[2]);
|
||||
argc -= 2;
|
||||
argv += 2;
|
||||
} else {
|
||||
parsed = 0;
|
||||
}
|
||||
}
|
||||
/* Set the options, based on command line arguments */
|
||||
init_flags = SDL_INIT_VIDEO;
|
||||
video_bpp = 8;
|
||||
video_flags = SDL_SWSURFACE;
|
||||
parsed = 1;
|
||||
while (parsed) {
|
||||
/* If the threaded option is enabled, and the SDL library hasn't
|
||||
been compiled with threaded events enabled, then the mouse and
|
||||
keyboard won't respond.
|
||||
*/
|
||||
if ((argc >= 2) && (strcmp(argv[1], "-threaded") == 0)) {
|
||||
init_flags |= SDL_INIT_EVENTTHREAD;
|
||||
argc -= 1;
|
||||
argv += 1;
|
||||
printf("Running with threaded events\n");
|
||||
} else if ((argc >= 2) && (strcmp(argv[1], "-fullscreen") == 0)) {
|
||||
video_flags |= SDL_FULLSCREEN;
|
||||
argc -= 1;
|
||||
argv += 1;
|
||||
} else if ((argc >= 3) && (strcmp(argv[1], "-bpp") == 0)) {
|
||||
video_bpp = atoi(argv[2]);
|
||||
argc -= 2;
|
||||
argv += 2;
|
||||
} else {
|
||||
parsed = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Initialize SDL with the requested flags */
|
||||
if ( SDL_Init(init_flags) < 0 ) {
|
||||
fprintf(stderr,
|
||||
"Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return(1);
|
||||
}
|
||||
/* Initialize SDL with the requested flags */
|
||||
if (SDL_Init(init_flags) < 0) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return (1);
|
||||
}
|
||||
|
||||
/* Set the icon -- this must be done before the first mode set */
|
||||
icon = LoadIconSurface("icon.bmp", &icon_mask);
|
||||
if ( icon != NULL ) {
|
||||
SDL_WM_SetIcon(icon, icon_mask);
|
||||
}
|
||||
if ( icon_mask != NULL )
|
||||
free(icon_mask);
|
||||
/* Set the icon -- this must be done before the first mode set */
|
||||
icon = LoadIconSurface("icon.bmp", &icon_mask);
|
||||
if (icon != NULL) {
|
||||
SDL_WM_SetIcon(icon, icon_mask);
|
||||
}
|
||||
if (icon_mask != NULL)
|
||||
free(icon_mask);
|
||||
|
||||
/* Initialize the display */
|
||||
screen = SDL_SetVideoMode(640, 480, video_bpp, video_flags);
|
||||
if ( screen == NULL ) {
|
||||
fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
|
||||
video_bpp, SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
printf("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ?
|
||||
"fullscreen" : "windowed");
|
||||
/* Initialize the display */
|
||||
screen = SDL_SetVideoMode(640, 480, video_bpp, video_flags);
|
||||
if (screen == NULL) {
|
||||
fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
|
||||
video_bpp, SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
printf("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ?
|
||||
"fullscreen" : "windowed");
|
||||
|
||||
/* Enable printable characters */
|
||||
SDL_EnableUNICODE(1);
|
||||
/* Enable printable characters */
|
||||
SDL_EnableUNICODE(1);
|
||||
|
||||
/* Set an event filter that discards everything but QUIT */
|
||||
SDL_SetEventFilter(FilterEvents);
|
||||
/* Set an event filter that discards everything but QUIT */
|
||||
SDL_SetEventFilter(FilterEvents, NULL);
|
||||
|
||||
/* Create the event handling threads */
|
||||
mouse_thread = SDL_CreateThread(HandleMouse, NULL);
|
||||
keybd_thread = SDL_CreateThread(HandleKeyboard, NULL);
|
||||
/* Create the event handling threads */
|
||||
mouse_thread = SDL_CreateThread(HandleMouse, NULL);
|
||||
keybd_thread = SDL_CreateThread(HandleKeyboard, NULL);
|
||||
|
||||
/* Set the surface pixels and refresh! */
|
||||
for ( i=0; i<256; ++i ) {
|
||||
palette[i].r = 255-i;
|
||||
palette[i].g = 255-i;
|
||||
palette[i].b = 255-i;
|
||||
}
|
||||
SDL_SetColors(screen, palette, 0, 256);
|
||||
if ( SDL_LockSurface(screen) < 0 ) {
|
||||
fprintf(stderr, "Couldn't lock display surface: %s\n",
|
||||
SDL_GetError());
|
||||
quit(2);
|
||||
}
|
||||
buffer = (Uint8 *)screen->pixels;
|
||||
for ( i=0; i<screen->h; ++i ) {
|
||||
memset(buffer,(i*255)/screen->h,
|
||||
screen->w*screen->format->BytesPerPixel);
|
||||
buffer += screen->pitch;
|
||||
}
|
||||
SDL_UnlockSurface(screen);
|
||||
SDL_UpdateRect(screen, 0, 0, 0, 0);
|
||||
/* Set the surface pixels and refresh! */
|
||||
for (i = 0; i < 256; ++i) {
|
||||
palette[i].r = 255 - i;
|
||||
palette[i].g = 255 - i;
|
||||
palette[i].b = 255 - i;
|
||||
}
|
||||
SDL_SetColors(screen, palette, 0, 256);
|
||||
if (SDL_LockSurface(screen) < 0) {
|
||||
fprintf(stderr, "Couldn't lock display surface: %s\n",
|
||||
SDL_GetError());
|
||||
quit(2);
|
||||
}
|
||||
buffer = (Uint8 *) screen->pixels;
|
||||
for (i = 0; i < screen->h; ++i) {
|
||||
memset(buffer, (i * 255) / screen->h,
|
||||
screen->w * screen->format->BytesPerPixel);
|
||||
buffer += screen->pitch;
|
||||
}
|
||||
SDL_UnlockSurface(screen);
|
||||
SDL_UpdateRect(screen, 0, 0, 0, 0);
|
||||
|
||||
/* Loop, waiting for QUIT */
|
||||
while ( ! done ) {
|
||||
if ( ! (init_flags & SDL_INIT_EVENTTHREAD) ) {
|
||||
SDL_PumpEvents(); /* Needed when event thread is off */
|
||||
}
|
||||
if ( SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_QUITMASK) ) {
|
||||
done = 1;
|
||||
}
|
||||
/* Give up some CPU so the events can accumulate */
|
||||
SDL_Delay(20);
|
||||
}
|
||||
SDL_WaitThread(mouse_thread, NULL);
|
||||
SDL_WaitThread(keybd_thread, NULL);
|
||||
SDL_Quit();
|
||||
return(0);
|
||||
/* Loop, waiting for QUIT */
|
||||
while (!done) {
|
||||
if (!(init_flags & SDL_INIT_EVENTTHREAD)) {
|
||||
SDL_PumpEvents(); /* Needed when event thread is off */
|
||||
}
|
||||
if (SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_QUITMASK)) {
|
||||
done = 1;
|
||||
}
|
||||
/* Give up some CPU so the events can accumulate */
|
||||
SDL_Delay(20);
|
||||
}
|
||||
SDL_WaitThread(mouse_thread, NULL);
|
||||
SDL_WaitThread(keybd_thread, NULL);
|
||||
SDL_Quit();
|
||||
return (0);
|
||||
}
|
||||
|
||||
@@ -14,78 +14,83 @@
|
||||
static char volatile time_for_threads_to_die[NUMTHREADS];
|
||||
|
||||
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
||||
static void quit(int rc)
|
||||
static void
|
||||
quit(int rc)
|
||||
{
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
SDL_Quit();
|
||||
exit(rc);
|
||||
}
|
||||
|
||||
int SDLCALL SubThreadFunc(void *data) {
|
||||
while(! *(int volatile *)data) {
|
||||
; /*SDL_Delay(10);*/ /* do nothing */
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SDLCALL ThreadFunc(void *data) {
|
||||
SDL_Thread *sub_threads[NUMTHREADS];
|
||||
int flags[NUMTHREADS];
|
||||
int i;
|
||||
int tid = (int)(uintptr_t)data;
|
||||
|
||||
fprintf(stderr, "Creating Thread %d\n", tid);
|
||||
|
||||
for(i = 0; i < NUMTHREADS; i++) {
|
||||
flags[i] = 0;
|
||||
sub_threads[i] = SDL_CreateThread(SubThreadFunc, &flags[i]);
|
||||
}
|
||||
|
||||
printf("Thread '%d' waiting for signal\n", tid);
|
||||
while(time_for_threads_to_die[tid] != 1) {
|
||||
; /* do nothing */
|
||||
}
|
||||
|
||||
printf("Thread '%d' sending signals to subthreads\n", tid);
|
||||
for(i = 0; i < NUMTHREADS; i++) {
|
||||
flags[i] = 1;
|
||||
SDL_WaitThread(sub_threads[i], NULL);
|
||||
}
|
||||
|
||||
printf("Thread '%d' exiting!\n", tid);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int SDLCALL
|
||||
SubThreadFunc(void *data)
|
||||
{
|
||||
SDL_Thread *threads[NUMTHREADS];
|
||||
int i;
|
||||
|
||||
/* Load the SDL library */
|
||||
if ( SDL_Init(0) < 0 ) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
|
||||
return(1);
|
||||
}
|
||||
|
||||
signal(SIGSEGV, SIG_DFL);
|
||||
for(i = 0; i < NUMTHREADS; i++) {
|
||||
time_for_threads_to_die[i] = 0;
|
||||
threads[i] = SDL_CreateThread(ThreadFunc, (void *)(uintptr_t)i);
|
||||
|
||||
if ( threads[i] == NULL ) {
|
||||
fprintf(stderr,
|
||||
"Couldn't create thread: %s\n", SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
}
|
||||
|
||||
for(i = 0; i < NUMTHREADS; i++) {
|
||||
time_for_threads_to_die[i] = 1;
|
||||
}
|
||||
|
||||
for(i = 0; i < NUMTHREADS; i++) {
|
||||
SDL_WaitThread(threads[i], NULL);
|
||||
}
|
||||
SDL_Quit();
|
||||
return(0);
|
||||
while (!*(int volatile *) data) {
|
||||
; /*SDL_Delay(10); *//* do nothing */
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SDLCALL
|
||||
ThreadFunc(void *data)
|
||||
{
|
||||
SDL_Thread *sub_threads[NUMTHREADS];
|
||||
int flags[NUMTHREADS];
|
||||
int i;
|
||||
int tid = (int) (uintptr_t) data;
|
||||
|
||||
fprintf(stderr, "Creating Thread %d\n", tid);
|
||||
|
||||
for (i = 0; i < NUMTHREADS; i++) {
|
||||
flags[i] = 0;
|
||||
sub_threads[i] = SDL_CreateThread(SubThreadFunc, &flags[i]);
|
||||
}
|
||||
|
||||
printf("Thread '%d' waiting for signal\n", tid);
|
||||
while (time_for_threads_to_die[tid] != 1) {
|
||||
; /* do nothing */
|
||||
}
|
||||
|
||||
printf("Thread '%d' sending signals to subthreads\n", tid);
|
||||
for (i = 0; i < NUMTHREADS; i++) {
|
||||
flags[i] = 1;
|
||||
SDL_WaitThread(sub_threads[i], NULL);
|
||||
}
|
||||
|
||||
printf("Thread '%d' exiting!\n", tid);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
SDL_Thread *threads[NUMTHREADS];
|
||||
int i;
|
||||
|
||||
/* Load the SDL library */
|
||||
if (SDL_Init(0) < 0) {
|
||||
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
return (1);
|
||||
}
|
||||
|
||||
signal(SIGSEGV, SIG_DFL);
|
||||
for (i = 0; i < NUMTHREADS; i++) {
|
||||
time_for_threads_to_die[i] = 0;
|
||||
threads[i] = SDL_CreateThread(ThreadFunc, (void *) (uintptr_t) i);
|
||||
|
||||
if (threads[i] == NULL) {
|
||||
fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < NUMTHREADS; i++) {
|
||||
time_for_threads_to_die[i] = 1;
|
||||
}
|
||||
|
||||
for (i = 0; i < NUMTHREADS; i++) {
|
||||
SDL_WaitThread(threads[i], NULL);
|
||||
}
|
||||
SDL_Quit();
|
||||
return (0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user