OpenXcom  1.0
Open-source clone of the original X-Com
Surface.h
1 #pragma once
2 /*
3  * Copyright 2010-2016 OpenXcom Developers.
4  *
5  * This file is part of OpenXcom.
6  *
7  * OpenXcom is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * OpenXcom is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
19  */
20 #include <SDL.h>
21 #include <string>
22 #include <vector>
23 #include "GraphSubset.h"
24 
25 namespace OpenXcom
26 {
27 
28 class Font;
29 class Language;
30 
38 class Surface
39 {
40 protected:
41  SDL_Surface *_surface;
42  int _x, _y;
43  SDL_Rect _crop, _clear;
44  bool _visible, _hidden, _redraw, _tftdMode;
45  void *_alignedBuffer;
46  std::string _tooltip;
47 
49  template <typename T>
50  void rawCopy(const std::vector<T> &bytes);
52  void resize(int width, int height);
53 public:
55  Surface(int width, int height, int x = 0, int y = 0, int bpp = 8);
57  Surface(const Surface& other);
59  virtual ~Surface();
61  void loadRaw(const std::vector<unsigned char> &bytes);
63  void loadRaw(const std::vector<char> &bytes);
65  void loadScr(const std::string &filename);
67  void loadSpk(const std::string &filename);
69  void loadBdy(const std::string &filename);
71  void loadImage(const std::string &filename);
73  void clear(Uint32 color = 0);
75  void offset(int off, int min = -1, int max = -1, int mul = 1);
77  void offsetBlock(int off, int blk = 16, int mul = 1);
79  void invert(Uint8 mid);
81  virtual void think();
83  virtual void draw();
85  virtual void blit(Surface *surface);
87  virtual void initText(Font *, Font *, Language *) {};
89  void copy(Surface *surface);
91  void drawRect(SDL_Rect *rect, Uint8 color);
93  void drawRect(Sint16 x, Sint16 y, Sint16 w, Sint16 h, Uint8 color);
95  void drawLine(Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint8 color);
97  void drawCircle(Sint16 x, Sint16 y, Sint16 r, Uint8 color);
99  void drawPolygon(Sint16 *x, Sint16 *y, int n, Uint8 color);
101  void drawTexturedPolygon(Sint16 *x, Sint16 *y, int n, Surface *texture, int dx, int dy);
103  void drawString(Sint16 x, Sint16 y, const char *s, Uint8 color);
105  virtual void setPalette(SDL_Color *colors, int firstcolor = 0, int ncolors = 256);
110  SDL_Color *getPalette() const
111  {
112  return _surface->format->palette->colors;
113  }
115  virtual void setX(int x);
120  int getX() const
121  {
122  return _x;
123  }
125  virtual void setY(int y);
130  int getY() const
131  {
132  return _y;
133  }
135  virtual void setVisible(bool visible);
137  bool getVisible() const;
139  void resetCrop();
141  SDL_Rect *getCrop();
149  void setPixel(int x, int y, Uint8 pixel)
150  {
151  if (x < 0 || x >= getWidth() || y < 0 || y >= getHeight())
152  {
153  return;
154  }
155  *getRaw(x, y) = pixel;
156  }
165  void setPixelIterative(int *x, int *y, Uint8 pixel)
166  {
167  setPixel(*x, *y, pixel);
168  (*x)++;
169  if (*x == getWidth())
170  {
171  (*y)++;
172  *x = 0;
173  }
174  }
181  Uint8 getPixel(int x, int y) const
182  {
183  if (x < 0 || x >= getWidth() || y < 0 || y >= getHeight())
184  {
185  return 0;
186  }
187  return *getRaw(x, y);
188  }
195  Uint8 *getRaw(int x, int y) const
196  {
197  return (Uint8 *)_surface->pixels + (y * _surface->pitch + x * _surface->format->BytesPerPixel);
198  }
203  SDL_Surface *getSurface() const
204  {
205  return _surface;
206  }
211  int getWidth() const
212  {
213  return _surface->w;
214  }
216  virtual void setWidth(int width);
221  int getHeight() const
222  {
223  return _surface->h;
224  }
226  virtual void setHeight(int height);
228  void setHidden(bool hidden);
230  void lock();
232  void unlock();
234  void blitNShade(Surface *surface, int x, int y, int shade, bool half = false, int newBaseColor = 0);
236  void blitNShade(Surface *surface, int x, int y, int shade, GraphSubset range);
238  void invalidate(bool valid = true);
240  std::string getTooltip() const;
242  void setTooltip(const std::string &tooltip);
243 
245  virtual void setColor(Uint8 /*color*/) { /* empty by design */ };
247  virtual void setSecondaryColor(Uint8 /*color*/) { /* empty by design */ };
249  virtual void setBorderColor(Uint8 /*color*/) { /* empty by design */ };
251  virtual void setHighContrast(bool /*contrast*/) { /* empty by design */ };
253  void setTFTDMode(bool mode);
255  bool isTFTDMode() const;
256 
257 };
258 
259 }
virtual void setY(int y)
Sets the Y position of the surface.
Definition: Surface.cpp:805
void drawRect(SDL_Rect *rect, Uint8 color)
Draws a filled rectangle on the surface.
Definition: Surface.cpp:706
void blitNShade(Surface *surface, int x, int y, int shade, bool half=false, int newBaseColor=0)
Specific blit function to blit battlescape terrain data in different shades in a fast way...
Definition: Surface.cpp:965
int getWidth() const
Returns the width of the surface.
Definition: Surface.h:211
virtual void setSecondaryColor(Uint8)
Sets the secondary color of the surface.
Definition: Surface.h:247
int getHeight() const
Returns the height of the surface.
Definition: Surface.h:221
void drawLine(Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint8 color)
Draws a line on the surface.
Definition: Surface.cpp:737
virtual ~Surface()
Cleans up the surface.
Definition: Surface.cpp:209
bool isTFTDMode() const
checks if this is a TFTD mode surface.
Definition: Surface.cpp:1104
virtual void setX(int x)
Sets the X position of the surface.
Definition: Surface.cpp:796
std::string getTooltip() const
Gets the tooltip of the surface.
Definition: Surface.cpp:1017
virtual void setVisible(bool visible)
Sets the surface&#39;s visibility.
Definition: Surface.cpp:815
virtual void setHighContrast(bool)
Sets the high contrast color setting of the surface.
Definition: Surface.h:251
SDL_Color * getPalette() const
Returns the surface&#39;s 8bpp palette.
Definition: Surface.h:110
virtual void setColor(Uint8)
Sets the color of the surface.
Definition: Surface.h:245
virtual void draw()
Draws the surface&#39;s graphic.
Definition: Surface.cpp:629
void rawCopy(const std::vector< T > &bytes)
Copies raw pixels.
Definition: Surface.cpp:220
void loadImage(const std::string &filename)
Loads a general image file.
Definition: Surface.cpp:291
void offsetBlock(int off, int blk=16, int mul=1)
Offsets the surface&#39;s colors in a color block.
Definition: Surface.cpp:542
int getY() const
Returns the position of the surface in the Y axis.
Definition: Surface.h:130
void setTooltip(const std::string &tooltip)
Sets the tooltip of the surface.
Definition: Surface.cpp:1027
Contains strings used throughout the game for localization.
Definition: Language.h:39
virtual void setBorderColor(Uint8)
Sets the border colour of the surface.
Definition: Surface.h:249
Takes care of loading and storing each character in a sprite font.
Definition: Font.h:46
void unlock()
Unlocks the surface.
Definition: Surface.cpp:890
SDL_Rect * getCrop()
Gets the cropping rectangle for the surface.
Definition: Surface.cpp:845
virtual void blit(Surface *surface)
Blits this surface onto another one.
Definition: Surface.cpp:642
Uint8 * getRaw(int x, int y) const
Returns the pointer to a specified pixel in the surface.
Definition: Surface.h:195
void drawString(Sint16 x, Sint16 y, const char *s, Uint8 color)
Draws a string on the surface.
Definition: Surface.cpp:787
void loadBdy(const std::string &filename)
Loads a TFTD BDY graphic.
Definition: Surface.cpp:422
void setPixel(int x, int y, Uint8 pixel)
Changes the color of a pixel in the surface, relative to the top-left corner of the surface...
Definition: Surface.h:149
void loadSpk(const std::string &filename)
Loads an X-Com SPK graphic.
Definition: Surface.cpp:366
virtual void setPalette(SDL_Color *colors, int firstcolor=0, int ncolors=256)
Sets the surface&#39;s palette.
Definition: Surface.cpp:856
Element that is blit (rendered) onto the screen.
Definition: Surface.h:38
int getX() const
Returns the position of the surface in the X axis.
Definition: Surface.h:120
void resize(int width, int height)
Resizes the surface.
Definition: Surface.cpp:1039
virtual void think()
Runs surface functionality every cycle.
Definition: Surface.cpp:618
Surface(int width, int height, int x=0, int y=0, int bpp=8)
Creates a new surface with the specified size and position.
Definition: Surface.cpp:139
SDL_Surface * getSurface() const
Returns the internal SDL_Surface for SDL calls.
Definition: Surface.h:203
void invert(Uint8 mid)
Inverts the surface&#39;s colors.
Definition: Surface.cpp:592
void loadScr(const std::string &filename)
Loads an X-Com SCR graphic.
Definition: Surface.cpp:273
void copy(Surface *surface)
Copies a portion of another surface into this one.
Definition: Surface.cpp:672
void drawPolygon(Sint16 *x, Sint16 *y, int n, Uint8 color)
Draws a filled polygon on the surface.
Definition: Surface.cpp:761
Definition: GraphSubset.h:32
void setHidden(bool hidden)
Sets the surface&#39;s special hidden flag.
Definition: Surface.cpp:869
void lock()
Locks the surface.
Definition: Surface.cpp:880
bool getVisible() const
Gets the surface&#39;s visibility.
Definition: Surface.cpp:824
void setPixelIterative(int *x, int *y, Uint8 pixel)
Changes the color of a pixel in the surface and returns the next pixel position.
Definition: Surface.h:165
Uint8 getPixel(int x, int y) const
Returns the color of a specified pixel in the surface.
Definition: Surface.h:181
void loadRaw(const std::vector< unsigned char > &bytes)
Loads a raw pixel array.
Definition: Surface.cpp:247
void drawCircle(Sint16 x, Sint16 y, Sint16 r, Uint8 color)
Draws a filled circle on the surface.
Definition: Surface.cpp:749
virtual void setWidth(int width)
Sets the width of the surface.
Definition: Surface.cpp:1073
void resetCrop()
Resets the cropping rectangle for the surface.
Definition: Surface.cpp:833
void setTFTDMode(bool mode)
Sets this button to use a colour lookup table instead of inversion for its alternate form...
Definition: Surface.cpp:1095
virtual void initText(Font *, Font *, Language *)
Initializes the surface&#39;s various text resources.
Definition: Surface.h:87
void invalidate(bool valid=true)
Invalidate the surface: force it to be redrawn.
Definition: Surface.cpp:1007
Definition: BaseInfoState.cpp:40
virtual void setHeight(int height)
Sets the height of the surface.
Definition: Surface.cpp:1085
void offset(int off, int min=-1, int max=-1, int mul=1)
Offsets the surface&#39;s colors by a set amount.
Definition: Surface.cpp:492
void drawTexturedPolygon(Sint16 *x, Sint16 *y, int n, Surface *texture, int dx, int dy)
Draws a textured polygon on the surface.
Definition: Surface.cpp:775
void clear(Uint32 color=0)
Clears the surface&#39;s contents eith a specified colour.
Definition: Surface.cpp:477