From 7c71af8aa15a2c94216f393055065f834286ddcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Cuevas?= Date: Fri, 6 Sep 2013 19:33:06 +0200 Subject: [PATCH] Modified to compile against SDL 2.0 --- Makefile | 6 ++-- interactive.c | 88 ++++++++++++++++++++++++++++++++---------------- main.c | 29 +++++++++++++--- mandel_classic.c | 36 ++++++++++++++++---- 4 files changed, 115 insertions(+), 44 deletions(-) diff --git a/Makefile b/Makefile index 794d645..57f4d68 100644 --- a/Makefile +++ b/Makefile @@ -2,21 +2,21 @@ CC=gcc # Flags! -SDLFLAGS=$(shell sdl-config --cflags) +SDLFLAGS=$(shell sdl2-config --cflags) # Comment this line and uncomment the next to get debug symbols CFLAGS=-c -Wall -O2 $(SDLFLAGS) # CFLAGS=-c -Wall -ggdb $(SDLFLAGS) # Libs! -SDLLIBS=$(shell sdl-config --libs) -lSDL_ttf +SDLLIBS=$(shell sdl2-config --libs) -lSDL2_ttf OPENCLLIBS=-lOpenCL LIBS=-lm -lpthread $(SDLLIBS) # Includes! -INCLUDE=-I/usr/include/SDL -I./ +INCLUDE=-I/usr/include/SDL2 -I./ all: mandelclassic clfract test clfractinteractive diff --git a/interactive.c b/interactive.c index c85977c..4e17847 100644 --- a/interactive.c +++ b/interactive.c @@ -15,19 +15,21 @@ #define MAX_SOURCE_SIZE (0x100000) -float map_x_mandelbrot(int x, int width, float zoom) +float map_x_mandelbrot(float x, int width, float zoom) { - return (((float)x / (float)width) * (3.5 * zoom)) - 2.5; + // return (((float)x / (float)width) * (3.5 * zoom)) - 2.5; + return ((x / (float)width) * (3.5 * zoom)); } -float map_x_julia(int x, int width, float zoom) +float map_x_julia(float x, int width, float zoom) { - return (((float)x / (float)width) * (3.5 * zoom)) - 1.75; + return ((x / (float)width) * (3.5 * zoom)) - 1.75; } -float map_y(int y, int height, float zoom) +float map_y(float y, int height, float zoom) { - return (((float)y / (float)height) * (2.0 * zoom)) - 1.0; + // return (((float)y / (float)height) * (2.0 * zoom)) - 1.0; + return ((y / (float)height) * (2.0 * zoom)); } int main(int argn, char **argv) { @@ -55,12 +57,27 @@ int main(int argn, char **argv) { printf("Julia mode activated...\n"); } - screen = SDL_SetVideoMode(res_x, res_y, 0, SDL_DOUBLEBUF); - if(!screen) + SDL_Window *window = SDL_CreateWindow("MandelClassic", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + res_x, res_y, 0); + + SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0); + + if ((!window) || (!renderer)) fprintf(stderr,"Could not set video mode: %s\n",SDL_GetError()); - // Set the title bar - SDL_WM_SetCaption("CLFract", "CLFract"); + // Blank the window + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_RenderClear(renderer); + SDL_RenderPresent(renderer); + + SDL_Texture *texture_screen = SDL_CreateTexture(renderer, + SDL_PIXELFORMAT_ARGB8888, + SDL_TEXTUREACCESS_STREAMING, + res_x, res_y); + + screen = SDL_CreateRGBSurface(0, res_x, res_y , 32, 0, 0, 0, 0); //Initialize SDL_ttf if( TTF_Init() == -1 ) @@ -172,6 +189,8 @@ int main(int argn, char **argv) { float stop_point; float center_x = 2.5; float center_y = 1.75; + float mouse_x = res_x / 2; + float mouse_y = res_y / 2; if (julia_mode == 0) stop_point = 0.00001; @@ -249,26 +268,26 @@ int main(int argn, char **argv) { iteration = graph_line[line_count]; if ((iteration < 128) && (iteration > 0)) { pixel[(current_line * res_x) + line_count] = SDL_MapRGBA(screen->format, - 0, - 20 + iteration, - 0, - 255); + 0, + 20 + iteration, + 0, + 255); } else if ((iteration >= 128) && (iteration < ITERATIONS)) { pixel[(current_line * res_x) + line_count] = SDL_MapRGBA(screen->format, - iteration, - 148, - iteration, - 255); + iteration, + 148, + iteration, + 255); } else { pixel[(current_line * res_x) + line_count] = SDL_MapRGBA(screen->format, - 0, - 0, - 0, - 255); + 0, + 0, + 0, + 255); } } } @@ -291,13 +310,10 @@ int main(int argn, char **argv) { { motion = -1; } + + mouse_x = ( (float) button.x - ((float) res_x / 2.0) ) / 10.0; + mouse_y = ( (float) button.y - ((float) res_y / 2.0) ) / 10.0; - if (julia_mode == 0) - center_x = map_x_mandelbrot(button.x, res_x, zoom); - else - center_x = map_x_julia(button.x, res_x, zoom); - - center_y = map_y(button.y, res_y, zoom); } else if ( (ev.type == SDL_MOUSEBUTTONUP) ) { @@ -322,6 +338,16 @@ int main(int argn, char **argv) { zoom += 0.01; } + if (motion != 0) + { + if (julia_mode == 0) + center_x = map_x_mandelbrot(center_x + mouse_x, res_x, zoom); + else + center_x = map_x_julia(center_x + mouse_x, res_x, zoom); + + center_y = map_y(center_y + mouse_y, res_y, zoom); + } + // Draw message on a corner... char* msg = (char *)malloc(100 * sizeof(char)); sprintf(msg, "Zoom level: %0.3f", zoom * 100.0); @@ -332,8 +358,12 @@ int main(int argn, char **argv) { free(message); + SDL_UpdateTexture(texture_screen, NULL, (Uint32*)screen->pixels, 800 * sizeof (Uint32)); + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, texture_screen, NULL, NULL); + SDL_RenderPresent(renderer); // Draw to the screen - SDL_Flip(screen); + // SDL_Flip(screen); } // Clean up diff --git a/main.c b/main.c index 889badc..4521820 100644 --- a/main.c +++ b/main.c @@ -40,12 +40,27 @@ int main(int argn, char **argv) { printf("Julia mode activated...\n"); } - screen = SDL_SetVideoMode(res_x, res_y, 0, SDL_DOUBLEBUF); - if(!screen) + SDL_Window *window = SDL_CreateWindow("CLFract", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + res_x, res_y, 0); + + SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0); + + if ((!window) || (!renderer)) fprintf(stderr,"Could not set video mode: %s\n",SDL_GetError()); - // Set the title bar - SDL_WM_SetCaption("CLFract", "CLFract"); + // Blank the window + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_RenderClear(renderer); + SDL_RenderPresent(renderer); + + SDL_Texture *texture_screen = SDL_CreateTexture(renderer, + SDL_PIXELFORMAT_ARGB8888, + SDL_TEXTUREACCESS_STREAMING, + res_x, res_y); + + screen = SDL_CreateRGBSurface(0, res_x, res_y , 32, 0, 0, 0, 0); //Initialize SDL_ttf if( TTF_Init() == -1 ) @@ -255,8 +270,12 @@ int main(int argn, char **argv) { free(message); + SDL_UpdateTexture(texture_screen, NULL, (Uint32*)screen->pixels, 800 * sizeof (Uint32)); + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, texture_screen, NULL, NULL); + SDL_RenderPresent(renderer); // Draw to the screen - SDL_Flip(screen); + // SDL_Flip(screen); } printf("Time elapsed %0.5f seconds\n", ((double)clock() - start) / CLOCKS_PER_SEC); diff --git a/mandel_classic.c b/mandel_classic.c index 018eed4..68eeb9f 100644 --- a/mandel_classic.c +++ b/mandel_classic.c @@ -314,11 +314,11 @@ int main(int argn, char **argv) { // Init SDL if(SDL_Init(SDL_INIT_VIDEO) != 0) - fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError()); + fprintf(stderr, "Could not initialize SDL2: %s\n", SDL_GetError()); printf("SDL Initialized\n"); - // Create screen surface + // Create screen texture SDL_Surface *screen, *message; int res_x = 800; int res_y = 600; @@ -371,11 +371,28 @@ int main(int argn, char **argv) #endif // screen = SDL_SetVideoMode(res_x, res_y, 0, SDL_HWSURFACE|SDL_DOUBLEBUF); - screen = SDL_SetVideoMode(res_x, res_y, 0, SDL_DOUBLEBUF); - if(!screen) + // screen = SDL_SetVideoMode(res_x, res_y, 0, SDL_DOUBLEBUF); + SDL_Window *window = SDL_CreateWindow("MandelClassic", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + res_x, res_y, 0); + + SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0); + + if ((!window) || (!renderer)) fprintf(stderr,"Could not set video mode: %s\n",SDL_GetError()); - // Set the title bar - SDL_WM_SetCaption("CLFract", "CLFract"); + + // Blank the window + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_RenderClear(renderer); + SDL_RenderPresent(renderer); + + SDL_Texture *texture_screen = SDL_CreateTexture(renderer, + SDL_PIXELFORMAT_ARGB8888, + SDL_TEXTUREACCESS_STREAMING, + res_x, res_y); + + screen = SDL_CreateRGBSurface(0, res_x, res_y , 32, 0, 0, 0, 0); //Initialize SDL_ttf if( TTF_Init() == -1 ) @@ -501,7 +518,12 @@ int main(int argn, char **argv) free(message); - SDL_Flip(screen); + SDL_UpdateTexture(texture_screen, NULL, (Uint32*)screen->pixels, 800 * sizeof (Uint32)); + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, texture_screen, NULL, NULL); + SDL_RenderPresent(renderer); + + // SDL_Flip(screen); } printf("Time elapsed %0.5f seconds\n", ((double)clock() - start) / CLOCKS_PER_SEC);