mbrot-mt

diff src/mbrot-mt.c @ 1:3a893f9831ac

ported to SDL2
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 14 Mar 2014 03:39:37 +0200
parents e9ae6289e14f
children
line diff
     1.1 --- a/src/mbrot-mt.c	Fri Mar 07 07:42:48 2014 +0200
     1.2 +++ b/src/mbrot-mt.c	Fri Mar 14 03:39:37 2014 +0200
     1.3 @@ -1,17 +1,43 @@
     1.4  #include <stdio.h>
     1.5  #include <complex.h>
     1.6 -#include <SDL/SDL.h>
     1.7 +#include <SDL2/SDL.h>
     1.8  
     1.9  void display(void);
    1.10  int mandelbrot(float x, float y, int max_iter, int *iter);
    1.11  int handle_event(SDL_Event *ev);
    1.12  
    1.13 -static SDL_Surface *fbsurf;
    1.14 +static SDL_Window *win;
    1.15 +static SDL_Surface *winsurf;
    1.16  static int win_width, win_height;
    1.17  static float win_aspect;
    1.18  static float xoffs, yoffs, zoom = 1.0;
    1.19  static int max_iter = 64;
    1.20  
    1.21 +static void trysdl2stuff(void)
    1.22 +{
    1.23 +	int i, j, num_modes, num_scr;
    1.24 +
    1.25 +	num_scr = SDL_GetNumVideoDisplays();
    1.26 +
    1.27 +	for(i=0; i<num_scr; i++) {
    1.28 +		printf("Screen %d: %s\n", i, SDL_GetDisplayName(i));
    1.29 +
    1.30 +		num_modes = SDL_GetNumDisplayModes(i);
    1.31 +		for(j=0; j<num_modes; j++) {
    1.32 +			SDL_DisplayMode mode;
    1.33 +			int bpp;
    1.34 +			unsigned int rmask, gmask, bmask, amask;
    1.35 +
    1.36 +			SDL_GetDisplayMode(i, j, &mode);
    1.37 +			SDL_PixelFormatEnumToMasks(mode.format, &bpp, &rmask, &gmask, &bmask, &amask);
    1.38 +
    1.39 +			printf("  %dx%d %dbpp %dhz\n", mode.w, mode.h, bpp, mode.refresh_rate);
    1.40 +		}
    1.41 +	}
    1.42 +
    1.43 +	SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "Information", "Mandelbrot rules!", 0);
    1.44 +}
    1.45 +
    1.46  int main(void)
    1.47  {
    1.48  	win_width = 800;
    1.49 @@ -19,12 +45,13 @@
    1.50  	win_aspect = (float)win_width / (float)win_height;
    1.51  
    1.52  	SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
    1.53 -	if(!(fbsurf = SDL_SetVideoMode(win_width, win_height, 32, SDL_SWSURFACE | SDL_RESIZABLE))) {
    1.54 +	trysdl2stuff();
    1.55 +
    1.56 +	if(!(win = SDL_CreateWindow("Mandelbrot", 0, 0, win_width, win_height, SDL_WINDOW_RESIZABLE))) {
    1.57  		fprintf(stderr, "failed to create framebuffer\n");
    1.58  		return 1;
    1.59  	}
    1.60 -
    1.61 -	SDL_WM_SetCaption("Mandelbrot", 0);
    1.62 +	winsurf = SDL_GetWindowSurface(win);
    1.63  
    1.64  	for(;;) {
    1.65  		SDL_Event ev;
    1.66 @@ -48,12 +75,12 @@
    1.67  	unsigned char *fbuf, *pixptr;
    1.68  	int i, j, xsz, ysz;
    1.69  
    1.70 -	if(SDL_MUSTLOCK(fbsurf)) {
    1.71 -		SDL_LockSurface(fbsurf);
    1.72 +	if(SDL_MUSTLOCK(winsurf)) {
    1.73 +		SDL_LockSurface(winsurf);
    1.74  	}
    1.75 -	fbuf = fbsurf->pixels;
    1.76 -	xsz = fbsurf->w;
    1.77 -	ysz = fbsurf->h;
    1.78 +	fbuf = winsurf->pixels;
    1.79 +	xsz = winsurf->w;
    1.80 +	ysz = winsurf->h;
    1.81  
    1.82  	pixptr = fbuf;
    1.83  	for(i=0; i<ysz; i++) {
    1.84 @@ -79,10 +106,10 @@
    1.85  		}
    1.86  	}
    1.87  
    1.88 -	if(SDL_MUSTLOCK(fbsurf)) {
    1.89 -		SDL_UnlockSurface(fbsurf);
    1.90 +	if(SDL_MUSTLOCK(winsurf)) {
    1.91 +		SDL_UnlockSurface(winsurf);
    1.92  	}
    1.93 -	SDL_Flip(fbsurf);
    1.94 +	SDL_UpdateWindowSurface(win);
    1.95  }
    1.96  
    1.97  int mandelbrot(float x, float y, int max_iter, int *iter)
    1.98 @@ -146,10 +173,12 @@
    1.99  		}
   1.100  		break;
   1.101  
   1.102 -	case SDL_VIDEORESIZE:
   1.103 -		win_width = ev->resize.w;
   1.104 -		win_height = ev->resize.h;
   1.105 -		win_aspect = (float)win_width / (float)win_height;
   1.106 +	case SDL_WINDOWEVENT:
   1.107 +		if(ev->window.event == SDL_WINDOWEVENT_RESIZED) {
   1.108 +			win_width = ev->window.data1;
   1.109 +			win_height = ev->window.data2;
   1.110 +			win_aspect = (float)win_width / (float)win_height;
   1.111 +		}
   1.112  		break;
   1.113  
   1.114  	default: