Otclient  14/8/2020
coordsbuffer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "coordsbuffer.h"
24 #include "graphics.h"
25 
27 {
28  m_hardwareCacheMode = HardwareBuffer::DynamicDraw;
29  m_hardwareVertexArray = nullptr;
30  m_hardwareTextureCoordArray = nullptr;
31  m_hardwareCached = false;
32  m_hardwareCaching = false;
33 }
34 
36 {
37  delete m_hardwareVertexArray;
38  delete m_hardwareTextureCoordArray;
39 }
40 
41 void CoordsBuffer::addBoudingRect(const Rect& dest, int innerLineWidth)
42 {
43  int left = dest.left();
44  int right = dest.right();
45  int top = dest.top();
46  int bottom = dest.bottom();
47  int width = dest.width();
48  int height = dest.height();
49  int w = innerLineWidth;
50 
51  addRect(Rect(left, top, width - w, w)); // top
52  addRect(Rect(right - w + 1, top, w, height - w)); // right
53  addRect(Rect(left + w, bottom - w + 1, width - w, w)); // bottom
54  addRect(Rect(left, top + w, w, height - w)); // left
55 }
56 
57 void CoordsBuffer::addRepeatedRects(const Rect& dest, const Rect& src)
58 {
59  if(dest.isEmpty() || src.isEmpty())
60  return;
61 
62  Rect virtualDest(0, 0, dest.size());
63  for(int y = 0; y <= virtualDest.height(); y += src.height()) {
64  for(int x = 0; x <= virtualDest.width(); x += src.width()) {
65  Rect partialDest(x, y, src.size());
66  Rect partialSrc(src);
67 
68  // partialCoords to screenCoords bottomRight
69  if(partialDest.bottom() > virtualDest.bottom()) {
70  partialSrc.setBottom(partialSrc.bottom() + (virtualDest.bottom() - partialDest.bottom()));
71  partialDest.setBottom(virtualDest.bottom());
72  }
73  if(partialDest.right() > virtualDest.right()) {
74  partialSrc.setRight(partialSrc.right() + (virtualDest.right() - partialDest.right()));
75  partialDest.setRight(virtualDest.right());
76  }
77 
78  partialDest.translate(dest.topLeft());
79  m_vertexArray.addRect(partialDest);
80  m_textureCoordArray.addRect(partialSrc);
81  }
82  }
83  m_hardwareCached = false;
84 }
85 
87 {
89  return;
90 
91  m_hardwareCacheMode = usagePattern;
92  m_hardwareCaching = true;
93  m_hardwareCached = false;
94 }
95 
97 {
98  if(!m_hardwareCaching || m_hardwareCached)
99  return;
100 
101  int vertexCount = m_vertexArray.vertexCount();
102 
103  // there is only performance improvement when caching a lot of vertices
104  if(vertexCount < CACHE_MIN_VERTICES_COUNT)
105  return;
106 
107  if(vertexCount > 0) {
108  if(!m_hardwareVertexArray && m_vertexArray.vertexCount() > 0)
109  m_hardwareVertexArray = new HardwareBuffer(HardwareBuffer::VertexBuffer);
110  m_hardwareVertexArray->bind();
111  m_hardwareVertexArray->write((void*)m_vertexArray.vertices(), m_vertexArray.size() * sizeof(float), m_hardwareCacheMode);
112  }
113 
114  if(m_textureCoordArray.vertexCount() > 0) {
115  if(!m_hardwareTextureCoordArray && m_textureCoordArray.vertexCount() > 0)
116  m_hardwareTextureCoordArray = new HardwareBuffer(HardwareBuffer::VertexBuffer);
117  m_hardwareTextureCoordArray->bind();
118  m_hardwareTextureCoordArray->write((void*)m_textureCoordArray.vertices(), m_textureCoordArray.size() * sizeof(float), m_hardwareCacheMode);
119  }
120 
121  m_hardwareCached = true;
122 }
HardwareBuffer::DynamicDraw
@ DynamicDraw
Definition: hardwarebuffer.h:40
CoordsBuffer::addRepeatedRects
void addRepeatedRects(const Rect &dest, const Rect &src)
Definition: coordsbuffer.cpp:57
graphics.h
HardwareBuffer::bind
void bind()
Definition: hardwarebuffer.h:46
TRect< int >
TRect::left
T left() const
Definition: rect.h:52
coordsbuffer.h
TRect::isEmpty
bool isEmpty() const
Definition: rect.h:49
HardwareBuffer::write
void write(void *data, int count, UsagePattern usage)
Definition: hardwarebuffer.h:48
TRect::bottom
T bottom() const
Definition: rect.h:55
HardwareBuffer::VertexBuffer
@ VertexBuffer
Definition: hardwarebuffer.h:33
CoordsBuffer::enableHardwareCaching
void enableHardwareCaching(HardwareBuffer::UsagePattern usagePattern=HardwareBuffer::DynamicDraw)
Definition: coordsbuffer.cpp:86
CoordsBuffer::~CoordsBuffer
~CoordsBuffer()
Definition: coordsbuffer.cpp:35
HardwareBuffer::UsagePattern
UsagePattern
Definition: hardwarebuffer.h:37
TRect::setRight
void setRight(T pos)
Definition: rect.h:77
HardwareBuffer
Definition: hardwarebuffer.h:29
TRect::topLeft
TPoint< T > topLeft() const
Definition: rect.h:60
VertexArray::vertexCount
int vertexCount() const
Definition: vertexarray.h:78
CoordsBuffer::updateCaches
void updateCaches()
Definition: coordsbuffer.cpp:96
Graphics::canUseHardwareBuffers
bool canUseHardwareBuffers()
Definition: graphics.cpp:314
g_graphics
Graphics g_graphics
Definition: graphics.cpp:44
CoordsBuffer::addBoudingRect
void addBoudingRect(const Rect &dest, int innerLineWidth)
Definition: coordsbuffer.cpp:41
Rect
TRect< int > Rect
Definition: rect.h:319
VertexArray::size
int size() const
Definition: vertexarray.h:79
VertexArray::addRect
void addRect(const Rect &rect)
Definition: vertexarray.h:38
TRect::right
T right() const
Definition: rect.h:54
CoordsBuffer::CoordsBuffer
CoordsBuffer()
Definition: coordsbuffer.cpp:26
VertexArray::vertices
float * vertices() const
Definition: vertexarray.h:77
CoordsBuffer::addRect
void addRect(const Rect &dest)
Definition: coordsbuffer.h:48
TRect::height
T height() const
Definition: rect.h:70
TRect::top
T top() const
Definition: rect.h:53
TRect::size
TSize< T > size() const
Definition: rect.h:71
TRect::width
T width() const
Definition: rect.h:69
TRect::translate
void translate(T x, T y)
Definition: rect.h:98
TRect::setBottom
void setBottom(T pos)
Definition: rect.h:78