Przykład Przykład wykorzystujący SDL: #include

#define DIM 400.0

int main() { SDL_Surface *screen = SDL_SetVideoMode(DIM, DIM, 0, 0); SDL_Surface *surface = SDL_CreateRGBSurface(SDL_SWSURFACE, DIM, DIM, 24, 0xFF, 0xFF00, 0xFF0000, 0);

double fact = 2; double cx = -0.74364500005891; double cy = 0.13182700000109;

while (fact > 1e-18) { double xa = cx - fact; double ya = cy - fact; int y;

for (y = 0; y pixels + y*surface->pitch; double y0 = ya + y/DIM*2*fact; int x; for (x = 0; x < DIM; x++) { double x0 = xa + x/DIM*2*fact; double xn = 0, yn = 0, tmpxn; int i; for (i = 0; i 4) break; // approximate infinity

if (i == 512) { // in Mandelbrot set pixline[x*3] = pixline[x*3+1] = pixline[x*3+2] = 0; } else { // not in Mandelbrot set; use escape iteration value to set color (grades of blue then white) pixline[x*3] = pixline[x*3+1] = i < 256 ? 0 : i - 256; pixline[x*3+2] = i < 256 ? i : 255;

SDL_BlitSurface(surface, NULL, screen, NULL); SDL_Flip(screen); fact /= 2;

SDL_Quit(); return(0);

Plik zapisujemy jako: mandelbrot.c

Kompilujemy za pomocą komendy: gcc `sdl-config --cflags --libs` -O3 mandelbrot.c && ./a.out

pkg-config --cflags --libs sdl2

Przykładowy wynik:

-D_REENTRANT -I/usr/include/SDL2 -lSDL2

Kompilacja:

gcc s.c -Wextra -Wall -lSDL2 `pkg-config --cflags --libs sdl2`

Instalacja Jeśli nie mamy SDL to instalujemy ją, np.:askubuntu question: fatal-error-sdl-sdl-h-no-such-file-or-directory

sudo apt-get install libsdl1.2-dev libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev

ew:

sudo apt-get install libsdl2-gfx-dev

Żródła

Writing 2D Games in C using SDL by Thomas Lively libsdl wiki moderncprogramming: can-you-use-sdl-simple-directmedia-layer-in-pure-c