2 Copyright 2012 Marten Richter
\r
4 This file is part of VOMP.
\r
6 VOMP is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 VOMP is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with VOMP; if not, write to the Free Software
\r
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
\r
21 #include "osdvector.h"
\r
22 #include "surfacevector.h"
\r
24 OsdVector::OsdVector()
\r
26 setlocale(LC_CTYPE,"C.UTF-8");
\r
29 OsdVector::~OsdVector()
\r
35 int OsdVector::getFD()
\r
40 void OsdVector::screenShot(const char* fileName)
\r
45 Surface * OsdVector::createNewSurface()
\r
47 return new SurfaceVector(this);
\r
50 void OsdVector::BeginPainting()
\r
52 surfaces_mutex.Lock();
\r
54 void OsdVector::EndPainting()
\r
56 surfaces_mutex.Unlock();
\r
59 void OsdVector::Blank()
\r
61 // do nothing? remove this one?
\r
64 void OsdVector::drawSurfaces()
\r
66 surfaces_mutex.Lock();
\r
67 list<SurfaceCommands*> todraw; //First figure out if a surfaces is below another surface
\r
68 list<SurfaceCommands>::iterator itty1=scommands.begin();
\r
69 while (itty1!=scommands.end()) {
\r
70 list<SurfaceCommands>::iterator itty2=itty1;
\r
73 while (itty2!=scommands.end()) {
\r
74 SurfaceCommands & ref1=*itty1;
\r
75 SurfaceCommands & ref2=*itty2;
\r
76 if (ref1.x>=ref2.x && ref1.y>=ref2.y
\r
77 && (ref1.x+ref1.w) <= (ref2.x+ref2.w)
\r
78 && (ref1.y+ref1.h) <= (ref2.y+ref2.h) ) {
\r
84 if (!hidden) { // we are not hidden, perfect
\r
85 todraw.push_back(&(*itty1));
\r
89 //Now go through all surfaces and draw them
\r
90 list<SurfaceCommands*>::iterator curdraw=todraw.begin();
\r
91 while (curdraw!=todraw.end()) {
\r
92 drawSetTrans(*(*curdraw));
\r
93 list<SVGCommand>::iterator commands=(*(*curdraw)).commands.begin();
\r
94 list<SVGCommand>::iterator end=(*(*curdraw)).commands.end();
\r
95 while (commands!=end) {
\r
96 executeDrawCommand(*commands);
\r
102 surfaces_mutex.Unlock();
\r
106 void OsdVector::updateOrAddSurface(const SurfaceVector *surf,float x,float y,float height,float width,
\r
107 list<SVGCommand>& commands)
\r
109 surfaces_mutex.Lock();
\r
110 //First determine it is already in our system
\r
111 list<SurfaceCommands>::iterator itty=scommands.begin();
\r
112 while (itty!=scommands.end()) {
\r
113 if ((*itty).surf==surf) {
\r
114 //decrease the references
\r
115 dereferenceSVGCommand((*itty).commands);
\r
120 // if not insert it
\r
121 if (itty==scommands.end()) {
\r
122 SurfaceCommands new_sc;
\r
128 itty=scommands.insert(itty,new_sc);
\r
130 // then clear and copy
\r
131 (*itty).commands.clear();
\r
132 (*itty).commands=commands;
\r
133 //increase the references
\r
134 list<SVGCommand>::iterator sitty=(*itty).commands.begin();
\r
135 while (sitty!=(*itty).commands.end())
\r
137 incStyleRef((*sitty).getRef());
\r
138 ImageIndex ii=(*sitty).getImageIndex();
\r
139 if (ii) incImageRef(ii);
\r
142 cleanupOrphanedRefs();
\r
144 surfaces_mutex.Unlock();
\r
147 void OsdVector::removeSurface(const SurfaceVector *surf)
\r
149 surfaces_mutex.Lock();
\r
150 //First determine it is already in our system
\r
151 list<SurfaceCommands>::iterator itty=scommands.begin();
\r
152 while (itty!=scommands.end()) {
\r
153 if ((*itty).surf==surf) {
\r
154 dereferenceSVGCommand((*itty).commands);
\r
155 (*itty).commands.clear();
\r
156 scommands.erase(itty);
\r
161 surfaces_mutex.Unlock();
\r
165 void OsdVector::dereferenceSVGCommand(list<SVGCommand>& commands )
\r
168 list<SVGCommand>::iterator sitty = commands.begin();
\r
169 while (sitty != commands.end()) {
\r
170 removeStyleRef((*sitty).getRef());
\r
171 ImageIndex ii = (*sitty).getImageIndex();
\r
172 if (ii) removeImageRef(ii);
\r
177 void OsdVector::referenceSVGCommand(list<SVGCommand>& commands )
\r
179 list<SVGCommand>::iterator sitty=commands.begin();
\r
180 while (sitty!=commands.end())
\r
182 incStyleRef((*sitty).getRef());
\r
183 ImageIndex ii=(*sitty).getImageIndex();
\r
184 if (ii) incImageRef(ii);
\r
189 void OsdVector::incImageRef(ImageIndex index)
\r
191 if (images_ref.find(index)==images_ref.end()) {
\r
192 images_ref[index]=1;
\r
194 images_ref[index]++;
\r
198 void OsdVector::removeImageRef(const ImageIndex ref)
\r
203 void OsdVector::cleanupOrphanedRefs()
\r
204 { // Do some garbage collection
\r
205 map<ImageIndex,unsigned int>::iterator iitty=images_ref.begin();
\r
206 while (iitty!=images_ref.end()) {
\r
207 int count=(*iitty).second;
\r
209 ImageIndex ref=(*iitty).first;
\r
210 images_ref.erase(iitty++);
\r
211 destroyImageRef(ref);
\r
214 map<unsigned int,unsigned int>::iterator sitty=styles_ref.begin();
\r
215 while (sitty!=styles_ref.end()) {
\r
216 int count=(*sitty).second;
\r
218 unsigned int ref=(*sitty).first;
\r
219 styles_ref.erase(sitty++);
\r
220 destroyStyleRef(ref);
\r
227 unsigned int OsdVector::getImageRef(ImageIndex index)
\r
229 if (images_ref.find(index)==images_ref.end()) {
\r
232 return images_ref[index];
\r
236 void OsdVector::incStyleRef(unsigned int index)
\r
238 if (styles_ref.find(index)==styles_ref.end()) {
\r
239 styles_ref[index]=1;
\r
241 styles_ref[index]++;
\r
245 void OsdVector::removeStyleRef(unsigned int index)
\r
247 styles_ref[index]--;
\r
250 unsigned int OsdVector::getStyleRef(const DrawStyle &c)
\r
252 unsigned int style_handle=0;
\r
253 if (styles.find(pair<Colour*,unsigned int>((Colour*)&c,c.rgba()))==styles.end())
\r
255 style_handle=styles[pair<Colour*,unsigned int>((Colour*)&c,c.rgba())]=createStyleRef(c);
\r
257 style_handle=styles[pair<Colour*,unsigned int>((Colour*)&c,c.rgba())];
\r
258 //Now check if the handle is valid
\r
259 if (styles_ref.find(style_handle)==styles_ref.end()) {
\r
260 //invalid handle recreate
\r
261 style_handle=styles[pair<Colour*,unsigned int>((Colour*)&c,c.rgba())]=createStyleRef(c);
\r
264 incStyleRef(style_handle);
\r
265 return style_handle;
\r
268 unsigned int OsdVector::getColorRef(const Colour &c)
\r
270 unsigned int style_handle=0;
\r
271 if (styles.find(pair<Colour*,unsigned int>((Colour*)&c,c.rgba()))==styles.end())
\r
273 style_handle=styles[pair<Colour*,unsigned int>((Colour*)&c,c.rgba())]=createColorRef(c);
\r
275 style_handle=styles[pair<Colour*,unsigned int>((Colour*)&c,c.rgba())];
\r
276 if (styles_ref.find(style_handle)==styles_ref.end()) {
\r
277 //invalid handle recreate
\r
278 style_handle=styles[pair<Colour*,unsigned int>((Colour*)&c,c.rgba())]=createColorRef(c);
\r
281 incStyleRef(style_handle);
\r
282 return style_handle;
\r
285 unsigned int OsdVector::getStyleRef(unsigned int index)
\r
287 if (styles_ref.find(index)==styles_ref.end()) {
\r
290 return styles_ref[index];
\r
294 ImageIndex OsdVector::getJpegRef(const char* fileName, int *width,int *height)
\r
296 ImageIndex image_handle=0;
\r
297 if (jpegs.find(fileName)==jpegs.end())
\r
299 image_handle=jpegs[fileName]=createJpeg(fileName,width,height);
\r
301 image_handle=jpegs[fileName];
\r
304 if (images_ref.find(image_handle)==images_ref.end()) {
\r
305 //invalid handle recreate
\r
306 image_handle=jpegs[fileName]=createJpeg(fileName,width,height);
\r
309 incImageRef(image_handle);
\r
310 return image_handle;
\r
313 ImageIndex OsdVector::getMonoBitmapRef(void *base,int width,int height)
\r
315 ImageIndex image_handle;
\r
316 if (monobitmaps.find(base)==monobitmaps.end())
\r
318 image_handle=monobitmaps[base]=createMonoBitmap(base,width,height);
\r
320 image_handle=monobitmaps[base];
\r
321 if (images_ref.find(image_handle)==images_ref.end()) {
\r
322 //invalid handle recreate
\r
323 image_handle=monobitmaps[base]=createMonoBitmap(base,width,height);
\r
326 incImageRef(image_handle);
\r
327 return image_handle;
\r
330 ImageIndex OsdVector::getImageRGBA(int width,int height)
\r
332 ImageIndex image_handle;
\r
333 image_handle=createImageRGBA(width,height);
\r
334 incImageRef(image_handle);
\r
335 return image_handle;
\r