SDL_TTF和OPENGL协同工作时出现问题
Trouble having SDL_TTF and OPENGL work together
对于我正在开发的游戏,我希望在很多图形中使用OpenGL,在文本中使用SDL_TTF
。我可以让两个人同时上班,但不能同时上班。这是我的代码(基于Lazy Foo):
#include "SDL/SDL.h"
#include "SDL/SDL_ttf.h"
#include "GL/gl.h"
const bool useOpenGl = true;
//The surfaces
SDL_Surface *message = NULL;
SDL_Surface *screen = NULL;
//The event structure
SDL_Event event;
//The font that's going to be used
TTF_Font *font = NULL;
//The color of the font
SDL_Color textColor = {255, 255, 255};
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL)
{
//Holds offsets
SDL_Rect offset;
//Get offsets
offset.x = x;
offset.y = y;
//Blit
SDL_BlitSurface(source, clip, destination, &offset);
}
bool init()
{
SDL_Init (SDL_INIT_EVERYTHING);
if (useOpenGl)
{
screen = SDL_SetVideoMode (1280, 720, 32, SDL_SWSURFACE | SDL_OPENGL); //With SDL_OPENGL flag only opengl is sceen, without only text is
} else {
screen = SDL_SetVideoMode (1280, 720, 32, SDL_SWSURFACE);
}
TTF_Init();
SDL_WM_SetCaption ("TTF Not Working With OpenGL", NULL);
if (useOpenGl)
{
glClearColor(1.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, screen->w, screen->h, 1.0, -1.0, 1.0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
return true;
}
bool load_files()
{
font = TTF_OpenFont ("arial.ttf", 28);
return true;
}
void clean_up()
{
SDL_FreeSurface (message);
TTF_CloseFont (font);
TTF_Quit();
SDL_Quit();
}
int main(int argc, char* args[])
{
//Quit flag
bool quit = false;
init();
load_files();
if (useOpenGl)
{
glClear(GL_COLOR_BUFFER_BIT); //clearing the screen
glPushMatrix();
glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(0, 0);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(1280, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex2f(1280, 720);
glColor4f(0.5, 0.5, 1.0, 0.1);
glVertex2f(0, 720);
glEnd();
glPopMatrix();
glFlush();
}
//Render the text
message = TTF_RenderText_Solid (font, "The quick brown fox jumps over the lazy dog", textColor);
//Apply the images to the screen
apply_surface (0, 150, message, screen);
//I'm guessing this is where the problem is coming from
SDL_GL_SwapBuffers();
SDL_Flip (screen);
while (quit == false)
{
while (SDL_PollEvent (&event))
{
if (event.type == SDL_QUIT)
{
quit = true;
}
}
}
clean_up();
return 0;
}
如果变量useOpenGl
设置为false,程序将仅使用SDL_TTF
,如果设置为true,则将同时使用SDL_TTF
和OpenGL。从玩它的角度来看,问题似乎在于我在创建窗口时是否使用"SDL_OPENGL"标志。
您可能需要查找另一个库,如FTGL或freetypegl。
相关文章:
- 我不明白参数和参数如何协同工作
- 如何使这个 Foor 循环在这个问题中工作?
- Qt C++括号匹配和突出显示当前线路功能无法协同工作
- scanf() 语句中"%*[^n]"的格式字符串指示什么?分配抑制器 (*) 和否定扫描集 ([^) 如何协同工作?
- Emrun 和无头铬 - 如何让这两者协同工作?
- 如何编译Freetype(2)和Harfbuzz(带有Visual studio)使它们协同工作?
- 模板类实例化如何与类继承协同工作
- 如何将RegisterDragDrop,RoInitialize组合在一个线程中协同工作
- 如何让助推upgrade_to_unique_lock+condition_variable协同工作
- Fruchterman-Reingold的吸引力是如何与Boost图库协同工作的
- 如何使Ws2_32.lib与针对Windows 8.1的VS 2013项目协同工作
- 如何在C++中使一个void函数与另一个void功能协同工作
- std::transform 和 std::p lus 如何协同工作
- 无法让 cgal 和 hdf5 协同工作
- std::ref 和交换功能似乎不能很好地协同工作
- 类的详细信息:设计一组协同工作的类时出错
- 如何使这两个模板类协同工作?(C++中的属性)
- 有什么方法可以让谷歌测试框架与Visual Studio 6协同工作吗
- c++如何使双语句sql与OLEDB协同工作
- SDL_TTF和OPENGL协同工作时出现问题