Mercurial > sdl-ios-xcode
annotate test/testfill.c @ 4634:242c805baf68
Updated with the latest version of glext.h
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sat, 14 Aug 2010 12:22:06 -0700 |
parents | 5ea08f1c29d0 |
children |
rev | line source |
---|---|
3576
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
1 /* Simple program: Fill the screen with colors as fast as possible */ |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
2 |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
3 #include <stdlib.h> |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
4 #include <stdio.h> |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
5 #include <string.h> |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
6 #include <time.h> |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
7 |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
8 #include "SDL.h" |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
9 |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
10 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
11 static void |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
12 quit(int rc) |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
13 { |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
14 SDL_Quit(); |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
15 exit(rc); |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
16 } |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
17 |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
18 int |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
19 main(int argc, char *argv[]) |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
20 { |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
21 SDL_Surface *screen; |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
22 int width, height; |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
23 Uint8 video_bpp; |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
24 Uint32 videoflags; |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
25 Uint32 colors[3]; |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
26 int i, done; |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
27 SDL_Event event; |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
28 Uint32 then, now, frames; |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
29 |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
30 /* Initialize SDL */ |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
31 if (SDL_Init(SDL_INIT_VIDEO) < 0) { |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
32 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
33 return (1); |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
34 } |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
35 |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
36 width = 640; |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
37 height = 480; |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
38 video_bpp = 8; |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
39 videoflags = 0; |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
40 while (argc > 1) { |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
41 --argc; |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
42 if (strcmp(argv[argc - 1], "-width") == 0) { |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
43 width = atoi(argv[argc]); |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
44 --argc; |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
45 } else if (strcmp(argv[argc - 1], "-height") == 0) { |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
46 height = atoi(argv[argc]); |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
47 --argc; |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
48 } else if (strcmp(argv[argc - 1], "-bpp") == 0) { |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
49 video_bpp = atoi(argv[argc]); |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
50 --argc; |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
51 } else if (strcmp(argv[argc], "-fullscreen") == 0) { |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
52 videoflags ^= SDL_FULLSCREEN; |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
53 } else { |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
54 fprintf(stderr, |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
55 "Usage: %s [-width N] [-height N] [-bpp N] [-fullscreen]\n", |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
56 argv[0]); |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
57 quit(1); |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
58 } |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
59 } |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
60 |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
61 /* Set video mode */ |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
62 screen = SDL_SetVideoMode(width, height, video_bpp, 0); |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
63 if (!screen) { |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
64 fprintf(stderr, "Couldn't set %dx%d video mode: %s\n", |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
65 width, height, SDL_GetError()); |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
66 quit(2); |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
67 } |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
68 |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
69 /* Get the colors */ |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
70 colors[0] = SDL_MapRGB(screen->format, 0xFF, 0x00, 0x00); |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
71 colors[1] = SDL_MapRGB(screen->format, 0x00, 0xFF, 0x00); |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
72 colors[2] = SDL_MapRGB(screen->format, 0x00, 0x00, 0xFF); |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
73 |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
74 /* Loop, filling and waiting for a keystroke */ |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
75 frames = 0; |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
76 then = SDL_GetTicks(); |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
77 done = 0; |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
78 while (!done) { |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
79 /* Check for events */ |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
80 ++frames; |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
81 while (SDL_PollEvent(&event)) { |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
82 switch (event.type) { |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
83 case SDL_MOUSEBUTTONDOWN: |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
84 SDL_WarpMouse(screen->w / 2, screen->h / 2); |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
85 break; |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
86 case SDL_KEYDOWN: |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
87 /* Any keypress quits the app... */ |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
88 case SDL_QUIT: |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
89 done = 1; |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
90 break; |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
91 default: |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
92 break; |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
93 } |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
94 } |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
95 SDL_FillRect(screen, NULL, colors[frames%3]); |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
96 SDL_Flip(screen); |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
97 } |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
98 |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
99 /* Print out some timing information */ |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
100 now = SDL_GetTicks(); |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
101 if (now > then) { |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
102 double fps = ((double) frames * 1000) / (now - then); |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
103 printf("%2.2f frames per second\n", fps); |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
104 } |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
105 SDL_Quit(); |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
106 return (0); |
5ea08f1c29d0
Added testfill to test raw fill performance
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
107 } |