Screen vertically flipped on SDL 2.0.9 depending on RenderTarget


#1

Hello,

When we render an image in a texture and then the texture on screen, the result is strangely flipped vertically in 2.0.9. It’s not the case in 2.0.8.

The test case is the following one:

#include <stdio.h>
#include <stdlib.h>

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

SDL_Texture *WID_loadTexture( SDL_Renderer *renderer, char *name)
{
	SDL_Texture *texture = NULL;

	if(renderer == NULL)
	{
		printf("%s\n", SDL_GetError());
		SDL_ClearError();
		return NULL;
	}

	SDL_Surface* surface = IMG_Load( name );
	if (surface == NULL)
	{
		printf("%s\n", SDL_GetError());
	}
	else
	{
		texture = SDL_CreateTextureFromSurface( renderer, surface );
		if (texture == NULL) {
			printf("%s\n", SDL_GetError());
			SDL_ClearError();
			return NULL;
		}
		SDL_FreeSurface( surface );
	}

	return texture;
}


int main(void) {
	SDL_Window *WID_main_window;
	SDL_Renderer *WID_main_renderer;
    SDL_Event sdlevt;


    if ( SDL_Init( SDL_INIT_EVERYTHING) < 0 ) {
        exit(-1);
    }

	int video_driver_index = 1;
	int renderer_index = 1;

    SDL_RendererInfo info;

    setenv("SDL_VIDEODRIVER",SDL_GetVideoDriver(video_driver_index),1);

	SDL_GetRenderDriverInfo(renderer_index,&info);

	if(SDL_SetHint(SDL_HINT_RENDER_DRIVER, info.name) != SDL_TRUE)
	{
		printf("SDL_SetHint %s\n", SDL_GetError());
		exit(-1);
	}

	if ((WID_main_window = SDL_CreateWindow("icoone", 0, 0, 1024, 768, SDL_WINDOW_SHOWN)) == NULL) {
		exit(-1);
	}

	if ((WID_main_renderer = SDL_CreateRenderer(WID_main_window, renderer_index, info.flags)) == NULL) {
		exit(-1);
	}

    if (SDL_SetRenderTarget(WID_main_renderer, NULL) != 0) {
		printf("SDL_SetRenderTarget %s\n", SDL_GetError());
		exit(-1);
	}
	SDL_SetRenderDrawColor(WID_main_renderer, 0x80, 0x80, 0x80, SDL_ALPHA_OPAQUE);

	SDL_Texture *drawingTexture;

	if ((drawingTexture = SDL_CreateTexture(WID_main_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, 1024, 768)) == NULL) {
		printf("SDL_CreateTexture %s\n", SDL_GetError());
		exit(-1);
	}

	SDL_Texture			*backgroundImage;
	if ((backgroundImage = WID_loadTexture(WID_main_renderer, "visage.jpg")) == NULL) {
		exit(-1);
	}

	SDL_RenderClear(WID_main_renderer);

	if (SDL_SetRenderTarget(WID_main_renderer, NULL) != 0) {
		fprintf(stderr, "SDL_SetRenderTarget error %s\n", SDL_GetError());
		exit(-1);
	}

	if (SDL_RenderCopy( WID_main_renderer, backgroundImage, NULL, NULL) != 0) {
		fprintf(stderr, "SDL_RenderCopy error %s\n", SDL_GetError());
		exit(-1);
	}

	SDL_RenderPresent(WID_main_renderer);

	SDL_Delay(2000);

	SDL_RenderClear(WID_main_renderer);

	if (SDL_SetRenderTarget(WID_main_renderer, drawingTexture) != 0) {
		fprintf(stderr, "SDL_SetRenderTarget error %s\n", SDL_GetError());
		exit(-1);
	}
	if (SDL_RenderCopy( WID_main_renderer, backgroundImage, NULL, NULL) != 0) {
		fprintf(stderr, "SDL_RenderCopy error %s\n", SDL_GetError());
		exit(-1);
	}

	if (SDL_SetRenderTarget(WID_main_renderer, NULL) != 0) {
		fprintf(stderr, "SDL_SetRenderTarget error %s\n", SDL_GetError());
		exit(-1);
	}

	if (SDL_RenderCopy( WID_main_renderer, drawingTexture, NULL, NULL) != 0) {
		fprintf(stderr, "SDL_RenderCopy error %s\n", SDL_GetError());
		exit(-1);
	}

	SDL_RenderPresent(WID_main_renderer);

    int loop = 1;
    while (loop) {
    	SDL_PollEvent(&sdlevt);

		if ( sdlevt.type == SDL_QUIT ) {
			loop = 0;
		}

		SDL_Delay(50);
		SDL_FlushEvent(SDL_MOUSEMOTION);

    }

    SDL_DestroyTexture(backgroundImage);
    SDL_DestroyRenderer(WID_main_renderer);
    SDL_DestroyWindow(WID_main_window);
    SDL_Quit();

	return EXIT_SUCCESS;
}

The image is here https://i.imgur.com/Gg4IBM2.jpg


Rendering to Texture Target and Clipping
#2

I’m having the same problem after 2.0.8 with target textures being flipped. Seems to be happening for me only with openGL renderer. I’m using Windows 10.

Are you able to raise a bug?


#3

Early versions of SDL2 had a similar (or identical) bug, which was fixed in 2.0.5 or thereabouts, so this may be a regression.


#4

:-((((
Ok I’m going to report it.

Thanks a lot

Alain