From 78275917cb7636f5760c028f1c0dfc3397e2b6a3 Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Sat, 30 Apr 2011 22:38:09 +0300 Subject: [PATCH] Initial commit Signed-off-by: Timo Kokkonen --- .gitignore | 5 ++++ Makefile | 61 ++++++++++++++++++++++++++++++++++++++ mandelbrot.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 mandelbrot.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5262bf0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.*.o.d +mandelbrot +debug-mandelbrot +*.o +*~ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..cb2543b --- /dev/null +++ b/Makefile @@ -0,0 +1,61 @@ +SDL_CONFIG = $(shell sdl-config --cflags) +SDL_LIBS = $(shell sdl-config --libs) +CFLAGS = $(SDL_CONFIG) -Wall -O2 -g + +LIBS = $(SDL_LIBS) -lm -lrt + +CC = gcc + +MANDELBROT_OBJS = mandelbrot.o +MANDELBROT_DEBUG_OBJS = $(patsubst %.o,%-debug.o,$(MANDELBROT_OBJS)) +ALL_OBJS = $(MANDELBROT_OBJS) $(MANDELBROT_DEBUG_OBJS) +ALL_DEBS = $(patsubst %.o,.%.o.d,$(ALL_OBJS)) + +ifeq ($(V),1) + Q = + QUIET_CC = + QUIET_LINK = +else + Q = @ + QUIET_CC = @echo " CC " $@; + QUIET_LINK = @echo " LINK " $@; +endif + +mandelbrot: $(MANDELBROT_OBJS) + $(QUIET_LINK)$(CC) $(LIBS) -o $@ $(MANDELBROT_OBJS) + +debug-mandelbrot: $(MANDELBROT_DEBUG_OBJS) + $(QUIET_LINK)$(CC) $(LIBS) -o $@ $(MANDELBROT_DEBUG_OBJS) -DDEBUG + +.c.o: + $(QUIET_CC)$(CC) -MMD -MF .$@.d $(CFLAGS) -c $< -o $@ + $(Q)cp .$@.d .$@.P; \ + sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \ + -e '/^$$/ d' -e 's/$$/ :/' < .$@.d >> .$@.P; \ + mv .$@.P .$@.d + +ifeq ($(C),1) + sparse $(CFLAGS) $< +endif +ifeq ($(C),2) + $(CHECKPATCH) -f $< +endif + +%-debug.o: %.c + $(QUIET_CC)$(CC) -MMD -MF .$@.d $(CFLAGS) -DDEBUG -O0 -c $< -o $@ + $(Q)cp .$@.d .$@.P; \ + sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \ + -e '/^$$/ d' -e 's/$$/ :/' < .$@.d >> .$@.P; \ + mv .$@.P .$@.d + +TAGS: + @echo " TAGS" + $(Q)etags *.[ch] + +clean: + rm -rfv $(ALL_OBJS) *~ mandelbrot TAGS $(ALL_DEBS) \ + debug-mandelbrot + +.PHONY: all clean TAGS + +-include $(ALL_DEBS) diff --git a/mandelbrot.c b/mandelbrot.c new file mode 100644 index 0000000..ef17b9e --- /dev/null +++ b/mandelbrot.c @@ -0,0 +1,83 @@ +#include +#include +#include +#include + +static void putpixel(struct SDL_Surface *screen, const int x, const int y, + const unsigned char r, const unsigned char g, + const unsigned char b) +{ + int offset = y * screen->pitch + x * 4; + unsigned char *buf = screen->pixels; + + buf[offset++] = b; + buf[offset++] = g; + buf[offset] = r; +} + +#define MAX_ITERATION 1000 + +int draw_mandelbrot(struct SDL_Surface *screen) +{ + double x, y, x0, y0, xtemp; + int iteration, xs,ys; + + for (ys = 0, y0 = -1; ys < screen->h; + y0 += 2 / (double)screen->h, ys++) { + for (xs = 0, x0 = -2.5; xs < screen->w; + x0 += 3 / (double)screen->w, xs++) { + iteration = 0; + x = 0; + y = 0; + + while (x * x + y * y < 2 * 2 && + iteration < MAX_ITERATION) { + + xtemp = x*x - y*y + x0; + y = 2*x*y + y0; + + x = xtemp; + iteration++; + } + + if (iteration == MAX_ITERATION) + putpixel(screen, xs, ys, 255, 255, 255); + else + putpixel(screen, xs, ys, + iteration, 0, 0); + } + SDL_Flip(screen); + } + + return 0; +} + +int main(int argc, char *argv[]) +{ + SDL_Surface *screen; + int flags = SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_RESIZABLE; + int xres = 800, yres = 600; + + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + fprintf(stderr, "Unable to initialize SDL: %s\n", + SDL_GetError()); + + return 1; + } + atexit(SDL_Quit); + + screen = SDL_SetVideoMode(xres, yres, 32, flags); + if (screen == NULL) { + fprintf(stderr, "Unable to set video mode: %s\n", + SDL_GetError()); + return 2; + } + + SDL_WM_SetCaption(argv[0], NULL); + + draw_mandelbrot(screen); + + yres = read(0, &xres, 1); + + return 0; +} -- 2.45.0