下雪了

【UE4】加载材质、形状的方法

利用ConstructorHelpers::FObjectFinder模板来从UE4的文件引用中获取所需素材,然后对Actor、组件等进行设置。

以Tetris_UE中的IBlock为例,加载以下材质。

//加载形状
static ConstructorHelpers::FObjectFinder<UStaticMesh> Basic_Block(TEXT("StaticMesh'/Game/Shapes/Basic_Block.Basic_Block'"));
if (Basic_Block.Succeeded())
{
    IB_Left->SetStaticMesh(Basic_Block.Object);
    IB_Center->SetStaticMesh(Basic_Block.Object);
    IB_Right->SetStaticMesh(Basic_Block.Object);
    IB_RightRight->SetStaticMesh(Basic_Block.Object);
}

//加载材质
static ConstructorHelpers::FObjectFinder<UMaterial> IBlock_Material(TEXT("Material'/Game/Materials/M_IBlock.M_IBlock'"));
if (IBlock_Material.Succeeded())
{
    IB_Left->SetMaterial(0,IBlock_Material.Object);
    IB_Center->SetMaterial(0,IBlock_Material.Object);
    IB_Right->SetMaterial(0, IBlock_Material.Object);
    IB_RightRight->SetMaterial(0, IBlock_Material.Object);
}

如需在蓝图中调用,除了要加上宏 UFUNCTION(BlueprintCallable) 之外,参数类型应该是 UMaterialInterface* (材质接口)

//设定方块材质
void ABlock::SetBlockMaterial(UMaterialInterface* material)
{
    for (int i = 0; i < MeshList.Num(); i++)
    {
        MeshList[i]->SetMaterial(0, material);
    }
}

 

【Qt】窗口拖动代码

来自网上的博客,是通过鼠标相关的事件来实现的,封装起来很方便。

//Widget.h 
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

class QMouseEvent;
class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();

protected:
    //拖拽窗口
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);

private:
    bool        m_bDrag;
    QPoint      mouseStartPoint;
    QPoint      windowTopLeftPoint;
};

#endif // WIDGET_H
//Widget.cpp
#include <QMouseEvent>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , m_bDrag(false)
{
    setWindowTitle("窗口拖拽移动");
    setFixedSize(640, 480);
}

Widget::~Widget()
{

}

/* QPoint QMouseEvent::pos() const
    Returns the position of the mouse cursor, relative to the widget that received the event.
    If you move the widget as a result of the mouse event, use the global position returned by globalPos() to avoid a shaking motion.
 */

//拖拽操作
void Widget::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        m_bDrag = true;
        //获得鼠标的初始位置
        mouseStartPoint = event->globalPos();
        //mouseStartPoint = event->pos();
        //获得窗口的初始位置
        windowTopLeftPoint = this->frameGeometry().topLeft();
    }
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    if(m_bDrag)
    {
        //获得鼠标移动的距离
        QPoint distance = event->globalPos() - mouseStartPoint;
        //QPoint distance = event->pos() - mouseStartPoint;
        //改变窗口的位置
        this->move(windowTopLeftPoint + distance);
    }
}

void Widget::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        m_bDrag = false;
    }
}

 

【Boost】发送/接收HTML请求(Get/Post)

头文件:

#include <boost/asio.hpp>
#include <boost/beast.hpp>
#include <boost/locale.hpp>

具体步骤:

void conn()
{

    try
    {
        //三个基础参数
        string host="zofz.cf";
        string port="80";
        string target="/123/";

        //http版本
        int version = 11;

        //建立基础IO接口,并初始化resolver和socket
        boost::asio::io_service io_se;
        boost::asio::ip::tcp::resolver resolver{ io_se };
        boost::asio::ip::tcp::socket socket{ io_se };

        //设定GET/POST请求
        boost::asio::streambuf request;
        std::ostream request_stream(&request);

        //创建http头,注意格式
        request_stream << "GET " << target << " HTTP/1.1 \r\n";
        request_stream << "Host: " << host << "\r\n";
        request_stream << "Accept: application/json \r\n";
        request_stream << "Accept-Encoding: gzip, deflate, br \r\n";
        request_stream << "Conection: keep-alive \r\n";
        request_stream << "Content-Type: application/x-www-form-urlencoded \r\n";
        request_stream << "User-Agent: " << ua << " \r\n";

        // 发送request
        write(socket, request);

        //声明一个容器来保存响应
        boost::beast::flat_buffer buf;
        response<string_body> resp;
        stringstream ss;

        //接收容器
        read(socket, buf, resp);
        
        //遍历Header
        for(auto &i:resp)
        {
            cout<<i.name()<<i.value()<<endl;
        }
        
        //输出body
        cout<<resp.body()<<endl;
    }
    catch (std::exception const& e)
    {
        //输出错误
        cout<< e.what()<<);
    }
}

 

【Boost】对JSON的操作

头文件:

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

用法:

void encode()
{
    /*
    将形如:
    {
        "data":
        {
            "key":"value";
        }
    }
    的JSON文本转为string字符串
    */
    
    ptree root, data;
    stringstream ss;
    
    //创建data
    data.put("key", "value");
    
    //放入data
    root.put_child("data", data);
    
    //将JSON文本写入字符流
    write_json(ss, root);
    
    //读出字符串
    string json = ss.str();
}



void decode(string json)
{
    /*
    解析形如
    {
        "data":
        {
            "key":"value";
        }
    }
    的JSON文本到string中
    */
    ptree root, data;
    
    //将字符串读入字符流
    stringstream ss(json);
    
    //读出JSON格式的文本
    read_json(ss, root);
    
    //分离出data
    data = root.get_child("data");
    
    //分离出key,value
    string value = data.get & lt; string& gt; ("key");
}