Tiny graphics library for STM32F746G-DISCO board

Dependents:   RadarDemo 3DDemo RadarDemoT

Canvas.h

Committer:
karpent
Date:
2016-11-11
Revision:
3:1ddc4aa1e5cb
Parent:
2:02b7b78e8510

File content as of revision 3:1ddc4aa1e5cb:

/*
    Canvas.h - Simple canvas class declaration

    Copyright(c) 2016 karpent at gmail.com, MIT License

    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"),
    to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and / or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :

    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
    OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
    THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#pragma once

#include "Commons.h"
#include "Display.h"
#include "GrFont.h"

#define PLANE_BITMAP_ELEMENT_BITS 8

/**
*  @brief Canvas is a frame buffer where everything is drawn,
*  uses its own graphics commands to create graphics.
*  Cavas bitmap must be copied to the display to make image visible.
*/
class Canvas : public Display
{
public:

    /// <summary>
    /// Initializes a new instance of the <see cref="Canvas"/> class.
    /// </summary>
    /// Class constructor
    Canvas();

    /// <summary>
    /// Initializes a new instance of the <see cref="Canvas"/> class.
    /// </summary>
    /// <param name="width">The width.</param>
    /// <param name="height">The height.</param>
    Canvas(uint16_t width, uint16_t height);

    /// <summary>
    /// Finalizes an instance of the <see cref="Canvas"/> class.
    /// </summary>
    ~Canvas();

    /// <summary>
    /// Clears the canvas.
    /// </summary>
    void virtual Clear();

    /// <summary>
    /// Gets the bitmap.
    /// </summary>
    /// <returns></returns>
    uint8_t* GetBitmap();

    /// <summary>
    /// Returns screen width.
    /// </summary>
    /// <returns></returns>
    uint16_t virtual DisplayWidth();

    /// <summary>
    /// Redurns screen height.
    /// </summary>
    /// <returns></returns>
    uint16_t virtual DisplayHeight();
    
    /// <summary>
    /// Sets the color of the draw.
    /// </summary>
    /// <param name="red">The red.</param>
    /// <param name="green">The green.</param>
    /// <param name="blue">The blue.</param>
    /// <param name="alpha">The alpha.</param>
    void virtual SetDrawColor(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha);
    
    /// <summary>
    /// Gets the color of the draw.
    /// </summary>
    /// <returns>Draw color value</returns>
    uint32_t virtual GetDrawColor();
    
    /// <summary>
    /// Sets the clear color.
    /// </summary>
    /// <param name="red">The red.</param>
    /// <param name="green">The green.</param>
    /// <param name="blue">The blue.</param>
    /// <param name="alpha">The alpha.</param>
    void virtual SetClearColor(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha);
    
    /// <summary>
    /// Gets the clear color.
    /// </summary>
    /// <returns>Clear color value</returns>
    uint32_t virtual GetClearColor();

    /// <summary>
    /// Draws the point.
    /// </summary>
    /// <param name="posX">The position x.</param>
    /// <param name="posY">The position y.</param>
    /// <param name="colorMask">The color mask.</param>
    void virtual DrawPoint(int posX, int posY, uint32_t colorMask);

    /// <summary>
    /// Sets the size.
    /// </summary>
    /// <param name="width">The width.</param>
    /// <param name="height">The height.</param>
    /// <returns></returns>
    bool SetSize(uint16_t width, uint16_t height);

    /// <summary>
    /// Determines whether this instance is set.
    /// </summary>
    /// <returns>
    ///   <c>true</c> if this instance is set; otherwise, <c>false</c>.
    /// </returns>
    bool IsSet();
    
    /// <summary>
    /// Draws single character using actual font type and size.
    /// </summary>
    /// <param name="posX">The position x.</param>
    /// <param name="posY">The position y.</param>
    /// <param name="ch">The character.</param>
    void DrawChar(int posX, int posY, char ch);
    
    /// <summary>
    /// Draws the text using actual font type and size.
    /// </summary>
    /// <param name="posX">The position x.</param>
    /// <param name="posY">The position y.</param>
    /// <param name="str">The string.</param>
    void virtual DrawText(int posX, int posY, char * str);

private:
    /// <summary>
    /// Resets canvas properties.
    /// </summary>
    void Reset();

    uint16_t _width;
    uint32_t _height;

    /*
    *  Canvas plane bitmap - depth 1.
    */
    uint8_t *_planeBitmap;
    uint32_t _planeBitmapSize;
    
    uint32_t _drawColor;
    uint32_t _clearColor;
    
    /**
    *   @brief Current fonts for layers
    */
    GrFont _selectedFont;
};