Revision | 887 (tree) |
---|---|
Time | 2012-02-15 11:11:12 |
Author | loopsketch |
add telop scene(rss1.0)
@@ -27,13 +27,13 @@ | ||
27 | 27 | #include "CaptureScene.h" |
28 | 28 | #include "MainScene.h" |
29 | 29 | #include "DiffDetectScene.h" |
30 | +#include "Utils.h" | |
31 | +#include "WebAPI.h" | |
32 | +#include "TelopScene.h" | |
33 | + | |
30 | 34 | #ifdef USE_OPENNI |
31 | 35 | #include "OpenNIScene.h" |
32 | 36 | #endif |
33 | -//#include "UserInterfaceScene.h" | |
34 | -#include "Utils.h" | |
35 | -#include "WebAPI.h" | |
36 | -//#include "ui/UserInterfaceManager.h" | |
37 | 37 | |
38 | 38 | //#ifndef _DEBUG |
39 | 39 | //#include <omp.h> |
@@ -239,6 +239,11 @@ | ||
239 | 239 | mainScene = new MainScene(*_renderer); |
240 | 240 | _renderer->addScene("main", mainScene); |
241 | 241 | } |
242 | + if (_conf.hasScene("telop")) { | |
243 | + TelopScenePtr telop = new TelopScene(*_renderer); | |
244 | + telop->initialize(); | |
245 | + _renderer->addScene("telop", telop); | |
246 | + } | |
242 | 247 | //if (_conf.hasScene("diff")) { |
243 | 248 | // DiffDetectScenePtr diffScene = new DiffDetectScene(*_renderer); |
244 | 249 | // diffScene->initialize(); |
@@ -0,0 +1,79 @@ | ||
1 | +#pragma once | |
2 | + | |
3 | +#include <winsock2.h> | |
4 | +#include "Scene.h" | |
5 | +#include <queue> | |
6 | +#include <Poco/Net/HTTPCookie.h> | |
7 | +#include <Poco/URI.h> | |
8 | + | |
9 | +using std::queue; | |
10 | + | |
11 | + | |
12 | +class Telop { | |
13 | +private: | |
14 | + Telop& copy(const Telop& t) { | |
15 | + if (this == &t) return *this; | |
16 | + x = t.x; | |
17 | + y = t.y; | |
18 | + w = t.w; | |
19 | + h = t.h; | |
20 | + texture = t.texture; | |
21 | + return *this; | |
22 | + } | |
23 | + | |
24 | +public: | |
25 | + int x; | |
26 | + int y; | |
27 | + int w; | |
28 | + int h; | |
29 | + LPDIRECT3DTEXTURE9 texture; | |
30 | + | |
31 | + Telop() {}; | |
32 | + //Telop(const Telop& t); | |
33 | + virtual ~Telop() {}; | |
34 | + | |
35 | + Telop& operator=(const Telop& t) { | |
36 | + return copy(t); | |
37 | + } | |
38 | +}; | |
39 | + | |
40 | + | |
41 | +class TelopScene: private Poco::Runnable, public Scene | |
42 | +{ | |
43 | +private: | |
44 | + Poco::FastMutex _lock; | |
45 | + Poco::Thread _thread; | |
46 | + Poco::Runnable* _worker; | |
47 | + | |
48 | + string _remoteURL; | |
49 | + int _validMinutes; | |
50 | + int _speed; | |
51 | + int _space; | |
52 | + vector<string> _categories; | |
53 | + bool _creating; | |
54 | + | |
55 | + queue<Telop> _prepared; | |
56 | + vector<Telop> _telops; | |
57 | + queue<Telop> _deletes; | |
58 | + | |
59 | + void run(); | |
60 | + | |
61 | + map<string, vector<string>> _readXML(const Poco::LocalDateTime& now); | |
62 | + map<string, vector<string>> readXML(const Poco::LocalDateTime& now); | |
63 | + | |
64 | + | |
65 | +public: | |
66 | + TelopScene(Renderer& renderer); | |
67 | + | |
68 | + virtual ~TelopScene(); | |
69 | + | |
70 | + virtual bool initialize(); | |
71 | + | |
72 | + virtual void process(); | |
73 | + | |
74 | + virtual void draw1(); | |
75 | + | |
76 | + virtual void draw2(); | |
77 | +}; | |
78 | + | |
79 | +typedef TelopScene* TelopScenePtr; |
@@ -0,0 +1,292 @@ | ||
1 | + | |
2 | +#include "TelopScene.h" | |
3 | +#include <Poco/DateTimeFormat.h> | |
4 | +#include <Poco/DateTimeFormatter.h> | |
5 | +#include <Poco/DateTimeParser.h> | |
6 | +#include <Poco/DOM/DOMParser.h> | |
7 | +#include <Poco/Timezone.h> | |
8 | +#include <Poco/DOM/Document.h> | |
9 | +#include <Poco/DOM/Element.h> | |
10 | +#include <Poco/DOM/NodeList.h> | |
11 | +#include <Poco/DOM/DOMWriter.h> | |
12 | +#include <Poco/XML/XMLWriter.h> | |
13 | +#include <Poco/SAX/InputSource.h> | |
14 | +#include <Poco/UnicodeConverter.h> | |
15 | +#include <sstream> | |
16 | +#include <Poco/Net/HTTPClientSession.h> | |
17 | +#include <Poco/Net/HTTPRequest.h> | |
18 | +#include <Poco/Net/HTTPResponse.h> | |
19 | +#include "Utils.h" | |
20 | + | |
21 | + | |
22 | +TelopScene::TelopScene(Renderer& renderer): Scene(renderer) { | |
23 | +} | |
24 | + | |
25 | +TelopScene::~TelopScene() { | |
26 | + _log.information("*release TelopScene"); | |
27 | + _worker = NULL; | |
28 | + _thread.join(); | |
29 | +} | |
30 | + | |
31 | +bool TelopScene::initialize() { | |
32 | + _log.information("*initialize TelopScene"); | |
33 | + _remoteURL = "http://rss.asahi.com/rss/asahi/newsheadlines.rdf"; | |
34 | + _validMinutes = 120; | |
35 | + _speed = -1; | |
36 | + _space = 200; | |
37 | + string s; | |
38 | + //Poco::UnicodeConverter::toUTF8(L"YD,T0,T1,T2,T3,T4,T5,T6,JW01", s); | |
39 | + //Poco::UnicodeConverter::toUTF8(L"社会,スポーツ,デジタル", s); | |
40 | + //svvitch::split(s, ',', _categories); // ent,int,pol,soc,eco,spo | |
41 | + _creating = false; | |
42 | + | |
43 | + _worker = this; | |
44 | + _thread.start(*_worker); | |
45 | + return true; | |
46 | +} | |
47 | + | |
48 | +void TelopScene::run() { | |
49 | + queue<string> texts; | |
50 | + int i = -1; | |
51 | + while (_worker) { | |
52 | + Poco::LocalDateTime now; | |
53 | + map<string, vector<string>> sources = readXML(now); | |
54 | + | |
55 | + for (int t = 0; _worker && t < 300; ++t) { | |
56 | + if (texts.empty()) { | |
57 | + for (map<string, vector<string>>::iterator m = sources.begin(); m != sources.end(); ++m) { | |
58 | + for (vector<string>::iterator it = m->second.begin(); it != m->second.end(); ++it) { | |
59 | + texts.push(*it); | |
60 | + } | |
61 | + _log.information(Poco::format("add source[%s] %u", m->first, m->second.size())); | |
62 | + } | |
63 | + } | |
64 | + | |
65 | + if (!texts.empty() && _prepared.empty() && _creating) { | |
66 | + _creating = false; | |
67 | + string text = texts.front(); | |
68 | + texts.pop(); | |
69 | + if (!text.empty()) { | |
70 | + LPDIRECT3DTEXTURE9 texture = _renderer.createTexturedText(L"", 24, 0xccffffff, 0x9999ccff, 2, 0xcc000000, 0, 0xff000000, text); | |
71 | + if (texture) { | |
72 | + D3DSURFACE_DESC desc; | |
73 | + HRESULT hr = texture->GetLevelDesc(0, &desc); | |
74 | + Telop telop; | |
75 | + telop.w = desc.Width; | |
76 | + telop.h = desc.Height; | |
77 | + telop.x = config().stageRect.right; | |
78 | + telop.y = config().stageRect.bottom - telop.h; | |
79 | + telop.texture = texture; | |
80 | + Poco::ScopedLock<Poco::FastMutex> lock(_lock); | |
81 | + _prepared.push(telop); | |
82 | + } | |
83 | + } | |
84 | + } else { | |
85 | + Poco::Thread::sleep(900); | |
86 | + } | |
87 | + if (!_deletes.empty()) { | |
88 | + Telop t = _deletes.front(); | |
89 | + _deletes.pop(); | |
90 | + t.texture->Release(); | |
91 | + } | |
92 | + Poco::Thread::sleep(100); | |
93 | + } | |
94 | + } | |
95 | + | |
96 | + Poco::ScopedLock<Poco::FastMutex> lock(_lock); | |
97 | + for (vector<Telop>::iterator it = _telops.begin(); it != _telops.end(); ++it) { | |
98 | + Telop& t = *it; | |
99 | + t.texture->Release(); | |
100 | + } | |
101 | + while (!_prepared.empty()) { | |
102 | + Telop t = _prepared.front(); | |
103 | + _prepared.pop(); | |
104 | + t.texture->Release(); | |
105 | + } | |
106 | + while (!_deletes.empty()) { | |
107 | + Telop t = _deletes.front(); | |
108 | + _deletes.pop(); | |
109 | + t.texture->Release(); | |
110 | + } | |
111 | +} | |
112 | + | |
113 | +map<string, vector<string>> TelopScene::readXML(const Poco::LocalDateTime& now) { | |
114 | + // rss1.0 | |
115 | + _log.information(Poco::format("check remote[%s]: %s", Poco::DateTimeFormatter::format(now, Poco::DateTimeFormat::SORTABLE_FORMAT), _remoteURL)); | |
116 | + map<string, vector<string>> sources; | |
117 | + Poco::XML::DOMParser parser; | |
118 | + try { | |
119 | + Poco::XML::Document* doc = parser.parse(_remoteURL); | |
120 | + if (doc) { | |
121 | + Poco::XML::NodeList* list = doc->getElementsByTagName("item"); | |
122 | + for (int i = 0; i < list->length(); i++) { | |
123 | + Poco::XML::Element* e = (Poco::XML::Element*)list->item(i); | |
124 | + Poco::XML::Element* date = e->getChildElement("dc:date"); | |
125 | + int tzd = 0; | |
126 | + Poco::DateTime dt; | |
127 | + if (date) { | |
128 | + dt = Poco::DateTimeParser::parse(Poco::DateTimeFormat::ISO8601_FORMAT, date->innerText(), tzd); | |
129 | + if (now.day() != dt.day()) continue; | |
130 | + int d = (now.timestamp() - dt.timestamp()) / (60 * Poco::Timestamp::resolution()); | |
131 | + if (d > _validMinutes) continue; | |
132 | + //_log.information(Poco::format("date: %s %d", date->innerText(), d)); | |
133 | + } | |
134 | + vector<string> categories; | |
135 | + Poco::XML::Element* category = e->getChildElement("dc:subject"); | |
136 | + if (category) svvitch::split(category->innerText(), ',', categories); | |
137 | + if (categories.empty()) continue; | |
138 | + for (vector<string>::iterator it = categories.begin(); it != categories.end(); ++it) { | |
139 | + //vector<string>::iterator f = _categories.find(key); | |
140 | + } | |
141 | + Poco::XML::Element* title = e->getChildElement("title"); | |
142 | + if (title) { | |
143 | + for (vector<string>::iterator it = categories.begin(); it != categories.end(); ++it) { | |
144 | + const string& key = *it; | |
145 | + map<string, vector<string>>::iterator m = sources.find(key); | |
146 | + if (m == sources.end()) { | |
147 | + vector<string> vec; | |
148 | + sources[key] = vec; | |
149 | + } | |
150 | + sources[key].push_back(title->innerText()); | |
151 | + } | |
152 | + } | |
153 | + } | |
154 | + list->release(); | |
155 | + doc->release(); | |
156 | + | |
157 | + int count = 0; | |
158 | + for (map<string, vector<string>>::iterator m = sources.begin(); m != sources.end(); ++m) { | |
159 | + //_log.information(Poco::format("category: %s",m->first)); | |
160 | + //for (vector<string>::iterator it = m->second.begin(); it != m->second.end(); ++it) { | |
161 | + // _log.information(Poco::format("text[%s]",*it)); | |
162 | + //} | |
163 | + count += m->second.size(); | |
164 | + } | |
165 | + _log.information(Poco::format("remote reading %ucategories, %dtexts", sources.size(), count)); | |
166 | + } else { | |
167 | + _log.warning("failed not parse remote XML"); | |
168 | + } | |
169 | + } catch (Poco::IOException& ex) { | |
170 | + _log.warning(Poco::format("failed not read XML: %s", ex.displayText())); | |
171 | + } catch (...) { | |
172 | + _log.warning(Poco::format("failed exception in readMXL(): %s", _remoteURL)); | |
173 | + } | |
174 | + return sources; | |
175 | +} | |
176 | + | |
177 | +map<string, vector<string>> TelopScene::_readXML(const Poco::LocalDateTime& now) { | |
178 | + _log.information(Poco::format("check remote[%s]: %s", Poco::DateTimeFormatter::format(now, Poco::DateTimeFormat::SORTABLE_FORMAT), _remoteURL)); | |
179 | + map<string, vector<string>> sources; | |
180 | + Poco::XML::DOMParser parser; | |
181 | + try { | |
182 | + Poco::XML::Document* doc = parser.parse(_remoteURL); | |
183 | + if (doc) { | |
184 | + Poco::XML::NodeList* list = doc->getElementsByTagName("item"); | |
185 | + for (int i = 0; i < list->length(); i++) { | |
186 | + Poco::XML::Element* e = (Poco::XML::Element*)list->item(i); | |
187 | + Poco::XML::Element* date = e->getChildElement("date"); | |
188 | + int tzd = 0; | |
189 | + Poco::DateTime dt; | |
190 | + if (date) { | |
191 | + dt = Poco::DateTimeParser::parse("%Y/%m/%d %H:%M:%S", date->innerText(), tzd); | |
192 | + if (now.day() != dt.day()) continue; | |
193 | + int d = (now.timestamp() - dt.timestamp()) / (60 * Poco::Timestamp::resolution()); | |
194 | + if (d > _validMinutes) continue; | |
195 | + //_log.information(Poco::format("date: %s %d", date->innerText(), d)); | |
196 | + } | |
197 | + Poco::XML::Element* command = e->getChildElement("command"); | |
198 | + if (command && command->innerText() != "9") { | |
199 | + vector<string> categories; | |
200 | + Poco::XML::Element* category = e->getChildElement("category"); | |
201 | + if (category) svvitch::split(category->innerText(), ',', categories); | |
202 | + if (categories.empty()) continue; | |
203 | + Poco::XML::Element* title = e->getChildElement("title"); | |
204 | + if (title) { | |
205 | + for (vector<string>::iterator it = categories.begin(); it != categories.end(); ++it) { | |
206 | + const string& key = *it; | |
207 | + map<string, vector<string>>::iterator m = sources.find(key); | |
208 | + if (m == sources.end()) { | |
209 | + vector<string> vec; | |
210 | + sources[key] = vec; | |
211 | + } | |
212 | + sources[key].push_back(title->innerText()); | |
213 | + } | |
214 | + } | |
215 | + //Poco::XML::Element* caption = e->getChildElement("caption"); | |
216 | + //if (caption) _log.information(Poco::format("caption: %s", caption->innerText())); | |
217 | + } | |
218 | + } | |
219 | + list->release(); | |
220 | + doc->release(); | |
221 | + | |
222 | + int count = 0; | |
223 | + for (map<string, vector<string>>::iterator m = sources.begin(); m != sources.end(); ++m) { | |
224 | + //_log.information(Poco::format("category: %s",m->first)); | |
225 | + //for (vector<string>::iterator it = m->second.begin(); it != m->second.end(); ++it) { | |
226 | + // _log.information(Poco::format("text[%s]",*it)); | |
227 | + //} | |
228 | + count += m->second.size(); | |
229 | + } | |
230 | + _log.information(Poco::format("remote reading %ucategories, %dtexts", sources.size(), count)); | |
231 | + } else { | |
232 | + _log.warning("failed not parse remote XML"); | |
233 | + } | |
234 | + } catch (Poco::IOException& ex) { | |
235 | + _log.warning(Poco::format("failed not read XML: %s", ex.displayText())); | |
236 | + } catch (...) { | |
237 | + _log.warning(Poco::format("failed exception in readMXL(): %s", _remoteURL)); | |
238 | + } | |
239 | + return sources; | |
240 | +} | |
241 | + | |
242 | + | |
243 | +void TelopScene::process() { | |
244 | + if (_telops.empty()) { | |
245 | + _creating = true; | |
246 | + } else { | |
247 | + { | |
248 | + Poco::ScopedLock<Poco::FastMutex> lock(_lock); | |
249 | + bool creating = true; | |
250 | + for (vector<Telop>::iterator it = _telops.begin(); it != _telops.end(); ) { | |
251 | + Telop& t = *it; | |
252 | + creating &= t.x < config().stageRect.right - t.w; | |
253 | + if (t.x < -t.w) { | |
254 | + _deletes.push(t); | |
255 | + it = _telops.erase(it); | |
256 | + continue; | |
257 | + } | |
258 | + t.x += _speed; | |
259 | + ++it; | |
260 | + } | |
261 | + _creating = creating; | |
262 | + } | |
263 | + } | |
264 | + if (!_prepared.empty()) { | |
265 | + { | |
266 | + Poco::ScopedLock<Poco::FastMutex> lock(_lock); | |
267 | + bool add = true; | |
268 | + for (vector<Telop>::iterator it = _telops.begin(); it != _telops.end(); ++it) { | |
269 | + Telop& t = *it; | |
270 | + add &= t.x < config().stageRect.right - t.w - _space; | |
271 | + } | |
272 | + if (add) { | |
273 | + _telops.push_back(_prepared.front()); | |
274 | + _prepared.pop(); | |
275 | + } | |
276 | + } | |
277 | + } | |
278 | +} | |
279 | + | |
280 | +void TelopScene::draw1() { | |
281 | + DWORD col = 0xffffffff; | |
282 | + { | |
283 | + Poco::ScopedLock<Poco::FastMutex> lock(_lock); | |
284 | + for (vector<Telop>::iterator it = _telops.begin(); it != _telops.end(); ++it) { | |
285 | + Telop& t = *it; | |
286 | + _renderer.drawTexture(t.x, t.y, t.texture, 0, col, col, col, col); | |
287 | + } | |
288 | + } | |
289 | +} | |
290 | + | |
291 | +void TelopScene::draw2() { | |
292 | +} |