2012-04-10 7 views
0

큰 프로젝트를 수행하고 있으며 사용자에게 멋진 정보를 보여주고 싶습니다.C++ SDL 함께 텍스트와 그림 그리기

내가 필요로하는 모든 것은 내부에 정사각형과 텍스트를 그릴 수 있어야합니다.

텍스트와 사각형을 그렸지만 함께 놓을 수는 없습니다. 나는 문제가있는 곳을 발견했다. 함수 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로 설정되어있는 경우 , 나는 사각형

FLA는이 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 when variable flag = 0

변수 플래그 = SDL_OPENGL enter image description here

코드의 크기가 크기 때문에 그림과 관련된 기능 만 남겼습니다. 전체 파일 : http://rghost.net/37518422

솔루션

덕분에 내가 지금이 문제를 해결했습니다 '제록합니다 : 나는 사각형을 정의했다. 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(); 
     } 

을하고 내가 원하는 걸 얻었 : 당신은 SDL_BlitSurface()와 텍스트를 블리 팅하고 enter image description here

답변

1

. 그것이 잘못되면 정확히 왜 (SDL_BlitSurface을) 잘 모르겠지만, SDL 위키 클리 어리는 말한다 :

:

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

당신이 OpenGL을 고수하고자하는 경우는,이 ​​질문은 렌더링 텍스트에 대한 몇 가지 가능성을 다룹니다 How to do OpenGL live text-rendering for a GUI?

+0

OpenGL을 사용하지 않고 SDL 기능을 사용하여 그림을 그리는 방법이 없습니까? 텍스트를 그리는 것이 쉽지 않았기 때문에 SDL에서 모두 할 수있어서 기뻤습니다. 나는 정말로 길을 잃었다. 내게 너무 단순 해 보였습니다. 정사각형과 텍스트지만 .. – Tebe

+1

@shbk 이미지 파일에 그림을 저장하고로드 한 다음 SDL로 블릿 할 수 있습니다.http://wiki.libsdl.org/moin.cgi/CategorySurface 간단한 사각형은'SDL_FillRect()'로 그릴 수 있습니다. – jrok

+0

예! SDL_FillRect()가 제 선택입니다. SDL 라이브러리는 단지 재미로 작성된 것이 아니기 때문에 프로그래밍이 더 쉽고 더 많은 플랫폼이되도록하기 위해서였습니다. 우리 문제가 아닌가? 감사! 나는 당신이 없으면이 해결책을 아직 며칠 발견 할 것이고 나는 그것을 전혀 얻을 수 없을 것이라고 확신하지 못한다. – Tebe