SFML基础

原文地址:https://www.cnblogs.com/karl07/p/10285692.html

 

(1) 窗口和交互

创建一个新窗口:

sf::RenderWindow window(sf::VideoMode(500,500),"new window");

但是光创建一个窗口并不能显示

还要加一个循环

复制代码

    while (window.isOpen()){
        sf::Event event;  //接受窗口事件
        while (window.pollEvent(event)){
            if (event.type == sf::Event::Closed){ // 检测关闭
                window.close();
            }
        }
    }

复制代码

然后就能看到一个黑色的窗口了

Event是一个union 可以通过 event.type 来判断

具体可以参考官网

 

键盘鼠标交互:

鼠标的操作信息可以通过event来检测

复制代码

void check_mouse(const sf::Event &event)
{
    if (event.type == sf::Event::MouseButtonPressed){ //检测鼠标 输出鼠标坐标
        if (event.mouseButton.button == sf::Mouse::Right){
            std::cout << event.mouseButton.x << std::endl;
            std::cout << event.mouseButton.y << std::endl;
        }
    }

    if (event.type == sf::Event::MouseButtonReleased){ //检测鼠标释放
        std::cout << "realease" << std::endl;
    }

}

复制代码

键盘的话一种是类似于鼠标的方式通过event检测

另外一种就是直接检测当前键有没有按下

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){ //检测键盘信息 上键是否按下
    std::cout << "up\n";
}

(2) 图像和音频

精灵

精灵(sf::Sprite)就是截取纹理(sf::Texture)的一块 或者重复纹理贴图

初始化精灵和纹理的一些方法:

复制代码

sf::Sprite init_sprite(const sf::Texture & tex){
    sf::Sprite spr;
    spr.setTexture(tex);//设置纹理
    spr.setTextureRect(sf::IntRect(0,0,100,100));//选择纹理区域
    spr.setColor(sf::Color(255,0,0,120));//设置颜色透明度
    spr.setPosition(sf::Vector2f(100,100));//设置位置
    spr.setRotation(90);//旋转
    spr.setScale(sf::Vector2f(2,2));//设置大小
    spr.setOrigin(sf::Vector2f(50,50));//设置中心点
    return spr;
}

sf::Texture init_texture(const std::string &s){
    sf::Texture tex;//纹理
    if (tex.loadFromFile(s)) std::cout << "texture success\n";//打开图片作为纹理 s 为图片路径
    tex.setSmooth(true); //平滑
    tex.setRepeated(false); //重复 当选择的区域大于图片时是否重复
    return tex;
}

复制代码

初始化之后可以对精灵进行操作

    spr.move(sf::Vector2f(1,1));//移动
    spr.rotate(1);//旋转
    spr.scale(sf::Vector2f(0.9,0.9));//大小

默认的旋转中心在精灵的左上角 可以通过setOrigin来改变

注意:精灵和对应的纹理要存在于同一个生命周期

通过window.draw()可以在屏幕上显示精灵

auto tex = init_texture("tex.png");
auto spr = init_sprite(tex); 
window.draw(spr);

文字

文字(sf::Text)和精灵相似需要用字体(sf::Font)来初始化:

复制代码

sf::Text init_text(const std::wstring & s,const sf::Font & font){
    sf::Text text;
    text.setString(s); //设置字符串
    text.setFont(font); //设置字体
    text.setCharacterSize(36); //文字大小
    text.setFillColor(sf::Color::Blue); //颜色
    text.setStyle(sf::Text::Bold | sf::Text::Underlined | sf::Text::Italic | sf::Text::StrikeThrough);
    //属性
    return text;
}

sf::Font init_font(const std::string & s){
    sf::Font font;
    if (font.loadFromFile(s)) std::cout << "font success\n";
    return font;
}

复制代码

如果要显示中文字符的话要用宽字符串(wstring)

文字的显示也和精灵相似

auto font = init_font("font.ttf");
auto text = init_text(L"hello world!啦啦啦",font); 
window.draw(text);

声音

声音(sf::Sound)的加载方式也是类似的,要加载(sf::SoundBuffer)

不过Sound是不可复制的

sf::SoundBuffer init_buffer(const std::string & s){
    sf::SoundBuffer buf;
    if (buf.loadFromFile(s)) std::cout << "buffer success\n";
    return buf;
}
    sf::SoundBuffer buf = init_buffer("buf.wav");
    sf::Sound sou;
    sou.setBuffer(buf);
    sou.play(); // 播放音频

另外还有一个sf::Music是用来加载比较长的音乐

 

(3) 视角和碰撞检测

视角

window.draw();所画出的对象是在世界坐标的绝对位置。

视角可以选定在窗口中显示世界坐标下的的哪一个区域。

sf::View init_view (){
    sf::View vi(sf::Vector2f(0,0),sf::Vector2f(1000,1000));// 设置视角的中心和大小
    vi.setViewport(sf::FloatRect(0,0,0.9,0.9)); // 设置视角在窗口的相对位置 (起始x,起始y,x大小,y大小)
    return vi;
}
    auto vi = init_view();
    window.setView(vi);
    window.draw(text);
    window.draw(spr);

注意:要先setview再draw这样才能出现在选定视角里面

 

碰撞检测

碰撞检测是通过检测两个物体的外接的矩形是否相交来检测的

template<typename T1,typename T2>
bool check_collision(const T1 &a,const T2 &b){ //碰撞检测
    sf::FloatRect box1 = a.getGlobalBounds();
    sf::FloatRect box2 = b.getGlobalBounds();
    return box1.intersects(box2);
}
  • 11
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值