Removed the SDL 1.2 compatibility API... we'll see how painful this is.

This commit is contained in:
Sam Lantinga 2012-01-22 18:11:41 -05:00
parent cb74808ecb
commit 5ef4144446
29 changed files with 51 additions and 7893 deletions

View file

@ -9,23 +9,11 @@ LIBS = @LIBS@
TARGETS = \
checkkeys$(EXE) \
graywin$(EXE) \
loopwave$(EXE) \
testalpha$(EXE) \
testatomic$(EXE) \
testaudioinfo$(EXE) \
testbitmap$(EXE) \
testblitspeed$(EXE) \
testcursor$(EXE) \
testdraw2$(EXE) \
testdyngl$(EXE) \
testdyngles$(EXE) \
testerror$(EXE) \
testfile$(EXE) \
testfill$(EXE) \
testgamma$(EXE) \
testgesture$(EXE) \
testgl$(EXE) \
testgl2$(EXE) \
testgles$(EXE) \
testhaptic$(EXE) \
@ -40,7 +28,6 @@ TARGETS = \
testlock$(EXE) \
testmultiaudio$(EXE) \
testnative$(EXE) \
testoverlay$(EXE) \
testoverlay2$(EXE) \
testplatform$(EXE) \
testpower$(EXE) \
@ -50,17 +37,12 @@ TARGETS = \
testsem$(EXE) \
testshader$(EXE) \
testshape$(EXE) \
testsprite$(EXE) \
testsprite2$(EXE) \
testspriteminimal$(EXE) \
teststreaming$(EXE) \
testtimer$(EXE) \
testver$(EXE) \
testvidinfo$(EXE) \
testwin$(EXE) \
testwm$(EXE) \
testwm2$(EXE) \
threadwin$(EXE) \
torturethread$(EXE) \
all: Makefile $(TARGETS)
@ -71,9 +53,6 @@ Makefile: $(srcdir)/Makefile.in
checkkeys$(EXE): $(srcdir)/checkkeys.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
graywin$(EXE): $(srcdir)/graywin.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
loopwave$(EXE): $(srcdir)/loopwave.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
@ -86,51 +65,24 @@ testaudioinfo$(EXE): $(srcdir)/testaudioinfo.c
testmultiaudio$(EXE): $(srcdir)/testmultiaudio.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
testalpha$(EXE): $(srcdir)/testalpha.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
testatomic$(EXE): $(srcdir)/testatomic.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
testbitmap$(EXE): $(srcdir)/testbitmap.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
testblitspeed$(EXE): $(srcdir)/testblitspeed.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
testcursor$(EXE): $(srcdir)/testcursor.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
testintersections$(EXE): $(srcdir)/testintersections.c $(srcdir)/common.c
$(CC) -o $@ $(srcdir)/testintersections.c $(srcdir)/common.c $(CFLAGS) $(LIBS)
testdraw2$(EXE): $(srcdir)/testdraw2.c $(srcdir)/common.c
$(CC) -o $@ $(srcdir)/testdraw2.c $(srcdir)/common.c $(CFLAGS) $(LIBS)
testdyngl$(EXE): $(srcdir)/testdyngl.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
testdyngles$(EXE): $(srcdir)/testdyngles.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
testerror$(EXE): $(srcdir)/testerror.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
testfile$(EXE): $(srcdir)/testfile.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
testfill$(EXE): $(srcdir)/testfill.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
testgamma$(EXE): $(srcdir)/testgamma.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
testgesture$(EXE): $(srcdir)/testgesture.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
testgl$(EXE): $(srcdir)/testgl.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS) @GLLIB@ @MATHLIB@
testgl2$(EXE): $(srcdir)/testgl2.c $(srcdir)/common.c
$(CC) -o $@ $(srcdir)/testgl2.c $(srcdir)/common.c $(CFLAGS) $(LIBS) @GLLIB@ @MATHLIB@
@ -173,9 +125,6 @@ testnative$(EXE): $(srcdir)/testnative.c \
testoverlay2$(EXE): $(srcdir)/testoverlay2.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
testoverlay$(EXE): $(srcdir)/testoverlay.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
testplatform$(EXE): $(srcdir)/testplatform.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
@ -197,9 +146,6 @@ testshader$(EXE): $(srcdir)/testshader.c
testshape$(EXE): $(srcdir)/testshape.c
$(CC) -o $@ $? -std=c99 $(CFLAGS) $(LIBS)
testsprite$(EXE): $(srcdir)/testsprite.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
testsprite2$(EXE): $(srcdir)/testsprite2.c $(srcdir)/common.c
$(CC) -o $@ $(srcdir)/testsprite2.c $(srcdir)/common.c $(CFLAGS) $(LIBS) @MATHLIB@
@ -215,21 +161,9 @@ testtimer$(EXE): $(srcdir)/testtimer.c
testver$(EXE): $(srcdir)/testver.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
testvidinfo$(EXE): $(srcdir)/testvidinfo.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
testwin$(EXE): $(srcdir)/testwin.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
testwm$(EXE): $(srcdir)/testwm.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
testwm2$(EXE): $(srcdir)/testwm2.c $(srcdir)/common.c
$(CC) -o $@ $(srcdir)/testwm2.c $(srcdir)/common.c $(CFLAGS) $(LIBS)
threadwin$(EXE): $(srcdir)/threadwin.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)
torturethread$(EXE): $(srcdir)/torturethread.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)

View file

@ -12,7 +12,6 @@
/* A simple test program framework */
#define SDL_NO_COMPAT
#include "SDL.h"
#ifdef __NDS__

View file

@ -1,552 +0,0 @@
/*
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
/* Simple program: Fill a colormap with gray and stripe it down the screen,
Then move an alpha valued sprite around the screen.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "SDL.h"
#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)
{
SDL_Quit();
exit(rc);
}
/* Fill the screen with a gradient */
static void
FillBackground(SDL_Surface * screen)
{
Uint8 *buffer;
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;
switch (screen->format->BytesPerPixel) {
case 1:
case 3:
for (i = 0; i < screen->h; ++i) {
memset(buffer, (i * 255) / screen->h,
screen->w * screen->format->BytesPerPixel);
buffer += screen->pitch;
}
break;
case 2:
for (i = 0; i < screen->h; ++i) {
Uint16 *buffer16;
Uint16 color;
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++ = color;
}
buffer += screen->pitch;
}
break;
case 4:
for (i = 0; i < screen->h; ++i) {
Uint32 *buffer32;
Uint32 color;
gradient = ((i * 255) / screen->h);
color = SDL_MapRGB(screen->format, gradient, gradient, gradient);
buffer32 = (Uint32 *) buffer;
for (k = 0; k < screen->w; k++) {
*buffer32++ = color;
}
buffer += screen->pitch;
}
break;
}
SDL_UnlockSurface(screen);
SDL_UpdateRect(screen, 0, 0, 0, 0);
}
/* Create a "light" -- a yellowish surface with variable alpha */
SDL_Surface *
CreateLight(int radius)
{
Uint8 trans, alphamask;
int range, addition;
int xdist, ydist;
Uint16 x, y;
Uint16 skip;
Uint32 pixel;
SDL_Surface *light;
#ifdef LIGHT_16BIT
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);
#else
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);
}
#endif
/* Fill with a light yellow-orange color */
skip = light->pitch - (light->w * light->format->BytesPerPixel);
#ifdef LIGHT_16BIT
buf = (Uint16 *) light->pixels;
#else
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... */
}
/* Calculate alpha values for the surface. */
#ifdef LIGHT_16BIT
buf = (Uint16 *) light->pixels;
#else
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);
/* 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);
/* 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)
{
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;
/* 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;
int
LoadSprite(SDL_Surface * screen, char *file)
{
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);
}
/* 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;
/* 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;
/* 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);
}
void
AttractSprite(Uint16 x, Uint16 y)
{
x_vel = ((int) x - position.x) / 10;
y_vel = ((int) y - position.y) / 10;
}
void
MoveSprite(SDL_Surface * screen, SDL_Surface * light)
{
SDL_Rect updates[2];
Uint8 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;
}
/* 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;
}
/* Update transparency (fade in and out) */
SDL_GetSurfaceAlphaMod(sprite, &alpha);
if (((int) alpha + alpha_vel) < 0) {
alpha_vel = -alpha_vel;
} else if (((int) alpha + alpha_vel) > 255) {
alpha_vel = -alpha_vel;
}
SDL_SetAlpha(sprite, SDL_SRCALPHA, (Uint8) (alpha + alpha_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]);
/* Make it so! */
SDL_UpdateRects(screen, 2, updates);
}
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;
/* 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 */
#ifdef _WIN32_WCE
/* Pocket PC */
w = 240;
h = 320;
#else
w = 640;
h = 480;
#endif
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");
}
} 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);
/* 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);
}
/* 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");
}
/* 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);
}
/* 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));
#endif
SDL_Delay(FRAME_TICKS - (ticks - lastticks));
#ifdef CHECK_SLEEP_GRANULARITY
fprintf(stderr, "Slept %d ticks\n", (SDL_GetTicks() - ticks));
#endif
}
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;
area.x = event.button.x - 16;
area.y = event.button.y - 16;
area.w = 32;
area.h = 32;
SDL_FillRect(screen, &area,
SDL_MapRGB(screen->format, 0, 0, 0));
SDL_UpdateRects(screen, 1, &area);
}
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE) {
done = 1;
}
if (event.key.keysym.sym == SDLK_RETURN) {
SDL_WM_ToggleFullScreen(screen);
}
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);
}
SDL_Quit();
return (0);
}

View file

@ -1,203 +0,0 @@
/*
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
/* Simple program: Test bitmap blits */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SDL.h"
#include "picture.xbm"
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void
quit(int rc)
{
SDL_Quit();
exit(rc);
}
SDL_Surface *
LoadXBM(SDL_Surface * screen, int w, int h, Uint8 * bits)
{
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);
}
/* 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];
int video_w, video_h;
/* 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);
}
}
#ifdef __IPHONEOS__
video_w = 320;
video_h = 480;
#else
video_w = 640;
video_h = 480;
#endif
/* Set 640x480 video mode */
if ((screen =
SDL_SetVideoMode(video_w, video_h, video_bpp, videoflags)) == NULL) {
fprintf(stderr, "Couldn't set %dx%d %d video mode: %s\n", video_w,
video_h, 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);
}

View file

@ -1,433 +0,0 @@
/*
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
/*
* Benchmarks surface-to-surface blits in various formats.
*
* Written by Ryan C. Gordon.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SDL.h"
static SDL_Surface *dest = NULL;
static SDL_Surface *src = NULL;
static int testSeconds = 10;
static int
percent(int val, int total)
{
return ((int) ((((float) val) / ((float) total)) * 100.0f));
}
static int
randRange(int lo, int hi)
{
return (lo + (int) (((double) hi) * rand() / (RAND_MAX + 1.0)));
}
static void
copy_trunc_str(char *str, size_t strsize, const char *flagstr)
{
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)
{
if (_surface->flags & flag)
copy_trunc_str(str, strsize, flagstr);
}
#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)
{
const SDL_VideoInfo *info = SDL_GetVideoInfo();
printf("SDL_GetVideoInfo():\n");
if (info == NULL)
printf(" (null.)\n");
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 color fills", info->blit_fill);
printf(" video memory: (%d)\n", info->video_mem);
}
printf("\n");
}
static void
output_surface_details(const char *name, SDL_Surface * surface)
{
printf("Details for %s:\n", name);
if (surface == NULL) {
printf("-WARNING- You've got a NULL surface!");
} 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(" pitch : %d\n", (int) surface->pitch);
printf(" red bits : 0x%08X mask, %d shift, %d loss\n",
(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);
printf(" blue bits : 0x%08X mask, %d shift, %d loss\n",
(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);
f[0] = '\0';
/*append_sdl_surface_flag(surface, f, sizeof (f), SDL_SWSURFACE); */
if ((surface->flags & SDL_HWSURFACE) == 0)
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_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)");
printf(" flags :%s\n", f);
}
printf("\n");
}
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)
{
Uint32 start = 0;
SDL_Rect srcRect;
SDL_Rect dstRect;
srcRect.x = 0;
srcRect.y = 0;
dstRect.x = x;
dstRect.y = y;
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);
}
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)
{
if (str == NULL)
return 0;
if (strlen(str) > 2) {
int retval = 0;
if ((str[0] == '0') && (str[1] == 'x'))
sscanf(str + 2, "%X", &retval);
return (retval);
}
return (atoi(str));
}
static int
setup_test(int argc, char **argv)
{
const char *dumpfile = NULL;
SDL_Surface *bmp = NULL;
Uint32 dstbpp = 32;
Uint32 dstrmask = 0x00FF0000;
Uint32 dstgmask = 0x0000FF00;
Uint32 dstbmask = 0x000000FF;
Uint32 dstamask = 0x00000000;
Uint32 dstflags = 0;
int dstw = 640;
int dsth = 480;
Uint32 srcbpp = 32;
Uint32 srcrmask = 0x00FF0000;
Uint32 srcgmask = 0x0000FF00;
Uint32 srcbmask = 0x000000FF;
Uint32 srcamask = 0x00000000;
Uint32 srcflags = 0;
int srcw = 640;
int srch = 480;
Uint32 origsrcalphaflags = 0;
Uint32 origdstalphaflags = 0;
Uint32 srcalphaflags = 0;
Uint32 dstalphaflags = 0;
Uint8 origsrcalpha = 255;
Uint8 origdstalpha = 255;
Uint8 srcalpha = 255;
Uint8 dstalpha = 255;
int screenSurface = 0;
int i = 0;
for (i = 1; i < argc; i++) {
const char *arg = argv[i];
if (strcmp(arg, "--dstbpp") == 0)
dstbpp = atoi(argv[++i]);
else if (strcmp(arg, "--dstrmask") == 0)
dstrmask = atoi_hex(argv[++i]);
else if (strcmp(arg, "--dstgmask") == 0)
dstgmask = atoi_hex(argv[++i]);
else if (strcmp(arg, "--dstbmask") == 0)
dstbmask = atoi_hex(argv[++i]);
else if (strcmp(arg, "--dstamask") == 0)
dstamask = atoi_hex(argv[++i]);
else if (strcmp(arg, "--dstwidth") == 0)
dstw = atoi(argv[++i]);
else if (strcmp(arg, "--dstheight") == 0)
dsth = atoi(argv[++i]);
else if (strcmp(arg, "--dsthwsurface") == 0)
dstflags |= SDL_HWSURFACE;
else if (strcmp(arg, "--srcbpp") == 0)
srcbpp = atoi(argv[++i]);
else if (strcmp(arg, "--srcrmask") == 0)
srcrmask = atoi_hex(argv[++i]);
else if (strcmp(arg, "--srcgmask") == 0)
srcgmask = atoi_hex(argv[++i]);
else if (strcmp(arg, "--srcbmask") == 0)
srcbmask = atoi_hex(argv[++i]);
else if (strcmp(arg, "--srcamask") == 0)
srcamask = atoi_hex(argv[++i]);
else if (strcmp(arg, "--srcwidth") == 0)
srcw = atoi(argv[++i]);
else if (strcmp(arg, "--srcheight") == 0)
srch = atoi(argv[++i]);
else if (strcmp(arg, "--srchwsurface") == 0)
srcflags |= SDL_HWSURFACE;
else if (strcmp(arg, "--seconds") == 0)
testSeconds = atoi(argv[++i]);
else if (strcmp(arg, "--screen") == 0)
screenSurface = 1;
else if (strcmp(arg, "--dumpfile") == 0)
dumpfile = argv[++i];
/* !!! FIXME: set colorkey. */
else if (0) { /* !!! FIXME: we handle some commandlines elsewhere now */
fprintf(stderr, "Unknown commandline option: %s\n", arg);
return (0);
}
}
if (SDL_Init(SDL_INIT_VIDEO) == -1) {
fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
return (0);
}
bmp = SDL_LoadBMP("sample.bmp");
if (bmp == NULL) {
fprintf(stderr, "SDL_LoadBMP failed: %s\n", SDL_GetError());
SDL_Quit();
return (0);
}
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 {
dest = SDL_CreateRGBSurface(dstflags, dstw, dsth, dstbpp,
dstrmask, dstgmask, dstbmask, dstamask);
}
if (dest == NULL) {
fprintf(stderr, "dest surface creation failed: %s\n", SDL_GetError());
SDL_Quit();
return (0);
}
src = SDL_CreateRGBSurface(srcflags, srcw, srch, srcbpp,
srcrmask, srcgmask, srcbmask, srcamask);
if (src == NULL) {
fprintf(stderr, "src surface creation failed: %s\n", SDL_GetError());
SDL_Quit();
return (0);
}
/* handle alpha settings... */
srcalphaflags = (src->flags & SDL_SRCALPHA) | (src->flags & SDL_RLEACCEL);
dstalphaflags =
(dest->flags & SDL_SRCALPHA) | (dest->flags & SDL_RLEACCEL);
origsrcalphaflags = srcalphaflags;
origdstalphaflags = dstalphaflags;
SDL_GetSurfaceAlphaMod(src, &srcalpha);
SDL_GetSurfaceAlphaMod(dest, &dstalpha);
origsrcalpha = srcalpha;
origdstalpha = dstalpha;
for (i = 1; i < argc; i++) {
const char *arg = argv[i];
if (strcmp(arg, "--srcalpha") == 0)
srcalpha = atoi(argv[++i]);
else if (strcmp(arg, "--dstalpha") == 0)
dstalpha = atoi(argv[++i]);
else if (strcmp(arg, "--srcsrcalpha") == 0)
srcalphaflags |= SDL_SRCALPHA;
else if (strcmp(arg, "--srcnosrcalpha") == 0)
srcalphaflags &= ~SDL_SRCALPHA;
else if (strcmp(arg, "--srcrleaccel") == 0)
srcalphaflags |= SDL_RLEACCEL;
else if (strcmp(arg, "--srcnorleaccel") == 0)
srcalphaflags &= ~SDL_RLEACCEL;
else if (strcmp(arg, "--dstsrcalpha") == 0)
dstalphaflags |= SDL_SRCALPHA;
else if (strcmp(arg, "--dstnosrcalpha") == 0)
dstalphaflags &= ~SDL_SRCALPHA;
else if (strcmp(arg, "--dstrleaccel") == 0)
dstalphaflags |= SDL_RLEACCEL;
else if (strcmp(arg, "--dstnorleaccel") == 0)
dstalphaflags &= ~SDL_RLEACCEL;
}
if ((dstalphaflags != origdstalphaflags) || (origdstalpha != dstalpha))
SDL_SetAlpha(dest, dstalphaflags, dstalpha);
if ((srcalphaflags != origsrcalphaflags) || (origsrcalpha != srcalpha))
SDL_SetAlpha(src, srcalphaflags, srcalpha);
/* set some sane defaults so we can see if the blit code is broken... */
SDL_FillRect(dest, NULL, SDL_MapRGB(dest->format, 0, 0, 0));
SDL_FillRect(src, NULL, SDL_MapRGB(src->format, 0, 0, 0));
blitCentered(src, bmp);
SDL_FreeSurface(bmp);
if (dumpfile)
SDL_SaveBMP(src, dumpfile); /* make sure initial convert is sane. */
output_details();
return (1);
}
static void
test_blit_speed(void)
{
Uint32 clearColor = SDL_MapRGB(dest->format, 0, 0, 0);
Uint32 iterations = 0;
Uint32 elasped = 0;
Uint32 end = 0;
Uint32 now = 0;
Uint32 last = 0;
int testms = testSeconds * 1000;
int wmax = (dest->w - src->w);
int hmax = (dest->h - src->h);
int isScreen = (SDL_GetVideoSurface() == dest);
SDL_Event event;
printf("Testing blit speed for %d seconds...\n", testSeconds);
now = SDL_GetTicks();
end = now + testms;
do {
/* pump the event queue occasionally to keep OS happy... */
if (now - last > 1000) {
last = now;
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! */
}
now = SDL_GetTicks();
} while (now < end);
printf("Non-blitting crap accounted for %d percent of this run.\n",
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
main(int argc, char **argv)
{
int initialized = setup_test(argc, argv);
if (initialized) {
test_blit_speed();
SDL_Quit();
}
return (!initialized);
}
/* end of testblitspeed.c ... */

View file

@ -1,260 +0,0 @@
/*
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
#include <stdio.h>
#include <stdlib.h>
#include "SDL.h"
/* This is an example 16x16 cursor
top left : black
top right : inverted color or black
bottom left: white
bottom right: transparent
(swap left and right for different endianness)
*/
Uint16 cursor_data[16] = {
0xffff,
0xffff,
0xffff,
0xffff,
0xffff,
0xffff,
0xffff,
0xffff,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000
};
Uint16 cursor_mask[16] = {
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 };
/* 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"
};
static SDL_Cursor *
create_arrow_cursor()
{
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;
}
}
}
SDL_sscanf(arrow[4 + row], "%d,%d", &hot_x, &hot_y);
return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
}
SDL_Surface *
LoadSprite(char *file)
{
SDL_Surface *sprite;
/* Load the sprite image */
sprite = SDL_LoadBMP(file);
if (sprite == NULL) {
fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
return NULL;
}
/* Set transparent pixel as the pixel at (0,0) */
if (sprite->format->palette) {
SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY | SDL_RLEACCEL),
*(Uint8 *) sprite->pixels);
}
/* We're ready to roll. :) */
return sprite;
}
int
main(int argc, char *argv[])
{
SDL_Surface *screen;
SDL_bool quit = SDL_FALSE, first_time = SDL_TRUE;
SDL_Cursor *cursor[5];
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);
}
screen = SDL_SetVideoMode(640, 480, 8, SDL_ANYFORMAT);
if (screen == NULL) {
fprintf(stderr, "Couldn't initialize video mode: %s\n",
SDL_GetError());
return (1);
}
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[3] = SDL_CreateColorCursor(LoadSprite("icon.bmp"), 0, 0);
cursor[4] = SDL_GetCursor();
current = SDL_arraysize(cursor)-1;
SDL_SetCursor(cursor[current]);
while (!quit) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_MOUSEBUTTONDOWN:
current = (current + 1) % SDL_arraysize(cursor);
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);
}
for (current = 0; current < SDL_arraysize(cursor); ++current) {
SDL_FreeCursor(cursor[current]);
}
SDL_Quit();
return (0);
}
/* vi: set ts=4 sw=4 expandtab: */

View file

@ -1,221 +0,0 @@
/*
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
/*
* Small SDL example to demonstrate dynamically loading
* OpenGL lib and functions
*
* (FYI it was supposed to look like snow in the wind or something...)
*
* Compile with :
* gcc testdyngl.c `sdl-config --libs --cflags` -o testdyngl -DHAVE_OPENGL
*
* You can specify a different OpenGL lib on the command line, i.e. :
* ./testdyngl /usr/X11R6/lib/libGL.so.1.2
* or
* ./testdyngl /usr/lib/libGL.so.1.0.4496
*
*/
#include <stdio.h>
#include <stdlib.h>
#include "SDL.h"
#ifdef __MACOS__
#define HAVE_OPENGL
#endif
#ifdef HAVE_OPENGL
#include "SDL_opengl.h"
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void
quit(int rc)
{
SDL_Quit();
exit(rc);
}
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;
}
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);
const GLubyte* (APIENTRY * glGetString) (GLenum);
}
glfuncs;
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->glGetString = get_funcaddr("glGetString");
printf("Vendor : %s\n", f->glGetString(GL_VENDOR));
printf("Renderer : %s\n", f->glGetString(GL_RENDERER));
printf("Version : %s\n", f->glGetString(GL_VERSION));
printf("Extensions : %s\n", f->glGetString(GL_EXTENSIONS));
}
#define NB_PIXELS 1000
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 */
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 (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_SetVideoMode(640, 480, 0, SDL_OPENGL) == NULL) {
printf("Unable to open video mode : %s\n", SDL_GetError());
quit(1);
}
/* Set the window manager title bar */
SDL_WM_SetCaption("SDL Dynamic OpenGL Loading Test", "testdyngl");
init_glfuncs(&f);
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[])
{
printf("No OpenGL support on this system\n");
return 1;
}
#endif /* HAVE_OPENGL */

View file

@ -1,226 +0,0 @@
/*
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
/*
* Small SDL example to demonstrate dynamically loading
* OpenGL lib and functions
*
* (FYI it was supposed to look like snow in the wind or something...)
*
* Compile with :
* gcc testdyngl.c `sdl-config --libs --cflags` -o testdyngl -DHAVE_OPENGL
*
* You can specify a different OpenGL lib on the command line, i.e. :
* ./testdyngl /usr/X11R6/lib/libGL.so.1.2
* or
* ./testdyngl /usr/lib/libGL.so.1.0.4496
*
*/
#include <stdio.h>
#include <stdlib.h>
#include "SDL.h"
#ifdef __IPHONEOS__
#define HAVE_OPENGLES
#endif
#ifdef HAVE_OPENGLES
#include "SDL_opengles.h"
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void
quit(int rc)
{
SDL_Quit();
exit(rc);
}
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;
}
typedef struct
{
void (APIENTRY * glEnableClientState) (GLenum array);
void (APIENTRY * glDisableClientState) (GLenum array);
void (APIENTRY * glVertexPointer) (GLint size, GLenum type,
GLsizei stride,
const GLvoid * pointer);
void (APIENTRY * glDrawArrays) (GLenum mode, GLint first, GLsizei count);
void (APIENTRY * glClearColor) (GLfloat, GLfloat, GLfloat, GLfloat);
void (APIENTRY * glClear) (GLbitfield);
void (APIENTRY * glDisable) (GLenum);
void (APIENTRY * glEnable) (GLenum);
void (APIENTRY * glColor4f) (GLfloat, GLfloat, GLfloat, GLfloat);
void (APIENTRY * glPointSize) (GLfloat);
void (APIENTRY * glHint) (GLenum, GLenum);
void (APIENTRY * glBlendFunc) (GLenum, GLenum);
void (APIENTRY * glMatrixMode) (GLenum);
void (APIENTRY * glLoadIdentity) ();
void (APIENTRY * glOrthof) (GLfloat, GLfloat, GLfloat, GLfloat,
GLfloat, GLfloat);
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)
{
f->glEnableClientState = get_funcaddr("glEnableClientState");
f->glDisableClientState = get_funcaddr("glDisableClientState");
f->glVertexPointer = get_funcaddr("glVertexPointer");
f->glDrawArrays = get_funcaddr("glDrawArrays");
f->glClearColor = get_funcaddr("glClearColor");
f->glClear = get_funcaddr("glClear");
f->glDisable = get_funcaddr("glDisable");
f->glEnable = get_funcaddr("glEnable");
f->glColor4f = get_funcaddr("glColor4f");
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->glOrthof = get_funcaddr("glOrthof");
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[])
{
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 */
int video_w, video_h;
/* you may want to change these according to the platform */
video_w = 320;
video_h = 480;
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 (SDL_GL_LoadLibrary(gl_library) < 0) {
printf("Unable to dynamically open GL ES lib : %s\n", SDL_GetError());
quit(1);
}
if (SDL_SetVideoMode(video_h, video_w, 0, SDL_OPENGL) == NULL) {
printf("Unable to open video mode : %s\n", SDL_GetError());
quit(1);
}
/* Set the window manager title bar */
SDL_WM_SetCaption("SDL Dynamic OpenGL ES Loading Test", "testdyngles");
init_glfuncs(&f);
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, video_w, video_h);
f.glMatrixMode(GL_PROJECTION);
f.glLoadIdentity();
f.glOrthof(-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(1.0f);
f.glEnable(GL_FOG);
f.glFogf(GL_FOG_START, -500);
f.glFogf(GL_FOG_END, 500);
f.glFogf(GL_FOG_DENSITY, 0.005);
f.glVertexPointer(3, GL_FLOAT, 0, pixels);
f.glEnableClientState(GL_VERTEX_ARRAY);
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.glColor4f(1.0, 1.0, 1.0, 1.0);
f.glDrawArrays(GL_POINTS, 0, NB_PIXELS);
SDL_GL_SwapBuffers();
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
done = 1;
if (event.type == SDL_KEYDOWN)
done = 1;
}
SDL_Delay(20);
}
while (!done);
f.glDisableClientState(GL_VERTEX_ARRAY);
SDL_Quit();
return 0;
}
#else /* HAVE_OPENGLES */
int
main(int argc, char *argv[])
{
printf("No OpenGL ES support on this system\n");
return 1;
}
#endif /* HAVE_OPENGLES */

View file

@ -1,118 +0,0 @@
/*
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
/* Simple program: Fill the screen with colors as fast as possible */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "SDL.h"
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void
quit(int rc)
{
SDL_Quit();
exit(rc);
}
int
main(int argc, char *argv[])
{
SDL_Surface *screen;
int width, height;
Uint8 video_bpp;
Uint32 videoflags;
Uint32 colors[3];
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);
}
width = 640;
height = 480;
video_bpp = 8;
videoflags = 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]);
--argc;
} else if (strcmp(argv[argc], "-fullscreen") == 0) {
videoflags ^= SDL_FULLSCREEN;
} else {
fprintf(stderr,
"Usage: %s [-width N] [-height N] [-bpp N] [-fullscreen]\n",
argv[0]);
quit(1);
}
}
/* Set video mode */
screen = SDL_SetVideoMode(width, height, video_bpp, 0);
if (!screen) {
fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
width, height, SDL_GetError());
quit(2);
}
/* Get the colors */
colors[0] = SDL_MapRGB(screen->format, 0xFF, 0x00, 0x00);
colors[1] = SDL_MapRGB(screen->format, 0x00, 0xFF, 0x00);
colors[2] = SDL_MapRGB(screen->format, 0x00, 0x00, 0xFF);
/* Loop, filling 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_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;
}
}
SDL_FillRect(screen, NULL, colors[frames%3]);
SDL_Flip(screen);
}
/* Print out some timing information */
now = SDL_GetTicks();
if (now > then) {
double fps = ((double) frames * 1000) / (now - then);
printf("%2.2f frames per second\n", fps);
}
SDL_Quit();
return (0);
}

View file

@ -1,179 +0,0 @@
/*
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
/* Bring up a window and manipulate the gamma on it */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "SDL.h"
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void
quit(int rc)
{
SDL_Quit();
exit(rc);
}
/* 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 i;
*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;
}
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;
/* 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 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 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);
}
/* 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);
}
/* 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;
}
}
}
/* Perform a gamma flash to red using color ramps */
while (gamma < 10.0) {
/* Increase the red gamma and decrease everything else... */
gamma += 0.1f;
SDL_CalculateGammaRamp(gamma, red_ramp);
SDL_CalculateGammaRamp(1.0f / 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);
SDL_Quit();
return (0);
}

View file

@ -1,782 +0,0 @@
/*
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "SDL.h"
#ifdef __MACOS__
#define HAVE_OPENGL
#endif
#ifdef HAVE_OPENGL
#include "SDL_opengl.h"
/* Undefine this if you want a flat cube instead of a rainbow cube */
#define SHADED_CUBE
/* Define this to be the name of the logo image to use with -logo */
#define LOGO_FILE "icon.bmp"
static SDL_Surface *global_image = NULL;
static GLuint global_texture = 0;
static GLuint cursor_texture = 0;
/**********************************************************************/
void
HotKey_ToggleFullScreen(void)
{
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");
}
}
void
HotKey_ToggleGrab(void)
{
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);
if (mode == SDL_GRAB_ON) {
printf("Grab is now on\n");
} else {
printf("Grab is now off\n");
}
}
void
HotKey_Iconify(void)
{
printf("Ctrl-Z: iconifying window!\n");
SDL_WM_IconifyWindow();
}
int
HandleEvent(SDL_Event * event)
{
int done;
done = 0;
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 ");
} else if (event->active.state & SDL_APPMOUSEFOCUS) {
printf("mouse ");
} else if (event->active.state & SDL_APPINPUTFOCUS) {
printf("input ");
}
printf("focus\n");
break;
case SDL_KEYDOWN:
if (event->key.keysym.sym == SDLK_ESCAPE) {
done = 1;
}
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();
}
printf("key '%s' pressed\n", SDL_GetKeyName(event->key.keysym.sym));
break;
case SDL_QUIT:
done = 1;
break;
}
return (done);
}
void
SDL_GL_Enter2DMode()
{
SDL_Surface *screen = SDL_GetVideoSurface();
/* Note, there may be other things you need to change,
depending on how you have your OpenGL state set up.
*/
glPushAttrib(GL_ENABLE_BIT);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);
/* This allows alpha blending of 2D textures with the scene */
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glViewport(0, 0, screen->w, screen->h);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0, (GLdouble) screen->w, (GLdouble) screen->h, 0.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
}
void
SDL_GL_Leave2DMode()
{
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glPopAttrib();
}
/* Quick utility function for texture creation */
static int
power_of_two(int input)
{
int value = 1;
while (value < input) {
value <<= 1;
}
return value;
}
GLuint
SDL_GL_LoadTexture(SDL_Surface * surface, GLfloat * texcoord)
{
GLuint texture;
int w, h;
SDL_Surface *image;
SDL_Rect area;
Uint32 saved_flags;
Uint8 saved_alpha;
/* Use the surface width and height expanded to powers of 2 */
w = power_of_two(surface->w);
h = power_of_two(surface->h);
texcoord[0] = 0.0f; /* Min X */
texcoord[1] = 0.0f; /* Min Y */
texcoord[2] = (GLfloat) surface->w / w; /* Max X */
texcoord[3] = (GLfloat) surface->h / h; /* Max Y */
image = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
0x000000FF,
0x0000FF00, 0x00FF0000, 0xFF000000
#else
0xFF000000,
0x00FF0000, 0x0000FF00, 0x000000FF
#endif
);
if (image == NULL) {
return 0;
}
/* Save the alpha blending attributes */
saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
SDL_GetSurfaceAlphaMod(surface, &saved_alpha);
if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
SDL_SetAlpha(surface, 0, 0);
}
/* Copy the surface into the GL texture image */
area.x = 0;
area.y = 0;
area.w = surface->w;
area.h = surface->h;
SDL_BlitSurface(surface, &area, image, &area);
/* Restore the alpha blending attributes */
if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
SDL_SetAlpha(surface, saved_flags, saved_alpha);
}
/* Create an OpenGL texture for the image */
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
SDL_FreeSurface(image); /* No longer needed */
return texture;
}
void
DrawLogoCursor(void)
{
static GLfloat texMinX, texMinY;
static GLfloat texMaxX, texMaxY;
static int w, h;
int x, y;
if (!cursor_texture) {
SDL_Surface *image;
GLfloat texcoord[4];
/* Load the image (could use SDL_image library here) */
image = SDL_LoadBMP(LOGO_FILE);
if (image == NULL) {
return;
}
w = image->w;
h = image->h;
/* Convert the image into an OpenGL texture */
cursor_texture = SDL_GL_LoadTexture(image, texcoord);
/* Make texture coordinates easy to understand */
texMinX = texcoord[0];
texMinY = texcoord[1];
texMaxX = texcoord[2];
texMaxY = texcoord[3];
/* We don't need the original image anymore */
SDL_FreeSurface(image);
/* Make sure that the texture conversion is okay */
if (!cursor_texture) {
return;
}
}
/* Move the image around */
SDL_GetMouseState(&x, &y);
x -= w / 2;
y -= h / 2;
/* Show the image on the screen */
SDL_GL_Enter2DMode();
glBindTexture(GL_TEXTURE_2D, cursor_texture);
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(texMinX, texMinY);
glVertex2i(x, y);
glTexCoord2f(texMaxX, texMinY);
glVertex2i(x + w, y);
glTexCoord2f(texMinX, texMaxY);
glVertex2i(x, y + h);
glTexCoord2f(texMaxX, texMaxY);
glVertex2i(x + w, y + h);
glEnd();
SDL_GL_Leave2DMode();
}
void
DrawLogoTexture(void)
{
static GLfloat texMinX, texMinY;
static GLfloat texMaxX, texMaxY;
static int x = 0;
static int y = 0;
static int w, h;
static int delta_x = 1;
static int delta_y = 1;
SDL_Surface *screen = SDL_GetVideoSurface();
if (!global_texture) {
SDL_Surface *image;
GLfloat texcoord[4];
/* Load the image (could use SDL_image library here) */
image = SDL_LoadBMP(LOGO_FILE);
if (image == NULL) {
return;
}
w = image->w;
h = image->h;
/* Convert the image into an OpenGL texture */
global_texture = SDL_GL_LoadTexture(image, texcoord);
/* Make texture coordinates easy to understand */
texMinX = texcoord[0];
texMinY = texcoord[1];
texMaxX = texcoord[2];
texMaxY = texcoord[3];
/* We don't need the original image anymore */
SDL_FreeSurface(image);
/* Make sure that the texture conversion is okay */
if (!global_texture) {
return;
}
}
/* Move the image around */
x += delta_x;
if (x < 0) {
x = 0;
delta_x = -delta_x;
} else if ((x + w) > screen->w) {
x = screen->w - w;
delta_x = -delta_x;
}
y += delta_y;
if (y < 0) {
y = 0;
delta_y = -delta_y;
} else if ((y + h) > screen->h) {
y = screen->h - h;
delta_y = -delta_y;
}
/* Show the image on the screen */
SDL_GL_Enter2DMode();
glBindTexture(GL_TEXTURE_2D, global_texture);
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(texMinX, texMinY);
glVertex2i(x, y);
glTexCoord2f(texMaxX, texMinY);
glVertex2i(x + w, y);
glTexCoord2f(texMinX, texMaxY);
glVertex2i(x, y + h);
glTexCoord2f(texMaxX, texMaxY);
glVertex2i(x + w, y + h);
glEnd();
SDL_GL_Leave2DMode();
}
int
RunGLTest(int argc, char *argv[],
int logo, int logocursor, int slowly, int bpp, float gamma,
int noframe, int fsaa, int sync, int accel)
{
int i;
int rgb_size[3];
int w = 640;
int h = 480;
int done = 0;
int frames;
Uint32 start_time, this_time;
float color[8][3] = { {1.0, 1.0, 0.0},
{1.0, 0.0, 0.0},
{0.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
{0.0, 1.0, 1.0},
{1.0, 1.0, 1.0},
{1.0, 0.0, 1.0},
{0.0, 0.0, 1.0}
};
float cube[8][3] = { {0.5, 0.5, -0.5},
{0.5, -0.5, -0.5},
{-0.5, -0.5, -0.5},
{-0.5, 0.5, -0.5},
{-0.5, 0.5, 0.5},
{0.5, 0.5, 0.5},
{0.5, -0.5, 0.5},
{-0.5, -0.5, 0.5}
};
Uint32 video_flags;
int value;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
}
/* See if we should detect the display depth */
if (bpp == 0) {
if (SDL_GetVideoInfo()->vfmt->BitsPerPixel <= 8) {
bpp = 8;
} else {
bpp = 16; /* More doesn't seem to work */
}
}
/* Set the flags we want to use for setting the video mode */
video_flags = SDL_OPENGL;
for (i = 1; argv[i]; ++i) {
if (strcmp(argv[i], "-fullscreen") == 0) {
video_flags |= SDL_FULLSCREEN;
}
}
if (noframe) {
video_flags |= SDL_NOFRAME;
}
/* Initialize the display */
switch (bpp) {
case 8:
rgb_size[0] = 3;
rgb_size[1] = 3;
rgb_size[2] = 2;
break;
case 15:
case 16:
rgb_size[0] = 5;
rgb_size[1] = 5;
rgb_size[2] = 5;
break;
default:
rgb_size[0] = 8;
rgb_size[1] = 8;
rgb_size[2] = 8;
break;
}
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, rgb_size[0]);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, rgb_size[1]);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, rgb_size[2]);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
if (fsaa) {
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, fsaa);
}
if (accel >= 0) {
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, accel);
}
if (SDL_SetVideoMode(w, h, bpp, video_flags) == NULL) {
fprintf(stderr, "Couldn't set GL mode: %s\n", SDL_GetError());
SDL_Quit();
exit(1);
}
if (sync) {
SDL_GL_SetSwapInterval(1);
} else {
SDL_GL_SetSwapInterval(0);
}
printf("Screen BPP: %d\n", SDL_GetVideoSurface()->format->BitsPerPixel);
printf("\n");
printf("Vendor : %s\n", glGetString(GL_VENDOR));
printf("Renderer : %s\n", glGetString(GL_RENDERER));
printf("Version : %s\n", glGetString(GL_VERSION));
printf("Extensions : %s\n", glGetString(GL_EXTENSIONS));
printf("\n");
SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
printf("SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0], value);
SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
printf("SDL_GL_GREEN_SIZE: requested %d, got %d\n", rgb_size[1], value);
SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
printf("SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2], value);
SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
printf("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value);
SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &value);
printf("SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value);
if (fsaa) {
SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
printf("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
printf("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
value);
}
if (accel >= 0) {
SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
printf("SDL_GL_ACCELERATED_VISUAL: requested %d, got %d\n", accel,
value);
}
if (sync) {
printf("Buffer swap interval: requested 1, got %d\n",
SDL_GL_GetSwapInterval());
}
/* Set the window manager title bar */
SDL_WM_SetCaption("SDL GL test", "testgl");
/* Set the gamma for the window */
if (gamma != 0.0) {
SDL_SetGamma(gamma, gamma, gamma);
}
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glShadeModel(GL_SMOOTH);
/* Loop until done. */
start_time = SDL_GetTicks();
frames = 0;
while (!done) {
GLenum gl_error;
char *sdl_error;
SDL_Event event;
/* Do our drawing, too. */
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
#ifdef SHADED_CUBE
glColor3fv(color[0]);
glVertex3fv(cube[0]);
glColor3fv(color[1]);
glVertex3fv(cube[1]);
glColor3fv(color[2]);
glVertex3fv(cube[2]);
glColor3fv(color[3]);
glVertex3fv(cube[3]);
glColor3fv(color[3]);
glVertex3fv(cube[3]);
glColor3fv(color[4]);
glVertex3fv(cube[4]);
glColor3fv(color[7]);
glVertex3fv(cube[7]);
glColor3fv(color[2]);
glVertex3fv(cube[2]);
glColor3fv(color[0]);
glVertex3fv(cube[0]);
glColor3fv(color[5]);
glVertex3fv(cube[5]);
glColor3fv(color[6]);
glVertex3fv(cube[6]);
glColor3fv(color[1]);
glVertex3fv(cube[1]);
glColor3fv(color[5]);
glVertex3fv(cube[5]);
glColor3fv(color[4]);
glVertex3fv(cube[4]);
glColor3fv(color[7]);
glVertex3fv(cube[7]);
glColor3fv(color[6]);
glVertex3fv(cube[6]);
glColor3fv(color[5]);
glVertex3fv(cube[5]);
glColor3fv(color[0]);
glVertex3fv(cube[0]);
glColor3fv(color[3]);
glVertex3fv(cube[3]);
glColor3fv(color[4]);
glVertex3fv(cube[4]);
glColor3fv(color[6]);
glVertex3fv(cube[6]);
glColor3fv(color[1]);
glVertex3fv(cube[1]);
glColor3fv(color[2]);
glVertex3fv(cube[2]);
glColor3fv(color[7]);
glVertex3fv(cube[7]);
#else /* flat cube */
glColor3f(1.0, 0.0, 0.0);
glVertex3fv(cube[0]);
glVertex3fv(cube[1]);
glVertex3fv(cube[2]);
glVertex3fv(cube[3]);
glColor3f(0.0, 1.0, 0.0);
glVertex3fv(cube[3]);
glVertex3fv(cube[4]);
glVertex3fv(cube[7]);
glVertex3fv(cube[2]);
glColor3f(0.0, 0.0, 1.0);
glVertex3fv(cube[0]);
glVertex3fv(cube[5]);
glVertex3fv(cube[6]);
glVertex3fv(cube[1]);
glColor3f(0.0, 1.0, 1.0);
glVertex3fv(cube[5]);
glVertex3fv(cube[4]);
glVertex3fv(cube[7]);
glVertex3fv(cube[6]);
glColor3f(1.0, 1.0, 0.0);
glVertex3fv(cube[5]);
glVertex3fv(cube[0]);
glVertex3fv(cube[3]);
glVertex3fv(cube[4]);
glColor3f(1.0, 0.0, 1.0);
glVertex3fv(cube[6]);
glVertex3fv(cube[1]);
glVertex3fv(cube[2]);
glVertex3fv(cube[7]);
#endif /* SHADED_CUBE */
glEnd();
glMatrixMode(GL_MODELVIEW);
glRotatef(5.0, 1.0, 1.0, 1.0);
/* Draw 2D logo onto the 3D display */
if (logo) {
DrawLogoTexture();
}
if (logocursor) {
DrawLogoCursor();
}
SDL_GL_SwapBuffers();
/* Check for error conditions. */
gl_error = glGetError();
if (gl_error != GL_NO_ERROR) {
fprintf(stderr, "testgl: OpenGL error: %d\n", gl_error);
}
sdl_error = (char *)SDL_GetError();
if (sdl_error[0] != '\0') {
fprintf(stderr, "testgl: SDL error '%s'\n", sdl_error);
SDL_ClearError();
}
/* Allow the user to see what's happening */
if (slowly) {
SDL_Delay(20);
}
/* Check if there's a pending event. */
while (SDL_PollEvent(&event)) {
done |= HandleEvent(&event);
}
++frames;
}
/* Print out the frames per second */
this_time = SDL_GetTicks();
if (this_time != start_time) {
printf("%2.2f FPS\n",
((float) frames / (this_time - start_time)) * 1000.0);
}
if (global_image) {
SDL_FreeSurface(global_image);
global_image = NULL;
}
if (global_texture) {
glDeleteTextures(1, &global_texture);
global_texture = 0;
}
if (cursor_texture) {
glDeleteTextures(1, &cursor_texture);
cursor_texture = 0;
}
/* Destroy our GL context, etc. */
SDL_Quit();
return (0);
}
int
main(int argc, char *argv[])
{
int i, logo, logocursor = 0;
int numtests;
int bpp = 0;
int slowly;
float gamma = 0.0;
int noframe = 0;
int fsaa = 0;
int accel = -1;
int sync = 0;
logo = 0;
slowly = 0;
numtests = 1;
for (i = 1; argv[i]; ++i) {
if (strcmp(argv[i], "-twice") == 0) {
++numtests;
}
if (strcmp(argv[i], "-logo") == 0) {
logo = 1;
}
if (strcmp(argv[i], "-logocursor") == 0) {
logocursor = 1;
}
if (strcmp(argv[i], "-slow") == 0) {
slowly = 1;
}
if (strcmp(argv[i], "-bpp") == 0) {
bpp = atoi(argv[++i]);
}
if (strcmp(argv[i], "-gamma") == 0) {
gamma = (float) atof(argv[++i]);
}
if (strcmp(argv[i], "-noframe") == 0) {
noframe = 1;
}
if (strcmp(argv[i], "-fsaa") == 0) {
++fsaa;
}
if (strcmp(argv[i], "-accel") == 0) {
accel = atoi(argv[++i]);
}
if (strcmp(argv[i], "-sync") == 0) {
++sync;
}
if (strncmp(argv[i], "-h", 2) == 0) {
printf
("Usage: %s [-twice] [-logo] [-logocursor] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa] [-accel n] [-sync] [-fullscreen]\n",
argv[0]);
exit(0);
}
}
for (i = 0; i < numtests; ++i) {
RunGLTest(argc, argv, logo, logocursor, slowly, bpp, gamma,
noframe, fsaa, sync, accel);
}
return 0;
}
#else /* HAVE_OPENGL */
int
main(int argc, char *argv[])
{
printf("No OpenGL support on this system\n");
return 1;
}
#endif /* HAVE_OPENGL */

View file

@ -11,6 +11,15 @@
*/
/* A simple program to test the Input Method support in the SDL library (2.0+) */
#if 1 /* FIXME: Rework this using the 2.0 API */
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("FIXME\n");
return 0;
}
#else
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@ -383,5 +392,6 @@ int main(int argc, char *argv[])
CleanupVideo();
return 0;
}
#endif
/* vi: set ts=4 sw=4 expandtab: */

View file

@ -12,6 +12,15 @@
/* Simple program to test the SDL joystick routines */
#if 1 /* FIXME: Rework this using the 2.0 API */
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("FIXME\n");
return 0;
}
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -202,3 +211,4 @@ main(int argc, char *argv[])
return (0);
}
#endif

View file

@ -62,7 +62,7 @@ LoadSprite(SDL_Renderer *renderer, char *file)
/* Set transparent pixel as the pixel at (0,0) */
if (temp->format->palette) {
SDL_SetColorKey(temp, SDL_SRCCOLORKEY, *(Uint8 *) temp->pixels);
SDL_SetColorKey(temp, 1, *(Uint8 *) temp->pixels);
}
/* Create textures from the image */

View file

@ -1,571 +0,0 @@
/*
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
/* Bring up a window and play with it */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define BENCHMARK_SDL
#define NOTICE(X) printf("%s", X);
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
#include "SDL.h"
SDL_Surface *screen, *pic;
SDL_Overlay *overlay;
int scale;
int monochrome;
int luminance;
int w, h;
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void
quit(int rc)
{
SDL_Quit();
exit(rc);
}
/* NOTE: These RGB conversion functions are not intended for speed,
only as examples.
*/
void
RGBtoYUV(Uint8 * rgb, int *yuv, int monochrome, int luminance)
{
if (monochrome) {
#if 1 /* these are the two formulas that I found on the FourCC site... */
yuv[0] = (int)(0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]);
yuv[1] = 128;
yuv[2] = 128;
#else
yuv[0] = (int)((0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16);
yuv[1] = 128;
yuv[2] = 128;
#endif
} else {
#if 1 /* these are the two formulas that I found on the FourCC site... */
yuv[0] = (int)(0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]);
yuv[1] = (int)((rgb[2] - yuv[0]) * 0.565 + 128);
yuv[2] = (int)((rgb[0] - yuv[0]) * 0.713 + 128);
#else
yuv[0] = (int)((0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16);
yuv[1] = (int)(128 - (0.148 * rgb[0]) - (0.291 * rgb[1]) + (0.439 * rgb[2]));
yuv[2] = (int)(128 + (0.439 * rgb[0]) - (0.368 * rgb[1]) - (0.071 * rgb[2]));
#endif
}
if (luminance != 100) {
yuv[0] = yuv[0] * luminance / 100;
if (yuv[0] > 255)
yuv[0] = 255;
}
/* clamp values...if you need to, we don't seem to have a need */
/*
for(i=0;i<3;i++)
{
if(yuv[i]<0)
yuv[i]=0;
if(yuv[i]>255)
yuv[i]=255;
}
*/
}
void
ConvertRGBtoYV12(SDL_Surface * s, SDL_Overlay * o, int monochrome,
int luminance)
{
int x, y;
int yuv[3];
Uint8 *p, *op[3];
SDL_LockSurface(s);
SDL_LockYUVOverlay(o);
/* Black initialization */
/*
memset(o->pixels[0],0,o->pitches[0]*o->h);
memset(o->pixels[1],128,o->pitches[1]*((o->h+1)/2));
memset(o->pixels[2],128,o->pitches[2]*((o->h+1)/2));
*/
/* Convert */
for (y = 0; y < s->h && y < o->h; y++) {
p = ((Uint8 *) s->pixels) + s->pitch * y;
op[0] = o->pixels[0] + o->pitches[0] * y;
op[1] = o->pixels[1] + o->pitches[1] * (y / 2);
op[2] = o->pixels[2] + o->pitches[2] * (y / 2);
for (x = 0; x < s->w && x < o->w; x++) {
RGBtoYUV(p, yuv, monochrome, luminance);
*(op[0]++) = yuv[0];
if (x % 2 == 0 && y % 2 == 0) {
*(op[1]++) = yuv[2];
*(op[2]++) = yuv[1];
}
p += s->format->BytesPerPixel;
}
}
SDL_UnlockYUVOverlay(o);
SDL_UnlockSurface(s);
}
void
ConvertRGBtoIYUV(SDL_Surface * s, SDL_Overlay * o, int monochrome,
int luminance)
{
int x, y;
int yuv[3];
Uint8 *p, *op[3];
SDL_LockSurface(s);
SDL_LockYUVOverlay(o);
/* Black initialization */
/*
memset(o->pixels[0],0,o->pitches[0]*o->h);
memset(o->pixels[1],128,o->pitches[1]*((o->h+1)/2));
memset(o->pixels[2],128,o->pitches[2]*((o->h+1)/2));
*/
/* Convert */
for (y = 0; y < s->h && y < o->h; y++) {
p = ((Uint8 *) s->pixels) + s->pitch * y;
op[0] = o->pixels[0] + o->pitches[0] * y;
op[1] = o->pixels[1] + o->pitches[1] * (y / 2);
op[2] = o->pixels[2] + o->pitches[2] * (y / 2);
for (x = 0; x < s->w && x < o->w; x++) {
RGBtoYUV(p, yuv, monochrome, luminance);
*(op[0]++) = yuv[0];
if (x % 2 == 0 && y % 2 == 0) {
*(op[1]++) = yuv[1];
*(op[2]++) = yuv[2];
}
p += s->format->BytesPerPixel;
}
}
SDL_UnlockYUVOverlay(o);
SDL_UnlockSurface(s);
}
void
ConvertRGBtoUYVY(SDL_Surface * s, SDL_Overlay * o, int monochrome,
int luminance)
{
int x, y;
int yuv[3];
Uint8 *p, *op;
SDL_LockSurface(s);
SDL_LockYUVOverlay(o);
for (y = 0; y < s->h && y < o->h; y++) {
p = ((Uint8 *) s->pixels) + s->pitch * y;
op = o->pixels[0] + o->pitches[0] * y;
for (x = 0; x < s->w && x < o->w; x++) {
RGBtoYUV(p, yuv, monochrome, luminance);
if (x % 2 == 0) {
*(op++) = yuv[1];
*(op++) = yuv[0];
*(op++) = yuv[2];
} else
*(op++) = yuv[0];
p += s->format->BytesPerPixel;
}
}
SDL_UnlockYUVOverlay(o);
SDL_UnlockSurface(s);
}
void
ConvertRGBtoYVYU(SDL_Surface * s, SDL_Overlay * o, int monochrome,
int luminance)
{
int x, y;
int yuv[3];
Uint8 *p, *op;
SDL_LockSurface(s);
SDL_LockYUVOverlay(o);
for (y = 0; y < s->h && y < o->h; y++) {
p = ((Uint8 *) s->pixels) + s->pitch * y;
op = o->pixels[0] + o->pitches[0] * y;
for (x = 0; x < s->w && x < o->w; x++) {
RGBtoYUV(p, yuv, monochrome, luminance);
if (x % 2 == 0) {
*(op++) = yuv[0];
*(op++) = yuv[2];
op[1] = yuv[1];
} else {
*op = yuv[0];
op += 2;
}
p += s->format->BytesPerPixel;
}
}
SDL_UnlockYUVOverlay(o);
SDL_UnlockSurface(s);
}
void
ConvertRGBtoYUY2(SDL_Surface * s, SDL_Overlay * o, int monochrome,
int luminance)
{
int x, y;
int yuv[3];
Uint8 *p, *op;
SDL_LockSurface(s);
SDL_LockYUVOverlay(o);
for (y = 0; y < s->h && y < o->h; y++) {
p = ((Uint8 *) s->pixels) + s->pitch * y;
op = o->pixels[0] + o->pitches[0] * y;
for (x = 0; x < s->w && x < o->w; x++) {
RGBtoYUV(p, yuv, monochrome, luminance);
if (x % 2 == 0) {
*(op++) = yuv[0];
*(op++) = yuv[1];
op[1] = yuv[2];
} else {
*op = yuv[0];
op += 2;
}
p += s->format->BytesPerPixel;
}
}
SDL_UnlockYUVOverlay(o);
SDL_UnlockSurface(s);
}
void
Draw()
{
SDL_Rect rect;
int i;
int disp;
if (!scale) {
rect.w = overlay->w;
rect.h = overlay->h;
for (i = 0; i < h - rect.h && i < w - rect.w; i++) {
rect.x = i;
rect.y = i;
SDL_DisplayYUVOverlay(overlay, &rect);
}
} else {
rect.w = overlay->w / 2;
rect.h = overlay->h / 2;
rect.x = (w - rect.w) / 2;
rect.y = (h - rect.h) / 2;
disp = rect.y - 1;
for (i = 0; i < disp; i++) {
rect.w += 2;
rect.h += 2;
rect.x--;
rect.y--;
SDL_DisplayYUVOverlay(overlay, &rect);
}
}
printf("Displayed %d times.\n", i);
}
static void
PrintUsage(char *argv0)
{
fprintf(stderr, "Usage: %s [arg] [arg] [arg] ...\n", argv0);
fprintf(stderr, "Where 'arg' is one of:\n");
fprintf(stderr, " -delay <seconds>\n");
fprintf(stderr, " -width <pixels>\n");
fprintf(stderr, " -height <pixels>\n");
fprintf(stderr, " -bpp <bits>\n");
fprintf(stderr,
" -format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
fprintf(stderr, " -hw\n");
fprintf(stderr, " -flip\n");
fprintf(stderr,
" -scale (test scaling features, from 50%% upto window size)\n");
fprintf(stderr, " -mono (use monochromatic RGB2YUV conversion)\n");
fprintf(stderr,
" -lum <perc> (use luminance correction during RGB2YUV conversion,\n");
fprintf(stderr,
" from 0%% to unlimited, normal is 100%%)\n");
fprintf(stderr, " -help (shows this help)\n");
fprintf(stderr, " -fullscreen (test overlay in fullscreen mode)\n");
}
int
main(int argc, char **argv)
{
char *argv0 = argv[0];
int flip;
int delay;
int desired_bpp;
Uint32 video_flags, overlay_format;
char *bmpfile;
#ifdef BENCHMARK_SDL
Uint32 then, now;
#endif
int i;
/* Set default options and check command-line */
flip = 0;
scale = 0;
monochrome = 0;
luminance = 100;
delay = 1;
w = WINDOW_WIDTH;
h = WINDOW_HEIGHT;
desired_bpp = 0;
video_flags = 0;
overlay_format = SDL_YV12_OVERLAY;
while (argc > 1) {
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");
return (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");
return (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");
return (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");
return (1);
}
} else if (strcmp(argv[1], "-lum") == 0) {
if (argv[2]) {
luminance = atoi(argv[2]);
argv += 2;
argc -= 2;
} else {
fprintf(stderr, "The -lum option requires an argument\n");
return (1);
}
} else if (strcmp(argv[1], "-format") == 0) {
if (argv[2]) {
if (!strcmp(argv[2], "YV12"))
overlay_format = SDL_YV12_OVERLAY;
else if (!strcmp(argv[2], "IYUV"))
overlay_format = SDL_IYUV_OVERLAY;
else if (!strcmp(argv[2], "YUY2"))
overlay_format = SDL_YUY2_OVERLAY;
else if (!strcmp(argv[2], "UYVY"))
overlay_format = SDL_UYVY_OVERLAY;
else if (!strcmp(argv[2], "YVYU"))
overlay_format = SDL_YVYU_OVERLAY;
else {
fprintf(stderr,
"The -format option %s is not recognized\n",
argv[2]);
return (1);
}
argv += 2;
argc -= 2;
} else {
fprintf(stderr, "The -format option requires an argument\n");
return (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], "-scale") == 0) {
scale = 1;
argv += 1;
argc -= 1;
} else if (strcmp(argv[1], "-mono") == 0) {
monochrome = 1;
argv += 1;
argc -= 1;
} else if ((strcmp(argv[1], "-help") == 0)
|| (strcmp(argv[1], "-h") == 0)) {
PrintUsage(argv0);
return (1);
} else if (strcmp(argv[1], "-fullscreen") == 0) {
video_flags |= SDL_FULLSCREEN;
argv += 1;
argc -= 1;
} else
break;
}
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
return (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 overlay", "testoverlay");
/* Load picture */
bmpfile = (argv[1] ? argv[1] : "sample.bmp");
pic = SDL_LoadBMP(bmpfile);
if (pic == NULL) {
fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError());
quit(1);
}
/* Convert the picture to 32bits, for easy conversion */
{
SDL_Surface *newsurf;
SDL_PixelFormat format;
format.palette = NULL;
format.BitsPerPixel = 32;
format.BytesPerPixel = 4;
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
format.Rshift = 0;
format.Gshift = 8;
format.Bshift = 16;
#else
format.Rshift = 24;
format.Gshift = 16;
format.Bshift = 8;
#endif
format.Ashift = 0;
format.Rmask = 0xff << format.Rshift;
format.Gmask = 0xff << format.Gshift;
format.Bmask = 0xff << format.Bshift;
format.Amask = 0;
format.Rloss = 0;
format.Gloss = 0;
format.Bloss = 0;
format.Aloss = 8;
newsurf = SDL_ConvertSurface(pic, &format, SDL_SWSURFACE);
if (!newsurf) {
fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n",
SDL_GetError());
quit(1);
}
SDL_FreeSurface(pic);
pic = newsurf;
}
/* Create the overlay */
overlay = SDL_CreateYUVOverlay(pic->w, pic->h, overlay_format, screen);
if (overlay == NULL) {
fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError());
quit(1);
}
printf("Created %dx%dx%d %s %s overlay\n", overlay->w, overlay->h,
overlay->planes, overlay->hw_overlay ? "hardware" : "software",
overlay->format == SDL_YV12_OVERLAY ? "YV12" : overlay->format ==
SDL_IYUV_OVERLAY ? "IYUV" : overlay->format ==
SDL_YUY2_OVERLAY ? "YUY2" : overlay->format ==
SDL_UYVY_OVERLAY ? "UYVY" : overlay->format ==
SDL_YVYU_OVERLAY ? "YVYU" : "Unknown");
for (i = 0; i < overlay->planes; i++) {
printf(" plane %d: pitch=%d\n", i, overlay->pitches[i]);
}
/* Convert to YUV, and draw to the overlay */
#ifdef BENCHMARK_SDL
then = SDL_GetTicks();
#endif
switch (overlay->format) {
case SDL_YV12_OVERLAY:
ConvertRGBtoYV12(pic, overlay, monochrome, luminance);
break;
case SDL_UYVY_OVERLAY:
ConvertRGBtoUYVY(pic, overlay, monochrome, luminance);
break;
case SDL_YVYU_OVERLAY:
ConvertRGBtoYVYU(pic, overlay, monochrome, luminance);
break;
case SDL_YUY2_OVERLAY:
ConvertRGBtoYUY2(pic, overlay, monochrome, luminance);
break;
case SDL_IYUV_OVERLAY:
ConvertRGBtoIYUV(pic, overlay, monochrome, luminance);
break;
default:
printf("cannot convert RGB picture to obtained YUV format!\n");
quit(1);
break;
}
#ifdef BENCHMARK_SDL
now = SDL_GetTicks();
printf("Conversion Time: %d milliseconds\n", now - then);
#endif
/* Do all the drawing work */
#ifdef BENCHMARK_SDL
then = SDL_GetTicks();
#endif
Draw();
#ifdef BENCHMARK_SDL
now = SDL_GetTicks();
printf("Time: %d milliseconds\n", now - then);
#endif
SDL_Delay(delay * 1000);
SDL_Quit();
return (0);
}

View file

@ -16,6 +16,15 @@
* *
********************************************************************************/
#if 1 /* FIXME: Rework this using the 2.0 API */
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("FIXME\n");
return 0;
}
#else
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@ -591,5 +600,6 @@ main(int argc, char **argv)
quit(0);
return 0;
}
#endif
/* vi: set ts=4 sw=4 expandtab: */

View file

@ -11,6 +11,15 @@
*/
/* This is a simple example of using GLSL shaders with SDL */
#if 1 /* FIXME: Rework this using the 2.0 API */
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("FIXME\n");
return 0;
}
#else
#include "SDL.h"
#ifdef HAVE_OPENGL
@ -494,5 +503,6 @@ main(int argc, char *argv[])
}
#endif /* HAVE_OPENGL */
#endif
/* vi: set ts=4 sw=4 expandtab: */

View file

@ -1,339 +0,0 @@
/*
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
/* Simple program: Move N sprites around on the screen as fast as possible */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
#include "SDL.h"
#define NUM_SPRITES 100
#define MAX_SPEED 1
SDL_Surface *sprite;
int numsprites;
SDL_Rect *sprite_rects;
SDL_Rect *positions;
SDL_Rect *velocities;
int sprites_visible;
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)
{
SDL_Quit();
exit(rc);
}
int
LoadSprite(char *file)
{
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);
}
/* 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;
/* We're ready to roll. :) */
return (0);
}
void
MoveSprites(SDL_Surface * screen, Uint32 background)
{
int i, nupdates;
SDL_Rect area, *position, *velocity;
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;
}
/* 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;
Uint32 color = SDL_MapRGB(screen->format, 255, 0, 0);
SDL_Rect r;
r.x =
(int)((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;
}
}
/* 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)
{
const SDL_VideoInfo *info;
/* 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 > (Uint32)(height * width * bpp / 8)) {
flags |= SDL_DOUBLEBUF;
} else {
flags &= ~SDL_HWSURFACE;
}
}
/* Return the flags */
return (flags);
}
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;
/* 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 | SDL_RESIZABLE;
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);
}
/* 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((unsigned int)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;
}
}
/* Clear the background to grey */
background = SDL_MapRGB(screen->format, 0xA0, 0xA0, 0xA0);
SDL_FillRect(screen, NULL, background);
SDL_Flip(screen);
/* 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_VIDEORESIZE:
screen = SDL_SetVideoMode(event.resize.w, event.resize.h, video_bpp, videoflags);
break;
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) {
double fps = ((double) frames * 1000) / (now - then);
printf("%2.2f frames per second\n", fps);
}
SDL_Quit();
return (0);
}

View file

@ -14,6 +14,15 @@
platform
*/
#if 1 /* FIXME: Rework this using the 2.0 API */
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("FIXME\n");
return 0;
}
#else
#include <stdlib.h>
#include <stdio.h>
@ -107,5 +116,6 @@ main(int argc, char *argv[])
SDL_Quit();
return (0);
}
#endif
/* vi: set ts=4 sw=4 expandtab: */

View file

@ -1,546 +0,0 @@
/*
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
/* Simple program -- figure out what kind of video display we have */
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SDL.h"
#define NUM_BLITS 10
#define NUM_UPDATES 500
#define FLAG_MASK (SDL_HWSURFACE | SDL_FULLSCREEN | SDL_DOUBLEBUF | \
SDL_SRCCOLORKEY | SDL_SRCALPHA | SDL_RLEACCEL | \
SDL_RLEACCELOK)
#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");
}
}
int
RunBlitTests(SDL_Surface * screen, SDL_Surface * bmp, int blitcount)
{
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);
}
return i;
}
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;
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);
}
/* 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;
}
/* 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);
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);
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);
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);
}
/* 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;
}
/* 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);
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) {
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[] = {
{
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;
/* 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
int
main(int argc, char *argv[])
{
const SDL_VideoInfo *info;
int i, d, n;
const char *driver;
SDL_DisplayMode mode;
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
int nmodes;
/* 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) {
SDL_Rect bounds;
SDL_GetDisplayBounds(d, &bounds);
printf("Display %d: %dx%d at %d,%d\n", d,
bounds.w, bounds.h, bounds.x, bounds.y);
SDL_GetDesktopDisplayMode(d, &mode);
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(d);
if (nmodes == 0) {
printf("No available fullscreen video modes\n");
} else {
printf(" Fullscreen video modes:\n");
for (i = 0; i < nmodes; ++i) {
SDL_GetDisplayMode(d, i, &mode);
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");
}
printf("Current resolution: %dx%d\n", info->current_w, info->current_h);
#if 0
if (argv[1] && (strcmp(argv[1], "-benchmark") == 0)) {
RunVideoTests();
}
#endif
SDL_Quit();
return (0);
}

View file

@ -1,351 +0,0 @@
/*
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
/* Bring up a window and play with it */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define BENCHMARK_SDL
#define NOTICE(X) printf("%s", X);
#include "SDL.h"
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void
quit(int rc)
{
SDL_Quit();
exit(rc);
}
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;
/* 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;
/* 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");
}
/* 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());
#endif
#ifndef BENCHMARK_SDL
/* 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];
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[])
{
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;
#endif
/* 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;
#else
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);
}
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;
}
/* Set the window manager title bar */
SDL_WM_SetCaption("SDL test window", "testwin");
/* 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);
#else
DrawPict(screen, argv[1], speedy, flip, nofade);
#endif
SDL_Delay(delay * 1000);
SDL_Quit();
return (0);
}

View file

@ -1,465 +0,0 @@
/*
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
/* Test out the window manager interaction functions */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SDL.h"
/* Is the cursor visible? */
static int visible = 1;
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)
{
SDL_Quit();
exit(rc);
}
int
SetVideoMode(int w, int h)
{
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");
/* 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);
}
SDL_Surface *
LoadIconSurface(char *file, Uint8 ** maskp)
{
SDL_Surface *icon;
Uint8 *pixels;
Uint8 *mask;
int mlen, i, j;
*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);
}
/* 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));
/* 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));
}
*maskp = mask;
return (icon);
}
void
HotKey_ToggleFullScreen(void)
{
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);
}
}
void
HotKey_ToggleGrab(void)
{
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");
}
}
void
HotKey_Iconify(void)
{
printf("Ctrl-Z: iconifying window!\n");
SDL_WM_IconifyWindow();
}
void
HotKey_Quit(void)
{
SDL_Event event;
printf("Posting internal quit request\n");
event.type = SDL_USEREVENT;
SDL_PushEvent(&event);
}
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");
}
static void PrintKey(const 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 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);
#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);
#endif
}
}
print_modifiers();
printf("\n");
}
static int (SDLCALL * old_filterfunc) (void *, SDL_Event *);
static void *old_filterdata;
int SDLCALL
FilterEvents(void *userdata, SDL_Event * event)
{
static int reallyquit = 0;
if (old_filterfunc) {
old_filterfunc(old_filterdata, event);
}
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_APPINPUTFOCUS)
printf("input ");
if (event->active.state & SDL_APPMOUSEFOCUS)
printf("mouse ");
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);
/* 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 at %d,%d\n",
event->button.button,
(event->button.state == SDL_PRESSED) ? "pressed" : "released",
event->button.x, event->button.y);
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);
#endif
return (0);
case SDL_KEYDOWN:
PrintKey(&event->key.keysym, 1);
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|KMOD_META))) {
HotKey_ToggleFullScreen();
}
return (0);
case SDL_KEYUP:
PrintKey(&event->key.keysym, 0);
return(0);
/* 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 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);
}
}
int
main(int argc, char *argv[])
{
SDL_Event event;
const 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);
}
/* 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 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");
/* Initialize the display */
if (SetVideoMode(w, h) < 0) {
quit(1);
}
/* Set an event filter that discards everything but QUIT */
SDL_GetEventFilter(&old_filterfunc, &old_filterdata);
SDL_SetEventFilter(FilterEvents, NULL);
/* 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);
}

View file

@ -1,337 +0,0 @@
/*
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
/* Test out the multi-threaded event handling functions */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "SDL.h"
#include "SDL_thread.h"
/* Are we done yet? */
static int done = 0;
/* Is the cursor visible? */
static int visible = 1;
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void
quit(int rc)
{
SDL_Quit();
exit(rc);
}
SDL_Surface *
LoadIconSurface(char *file, Uint8 ** maskp)
{
SDL_Surface *icon;
Uint8 *pixels;
Uint8 *mask;
int mlen, i;
*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);
}
/* 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));
/* 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(void *userdata, SDL_Event * event)
{
static int reallyquit = 0;
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");
/* 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);
/* 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);
}
}
int SDLCALL
HandleMouse(void *unused)
{
SDL_Event events[10];
int i, found;
/* Handle mouse events here */
while (!done) {
found = SDL_PeepEvents(events, 10, SDL_GETEVENT, SDL_MOUSEMOTION, SDL_MOUSEBUTTONUP);
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)
{
SDL_Event events[10];
int i, found;
/* Handle mouse events here */
while (!done) {
found = SDL_PeepEvents(events, 10, SDL_GETEVENT, SDL_KEYDOWN, SDL_KEYUP);
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' (keysym==%d) has been %s\n",
events[i].key.keysym.unicode,
(int) events[i].key.keysym.sym,
(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;
}
/* 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_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);
}
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;
/* 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 ((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);
}
/* 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");
/* Enable printable characters */
SDL_EnableUNICODE(1);
/* Set an event filter that discards everything but QUIT */
SDL_SetEventFilter(FilterEvents, NULL);
/* Create the event handling threads */
mouse_thread = SDL_CreateThread(HandleMouse, "MouseHandler", NULL);
keybd_thread = SDL_CreateThread(HandleKeyboard, "KeyboardHandler", 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);
/* Loop, waiting for QUIT */
while (!done) {
SDL_PumpEvents();
if (SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_QUIT, SDL_QUIT)) {
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);
}