Skip to content

Commit ac2dd35

Browse files
committed
Split SDL heatmap streaming
1 parent 5178f38 commit ac2dd35

File tree

5 files changed

+70
-26
lines changed

5 files changed

+70
-26
lines changed

bullet/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Tested on Bullet 2.83, Ubuntu 16.04.
99
1. [Build](build.md)
1010
1. [Source code](source-code.md)
1111
1. [Users](users.md)
12+
1. [Benchmarks](benchmarks.md)
1213
1. Examples
1314
1. [Gravity](gravity.cpp)
1415
1. [Gravity collision](gravity_collision.cpp)

bullet/benchmarks.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Benchmarks
2+
3+
<https://grasp.robotics.cs.rpi.edu/bpmd/>

sdl/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
1. Plots
1616
1. [heatmap 1D](heatmap1d.c)
1717
1. [heatmap](heatmap.c)
18+
1. [heatmap streaming](heatmap_streaming.c)
1819
1. [heatmap shader](heatmap_shader.c)
1920
1. [OpenGL](opengl.c)
2021
1. Audio

sdl/heatmap.c

+6-26
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,26 @@ TODO: why do I see some tearing. vsync problem?
2929

3030
#include "common.h"
3131

32-
/* Input parameters. Play with those to try to increase FPS. */
32+
/*
33+
Turn SDL on or off.
34+
If off, no window is created, but computations are still run.
35+
Play with those to try to increase FPS and find out if the limiting factor is computation or display.
36+
*/
3337
#define SDL 1
34-
#define STREAMING (SDL && 1)
3538

3639
#define COLOR_MAX 255
3740

3841
int main(void) {
3942
#if SDL
4043
SDL_Event event;
41-
SDL_Rect rect;
4244
SDL_Renderer *renderer;
4345
SDL_Window *window;
44-
# if STREAMING
4546
SDL_Texture *texture = NULL;
4647
void *pixels;
4748
Uint8 *base;
4849
/* TODO: is it mandatory to use this? Maybe SDL_LockTexture
4950
* will make the array non continuous to improve alignment? */
5051
int pitch;
51-
# endif
5252
#else
5353
double sum;
5454
#endif
@@ -68,47 +68,27 @@ int main(void) {
6868
#if SDL
6969
SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO);
7070
SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_WIDTH, 0, &window, &renderer);
71-
# if STREAMING
72-
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
73-
SDL_TEXTUREACCESS_STREAMING, WINDOW_WIDTH, WINDOW_HEIGHT);
74-
# endif
7571
#endif
7672
initial_time = common_get_secs();
7773
common_fps_init();
7874
while (1) {
7975
dt = common_get_secs() - initial_time;
80-
#if STREAMING
81-
SDL_LockTexture(texture, NULL, &pixels, &pitch);
82-
#endif
8376
for (x = 0; x < WINDOW_WIDTH; x++) {
8477
for (y = 0; y < WINDOW_HEIGHT; y++) {
8578
xc = CENTER_X - x;
8679
yc = CENTER_Y - y;
8780
z = COLOR_MAX * 0.5 * (1.0 + (sin(PI2 * (sqrt(xc*xc + yc*yc) - SPEED * dt) / PERIOD)));
81+
/*z = (int)(x + y + SPEED * dt) % COLOR_MAX;*/
8882
#if SDL
89-
# if STREAMING
90-
base = ((Uint8 *)pixels) + (4 * (x * WINDOW_WIDTH + y));
91-
base[0] = 0;
92-
base[1] = 0;
93-
base[2] = z;
94-
base[3] = COLOR_MAX;
95-
# else
9683
SDL_SetRenderDrawColor(renderer, z, 0, 0, COLOR_MAX);
9784
SDL_RenderDrawPoint(renderer, x, y);
98-
# endif
9985
#else
10086
sum += z;
10187
#endif
10288
}
10389
}
10490
#if SDL
105-
# if STREAMING
106-
SDL_UnlockTexture(texture);
107-
SDL_RenderCopy(renderer, texture, NULL, NULL);
108-
# endif
10991
SDL_RenderPresent(renderer);
110-
#endif
111-
#if SDL
11292
common_fps_update_and_print();
11393
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
11494
break;

sdl/heatmap_streaming.c

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include "common.h"
2+
3+
#define COLOR_MAX 255
4+
5+
int main(void) {
6+
SDL_Event event;
7+
SDL_Renderer *renderer = NULL;
8+
SDL_Texture *texture = NULL;
9+
SDL_Window *window = NULL;
10+
Uint8 *base;
11+
int pitch;
12+
void *pixels = NULL;
13+
const unsigned int
14+
WINDOW_WIDTH = 500,
15+
WINDOW_HEIGHT = WINDOW_WIDTH;
16+
const double
17+
SPEED = WINDOW_WIDTH / 10.0,
18+
CENTER_X = WINDOW_WIDTH / 2.0,
19+
CENTER_Y = WINDOW_HEIGHT / 2.0,
20+
PERIOD = WINDOW_WIDTH / 10.0,
21+
PI2 = 2.0 * acos(-1.0);
22+
double dt, initial_time;
23+
float z;
24+
unsigned int x, xc, y, yc;
25+
26+
SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO);
27+
SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_WIDTH, 0, &window, &renderer);
28+
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
29+
SDL_TEXTUREACCESS_STREAMING, WINDOW_WIDTH, WINDOW_HEIGHT);
30+
initial_time = common_get_secs();
31+
common_fps_init();
32+
while (1) {
33+
dt = common_get_secs() - initial_time;
34+
SDL_LockTexture(texture, NULL, &pixels, &pitch);
35+
for (x = 0; x < WINDOW_WIDTH; x++) {
36+
for (y = 0; y < WINDOW_HEIGHT; y++) {
37+
xc = CENTER_X - x;
38+
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;*/
41+
base = ((Uint8 *)pixels) + (4 * (x * WINDOW_WIDTH + y));
42+
base[0] = 0;
43+
base[1] = 0;
44+
base[2] = z;
45+
base[3] = COLOR_MAX;
46+
}
47+
}
48+
SDL_UnlockTexture(texture);
49+
SDL_RenderCopy(renderer, texture, NULL, NULL);
50+
SDL_RenderPresent(renderer);
51+
common_fps_update_and_print();
52+
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
53+
break;
54+
}
55+
SDL_DestroyRenderer(renderer);
56+
SDL_DestroyWindow(window);
57+
SDL_Quit();
58+
return EXIT_SUCCESS;
59+
}

0 commit comments

Comments
 (0)