From 23934078085348710a011190f4595f8c17ba4868 Mon Sep 17 00:00:00 2001 From: Pablo Musa Date: Tue, 3 Jun 2014 20:44:23 -0300 Subject: [PATCH] Now using SDL2 make updated and cleaned --- Makefile | 29 +++++++------- gsdl.c | 117 +++++++++++++++++++++++-------------------------------- 2 files changed, 64 insertions(+), 82 deletions(-) diff --git a/Makefile b/Makefile index 24737e4..8c7d6e9 100644 --- a/Makefile +++ b/Makefile @@ -1,41 +1,42 @@ # # Author: Pablo Musa # Creation Date: mar 27 2011 -# Last Modification: aug 22 2011 +# Last Modification: jun 03 2014 # See Copyright Notice in COPYRIGHT # # luamemprofiler - A Memory Profiler for the Lua language # -SDL_CFLAGS = -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT -SDL_LDFLAGS = -L/usr/lib -lSDL -lSDL_ttf -LUA_DIR = /home/pablomusa/aulas/2011.1/PFP/lua5.2/include - CC = gcc +CFLAGS = -g -Wall -ansi -pedantic -fPIC -shared -# compilation should generate Dynamic-Link Libraries -CFLAGS = -g -Wall -O0 -ansi -pedantic -LFLAGS = -I$(LUA_DIR) -fpic -shared +# used the 'sdl2-config' output + the '-lSDL2_ttf' +SDL_LIBS = -L/home/pmusa/Programs/SDL/lib -Wl,-rpath,/home/pmusa/Programs/SDL/lib -lSDL2 -lSDL2_ttf -lpthread +SDL_CFLAGS = -I/home/pmusa/Programs/SDL/include/SDL2 -D_REENTRANT + +LUA_DIR = /usr/include/lua5.2 +LUA_CFLAGS = -I$(LUA_DIR) +LUA_LIBS = -llua5.2 all: luamemprofiler.so luamemprofiler.so: graphic.o lmp_struct.o vmemory.o lmp.o luamemprofiler.o - $(CC) $(CFLAGS) $(LFLAGS) $(SDL_CFLAGS) $(SDL_LDFLAGS) graphic.o lmp_struct.o vmemory.o lmp.o luamemprofiler.o -o luamemprofiler.so + $(CC) graphic.o lmp_struct.o vmemory.o lmp.o luamemprofiler.o -o luamemprofiler.so $(CFLAGS) $(SDL_LIBS) $(LUA_LIBS) luamemprofiler.o: luamemprofiler.c lmp.h - $(CC) $(CFLAGS) $(LFLAGS) -c luamemprofiler.c + $(CC) -c luamemprofiler.c $(CFLAGS) $(LUA_CFLAGS) lmp.o: lmp.c lmp.h lmp_struct.h vmemory.h - $(CC) $(CFLAGS) -c lmp.c + $(CC) -c lmp.c $(CFLAGS) $(LUA_CFLAGS) lmp_struct.o: lmp_struct.c lmp_struct.h - $(CC) $(CFLAGS) -c lmp_struct.c + $(CC) -c lmp_struct.c $(CFLAGS) $(LUA_CFLAGS) vmemory.o: vmemory.c lmp.h vmemory.h graphic.h - $(CC) $(CFLAGS) -c vmemory.c + $(CC) -c vmemory.c $(CFLAGS) $(LUA_CFLAGS) graphic.o: gsdl.c graphic.h - $(CC) $(CFLAGS) $(SDL_CFLAGS) $(SDL_LDFLAGS) -c gsdl.c -o graphic.o + $(CC) -c gsdl.c -o graphic.o $(CFLAGS) $(SDL_CFLAGS) clean: rm *.o diff --git a/gsdl.c b/gsdl.c index d2e4f17..37206bf 100644 --- a/gsdl.c +++ b/gsdl.c @@ -12,12 +12,12 @@ #include #include +#include #include "graphic.h" /* GLOBAL VARIABLES */ static TTF_Font *font; static SDL_Colour fontcolor = {0,0,0}; -static Uint32 color; static int gr_textwidth; static int gr_textheight; @@ -26,22 +26,27 @@ static TTF_Font* loadfont(char* file, int ptsize); /* GLOBAL FUNCTIONS */ Screen *gr_newscreen(int width,int height,const char *icon,const char *title) { - int depth = 32; - SDL_Surface *screen; - Uint32 vmode = SDL_HWSURFACE | SDL_ANYFORMAT | SDL_RESIZABLE | - SDL_DOUBLEBUF | SDL_HWPALETTE; + SDL_Window *screen; + SDL_Renderer *renderer; + + Uint32 vmode = SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_RESIZABLE; + /*SDL_WINDOW_OPENGL;*/ if (SDL_Init(SDL_INIT_VIDEO) == -1) return NULL; - SDL_WM_SetIcon(SDL_LoadBMP(icon), NULL); - screen = SDL_SetVideoMode(width, height, depth, vmode); - if(!screen){ + screen = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, vmode); + if(!screen) { printf("Unable to set video mode: %s\n", SDL_GetError()); exit(EXIT_FAILURE); } + SDL_SetWindowIcon(screen, SDL_LoadBMP(icon)); - SDL_WM_SetCaption(title, 0); + renderer = SDL_CreateRenderer(screen, -1, 0); + if(!renderer) { + printf("Unable to set renderer: %s\n", SDL_GetError()); + exit(EXIT_FAILURE); + } if (TTF_Init() == -1) { printf("Unable to initialize SDL_ttf(fonts): %s \n", TTF_GetError()); @@ -50,7 +55,7 @@ Screen *gr_newscreen(int width,int height,const char *icon,const char *title) { font = loadfont("FreeMono.ttf", FONT_SIZE); - return (Screen*) screen; + return (Screen*) renderer; } void gr_destroyscreen (Screen *screen) { @@ -59,85 +64,61 @@ void gr_destroyscreen (Screen *screen) { } /* DRAW FUNCTIONS */ - -/* Bresenham's line_algorithm */ void gr_drawline (Screen *screen, int x0, int y0, int x1, int y1) { - int pos, dx, dy, sx, sy, err; - SDL_Surface *sdl_screen = (SDL_Surface*) screen; - - dx = abs(x1 - x0); - dy = abs(y1 - y0); - - if (x0 < x1) sx = 1; else sx = -1; - if (y0 < y1) sy = 1; else sy = -1; - - err = dx-dy; - - while(x0 != x1 || y0 != y1) { - int pos = (sdl_screen->w * y0) + x0; - int e2 = err + err; - ((Uint32 *) sdl_screen->pixels)[pos] = color; - if (e2 > -dy) { - err = err - dy; - x0 = x0 + sx; - } - if (e2 < dx) { - err = err + dx; - y0 = y0 + sy; - } - } - /* draw last pixel */ - pos = (sdl_screen->w * y0) + x0; - ((Uint32 *) sdl_screen->pixels)[pos] = color; - - SDL_Flip(sdl_screen); + SDL_Renderer *sdl_screen = (SDL_Renderer*) screen; + SDL_RenderDrawLine(sdl_screen, x0, y0, x1, y1); + SDL_RenderPresent(sdl_screen); } void gr_drawblock (Screen *screen, int x0 , int x1 , int y, int blockheight) { - SDL_Surface *sdl_screen = (SDL_Surface*) screen; - int i, pos = (sdl_screen->w * y) + x0; - int dx = x1 - x0 + 1; /* +1 -> draw x0 AND x1 */ - int y1 = y + blockheight; /* draw y but not y1 */ - - while(y < y1) { - for (i = x0; i <= x1; i++, pos++) { - ((Uint32 *) sdl_screen->pixels)[pos] = color; - } - pos = pos + sdl_screen->w - dx; /* go to the beginning of the next line */ - y++; - } - SDL_Flip(screen); + SDL_Renderer *sdl_screen = (SDL_Renderer*) screen; + SDL_Rect rect; + rect.x = x0; + rect.y = y; + rect.w = x1 - x0 + 1; /* +1 -> draw x0 AND x1 */ + rect.h = blockheight; + SDL_RenderFillRect(sdl_screen, &rect); + SDL_RenderPresent(sdl_screen); } void gr_drawtext(Screen *screen, const char *text, int x, int y) { - SDL_Surface *tsurface = TTF_RenderText_Solid(font, text, fontcolor); + SDL_Renderer *sdl_screen = (SDL_Renderer*) screen; + SDL_Surface *tsurface; + SDL_Texture *ttexture; SDL_Rect rect; rect.x = x; rect.y = y; + rect.w = strlen(text) * gr_textwidth; + rect.h = gr_textheight; + tsurface = TTF_RenderText_Solid(font, text, fontcolor); if(!tsurface) { - printf("Unable to write text: %s \n", TTF_GetError()); + printf("Unable to create text surface: %s \n", TTF_GetError()); exit(EXIT_FAILURE); - } else { - SDL_BlitSurface(tsurface, NULL, screen, &rect); - SDL_Flip(screen); - SDL_FreeSurface(tsurface); } + + ttexture = SDL_CreateTextureFromSurface(sdl_screen, tsurface); + SDL_FreeSurface(tsurface); + if(!ttexture) { + printf("Unable to create text texture: %s \n", SDL_GetError()); + exit(EXIT_FAILURE); + } + + SDL_RenderCopy(sdl_screen, ttexture, NULL, &rect); + SDL_RenderPresent(sdl_screen); } void gr_drawbackground(Screen *screen, Color clr) { - SDL_Surface *sdl_screen = (SDL_Surface*) screen; - color = SDL_MapRGB(sdl_screen->format, getRed(clr), getGreen(clr), - getBlue(clr)); - SDL_FillRect(sdl_screen, &sdl_screen->clip_rect, color); - SDL_Flip(sdl_screen); + SDL_Renderer *sdl_screen = (SDL_Renderer*) screen; + SDL_SetRenderDrawColor(sdl_screen, getRed(clr), getGreen(clr), getBlue(clr), SDL_ALPHA_OPAQUE); + SDL_RenderClear(sdl_screen); + SDL_RenderPresent(sdl_screen); } /* SET FUNCTIONS */ void gr_setdrawcolor(Screen *screen, Color clr) { - SDL_Surface *sdl_screen = (SDL_Surface*) screen; - color = SDL_MapRGB(sdl_screen->format, getRed(clr), getGreen(clr), - getBlue(clr)); + SDL_Renderer *sdl_screen = (SDL_Renderer*) screen; + SDL_SetRenderDrawColor(sdl_screen, getRed(clr), getGreen(clr), getBlue(clr), SDL_ALPHA_OPAQUE); } void gr_settextcolor (Screen *screen, Color clr) {