# HG changeset patch # User John Tsiombikas # Date 1394170968 -7200 # Node ID e9ae6289e14fb1b061398d327f6d3636a7e4f99e initial commit diff -r 000000000000 -r e9ae6289e14f Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Fri Mar 07 07:42:48 2014 +0200 @@ -0,0 +1,13 @@ +src = $(wildcard src/*.c) +obj = $(src:.c=.o) +bin = mbrot + +CFLAGS = -pedantic -Wall -g `pkg-config --cflags sdl` +LDFLAGS = `pkg-config --libs sdl` + +$(bin): $(obj) + $(CC) -o $@ $(obj) $(LDFLAGS) + +.PHONY: clean +clean: + rm -f $(obj) $(bin) diff -r 000000000000 -r e9ae6289e14f src/mbrot-mt.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/mbrot-mt.c Fri Mar 07 07:42:48 2014 +0200 @@ -0,0 +1,159 @@ +#include +#include +#include + +void display(void); +int mandelbrot(float x, float y, int max_iter, int *iter); +int handle_event(SDL_Event *ev); + +static SDL_Surface *fbsurf; +static int win_width, win_height; +static float win_aspect; +static float xoffs, yoffs, zoom = 1.0; +static int max_iter = 64; + +int main(void) +{ + win_width = 800; + win_height = 600; + win_aspect = (float)win_width / (float)win_height; + + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); + if(!(fbsurf = SDL_SetVideoMode(win_width, win_height, 32, SDL_SWSURFACE | SDL_RESIZABLE))) { + fprintf(stderr, "failed to create framebuffer\n"); + return 1; + } + + SDL_WM_SetCaption("Mandelbrot", 0); + + for(;;) { + SDL_Event ev; + + while(SDL_PollEvent(&ev)) { + if(handle_event(&ev) == -1) { + goto done; + } + } + + display(); + } + +done: + SDL_Quit(); + return 0; +} + +void display(void) +{ + unsigned char *fbuf, *pixptr; + int i, j, xsz, ysz; + + if(SDL_MUSTLOCK(fbsurf)) { + SDL_LockSurface(fbsurf); + } + fbuf = fbsurf->pixels; + xsz = fbsurf->w; + ysz = fbsurf->h; + + pixptr = fbuf; + for(i=0; i 4) { + if(iter) *iter = i; + return 0; + } + + z = z * z + c; + } + + return 1; +} + +int handle_event(SDL_Event *ev) +{ + static int prev_x, prev_y; + static int bnstate[32]; + + switch(ev->type) { + case SDL_KEYDOWN: + if(ev->key.keysym.sym == SDLK_ESCAPE) { + return -1; + } + + case SDL_MOUSEBUTTONDOWN: + prev_x = ev->button.x; + prev_y = ev->button.y; + bnstate[ev->button.button - SDL_BUTTON_LEFT] = 1; + break; + + case SDL_MOUSEBUTTONUP: + bnstate[ev->button.button - SDL_BUTTON_LEFT] = 0; + break; + + case SDL_MOUSEMOTION: + { + int dx = ev->button.x - prev_x; + int dy = ev->button.y - prev_y; + prev_x = ev->button.x; + prev_y = ev->button.y; + + if(!dx && !dy) break; + + if(bnstate[0]) { + xoffs += win_aspect * 2.0 * (float)dx / (float)win_width; + yoffs += 2.0 * (float)dy / (float)win_height; + } + if(bnstate[2]) { + zoom += (float)dy / (float)win_height; + if(zoom < 1e-6) zoom = 1e-6; + } + } + break; + + case SDL_VIDEORESIZE: + win_width = ev->resize.w; + win_height = ev->resize.h; + win_aspect = (float)win_width / (float)win_height; + break; + + default: + break; + } + return 0; +}