From 1165742c88f2daea799bc4a23787423f3701bd68 Mon Sep 17 00:00:00 2001 From: Ricardo Cruz Date: Sat, 12 Jun 2004 16:07:12 +0000 Subject: [PATCH] Optimized gradient when top color is the same as the bottom one. SVN-Revision: 1472 --- src/screen/drawing_context.cpp | 25 ++++++++++++++++--------- src/screen/screen.h | 8 +++++++- src/screen/texture.cpp | 36 ++++++++++++++++++++++-------------- 3 files changed, 45 insertions(+), 24 deletions(-) diff --git a/src/screen/drawing_context.cpp b/src/screen/drawing_context.cpp index c5903f5ed..13d260dc7 100644 --- a/src/screen/drawing_context.cpp +++ b/src/screen/drawing_context.cpp @@ -181,15 +181,22 @@ DrawingContext::draw_gradient(DrawingRequest& request) else { #endif - float redstep = (float(bottom.red)-float(top.red)) / float(screen->h); - float greenstep = (float(bottom.green)-float(top.green)) / float(screen->h); - float bluestep = (float(bottom.blue) - float(top.blue)) / float(screen->h); - - for(float y = 0; y < screen->h; y += 2) - fillrect(0, (int)y, screen->w, 2, - int(float(top.red) + redstep * y), - int(float(top.green) + greenstep * y), - int(float(top.blue) + bluestep * y), 255); + if(&top == &bottom) + { + fillrect(0, 0, screen->w, screen->h, top.red, top.green, top.blue); + } + else + { + float redstep = (float(bottom.red)-float(top.red)) / float(screen->h); + float greenstep = (float(bottom.green)-float(top.green)) / float(screen->h); + float bluestep = (float(bottom.blue) - float(top.blue)) / float(screen->h); + + for(float y = 0; y < screen->h; y += 2) + fillrect(0, (int)y, screen->w, 2, + int(float(top.red) + redstep * y), + int(float(top.green) + greenstep * y), + int(float(top.blue) + bluestep * y), 255); + } #ifndef NOOPENGL } diff --git a/src/screen/screen.h b/src/screen/screen.h index 85744e833..2383573e2 100644 --- a/src/screen/screen.h +++ b/src/screen/screen.h @@ -24,7 +24,7 @@ #ifndef NOOPENGL #include #endif - +#include class Color { public: @@ -40,6 +40,12 @@ public: : red(o.red), green(o.green), blue(o.blue), alpha(o.alpha) { } + bool operator==(const Color& o) + { if(red == o.red && green == o.green && + blue == o.blue && alpha == o.alpha) + return true; + return false; } + Uint8 red, green, blue, alpha; }; diff --git a/src/screen/texture.cpp b/src/screen/texture.cpp index 6e9f82eb8..e92ad6140 100644 --- a/src/screen/texture.cpp +++ b/src/screen/texture.cpp @@ -373,21 +373,29 @@ sdl_surface_from_gradient(Color top, Color bottom, int w, int h) if(sdl_surface == NULL) st_abort("Cannot create surface for the gradient", "SURFACE"); - float redstep = (float(bottom.red)-float(top.red)) / float(h); - float greenstep = (float(bottom.green)-float(top.green)) / float(h); - float bluestep = (float(bottom.blue) - float(top.blue)) / float(h); - - SDL_Rect rect; - rect.x = 0; - rect.w = w; - rect.h = 1; - for(float y = 0; y < h; y++) + if(top == bottom) { - rect.y = (int)y; - SDL_FillRect(sdl_surface, &rect, SDL_MapRGB(sdl_surface->format, - int(float(top.red) + redstep * y), - int(float(top.green) + greenstep * y), - int(float(top.blue) + bluestep * y))); + SDL_FillRect(sdl_surface, NULL, SDL_MapRGB(sdl_surface->format, + top.red, top.green, top.blue)); + } + else + { + float redstep = (float(bottom.red)-float(top.red)) / float(h); + float greenstep = (float(bottom.green)-float(top.green)) / float(h); + float bluestep = (float(bottom.blue) - float(top.blue)) / float(h); + + SDL_Rect rect; + rect.x = 0; + rect.w = w; + rect.h = 1; + for(float y = 0; y < h; y++) + { + rect.y = (int)y; + SDL_FillRect(sdl_surface, &rect, SDL_MapRGB(sdl_surface->format, + int(float(top.red) + redstep * y), + int(float(top.green) + greenstep * y), + int(float(top.blue) + bluestep * y))); + } } return sdl_surface; -- 2.11.0