Xcode-iPhoneOS/Demos/src/touch.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 13 Mar 2011 11:17:11 -0700
changeset 5483 ccec742c9b7d
parent 5285 ee03772989eb
permissions -rw-r--r--
Removed unreferenced variable
slouken@3277
     1
/*
slouken@3277
     2
 *	touch.c
slouken@3277
     3
 *	written by Holmes Futrell
slouken@3277
     4
 *	use however you want
slouken@3277
     5
 */
slouken@3277
     6
slouken@3277
     7
#include "SDL.h"
slouken@3277
     8
#include "math.h"
slouken@3277
     9
#include "common.h"
slouken@3277
    10
slouken@3277
    11
#define BRUSH_SIZE 32           /* width and height of the brush */
slouken@3277
    12
#define PIXELS_PER_ITERATION 5  /* number of pixels between brush blots when forming a line */
slouken@3277
    13
slouken@3685
    14
static SDL_Texture *brush = 0;       /* texture for the brush */
slouken@3277
    15
slouken@3277
    16
/*
slouken@3277
    17
	draws a line from (startx, starty) to (startx + dx, starty + dy)
slouken@3277
    18
	this is accomplished by drawing several blots spaced PIXELS_PER_ITERATION apart
slouken@3277
    19
*/
slouken@3277
    20
void
slouken@5208
    21
drawLine(SDL_Renderer *renderer, float startx, float starty, float dx, float dy)
slouken@3277
    22
{
slouken@3277
    23
slouken@3277
    24
    float distance = sqrt(dx * dx + dy * dy);   /* length of line segment (pythagoras) */
slouken@3277
    25
    int iterations = distance / PIXELS_PER_ITERATION + 1;       /* number of brush sprites to draw for the line */
slouken@3277
    26
    float dx_prime = dx / iterations;   /* x-shift per iteration */
slouken@3277
    27
    float dy_prime = dy / iterations;   /* y-shift per iteration */
slouken@3277
    28
    SDL_Rect dstRect;           /* rect to draw brush sprite into */
slouken@3277
    29
slouken@3277
    30
    dstRect.w = BRUSH_SIZE;
slouken@3277
    31
    dstRect.h = BRUSH_SIZE;
slouken@3277
    32
slouken@3277
    33
    /* setup x and y for the location of the first sprite */
slouken@3277
    34
    float x = startx - BRUSH_SIZE / 2.0f;
slouken@3277
    35
    float y = starty - BRUSH_SIZE / 2.0f;
slouken@3277
    36
slouken@3277
    37
    int i;
slouken@3277
    38
    /* draw a series of blots to form the line */
slouken@3277
    39
    for (i = 0; i < iterations; i++) {
slouken@3277
    40
        dstRect.x = x;
slouken@3277
    41
        dstRect.y = y;
slouken@3277
    42
        /* shift x and y for next sprite location */
slouken@3277
    43
        x += dx_prime;
slouken@3277
    44
        y += dy_prime;
slouken@3277
    45
        /* draw brush blot */
slouken@5208
    46
        SDL_RenderCopy(renderer, brush, NULL, &dstRect);
slouken@3277
    47
    }
slouken@3277
    48
}
slouken@3277
    49
slouken@3277
    50
/*
slouken@3277
    51
	loads the brush texture
slouken@3277
    52
*/
slouken@3277
    53
void
slouken@5208
    54
initializeTexture(SDL_Renderer *renderer)
slouken@3277
    55
{
slouken@3277
    56
    SDL_Surface *bmp_surface;
slouken@3277
    57
    bmp_surface = SDL_LoadBMP("stroke.bmp");
slouken@3277
    58
    if (bmp_surface == NULL) {
slouken@3277
    59
        fatalError("could not load stroke.bmp");
slouken@3277
    60
    }
slouken@3685
    61
    brush =
slouken@5208
    62
        SDL_CreateTextureFromSurface(renderer, bmp_surface);
slouken@3277
    63
    SDL_FreeSurface(bmp_surface);
slouken@3685
    64
    if (brush == 0) {
slouken@3277
    65
        fatalError("could not create brush texture");
slouken@3277
    66
    }
slouken@3277
    67
    /* additive blending -- laying strokes on top of eachother makes them brighter */
slouken@3685
    68
    SDL_SetTextureBlendMode(brush, SDL_BLENDMODE_ADD);
slouken@3277
    69
    /* set brush color (red) */
slouken@3685
    70
    SDL_SetTextureColorMod(brush, 255, 100, 100);
slouken@3277
    71
}
slouken@3277
    72
slouken@3277
    73
int
slouken@3277
    74
main(int argc, char *argv[])
slouken@3277
    75
{
slouken@3277
    76
slouken@3277
    77
    int x, y, dx, dy;           /* mouse location          */
slouken@3277
    78
    Uint8 state;                /* mouse (touch) state */
slouken@3277
    79
    SDL_Event event;
slouken@3685
    80
    SDL_Window *window;         /* main window */
slouken@5208
    81
	SDL_Renderer *renderer;
slouken@3277
    82
    int done;                   /* does user want to quit? */
slouken@3277
    83
slouken@3277
    84
    /* initialize SDL */
slouken@3277
    85
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
slouken@3277
    86
        fatalError("Could not initialize SDL");
slouken@3277
    87
    }
slouken@3277
    88
slouken@3277
    89
    /* create main window and renderer */
slouken@3685
    90
    window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
slouken@3277
    91
                                SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN |
slouken@3277
    92
                                SDL_WINDOW_BORDERLESS);
slouken@5208
    93
    renderer = SDL_CreateRenderer(window, 0, 0);
slouken@3277
    94
slouken@3277
    95
    /*load brush texture */
slouken@5208
    96
    initializeTexture(renderer);
slouken@3277
    97
slouken@3277
    98
    /* fill canvass initially with all black */
slouken@5208
    99
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
slouken@5285
   100
    SDL_RenderClear(renderer);
slouken@5208
   101
    SDL_RenderPresent(renderer);
slouken@3277
   102
slouken@3277
   103
    done = 0;
slouken@3277
   104
    while (!done && SDL_WaitEvent(&event)) {
slouken@3277
   105
        switch (event.type) {
slouken@3277
   106
        case SDL_QUIT:
slouken@3277
   107
            done = 1;
slouken@3277
   108
            break;
slouken@3277
   109
        case SDL_MOUSEMOTION:
slouken@3277
   110
            state = SDL_GetMouseState(&x, &y);  /* get its location */
slouken@3277
   111
            SDL_GetRelativeMouseState(&dx, &dy);        /* find how much the mouse moved */
slouken@3277
   112
            if (state & SDL_BUTTON_LMASK) {     /* is the mouse (touch) down? */
slouken@5208
   113
                drawLine(renderer, x - dx, y - dy, dx, dy);       /* draw line segment */
slouken@5208
   114
                SDL_RenderPresent(renderer);
slouken@3277
   115
            }
slouken@3277
   116
            break;
slouken@3277
   117
        }
slouken@3277
   118
    }
slouken@3277
   119
slouken@3277
   120
    /* cleanup */
slouken@3685
   121
    SDL_DestroyTexture(brush);
slouken@3277
   122
    SDL_Quit();
slouken@3277
   123
slouken@3277
   124
    return 0;
slouken@3277
   125
}