comparison Xcode-iPhoneOS/Demos/src/touch.c @ 5211:78db79f5a4e2

Updated the iPhone demos for the new API
author Sam Lantinga <slouken@libsdl.org>
date Sun, 06 Feb 2011 09:02:10 -0800
parents 06c7423f8c60
children
comparison
equal deleted inserted replaced
5210:443a850284a1 5211:78db79f5a4e2
16 /* 16 /*
17 draws a line from (startx, starty) to (startx + dx, starty + dy) 17 draws a line from (startx, starty) to (startx + dx, starty + dy)
18 this is accomplished by drawing several blots spaced PIXELS_PER_ITERATION apart 18 this is accomplished by drawing several blots spaced PIXELS_PER_ITERATION apart
19 */ 19 */
20 void 20 void
21 drawLine(float startx, float starty, float dx, float dy) 21 drawLine(SDL_Renderer *renderer, float startx, float starty, float dx, float dy)
22 { 22 {
23 23
24 float distance = sqrt(dx * dx + dy * dy); /* length of line segment (pythagoras) */ 24 float distance = sqrt(dx * dx + dy * dy); /* length of line segment (pythagoras) */
25 int iterations = distance / PIXELS_PER_ITERATION + 1; /* number of brush sprites to draw for the line */ 25 int iterations = distance / PIXELS_PER_ITERATION + 1; /* number of brush sprites to draw for the line */
26 float dx_prime = dx / iterations; /* x-shift per iteration */ 26 float dx_prime = dx / iterations; /* x-shift per iteration */
41 dstRect.y = y; 41 dstRect.y = y;
42 /* shift x and y for next sprite location */ 42 /* shift x and y for next sprite location */
43 x += dx_prime; 43 x += dx_prime;
44 y += dy_prime; 44 y += dy_prime;
45 /* draw brush blot */ 45 /* draw brush blot */
46 SDL_RenderCopy(brush, NULL, &dstRect); 46 SDL_RenderCopy(renderer, brush, NULL, &dstRect);
47 } 47 }
48 } 48 }
49 49
50 /* 50 /*
51 loads the brush texture 51 loads the brush texture
52 */ 52 */
53 void 53 void
54 initializeTexture() 54 initializeTexture(SDL_Renderer *renderer)
55 { 55 {
56 SDL_Surface *bmp_surface; 56 SDL_Surface *bmp_surface;
57 bmp_surface = SDL_LoadBMP("stroke.bmp"); 57 bmp_surface = SDL_LoadBMP("stroke.bmp");
58 if (bmp_surface == NULL) { 58 if (bmp_surface == NULL) {
59 fatalError("could not load stroke.bmp"); 59 fatalError("could not load stroke.bmp");
60 } 60 }
61 brush = 61 brush =
62 SDL_CreateTextureFromSurface(SDL_PIXELFORMAT_ABGR8888, bmp_surface); 62 SDL_CreateTextureFromSurface(renderer, bmp_surface);
63 SDL_FreeSurface(bmp_surface); 63 SDL_FreeSurface(bmp_surface);
64 if (brush == 0) { 64 if (brush == 0) {
65 fatalError("could not create brush texture"); 65 fatalError("could not create brush texture");
66 } 66 }
67 /* additive blending -- laying strokes on top of eachother makes them brighter */ 67 /* additive blending -- laying strokes on top of eachother makes them brighter */
76 76
77 int x, y, dx, dy; /* mouse location */ 77 int x, y, dx, dy; /* mouse location */
78 Uint8 state; /* mouse (touch) state */ 78 Uint8 state; /* mouse (touch) state */
79 SDL_Event event; 79 SDL_Event event;
80 SDL_Window *window; /* main window */ 80 SDL_Window *window; /* main window */
81 SDL_Renderer *renderer;
81 int done; /* does user want to quit? */ 82 int done; /* does user want to quit? */
82 83
83 /* initialize SDL */ 84 /* initialize SDL */
84 if (SDL_Init(SDL_INIT_VIDEO) < 0) { 85 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
85 fatalError("Could not initialize SDL"); 86 fatalError("Could not initialize SDL");
87 88
88 /* create main window and renderer */ 89 /* create main window and renderer */
89 window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 90 window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
90 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | 91 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN |
91 SDL_WINDOW_BORDERLESS); 92 SDL_WINDOW_BORDERLESS);
92 SDL_CreateRenderer(window, 0, 0); 93 renderer = SDL_CreateRenderer(window, 0, 0);
93 94
94 /*load brush texture */ 95 /*load brush texture */
95 initializeTexture(); 96 initializeTexture(renderer);
96 97
97 /* fill canvass initially with all black */ 98 /* fill canvass initially with all black */
98 SDL_SetRenderDrawColor(0, 0, 0, 255); 99 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
99 SDL_RenderFill(NULL); 100 SDL_RenderClear();
100 SDL_RenderPresent(); 101 SDL_RenderPresent(renderer);
101 102
102 done = 0; 103 done = 0;
103 while (!done && SDL_WaitEvent(&event)) { 104 while (!done && SDL_WaitEvent(&event)) {
104 switch (event.type) { 105 switch (event.type) {
105 case SDL_QUIT: 106 case SDL_QUIT:
107 break; 108 break;
108 case SDL_MOUSEMOTION: 109 case SDL_MOUSEMOTION:
109 state = SDL_GetMouseState(&x, &y); /* get its location */ 110 state = SDL_GetMouseState(&x, &y); /* get its location */
110 SDL_GetRelativeMouseState(&dx, &dy); /* find how much the mouse moved */ 111 SDL_GetRelativeMouseState(&dx, &dy); /* find how much the mouse moved */
111 if (state & SDL_BUTTON_LMASK) { /* is the mouse (touch) down? */ 112 if (state & SDL_BUTTON_LMASK) { /* is the mouse (touch) down? */
112 drawLine(x - dx, y - dy, dx, dy); /* draw line segment */ 113 drawLine(renderer, x - dx, y - dy, dx, dy); /* draw line segment */
113 SDL_RenderPresent(); 114 SDL_RenderPresent(renderer);
114 } 115 }
115 break; 116 break;
116 } 117 }
117 } 118 }
118 119