void Box2dApp::setup()
{
    b2Vec2 gravity( 0.0f,0.1f );
	mWorld = new b2World( gravity );
    mWorld->SetContactListener(&myContactListenerInstance);
    mGround = ground(mWorld);
    
}
void Ex58AdvancedCollisionsApp::setup() {
    world = new b2World( b2Vec2( 0.0f, 10.0f ) );
    timeStep = 1.0f / 60.0f;
    velocityIterations = 8;
    positionIterations = 3;
    
    world->SetContactListener( &collisions );
    
    floor = new Boundary( world, 0.0f, getWindowHeight() - 50.0f, getWindowWidth(), 10.0f );
    
    Rand::randomize();
}
Exemple #3
0
int main()
{
    // Initialize globals
    initGlobals();

    // Window
    window.setSize(sf::Vector2u(window_width, window_height));
    window.setPosition(sf::Vector2i(200, 200));
    window.setFramerateLimit(FRAMES_PER_SECOND);
    //window.setVerticalSyncEnabled(true);
    window.setKeyRepeatEnabled(false);

    // Camera view
    sf::View windowView;

    // Menu
    initializeMenu();

    // UI
    ui.init();

    // Minimap
//    Minimap minimap;

    // Create & Set contactlistener
    BoxContactListener boxContactListener;
    physicsWorld.SetContactListener(&boxContactListener);

    // Build world
    //gameWorld.generateWorld(physicsWorld);

    // ------------------------------- MAIN LOOP -------------------------------
    while(window.isOpen())
    {
        // ------------------------------- Input & Views -------------------------------
        sf::Event event;
        gameEvents.processEvents(window, event);

        // Update view in case of window resize
        window_width = window.getSize().x;
        window_height = window.getSize().y;
        windowView.setSize(window_width, window_height);

//        if(minimap.updateViewThroughMinimap)
//        {
//            windowView.setCenter(minimap.newViewCenter.x, minimap.newViewCenter.y);
//        }

        if(player.hasNewFocus)
        {
            windowView.setCenter(player.getNewFocus().x, player.getNewFocus().y);
        }

        // Update normal view with inputs
        windowView = updateView(windowView);

        if(gameWorld.completionStarted && gameWorld.completionTimer.timeReached())
        {
            windowView.setCenter(window_width/2, window_height/2);
            global_levelComplete = false;
        }

        // Clear window
        window.clear(sf::Color(255, 255, 255, 255));

        if(global_isMenu)
        {
            ui.setSize(window_width, window_height);
            window.setView(ui.getView());

            menu_logo_bottom->setPosition(window_width - textures["menu_logo_bottom"].getSize().x, window_height - textures["menu_logo_bottom"].getSize().y);

            menu.update(gameEvents);
            menu.draw(window);

            // Instructions
            if(show_instructions)
            {
                showInstructions();

                if(show_instructions_controls)
                {
                    showControls();
                }

                if(show_instructions_gameplay)
                {
                    showGameplay();
                }
            }
        }
        else
        {
            window.setView(windowView);

            // ------------------------------- Updates -------------------------------

            // Player
            player.update(physicsWorld, gameEvents, gameWorld);

            if(!global_isPaused)
            {
                // Physics
                physicsWorld.Step(TIME_STEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS);

                // World
                gameWorld.update(gameEvents, physicsWorld, player);

                // UI
                ui.update(gameWorld, player);
            }


            // Calculate viewable area
            int viewShiftX = window.getSize().x/2 - window.getView().getCenter().x;
            int viewShiftY = window.getSize().y/2 - window.getView().getCenter().y;
            int windowMinX = -100 - viewShiftX;
            int windowMaxX = windowMinX + window_width + 200;
            int windowMinY = -100 - viewShiftY;
            int windowMaxY = windowMinY + window_height + 200;

            // ------------------------------- Drawing -------------------------------

            window.setView(ui.getView());
            // Background
            sf::Vertex rectangle[] =
            {
                sf::Vertex(sf::Vector2f(0.0f, 0.0f), sf::Color(0, 100, 130, 255)),
                sf::Vertex(sf::Vector2f(window_width, 0.0f), sf::Color(0, 100,130, 255)),
                sf::Vertex(sf::Vector2f(window_width, window_height), sf::Color(0, 200, 230, 255)),
                sf::Vertex(sf::Vector2f(0.0f, window_height), sf::Color(0, 200, 230, 255))
            };
            window.draw(rectangle, 4, sf::Quads);

            window.setView(windowView);

            // World & Player
            gameWorld.draw(window, b2Vec2(windowMinX, windowMinY), b2Vec2(windowMaxX, windowMaxY), false);
            player.draw(window);

            // HUD !!CLASS!!
            ui.setSize(window_width, window_height);
            window.setView(ui.getView());

            if(!gameWorld.completionTimer.timeReached() && gameWorld.completionStarted)
            {
                sf::Text temp("You have won", font_default, 40);

                int midX = window_width / 2.0f - 100;
                int midY = window_height / 2.0f - 40;

                temp.setPosition(midX, midY);
                temp.setColor(sf::Color::White);

                window.draw(temp);
            }

            if(global_isPaused)
            {
                sf::RectangleShape rect;
                rect.setSize(sf::Vector2f(window_width, window_height));
                rect.setPosition(0,0);
                rect.setFillColor(sf::Color(100, 100, 100, 155));

                window.draw(rect);
            }

            ui.draw(window);


    //        sf::RectangleShape border;
    //        border.setPosition(0.0f, window_height - (window_height*minimapSize + MINIMAP_BORDER_SIZE));
    //        border.setSize(sf::Vector2f(window_width*minimapSize + MINIMAP_BORDER_SIZE, window_height*minimapSize + MINIMAP_BORDER_SIZE));
    //        border.setFillColor(sf::Color(0, 0, 200, 255));
    //
    //        window.draw(border);
    //
    //        // ------------------------------- Minimap -------------------------------
    //
    //        minimap.setSize(MAX_WORLD_WIDTH * BOX_SIZE, MAX_WORLD_WIDTH * BOX_SIZE, window_width, window_height);
    //        minimap.calcViewport(minimapSize);
    //        minimap.setCameraPosition(windowView.getCenter().x, windowView.getCenter().y);
    //        window.setView(minimap.getUpdatedView());
    //
    //        minimap.update(gameEvents);
    //
    //        minimap.draw(window);
    //        gameWorld.draw(window, b2Vec2(-10000, -20000), b2Vec2(10000, 10000), true);
    //        player.draw(window);
    //        minimap.drawCameraWindow(window);

            // Default View again
            window.setView(windowView);
        }

        // Display
        window.display();
    }

    return 0;
}