我怀疑sdl严重在坑我!!
sdlgui吧
全部回复
仅看楼主
level 11
输出文字怎么都不起效。。。运行4个succeed但就是打印不出来
#include <SDL2/SDL.h>
#include <iostream>
#include <SDL2/SDL_ttf.h>
using namespace std;
SDL_Window*win=SDL_CreateWindow("test",800,80,600,600,SDL_WINDOW_SHOWN);
SDL_Renderer*ren=SDL_CreateRenderer(win,-1,SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC);
int main(int argc,char**argv)
{
if(ren==NULL)
cout<<"fail"<<endl;
else
cout<<"succeed"<<endl;
TTF_Init() ;
SDL_Color color = { 255, 255, 255 };
SDL_Rect pos;
pos.x=300,pos.y=300;
TTF_Font *font =TTF_OpenFont("font_a.ttf",36);
if(font==NULL)
cout<<"fail"<<endl;
else
cout<<"succeed"<<endl;
SDL_Surface *surf = TTF_RenderText_Solid(font,"hello world",color);
if(surf==NULL)
cout<<"fail"<<endl;
else
cout<<"succeed"<<endl;
SDL_Texture *texture = SDL_CreateTextureFromSurface(ren, surf);
if(texture==NULL)
cout<<"fail"<<endl;
else
cout<<"succeed"<<endl;
SDL_RenderCopy(ren ,texture,NULL,&pos);
SDL_RenderPresent(ren);
SDL_Delay(2000);
SDL_FreeSurface(surf);
TTF_CloseFont(font);
return 0;
}
@twtfcu3 我已经折腾了一天了。。。仍然是第一次打开花屏,以后运行黑屏[转圈哭]
2014年06月28日 14点06分 1
level 11
2014年06月28日 14点06分 2
level 10
SDL_RenderClear(ren);
SDL_RenderCopy(ren ,texture,NULL,NULL);
SDL_RenderPresent(ren);
SDL_Delay(2000);
2014年06月28日 14点06分 3
很完美的又黑屏了[心碎]
2014年06月28日 14点06分
level 10
2014年06月28日 14点06分 4
level 10
#include <SDL2/SDL.h>
#include <iostream>
#include <SDL2/SDL_ttf.h>
using namespace std;
int main(int argc,char**argv)
{
SDL_Window*win=SDL_CreateWindow("test",800,80,600,600,SDL_WINDOW_SHOWN);
SDL_Renderer*ren=SDL_CreateRenderer(win,-1,SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC);
if(ren==NULL)
cout<<"fail"<<endl;
else
cout<<"succeed"<<endl;
TTF_Init() ;
SDL_Color color = { 255, 255, 255 };
SDL_Rect pos;
pos.x=300,pos.y=300;
TTF_Font *font =TTF_OpenFont("aa.ttf",36);
if(font==NULL)
cout<<"fail"<<endl;
else
cout<<"succeed"<<endl;
SDL_Surface *surf = TTF_RenderText_Solid(font,"hello world",color);
if(surf==NULL)
cout<<"fail"<<endl;
else
cout<<"succeed"<<endl;
SDL_Texture *texture = SDL_CreateTextureFromSurface(ren, surf);
if(texture==NULL)
cout<<"fail"<<endl;
else
cout<<"succeed"<<endl;
SDL_RenderClear(ren);
SDL_RenderCopy(ren ,texture,NULL,NULL);
SDL_RenderPresent(ren);
SDL_Delay(2000);
SDL_FreeSurface(surf);
TTF_CloseFont(font);
return 0;
}
2014年06月28日 14点06分 5
我把字体重命名为aa.ttf了,你要注意一下
2014年06月28日 14点06分
level 11
还是不行。。。[转圈哭]
会不会是显卡的原因呢?我会出现花屏。
2014年06月28日 15点06分 6
2014年06月28日 15点06分
不清楚。。。。。。。反正SDL2的渲染系统不稳定,至少我现在还没完全使用SDL_Renderer
2014年06月28日 15点06分
你使用的是2.( )版的SDL2
2014年06月28日 15点06分
回复 twtfcu3 :我用的是你传的sdl for codeblocks上的。
2014年06月28日 15点06分
level 3
建议你把
SDL_Window*win=SDL_CreateWindow("test",800,80,600,600,SDL_WINDOW_SHOWN);
SDL_Renderer*ren=SDL_CreateRenderer(win,-1,SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC);
放到main函数中。
main函数的所有程序执行的开始,是入口。
如果上面的两句不放在main里,就是没有执行过,也就是说win和ren没有初始化,是空的。如此,你说它会显示窗口吗?
我帮你修改了下,运行结果如下:
完整代码:
int main(int argc, char**argv)
{
SDL_Init(SDL_INIT_EVERYTHING);//你连SDL都没初始化
TTF_Init();
SDL_Window*win = SDL_CreateWindow("test", 800, 80, 600, 600, SDL_WINDOW_SHOWN);
SDL_Renderer*ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (ren == NULL || win==NULL)//两个都测试吧
cout << "无法创建窗口或渲染器" << endl;
else
cout << "成功创建窗口或渲染器" << endl;
SDL_Color color = { 255, 0, 0 };//红色
SDL_Rect pos;
pos.x = 100, pos.y = 100,pos.w=400,pos.h=100;//四个成员都要设定,不能只设置部分,出错的部分在此
TTF_Font *font = TTF_OpenFont("C:\\Windows\\Fonts\\msyh.ttc", 36);
//注意字体路径的完整性和书写
if (font == NULL)
cout << "字体打开失败" << endl;
else
cout << "字体打开成功" << endl;
SDL_Surface *surf = TTF_RenderText_Solid(font, "hello world", color);
if (surf == NULL)
cout << "fail" << endl;
else
cout << "succeed" << endl;
SDL_Texture *texture = SDL_CreateTextureFromSurface(ren, surf);
if (texture == NULL)
cout << "fail" << endl;
else
cout << "succeed" << endl;
SDL_RenderCopy(ren, texture, NULL, &pos);
SDL_RenderPresent(ren);
SDL_Delay(2000);
getchar();//控制台暂停
SDL_DestroyTexture(texture);//纹理也要销毁
SDL_FreeSurface(surf);
TTF_CloseFont(font);
return 0;
}
2014年06月28日 22点06分 8
其实是字体文件出了错误[勉强]
2014年06月29日 03点06分
1