helper.cpp

Go to the documentation of this file.
00001 /* $Id: helper.cpp 26697 2008-05-18 14:08:54Z mordante $ */
00002 /*
00003    copyright (C) 2008 by mark de wever <koraq@xs4all.nl>
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 version 2
00008    or at your option any later version.
00009    this program is distributed in the hope that it will be useful,
00010    but without any warranty.
00011 
00012    see the copying file for more details.
00013 */
00014 
00015 #include "gui/widgets/helper.hpp"
00016 
00017 #include "gui/widgets/settings.hpp"
00018 #include "sdl_utils.hpp"
00019 #include "serialization/string_utils.hpp"
00020 #include "log.hpp"
00021 
00022 #include "SDL_ttf.h"
00023 
00024 #define DBG_G LOG_STREAM_INDENT(debug, gui)
00025 #define LOG_G LOG_STREAM_INDENT(info, gui)
00026 #define WRN_G LOG_STREAM_INDENT(warn, gui)
00027 #define ERR_G LOG_STREAM_INDENT(err, gui)
00028 
00029 #define DBG_G_D LOG_STREAM_INDENT(debug, gui_draw)
00030 #define LOG_G_D LOG_STREAM_INDENT(info, gui_draw)
00031 #define WRN_G_D LOG_STREAM_INDENT(warn, gui_draw)
00032 #define ERR_G_D LOG_STREAM_INDENT(err, gui_draw)
00033 
00034 #define DBG_G_E LOG_STREAM_INDENT(debug, gui_event)
00035 #define LOG_G_E LOG_STREAM_INDENT(info, gui_event)
00036 #define WRN_G_E LOG_STREAM_INDENT(warn, gui_event)
00037 #define ERR_G_E LOG_STREAM_INDENT(err, gui_event)
00038 
00039 #define DBG_G_P LOG_STREAM_INDENT(debug, gui_parse)
00040 #define LOG_G_P LOG_STREAM_INDENT(info, gui_parse)
00041 #define WRN_G_P LOG_STREAM_INDENT(warn, gui_parse)
00042 #define ERR_G_P LOG_STREAM_INDENT(err, gui_parse)
00043 
00044 namespace gui2 {
00045 
00046 namespace {
00047     static bool initialized_ = false;
00048 }
00049 
00050 bool init() {
00051     if(initialized_) {
00052         return true;
00053     }
00054     
00055     load_settings();
00056 
00057     initialized_ = true;
00058 
00059     return initialized_;
00060 }
00061 
00062 SDL_Rect create_rect(const tpoint& origin, const tpoint& size) 
00063 { 
00064     return ::create_rect(origin.x, origin.y, size.x, size.y); 
00065 }
00066 tpoint& tpoint::operator+=(const tpoint& point)
00067 { 
00068     x += point.x; 
00069     y += point.y;
00070     return *this;
00071 }
00072 
00073 std::ostream &operator<<(std::ostream &stream, const tpoint& point)
00074 {
00075     stream << point.x << ',' << point.y;
00076     return stream;
00077 }
00078 
00079 
00080 int decode_font_style(const std::string& style)
00081 {
00082     if(style == "bold") {
00083         return TTF_STYLE_BOLD;
00084     } else if(style == "italic") {
00085         return TTF_STYLE_ITALIC;
00086     } else if(style == "underline") {
00087         return TTF_STYLE_UNDERLINE;
00088     } else if(style.empty() || style == "normal") {
00089         return TTF_STYLE_NORMAL;
00090     }
00091 
00092     ERR_G << "Unknown style '" << style << "' using 'normal' instead.\n";
00093 
00094     return TTF_STYLE_NORMAL;
00095 }
00096 
00097 Uint32 decode_colour(const std::string& colour)
00098 {
00099     std::vector<std::string> fields = utils::split(colour);
00100 
00101     // make sure we have four fields
00102     while(fields.size() < 4) fields.push_back("0");
00103 
00104     Uint32 result = 0;
00105     for(int i = 0; i < 4; ++i) {
00106         // shift the previous value before adding, since it's a nop on the
00107         // first run there's no need for an if.
00108         result = result << 8;
00109         result |= lexical_cast_default<int>(fields[i]);
00110     }
00111 
00112     return result;
00113 }
00114 
00115 surface save_background(const surface& background, const SDL_Rect& rect)
00116 {
00117     assert(background);
00118     assert((background->flags & SDL_RLEACCEL) == 0);
00119     assert(rect.x + rect.w <= background->w);
00120     assert(rect.y + rect.h <= background->h);
00121 
00122     surface result(SDL_CreateRGBSurface(SDL_SWSURFACE, 
00123         rect.w, rect.h, 32, 0xFF0000, 0xFF00, 0xFF, 0xFF000000));
00124 
00125     {
00126         // Extra scoping used for the surface_lock.
00127         surface_lock src_lock(background);
00128         surface_lock dst_lock(result);
00129 
00130         Uint32* src_pixels = reinterpret_cast<Uint32*>(src_lock.pixels());
00131         Uint32* dst_pixels = reinterpret_cast<Uint32*>(dst_lock.pixels());
00132 
00133         unsigned offset = rect.y * background->w + rect.x;
00134         for(unsigned y = 0; y < rect.h; ++y) {
00135             for(unsigned x = 0; x < rect.w; ++x) {
00136 
00137                 *dst_pixels++ = src_pixels[offset + x];
00138             
00139             }
00140             offset += background->w;
00141         }
00142     }
00143 
00144     return result;
00145 }
00146 
00147 void restore_background(const surface& restorer, 
00148         surface& background, const SDL_Rect& rect)
00149 {
00150     assert(background);
00151     assert(restorer);
00152     assert((background->flags & SDL_RLEACCEL) == 0);
00153     assert((restorer->flags & SDL_RLEACCEL) == 0);
00154     assert(rect.x + rect.w <= background->w);
00155     assert(rect.y + rect.h <= background->h);
00156 
00157     {
00158         // Extra scoping used for the surface_lock.
00159         surface_lock src_lock(restorer);
00160         surface_lock dst_lock(background);
00161 
00162         Uint32* src_pixels = reinterpret_cast<Uint32*>(src_lock.pixels());
00163         Uint32* dst_pixels = reinterpret_cast<Uint32*>(dst_lock.pixels());
00164 
00165         unsigned offset = rect.y * background->w + rect.x;
00166         for(unsigned y = 0; y < rect.h; ++y) {
00167             for(unsigned x = 0; x < rect.w; ++x) {
00168 
00169                 dst_pixels[offset + x] = *src_pixels++;
00170 
00171             }
00172             offset += background->w;
00173         }
00174     }
00175 }
00176 
00177 } // namespace gui2
00178 

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs