image_modifications.hpp

Go to the documentation of this file.
00001 /* $Id: image_modifications.hpp 54113 2012-05-06 22:06:46Z jamit $ */
00002 /*
00003    Copyright (C) 2009 - 2012 by Ignacio R. Morelle <shadowm2006@gmail.com>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 /** @file */
00017 
00018 #ifndef IMAGE_MODIFICATIONS_HPP_INCLUDED
00019 #define IMAGE_MODIFICATIONS_HPP_INCLUDED
00020 
00021 #include "sdl_utils.hpp"
00022 #include <queue>
00023 
00024 namespace image {
00025 
00026 class modification;
00027 struct mod_ptr_comparator_;
00028 /// A priority queue used to enforce using the rc modifications first
00029 typedef std::priority_queue<modification*,
00030                 std::vector<modification*>,
00031                 mod_ptr_comparator_> modification_queue;
00032 
00033 /// Base abstract class for an image-path modification
00034 class modification
00035 {
00036 public:
00037 
00038     /** Exception thrown by the operator() when an error occurs. */
00039     struct texception
00040         : public tlua_jailbreak_exception
00041     {
00042         /**
00043          * Constructor.
00044          *
00045          * @pre message_stream.str()[message_stream.str().size() - 1] == '\n'
00046          *
00047          * @param message_stream  Stream with the error message regarding
00048          *                        the failed operation.
00049          */
00050         texception(const std::stringstream& message_stream);
00051 
00052         /**
00053          * Constructor.
00054          *
00055          * @pre message[message.size() - 1] == '\n'
00056          *
00057          * @param message         String with the error message regarding
00058          *                        the failed operation.
00059          */
00060         texception(const std::string& message);
00061 
00062         ~texception() throw() {}
00063 
00064         /** The error message regarding the failed operation. */
00065         const std::string message;
00066 
00067     private:
00068 
00069         IMPLEMENT_LUA_JAILBREAK_EXCEPTION(texception)
00070     };
00071 
00072     /// Decodes modifications from a modification string
00073     static modification_queue decode(const std::string&);
00074 
00075     virtual ~modification() {}
00076 
00077     ///Applies the image-path modification on the specified surface
00078     virtual surface operator()(const surface& src) const = 0;
00079 
00080     /// Specifies the priority of the modification
00081     virtual int priority() const { return 0; }
00082 };
00083 
00084 /// A functor for comparing modification pointers
00085 struct mod_ptr_comparator_
00086 {
00087     /// Provides a descending priority ordering
00088     bool operator()(const modification* a, const modification* b) const;
00089 };
00090 
00091 /**
00092  * Recolor (RC/TC/PAL) modification.
00093  * It is used not only for color-range-based recoloring ("~RC(magenta>teal)")
00094  * but also for team-color-based color range selection and recoloring
00095  * ("~TC(3,magenta)") and palette switches ("~PAL(000000,005000 > FFFFFF,FF00FF)").
00096  */
00097 class rc_modification : public modification
00098 {
00099 public:
00100     /**
00101      * Default constructor.
00102      */
00103     rc_modification()
00104         : rc_map_()
00105     {}
00106     /**
00107      * RC-map based constructor.
00108      * @param recolor_map The palette switch map.
00109      */
00110     rc_modification(const std::map<Uint32, Uint32>& recolor_map)
00111         : rc_map_(recolor_map)
00112     {}
00113     virtual surface operator()(const surface& src) const;
00114 
00115     // The rc modification has a higher priority
00116     virtual int priority() const { return 1; }
00117 
00118     bool no_op() const { return rc_map_.empty(); }
00119 
00120     const std::map<Uint32, Uint32>& map() const { return rc_map_;}
00121     std::map<Uint32, Uint32>& map() { return rc_map_;}
00122 
00123 private:
00124     std::map<Uint32, Uint32> rc_map_;
00125 };
00126 
00127 /**
00128  * Mirror (FL) modification.
00129  */
00130 class fl_modification : public modification
00131 {
00132 public:
00133     /**
00134      * Constructor.
00135      * @param horiz Horizontal mirror flag.
00136      * @param vert  Vertical mirror flag.
00137      */
00138     fl_modification(bool horiz = false, bool vert = false)
00139         : horiz_(horiz)
00140         , vert_(vert)
00141     {}
00142     virtual surface operator()(const surface& src) const;
00143 
00144     void set_horiz(bool val)  { horiz_ = val; }
00145     void set_vert(bool val)   { vert_ = val; }
00146     bool get_horiz() const    { return horiz_; }
00147     bool get_vert() const     { return vert_; }
00148     /** Toggle horizontal mirror flag.
00149      *  @return The new flag state after toggling. */
00150     bool toggle_horiz()       { return((horiz_ = !horiz_)); }
00151     /** Toggle vertical mirror flag.
00152      *  @return The new flag state after toggling. */
00153     bool toggle_vert()        { return((vert_ = !vert_)); }
00154 
00155     bool no_op() const { return ((!horiz_) && (!vert_)); }
00156 
00157 private:
00158     bool horiz_;
00159     bool vert_;
00160 };
00161 
00162 /**
00163  * Rotate (ROTATE) modification.
00164  */
00165 class rotate_modification : public modification
00166 {
00167 public:
00168     /**
00169      * Constructor.
00170      * @param amount  Amount of rotation (in degrees).
00171      *                Positive values are clockwise; negative are counter-clockwise.
00172      *                Only multiples of 90 are supported.
00173      */
00174     rotate_modification(int degrees = 90)
00175         : degrees_(degrees)
00176     {}
00177     virtual surface operator()(const surface& src) const;
00178 
00179     bool no_op() const { return degrees_ % 360 == 0; }
00180 
00181 private:
00182     int degrees_;
00183 };
00184 
00185 /**
00186  * Grayscale (GS) modification.
00187  */
00188 class gs_modification : public modification
00189 {
00190 public:
00191     virtual surface operator()(const surface& src) const;
00192 };
00193 
00194 /**
00195  * Crop (CROP) modification.
00196  */
00197 class crop_modification : public modification
00198 {
00199 public:
00200     crop_modification(const SDL_Rect& slice)
00201         : slice_(slice)
00202     {}
00203     virtual surface operator()(const surface& src) const;
00204 
00205     const SDL_Rect& get_slice() const;
00206 
00207 private:
00208     SDL_Rect slice_;
00209 };
00210 
00211 /**
00212  * Scale (BLIT) modification.
00213  */
00214 
00215 class blit_modification : public modification
00216 {
00217 public:
00218     blit_modification(const surface& surf, int x, int y)
00219         : surf_(surf), x_(x), y_(y)
00220     {}
00221     virtual surface operator()(const surface& src) const;
00222 
00223     const surface& get_surface() const;
00224     int get_x() const;
00225     int get_y() const;
00226 
00227 private:
00228     surface surf_;
00229     int x_;
00230     int y_;
00231 };
00232 
00233 /**
00234  * Mask (MASK) modification.
00235  */
00236 
00237 class mask_modification : public modification
00238 {
00239 public:
00240     mask_modification(const surface& mask, int x, int y)
00241         : mask_(mask), x_(x), y_(y)
00242     {}
00243     virtual surface operator()(const surface& src) const;
00244 
00245     const surface& get_mask() const;
00246     int get_x() const;
00247     int get_y() const;
00248 
00249 private:
00250     surface mask_;
00251     int x_;
00252     int y_;
00253 };
00254 
00255 /**
00256  * LIGHT (L) modification.
00257  */
00258 
00259 class light_modification : public modification
00260 {
00261 public:
00262     light_modification(const surface& surf)
00263         : surf_(surf)
00264     {}
00265     virtual surface operator()(const surface& src) const;
00266 
00267     const surface& get_surface() const;
00268 
00269 private:
00270     surface surf_;
00271 };
00272 
00273 /**
00274  * Scale (SCALE) modification.
00275  */
00276 class scale_modification : public modification
00277 {
00278 public:
00279     scale_modification(int width, int height)
00280         : w_(width), h_(height)
00281     {}
00282     virtual surface operator()(const surface& src) const;
00283     int get_w() const;
00284     int get_h() const;
00285 
00286 private:
00287     int w_, h_;
00288 };
00289 
00290 /**
00291  * Opacity (O) modification
00292  */
00293 class o_modification : public modification
00294 {
00295 public:
00296     o_modification(float opacity)
00297         : opacity_(opacity)
00298     {}
00299     virtual surface operator()(const surface& src) const;
00300     float get_opacity() const;
00301 
00302 private:
00303     float opacity_;
00304 };
00305 
00306 /**
00307  * Color-shift (CS, R, G, B) modification.
00308  */
00309 class cs_modification : public modification
00310 {
00311 public:
00312     cs_modification(int r, int g, int b)
00313         : r_(r), g_(g), b_(b)
00314     {}
00315     virtual surface operator()(const surface& src) const;
00316     int get_r() const;
00317     int get_g() const;
00318     int get_b() const;
00319 
00320 private:
00321     int r_, g_, b_;
00322 };
00323 
00324 /**
00325  * Color blending (BLEND) modification
00326  */
00327 class blend_modification : public modification
00328 {
00329 public:
00330     blend_modification(int r, int g, int b, float a)
00331         : r_(r), g_(g), b_(b), a_(a)
00332     {}
00333     virtual surface operator()(const surface& src) const;
00334     int get_r() const;
00335     int get_g() const;
00336     int get_b() const;
00337     float get_a() const;
00338 
00339 private:
00340     int r_, g_, b_;
00341     float a_;
00342 };
00343 
00344 /**
00345  * Gaussian-like blur (BL) modification.
00346  */
00347 class bl_modification : public modification
00348 {
00349 public:
00350     bl_modification(int depth)
00351         : depth_(depth)
00352     {}
00353     virtual surface operator()(const surface& src) const;
00354     int get_depth() const;
00355 
00356 private:
00357     int depth_;
00358 };
00359 
00360 /**
00361  * Overlay with ToD brightening (BRIGHTEN).
00362  */
00363 struct brighten_modification : modification
00364 {
00365     virtual surface operator()(const surface &src) const;
00366 };
00367 
00368 /**
00369  * Overlay with ToD darkening (DARKEN).
00370  */
00371 struct darken_modification : modification
00372 {
00373     virtual surface operator()(const surface &src) const;
00374 };
00375 
00376 /**
00377  * Fill background with a color (BG).
00378  */
00379 struct background_modification : modification
00380 {
00381     background_modification(SDL_Color const &c): color_(c) {}
00382     virtual surface operator()(const surface &src) const;
00383     const SDL_Color& get_color() const;
00384 
00385 private:
00386     SDL_Color color_;
00387 };
00388 
00389 } /* end namespace image */
00390 
00391 #endif /* !defined(IMAGE_MODIFICATIONS_HPP_INCLUDED) */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:02 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs