Now using SDL2

make updated and cleaned
master
Pablo Musa 2014-06-03 20:44:23 -03:00
parent 827a48a77b
commit 2393407808
2 changed files with 64 additions and 82 deletions

View File

@ -1,41 +1,42 @@
# #
# Author: Pablo Musa # Author: Pablo Musa
# Creation Date: mar 27 2011 # Creation Date: mar 27 2011
# Last Modification: aug 22 2011 # Last Modification: jun 03 2014
# See Copyright Notice in COPYRIGHT # See Copyright Notice in COPYRIGHT
# #
# luamemprofiler - A Memory Profiler for the Lua language # 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 CC = gcc
CFLAGS = -g -Wall -ansi -pedantic -fPIC -shared
# compilation should generate Dynamic-Link Libraries # used the 'sdl2-config' output + the '-lSDL2_ttf'
CFLAGS = -g -Wall -O0 -ansi -pedantic SDL_LIBS = -L/home/pmusa/Programs/SDL/lib -Wl,-rpath,/home/pmusa/Programs/SDL/lib -lSDL2 -lSDL2_ttf -lpthread
LFLAGS = -I$(LUA_DIR) -fpic -shared 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 all: luamemprofiler.so
luamemprofiler.so: graphic.o lmp_struct.o vmemory.o lmp.o luamemprofiler.o 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 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 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 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 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 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: clean:
rm *.o rm *.o

117
gsdl.c
View File

@ -12,12 +12,12 @@
#include <SDL.h> #include <SDL.h>
#include <SDL_ttf.h> #include <SDL_ttf.h>
#include <string.h>
#include "graphic.h" #include "graphic.h"
/* GLOBAL VARIABLES */ /* GLOBAL VARIABLES */
static TTF_Font *font; static TTF_Font *font;
static SDL_Colour fontcolor = {0,0,0}; static SDL_Colour fontcolor = {0,0,0};
static Uint32 color;
static int gr_textwidth; static int gr_textwidth;
static int gr_textheight; static int gr_textheight;
@ -26,22 +26,27 @@ static TTF_Font* loadfont(char* file, int ptsize);
/* GLOBAL FUNCTIONS */ /* GLOBAL FUNCTIONS */
Screen *gr_newscreen(int width,int height,const char *icon,const char *title) { Screen *gr_newscreen(int width,int height,const char *icon,const char *title) {
int depth = 32; SDL_Window *screen;
SDL_Surface *screen; SDL_Renderer *renderer;
Uint32 vmode = SDL_HWSURFACE | SDL_ANYFORMAT | SDL_RESIZABLE |
SDL_DOUBLEBUF | SDL_HWPALETTE; Uint32 vmode = SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_RESIZABLE;
/*SDL_WINDOW_OPENGL;*/
if (SDL_Init(SDL_INIT_VIDEO) == -1) if (SDL_Init(SDL_INIT_VIDEO) == -1)
return NULL; return NULL;
SDL_WM_SetIcon(SDL_LoadBMP(icon), NULL); screen = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, vmode);
screen = SDL_SetVideoMode(width, height, depth, vmode); if(!screen) {
if(!screen){
printf("Unable to set video mode: %s\n", SDL_GetError()); printf("Unable to set video mode: %s\n", SDL_GetError());
exit(EXIT_FAILURE); 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) { if (TTF_Init() == -1) {
printf("Unable to initialize SDL_ttf(fonts): %s \n", TTF_GetError()); 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); font = loadfont("FreeMono.ttf", FONT_SIZE);
return (Screen*) screen; return (Screen*) renderer;
} }
void gr_destroyscreen (Screen *screen) { void gr_destroyscreen (Screen *screen) {
@ -59,85 +64,61 @@ void gr_destroyscreen (Screen *screen) {
} }
/* DRAW FUNCTIONS */ /* DRAW FUNCTIONS */
/* Bresenham's line_algorithm */
void gr_drawline (Screen *screen, int x0, int y0, int x1, int y1) { void gr_drawline (Screen *screen, int x0, int y0, int x1, int y1) {
int pos, dx, dy, sx, sy, err; SDL_Renderer *sdl_screen = (SDL_Renderer*) screen;
SDL_Surface *sdl_screen = (SDL_Surface*) screen; SDL_RenderDrawLine(sdl_screen, x0, y0, x1, y1);
SDL_RenderPresent(sdl_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);
} }
void gr_drawblock (Screen *screen, int x0 , int x1 , int y, int blockheight) { void gr_drawblock (Screen *screen, int x0 , int x1 , int y, int blockheight) {
SDL_Surface *sdl_screen = (SDL_Surface*) screen; SDL_Renderer *sdl_screen = (SDL_Renderer*) screen;
int i, pos = (sdl_screen->w * y) + x0; SDL_Rect rect;
int dx = x1 - x0 + 1; /* +1 -> draw x0 AND x1 */ rect.x = x0;
int y1 = y + blockheight; /* draw y but not y1 */ rect.y = y;
rect.w = x1 - x0 + 1; /* +1 -> draw x0 AND x1 */
while(y < y1) { rect.h = blockheight;
for (i = x0; i <= x1; i++, pos++) { SDL_RenderFillRect(sdl_screen, &rect);
((Uint32 *) sdl_screen->pixels)[pos] = color; SDL_RenderPresent(sdl_screen);
}
pos = pos + sdl_screen->w - dx; /* go to the beginning of the next line */
y++;
}
SDL_Flip(screen);
} }
void gr_drawtext(Screen *screen, const char *text, int x, int y) { 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; SDL_Rect rect;
rect.x = x; rect.x = x;
rect.y = y; rect.y = y;
rect.w = strlen(text) * gr_textwidth;
rect.h = gr_textheight;
tsurface = TTF_RenderText_Solid(font, text, fontcolor);
if(!tsurface) { if(!tsurface) {
printf("Unable to write text: %s \n", TTF_GetError()); printf("Unable to create text surface: %s \n", TTF_GetError());
exit(EXIT_FAILURE); 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) { void gr_drawbackground(Screen *screen, Color clr) {
SDL_Surface *sdl_screen = (SDL_Surface*) screen; SDL_Renderer *sdl_screen = (SDL_Renderer*) screen;
color = SDL_MapRGB(sdl_screen->format, getRed(clr), getGreen(clr), SDL_SetRenderDrawColor(sdl_screen, getRed(clr), getGreen(clr), getBlue(clr), SDL_ALPHA_OPAQUE);
getBlue(clr)); SDL_RenderClear(sdl_screen);
SDL_FillRect(sdl_screen, &sdl_screen->clip_rect, color); SDL_RenderPresent(sdl_screen);
SDL_Flip(sdl_screen);
} }
/* SET FUNCTIONS */ /* SET FUNCTIONS */
void gr_setdrawcolor(Screen *screen, Color clr) { void gr_setdrawcolor(Screen *screen, Color clr) {
SDL_Surface *sdl_screen = (SDL_Surface*) screen; SDL_Renderer *sdl_screen = (SDL_Renderer*) screen;
color = SDL_MapRGB(sdl_screen->format, getRed(clr), getGreen(clr), SDL_SetRenderDrawColor(sdl_screen, getRed(clr), getGreen(clr), getBlue(clr), SDL_ALPHA_OPAQUE);
getBlue(clr));
} }
void gr_settextcolor (Screen *screen, Color clr) { void gr_settextcolor (Screen *screen, Color clr) {