From: Ricardo Cruz Date: Fri, 25 Jun 2004 11:54:37 +0000 (+0000) Subject: Moved drawing line code from drawing_context back to screen. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=1416edad687bd0bcdc9da304f200d41d8afb628d;p=supertux.git Moved drawing line code from drawing_context back to screen. The SDL code has the following issues: - it writes right away to the screen. Not waiting for the updating screen call. - it is extremly slow, since our screen is a hardware surface. Would be neat to have a draw_line func in drawing_context, but looks like it will have to wait. SVN-Revision: 1513 --- diff --git a/src/screen/drawing_context.cpp b/src/screen/drawing_context.cpp index dd5b107d0..9a2227441 100644 --- a/src/screen/drawing_context.cpp +++ b/src/screen/drawing_context.cpp @@ -145,24 +145,6 @@ DrawingContext::draw_filled_rect(const Vector& topleft, const Vector& size, } 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 @@ -306,87 +288,6 @@ DrawingContext::draw_filled_rect(DrawingRequest& request) 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() { @@ -413,9 +314,6 @@ DrawingContext::do_drawing() case FILLRECT: draw_filled_rect(*i); break; - case LINE: - draw_line(*i); - break; } } diff --git a/src/screen/drawing_context.h b/src/screen/drawing_context.h index 7e29a6c1c..1c6241929 100644 --- a/src/screen/drawing_context.h +++ b/src/screen/drawing_context.h @@ -71,9 +71,6 @@ public: /** fills a rectangle */ 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 */ void do_drawing(); @@ -105,7 +102,7 @@ private: enum RequestType { - SURFACE, SURFACE_PART, TEXT, GRADIENT, FILLRECT, LINE + SURFACE, SURFACE_PART, TEXT, GRADIENT, FILLRECT }; struct SurfacePartRequest @@ -132,12 +129,6 @@ private: Vector size; }; - struct LineRequest - { - Color color; - Vector botright; - }; - struct DrawingRequest { int layer; @@ -158,7 +149,6 @@ private: void draw_text(DrawingRequest& request); void draw_gradient(DrawingRequest& request); void draw_filled_rect(DrawingRequest& request); - void draw_line(DrawingRequest& request); typedef std::vector DrawingRequests; DrawingRequests drawingrequests; diff --git a/src/screen/screen.cpp b/src/screen/screen.cpp index 48f2b41b1..4e8ca5681 100644 --- a/src/screen/screen.cpp +++ b/src/screen/screen.cpp @@ -177,6 +177,74 @@ if(h < 0) #endif } +/* Needed for line calculations */ +#define SGN(x) ((x)>0 ? 1 : ((x)==0 ? 0:(-1))) +#define ABS(x) ((x)>0 ? (x) : (-x)) + +void +draw_line(float x1, float y1, float x2, float 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 = (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 +} + #define LOOP_DELAY 20.0 void fadeout(int fade_time) diff --git a/src/screen/screen.h b/src/screen/screen.h index 796924dca..f5192327a 100644 --- a/src/screen/screen.h +++ b/src/screen/screen.h @@ -29,10 +29,10 @@ class Color { public: Color() - : red(0), green(0), blue(0), alpha(0) + : red(0), green(0), blue(0), alpha(255) {} - Color(Uint8 red_, Uint8 green_, Uint8 blue_, Uint8 alpha_ = 0) + Color(Uint8 red_, Uint8 green_, Uint8 blue_, Uint8 alpha_ = 255) : red(red_), green(green_), blue(blue_), alpha(alpha_) {} @@ -61,6 +61,7 @@ class Vector; 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 draw_line(float x1, float y1, float x2, int r, int g, int b, int a = 255); void fadeout(int fade_time); void shrink_fade(const Vector& point, int fade_time);