libAlgAudio  v1.99-440-g08538e5-dirty
The development library for AlgAudio framework.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AlgAudio::DrawContext Class Reference

#include <DrawContext.hpp>

Public Member Functions

void Clear ()
 
void Clear (Color)
 
 DrawContext ()
 
 DrawContext (SDL_Window *w, SDL_Renderer *renderer, SDL_GLContext cont, int x, int y, int width, int height)
 
void DrawCubicBezier (Point2D p1, Point2D p2, Point2D p3, Point2D p4, float width, unsigned int segments=15)
 
void DrawText (std::shared_ptr< SDLTextTexture > text, Color c, Point2D p=Point2D(0, 0))
 
void DrawTexture (std::shared_ptr< SDLTexture > texture, Point2D p=Point2D(0, 0))
 
void Fill ()
 
bool HasZeroArea ()
 
void MarkApiTrace (int no)
 
void Pop ()
 
void Push (std::shared_ptr< SDLTexture >, int width, int height)
 
void ResetOffset ()
 
void Restore ()
 
void SetOffset (Point2D off)
 
void SetScale (float s)
 
Size2D Size ()
 
void SetColor (short r, short g, short b, short a=255)
 
void SetColor (const Color &)
 
void DrawLine (int x1, int y1, int x2, int y2)
 
void DrawLine (Point2D from, Point2D to, bool smooth=false)
 
void DrawLineEx (float x1, float y1, float x2, float y2, float width=1.0)
 
void DrawRect (int x, int y, int w, int h)
 
void DrawRect (Rect r)
 
void DrawRectBorder (Rect r)
 
void Push (Point2D p, Size2D s)
 
void Push (Rect r)
 

Detailed Description

A DrawContext is a universal drawing and rendering proxy. It provides a clean interface for drawing some primitives. It carries a context stack, which may be stacked on with a new clipping dimentions, or a new target texture. This way when using a context passed from a parent call, one does not need to bother about binding a correct texture, or applying a correct offset, as all operations done on DrawContext ensure all this.

The workflow with DrawContext is usually similar in all cases. Mose of the time, when performing any drawings, you do not create a new context, but are given one (e.g. UIWidget::CustomDraw()). The context you were given is bound to some texture, zoom and offset, but you should not care about it, whoever gave you that context wants you to draw your drawing there. You can check the size you have available for drawing with DrawContext::Size(). Then use operations such as SetColor, DrawLine and DrawTexture to perform your drawing. If you want someone else to fill a part of your drawing, use Push(Point2D, Size2D) to specify the new draw area, and then pass your current context to be drawn at. All subsequent draw operations will automatically have an offset applied, according to what you passed as an argument to Push. When done, call Pop to have your context return to the previous state (all drawings are kept), so you can continue drawing on the area you were originally given.

For examples on how to use a DrawContext, see the implementation of CustomDraw method in any widget.

Constructor & Destructor Documentation

AlgAudio::DrawContext::DrawContext ( )
inline
AlgAudio::DrawContext::DrawContext ( SDL_Window *  w,
SDL_Renderer *  renderer,
SDL_GLContext  cont,
int  x,
int  y,
int  width,
int  height 
)

Creates a new DrawContext bound to a particuar SDL window and renderer as well as a GLcontext, with some initial bounds size.

Member Function Documentation

void AlgAudio::DrawContext::Clear ( )

Makes the whole context area fully transparent.

void AlgAudio::DrawContext::Clear ( Color  )

Crears the whole context area to the given color.

void AlgAudio::DrawContext::DrawCubicBezier ( Point2D  p1,
Point2D  p2,
Point2D  p3,
Point2D  p4,
float  width,
unsigned int  segments = 15 
)

Draws a cubic Bezier curve defined by the given 4 points.

Parameters
p1Point 1
p2Point 2
p3Point 3
p4Point 4
widthThe line width to use.
segmentsThe number of straight lines the curve will be divided into.
void AlgAudio::DrawContext::DrawLine ( int  x1,
int  y1,
int  x2,
int  y2 
)
inline

Draws a line from a point to another.

void AlgAudio::DrawContext::DrawLine ( Point2D  from,
Point2D  to,
bool  smooth = false 
)

Draws a line from a point to another.

void AlgAudio::DrawContext::DrawLineEx ( float  x1,
float  y1,
float  x2,
float  y2,
float  width = 1.0 
)

Draws a line from a point to another.

void AlgAudio::DrawContext::DrawRect ( int  x,
int  y,
int  w,
int  h 
)

Draws a rectangle onto the context.

void AlgAudio::DrawContext::DrawRect ( Rect  r)
inline

Draws a rectangle onto the context.

void AlgAudio::DrawContext::DrawRectBorder ( Rect  r)

Draws a rectangle onto the context.

void AlgAudio::DrawContext::DrawText ( std::shared_ptr< SDLTextTexture text,
Color  c,
Point2D  p = Point2D(0, 0) 
)

Renders the given text texture onto the context at point p.

void AlgAudio::DrawContext::DrawTexture ( std::shared_ptr< SDLTexture texture,
Point2D  p = Point2D(0, 0) 
)

Renders the given texture onto the context at point p.

void AlgAudio::DrawContext::Fill ( )

Fills the whole context with current color.

bool AlgAudio::DrawContext::HasZeroArea ( )

Returns if the context is currently empty in size. In such case no draw operation will have any effect.

void AlgAudio::DrawContext::MarkApiTrace ( int  no)
void AlgAudio::DrawContext::Pop ( )

Restores one level ot the context stack upwards, reverting to previous context state.

void AlgAudio::DrawContext::Push ( Point2D  p,
Size2D  s 
)

Pushes the context stact one level down, changing the relative offset and size. Useful for drawing children widgets.

void AlgAudio::DrawContext::Push ( Rect  r)
inline

Pushes the context stact one level down, changing the relative offset and size. Useful for drawing children widgets.

void AlgAudio::DrawContext::Push ( std::shared_ptr< SDLTexture ,
int  width,
int  height 
)

Pushes the context stack one level down, changing the target texture. To change the texture to the window associated with the internal renderer, pass nullptr as the texture. This method is useful for switching targets and renderning widgets to cache.

void AlgAudio::DrawContext::ResetOffset ( )
inline

Clears the offset set by SetOffset().

void AlgAudio::DrawContext::Restore ( )

Reapplies the current context params. It is useful if your context was messed up by a different context using the same window. For example, if you use TextRenderer::Render inside a CustomDraw, then the remporary drawcontext used by TextRenderer will reset the settings carried by your Draw context, because they will end up sending the same SDL calls to the same window. Use Restore to fix the context after text rendering is completed.

void AlgAudio::DrawContext::SetColor ( short  r,
short  g,
short  b,
short  a = 255 
)

Changes the current color used for drawing.

void AlgAudio::DrawContext::SetColor ( const Color )

Changes the current color used for drawing.

void AlgAudio::DrawContext::SetOffset ( Point2D  off)
inline

Sets the offset for all draw operations on this context.

void AlgAudio::DrawContext::SetScale ( float  s)
inline

Sets the scaling factor for all draw operations on this context.

Size2D AlgAudio::DrawContext::Size ( )
inline

Returns the current dimentions of the context.


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