Hi, I have problem with setting font color. When I set white color, I see white color, when I set red color, I see blue color and so it works with all colors except white! How can I fix this error?
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_opengl.h>
#include <string>
SDL_Window *window;
SDL_Event event;
SDL_Surface *surface;
GLuint textureID=0;
TTF_Font *font;
SDL_Color color={250, 0, 0, 0};
void loadFont(std::string path, int size){
font=TTF_OpenFont(path.c_str(), size);
}
void setText(std::string text){
surface=TTF_RenderText_Blended(font, text.c_str(), color);
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0,
GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
void display(){
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glTranslatef(0, 0-surface->h/4, 0);
glBindTexture(GL_TEXTURE_2D, textureID);
glBegin(GL_POLYGON);
glTexCoord2f(0, 0); glVertex2f(0, 0);
glTexCoord2f(1, 0); glVertex2f(surface->w, 0);
glTexCoord2f(1, 1); glVertex2f(surface->w, surface->h);
glTexCoord2f(0, 1); glVertex2f(0, surface->h);
glEnd();
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}
int main(){
TTF_Init();
window=SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 800, SDL_WINDOW_OPENGL);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GLContext context=SDL_GL_CreateContext(window);
SDL_GL_SetSwapInterval(SDL_FALSE);
loadFont("sans.ttf", 24);
setText("blablabla");
while(window!=NULL){
while(SDL_PollEvent(&event)){
if(event.type==SDL_QUIT)
window=NULL;
if(event.key.keysym.scancode==SDL_SCANCODE_ESCAPE)
window=NULL;
}
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, 800, 800);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 800, 800, 0, -10, 10);
display();
SDL_GL_SwapWindow(window);
}
}