ManaPlus
Public Member Functions | Data Fields
TextChunk Class Reference

#include <textchunk.h>

Public Member Functions

 TextChunk ()
 
 TextChunk (const std::string &text0, const Color &color0, const Color &color1, Font *const font)
 
 ~TextChunk ()
 
bool operator== (const TextChunk &chunk) const
 
void generate (TTF_Font *const font, const float alpha)
 
void deleteImage ()
 

Data Fields

Imageimg
 
FonttextFont
 
std::string text
 
Color color
 
Color color2
 
TextChunkprev
 
TextChunknext
 

Detailed Description

Definition at line 42 of file textchunk.h.

Constructor & Destructor Documentation

◆ TextChunk() [1/2]

TextChunk::TextChunk ( )

Definition at line 52 of file textchunk.cpp.

52  :
53  img(nullptr),
54  textFont(nullptr),
55  text(),
56  color(),
57  color2(),
58  prev(nullptr),
59  next(nullptr)
60 {
61 #ifdef UNITTESTS
62  textChunkCnt ++;
63 #endif // UNITTESTS
64 }
Font * textFont
Definition: textchunk.h:64
TextChunk * next
Definition: textchunk.h:69
Image * img
Definition: textchunk.h:63
Color color
Definition: textchunk.h:66
Color color2
Definition: textchunk.h:67
TextChunk * prev
Definition: textchunk.h:68
std::string text
Definition: textchunk.h:65

◆ TextChunk() [2/2]

TextChunk::TextChunk ( const std::string &  text0,
const Color color0,
const Color color1,
Font *const  font 
)

Definition at line 66 of file textchunk.cpp.

69  :
70  img(nullptr),
71  textFont(font),
72  text(text0),
73  color(color0),
74  color2(color1),
75  prev(nullptr),
76  next(nullptr)
77 {
78 #ifdef UNITTESTS
79  textChunkCnt ++;
80 #endif // UNITTESTS
81 }

◆ ~TextChunk()

TextChunk::~TextChunk ( )

Definition at line 83 of file textchunk.cpp.

84 {
85  delete2(img)
86 #ifdef UNITTESTS
87  textChunkCnt --;
88 #endif // UNITTESTS
89 }
#define delete2(var)
Definition: delete2.h:25

References delete2, and img.

Member Function Documentation

◆ deleteImage()

void TextChunk::deleteImage ( )

◆ generate()

void TextChunk::generate ( TTF_Font *const  font,
const float  alpha 
)

Definition at line 97 of file textchunk.cpp.

99 {
100  BLOCK_START("TextChunk::generate")
101  SDL_Color sdlCol;
102  sdlCol.b = CAST_U8(color.b);
103  sdlCol.r = CAST_U8(color.r);
104  sdlCol.g = CAST_U8(color.g);
105 #ifdef USE_SDL2
106  sdlCol.a = 255;
107 #else // USE_SDL2
108 
109  sdlCol.unused = 0;
110 #endif // USE_SDL2
111 
113 
114  SDL_Surface *surface = MTTF_RenderUTF8_Blended(
115  font, strBuf, sdlCol);
116 
117  if (surface == nullptr)
118  {
119  img = nullptr;
120  BLOCK_END("TextChunk::generate")
121  return;
122  }
123 
124  const int width = surface->w;
125  const int height = surface->h;
126 
127  if (color.r != color2.r || color.g != color2.g
128  || color.b != color2.b)
129  { // outlining
130  SDL_Color sdlCol2;
131  SDL_Surface *const background = imageHelper->create32BitSurface(
132  width, height);
133  if (background == nullptr)
134  {
135  img = nullptr;
136  MSDL_FreeSurface(surface);
137  BLOCK_END("TextChunk::generate")
138  return;
139  }
140  sdlCol2.b = CAST_U8(color2.b);
141  sdlCol2.r = CAST_U8(color2.r);
142  sdlCol2.g = CAST_U8(color2.g);
143 #ifdef USE_SDL2
144  sdlCol2.a = 255;
145 #else // USE_SDL2
146 
147  sdlCol2.unused = 0;
148 #endif // USE_SDL2
149 
150  SDL_Surface *const surface2 = MTTF_RenderUTF8_Blended(
151  font, strBuf, sdlCol2);
152  if (surface2 == nullptr)
153  {
154  img = nullptr;
155  MSDL_FreeSurface(surface);
156  BLOCK_END("TextChunk::generate")
157  return;
158  }
159  SDL_Rect rect =
160  {
161  OUTLINE_SIZE,
162  0,
163  static_cast<Uint16>(surface->w),
164  static_cast<Uint16>(surface->h)
165  };
166  SurfaceImageHelper::combineSurface(surface2, nullptr,
167  background, &rect);
168  rect.x = -OUTLINE_SIZE;
169  SurfaceImageHelper::combineSurface(surface2, nullptr,
170  background, &rect);
171  rect.x = 0;
172  rect.y = -OUTLINE_SIZE;
173  SurfaceImageHelper::combineSurface(surface2, nullptr,
174  background, &rect);
175  rect.y = OUTLINE_SIZE;
176  SurfaceImageHelper::combineSurface(surface2, nullptr,
177  background, &rect);
178  rect.x = 0;
179  rect.y = 0;
180  SurfaceImageHelper::combineSurface(surface, nullptr,
181  background, &rect);
182  MSDL_FreeSurface(surface);
183  MSDL_FreeSurface(surface2);
184  surface = background;
185  }
187  surface, width, height, alpha);
188  MSDL_FreeSurface(surface);
189 
190  BLOCK_END("TextChunk::generate")
191 }
#define CAST_U8
Definition: cast.h:27
virtual Image * createTextSurface(SDL_Surface *const tmpImage, const int width, const int height, const float alpha) const
Definition: imagehelper.h:76
virtual SDL_Surface * create32BitSurface(int width, int height) const
#define MTTF_RenderUTF8_Blended(font, text, fg)
Definition: debug.h:59
#define MSDL_FreeSurface(surface)
Definition: debug.h:54
if(!vert) return
ImageHelper * imageHelper
Definition: imagehelper.cpp:44
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79
const char * getSafeUtf8String(const std::string &text)
char * strBuf
Definition: textchunk.cpp:46

References Color::b, BLOCK_END, BLOCK_START, CAST_U8, color, color2, ImageHelper::create32BitSurface(), ImageHelper::createTextSurface(), Color::g, getSafeUtf8String(), imageHelper, img, MSDL_FreeSurface, MTTF_RenderUTF8_Blended, anonymous_namespace{textchunk.cpp}::OUTLINE_SIZE, Color::r, strBuf, and text.

Referenced by Font::drawString(), and Font::generate().

◆ operator==()

bool TextChunk::operator== ( const TextChunk chunk) const

Definition at line 91 of file textchunk.cpp.

92 {
93  return chunk.text == text && chunk.color == color
94  && chunk.color2 == color2;
95 }

References color, color2, and text.

Field Documentation

◆ color

Color TextChunk::color

◆ color2

Color TextChunk::color2

◆ img

Image* TextChunk::img

◆ next

TextChunk* TextChunk::next

◆ prev

TextChunk* TextChunk::prev

◆ text

std::string TextChunk::text

◆ textFont

Font* TextChunk::textFont

The documentation for this class was generated from the following files: