cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

touch.c (3854B)


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