ROLAND  0.70
Amstrad Emulator based on Caprice Source rewritten in C++.
canvas.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) by Fred Klaus *
3  * development@fkweb.de *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 #ifndef SDLTK_CANVAS_H
21 #define SDLTK_CANVAS_H
22 
23 #include "types.h"
24 #include "image.h"
25 #include "imagefont.h"
26 #include "SDL.h"
27 #include <cstdlib>
28 #include <algorithm>
29 
30 
31 namespace sdltk
32 {
33 
34  class Color;
35  class Point;
36  class Rect;
37  class Size;
38 
40  class Canvas
41  {
42 
43  public:
45  virtual ~Canvas() ROLAND_DEFAULT
46 
47  virtual void begin() {}
48  virtual void end() {}
49 
50  virtual void point(const Point & pos) = 0;
51  virtual void line (const Point & pos1, const Point & pos2) = 0;
52  virtual void rect (const Rect & rect) = 0;
53  virtual void fill (const Rect & rect) = 0;
54  virtual void image(const Image & image, const Rect & src, const Rect & dest) = 0;
55  virtual void setColor(const Color & color) {mColor = color;}
56  virtual void setClipRect(const Rect & rect) {}
57  virtual void clearClipRect() {}
58 
59  void setFont(const String & fname, const String & glyphs, uchar alpha=255);
60  void setSurface(SDL_Surface * const surface) {mSurface = surface;}
61 
62  void point(Point * const pos) {point(*pos);}
63  void line (Point * const pos1, Point * const pos2) {line(*pos1, *pos2);}
64  void rect (Rect * const rec) {rect(*rec);}
65  void fill (Rect * const rec) {fill(*rec);}
66  void image(Image * const img, const Point & pos) {image(*img, pos);}
67 
68  void image(const Image & img, const Point & pos);
69  void write(const Point & pos, const String & text);
70 
71  const Size & textSize(const String & text);
72  Uint16 textHeight() const;
73 
74  const ImageFont & font() const {return mFont;}
75  const Color & color() const {return mColor;}
76 
77  uint width() {return mSurface ? mSurface->w : 320;}
78 
79  const Rect & clipRect() const {return mClipRect;}
80 
81  protected:
82  SDL_Surface * mSurface;
88  };
89 
90 
91  inline void Canvas::image(const Image & img, const Point & pos)
92  {
93  Rect src(0, 0, img.size());
94  Rect dst(pos, img.size());
95 
96  image(img, src, dst);
97  }
98 
99  inline void Canvas::write(const Point & pos, const String & text)
100  {
101  Rect src, dest;
102  Point p(pos);
103  for (Uint16 i=0; i < text.length(); i++)
104  {
105  src = mFont.glyph(text[i]);
106  dest.set(p, src.size());
107  image(mFont.image(), src, dest);
108  p.setX(dest.x() + src.width() + mFont.spacing());
109  }
110  }
111 
112  inline const Size & Canvas::textSize(const String & text)
113  {
114  Rect rect;
115  mTextSize.clear();
116  for (Uint16 i=0; i < text.length(); i++)
117  {
118  rect = mFont.glyph(text[i]);
120  }
122  mTextSize.setHeight(rect.height());
123  return mTextSize;
124  }
125 
126  inline Uint16 Canvas::textHeight() const
127  {
128  Rect rect = mFont.glyph(' ');
129  return rect.height();
130  }
131 
132 } // sdltk
133 
134 #endif // SDLTK_CANVAS_H
const ImageFont & font() const
Definition: canvas.h:74
virtual void image(const Image &image, const Rect &src, const Rect &dest)=0
void rect(Rect *const rec)
Definition: canvas.h:64
Sint16 x() const
Definition: rect.h:102
Rect mClipRect
Definition: canvas.h:87
const Image & image()
Definition: imagefont.h:56
Image to display either on an OpenGL- or plain SDL-Surface.
Definition: image.h:39
Color mColor
Definition: canvas.h:84
void setFont(const String &fname, const String &glyphs, uchar alpha=255)
Definition: canvas.cpp:25
void setWidth(Uint16 w)
Definition: size.h:61
Uint16 spacing() const
Definition: imagefont.h:59
SDL_Surface * mSurface
Definition: canvas.h:82
unsigned char uchar
Definition: types.h:127
Size mTextSize
Definition: canvas.h:83
const Size & size
Definition: rect.h:39
virtual void line(const Point &pos1, const Point &pos2)=0
void set(const Point &pos)
Definition: rect.h:73
virtual void rect(const Rect &rect)=0
const Rect & clipRect() const
Definition: canvas.h:79
const Rect & glyph(Uint8 chr) const
Definition: imagefont.h:70
const Color & color() const
Definition: canvas.h:75
Definition: canvas.h:40
const Size & size() const
Definition: image.h:64
void setSurface(SDL_Surface *const surface)
Definition: canvas.h:60
virtual void clearClipRect()
Definition: canvas.h:57
virtual void setClipRect(const Rect &rect)
Definition: canvas.h:56
void clear()
initializes everything to 0.
Definition: size.h:54
Uint16 textHeight() const
Definition: canvas.h:126
ImageFont mFont
Definition: canvas.h:85
const Size & textSize(const String &text)
Definition: canvas.h:112
#define ROLAND_DEFAULT
Definition: compspec.h:46
unsigned int uint
Definition: types.h:121
virtual void setColor(const Color &color)
Definition: canvas.h:55
Defines a 2D point (x, y)
Definition: point.h:28
Uint16 height() const
Definition: rect.h:105
virtual void fill(const Rect &rect)=0
uint width()
Definition: canvas.h:77
defines a planar size (width, height)
Definition: size.h:30
void write(const Point &pos, const String &text)
Definition: canvas.h:99
void point(Point *const pos)
Definition: canvas.h:62
RGBA Color Type.
Definition: color.h:30
Uint16 width() const
Definition: size.h:57
Definition: rect.h:32
virtual void point(const Point &pos)=0
a font created from an Image
Definition: imagefont.h:33
std::string String
Definition: types.h:29
void setHeight(Uint16 h)
Definition: size.h:62
void image(Image *const img, const Point &pos)
Definition: canvas.h:66
Uint16 width() const
Definition: rect.h:104
void fill(Rect *const rec)
Definition: canvas.h:65
String mNumber
Definition: canvas.h:86
the SDL based Stuff
Definition: audio.cpp:22
Canvas() ROLAND_DEFAULT virtual ~Canvas() ROLAND_DEFAULT virtual void begin()
Definition: canvas.h:44
virtual void end()
Definition: canvas.h:48
void line(Point *const pos1, Point *const pos2)
Definition: canvas.h:63