Touch input demo (multitouch) gsoc2008_iphone
authorHolmes Futrell <hfutrell@umail.ucsb.edu>
Fri, 18 Jul 2008 20:52:32 +0000
branchgsoc2008_iphone
changeset 2386c5cbbba41c65
parent 2385 0705e8ebe951
child 2387 c33898db6b34
Touch input demo (multitouch)
XCodeiPhoneOS/Demos/src/touch.c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/XCodeiPhoneOS/Demos/src/touch.c	Fri Jul 18 20:52:32 2008 +0000
     1.3 @@ -0,0 +1,116 @@
     1.4 +/*
     1.5 + *	touch.c
     1.6 + *	written by Holmes Futrell
     1.7 + *	use however you want
     1.8 + */
     1.9 +
    1.10 +#include "SDL.h"
    1.11 +#include "math.h"
    1.12 +#include "common.h"
    1.13 +
    1.14 +#define BRUSH_SIZE 32			/* width and height of the brush */
    1.15 +#define PIXELS_PER_ITERATION 5  /* number of pixels between brush blots when forming a line */ 
    1.16 +
    1.17 +static SDL_TextureID brushID=0; /* texture for the brush */
    1.18 +
    1.19 +/*
    1.20 +	draws a line from (startx, starty) to (startx + dx, starty + dy)
    1.21 +	this is accomplished by drawing several blots spaced PIXELS_PER_ITERATION apart
    1.22 +*/
    1.23 +void drawLine(float startx, float starty, float dx, float dy) {
    1.24 +
    1.25 +	float distance = sqrt(dx*dx+dy*dy);						/* length of line segment (pythagoras) */
    1.26 +	int iterations = distance / PIXELS_PER_ITERATION + 1;	/* number of brush sprites to draw for the line */
    1.27 +	float dx_prime = dx / iterations;						/* x-shift per iteration */
    1.28 +	float dy_prime = dy / iterations;						/* y-shift per iteration */
    1.29 +	SDL_Rect dstRect;										/* rect to draw brush sprite into */
    1.30 +	
    1.31 +	dstRect.w = BRUSH_SIZE;
    1.32 +	dstRect.h = BRUSH_SIZE;
    1.33 +	
    1.34 +	/* setup x and y for the location of the first sprite */
    1.35 +	float x = startx - BRUSH_SIZE / 2.0f;
    1.36 +	float y = starty - BRUSH_SIZE / 2.0f;		
    1.37 +	
    1.38 +	int i;
    1.39 +	/* draw a series of blots to form the line */
    1.40 +	for (i=0; i<iterations; i++) {
    1.41 +		dstRect.x = x;
    1.42 +		dstRect.y = y;
    1.43 +		/* shift x and y for next sprite location */
    1.44 +		x += dx_prime;
    1.45 +		y += dy_prime;
    1.46 +		/* draw brush blot */
    1.47 +		SDL_RenderCopy(brushID, NULL, &dstRect);
    1.48 +	}
    1.49 +}
    1.50 +
    1.51 +/*
    1.52 +	loads the brush texture
    1.53 +*/
    1.54 +void initializeTexture() {
    1.55 +	SDL_Surface *bmp_surface;
    1.56 +	bmp_surface = SDL_LoadBMP("stroke.bmp");
    1.57 +	if (bmp_surface == NULL) {
    1.58 +		fatalError("could not load stroke.bmp");
    1.59 +	}
    1.60 +	brushID = SDL_CreateTextureFromSurface(SDL_PIXELFORMAT_ABGR8888, bmp_surface);
    1.61 +	SDL_FreeSurface(bmp_surface);
    1.62 +	if (brushID == 0) {
    1.63 +		fatalError("could not create brush texture");
    1.64 +	}
    1.65 +	/* additive blending -- laying strokes on top of eachother makes them brighter */
    1.66 +	SDL_SetTextureBlendMode(brushID, SDL_TEXTUREBLENDMODE_ADD);
    1.67 +	/* set brush color (red) */
    1.68 +	SDL_SetTextureColorMod(brushID, 255, 100, 100);
    1.69 +}
    1.70 +
    1.71 +int main(int argc, char *argv[]) {
    1.72 +	
    1.73 +	int x, y, dx, dy;		/* mouse location	   */
    1.74 +	Uint8 state;			/* mouse (touch) state */
    1.75 +	SDL_Event event;
    1.76 +	SDL_WindowID windowID;	/* main window */
    1.77 +	int done;				/* does user want to quit? */
    1.78 +	
    1.79 +	/* initialize SDL */
    1.80 +	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    1.81 +		fatalError("Could not initialize SDL");
    1.82 +	}
    1.83 +	
    1.84 +	/* create main window and renderer */
    1.85 +	windowID = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,\
    1.86 +								SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN|SDL_WINDOW_BORDERLESS);
    1.87 +	SDL_CreateRenderer(windowID, 0, 0);		
    1.88 +	
    1.89 +	/*load brush texture */
    1.90 +	initializeTexture();
    1.91 +	
    1.92 +	/* fill canvass initially with all black */
    1.93 +	SDL_RenderFill(0, 0, 0, 255, NULL);
    1.94 +	SDL_RenderPresent();
    1.95 +		
    1.96 +	done = 0;
    1.97 +	while (!done && SDL_WaitEvent(&event)) {
    1.98 +		switch (event.type) {
    1.99 +			case SDL_QUIT:
   1.100 +				done = 1;
   1.101 +				break;
   1.102 +			case SDL_MOUSEMOTION:
   1.103 +				SDL_SelectMouse(event.motion.which);  /* select 'mouse' (touch) that moved */
   1.104 +				state = SDL_GetMouseState(&x, &y);	  /* get its location */
   1.105 +				SDL_GetRelativeMouseState(&dx, &dy);  /* find how much the mouse moved */
   1.106 +				if (state & SDL_BUTTON_LMASK) {		  /* is the mouse (touch) down? */
   1.107 +					drawLine(x - dx, y - dy, dx, dy); /* draw line segment */
   1.108 +					SDL_RenderPresent();
   1.109 +				}
   1.110 +				break;
   1.111 +		}
   1.112 +	}
   1.113 +	
   1.114 +	/* cleanup */
   1.115 +	SDL_DestroyTexture(brushID);
   1.116 +	SDL_Quit();
   1.117 +	
   1.118 +	return 0;
   1.119 +}
   1.120 \ No newline at end of file