I have a problem I using SDL in Visual Studio 2019. It is a new problem I did not have 2 week ago. I can’t tell exactly when it appeared because I have updated VS2019, Windows, my Nvidia driver since I last compiled SDL in VS. This have worked as I expected in the past, it is just recently it started to happen.
The problem is that when I print text the color when I draw simple shaped like SDL_RenderFillRect is disturbed. I run the code below and the verry first frame it works as intende then all rectangles and lines turn black.
/*Current NuGet packets installed
Install-Package sdl2.nuget -Version 2.0.10
Install-Package sdl2_image.nuget -Version 2.0.5
Install-Package sdl2_ttf.nuget -Version 2.0.15
Install-Package sdl2_mixer.nuget -Version 2.0.4
*/
#include <iostream>
#include <string>
#include <assert.h>
#include <SDL.h>
#include <SDL_ttf.h>
void DrawText(SDL_Renderer *renderer, TTF_Font *amiga, std::string text, SDL_Color color, int x, int y)
{
SDL_Surface *surface;
SDL_Texture *texture;
surface = TTF_RenderText_Solid(amiga, text.c_str(), color);
texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Rect dest;
int w;
int h;
SDL_QueryTexture(texture, NULL, NULL, &w, &h);
dest.x = x;
dest.y = y;
dest.w = w;
dest.h = h;
SDL_RenderCopy(renderer, texture, NULL, &dest);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
}
int main(int argc, char *argv[])
{
std::cout << "enter" << std::endl;
SDL_Init(SDL_INIT_EVERYTHING);
TTF_Init();
SDL_Window *window = SDL_CreateWindow("A bug?", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1024, 768, SDL_WINDOW_SHOWN);
assert(window);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
assert(renderer);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0");
TTF_Font *amiga = TTF_OpenFont("amiga4ever pro2.ttf", 16);;
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
for(int i = 0; i < 5; i++)
{
SDL_SetRenderDrawColor(renderer, 128, 128, 128, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0, 255, 255, 255);
SDL_RenderDrawLine(renderer, 10, 10, 900, 600);
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_Rect dest;
dest.x = 100;
dest.y = 100;
dest.h = 100;
dest.w = 100;
SDL_RenderFillRect(renderer, &dest);
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
dest;
dest.x = 200;
dest.y = 200;
dest.h = 100;
dest.w = 100;
SDL_RenderFillRect(renderer, &dest);
DrawText(renderer, amiga, "white text", { 255, 255, 255, 255 }, 40, 500);
SDL_RenderPresent(renderer);
SDL_Delay(2000); // 2 second delay show the error
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_CloseFont(amiga);
SDL_Quit();
std::cout << "exit" << std::endl;
system("pause");
return 0;
}
This is the very first frame of the program. I used a sleep for 2 seconds between frames to make it easy to spot for me.
Then every frame after that looks like this. It is after I print text that the color of the rectangles and line changes to what I do not expect.
I got two computers both running Windows 10, VS2019, and got Nvidia graphics cards. And both have the same problem. I even tried downgrading SDL to 2.0.9 but the same problem occur.