From 8518ee232ef7cc0cfa6e92afb3ad9e3bb2f474c8 Mon Sep 17 00:00:00 2001 From: Ricardo Cruz Date: Wed, 23 Jun 2004 10:18:00 +0000 Subject: [PATCH] Moved line drawing from screen to drawing_context. SVN-Revision: 1512 --- src/screen/drawing_context.cpp | 102 +++++++++++++++++++++++++++++++++++++ src/screen/drawing_context.h | 14 ++++- src/screen/screen.cpp | 113 ----------------------------------------- src/screen/screen.h | 6 +-- 4 files changed, 117 insertions(+), 118 deletions(-) diff --git a/src/screen/drawing_context.cpp b/src/screen/drawing_context.cpp index 9a2227441..dd5b107d0 100644 --- a/src/screen/drawing_context.cpp +++ b/src/screen/drawing_context.cpp @@ -145,6 +145,24 @@ 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 @@ -288,6 +306,87 @@ 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() { @@ -314,6 +413,9 @@ 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 d171c4413..7e29a6c1c 100644 --- a/src/screen/drawing_context.h +++ b/src/screen/drawing_context.h @@ -69,7 +69,10 @@ public: /** 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 @@ private: enum RequestType { - SURFACE, SURFACE_PART, TEXT, GRADIENT, FILLRECT + SURFACE, SURFACE_PART, TEXT, GRADIENT, FILLRECT, LINE }; struct SurfacePartRequest @@ -129,6 +132,12 @@ private: Vector size; }; + struct LineRequest + { + Color color; + Vector botright; + }; + struct DrawingRequest { int layer; @@ -149,6 +158,7 @@ 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 a89f0253e..48f2b41b1 100644 --- a/src/screen/screen.cpp +++ b/src/screen/screen.cpp @@ -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 @@ void drawpixel(int x, int y, Uint32 pixel) 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 @@ if(h < 0) #endif } - #define LOOP_DELAY 20.0 void fadeout(int fade_time) diff --git a/src/screen/screen.h b/src/screen/screen.h index 7932391d1..796924dca 100644 --- a/src/screen/screen.h +++ b/src/screen/screen.h @@ -58,9 +58,9 @@ class Vector; #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); -- 2.11.0