我正在做一个大项目,我想向用户展示看起来不错的信息。
所有我需要的是能够在里面绘制正方形和文字。
我画了文字和正方形,但我不能把它们放在一起。我甚至发现了问题所在的地方。它的功能是SDL_SetVideoMode(mwidth,mheight,bpp,flags)。
这个地方突出显示。提前谢谢!
using namespace std;
#define SPACING 0
TTF_Font *font=0;
int font_size=16;
SDL_Surface *screen;)
void draw_text_at(SDL_Surface *scr,char *msg, int x0, int y0)
{
int h;
SDL_Rect r;
SDL_Color fg={0,0,0,255};
h=TTF_FontLineSkip(font);
r.x=x0;
r.y=y0-h;
r.x=x0-TTF_GetFontOutline(font);
r.y+=h+SPACING-TTF_GetFontOutline(font);
SDL_Surface *surf=TTF_RenderText_Blended(font,msg,fg);
if(surf)
{ SDL_BlitSurface(surf,0,scr,&r);
SDL_FreeSurface(surf);
}
}
void window::handle_key_down(SDL_keysym * keysym)
{
switch(keysym->sym)
{
case SDLK_ESCAPE:
exit(0);
break;
default:
break;
}
}
void window::setup_opengl()
{
float ratio=(float)mwidth/(float)mheight;
glShadeModel(GL_SMOOTH);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
glEnable(GL_CULL_FACE);
glClearColor(1.0f,1.0f,1.0f,1.0f);
glViewport(0,0,mwidth,mheight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0,ratio,1.0,1024.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
}
void window::draw_screen()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); // set system of coordinates to the center of the screen
glTranslatef(0.0,0.0,-10.0);
glColor4f(0.0,1.0,1.0,1.0);
glBegin(GL_QUADS);
glVertex3f(-8, 0-2 ,0);
glVertex3f(-2+-8,0-2 ,0);
glVertex3f(-2+-8,-2-2,0);
glVertex3f(0+-8, -2-2,0);
glEnd();
glBegin(GL_QUADS);
glVertex3f(-8+3, 0-2 ,0);
glVertex3f(-2+-8+3,0-2 ,0);
glVertex3f(-2+-8+3,-2-2,0);
glVertex3f(0+-8+3, -2-2,0);
glEnd ();
SDL_GL_SwapBuffers( );
}
void window(int quantity_of_disks, hard_disk &disk)
{
if((SDL_Init(SDL_INIT_VIDEO)<0))
{printf("Could not initialize SDL: %s.\n", SDL_GetError());
exit(-1); //return -1;
}
if(TTF_Init()==-1)
{ printf("TTF_Init: %s\n", TTF_GetError());
exit(-2);
}
const SDL_VideoInfo *info=NULL;
int flags=0,bpp=0;
info=SDL_GetVideoInfo();
if(!info)
{ fprintf(stderr,"Video query failed");
SDL_Quit();
exit(0);
}
bpp=info->vfmt->BitsPerPixel;
SDL_GL_SetAttribute(SDL_GL_RED_SIZE,5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,5);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,5);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
下面
如果我将标志设置为零,我会看到文字
如果flag设置为SDL_OPENGL或SDL_OPENGLBLIT,我看到正方形
闪存转移到功能SDL_SetVideoMode 但我希望看到两者。我尝试改变它,但是......
flags= 0;
// SDL_ANYFORMAT | SDL_OPENGLBLIT | SDL_ASYNCBLIT
这一刻在这里:
screen = SDL_SetVideoMode(mwidth,mheight,bpp,flags);
if ( screen == NULL )
{ fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
exit(-2); //return -2;
}
SDL_WM_SetCaption ("MBR informant",NULL);
setup_opengl();
glMatrixMode(GL_MODELVIEW);
char *path_to_font="freefont-ttf/sfd/FreeMono.ttf";
load_font(path_to_font, font_size);
SDL_FillRect(screen,0,~0);
while (1)
{ draw_screen();
char *msg="we typed it";
draw_text_at(screen,msg,50,50);
SDL_Flip(screen);
SDL_Delay(1000);
process_events ();
}
SDL_Quit();
}
看起来像:
当变量flag = 0时
当变量flag = SDL_OPENGL时
由于代码很大,我只留下了与绘画有关的功能。 完整档案:http://rghost.net/37518422
解
感谢jrok我已经解决了这个问题: 我定义了矩形。 SDL_Rect rect; rect.x = 0; rect.y = 0; rect.w = 150; rect.h = 140;
我修改了一部分:
while (1)
{ draw_screen();
char *msg="we typed it";
draw_text_at(screen,msg,50,50);
**SDL_FillRect (screen,&rect,100);**
SDL_Flip(screen);
SDL_Delay(1000);
process_events ();
}
我得到了我想要的东西:
答案 0 :(得分:1)
您正在使用SDL_BlitSurface()
对文字进行blitting。究竟为什么它出错我不知道,但SDL维基清楚说(SDL_BlitSurface):
Like most surface manipulation functions in SDL, it should not be used together with OpenGL.
如果您希望坚持使用OpenGL,这个问题涵盖了渲染文本的一些可能性: