[Super-tux-commit] supertux/src/screen drawing_context.cpp,1.9,1.10 drawing_context.h,1.7,1.8 screen
Brought to you by:
wkendrick
From: Ricardo C. <rm...@us...> - 2004-06-23 10:18:10
|
Update of /cvsroot/super-tux/supertux/src/screen In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26828/src/screen Modified Files: drawing_context.cpp drawing_context.h screen.cpp screen.h Log Message: Moved line drawing from screen to drawing_context. Index: drawing_context.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/screen/drawing_context.cpp,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- drawing_context.cpp 14 Jun 2004 22:45:24 -0000 1.9 +++ drawing_context.cpp 23 Jun 2004 10:18:00 -0000 1.10 @@ -145,6 +145,24 @@ } void +DrawingContext::draw_line(const Vector& topleft, const Vector& botright, + Color color, int layer) +{ + DrawingRequest request; + + request.type = LINE; + request.layer = layer; + request.pos = transform.apply(topleft); + + LineRequest* linerequest = new LineRequest; + linerequest->botright = botright; + linerequest->color = color; + request.request_data = linerequest; + + drawingrequests.push_back(request); +} + +void DrawingContext::draw_surface_part(DrawingRequest& request) { SurfacePartRequest* surfacepartrequest @@ -288,6 +306,87 @@ delete fillrectrequest; } +/* Needed for line calculations */ +#define SGN(x) ((x)>0 ? 1 : ((x)==0 ? 0:(-1))) +#define ABS(x) ((x)>0 ? (x) : (-x)) + +void +DrawingContext::draw_line(DrawingRequest& request) +{ + LineRequest* linerequest = (LineRequest*) request.request_data; + + float x1 = request.pos.x; + float y1 = request.pos.y; + float x2 = linerequest->botright.x; + float y2 = linerequest->botright.y; + int r = linerequest->color.red; + int g = linerequest->color.green; + int b = linerequest->color.blue; + int a = linerequest->color.alpha; + +#ifndef NOOPENGL + if(use_gl) + { + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glColor4ub(r, g, b,a); + + glBegin(GL_LINES); + glVertex2f(x1, y1); + glVertex2f(x2, y2); + glEnd(); + glDisable(GL_BLEND); + } + else + { +#endif + /* Basic unantialiased Bresenham line algorithm */ + int lg_delta, sh_delta, cycle, lg_step, sh_step; + Uint32 color = SDL_MapRGBA(screen->format, r, g, b, a); + + lg_delta = (int)(x2 - x1); + sh_delta = (int)(y2 - y1); + lg_step = SGN(lg_delta); + lg_delta = ABS(lg_delta); + sh_step = SGN(sh_delta); + sh_delta = ABS(sh_delta); + if (sh_delta < lg_delta) + { + cycle = lg_delta >> 1; + while (x1 != x2) + { + drawpixel((int)x1, (int)y1, color); + cycle += sh_delta; + if (cycle > lg_delta) + { + cycle -= lg_delta; + y1 += sh_step; + } + x1 += lg_step; + } + drawpixel((int)x1, (int)y1, color); + } + cycle = sh_delta >> 1; + while (y1 != y2) + { + drawpixel((int)x1, (int)y1, color); + cycle += lg_delta; + if (cycle > sh_delta) + { + cycle -= sh_delta; + x1 += lg_step; + } + y1 += sh_step; + } + drawpixel((int)x1, (int)y1, color); +#ifndef NOOPENGL + + } +#endif + + delete linerequest; +} + void DrawingContext::do_drawing() { @@ -314,6 +413,9 @@ case FILLRECT: draw_filled_rect(*i); break; + case LINE: + draw_line(*i); + break; } } Index: drawing_context.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/screen/drawing_context.h,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- drawing_context.h 10 Jun 2004 14:09:49 -0000 1.7 +++ drawing_context.h 23 Jun 2004 10:18:00 -0000 1.8 @@ -69,7 +69,10 @@ /** draws a color gradient onto the whole screen */ void draw_gradient(Color from, Color to, int layer); /** fills a rectangle */ - void draw_filled_rect(const Vector& topleft, const Vector& downright, + void draw_filled_rect(const Vector& topleft, const Vector& size, + Color color, int layer); + /** draws an one-pixel line */ + void draw_line(const Vector& topleft, const Vector& downright, Color color, int layer); /** Processes all pending drawing requests and flushes the list */ @@ -102,7 +105,7 @@ enum RequestType { - SURFACE, SURFACE_PART, TEXT, GRADIENT, FILLRECT + SURFACE, SURFACE_PART, TEXT, GRADIENT, FILLRECT, LINE }; struct SurfacePartRequest @@ -129,6 +132,12 @@ Vector size; }; + struct LineRequest + { + Color color; + Vector botright; + }; + struct DrawingRequest { int layer; @@ -149,6 +158,7 @@ void draw_text(DrawingRequest& request); void draw_gradient(DrawingRequest& request); void draw_filled_rect(DrawingRequest& request); + void draw_line(DrawingRequest& request); typedef std::vector<DrawingRequest> DrawingRequests; DrawingRequests drawingrequests; Index: screen.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/screen/screen.h,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- screen.h 14 Jun 2004 22:45:24 -0000 1.5 +++ screen.h 23 Jun 2004 10:18:00 -0000 1.6 @@ -58,9 +58,9 @@ #define USE_ALPHA 0 #define IGNORE_ALPHA 1 -void drawline(int x1, int y1, int x2, int y2, int r, int g, int b, int a); -void fillrect(float x, float y, float w, float h, int r, int g, int b, - int a = 255); +void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel); +void drawpixel(int x, int y, Uint32 pixel); +void fillrect(float x, float y, float w, float h, int r, int g, int b, int a = 255); void fadeout(int fade_time); void shrink_fade(const Vector& point, int fade_time); Index: screen.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/screen/screen.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- screen.cpp 9 Jun 2004 04:43:15 -0000 1.3 +++ screen.cpp 23 Jun 2004 10:18:00 -0000 1.4 @@ -40,54 +40,6 @@ #include "drawing_context.h" #include "type.h" -/* Needed for line calculations */ -#define SGN(x) ((x)>0 ? 1 : ((x)==0 ? 0:(-1))) -#define ABS(x) ((x)>0 ? (x) : (-x)) - -/* --- FADE IN --- */ - -/** Fades the given surface into a black one. If fade_out is true, it will fade out, else -it will fade in */ - -#if 0 -void fade(Surface *surface, int seconds, bool fade_out); - -void fade(const std::string& surface, int seconds, bool fade_out) -{ -Surface* sur = new Surface(datadir + surface, IGNORE_ALPHA); -fade(sur, seconds, fade_out); -delete sur; -} - -void fade(Surface *surface, int seconds, bool fade_out) -{ -float alpha; -if (fade_out) - alpha = 0; -else - alpha = 255; - - int cur_time, old_time; - cur_time = SDL_GetTicks(); - - while(alpha >= 0 && alpha < 256) - { - surface->draw(0,0,(int)alpha); - flipscreen(); - - old_time = cur_time; - cur_time = SDL_GetTicks(); - - /* Calculate the next alpha value */ - float calc = (float) ((cur_time - old_time) / seconds); - if(fade_out) - alpha += 255 * calc; - else - alpha -= 255 * calc; - } -} -#endif - /* 'Stolen' from the SDL documentation. * Set the pixel at (x, y) to the given value * NOTE: The surface must be locked before calling this! @@ -153,70 +105,6 @@ SDL_UpdateRect(screen, x, y, 1, 1); } -void drawline(int x1, int y1, int x2, int y2, int r, int g, int b, int a) -{ -#ifndef NOOPENGL - if(use_gl) - { - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glColor4ub(r, g, b,a); - - glBegin(GL_LINES); - glVertex2f(x1, y1); - glVertex2f(x2, y2); - glEnd(); - glDisable(GL_BLEND); - } - else - { -#endif - - /* Basic unantialiased Bresenham line algorithm */ - int lg_delta, sh_delta, cycle, lg_step, sh_step; - Uint32 color = SDL_MapRGBA(screen->format, r, g, b, a); - - lg_delta = x2 - x1; - sh_delta = y2 - y1; - lg_step = SGN(lg_delta); - lg_delta = ABS(lg_delta); - sh_step = SGN(sh_delta); - sh_delta = ABS(sh_delta); - if (sh_delta < lg_delta) - { - cycle = lg_delta >> 1; - while (x1 != x2) - { - drawpixel(x1, y1, color); - cycle += sh_delta; - if (cycle > lg_delta) - { - cycle -= lg_delta; - y1 += sh_step; - } - x1 += lg_step; - } - drawpixel(x1, y1, color); - } - cycle = sh_delta >> 1; - while (y1 != y2) - { - drawpixel(x1, y1, color); - cycle += lg_delta; - if (cycle > sh_delta) - { - cycle -= sh_delta; - x1 += lg_step; - } - y1 += sh_step; - } - drawpixel(x1, y1, color); -#ifndef NOOPENGL - - } -#endif -} - /* --- FILL A RECT --- */ void fillrect(float x, float y, float w, float h, int r, int g, int b, int a) @@ -289,7 +177,6 @@ #endif } - #define LOOP_DELAY 20.0 void fadeout(int fade_time) |