Mercurial > sdl-ios-xcode
annotate XCodeiPhoneOS/Demos/src/rectangles.c @ 2385:0705e8ebe951 gsoc2008_iphone
Rectangles demo (SDL_RenderFill)
author | Holmes Futrell <hfutrell@umail.ucsb.edu> |
---|---|
date | Fri, 18 Jul 2008 20:52:18 +0000 |
parents | |
children |
rev | line source |
---|---|
2385
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
1 /* |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
2 * rectangles.c |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
3 * written by Holmes Futrell |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
4 * use however you want |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
5 */ |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
6 |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
7 #include "SDL.h" |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
8 #include <time.h> |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
9 #include "common.h" |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
10 |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
11 void render(void) { |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
12 |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
13 Uint8 r, g, b; |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
14 /* Come up with a random rectangle */ |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
15 SDL_Rect rect; |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
16 rect.w = randomInt(64, 128); |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
17 rect.h = randomInt(64, 128); |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
18 rect.x = randomInt(0, SCREEN_WIDTH); |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
19 rect.y = randomInt(0, SCREEN_HEIGHT); |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
20 |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
21 /* Come up with a random color */ |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
22 r = randomInt(50, 255); |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
23 g = randomInt(50, 255); |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
24 b = randomInt(50, 255); |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
25 |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
26 /* Fill the rectangle in the color */ |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
27 SDL_RenderFill(r, g, b, 255, &rect); |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
28 |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
29 /* update screen */ |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
30 SDL_RenderPresent(); |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
31 |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
32 } |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
33 |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
34 int main(int argc, char *argv[]) { |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
35 |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
36 SDL_WindowID windowID; |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
37 int done; |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
38 SDL_Event event; |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
39 |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
40 /* initialize SDL */ |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
41 if (SDL_Init(SDL_INIT_VIDEO) < 0) { |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
42 fatalError("Could not initialize SDL"); |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
43 } |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
44 |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
45 /* seed random number generator */ |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
46 srand(time(NULL)); |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
47 |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
48 /* create window and renderer */ |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
49 windowID = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
50 if (windowID == 0) { |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
51 fatalError("Could not initialize Window"); |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
52 } |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
53 if (SDL_CreateRenderer(windowID, -1, 0) != 0) { |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
54 fatalError("Could not create renderer"); |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
55 } |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
56 |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
57 /* Fill screen with black */ |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
58 SDL_RenderFill(0,0,0,0, NULL); |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
59 |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
60 /* Enter render loop, waiting for user to quit */ |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
61 done = 0; |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
62 while(!done) { |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
63 while (SDL_PollEvent(&event)) { |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
64 if (event.type == SDL_QUIT) { |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
65 done = 1; |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
66 } |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
67 } |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
68 render(); |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
69 SDL_Delay(1); |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
70 } |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
71 |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
72 /* shutdown SDL */ |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
73 SDL_Quit(); |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
74 |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
75 return 0; |
0705e8ebe951
Rectangles demo (SDL_RenderFill)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
76 } |