我正在做一個大項目,我想向用戶顯示看起來不錯的信息。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,我看到廣場
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
時可變標誌= SDL_OPENGL
由於代碼的大小,我只留下了與繪畫相關的功能。 全部文件:http://rghost.net/37518422
SOLUTION
感謝羅我已經解決了這個問題,所以: 我定義的矩形。 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();
}
,我得到了我想要的東西:
如果沒有Opengl,使用SDL函數是否沒有辦法繪製圖形?對我來說繪製文本並不容易,所以我很樂意在SDL中做所有事情。我真的迷失了方向。對我來說這似乎很簡單:只是方塊和文字,但是.. – Tebe 2012-04-10 21:01:42
@shbk您可以將圖片保存在圖像文件中,加載它們並用SDL將它們抹掉。瀏覽這個有點:http://wiki.libsdl.org/moin.cgi/CategorySurface簡單的矩形也可以用'SDL_FillRect()'繪製。 – jrok 2012-04-10 21:05:38
是啊! SDL_FillRect()是我的選擇。因爲據我瞭解SDL庫不是爲了好玩而編寫的,而是爲了讓編程更容易,更加交叉平臺化。這不是我們的問題嗎?謝謝!我想沒有你,我會在幾天內找到這個解決方案,但我不確定我能否得到它 – Tebe 2012-04-10 22:13:47