Skip to content

Commit 9214d7e

Browse files
committed
sdl fun
1 parent 700f105 commit 9214d7e

22 files changed

Lines changed: 469 additions & 71 deletions

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ C and C++ minimal examples. Asserts used wherever possible. Cheatsheets, tutoria
4444
1. [Qt](qt/)
4545
1. [X11](x11)
4646
1. [Open GL](opengl/)
47+
1. [SDL](sdl/)
4748
1. Scientific
4849
1. [Fortran](fortran/)
4950
1. [GSL](gsl/)

‎gsl/Makefile_params‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
LIBS := -lgsl -lgslcblas -lm -lplplotd
1+
LIBS := -lgsl -lgslcblas -lm -lplplotd

‎gsl/README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# GSL
2+
3+
Gnu Scientific Library.
4+
5+
<https://en.wikipedia.org/wiki/GNU_Scientific_Library>

‎gsl/main.c‎

Lines changed: 63 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <assert.h>
22
#include <math.h>
33
#include <stdio.h>
4+
#include <stdlib.h>
45

56
#include <gsl/gsl_errno.h>
67
#include <gsl/gsl_matrix.h>
@@ -19,49 +20,46 @@ where
1920
2021
$Y(t) = ( y(t), y'(t), y''(t), ... )$
2122
*/
22-
int ode_func( double t, const double y[], double f[], void *params )
23-
{
23+
int ode_func(double t, const double y[], double f[], void *params) {
2424
double mu = *((double*)params);
2525
f[0] = y[1];
2626
f[1] = -y[0] - mu*y[1]*(y[0]*y[0] - 1);
2727
return GSL_SUCCESS;
2828
}
2929

3030
/*
31-
some methods ask for you to give the jacobian of f too
31+
Some methods ask for you to give the jacobian of it too
3232
for better performance.
3333
34-
you have to calculate this by hand.
34+
You have to calculate this by hand.
3535
*/
36-
int ode_jac( double t, const double y[], double *dfdy, double dfdt[], void *params )
37-
{
36+
int ode_jac(double t, const double y[], double *dfdy, double dfdt[], void *params) {
3837
double mu = *((double*)params);
3938
gsl_matrix_view dfdy_mat = gsl_matrix_view_array (dfdy, 2, 2);
40-
gsl_matrix* m = &dfdy_mat.matrix;
41-
gsl_matrix_set (m, 0, 0, 0.0);
42-
gsl_matrix_set (m, 0, 1, 1.0);
43-
gsl_matrix_set (m, 1, 0, -2.0*mu*y[0]*y[1] - 1.0);
44-
gsl_matrix_set (m, 1, 1, -mu*(y[0]*y[0] - 1.0));
39+
gsl_matrix* m = &dfdy_mat.matrix;
40+
gsl_matrix_set(m, 0, 0, 0.0);
41+
gsl_matrix_set(m, 0, 1, 1.0);
42+
gsl_matrix_set(m, 1, 0, -2.0*mu*y[0]*y[1] - 1.0);
43+
gsl_matrix_set(m, 1, 1, -mu*(y[0]*y[0] - 1.0));
4544
dfdt[0] = 0.0;
4645
dfdt[1] = 0.0;
4746
return GSL_SUCCESS;
4847
}
4948

50-
int main (void)
51-
{
49+
int main (void) {
5250
double x = 5.0;
53-
double y = gsl_sf_bessel_J0( x );
54-
printf( "J0(%g) = %.18e\n", x, y );
51+
double y = gsl_sf_bessel_J0(x);
52+
printf("J0(%g) = %.18e\n", x, y);
5553

56-
//#eqdifs
54+
/* #eqdifs */
5755
{
58-
//system param
56+
/* system param */
5957
double mu = 1.0;
6058

61-
//complete system description:
59+
/* complete system description: */
6260
gsl_odeiv2_system sys = { ode_func, ode_jac, 2, &mu };
6361

64-
//system + solver description:
62+
/* system + solver description: */
6563
gsl_odeiv2_driver * d = gsl_odeiv2_driver_alloc_y_new(
6664
&sys,
6765
gsl_odeiv2_step_rk8pd, //stepping function (given current state and step size, calculate new state)
@@ -72,80 +70,76 @@ int main (void)
7270

7371
int i;
7472

75-
//initial time:
73+
/* initial time: */
7674
double t = 0.0;
7775

78-
//max desired time:
76+
/* max desired time: */
7977
double t1 = 100.0;
8078

81-
//initial condition
79+
/* initial condition */
8280
double y[2] = { 1.0, 0.0 };
8381

84-
//number of points to print
82+
/* number of points to print */
8583
const int nPts = 1000;
8684

87-
//time interval between each print
85+
/* time interval between each print */
8886
double dt = t1 / nPts;
89-
90-
//prepare to get the output
87+
88+
/* prepare to get the output */
9189
FILE* fp = fopen( "ode.tmp", "w" );
9290
double datax[nPts];
9391
double datay[nPts];
9492

95-
for (i = 0; i < nPts; i++)
96-
{
93+
for (i = 0; i < nPts; i++) {
9794
double ti = i * dt;
9895

99-
//advance time from t to at leaast ti.
100-
//t will contain new last time value.
96+
/*
97+
Advance time from t to at leaast is
98+
will contain new last time value.
99+
*/
101100
int status = gsl_odeiv2_driver_apply (d, &t, ti, y);
102101

103-
if (status != GSL_SUCCESS)
104-
{
102+
if (status != GSL_SUCCESS) {
105103
fprintf( stderr, "error, return value=%d\n", status );
106104
break;
107105
}
108106

109-
//#get output
110-
111-
//to a file:
112-
113-
fprintf( fp, "%.5e %.5e %.5e\n", t, y[0], y[1] );
107+
/* #get output */
114108

115-
//to stdout:
109+
/* to a file: */
110+
fprintf( fp, "%.5e %.5e %.5e\n", t, y[0], y[1] );
116111

117-
printf( "%.5e %.5e %.5e\n", t, y[0], y[1] );
112+
/* to stdout: */
113+
printf( "%.5e %.5e %.5e\n", t, y[0], y[1] );
118114

119-
//to array
120-
121-
datax[i] = y[0];
122-
datay[i] = y[1];
115+
/* to array */
116+
datax[i] = y[0];
117+
datay[i] = y[1];
123118
}
124-
125-
//finalize
126-
fclose(fp);
127-
gsl_odeiv2_driver_free(d);
128-
129-
//plot
130-
131-
//#plplot
132-
133-
plsdev("xwin");
134-
plinit();
135-
plenv(
136-
gsl_stats_min( datax, 1, nPts),
137-
gsl_stats_max( datax, 1, nPts),
138-
gsl_stats_min( datay, 1, nPts),
139-
gsl_stats_max( datay, 1, nPts),
140-
0,
141-
1
142-
);
143-
plstring( nPts, datax, datay, "*" );
144-
plend();
145-
146-
//#gnuplot
147-
//system( "gnuplot -e 'p \"ode.tmp\" u 2:3 w lines; pause -1'" );
119+
120+
/* finalize */
121+
fclose(fp);
122+
gsl_odeiv2_driver_free(d);
123+
124+
/* plot */
125+
126+
/* #plplot */
127+
plsdev("xwin");
128+
plinit();
129+
plenv(
130+
gsl_stats_min( datax, 1, nPts),
131+
gsl_stats_max( datax, 1, nPts),
132+
gsl_stats_min( datay, 1, nPts),
133+
gsl_stats_max( datay, 1, nPts),
134+
0,
135+
1
136+
);
137+
plstring( nPts, datax, datay, "*" );
138+
plend();
139+
140+
/* #gnuplot */
141+
/*system( "gnuplot -e 'p \"ode.tmp\" u 2:3 w lines; pause -1'" );*/
148142
}
149-
150-
return 0;
143+
144+
return EXIT_SUCCESS;
151145
}

‎posix/socket/inet/TODO.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
- UDP. `sendto`, `recvfrom` <https://www.cs.rutgers.edu/~pxk/417/notes/sockets/udp.html>
44
- threaded server. TODO: is that a bad model? https://news.ycombinator.com/item?id=8343350 Apache vs Nginx
55
- build simple sockets ncurses chat socket program. TODO: how to receive while you are writing?
6-
- poll and select
6+
- poll and select. Linux also has epoll.
77
- larger examples
88
- P2P and server versions of:
99
- chat

‎sdl/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../Makefile_many

‎sdl/Makefile_params‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
LIBS := -lSDL2 -lm

‎sdl/README.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SDL
2+
3+
1. [Introduction](introduction.md)
4+
1. [Bibliography](bibliography.md)
5+
1. Examples
6+
1. [SDL_RenderDrawPoint](render_draw_point.c)
7+
1. [Error handling](error_handling.c)
8+
1. [Animation](animation.c)
9+
1. [Animation random walk](animation_random_walk.c)

‎sdl/animation.c‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
TODO find source saying the good way to control speed.
3+
4+
Simple movement that has an absolute equation.
5+
*/
6+
7+
#include <stdlib.h>
8+
9+
#include <SDL2/SDL.h>
10+
11+
#define WINDOW_WIDTH 600
12+
#define RECT_WIDTH WINDOW_WIDTH / 30
13+
#define MAX_RECT_POS (WINDOW_WIDTH - RECT_WIDTH)
14+
#define SCREENS_PER_SECOND 1.0
15+
16+
int main(void) {
17+
SDL_Event event;
18+
SDL_Rect rect;
19+
SDL_Renderer *renderer;
20+
SDL_Window *window;
21+
unsigned int initial_time;
22+
23+
SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO);
24+
SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_WIDTH, 0, &window, &renderer);
25+
rect.y = WINDOW_WIDTH / 2;
26+
rect.w = RECT_WIDTH;
27+
rect.h = RECT_WIDTH;
28+
initial_time = SDL_GetTicks();
29+
while (1) {
30+
rect.x = ((int)(
31+
SCREENS_PER_SECOND
32+
* (MAX_RECT_POS)
33+
* ((SDL_GetTicks() - initial_time)) / 1000.0))
34+
% (MAX_RECT_POS);
35+
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
36+
SDL_RenderClear(renderer);
37+
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
38+
SDL_RenderFillRect(renderer, &rect);
39+
SDL_RenderPresent(renderer);
40+
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
41+
break;
42+
}
43+
SDL_DestroyRenderer(renderer);
44+
SDL_DestroyWindow(window);
45+
SDL_Quit();
46+
return EXIT_SUCCESS;
47+
}

‎sdl/animation_random_walk.c‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Random walk.
3+
*/
4+
5+
#include <math.h>
6+
#include <stdlib.h>
7+
#include <time.h>
8+
9+
#include <SDL2/SDL.h>
10+
11+
#define WINDOW_WIDTH 600
12+
#define RECT_WIDTH (WINDOW_WIDTH / 30)
13+
#define MAX_RECT_POS (WINDOW_WIDTH - RECT_WIDTH)
14+
#define SCREENS_PER_SECOND 10.0
15+
16+
double rand_one() {
17+
return -1.0 + 2.0 * (rand() / (double)RAND_MAX);
18+
}
19+
20+
double get_coord(double *x, double magnitude) {
21+
*x += magnitude * rand_one();
22+
*x = fmod(*x, MAX_RECT_POS);
23+
if (*x < 0)
24+
*x = MAX_RECT_POS;
25+
return *x;
26+
}
27+
28+
int main(void) {
29+
SDL_Event event;
30+
SDL_Rect rect;
31+
SDL_Renderer *renderer;
32+
SDL_Window *window;
33+
unsigned int current_time;
34+
double magnitude;
35+
unsigned int last_time;
36+
double x;
37+
double y;
38+
39+
srand(time(NULL));
40+
SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO);
41+
SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_WIDTH, 0, &window, &renderer);
42+
rect.x = WINDOW_WIDTH / 2;
43+
rect.y = WINDOW_WIDTH / 2;
44+
rect.w = RECT_WIDTH;
45+
rect.h = RECT_WIDTH;
46+
last_time = SDL_GetTicks();
47+
while (1) {
48+
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
49+
break;
50+
current_time = SDL_GetTicks();
51+
if (current_time != last_time) {
52+
magnitude = SCREENS_PER_SECOND * MAX_RECT_POS * (current_time - last_time) / 1000.0;
53+
rect.x = get_coord(&x, magnitude);
54+
rect.y = get_coord(&y, magnitude);
55+
last_time = current_time;
56+
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
57+
SDL_RenderClear(renderer);
58+
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
59+
SDL_RenderFillRect(renderer, &rect);
60+
SDL_RenderPresent(renderer);
61+
}
62+
}
63+
SDL_DestroyRenderer(renderer);
64+
SDL_DestroyWindow(window);
65+
SDL_Quit();
66+
return EXIT_SUCCESS;
67+
}

0 commit comments

Comments
 (0)