Graphics.hpp
Go to the documentation of this file.
1 
4 #ifndef GOSU_GRAPHICS_HPP
5 #define GOSU_GRAPHICS_HPP
6 
7 #include <Gosu/Fwd.hpp>
8 #include <Gosu/Color.hpp>
9 #include <Gosu/GraphicsBase.hpp>
10 #include <Gosu/TR1.hpp>
11 #include <memory>
12 
13 namespace Gosu
14 {
16  unsigned screenWidth();
17 
19  unsigned screenHeight();
20 
24  unsigned const MAX_TEXTURE_SIZE = 1024;
25 
26  #ifdef GOSU_IS_MAC
27  // TODO: Without this gigantic hack, Gosu crashes in the "scale" function,
28  // but _only_ when used from Ruby 1.9. It is unclear what might cause this -
29  // maybe a compiler bug that tries to use SSE functions with the wrong
30  // alignment. Adding __attribute__((aligned(16))) does not help, though.
31  struct Transform
32  {
33  double value[16];
34  bool operator==(const Transform &other) { for (int i = 0; i < 16; ++i) if ((*this)[i] != other[i]) return false; return true; }
35  const double &operator[](std::size_t idx) const { return value[idx]; }
36  double &operator[](std::size_t idx) { return value[idx]; }
37  };
38  #else
39  typedef std::tr1::array<double, 16> Transform;
40  #endif
41  Transform translate(double x, double y);
42  Transform rotate(double angle, double aroundX = 0, double aroundY = 0);
43  Transform scale(double factor);
44  Transform scale(double factorX, double factorY, double fromX = 0, double fromY = 0);
45 
49  class Graphics
50  {
51  struct Impl;
52  const GOSU_UNIQUE_PTR<Impl> pimpl;
53 
54 #if defined(GOSU_CPP11_ENABLED)
55  // explicitly forbid copying and moving
56  Graphics(Graphics&&) = delete;
57  Graphics& operator=(Graphics&&) = delete;
58  Graphics(const Graphics&) = delete;
59  Graphics& operator=(const Graphics&) = delete;
60 #endif
61 
62  public:
63  Graphics(unsigned physicalWidth, unsigned physicalHeight, bool fullscreen);
64  ~Graphics();
65 
66  // Undocumented until I have thought about this...
67  void setResolution(unsigned virtualWidth, unsigned virtualHeight);
68  // End of Undocumented
69 
70  unsigned width() const;
71  unsigned height() const;
72  bool fullscreen() const;
73 
76  bool begin(Color clearWithColor = Color::BLACK);
78  void end();
81  void flush();
82 
85  void beginGL();
87  void endGL();
94  void scheduleGL(const std::tr1::function<void()>& functor, ZPos z);
95 
97  void beginClipping(double x, double y, double width, double height);
99  void endClipping();
100 
102  void beginRecording();
108  GOSU_UNIQUE_PTR<Gosu::ImageData> endRecording(int width, int height);
109 
111  void pushTransform(const Transform& transform);
113  void popTransform();
114 
119  void drawLine(double x1, double y1, Color c1,
120  double x2, double y2, Color c2,
121  ZPos z, AlphaMode mode = amDefault);
122 
123  void drawTriangle(double x1, double y1, Color c1,
124  double x2, double y2, Color c2,
125  double x3, double y3, Color c3,
126  ZPos z, AlphaMode mode = amDefault);
127 
128  void drawQuad(double x1, double y1, Color c1,
129  double x2, double y2, Color c2,
130  double x3, double y3, Color c3,
131  double x4, double y4, Color c4,
132  ZPos z, AlphaMode mode = amDefault);
133 
136  GOSU_UNIQUE_PTR<ImageData> createImage(const Bitmap& src,
137  unsigned srcX, unsigned srcY, unsigned srcWidth, unsigned srcHeight,
138  unsigned borderFlags);
139  };
140 }
141 
142 #endif
Includes all parts of C++03 (TR1) that are relevant for Gosu.
void drawLine(double x1, double y1, Color c1, double x2, double y2, Color c2, ZPos z, AlphaMode mode=amDefault)
Draws a line from one point to another (last pixel exclusive).
void beginRecording()
Starts recording a macro. Cannot be nested.
std::tr1::array< double, 16 > Transform
Definition: Graphics.hpp:39
void beginGL()
Finishes all pending Gosu drawing operations and executes the following OpenGL code in a clean enviro...
Transform scale(double factor)
Contains declarations of all of Gosu&#39;s available classes.
Represents an RGBA color value with 8 bits for each channel.
Definition: Color.hpp:18
void drawTriangle(double x1, double y1, Color c1, double x2, double y2, Color c2, double x3, double y3, Color c3, ZPos z, AlphaMode mode=amDefault)
void flush()
Flushes the Z queue to the screen and starts a new one.
void endGL()
Resets Gosu into its default rendering state.
double ZPos
Represents the Z position of something drawn with Gosu&#39;s graphics system.
void beginClipping(double x, double y, double width, double height)
Enables clipping to a specified rectangle.
Rectangular area of pixels, each represented by a Color value.
Definition: Bitmap.hpp:20
void setResolution(unsigned virtualWidth, unsigned virtualHeight)
The color&#39;s channels will be interpolated.
void pushTransform(const Transform &transform)
Pushes one transformation onto the transformation stack.
Graphics(unsigned physicalWidth, unsigned physicalHeight, bool fullscreen)
unsigned screenHeight()
Returns the height, in pixels, of the user&#39;s primary screen.
Transform translate(double x, double y)
Serves as the target of all drawing and provides primitive drawing functionality. ...
Definition: Graphics.hpp:49
static const Color BLACK
Definition: Color.hpp:148
void popTransform()
Pops one transformation from the transformation stack.
AlphaMode
Determines the way colors are combined when one is drawn onto another.
bool begin(Color clearWithColor=Color::BLACK)
Prepares the graphics object for drawing.
unsigned width() const
unsigned height() const
unsigned const MAX_TEXTURE_SIZE
Returns the maximum size of an texture that will be allocated internally by Gosu. ...
Definition: Graphics.hpp:24
bool operator==(Color a, Color b)
Definition: Color.hpp:167
GOSU_UNIQUE_PTR< ImageData > createImage(const Bitmap &src, unsigned srcX, unsigned srcY, unsigned srcWidth, unsigned srcHeight, unsigned borderFlags)
Turns a portion of a bitmap into something that can be drawn on this graphics object.
GOSU_UNIQUE_PTR< Gosu::ImageData > endRecording(int width, int height)
Finishes building the macro and returns it as a drawable object.
Interface of the Color class.
bool fullscreen() const
unsigned screenWidth()
Returns the width, in pixels, of the user&#39;s primary screen.
void scheduleGL(const std::tr1::function< void()> &functor, ZPos z)
(Experimental) Schedules a custom GL functor to be executed at a certain Z level. ...
void end()
Every call to begin must have a matching call to end.
double angle(double fromX, double fromY, double toX, double toY, double def=0)
Returns the angle from point 1 to point 2 in degrees, where 0.0 means upwards.
Transform rotate(double angle, double aroundX=0, double aroundY=0)
void endClipping()
Disables clipping.
void drawQuad(double x1, double y1, Color c1, double x2, double y2, Color c2, double x3, double y3, Color c3, double x4, double y4, Color c4, ZPos z, AlphaMode mode=amDefault)
Contains general typedefs and enums related to graphics.