Running on: Ubuntu 18.04, SDL2.
So my SDL2 window disappears after a second of appearing in the task bar. From researching online I found out that there is a main loop to be used to get events but nowhere stating that it is necessary. I have to display a Video read from OpenCV on SDL2 window. Here is the code:
main.cpp
int main() {
cv::Mat frame;
cv::VideoCapture video(constant::video::PATH);
Display2d* videoWindow = new Display2d(constant::video::WIDTH, constant::video::HEIGHT, "Video", (int)video.get(cv::CAP_PROP_FRAME_WIDTH), (int)video.get(cv::CAP_PROP_FRAME_HEIGHT));
if (!video.isOpened()) {
std::cerr << "Cannot open Video file" << '\n';
}
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
std::cerr << "Cannot Initialize SDL" << '\n';
}
while (true) {
video >> frame;
if (frame.empty()) break;
cv::resize(frame, frame, cv::Size(), 0.5, 0.5, cv::INTER_AREA);
//cv::imshow("Video", frame);
videoWindow->display(frame);
}
video.release();
videoWindow->clear();
SDL_Quit();
return 0;
}
display2d.hpp
Display2d::Display2d(int width, int height, std::string title, int frameWidth, int frameHeight) {
this->width = width;
this->height = height;
this->title = title.c_str();
this->window = SDL_CreateWindow(this->title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, this->width, this->height, SDL_WINDOW_SHOWN);
this->renderer = SDL_CreateRenderer(this->window, -1, 0);
this->texture = SDL_CreateTexture(this->renderer, SDL_PIXELFORMAT_BGR24, SDL_TEXTUREACCESS_STREAMING, frameWidth, frameHeight);
std::cout << SDL_GetError() << std::endl;
};
void Display2d::display(const cv::Mat & mat) {
IplImage frame = (IplImage)mat;
IplImage * img = &frame;
unsigned char * texture_data = nullptr;
int texture_pitch = 0;
SDL_LockTexture(this->texture, NULL, (void **)&texture_data, &texture_pitch);
memcpy(texture_data, (void *)img->imageData, img->width * img->height * img->nChannels);
SDL_UnlockTexture(this->texture);
SDL_RenderClear(this->renderer);
SDL_RenderCopy(this->renderer, this->texture, NULL, NULL);
SDL_RenderPresent(this->renderer);
}
void Display2d::clear() {
SDL_DestroyTexture(this->texture);
SDL_DestroyRenderer(this->renderer);
SDL_DestroyWindow(this->window);
}
Aucun commentaire:
Enregistrer un commentaire