|
From: Tapio V. <aa...@us...> - 2010-05-12 00:08:56
|
Module: performous
Branch: master
Commit: 7c43188229e352f1d57f5c31a4e89c7a2c2ca5b9
Author: Tapio Vierros <tap...@gm...>
Date: Mon May 10 15:03:27 2010 +0300
Webcam tweaks.
* Fix colors.
* Higher framerate limit.
* Allow regular video to display if webcam is disabled during gameplay.
---
game/screen_sing.cc | 2 +-
game/surface.cc | 2 +-
game/webcam.cc | 13 ++++++++++---
game/webcam.hh | 2 +-
4 files changed, 13 insertions(+), 6 deletions(-)
diff --git a/game/screen_sing.cc b/game/screen_sing.cc
index 20d5549..dcde072 100644
--- a/game/screen_sing.cc
+++ b/game/screen_sing.cc
@@ -307,7 +307,7 @@ void ScreenSing::draw() {
m_background->draw();
} else fillBG();
// Video
- if (m_video && !m_cam && !m_cam->is_good()) { m_video->render(time); double tmp = m_video->dimensions().ar(); if (tmp > 0.0) ar = tmp; }
+ if (m_video && (!m_cam || !m_cam->is_good())) { m_video->render(time); double tmp = m_video->dimensions().ar(); if (tmp > 0.0) ar = tmp; }
// Webcam
if (m_cam && config["graphic/webcam"].b()) m_cam->render();
// Top/bottom borders
diff --git a/game/surface.cc b/game/surface.cc
index be71e13..72b89bd 100644
--- a/game/surface.cc
+++ b/game/surface.cc
@@ -59,7 +59,7 @@ namespace {
PixFormats() {
using namespace pix;
m[RGB] = PixFmt(GL_RGB, GL_UNSIGNED_BYTE, false);
- m[BGR] = PixFmt(GL_RGB, GL_UNSIGNED_BYTE, true);
+ m[BGR] = PixFmt(GL_BGR, GL_UNSIGNED_BYTE, true);
m[CHAR_RGBA] = PixFmt(GL_RGBA, GL_UNSIGNED_BYTE, false);
m[INT_ARGB] = PixFmt(GL_BGRA, GL_UNSIGNED_INT_8_8_8_8, true);
}
diff --git a/game/webcam.cc b/game/webcam.cc
index 8409225..31bc263 100644
--- a/game/webcam.cc
+++ b/game/webcam.cc
@@ -7,6 +7,7 @@ Webcam::Webcam(int cam_id):
m_thread(), m_capture(NULL), m_frameAvailable(false), m_running(false), m_quit(false)
{
#ifdef USE_OPENCV
+ // Initialize the capture device
m_capture = cvCaptureFromCAM(cam_id);
if (!m_capture) {
std::cout << "Could not initialize webcam capturing!" << std::endl;
@@ -31,15 +32,19 @@ void Webcam::operator()() {
m_running = true;
while (!m_quit) {
IplImage* frame = 0;
+ // Try to get a new frame
if (m_running) frame = cvQueryFrame(m_capture);
if (frame) {
boost::mutex::scoped_lock l(m_mutex);
+ // Copy the frame to storage
m_frame.width = frame->width;
m_frame.height = frame->height;
m_frame.data.assign(frame->imageData, frame->imageData + (m_frame.width * m_frame.height * 3));
+ // Notify renderer
m_frameAvailable = true;
}
- boost::thread::sleep(now() + (m_running ? 0.05 : 0.5));
+ // Sleep a little, much if the cam isn't active
+ boost::thread::sleep(now() + (m_running ? 0.015 : 0.5));
}
#endif
}
@@ -53,11 +58,13 @@ void Webcam::pause(bool do_pause) {
void Webcam::render() {
#ifdef USE_OPENCV
if (!m_capture || !m_running) return;
+ // Do we have a new frame available?
if (m_frameAvailable && !m_frame.data.empty()) {
boost::mutex::scoped_lock l(m_mutex);
- m_surface.load(m_frame.width, m_frame.height, pix::RGB, &m_frame.data[0]);
+ // Load the image
+ m_surface.load(m_frame.width, m_frame.height, pix::BGR, &m_frame.data[0]);
m_frameAvailable = false;
}
- m_surface.draw();
+ m_surface.draw(); // Draw
#endif
}
diff --git a/game/webcam.hh b/game/webcam.hh
index a5c17b5..3fb1cea 100644
--- a/game/webcam.hh
+++ b/game/webcam.hh
@@ -30,7 +30,7 @@ class Webcam {
void operator()();
/// Is good?
- bool is_good() { return m_capture != 0; }
+ bool is_good() { return m_capture != 0 && m_running; }
/// When paused, does not get or render frames
void pause(bool do_pause = true);
/// Display frame
|