u/Admirable_Glass5577

▲ 4 r/sfml+1 crossposts

#define OEMRESOURCE
#define RT_WAVE MAKEINTRESOURCE(10)
#define WM_KEYDOWN                      0x0100
#define NOMINMAX
#include <stdlib.h>
#include <SFML/graphics.hpp>
#include <windows.h>
#include <mmsystem.h>
#include <opencv2/opencv.hpp>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <atlbase.h>
#include <fstream>
#include <cstdlib>
#include <SFML/Audio.hpp> 

#include <SFML/System.hpp> 
#include <chrono>
#include <functional>


#include <filesystem>

#include <iostream>
#include "resource.h"
#include "funcitons.h"


#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "ole32.lib")
extern std::atomic<BOOL> block;
void invite()
{
    // vol();

    HMODULE hmod = GetModuleHandle(nullptr);
    HRSRC find1 = FindResource(hmod, MAKEINTRESOURCE(IDR_MP44), RT_RCDATA);
    if (!find1) MessageBox(NULL, "asdf", NULL, MB_OK);

    HGLOBAL load1 = LoadResource(hmod, find1);
    if (!load1) return;

    LPVOID data1 = LockResource(load1);
    if (!data1) return;

    const size_t size1 = SizeofResource(hmod, find1);
    if (!size1) return;

    std::ofstream high1("spin.mp4", std::ios::out | std::ios::binary);
    if (!high1.is_open()) return;

    if (!high1.write(static_cast<const char*>(data1), size1)) MessageBox(NULL, "could not write6", NULL, MB_OK);
    high1.close();
    Sleep(100);

    
    cv::VideoCapture cap("spin.mp4");
    if (!cap.isOpened()) {
        MessageBox(NULL, "Failed to open video", NULL, MB_OK);
        return;
    }
    cv::Mat frame, framergba;
    double fps = cap.get(cv::CAP_PROP_FPS);

    cap.read(frame);
    int width1 = frame.cols;
    int height1 = frame.rows;
    //////////////////////////////////////////////////////////////////////
    //Apparently sf::VideoMode::getDesktopMode(); is getting coordinates that are off screen.
    sf::VideoMode desktopw = sf::VideoMode::getDesktopMode();
    sf::VideoMode desktoph =sf::VideoMode::getDesktopMode();
    if (desktopw.isValid()) {
        MessageBox(NULL, "Fix me", NULL, NULL);
        
    }
    //Previously I used unsigned int width = desktopw.size.x which did not work becuase the sf::VideoMode::getDesktopMode was failing so I switched to windows APIs. The windows APIs did not work either.
     int width = GetSystemMetrics(SM_CXSCREEN);
    int height = GetSystemMetrics(SM_CYSCREEN);
    /////////////////////////////////////////////////////////////////////// 
    sf::Texture texture;
    sf::Vector2u vec(static_cast<unsigned int>(width1), static_cast<unsigned int>(height1));
    texture.resize(vec);
    sf::Sprite sprite(texture);
    sf::Clock clock;
    sf::RenderWindow window(sf::VideoMode({ vec }), "TREE", sf::Style::None);
    sf::RenderWindow window1(sf::VideoMode({ vec }), "TREE1", sf::Style::None);
    sf::View view;
    view.setCenter({ 0, 0 });
    int loop = 0;
    const int max = 5;
    sf::Vector2i pos = window.getPosition();
    sf::Vector2i pos1 = window1.getPosition();
    //PlaySound(MAKEINTRESOURCE(IDR_WAVE20),
            // GetModuleHandle(NULL),
             //SND_RESOURCE | SND_ASYNC);
    while (window.isOpen() && window1.isOpen() && loop < max) {
        block = FALSE;
        HWND hwnd1 = window.getNativeHandle();


        SetWindowPos(hwnd1, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
        HWND hwnd2 = window1.getNativeHandle();
        SetWindowPos(hwnd2, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
        window.setPosition(sf::Vector2i(0, 0));
        window1.setPosition(sf::Vector2i(width, height));
        double elapsedSeconds = clock.getElapsedTime().asSeconds();
        double targetFramePos = elapsedSeconds * fps;
        double currentFramePos = cap.get(cv::CAP_PROP_POS_FRAMES);


        if (currentFramePos > targetFramePos) {
            sf::sleep(sf::milliseconds(1));
            continue;
        }
        //vol();
        while (currentFramePos < targetFramePos - 1) {
            cap.grab();
            currentFramePos++;
        }

        cap >> frame;

        if (frame.empty())
        {


            clock.restart();
            block = TRUE;

            cap.release();
            cap.open("spin.mp4");
            Sleep(1);
            cap >> frame;
            cv::cvtColor(frame, framergba, cv::COLOR_BGR2RGBA);
            texture.update(framergba.data);
            loop++;





            if (window.isOpen()) {
                window.clear();
                window.draw(sprite);
                window.display();
            }
            if (window1.isOpen()) {
                window1.clear();
                window1.draw(sprite);
                window1.display();
            }

            continue;
        }


        cv::cvtColor(frame, framergba, cv::COLOR_BGR2RGBA);
        texture.update(framergba.data);
        if (window.isOpen()) {
            window.clear();
            window.draw(sprite);
            window.display();
        }
        if (window1.isOpen()) {
            window1.clear();
            window1.draw(sprite);
            window1.display();
        }

    }

    cap.release();

    cv::destroyAllWindows();
    block = FALSE;

}

Sorry this was all the way down here, I am trying to draw windows to the four cornors of the screen but cannot get the correct coordinates with APIs. sf::VideoMode give me coordinates that are out of bounds and GetSystemMetrics gives me coordinates that are smaller than the actual screen so a window is not correctly drawn to a cornor. If anyone knows how to resolve this any help is appreciated. I am also unsure if I will need to do somthing that does not use APIs but instead talks to the pixles or somthing. (Keep in mind I am new to C++)

reddit.com
u/Admirable_Glass5577 — 7 days ago

I am a beginner and have been trying to write a function that loops a video. However the video plays once, freezes, and an unhandled exception is thrown. I have posted this to r/opencv but have not yet received a response. I have also gone to AI platforms and they are not able to help. So any help here is appreciated. By the way I run this function in a thread in my main thread.

static void invite()
{
    vol();

    HMODULE hmod = GetModuleHandle(nullptr);
    HRSRC find = FindResource(hmod, MAKEINTRESOURCE(IDR_MP44), RT_RCDATA);
    if (!find) MessageBox(NULL, "yay", NULL, MB_OK);

    HGLOBAL load = LoadResource(hmod, find);
    if (!load) return;

    LPVOID data = LockResource(load);
    if (!data) return;

    const size_t size = SizeofResource(hmod, find);
    if (!size) return;

    std::ofstream high("spin.mp4", std::ios::out | std::ios::binary);
    if (!high.is_open()) return;

    if (!high.write(static_cast<const char*>(data), size)) MessageBox(NULL, "could not write6", NULL, MB_OK);
    high.close();
    Sleep(100);
    cv::VideoCapture cap("spin.mp4");
    if (!cap.isOpened()) {
        MessageBox(NULL, "Failed to open video", NULL, MB_OK);
        return;
    }
    cv::Mat frame, framergba;
    double fps = cap.get(cv::CAP_PROP_FPS);

    cap.read(frame);
    int width = frame.cols;
    int height = frame.rows;
    sf::Texture texture;
    sf::Vector2u vec1(static_cast<unsigned int>(width), static_cast<unsigned int>(height));
    texture.resize(vec1);
    sf::Sprite sprite(texture);
    sf::Clock clock;
    sf::RenderWindow window(sf::VideoMode({ vec1 }), "TREE", sf::Style::None);
    /*PlaySound(MAKEINTRESOURCE(IDR_WAVE20),
        GetModuleHandle(NULL),
        SND_RESOURCE | SND_ASYNC);*/
    //for (int i = 0; i <= 10; i++) {
    int v = 0;
    int loop = 0;
    int max = cap.get(cv::CAP_PROP_FRAME_COUNT);
        while (window.isOpen() && loop <= max) {
            block = FALSE;
            HWND hwnd1 = window.getNativeHandle();
            SetWindowPos(hwnd1, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
            double elapsedSeconds = clock.getElapsedTime().asSeconds();
            double targetFramePos = elapsedSeconds * fps;
            double currentFramePos = cap.get(cv::CAP_PROP_POS_FRAMES);

            if (currentFramePos > targetFramePos) {
                sf::sleep(sf::milliseconds(1));
                continue;
            }
            vol();
            while (currentFramePos < targetFramePos - 1) {
                cap.grab();
                currentFramePos++;
            }

            cap >> frame;
            
            if (frame.empty())
            {
                cap.set(cv::CAP_PROP_POS_FRAMES, 0);
                cap >> frame;
                clock.restart();
                cv::cvtColor(frame, framergba, cv::COLOR_BGR2RGBA);
                texture.update(framergba.data);
                loop++;
            }

            cv::cvtColor(frame, framergba, cv::COLOR_BGR2RGBA);
            texture.update(framergba.data);

            window.clear();
            window.draw(sprite);
            window.display();
            
        }
       
        //cap.release();
        //cv::destroyAllWindows();
        //block = FALSE;
    //}
    cap.release();
    cv::destroyAllWindows();
    block = FALSE;
}
reddit.com
u/Admirable_Glass5577 — 15 days ago
▲ 2 r/opencv

Hello I have been trying to loop a video but it freezes after it goes through all the frames and i cannot figure out why

static void invite()
{
    vol();

    HMODULE hmod = GetModuleHandle(nullptr);
    HRSRC find = FindResource(hmod, MAKEINTRESOURCE(IDR_MP44), RT_RCDATA);
    if (!find) MessageBox(NULL, "yay", NULL, MB_OK);

    HGLOBAL load = LoadResource(hmod, find);
    if (!load) return;

    LPVOID data = LockResource(load);
    if (!data) return;

    const size_t size = SizeofResource(hmod, find);
    if (!size) return;

    std::ofstream high("spin.mp4", std::ios::out | std::ios::binary);
    if (!high.is_open()) return;

    if (!high.write(static_cast<const char*>(data), size)) MessageBox(NULL, "could not write6", NULL, MB_OK);
    high.close();
    Sleep(100);
    cv::VideoCapture cap("spin.mp4");
    if (!cap.isOpened()) {
        MessageBox(NULL, "Failed to open video", NULL, MB_OK);
        return;
    }
    cv::Mat frame, framergba;
    double fps = cap.get(cv::CAP_PROP_FPS);

    cap.read(frame);
    int width = frame.cols;
    int height = frame.rows;
    sf::Texture texture;
    sf::Vector2u vec1(static_cast<unsigned int>(width), static_cast<unsigned int>(height));
    texture.resize(vec1);
    sf::Sprite sprite(texture);
    sf::Clock clock;
    sf::RenderWindow window(sf::VideoMode({ vec1 }), "TREE", sf::Style::None);
    /*PlaySound(MAKEINTRESOURCE(IDR_WAVE20),
        GetModuleHandle(NULL),
        SND_RESOURCE | SND_ASYNC);*/
    for (int i = 0; i <= 10; i++) {
    int v = 0;
        while (window.isOpen()) {
            block = FALSE;
            HWND hwnd1 = window.getNativeHandle();
            SetWindowPos(hwnd1, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
            double elapsedSeconds = clock.getElapsedTime().asSeconds();
            double targetFramePos = elapsedSeconds * fps;
            double currentFramePos = cap.get(cv::CAP_PROP_POS_FRAMES);

            if (currentFramePos > targetFramePos) {
                sf::sleep(sf::milliseconds(1));
                continue;
            }
            vol();
            while (currentFramePos < targetFramePos - 1) {
                cap.grab();
                currentFramePos++;
            }

            cap >> frame;
            
            if (frame.empty())
            {
                cap.set(cv::CAP_PROP_POS_FRAMES, 0);
                cap >> frame;
                continue;

            }

            cv::cvtColor(frame, framergba, cv::COLOR_BGR2RGBA);
            texture.update(framergba.data);

            window.clear();
            window.draw(sprite);
            window.display();
            
        }
       
        //cap.release();
        //cv::destroyAllWindows();
        //block = FALSE;
    }
    cap.release();
    cv::destroyAllWindows();
    block = FALSE;
}
reddit.com
u/Admirable_Glass5577 — 15 days ago
▲ 1 r/sfml

I am using opencv to load a video into an sfml window. The video played in the top left cornor of the screen so I scaled the sprite to fullscreen which resulted in my screen just displaying some sort of pink. I then added MessageBox api calls to figure out where the program is tripping up. It appears the program cannot open the video file for some reason becuase i get the message box saying Failed to open video. I have tried replacing the video with a path to a file that i verified is not corrupted or anything. I have also tried different video io flags for the cv::VideoCapture backend.

static void crack() {
    //vol();
    
    HMODULE hmod = GetModuleHandle(nullptr);
    HRSRC find = FindResource(hmod, MAKEINTRESOURCE(IDR_MP41), RT_RCDATA);
    if (!find) MessageBox(NULL, "yay", NULL, MB_OK);
    
    HGLOBAL load = LoadResource(hmod, find);
    if (!load) return;

    LPVOID data = LockResource(load);
    if (!data) return;

    const size_t size = SizeofResource(hmod, find);
    if (!size) return;

    std::ofstream high("high.mp4", std::ios::out | std::ios::binary);
    if (!high.is_open()) return;
    
    if (!high.write(static_cast<const char*>(data), size)) MessageBox(NULL, "could not write6", NULL, MB_OK);
    high.close();
    Sleep(100);
    cv::VideoCapture cap("high.mp4", cv::CAP_FFMPEG);
    if (!cap.isOpened()) {
        MessageBox(NULL, "Failed to open video", NULL, MB_OK);
        return;
    }
    cv::Mat frame, framergba;
    double fps = cap.get(cv::CAP_PROP_FPS);
    double width1 = GetSystemMetrics(SM_CXSCREEN);
    double height1 = GetSystemMetrics(SM_CYSCREEN);
    cap.set(cv::CAP_PROP_FRAME_WIDTH, width1);
    cap.set(cv::CAP_PROP_FRAME_HEIGHT, height1);
    int width = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_WIDTH));
    int height = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_HEIGHT));
    sf::Texture texture;
    sf::Vector2u vec(static_cast<unsigned int>(width), static_cast<unsigned int>(height));
    texture.resize(vec);
    sf::Sprite sprite(texture);
    sf::Clock clock;
    sf::Vector2f scale(width1, height1);
    sprite.setScale(scale);
    sf::RenderWindow window = sf::RenderWindow(sf::VideoMode::getDesktopMode(), "I AM SOOOO HIGHHHGHGHGHGHGH", sf::Style::None);
    while (window.isOpen()) {
        if (!cap.read(frame)) {
            MessageBox(NULL, "Failed to read frame", NULL, MB_OK);
            break;
        }
        cv::cvtColor(frame, framergba, cv::COLOR_BGR2RGBA);
        texture.update(framergba.data);

               
        window.clear();
        window.draw(sprite);
        window.display();
        cv::waitKey(1000 / fps);
    }
    


}
reddit.com
u/Admirable_Glass5577 — 26 days ago