0

我正在做一个大项目,我想向用户展示看起来不错的信息。

我所需要的只是能够在里面绘制正方形和文本。

我画了文字和方块,但我不能把它们放在一起。我什至发现了一个问题所在。它是函数 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);

这里如果我将标志设置为零,我会看到文本
如果标志设置为 SDL_OPENGL 或 SDL_OPENGLBLIT ,我会看到方块

flas 被转移到函数 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();
     }

看起来像:

当变量标志 = 0 当变量标志 = 0

当变量标志 = SDL_OPENGL 在此处输入图像描述

由于代码量大,我只留下了与绘画有关的功能。完整文件:http ://rghost.net/37518422

解决方案

感谢 jrok 我已经解决了这个问题:我定义了矩形。SDL_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 ();
      }

我得到了我想要的: 在此处输入图像描述

4

1 回答 1

1

你正在用 .blitting 文本SDL_BlitSurface()。我不知道它到底为什么会出错,但 SDL wiki cleary 说(SDL_BlitSurface):

Like most surface manipulation functions in SDL, it should not be used together with OpenGL.

如果你想坚持使用 OpenGL,这个问题涵盖了渲染文本的一些可能性:

如何为 GUI 进行 OpenGL 实时文本渲染?

于 2012-04-10T20:30:20.757 回答