[ create a new paste ] login | about

Link: http://codepad.org/6GwlXXhD    [ raw code | fork ]

C, pasted on Jun 4:
#include "SDL2/SDL.h"

typedef struct {
    SDL_Rect r;
    float    x;
    int      vel;
} Rect;

int main (int argc, char *argv[])
{
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_Window* win =
        SDL_CreateWindow("SDL 8", 500, 1300, 1024, 768, SDL_WINDOW_SHOWN);
    SDL_Renderer* ren =
        SDL_CreateRenderer(win, -1, 0);

    Rect rs[20000];
    int i;
    for (i=0; i<20000; i++) {
        rs[i].r.w = 10;
        rs[i].r.h = 10;
        rs[i].r.y = rand()%750;
        rs[i].x   = 50;
        rs[i].vel = 20 + rand()%100;
    }

    int old = SDL_GetTicks();
    for (;;)
    {
        // calculate DT
        int now = SDL_GetTicks();
        int dt = now - old;
        old = now;

        // update all RS
        for (i=0; i<20000; i++) {
            rs[i].x += ((float)rs[i].vel*dt)/1000;
        }

        // clear screen
        SDL_SetRenderDrawColor(ren, 0,0,0,0xFF);
        SDL_Rect bg = { 0,0,1024,768 };
        SDL_RenderFillRect(ren, &bg);

        // draw all RS
        for (i=0; i<20000; i++) {
            rs[i].r.x = ((int)rs[i].x) % 950;
            SDL_SetRenderDrawColor(ren, 0xFF,0xFF,0xFF,0xFF);
            SDL_RenderFillRect(ren, &rs[i].r);
        }

        // update screen
        SDL_RenderPresent(ren);
    }
}


Create a new paste based on this one


Comments: