Reader.h
Go to the documentation of this file.
1 /****
2  * Drawing.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 "../Object.h"
25 #include "../Stream.h"
26 #include "Registers.h"
27 #include <FILO.h>
28 #include <WHashMap.h>
29 
30 namespace Graphics
31 {
32 class Object;
33 class DrawingObject;
34 
35 namespace Drawing
36 {
37 class Reader
38 {
39 public:
40  Reader(const DrawingObject& drawing);
41  ~Reader();
42 
43  Object* readObject();
44 
45 private:
46  struct DrawState {
47  Registers reg;
48 
49  void reset()
50  {
51  reg = Registers{};
52  }
53  };
54 
55  struct StackEntry {
56  uint32_t returnOffset;
57  std::unique_ptr<DrawState> state;
58  };
59 
60  using SubroutineMap = HashMap<AssetID, uint32_t>;
61 
62  const Asset* findAsset(uint16_t id) const;
63 
64  template <typename T> const T* findAsset(uint16_t id) const
65  {
66  auto asset = assets.find<T>(id);
67  if(asset == nullptr) {
68  asset = drawing.assets.find<T>(id);
69  }
70  return asset;
71  }
72 
73  Pen getPen() const
74  {
75  auto id = state.reg.penId;
76  if(id != 0) {
77  auto asset = findAsset<PenAsset>(id);
78  if(asset != nullptr) {
79  return *asset;
80  }
81  }
82  return Pen(state.reg.penColor, state.reg.penWidth);
83  }
84 
85  Brush getBrush() const
86  {
87  auto id = state.reg.brushId;
88  if(id != 0) {
89  auto asset = findAsset(id);
90  if(asset != nullptr) {
91  switch(asset->type()) {
92  case AssetType::Pen:
93  return *reinterpret_cast<const PenAsset*>(asset);
94  case AssetType::SolidBrush:
95  return reinterpret_cast<const SolidBrush*>(asset)->color;
96  case AssetType::TextureBrush:
97  return reinterpret_cast<const TextureBrush*>(asset);
98  default:
99  debug_e("[DRAW] Asset #%u is %s, not compatible with Brush", id, ::toString(asset->type()).c_str());
100  return Pen(state.reg.penColor, state.reg.penWidth);
101  }
102  }
103  }
104  return Brush(state.reg.brushColor);
105  }
106 
107  bool read(void* buffer, uint8_t count)
108  {
109  uint8_t len = cache.read(streamPos, buffer, count);
110  streamPos += len;
111  return len == count;
112  }
113 
114  void seek(uint32_t offset)
115  {
116  streamPos = offset;
117  }
118 
119  const DrawingObject& drawing;
120  SubroutineMap subroutines;
121  AssetList assets;
122  FILO<StackEntry*, 16> stack;
123  StackEntry root{};
124  StackEntry* sub{nullptr};
125  DrawState state;
126  uint32_t streamPos{0};
127  ReadStream cache;
128  bool definingSubroutine{false};
129 };
130 
131 } // namespace Drawing
132 } // namespace Graphics
size_t read(uint32_t offset, void *buffer, size_t count)
Definition: Virtual.h:30
#define debug_e(fmt,...)
Definition: debug_progmem.h:77
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:616
Asset * find(AssetID id)
Definition: Asset.h:774
Reader(const DrawingObject &drawing)
AssetList assets
Definition: Libraries/Graphics/src/include/Graphics/Object.h:1228
String toString(OpCode opcode)
An asset is used to render an Object, but is not itself drawable.
Definition: Asset.h:94