'SDL_RenderCopy-ing to another texture results in subtly darker image
I've been chasing this visual bug for a while, and I've concluded that the process of renderCopying from one texture to another results in a slightly darkened image.
#include "basics.h"
#include <SDL.h>
#include <SDL_ttf.h>
#ifdef WIN32
#define _WIN32_WINNT 0x0500
#include <windows.h>
#endif
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~O~~~~~~~~~~| M A I N |~~~~~~~~~~~O~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int main(int argc, char *argv[]){
SDL_Window *window;
SDL_Renderer *renderer;
int width = 640;
int height = 480;
int loop = 1;
int mouseX, mouseY, pmouseX, pmouseY;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError());
return 3;
}
if (SDL_CreateWindowAndRenderer(width, height, SDL_WINDOW_RESIZABLE | SDL_WINDOW_MAXIMIZED, &window, &renderer)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window and renderer: %s", SDL_GetError());
return 3;
}
SDL_GetWindowSize( window, &width, &height );
const SDL_Color black = {0, 0, 0, 255};
const SDL_Color white = {255, 255, 255, 255};
char font_filename [] = "fonts/FreeSerif.ttf";//AveriaLibre-Regular
int size = 20;
if(TTF_Init() == -1){
printf("TTF_Init: %s\n", TTF_GetError());
}
TTF_Font *font = TTF_OpenFont( font_filename, size );
char str [] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
SDL_Rect dst1 = (SDL_Rect){ 100, 100, 400, 200 };
SDL_Rect dst2 = (SDL_Rect){ 515, 100, 400, 200 };
SDL_Texture *buffer = SDL_CreateTexture( renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, width, height );
SDL_SetTextureBlendMode( buffer, SDL_BLENDMODE_BLEND );
SDL_SetRenderDrawColor( renderer, 0, 0, 0, 255);
SDL_RenderClear( renderer );
SDL_Surface *surf = TTF_RenderText_Blended_Wrapped( font, str, white, dst1.w );
dst1.w = surf->w; dst1.h = surf->h;
SDL_Texture *texture = SDL_CreateTextureFromSurface( renderer, surf );
SDL_RenderCopy( renderer, texture, NULL, &dst1 );
SDL_SetRenderTarget( renderer, buffer );
dst2.w = surf->w; dst2.h = surf->h;
SDL_RenderCopy( renderer, texture, NULL, &dst2 );
SDL_SetRenderTarget( renderer, NULL );
SDL_RenderCopy( renderer, buffer, NULL, &(SDL_Rect){0, 0, width, height} );
SDL_FreeSurface( surf );
SDL_DestroyTexture( texture );
char ascii [] = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
surf = TTF_RenderText_Blended( font, ascii, white );
texture = SDL_CreateTextureFromSurface( renderer, surf );
SDL_RenderCopy( renderer, texture, NULL, &(SDL_Rect){100, 500, surf->w, surf->h} );
SDL_SetRenderTarget( renderer, buffer );
SDL_RenderCopy( renderer, texture, NULL, &(SDL_Rect){100, 530, surf->w, surf->h} );
SDL_SetRenderTarget( renderer, NULL );
SDL_RenderCopy( renderer, buffer, NULL, &(SDL_Rect){0, 0, width, height} );
SDL_RenderPresent(renderer);
while( 1 ) {
SDL_Event event;
while( SDL_PollEvent(&event) ){
if(event.type == SDL_QUIT) {
goto exit;
}
}
}
exit:
SDL_DestroyTexture( buffer );
SDL_DestroyTexture( texture );
SDL_FreeSurface( surf );
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
this program results in this: text rendering output
Like I say, it's a subtle effect, you might need to zoom in, but for my purposes it's really awful. Anyone know what could be the cause, or some workaround perhaps? thanks.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
