Carlos Pinto
Carlos Pinto

Reputation: 45

Showing a video using two textures SDL2

I need to build an interface where on the left side of the screen shows part of one streaming video and the right side the other part. Something like this https://www.youtube.com/watch?v=fSPXpdVzamo

The video streaming is saved on a memory buffer that is being loaded on a texture. My question is how to render just the half of the texture, I've bee trying using SDL_Rect but nothing happens.

This is the relevant part of my code:

SDL_UpdateTexture(texture, NULL, buffer_start, fmt.fmt.pix.width * 2);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);

If I try something like this, it doesn't work:

SDL_UpdateTexture(texture, NULL, buffer_start, fmt.fmt.pix.width * 2);

SDL_Rect someRect;
someRect.x = 0;
someRect.y = 0;
someRect.w = 1500;
someRect.h = 3000;

SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, &someRect);
SDL_RenderPresent(renderer);

Any advice would be great!

Upvotes: 0

Views: 592

Answers (1)

aram
aram

Reputation: 1444

Without you posting a MCVE is hard to know where you went wrong. My guess is your x position is wrong. Here is an example where I show how to draw 2 images in the fashion of your video.

Green image: https://i.sstatic.net/Fqnlk.png

Red image: https://i.sstatic.net/0HKIf.png

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <iostream>

#define HEIGHT 600
#define WIDTH  800

using namespace std;

int main() {
    SDL_Init(SDL_INIT_VIDEO);

    SDL_Window *window = SDL_CreateWindow("Red Green", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN);

    SDL_Renderer *renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

    bool quit = false;
    SDL_Event event;

    SDL_Texture *green_part = IMG_LoadTexture(renderer, "Green400x600.png");
    SDL_Texture *red_part = IMG_LoadTexture(renderer, "Red400x600.png");

    while (!quit) {
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                quit = true;
            }
        }

        SDL_RenderClear(renderer);
        SDL_Rect copy_rect{0, 0, 400, 600};
        SDL_RenderCopy(renderer, green_part, nullptr, &copy_rect);
        // We now draw from half the screen onward x position = WIDTH / 2.
        copy_rect.x = 400; 
        SDL_RenderCopy(renderer, red_part, nullptr, &copy_rect);

        SDL_RenderPresent(renderer);
    }

    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);
    SDL_Quit();

    return 0;
}

Upvotes: 2

Related Questions