-
Notifications
You must be signed in to change notification settings - Fork 160
/
animation_random_walk.c
62 lines (56 loc) · 1.72 KB
/
animation_random_walk.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
Random walk.
*/
#include "common.h"
#define WINDOW_WIDTH 600
#define RECT_WIDTH (WINDOW_WIDTH / 30)
#define MAX_RECT_POS (WINDOW_WIDTH - RECT_WIDTH)
#define SCREENS_PER_SECOND 10.0
double rand_one() {
return -1.0 + 2.0 * (rand() / (double)RAND_MAX);
}
double get_coord(double *x, double magnitude) {
*x += magnitude * rand_one();
*x = fmod(*x, MAX_RECT_POS);
if (*x < 0)
*x = MAX_RECT_POS;
return *x;
}
int main(void) {
SDL_Event event;
SDL_Rect rect;
SDL_Renderer *renderer;
SDL_Window *window;
double x, y, magnitude;
unsigned int current_time, last_time;
srand(time(NULL));
SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO);
SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_WIDTH, 0, &window, &renderer);
rect.x = WINDOW_WIDTH / 2;
rect.y = WINDOW_WIDTH / 2;
rect.w = RECT_WIDTH;
rect.h = RECT_WIDTH;
last_time = SDL_GetTicks();
common_fps_init();
while (1) {
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
break;
current_time = SDL_GetTicks();
if (current_time != last_time) {
magnitude = SCREENS_PER_SECOND * MAX_RECT_POS * (current_time - last_time) / 1000.0;
rect.x = get_coord(&x, magnitude);
rect.y = get_coord(&y, magnitude);
last_time = current_time;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderFillRect(renderer, &rect);
SDL_RenderPresent(renderer);
}
common_fps_update_and_print();
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_SUCCESS;
}