mbrot-mt
changeset 1:3a893f9831ac tip
ported to SDL2
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 14 Mar 2014 03:39:37 +0200 |
parents | e9ae6289e14f |
children | |
files | Makefile src/mbrot-mt.c |
diffstat | 2 files changed, 48 insertions(+), 19 deletions(-) [+] |
line diff
1.1 --- a/Makefile Fri Mar 07 07:42:48 2014 +0200 1.2 +++ b/Makefile Fri Mar 14 03:39:37 2014 +0200 1.3 @@ -2,8 +2,8 @@ 1.4 obj = $(src:.c=.o) 1.5 bin = mbrot 1.6 1.7 -CFLAGS = -pedantic -Wall -g `pkg-config --cflags sdl` 1.8 -LDFLAGS = `pkg-config --libs sdl` 1.9 +CFLAGS = -pedantic -Wall -g `pkg-config --cflags sdl2` 1.10 +LDFLAGS = `pkg-config --libs sdl2` 1.11 1.12 $(bin): $(obj) 1.13 $(CC) -o $@ $(obj) $(LDFLAGS)
2.1 --- a/src/mbrot-mt.c Fri Mar 07 07:42:48 2014 +0200 2.2 +++ b/src/mbrot-mt.c Fri Mar 14 03:39:37 2014 +0200 2.3 @@ -1,17 +1,43 @@ 2.4 #include <stdio.h> 2.5 #include <complex.h> 2.6 -#include <SDL/SDL.h> 2.7 +#include <SDL2/SDL.h> 2.8 2.9 void display(void); 2.10 int mandelbrot(float x, float y, int max_iter, int *iter); 2.11 int handle_event(SDL_Event *ev); 2.12 2.13 -static SDL_Surface *fbsurf; 2.14 +static SDL_Window *win; 2.15 +static SDL_Surface *winsurf; 2.16 static int win_width, win_height; 2.17 static float win_aspect; 2.18 static float xoffs, yoffs, zoom = 1.0; 2.19 static int max_iter = 64; 2.20 2.21 +static void trysdl2stuff(void) 2.22 +{ 2.23 + int i, j, num_modes, num_scr; 2.24 + 2.25 + num_scr = SDL_GetNumVideoDisplays(); 2.26 + 2.27 + for(i=0; i<num_scr; i++) { 2.28 + printf("Screen %d: %s\n", i, SDL_GetDisplayName(i)); 2.29 + 2.30 + num_modes = SDL_GetNumDisplayModes(i); 2.31 + for(j=0; j<num_modes; j++) { 2.32 + SDL_DisplayMode mode; 2.33 + int bpp; 2.34 + unsigned int rmask, gmask, bmask, amask; 2.35 + 2.36 + SDL_GetDisplayMode(i, j, &mode); 2.37 + SDL_PixelFormatEnumToMasks(mode.format, &bpp, &rmask, &gmask, &bmask, &amask); 2.38 + 2.39 + printf(" %dx%d %dbpp %dhz\n", mode.w, mode.h, bpp, mode.refresh_rate); 2.40 + } 2.41 + } 2.42 + 2.43 + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "Information", "Mandelbrot rules!", 0); 2.44 +} 2.45 + 2.46 int main(void) 2.47 { 2.48 win_width = 800; 2.49 @@ -19,12 +45,13 @@ 2.50 win_aspect = (float)win_width / (float)win_height; 2.51 2.52 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); 2.53 - if(!(fbsurf = SDL_SetVideoMode(win_width, win_height, 32, SDL_SWSURFACE | SDL_RESIZABLE))) { 2.54 + trysdl2stuff(); 2.55 + 2.56 + if(!(win = SDL_CreateWindow("Mandelbrot", 0, 0, win_width, win_height, SDL_WINDOW_RESIZABLE))) { 2.57 fprintf(stderr, "failed to create framebuffer\n"); 2.58 return 1; 2.59 } 2.60 - 2.61 - SDL_WM_SetCaption("Mandelbrot", 0); 2.62 + winsurf = SDL_GetWindowSurface(win); 2.63 2.64 for(;;) { 2.65 SDL_Event ev; 2.66 @@ -48,12 +75,12 @@ 2.67 unsigned char *fbuf, *pixptr; 2.68 int i, j, xsz, ysz; 2.69 2.70 - if(SDL_MUSTLOCK(fbsurf)) { 2.71 - SDL_LockSurface(fbsurf); 2.72 + if(SDL_MUSTLOCK(winsurf)) { 2.73 + SDL_LockSurface(winsurf); 2.74 } 2.75 - fbuf = fbsurf->pixels; 2.76 - xsz = fbsurf->w; 2.77 - ysz = fbsurf->h; 2.78 + fbuf = winsurf->pixels; 2.79 + xsz = winsurf->w; 2.80 + ysz = winsurf->h; 2.81 2.82 pixptr = fbuf; 2.83 for(i=0; i<ysz; i++) { 2.84 @@ -79,10 +106,10 @@ 2.85 } 2.86 } 2.87 2.88 - if(SDL_MUSTLOCK(fbsurf)) { 2.89 - SDL_UnlockSurface(fbsurf); 2.90 + if(SDL_MUSTLOCK(winsurf)) { 2.91 + SDL_UnlockSurface(winsurf); 2.92 } 2.93 - SDL_Flip(fbsurf); 2.94 + SDL_UpdateWindowSurface(win); 2.95 } 2.96 2.97 int mandelbrot(float x, float y, int max_iter, int *iter) 2.98 @@ -146,10 +173,12 @@ 2.99 } 2.100 break; 2.101 2.102 - case SDL_VIDEORESIZE: 2.103 - win_width = ev->resize.w; 2.104 - win_height = ev->resize.h; 2.105 - win_aspect = (float)win_width / (float)win_height; 2.106 + case SDL_WINDOWEVENT: 2.107 + if(ev->window.event == SDL_WINDOWEVENT_RESIZED) { 2.108 + win_width = ev->window.data1; 2.109 + win_height = ev->window.data2; 2.110 + win_aspect = (float)win_width / (float)win_height; 2.111 + } 2.112 break; 2.113 2.114 default: