sdl

FORK: Simple Directmedia Layer
git clone https://git.neptards.moe/neptards/sdl.git
Log | Files | Refs

happy.c (4607B)


      1 /*
      2  *  happy.c
      3  *  written by Holmes Futrell
      4  *  use however you want
      5  */
      6 
      7 #include "SDL.h"
      8 #include "common.h"
      9 
     10 #define NUM_HAPPY_FACES 100     /* number of faces to draw */
     11 #define HAPPY_FACE_SIZE 32      /* width and height of happyface */
     12 
     13 static SDL_Texture *texture = 0;    /* reference to texture holding happyface */
     14 
     15 static struct
     16 {
     17     float x, y;                 /* position of happyface */
     18     float xvel, yvel;           /* velocity of happyface */
     19 } faces[NUM_HAPPY_FACES];
     20 
     21 /*
     22     Sets initial positions and velocities of happyfaces
     23     units of velocity are pixels per millesecond
     24 */
     25 void
     26 initializeHappyFaces(SDL_Renderer *renderer)
     27 {
     28     int i;
     29     int w;
     30     int h;
     31     SDL_RenderGetLogicalSize(renderer, &w, &h);
     32 
     33     for (i = 0; i < NUM_HAPPY_FACES; i++) {
     34         faces[i].x = randomFloat(0.0f, w - HAPPY_FACE_SIZE);
     35         faces[i].y = randomFloat(0.0f, h - HAPPY_FACE_SIZE);
     36         faces[i].xvel = randomFloat(-60.0f, 60.0f);
     37         faces[i].yvel = randomFloat(-60.0f, 60.0f);
     38     }
     39 }
     40 
     41 void
     42 render(SDL_Renderer *renderer, double deltaTime)
     43 {
     44     int i;
     45     SDL_Rect srcRect;
     46     SDL_Rect dstRect;
     47     int w;
     48     int h;
     49 
     50     SDL_RenderGetLogicalSize(renderer, &w, &h);
     51 
     52     /* setup boundaries for happyface bouncing */
     53     int maxx = w - HAPPY_FACE_SIZE;
     54     int maxy = h - HAPPY_FACE_SIZE;
     55     int minx = 0;
     56     int miny = 0;
     57 
     58     /* setup rects for drawing */
     59     srcRect.x = 0;
     60     srcRect.y = 0;
     61     srcRect.w = HAPPY_FACE_SIZE;
     62     srcRect.h = HAPPY_FACE_SIZE;
     63     dstRect.w = HAPPY_FACE_SIZE;
     64     dstRect.h = HAPPY_FACE_SIZE;
     65 
     66     /* fill background in with black */
     67     SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
     68     SDL_RenderClear(renderer);
     69 
     70     /*
     71        loop through all the happy faces:
     72        - update position
     73        - update velocity (if boundary is hit)
     74        - draw
     75      */
     76     for (i = 0; i < NUM_HAPPY_FACES; i++) {
     77         faces[i].x += faces[i].xvel * deltaTime;
     78         faces[i].y += faces[i].yvel * deltaTime;
     79         if (faces[i].x > maxx) {
     80             faces[i].x = maxx;
     81             faces[i].xvel = -faces[i].xvel;
     82         } else if (faces[i].y > maxy) {
     83             faces[i].y = maxy;
     84             faces[i].yvel = -faces[i].yvel;
     85         }
     86         if (faces[i].x < minx) {
     87             faces[i].x = minx;
     88             faces[i].xvel = -faces[i].xvel;
     89         } else if (faces[i].y < miny) {
     90             faces[i].y = miny;
     91             faces[i].yvel = -faces[i].yvel;
     92         }
     93         dstRect.x = faces[i].x;
     94         dstRect.y = faces[i].y;
     95         SDL_RenderCopy(renderer, texture, &srcRect, &dstRect);
     96     }
     97     /* update screen */
     98     SDL_RenderPresent(renderer);
     99 
    100 }
    101 
    102 /*
    103     loads the happyface graphic into a texture
    104 */
    105 void
    106 initializeTexture(SDL_Renderer *renderer)
    107 {
    108     SDL_Surface *bmp_surface;
    109     /* load the bmp */
    110     bmp_surface = SDL_LoadBMP("icon.bmp");
    111     if (bmp_surface == NULL) {
    112         fatalError("could not load bmp");
    113     }
    114     /* set white to transparent on the happyface */
    115     SDL_SetColorKey(bmp_surface, 1,
    116                     SDL_MapRGB(bmp_surface->format, 255, 255, 255));
    117 
    118     /* convert RGBA surface to texture */
    119     texture = SDL_CreateTextureFromSurface(renderer, bmp_surface);
    120     if (texture == 0) {
    121         fatalError("could not create texture");
    122     }
    123     SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
    124 
    125     /* free up allocated memory */
    126     SDL_FreeSurface(bmp_surface);
    127 }
    128 
    129 int
    130 main(int argc, char *argv[])
    131 {
    132     SDL_Window *window;
    133     SDL_Renderer *renderer;
    134     int done;
    135     int width;
    136     int height;
    137 
    138     /* initialize SDL */
    139     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    140         fatalError("Could not initialize SDL");
    141     }
    142 
    143     /* The specified window size doesn't matter - except for its aspect ratio,
    144      * which determines whether the window is in portrait or landscape on iOS
    145      * (if SDL_WINDOW_RESIZABLE isn't specified). */
    146     window = SDL_CreateWindow(NULL, 0, 0, 320, 480, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_ALLOW_HIGHDPI);
    147 
    148     renderer = SDL_CreateRenderer(window, -1, 0);
    149 
    150     SDL_GetWindowSize(window, &width, &height);
    151     SDL_RenderSetLogicalSize(renderer, width, height);
    152 
    153     initializeTexture(renderer);
    154     initializeHappyFaces(renderer);
    155 
    156 
    157     /* main loop */
    158     done = 0;
    159     while (!done) {
    160         SDL_Event event;
    161         double deltaTime = updateDeltaTime();
    162 
    163         while (SDL_PollEvent(&event)) {
    164             if (event.type == SDL_QUIT) {
    165                 done = 1;
    166             }
    167         }
    168 
    169         render(renderer, deltaTime);
    170         SDL_Delay(1);
    171     }
    172 
    173     /* cleanup */
    174     SDL_DestroyTexture(texture);
    175     /* shutdown SDL */
    176     SDL_Quit();
    177 
    178     return 0;
    179 
    180 }