Skip to content

Commit

Permalink
update sample
Browse files Browse the repository at this point in the history
  • Loading branch information
zhonghong322 committed Jan 16, 2024
1 parent c431242 commit fa99fea
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
26 changes: 18 additions & 8 deletions examples/c/window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,15 @@ class Window {
public:
// create a window with the specified name, width and height
Window(std::string name, int width, int height, RenderType renderType_ = RENDER_SINGLE)
: name_(name), width_(width), height_(height), windowClose_(false), key_(-1), showInfo_(false), renderType_(renderType_), alpha_(0.6) {
: name_(name),
width_(width),
height_(height),
windowClose_(false),
threadExit_(false),
key_(-1),
showInfo_(false),
renderType_(renderType_),
alpha_(0.6) {
processThread_ = std::thread(&Window::processFrames, this);
#ifndef __APPLE__
renderThread_ = std::thread(&Window::renderMats, this);
Expand Down Expand Up @@ -107,6 +115,7 @@ class Window {
return;
}
windowClose_ = true;
threadExit_ = true;
srcFramesCv_.notify_all();
processThread_.join();
#ifndef __APPLE__
Expand All @@ -132,7 +141,7 @@ class Window {
#ifdef __APPLE__
render();
#endif
return !windowClose_;
return !threadExit_;
}

// set show frame info
Expand All @@ -157,6 +166,7 @@ class Window {
int width_;
int height_;
bool windowClose_;
bool threadExit_;
bool showInfo_;
float alpha_;

Expand Down Expand Up @@ -190,11 +200,11 @@ class Window {
uint32_t width;
uint32_t height;
uint8_t pixelAvailableBitSize;
while(!windowClose_) {
while(!threadExit_) {
{
std::unique_lock<std::mutex> lk(srcFramesMtx_);
srcFramesCv_.wait(lk, [this] { return !srcFrames_.empty() || windowClose_; });
if(windowClose_) {
srcFramesCv_.wait(lk, [this] { return !srcFrames_.empty() || threadExit_; });
if(threadExit_) {
break;
}
frames = srcFrames_;
Expand Down Expand Up @@ -376,7 +386,7 @@ class Window {
keyCv_.notify_all();

if(key == ESC_KEY) {
windowClose_ = true;
threadExit_ = true;
srcFramesCv_.notify_all();
}
}
Expand Down Expand Up @@ -492,9 +502,9 @@ class Window {
cv::namedWindow(name_, cv::WINDOW_NORMAL | cv::WINDOW_KEEPRATIO);
cv::resizeWindow(name_, width_, height_);

while(!windowClose_) {
while(!threadExit_) {
render();
}
}
#endif
};
};
26 changes: 18 additions & 8 deletions examples/cpp/window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ class Window {
public:
// create a window with the specified name, width and height
Window(std::string name, int width, int height, RenderType renderType_ = RENDER_SINGLE)
: name_(name), width_(width), height_(height), windowClose_(false), key_(-1), showInfo_(false), renderType_(renderType_), alpha_(0.6) {
: name_(name),
width_(width),
height_(height),
windowClose_(false),
threadExit_(false),
key_(-1),
showInfo_(false),
renderType_(renderType_),
alpha_(0.6) {
processThread_ = std::thread(&Window::processFrames, this);
#ifndef __APPLE__
renderThread_ = std::thread(&Window::renderMats, this);
Expand Down Expand Up @@ -78,6 +86,7 @@ class Window {
return;
}
windowClose_ = true;
threadExit_ = true;
srcFramesCv_.notify_all();
processThread_.join();
#ifndef __APPLE__
Expand All @@ -99,7 +108,7 @@ class Window {
#ifdef __APPLE__
render();
#endif
return !windowClose_;
return !threadExit_;
}

// set show frame info
Expand All @@ -124,6 +133,7 @@ class Window {
int width_;
int height_;
bool windowClose_;
bool threadExit_;
bool showInfo_;
float alpha_;

Expand All @@ -149,11 +159,11 @@ class Window {
cv::Mat imuMat;
cv::Mat rstMat;
std::vector<std::shared_ptr<ob::Frame>> frames;
while(!windowClose_) {
while(!threadExit_) {
{
std::unique_lock<std::mutex> lk(srcFramesMtx_);
srcFramesCv_.wait(lk, [this] { return !srcFrames_.empty() || windowClose_; });
if(windowClose_) {
srcFramesCv_.wait(lk, [this] { return !srcFrames_.empty() || threadExit_; });
if(threadExit_) {
break;
}
frames = srcFrames_;
Expand Down Expand Up @@ -323,7 +333,7 @@ class Window {
keyCv_.notify_all();

if(key == ESC_KEY) {
windowClose_ = true;
threadExit_ = true;
srcFramesCv_.notify_all();
}
}
Expand Down Expand Up @@ -439,9 +449,9 @@ class Window {
cv::namedWindow(name_, cv::WINDOW_NORMAL | cv::WINDOW_KEEPRATIO);
cv::resizeWindow(name_, width_, height_);

while(!windowClose_) {
while(!threadExit_) {
render();
}
}
#endif
};
};

0 comments on commit fa99fea

Please sign in to comment.