Commit 3b997f23 authored by Sam Lantinga's avatar Sam Lantinga

Added a test program framework for easy initialization.

Started work on multi-window OpenGL demo

--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%401959
parent e0a8c14a
...@@ -119,7 +119,7 @@ typedef enum ...@@ -119,7 +119,7 @@ typedef enum
/** /**
* \struct SDL_WindowEvent * \struct SDL_WindowEvent
* *
* \brief Window state change event data * \brief Window state change event data (event.window.*)
*/ */
typedef struct SDL_WindowEvent typedef struct SDL_WindowEvent
{ {
...@@ -133,7 +133,7 @@ typedef struct SDL_WindowEvent ...@@ -133,7 +133,7 @@ typedef struct SDL_WindowEvent
/** /**
* \struct SDL_KeyboardEvent * \struct SDL_KeyboardEvent
* *
* \brief Keyboard button event structure * \brief Keyboard button event structure (event.key.*)
*/ */
typedef struct SDL_KeyboardEvent typedef struct SDL_KeyboardEvent
{ {
...@@ -147,7 +147,7 @@ typedef struct SDL_KeyboardEvent ...@@ -147,7 +147,7 @@ typedef struct SDL_KeyboardEvent
/** /**
* \struct SDL_TextInputEvent * \struct SDL_TextInputEvent
* *
* \brief Keyboard text input event structure * \brief Keyboard text input event structure (event.text.*)
*/ */
typedef struct SDL_TextInputEvent typedef struct SDL_TextInputEvent
{ {
...@@ -160,7 +160,7 @@ typedef struct SDL_TextInputEvent ...@@ -160,7 +160,7 @@ typedef struct SDL_TextInputEvent
/** /**
* \struct SDL_MouseMotionEvent * \struct SDL_MouseMotionEvent
* *
* \brief Mouse motion event structure * \brief Mouse motion event structure (event.motion.*)
*/ */
typedef struct SDL_MouseMotionEvent typedef struct SDL_MouseMotionEvent
{ {
...@@ -177,7 +177,7 @@ typedef struct SDL_MouseMotionEvent ...@@ -177,7 +177,7 @@ typedef struct SDL_MouseMotionEvent
/** /**
* \struct SDL_MouseButtonEvent * \struct SDL_MouseButtonEvent
* *
* \brief Mouse button event structure * \brief Mouse button event structure (event.button.*)
*/ */
typedef struct SDL_MouseButtonEvent typedef struct SDL_MouseButtonEvent
{ {
...@@ -193,20 +193,20 @@ typedef struct SDL_MouseButtonEvent ...@@ -193,20 +193,20 @@ typedef struct SDL_MouseButtonEvent
/** /**
* \struct SDL_MouseWheelEvent * \struct SDL_MouseWheelEvent
* *
* \brief Mouse wheel event structure * \brief Mouse wheel event structure (event.wheel.*)
*/ */
typedef struct SDL_MouseWheelEvent typedef struct SDL_MouseWheelEvent
{ {
Uint8 type; /**< SDL_MOUSEWHEEL */ Uint8 type; /**< SDL_MOUSEWHEEL */
Uint8 which; /**< The mouse device index */ Uint8 which; /**< The mouse device index */
int motion; /**< The direction and distance scrolled */ int motion; /**< The direction and distance scrolled */
SDL_WindowID windowID; /**< The window with mouse focus, if any */ SDL_WindowID windowID; /**< The window with mouse focus, if any */
} SDL_MouseWheelEvent; } SDL_MouseWheelEvent;
/** /**
* \struct SDL_JoyAxisEvent * \struct SDL_JoyAxisEvent
* *
* \brief Joystick axis motion event structure * \brief Joystick axis motion event structure (event.jaxis.*)
*/ */
typedef struct SDL_JoyAxisEvent typedef struct SDL_JoyAxisEvent
{ {
...@@ -219,7 +219,7 @@ typedef struct SDL_JoyAxisEvent ...@@ -219,7 +219,7 @@ typedef struct SDL_JoyAxisEvent
/** /**
* \struct SDL_JoyBallEvent * \struct SDL_JoyBallEvent
* *
* \brief Joystick trackball motion event structure * \brief Joystick trackball motion event structure (event.jball.*)
*/ */
typedef struct SDL_JoyBallEvent typedef struct SDL_JoyBallEvent
{ {
...@@ -233,7 +233,7 @@ typedef struct SDL_JoyBallEvent ...@@ -233,7 +233,7 @@ typedef struct SDL_JoyBallEvent
/** /**
* \struct SDL_JoyHatEvent * \struct SDL_JoyHatEvent
* *
* \brief Joystick hat position change event structure * \brief Joystick hat position change event structure (event.jhat.*)
*/ */
typedef struct SDL_JoyHatEvent typedef struct SDL_JoyHatEvent
{ {
...@@ -251,7 +251,7 @@ typedef struct SDL_JoyHatEvent ...@@ -251,7 +251,7 @@ typedef struct SDL_JoyHatEvent
/** /**
* \struct SDL_JoyButtonEvent * \struct SDL_JoyButtonEvent
* *
* \brief Joystick button event structure * \brief Joystick button event structure (event.jbutton.*)
*/ */
typedef struct SDL_JoyButtonEvent typedef struct SDL_JoyButtonEvent
{ {
...@@ -274,7 +274,7 @@ typedef struct SDL_QuitEvent ...@@ -274,7 +274,7 @@ typedef struct SDL_QuitEvent
/** /**
* \struct SDL_UserEvent * \struct SDL_UserEvent
* *
* \brief A user-defined event type * \brief A user-defined event type (event.user.*)
*/ */
typedef struct SDL_UserEvent typedef struct SDL_UserEvent
{ {
...@@ -287,7 +287,7 @@ typedef struct SDL_UserEvent ...@@ -287,7 +287,7 @@ typedef struct SDL_UserEvent
/** /**
* \struct SDL_SysWMEvent * \struct SDL_SysWMEvent
* *
* \brief A video driver dependent system event * \brief A video driver dependent system event (event.syswm.*)
* *
* \note If you want to use this event, you should include SDL_syswm.h * \note If you want to use this event, you should include SDL_syswm.h
*/ */
......
...@@ -7,9 +7,12 @@ EXE = @EXE@ ...@@ -7,9 +7,12 @@ EXE = @EXE@
CFLAGS = @CFLAGS@ CFLAGS = @CFLAGS@
LIBS = @LIBS@ LIBS = @LIBS@
TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcdrom$(EXE) testcursor$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl$(EXE) testhread$(EXE) testiconv$(EXE) testjoystick$(EXE) testkeys$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testsem$(EXE) testsprite$(EXE) testsprite2$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) testwm2$(EXE) threadwin$(EXE) torturethread$(EXE) TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcdrom$(EXE) testcursor$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl$(EXE) testgl2$(EXE) testhread$(EXE) testiconv$(EXE) testjoystick$(EXE) testkeys$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testsem$(EXE) testsprite$(EXE) testsprite2$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) testwm2$(EXE) threadwin$(EXE) torturethread$(EXE)
all: $(TARGETS) all: Makefile $(TARGETS)
Makefile: $(srcdir)/Makefile.in
$(SHELL) config.status $@
checkkeys$(EXE): $(srcdir)/checkkeys.c checkkeys$(EXE): $(srcdir)/checkkeys.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS) $(CC) -o $@ $? $(CFLAGS) $(LIBS)
...@@ -50,6 +53,9 @@ testgamma$(EXE): $(srcdir)/testgamma.c ...@@ -50,6 +53,9 @@ testgamma$(EXE): $(srcdir)/testgamma.c
testgl$(EXE): $(srcdir)/testgl.c testgl$(EXE): $(srcdir)/testgl.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS) @GLLIB@ @MATHLIB@ $(CC) -o $@ $? $(CFLAGS) $(LIBS) @GLLIB@ @MATHLIB@
testgl2$(EXE): $(srcdir)/testgl2.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS) @GLLIB@ @MATHLIB@
testhread$(EXE): $(srcdir)/testhread.c testhread$(EXE): $(srcdir)/testhread.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS) $(CC) -o $@ $? $(CFLAGS) $(LIBS)
...@@ -83,8 +89,8 @@ testsem$(EXE): $(srcdir)/testsem.c ...@@ -83,8 +89,8 @@ testsem$(EXE): $(srcdir)/testsem.c
testsprite$(EXE): $(srcdir)/testsprite.c testsprite$(EXE): $(srcdir)/testsprite.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@ $(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
testsprite2$(EXE): $(srcdir)/testsprite2.c testsprite2$(EXE): $(srcdir)/testsprite2.c $(srcdir)/common.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@ $(CC) -o $@ $(srcdir)/testsprite2.c $(srcdir)/common.c $(CFLAGS) $(LIBS) @MATHLIB@
testtimer$(EXE): $(srcdir)/testtimer.c testtimer$(EXE): $(srcdir)/testtimer.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS) $(CC) -o $@ $? $(CFLAGS) $(LIBS)
......
This diff is collapsed.
/* A simple test program framework */
#include "SDL.h"
#define VERBOSE_VIDEO 0x00000001
#define VERBOSE_MODES 0x00000002
#define VERBOSE_RENDER 0x00000004
#define VERBOSE_EVENT 0x00000008
#define VERBOSE_AUDIO 0x00000010
typedef struct
{
/* SDL init flags */
char **argv;
Uint32 flags;
Uint32 verbose;
/* Video info */
const char *videodriver;
int display;
const char *window_title;
Uint32 window_flags;
int window_x;
int window_y;
int window_w;
int window_h;
int depth;
int refresh_rate;
int num_windows;
SDL_WindowID *windows;
/* Renderer info */
const char *renderdriver;
Uint32 render_flags;
/* Audio info */
const char *audiodriver;
SDL_AudioSpec audiospec;
} CommonState;
extern CommonState *CommonCreateState(char **argv, Uint32 flags);
extern int CommonArg(CommonState * state, int index);
extern const char *CommonUsage(CommonState * state);
extern SDL_bool CommonInit(CommonState * state);
extern void CommonEvent(CommonState * state, SDL_Event * event, int *done);
extern void CommonQuit(CommonState * state);
This diff is collapsed.
...@@ -4,18 +4,14 @@ ...@@ -4,18 +4,14 @@
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#include "SDL.h" #include "common.h"
#define NUM_WINDOWS 4
#define WINDOW_W 640
#define WINDOW_H 480
#define NUM_SPRITES 100 #define NUM_SPRITES 100
#define MAX_SPEED 1 #define MAX_SPEED 1
#define BACKGROUND 0x00FFFFFF #define BACKGROUND 0x00FFFFFF
static int num_windows; static CommonState *state;
static int num_sprites; static int num_sprites;
static SDL_WindowID *windows;
static SDL_TextureID *sprites; static SDL_TextureID *sprites;
static SDL_Rect *positions; static SDL_Rect *positions;
static SDL_Rect *velocities; static SDL_Rect *velocities;
...@@ -25,9 +21,6 @@ static int sprite_w, sprite_h; ...@@ -25,9 +21,6 @@ static int sprite_w, sprite_h;
static void static void
quit(int rc) quit(int rc)
{ {
if (windows) {
SDL_free(windows);
}
if (sprites) { if (sprites) {
SDL_free(sprites); SDL_free(sprites);
} }
...@@ -37,7 +30,7 @@ quit(int rc) ...@@ -37,7 +30,7 @@ quit(int rc)
if (velocities) { if (velocities) {
SDL_free(velocities); SDL_free(velocities);
} }
SDL_Quit(); CommonQuit(state);
exit(rc); exit(rc);
} }
...@@ -62,8 +55,8 @@ LoadSprite(char *file) ...@@ -62,8 +55,8 @@ LoadSprite(char *file)
} }
/* Create textures from the image */ /* Create textures from the image */
for (i = 0; i < num_windows; ++i) { for (i = 0; i < state->num_windows; ++i) {
SDL_SelectRenderer(windows[i]); SDL_SelectRenderer(state->windows[i]);
sprites[i] = sprites[i] =
SDL_CreateTextureFromSurface(0, SDL_TextureAccess_Remote, temp); SDL_CreateTextureFromSurface(0, SDL_TextureAccess_Remote, temp);
if (!sprites[i]) { if (!sprites[i]) {
...@@ -125,78 +118,45 @@ MoveSprites(SDL_WindowID window, SDL_TextureID sprite) ...@@ -125,78 +118,45 @@ MoveSprites(SDL_WindowID window, SDL_TextureID sprite)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int window_w, window_h;
Uint32 window_flags = SDL_WINDOW_SHOWN;
Uint32 render_flags = 0;
SDL_DisplayMode *mode, fullscreen_mode;
int i, done; int i, done;
SDL_Event event; SDL_Event event;
Uint32 then, now, frames; Uint32 then, now, frames;
/* Initialize SDL */ /* Initialize parameters */
if (SDL_Init(SDL_INIT_VIDEO) < 0) { num_sprites = NUM_SPRITES;
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
return (1); /* Initialize test framework */
state = CommonCreateState(argv, SDL_INIT_VIDEO);
if (!state) {
return 1;
} }
for (i = 1; i < argc;) {
int consumed;
num_windows = NUM_WINDOWS; consumed = CommonArg(state, i);
num_sprites = NUM_SPRITES; if (consumed < 0) {
window_w = WINDOW_W; fprintf(stderr, "Usage: %s %s", argv[0], CommonUsage(state));
window_h = WINDOW_H;
for (i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-width") == 0 && (i + 1 < argc)) {
window_w = atoi(argv[++i]);
} else if (strcmp(argv[i], "-height") == 0 && (i + 1 < argc)) {
window_h = atoi(argv[++i]);
} else if (strcmp(argv[i], "-windows") == 0 && (i + 1 < argc)) {
num_windows = atoi(argv[++i]);
window_flags &= ~SDL_WINDOW_FULLSCREEN;
} else if (strcmp(argv[i], "-fullscreen") == 0) {
num_windows = 1;
window_flags |= SDL_WINDOW_FULLSCREEN;
} else if (strcmp(argv[i], "-sync") == 0) {
render_flags |= SDL_Renderer_PresentVSync;
} else if (isdigit(argv[i][0])) {
num_sprites = atoi(argv[i]);
} else {
fprintf(stderr,
"Usage: %s [-width N] [-height N] [-windows N] [-fullscreen] [-sync] [numsprites]\n",
argv[0]);
quit(1); quit(1);
} }
if (consumed == 0) {
num_sprites = SDL_atoi(argv[i]);
consumed = 1;
}
i += consumed;
} }
if (!CommonInit(state)) {
if (window_flags & SDL_WINDOW_FULLSCREEN) { quit(2);
SDL_zero(fullscreen_mode);
fullscreen_mode.w = window_w;
fullscreen_mode.h = window_h;
SDL_SetFullscreenDisplayMode(&fullscreen_mode);
} }
/* Create the windows, initialize the renderers, and load the textures */ /* Create the windows, initialize the renderers, and load the textures */
windows = (SDL_WindowID *) SDL_malloc(num_windows * sizeof(*windows)); sprites =
sprites = (SDL_TextureID *) SDL_malloc(num_windows * sizeof(*sprites)); (SDL_TextureID *) SDL_malloc(state->num_windows * sizeof(*sprites));
if (!windows || !sprites) { if (!sprites) {
fprintf(stderr, "Out of memory!\n"); fprintf(stderr, "Out of memory!\n");
quit(2); quit(2);
} }
for (i = 0; i < num_windows; ++i) { for (i = 0; i < state->num_windows; ++i) {
char title[32]; SDL_SelectRenderer(state->windows[i]);
SDL_snprintf(title, sizeof(title), "testsprite %d", i + 1);
windows[i] =
SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, window_w, window_h,
window_flags);
if (!windows[i]) {
fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
quit(2);
}
if (SDL_CreateRenderer(windows[i], -1, render_flags) < 0) {
fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
quit(2);
}
SDL_RenderFill(NULL, BACKGROUND); SDL_RenderFill(NULL, BACKGROUND);
} }
if (LoadSprite("icon.bmp") < 0) { if (LoadSprite("icon.bmp") < 0) {
...@@ -212,8 +172,8 @@ main(int argc, char *argv[]) ...@@ -212,8 +172,8 @@ main(int argc, char *argv[])
} }
srand(time(NULL)); srand(time(NULL));
for (i = 0; i < num_sprites; ++i) { for (i = 0; i < num_sprites; ++i) {
positions[i].x = rand() % (window_w - sprite_w); positions[i].x = rand() % (state->window_w - sprite_w);
positions[i].y = rand() % (window_h - sprite_h); positions[i].y = rand() % (state->window_h - sprite_h);
positions[i].w = sprite_w; positions[i].w = sprite_w;
positions[i].h = sprite_h; positions[i].h = sprite_h;
velocities[i].x = 0; velocities[i].x = 0;
...@@ -232,6 +192,7 @@ main(int argc, char *argv[]) ...@@ -232,6 +192,7 @@ main(int argc, char *argv[])
/* Check for events */ /* Check for events */
++frames; ++frames;
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
CommonEvent(state, &event, &done);
switch (event.type) { switch (event.type) {
case SDL_WINDOWEVENT: case SDL_WINDOWEVENT:
switch (event.window.event) { switch (event.window.event) {
...@@ -239,22 +200,14 @@ main(int argc, char *argv[]) ...@@ -239,22 +200,14 @@ main(int argc, char *argv[])
SDL_SelectRenderer(event.window.windowID); SDL_SelectRenderer(event.window.windowID);
SDL_RenderFill(NULL, BACKGROUND); SDL_RenderFill(NULL, BACKGROUND);
break; break;
case SDL_WINDOWEVENT_CLOSE:
done = 1;
break;
} }
break; break;
case SDL_KEYDOWN:
/* Any keypress quits the app... */
case SDL_QUIT:
done = 1;
break;
default: default:
break; break;
} }
} }
for (i = 0; i < num_windows; ++i) { for (i = 0; i < state->num_windows; ++i) {
MoveSprites(windows[i], sprites[i]); MoveSprites(state->windows[i], sprites[i]);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment