From 22328915ac56464987613cdf52e7316a4ec84dcf Mon Sep 17 00:00:00 2001 From: Thomas Perl Date: Sat, 1 Sep 2007 19:26:29 +0200 Subject: [PATCH] draw_line() function and pixel getting/setting Added new function to directly draw a line to the screen, and define macros: GET_PIXEL_DATA, GET_PIXEL_RGB, SET_PIXEL_RGB. --- graphics.c | 24 +++++++++++++++++------- graphics.h | 7 +++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/graphics.c b/graphics.c index f905d86..7848dd3 100644 --- a/graphics.c +++ b/graphics.c @@ -22,6 +22,7 @@ **/ #include +#include #include #include "tennix.h" @@ -228,11 +229,9 @@ int is_fading() { int font_get_metrics( unsigned int id, char ch, int* xp, int* wp) { SDL_Surface *bitmap; - SDL_PixelFormat *fmt; int pos, x = -1, w = 0; int search_pos = 0, search_x = 0; Uint8 red, green, blue; - Uint32 pixel; if( id > GR_COUNT) return 0; @@ -251,14 +250,10 @@ int font_get_metrics( unsigned int id, char ch, int* xp, int* wp) { } bitmap = images[id].data; - fmt = bitmap->format; SDL_LockSurface( bitmap); while( search_x < bitmap->w) { - pixel = *( (Uint32*)(bitmap->pixels + search_x*fmt->BytesPerPixel)); - red = (Uint8)(((pixel & fmt->Rmask) >> fmt->Rshift) << fmt->Rloss); - green = (Uint8)(((pixel & fmt->Gmask) >> fmt->Gshift) << fmt->Gloss); - blue = (Uint8)(((pixel & fmt->Bmask) >> fmt->Bshift) << fmt->Bloss); + GET_PIXEL_RGB( bitmap, search_x, 0, &red, &green, &blue); /* Increase pos counter if we have a "marker" pixel (255,0,255) */ if( red == 255 && green == 0 && blue == 255) { @@ -342,3 +337,18 @@ int font_get_string_width( unsigned int id, char* s) { return w; } +void draw_line( int x1, int y1, int x2, int y2, int r, int g, int b) { + float step, dx, dy, x = x1, y = y1; + int i; + + step = abs(x2-x1)>abs(y2-y1)?abs(x2-x1):abs(y2-y1); + dx = (x2-y1) / step; + dy = (y2-y1) / step; + + SDL_LockSurface( screen); + for( i=0; i<=step; i++) { + SET_PIXEL_RGB( screen, (int)(x+=dx), (int)(y+=dy), r, g, b); + } + SDL_UnlockSurface( screen); +} + diff --git a/graphics.h b/graphics.h index 04f9a7e..197fa2c 100644 --- a/graphics.h +++ b/graphics.h @@ -30,6 +30,11 @@ #define FADE_DURATION 500 +#define GET_PIXEL_DATA(surface,x,y) (*((Uint32*)(surface->pixels + x * surface->format->BytesPerPixel + y * surface->pitch))) + +#define GET_PIXEL_RGB(surface,x,y,r,g,b) (SDL_GetRGB( GET_PIXEL_DATA(surface,x,y), surface->format, r, g, b)) +#define SET_PIXEL_RGB(surface,x,y,r,g,b) (GET_PIXEL_DATA(surface,x,y)=SDL_MapRGB(surface->format, r, g, b)) + typedef struct { SDL_Surface* data; } Image; @@ -74,5 +79,7 @@ int font_draw_char( unsigned int id, char ch, int x_offset, int y_offset); void font_draw_string( unsigned int id, char* s, int x_offset, int y_offset, int start, int animation); int font_get_string_width( unsigned int id, char* s); +void draw_line( int x1, int y1, int x2, int y2, int r, int g, int b); + #endif -- 2.11.4.GIT