Hatena::ブログ(Diary)

C++でゲームプログラミング Twitter

2010-09-29

[][]boost::polygon::point_data ⇔ D3DXVECTOR2 続き

前回の続き。

geometry_concept、point_traits、point_mutable_traits の3つのクラスを特殊化すれば互換性の壁をぶっ壊せるらしい。

#include <assert.h>
#include <d3dx9.h>

#include <boost/polygon/polygon.hpp>

#include <boost/array.hpp>

#include <pstade/oven/copied.hpp>

namespace boost{ namespace polygon{

// D3DXVECTOR2 を boost::polygon で使用するための特殊化
template<>
struct geometry_concept<D3DXVECTOR2>{
    typedef point_concept type;
};

template<>
struct point_traits<D3DXVECTOR2>{
    typedef float coordinate_type;
    
    static inline coordinate_type get(const D3DXVECTOR2& point, orientation_2d orient) {
        return point[orient.to_int()]; 
    }
};

template<>
struct point_mutable_traits<D3DXVECTOR2>{
    typedef float coordinate_type;
    
    static inline void set(
        D3DXVECTOR3& point,
        orientation_2d orient,
        point_traits<D3DXVECTOR2>::coordinate_type value) {
        point[orient.to_int()] = value; 
    }
    static inline D3DXVECTOR2 construct(
        point_traits<D3DXVECTOR2>::coordinate_type x_value,
        point_traits<D3DXVECTOR2>::coordinate_type y_value) {
        return D3DXVECTOR2(x_value, y_value); 
    }
};

} }        // namespace boost{ namespace polygon{

int
main(){
    namespace oven = pstade::oven;
    namespace gtl = boost::polygon;
    
    D3DXVECTOR2    d3d_vec(1.0f, 2.0f);
    gtl::point_data<float>    point = d3d_vec;  // コピーされるよ
    assert(gtl::x(d3d_vec) == 1.0f);            // 関数も問題なく処理される

    boost::array<D3DXVECTOR2, 4> d2d_array = {
        D3DXVECTOR2(0.0f, 0.0),
        D3DXVECTOR2(1.0f, 0.0),
        D3DXVECTOR2(0.0f, 1.0),
        D3DXVECTOR2(0.0f, 0.0),
    };
    
    // 配列のコピーも問題ない
    std::vector<gtl::point_data<float> >  point_data_array = d2d_array|oven::copied;
    
//  d3d_vec = point;        // ただし逆の代入、てめーはダメだ!

    return 0;
}

これで、point_data 以外の型も処理することが出来るので使い勝手が良くなったんじゃなかろうかと。

代入や関数の処理も問題ないみたいです。

ただし、point_3d_data は、まだ未実装みたいです。

(各 traits は用意されてるけど代入部分が真っ白でした。)

あと逆の代入は無理なので、相変わらず自前で変換してやれないとダメみたい。


しかし、やろうとしてた事を先にやられてしまったのでまったく面白くないな!

コメントありがとうございます。


[boost]

ver 1.44.0

[参照]

http://www.boost.org/doc/libs/1_44_0/libs/polygon/doc/gtl_point_concept.htm

[][]boost::asio

ネットワーク通信を簡易に行えるすごいライブラリ

中の人がネットワークについてさっぱり理解していないので、これぐらいの認識です。

#include <iostream>

#include <boost/asio.hpp>

#include <boost/range/algorithm/for_each.hpp>

#include <pstade/oven/stream_lines.hpp>

int
main(){
	namespace asio = boost::asio;
	namespace oven = pstade::oven;
	
	// http://www.boost.org/LICENSE_1_0.txt を取得する
	asio::ip::tcp::iostream	tcp_iso("www.boost.org", "http");

	// 送信
	tcp_iso << "GET /LICENSE_1_0.txt HTTP/1.0\r\n";
	tcp_iso << "Host: www.boost.org\r\n";
	tcp_iso << "\r\n";
	tcp_iso << std::flush;
	
	// 受信
	boost::for_each(oven::stream_lines(tcp_iso), [](const std::string& str){
		std::cout << str << std::endl;
	});

	return 0;
}

出力:

HTTP/1.1 200 OK
Date: Wed, 29 Sep 2010 02:30:07 GMT
Server: Apache/2.0.52 (Red Hat)
Last-Modified: Fri, 28 Mar 2008 17:26:33 GMT
ETag: "110cc4a-53a-9a786440"
Accept-Ranges: bytes
Content-Length: 1338
Vary: Accept-Encoding
Cache-Control: max-age=3000000, public, must-revalidate
Expires: Fri, 31 Dec 2010 20:00:00 GMT
Connection: close
Content-Type: text/plain

Boost Software License - Version 1.0 - August 17th, 2003

Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:

The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

今回は、受信したデータをそのまま出力してるけど、本来であれば受信したデータに付いてるヘッダー情報なんかも解析しないとダメみたい。

そこら辺は、魔導書の『Boost.AsioによるHTTP通信入門』に詳しく載っているので、本格的に通信をやりたいなら読んでみると面白い。


しかし、こうやって触ってると俄然、サーバ間通信とかやってみたくなる。

WindowsLinuxなどの環境に左右されずに同じコーディングが出来るのがいいね。

例によってゲームの実機だとネットワーク周りの実装はどうなってるのかがすごく気になる。

まぁ最悪サーバ側だけでもこのライブラリで実装できるのかな?


[boost]

ver 1.44.0

[参照]

http://www.kmonos.net/alang/boost/classes/asio.html

[][]boost::polygon::point_3d_data<float> と D3DXVECTOR3 の変換

Boost.Polygonの謎 id:nagoya313:20100921

こんな感じ?

#include <iostream>
#include <d3dx9.h>

#include <boost/polygon/polygon.hpp>

#include <boost/array.hpp>
#include <boost/range/algorithm/for_each.hpp>
#include <boost/range/adaptor/transformed.hpp>

#include <pstade/oven/copied.hpp>

typedef boost::polygon::point_3d_data<float> point_3d_t;
typedef D3DXVECTOR3                          d3d_vec3_t;

// D3DXVECTOR3 -> boost::polygon::point_3d_data<float>
point_3d_t
d3d_vec3_to_point_3d(const d3d_vec3_t& src){
    return point_3d_t(src.x, src.y, src.z);
} 

// boost::polygon::point_3d_data<float> -> D3DXVECTOR3
d3d_vec3_t
point_3d_to_d3d_vec3(const point_3d_t& src){
    namespace gtl = boost::polygon;
    return d3d_vec3_t(gtl::x(src), gtl::y(src), gtl::z(src));
}

int
main(){    
    namespace oven = pstade::oven;
    namespace adaptors = boost::adaptors;
    namespace gtl = boost::polygon;
    
    boost::array<d3d_vec3_t, 4> d3d_array = {
        D3DXVECTOR3(0.0f, 0.0, 0.0f),
        D3DXVECTOR3(1.0f, 0.0, 0.0f),
        D3DXVECTOR3(0.0f, 1.0, 0.0f),
        D3DXVECTOR3(0.0f, 0.0, 1.0f),
    };

    std::vector<point_3d_t>    point_3d_array;

    // D3DXVECTOR3 -> polygon::point_3d_data
    point_3d_array = d3d_array
                   |adaptors::transformed(d3d_vec3_to_point_3d)  // 型を変換して
                   |oven::copied;                                // コピー
    
    // 出力
    boost::for_each(point_3d_array, [](const point_3d_t& pos){
        namespace gtl = boost::polygon;
        std::cout << gtl::x(pos) << ", "
                  << gtl::y(pos) << ", "
                  << gtl::z(pos) << std::endl;
    });
    
    // polygon::point_3d_data -> D3DXVECTOR3
    d3d_array = point_3d_array
              |adaptors::transformed(point_3d_to_d3d_vec3)
              |oven::copied;
    
    return 0;
}

出力:

0, 0, 0
1, 0, 0
0, 1, 0
0, 0, 1

adaptors::transformed を使って、ベクトルの型を変換している。

adaptors::transformed をもう1回使えば、バッファにコピーしないで直接、point_3d_data を操作できそう。

と、ここまで書いたんだけど Polygon で何が出来るのかいまいち理解していない。

個人的にあまりゲーム向けじゃない気がするんだけどどうなんだろう。

座標変換とかは全部 VRAM でやっちゃうし、当たり判定も物理エンジンの方がいい気がするし。

誰か日本語でまとめて。

今後の活躍に期待なのかなー。