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:
37  using Completed = Delegate<void(Object* object)>;
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  bool isActive() const
81  {
82  return !queue.isEmpty();
83  }
84 
85 private:
86  void renderObject(Object* object, const Location& location, Completed callback, uint16_t delayMs);
87  void renderDone(const Object* object) override;
88  const Object* getNextObject() override;
89 
90  // A queued object plus callback information
91  class Item : public LinkedObjectTemplate<Item>
92  {
93  public:
95 
96  Item(const Object& object, const Location& location, Completed callback, uint16_t delayMs)
97  : object(object), location(location), callback(callback), delayMs(delayMs)
98  {
99  }
100 
101  const Object& object;
103  Completed callback;
104  uint16_t delayMs;
105  };
106 
107  void run();
108 
109  RenderTarget& target;
110  Item::OwnedList queue;
111  std::unique_ptr<Item> item;
112  Surface::OwnedList surfaces;
114  bool done{false};
115 };
116 
117 } // namespace Graphics
Base class template for linked items with type casting.
Definition: LinkedObject.h:61
Delegate< void(Object *object)> Completed
Definition: RenderQueue.h:75
virtual Size getSize() const =0
Get target dimensions.
Class template for singly-linked list of objects.
Definition: LinkedObjectList.h:174
bool add(ObjectType *object)
Definition: LinkedObjectList.h:134
bool isActive() const
Definition: RenderQueue.h:118
Identifies position within bounding rectangle.
Definition: Libraries/Graphics/src/include/Graphics/Types.h:702
RenderQueue(RenderTarget &target, uint8_t surfaceCount=2, size_t bufferSize=0)
Constructor.
Definition: RenderQueue.h:92
virtual Surface * createSurface(size_t bufferSize=0)=0
Create a surface for use with this render target.
Definition: Virtual.h:30
A drawable object inherits from this virtual base class.
Definition: Libraries/Graphics/src/include/Graphics/Object.h:115
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't already.
Definition: RenderQueue.h:108
std::function< void(size_t current)> Callback
Callback function type.
Definition: malloc_count.h:103
bool isEmpty() const
Definition: LinkedObjectList.h:88
Interface for objects which support writing via surfaces.
Definition: Libraries/Graphics/src/include/Graphics/Object.h:759
Definition: Delegate.h:20
Location location
Definition: Libraries/Graphics/src/include/Graphics/Object.h:109