Skip to content

Commit a55cae5

Browse files
committed
Add FPS to all examples, add mouse examples
1 parent ac2dd35 commit a55cae5

13 files changed

+201
-35
lines changed

sdl/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
1. Examples
55
1. [SDL_RenderDrawPoint](render_draw_point.c)
66
1. [SDL_RenderDrawRect](render_draw_rect.c)
7+
1. [Render draw point rect](render_draw_point_rect.c)
78
1. [Check A pressed](check_a_pressed.c)
89
1. [Error handling](error_handling.c)
910
1. [Animation](animation.c)
@@ -17,6 +18,9 @@
1718
1. [heatmap](heatmap.c)
1819
1. [heatmap streaming](heatmap_streaming.c)
1920
1. [heatmap shader](heatmap_shader.c)
21+
1. [Mouse](mouse.c)
22+
1. [Mouse rect](mouse_rect.c)
23+
1. [Mouse point rect](mouse_point_rect.c)
2024
1. [OpenGL](opengl.c)
2125
1. Audio
2226
1. [Sound frequency](sound_frequency.c)

sdl/animation.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ TODO find source saying the good way to control speed.
44
Simple movement that has an absolute equation.
55
*/
66

7-
#include <stdlib.h>
8-
9-
#include <SDL2/SDL.h>
7+
#include "common.h"
108

119
#define WINDOW_WIDTH 600
1210
#define RECT_WIDTH WINDOW_WIDTH / 30
@@ -26,6 +24,7 @@ int main(void) {
2624
rect.w = RECT_WIDTH;
2725
rect.h = RECT_WIDTH;
2826
initial_time = SDL_GetTicks();
27+
common_fps_init();
2928
while (1) {
3029
rect.x = ((int)(
3130
SCREENS_PER_SECOND
@@ -39,6 +38,7 @@ int main(void) {
3938
SDL_RenderPresent(renderer);
4039
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
4140
break;
41+
common_fps_update_and_print();
4242
}
4343
SDL_DestroyRenderer(renderer);
4444
SDL_DestroyWindow(window);

sdl/animation_random_walk.c

+5-9
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
Random walk.
33
*/
44

5-
#include <stdlib.h>
6-
#include <time.h>
7-
8-
#include <SDL2/SDL.h>
5+
#include "common.h"
96

107
#define WINDOW_WIDTH 600
118
#define RECT_WIDTH (WINDOW_WIDTH / 30)
@@ -29,11 +26,8 @@ int main(void) {
2926
SDL_Rect rect;
3027
SDL_Renderer *renderer;
3128
SDL_Window *window;
32-
unsigned int current_time;
33-
double magnitude;
34-
unsigned int last_time;
35-
double x;
36-
double y;
29+
double x, y, magnitude;
30+
unsigned int current_time, last_time;
3731

3832
srand(time(NULL));
3933
SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO);
@@ -43,6 +37,7 @@ int main(void) {
4337
rect.w = RECT_WIDTH;
4438
rect.h = RECT_WIDTH;
4539
last_time = SDL_GetTicks();
40+
common_fps_init();
4641
while (1) {
4742
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
4843
break;
@@ -58,6 +53,7 @@ int main(void) {
5853
SDL_RenderFillRect(renderer, &rect);
5954
SDL_RenderPresent(renderer);
6055
}
56+
common_fps_update_and_print();
6157
}
6258
SDL_DestroyRenderer(renderer);
6359
SDL_DestroyWindow(window);

sdl/animation_user_control.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ Use controls a square.
44
https://www.libsdl.org/release/SDL-1.2.15/docs/html/guideinputkeyboard.html
55
*/
66

7-
#include <stdlib.h>
8-
#include <time.h>
9-
10-
#include <SDL2/SDL.h>
7+
#include "common.h"
118

129
#define WINDOW_WIDTH 600
1310
#define RECT_WIDTH (WINDOW_WIDTH / 30)
@@ -40,11 +37,13 @@ int main(void) {
4037

4138
SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO);
4239
SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_WIDTH, 0, &window, &renderer);
40+
SDL_SetWindowTitle(window, "arrow keys: move");
4341
rect.x = WINDOW_WIDTH / 2;
4442
rect.y = WINDOW_WIDTH / 2;
4543
rect.w = RECT_WIDTH;
4644
rect.h = RECT_WIDTH;
4745
last_time = SDL_GetTicks();
46+
common_fps_init();
4847
while (!quit) {
4948
while (SDL_PollEvent(&event) == 1) {
5049
if (event.type == SDL_QUIT) {
@@ -97,6 +96,7 @@ int main(void) {
9796
SDL_RenderFillRect(renderer, &rect);
9897
SDL_RenderPresent(renderer);
9998
}
99+
common_fps_update_and_print();
100100
}
101101
SDL_DestroyRenderer(renderer);
102102
SDL_DestroyWindow(window);

sdl/animation_user_control_discrete.c

+10-7
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ Using float coordinates would lead to an internal invisible state,
55
which is weird (you can be either at the left or right corner of the active square).
66
*/
77

8-
#include <stdlib.h>
9-
10-
#include <SDL2/SDL.h>
8+
#include "common.h"
119

1210
#define WINDOW_WIDTH 600
1311
#define RECTS_PER_WINDOW (10)
@@ -28,17 +26,21 @@ int main(void) {
2826
SDL_Renderer *renderer;
2927
SDL_Window *window;
3028
double current_time_s, last_key_pressed_time_s;
31-
unsigned int x = RECTS_PER_WINDOW / 2, y = RECTS_PER_WINDOW / 2;
32-
int quit = 0;
33-
int speed_x = 0, speed_y = 0;
34-
int key_pressed;
29+
unsigned int x = RECTS_PER_WINDOW / 2,
30+
y = RECTS_PER_WINDOW / 2;
31+
int key_pressed,
32+
quit = 0,
33+
speed_x = 0,
34+
speed_y = 0;
3535
unsigned int current_time, last_time;
3636

3737
SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO);
3838
SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_WIDTH, 0, &window, &renderer);
39+
SDL_SetWindowTitle(window, "arrow keys: move");
3940
rect.w = RECT_WIDTH;
4041
rect.h = RECT_WIDTH;
4142
last_key_pressed_time_s = SDL_GetTicks() / 1000.0;
43+
common_fps_init();
4244
while (!quit) {
4345
current_time = SDL_GetTicks();
4446
current_time_s = current_time / 1000.0;
@@ -98,6 +100,7 @@ int main(void) {
98100
SDL_RenderPresent(renderer);
99101
}
100102
last_time = current_time;
103+
common_fps_update_and_print();
101104
}
102105
SDL_DestroyRenderer(renderer);
103106
SDL_DestroyWindow(window);

sdl/heatmap_streaming.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ int main(void) {
3636
for (y = 0; y < WINDOW_HEIGHT; y++) {
3737
xc = CENTER_X - x;
3838
yc = CENTER_Y - y;
39-
z = COLOR_MAX * 0.5 * (1.0 + (sin(PI2 * (sqrt(xc*xc + yc*yc) - SPEED * dt) / PERIOD)));
40-
/*z = (int)(x + y + SPEED * dt) % COLOR_MAX;*/
39+
/*z = COLOR_MAX * 0.5 * (1.0 + (sin(PI2 * (sqrt(xc*xc + yc*yc) - SPEED * dt) / PERIOD)));*/
40+
z = (int)(x + y + SPEED * dt) % COLOR_MAX;
4141
base = ((Uint8 *)pixels) + (4 * (x * WINDOW_WIDTH + y));
4242
base[0] = 0;
4343
base[1] = 0;

sdl/mouse.c

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* Print mouse position to stdout. */
2+
3+
#include "common.h"
4+
5+
int main(void) {
6+
SDL_Event event;
7+
SDL_Window *window;
8+
int x, y, last_x, last_y;
9+
window = SDL_CreateWindow(__FILE__, 0, 0, 500, 500, SDL_WINDOW_OPENGL);
10+
x = 0;
11+
y = 0;
12+
last_x = 0;
13+
last_y = 0;
14+
common_fps_init();
15+
while (1) {
16+
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
17+
break;
18+
SDL_GetMouseState(&x, &y);
19+
if (x != last_x || y != last_y) {
20+
printf("%d %d\n", x, y);
21+
last_x = x;
22+
last_y = y;
23+
}
24+
common_fps_update_and_print();
25+
}
26+
SDL_DestroyWindow(window);
27+
SDL_Quit();
28+
return EXIT_SUCCESS;
29+
}

sdl/mouse_point_rect.c

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "common.h"
2+
3+
#define WINDOW_WIDTH 1000
4+
#define WINDOW_HEIGHT WINDOW_WIDTH
5+
6+
int main(void) {
7+
SDL_Event event;
8+
SDL_Rect rect;
9+
SDL_Renderer *renderer;
10+
SDL_Window *window;
11+
int i, j, x, y, half_w, half_h;
12+
13+
SDL_Init(SDL_INIT_VIDEO);
14+
SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_WIDTH, 0, &window, &renderer);
15+
rect.x = WINDOW_WIDTH / 2;
16+
rect.y = WINDOW_WIDTH / 2;
17+
rect.w = 3 * WINDOW_WIDTH / 4;
18+
rect.h = 3 * WINDOW_WIDTH / 4;
19+
half_w = rect.w / 2;
20+
half_h = rect.h / 2;
21+
common_fps_init();
22+
while (1) {
23+
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
24+
break;
25+
SDL_GetMouseState(&x, &y);
26+
if (x != rect.x || y != rect.y) {
27+
rect.x = x - half_w;
28+
rect.y = y - half_h;
29+
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
30+
SDL_RenderClear(renderer);
31+
for (i = 0; i < rect.w; ++i) {
32+
for (j = 0; j < rect.h; ++j) {
33+
if ((i + j) & 1) {
34+
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
35+
SDL_RenderDrawPoint(renderer, x + i - half_w, y + j - half_h);
36+
}
37+
}
38+
}
39+
SDL_RenderPresent(renderer);
40+
}
41+
common_fps_update_and_print();
42+
}
43+
SDL_DestroyRenderer(renderer);
44+
SDL_DestroyWindow(window);
45+
SDL_Quit();
46+
return EXIT_SUCCESS;
47+
}

sdl/mouse_rect.c

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "common.h"
2+
3+
#define WINDOW_WIDTH 600
4+
5+
int main(void) {
6+
SDL_Event event;
7+
SDL_Rect rect;
8+
SDL_Renderer *renderer;
9+
SDL_Window *window;
10+
int x, y, half_w, half_h;
11+
12+
SDL_Init(SDL_INIT_VIDEO);
13+
SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_WIDTH, 0, &window, &renderer);
14+
rect.x = WINDOW_WIDTH / 2;
15+
rect.y = WINDOW_WIDTH / 2;
16+
rect.w = WINDOW_WIDTH / 6;
17+
rect.h = WINDOW_WIDTH / 6;
18+
half_w = rect.w / 2;
19+
half_h = rect.h / 2;
20+
common_fps_init();
21+
while (1) {
22+
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
23+
SDL_RenderClear(renderer);
24+
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
25+
SDL_RenderFillRect(renderer, &rect);
26+
SDL_RenderPresent(renderer);
27+
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
28+
break;
29+
SDL_GetMouseState(&x, &y);
30+
if (x != rect.x || y != rect.y) {
31+
rect.x = x - half_w;
32+
rect.y = y - half_h;
33+
}
34+
common_fps_update_and_print();
35+
}
36+
SDL_DestroyRenderer(renderer);
37+
SDL_DestroyWindow(window);
38+
SDL_Quit();
39+
return EXIT_SUCCESS;
40+
}

sdl/render_draw_point.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
http://stackoverflow.com/questions/20579658/pixel-drawing-in-sdl2-0
33
*/
44

5-
#include <stdlib.h>
6-
7-
#include <SDL2/SDL.h>
5+
#include "common.h"
86

97
#define WINDOW_WIDTH 600
108

@@ -22,9 +20,11 @@ int main(void) {
2220
for (i = 0; i < WINDOW_WIDTH; ++i)
2321
SDL_RenderDrawPoint(renderer, i, i);
2422
SDL_RenderPresent(renderer);
23+
common_fps_init();
2524
while (1) {
2625
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
2726
break;
27+
common_fps_update_and_print();
2828
}
2929
SDL_DestroyRenderer(renderer);
3030
SDL_DestroyWindow(window);

sdl/render_draw_point_rect.c

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Draw a rectangle with points.
3+
4+
Bad idea, because the GPU can make that operation faster for quads,
5+
and SDL_rect uses that power by default.
6+
7+
So just to see how bad the naive method is.
8+
*/
9+
10+
#include "common.h"
11+
12+
#define WINDOW_WIDTH 500
13+
#define WINDOW_HEIGHT WINDOW_WIDTH
14+
15+
int main(void) {
16+
SDL_Event event;
17+
SDL_Renderer *renderer;
18+
SDL_Window *window;
19+
int x, y;
20+
21+
SDL_Init(SDL_INIT_VIDEO);
22+
SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_WIDTH, 0, &window, &renderer);
23+
SDL_RenderPresent(renderer);
24+
common_fps_init();
25+
while (1) {
26+
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
27+
break;
28+
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
29+
SDL_RenderClear(renderer);
30+
for (x = 0; x < WINDOW_WIDTH; ++x) {
31+
for (y = 0; y < WINDOW_HEIGHT; ++y) {
32+
if ((x + y) & 1) {
33+
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
34+
SDL_RenderDrawPoint(renderer, x, y);
35+
}
36+
}
37+
}
38+
SDL_RenderPresent(renderer);
39+
common_fps_update_and_print();
40+
}
41+
SDL_DestroyRenderer(renderer);
42+
SDL_DestroyWindow(window);
43+
SDL_Quit();
44+
return EXIT_SUCCESS;
45+
}

sdl/render_draw_rect.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#include <stdlib.h>
2-
3-
#include <SDL2/SDL.h>
1+
#include "common.h"
42

53
#define WINDOW_WIDTH 600
64

@@ -19,15 +17,20 @@ int main(void) {
1917
rect.y = WINDOW_WIDTH / 3;
2018
rect.w = WINDOW_WIDTH / 6;
2119
rect.h = WINDOW_WIDTH / 12;
20+
2221
/* Make it filled. */
2322
/*SDL_RenderFillRect(renderer, &rect);*/
2423
/*SDL_RenderDrawRect(renderer, &rect);*/
24+
2525
/* Same as the above. */
2626
SDL_RenderFillRect(renderer, &rect);
2727
SDL_RenderPresent(renderer);
28+
29+
common_fps_init();
2830
while (1) {
2931
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
3032
break;
33+
common_fps_update_and_print();
3134
}
3235
SDL_DestroyRenderer(renderer);
3336
SDL_DestroyWindow(window);

0 commit comments

Comments
 (0)