updating Nolok contrib templates
[supertux.git] / lib / video / drawing_context.h
index b08bd6c..8e1dfaa 100644 (file)
 
 #include <vector>
 #include <string>
+#include <stdint.h>
 
 #include "SDL.h"
 
 #include "math/vector.h"
 #include "video/screen.h"
+#include "video/surface.h"
+#include "video/font.h"
 
-class Surface;
-class Font;
-
-// some constants for predefined layer values
-enum {
-  LAYER_BACKGROUND0 = -300,
-  LAYER_BACKGROUND1 = -200,
-  LAYER_BACKGROUNDTILES = -100,
-  LAYER_TILES = 0,
-  LAYER_OBJECTS = 100,
-  LAYER_FOREGROUNDTILES = 200,
-  LAYER_FOREGROUND0 = 300,
-  LAYER_FOREGROUND1 = 400,
-  LAYER_GUI         = 500
-};
-
-/**
- * This class provides functions for drawing things on screen. It also
- * maintains a stack of transforms that are applied to graphics.
- */
-class DrawingContext
-{
-public:
-  DrawingContext();
-  ~DrawingContext();
-
-  /** Adds a drawing request for a surface into the request list */
-  void draw_surface(const Surface* surface, const Vector& position, int layer,
-      Uint32 drawing_effect = NONE_EFFECT);
-  /** Adds a drawing request for part of a surface */
-  void draw_surface_part(const Surface* surface, const Vector& source,
-      const Vector& size, const Vector& dest, int layer,
-      Uint32 drawing_effect = NONE_EFFECT);
-  /** draws a text */
-  void draw_text(Font* font, const std::string& text, const Vector& position,
-      int layer, Uint32 drawing_effect = NONE_EFFECT);
-  /** draws aligned text */
-  void draw_text_center(Font* font, const std::string& text,
-      const Vector& position, int layer, Uint32 drawing_effect = NONE_EFFECT);
-  /** draws a color gradient onto the whole screen */  
-  void draw_gradient(Color from, Color to, int layer);
-  /** fills a rectangle */
-  void draw_filled_rect(const Vector& topleft, const Vector& size,
-          Color color, int layer);
-  
-  /** Processes all pending drawing requests and flushes the list */
-  void do_drawing();
-
-  const Vector& get_translation() const
-  { return transform.translation; }
-  void set_translation(const Vector& newtranslation)
-  { transform.translation = newtranslation; }
-
-  void push_transform();
-  void pop_transform();
-
-  /** apply that effect in the next draws (effects are listed on surface.h) */
-  void set_drawing_effect(int effect);
-
-private:
-  class Transform
+namespace SuperTux
   {
-  public:
-    Vector translation; // only translation for now...
 
-    Vector apply(const Vector& v) const
-    {
-      return v - translation;
-    }
-
-    int draw_effect;
-  };
-
-  /// the transform stack
-  std::vector<Transform> transformstack;
-  /// the currently active transform
-  Transform transform;
-
-  enum RequestType
-  {
-    SURFACE, SURFACE_PART, TEXT, GRADIENT, FILLRECT
-  };
-
-  struct SurfacePartRequest
-  {
-    const Surface* surface;
-    Vector source, size;
-  };
-
-  struct TextRequest
-  {
-    Font* font;
-    std::string text;
+  class Surface;
+
+  // some constants for predefined layer values
+  enum {
+    LAYER_BACKGROUND0 = -300,
+    LAYER_BACKGROUND1 = -200,
+    LAYER_BACKGROUNDTILES = -100,
+    LAYER_TILES = 0,
+    LAYER_OBJECTS = 100,
+    LAYER_FOREGROUNDTILES = 200,
+    LAYER_FOREGROUND0 = 300,
+    LAYER_FOREGROUND1 = 400,
+    LAYER_GUI         = 500
   };
 
-  struct GradientRequest
-  {
-    Color top, bottom;
-    Vector size;
-  };
-
-  struct FillRectRequest
-  {
-    Color color;
-    Vector size;
-  };
-
-  struct DrawingRequest
-  {
-    int layer;
-    Uint32 drawing_effect;
-
-    RequestType type;
-    Vector pos;
-
-    void* request_data;
-
-    bool operator<(const DrawingRequest& other) const
+  /// Handles drawing of things.
+  /**
+   * This class provides functions for drawing things on screen. It also
+   * maintains a stack of transforms that are applied to graphics.
+   */
+  class DrawingContext
     {
-      return layer < other.layer;
-    }
-  };
-
-  void draw_surface_part(DrawingRequest& request);
-  void draw_text(DrawingRequest& request);
-  void draw_gradient(DrawingRequest& request);
-  void draw_filled_rect(DrawingRequest& request);
-  
-  typedef std::vector<DrawingRequest> DrawingRequests;
-  DrawingRequests drawingrequests;
-};
+    public:
+      DrawingContext();
+      ~DrawingContext();
+
+      /// Adds a drawing request for a surface into the request list.
+      void draw_surface(const Surface* surface, const Vector& position,
+          int layer, uint32_t drawing_effect = NONE_EFFECT);
+      /// Adds a drawing request for part of a surface.
+      void draw_surface_part(const Surface* surface, const Vector& source,
+                             const Vector& size, const Vector& dest, int layer,
+                             uint32_t drawing_effect = NONE_EFFECT);
+      /// Draws a text.
+      void draw_text(const Font* font, const std::string& text,
+          const Vector& position, FontAlignment alignment, int layer,
+          uint32_t drawing_effect = NONE_EFFECT);
+
+      /// Draws text on screen center (feed Vector.x with a 0).
+      /// This is the same as draw_text() with a SCREEN_WIDTH/2 position and
+      /// alignment set to LEFT_ALLIGN
+      void draw_center_text(const Font* font, const std::string& text,
+                           const Vector& position, int layer,
+                           uint32_t drawing_effect = NONE_EFFECT);
+      /// Draws a color gradient onto the whole screen */
+      void draw_gradient(Color from, Color to, int layer);
+      /// Fills a rectangle.
+      void draw_filled_rect(const Vector& topleft, const Vector& size,
+                            Color color, int layer);
+
+      /// Processes all pending drawing requests and flushes the list.
+      void do_drawing();
+
+      const Vector& get_translation() const
+      {  return transform.translation;  }
+      uint32_t get_drawing_effect() const
+      {  return transform.drawing_effect;  }
+
+      void set_translation(const Vector& newtranslation)
+        {  transform.translation = newtranslation;  }
+
+      void push_transform();
+      void pop_transform();
+
+      /// Apply that effect in the next draws (effects are listed on surface.h).
+      void set_drawing_effect(int effect);
+      /// apply that zoom in the next draws */
+      void set_zooming(float zoom);
+      /// apply that alpha in the next draws */
+      void set_alpha(int alpha);
+
+    private:
+      class Transform
+      {
+      public:
+        Vector translation;
+        uint32_t drawing_effect;
+        float zoom;
+        int alpha;
+
+        Transform()
+          : drawing_effect(NONE_EFFECT), zoom(1), alpha(255)
+        { }
+
+        Vector apply(const Vector& v) const
+        {
+          return v - translation;
+        }
+      };
+
+      /// the transform stack
+      std::vector<Transform> transformstack;
+      /// the currently active transform
+      Transform transform;
+
+      enum RequestType
+      {
+        SURFACE, SURFACE_PART, TEXT, GRADIENT, FILLRECT
+      };
+
+      struct SurfacePartRequest
+        {
+          const Surface* surface;
+          Vector source, size;
+        };
+
+      struct TextRequest
+        {
+          const Font* font;
+          std::string text;
+          FontAlignment alignment;
+        };
+
+      struct GradientRequest
+        {
+          Color top, bottom;
+          Vector size;
+        };
+
+      struct FillRectRequest
+        {
+          Color color;
+          Vector size;
+        };
+
+      struct DrawingRequest
+        {
+          RequestType type;
+          Vector pos;                
+          
+          int layer;
+          uint32_t drawing_effect;
+          float zoom;
+          int alpha;
+
+          void* request_data;
+
+          bool operator<(const DrawingRequest& other) const
+            {
+              return layer < other.layer;
+            }
+        };
+
+      void draw_surface_part(DrawingRequest& request);
+      void draw_text(DrawingRequest& request);
+      void draw_text_center(DrawingRequest& request);
+      void draw_gradient(DrawingRequest& request);
+      void draw_filled_rect(DrawingRequest& request);
+
+      typedef std::vector<DrawingRequest> DrawingRequests;
+      DrawingRequests drawingrequests;
+    };
+
+} //namespace SuperTux
 
 #endif /*SUPERTUX_DRAWINGCONTEXT_H*/