1
至於說,我想顯示使用這裏的紋理轉換一個SDL_Surface文本轉換一個SDL_Surface文字是我的代碼:顯示器使用一個紋理
if (SDL_Init(SDL_INIT_VIDEO) == 0) {
//Create a surface
switch (method) {
case 2: // load a texture with SDL_Image:
{
surface = IMG_Load("../../../../../data/box3.png");
}
break;
case 3: // load a texture with SDL_TTF:
SDL_Color textColor={ 255, 255, 0, 1 };
if (TTF_Init() == 0){;
TTF_Font *font;
font = TTF_OpenFont("../../../../../data/Bedizen.ttf", 20);
if (font != NULL){
qDebug() << TTF_FontFaceFamilyName(font);
surface = TTF_RenderText_Solid(font, ".....", textColor);
}
else
qDebug() << "Error (Font) : " << TTF_GetError();
}
else
qDebug() << "Error (Font) : " << TTF_GetError();
break;
}
if (surface != NULL){
GLint nbOfColors;
GLenum texture_format = 0;
qDebug("surface : %dx%d/%dbpp/%d", surface->w, surface->h,
surface->format->BytesPerPixel, surface->pitch);
MemoryDump(surface->pixels, surface->pitch, surface->h, surface->format->BytesPerPixel);
// get the number of channels in the SDL surface
nbOfColors = surface->format->BytesPerPixel;
switch (nbOfColors) {
case 1:
texture_format = GL_ALPHA;
break;
case 3: // no alpha channel
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGB;
else
texture_format = GL_BGR;
break;
case 4: // contains an alpha channel
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGBA;
else
texture_format = GL_BGRA;
break;
default:
qDebug() << "Warning: the image is not truecolor...";
break;
}
glEnable(GL_TEXTURE_2D);
// Have OpenGL generate a texture object handle for us
glGenTextures(1, &texture);
// Bind the texture object
glBindTexture(GL_TEXTURE_2D, texture);
// Set the texture's stretching properties
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Edit the texture object's image data using the information SDL_Surface gives us
glTexImage2D(GL_TEXTURE_2D, 0, nbOfColors, surface->w, surface->h, 0,
texture_format, GL_UNSIGNED_BYTE, surface->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
else{
qDebug() << "Error (SDL) : " << SDL_GetError();
}
}
與IMG_LOAD工作得非常好()但沒有使用TTF_RenderText_Solid(),所以我設法通過自己找到它,起初我試圖從TTF_RTS()中得到的曲面不是很好的像素映射,但是我能夠找到答案,這要歸功於自制內存傾倒它是好的。
-1,過深的縮進。 – genpfault
謝謝你,幫助我這麼辛苦。但你是對的,我編輯了我的帖子:) –