Beispiel #1
0
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    // Creating window
    WNDCLASSEX wcex;
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    if (!RegisterClassEx(&wcex)) {
        return 1;
    }
    hInst = hInstance;
    hWnd = CreateWindow(
               szWindowClass,
               szTitle,
               WS_OVERLAPPEDWINDOW,
               CW_USEDEFAULT, CW_USEDEFAULT,
               800, 600,
               NULL,
               NULL,
               hInstance,
               NULL
           );
    if (!hWnd) {
        return 1;
    }
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    // Win32 init finished

    contextPtr = r2d::CreateRenderContext(hWnd);
    contextPtr->Init();

    factoryPtr = contextPtr->GetFactory();
    cameraPtr = factoryPtr->CreateCamera(Rect(-400.0f, -300.0f, 400.0f, 300.0f));


    // Init graphics
    //std::auto_ptr<r2d::IMaterial> graphicMaterialPtr = factoryPtr->CreateMaterial(

    //)
    //factoryPtr->CreateGraphicObject(

    // Init physics
    p2d::Material material;
    p2d::BodyInitialMotion motion;

    Polygon2d polygon1(Rect(-300.0f, -50.0f, 300.0f, 0.0f));
    ;
    Polygon2d polygon2;

    polygon2 << vec2(-200.0f, -200.0f) << vec2(-180.0f, -300.0f) << vec2(-100.0f, -250.0f) << vec2(0.0f, -300.0f) << vec2(100.0f, -100.0f) << vec2(200.0f, -250.0f);

    polygon2 += vec2(-200.0f, 90.0f);

    motion.m_angle = 0.2f;
    physicsBodies.push_back(
        std::auto_ptr<p2d::IBody>(new p2d::SimpleBody(
                                      *physicsWorld,
                                      std::auto_ptr<p2d::IShape>(new p2d::PolygonShape(polygon1, material)),
                                      p2d::IBody::ptStatic,
                                      material,
                                      motion
                                  ))
    );
    motion.m_angle = 0.0f;

    physicsBodies.push_back(
        std::auto_ptr<p2d::IBody>(new p2d::SimpleBody(
                                      *physicsWorld,
                                      std::auto_ptr<p2d::IShape>(new p2d::ChainShape(polygon2, material)),
                                      p2d::IBody::ptStatic,
                                      material,
                                      motion
                                  ))
    );

    material.m_restitution = 0.5f;
    motion.m_position = vec2(0.0f, 300.0f);
    physicsBodies.push_back(
        std::auto_ptr<p2d::IBody>(new p2d::SimpleBody(
                                      *physicsWorld,
                                      std::auto_ptr<p2d::IShape>(new p2d::CircleShape(Circle(40.0f, vec2(0.0f, 0.0f)), material)),
                                      p2d::IBody::ptDynamic,
                                      material,
                                      motion
                                  ))
    );

    material.m_restitution = 1.0f;
    motion.m_position = vec2(100.0f, 300.0f);
    physicsBodies.push_back(
        std::auto_ptr<p2d::IBody>(new p2d::SimpleBody(
                                      *physicsWorld,
                                      std::auto_ptr<p2d::IShape>(new p2d::CircleShape(Circle(10.0f, vec2(0.0f, 0.0f)), material)),
                                      p2d::IBody::ptDynamic,
                                      material,
                                      motion
                                  ))
    );

    physicsJoints.push_back(
        std::auto_ptr<p2d::IJoint>(new p2d::LengthJoint(
                                       *physicsWorld,
                                       physicsBodies[2],
                                       physicsBodies[3],
                                       physicsBodies[2].GetPosition() + vec2(0.0f, 15.0f),
                                       physicsBodies[3].GetPosition() + vec2(0.0f, 5.0f)
                                   ))
    );
    //



    //
    //boost::thread* renderThread = new boost::thread(renderScene);
    // Dispatching messages
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
        renderScene();
    }
    complete = true;
    //renderThread->join();

    contextPtr->Deinit();


    // Exit
    return (int) msg.wParam;
}