RenderQueue.h
Go to the documentation of this file.
1 /****
2  * RenderQueue.h
3  *
4  * Copyright 2021 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the Sming-Graphics Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  * @author: May 2021 - mikee47 <mike@sillyhouse.net>
19  *
20  ****/
21 
22 #pragma once
23 
24 #include "Surface.h"
25 #include "Renderer.h"
26 
27 namespace Graphics
28 {
34 class RenderQueue : private MultiRenderer
35 {
36 public:
38 
54  RenderQueue(RenderTarget& target, uint8_t surfaceCount = 2, size_t bufferSize = 0)
55  : MultiRenderer(Location{}), target(target)
56  {
57  while(surfaceCount-- != 0) {
58  surfaces.add(target.createSurface(bufferSize));
59  }
60  }
61 
69  template <typename T>
70  void render(T* object, const Location& location, typename T::Callback callback = nullptr, uint16_t delayMs = 0)
71  {
72  renderObject(object, location, *reinterpret_cast<Completed*>(&callback), delayMs);
73  }
74 
75  template <typename T> void render(T* object, typename T::Callback callback = nullptr, uint16_t delayMs = 0)
76  {
77  renderObject(object, {target.getSize()}, *reinterpret_cast<Completed*>(&callback), delayMs);
78  }
79 
80 private:
81  void renderObject(Object* object, const Location& location, Completed callback, uint16_t delayMs);
82  void renderDone(const Object* object) override;
83  const Object* getNextObject() override;
84 
85  // A queued object plus callback information
86  class Item : public LinkedObjectTemplate<Item>
87  {
88  public:
90 
91  Item(const Object& object, const Location& location, Completed callback, uint16_t delayMs)
92  : object(object), location(location), callback(callback), delayMs(delayMs)
93  {
94  }
95 
96  const Object& object;
98  Completed callback;
99  uint16_t delayMs;
100  };
101 
102  void run();
103 
104  RenderTarget& target;
105  Item::OwnedList queue;
106  std::unique_ptr<Item> item;
107  Surface::OwnedList surfaces;
108  Surface::OwnedList active;
109  bool done{false};
110 };
111 
112 } // namespace Graphics
Top-level manager to queue objects for rendering to a specific target.
Definition: RenderQueue.h:34
Base class to render multiple objects.
Definition: Renderer.h:147
void render(T *object, typename T::Callback callback=nullptr, uint16_t delayMs=0)
Definition: RenderQueue.h:75
void render(T *object, const Location &location, typename T::Callback callback=nullptr, uint16_t delayMs=0)
Add object to the render queue and start rendering if it isn&#39;t already.
Definition: RenderQueue.h:70
bool add(ObjectType *object)
Definition: LinkedObjectList.h:134
Identifies position within bounding rectangle.
Definition: Libraries/Graphics/src/include/Graphics/Types.h:683
RenderQueue(RenderTarget &target, uint8_t surfaceCount=2, size_t bufferSize=0)
Constructor.
Definition: RenderQueue.h:54
A drawable object inherits from this virtual base class.
Definition: Libraries/Graphics/src/include/Graphics/Object.h:96
Location location
Definition: Libraries/Graphics/src/include/Graphics/Object.h:90
Base class template for linked items with type casting.
Definition: LinkedObject.h:61
Class template for singly-linked list of objects.
Definition: LinkedObjectList.h:174
virtual Size getSize() const =0
Get target dimensions.
Interface for objects which support writing via surfaces.
Definition: Libraries/Graphics/src/include/Graphics/Object.h:739
std::function< void(size_t current)> Callback
Callback function type.
Definition: malloc_count.h:77
Definition: Virtual.h:30
virtual Surface * createSurface(size_t bufferSize=0)=0
Create a surface for use with this render target.