Example #1
0
void Game::draw()
{
    al_clear_to_color(al_map_rgb(0, 0, 0));

    // draw player
    float x1 = m_player->getX();
    float x2 = x1 + (m_player->getWidth() * PLAYER_WIDTH);
    float y1 = m_player->getY();
    float y2 = y1 + (m_player->getHeight() * PLAYER_HEIGHT);
    al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(0, 255, 0));

    float playerX = x1;

    // move camera
    if (playerX <= SCREEN_WIDTH / 2)
        //camX = 0;
        ;
    else if (playerX >= m_levelWidth - SCREEN_WIDTH / 2)
        m_camX = m_levelWidth - SCREEN_WIDTH;
        //;
    else
    {
        m_camX = playerX - SCREEN_WIDTH / 2;
    }
    ALLEGRO_TRANSFORM trans;
    al_identity_transform(&trans);
    al_translate_transform(&trans, -m_camX, 0);
    al_use_transform(&trans);

    for (list<StationaryObject*>::iterator it = m_stationaryobjects.begin(); it != m_stationaryobjects.end(); it++)
    {
        StationaryObject* current = *it;
        int width = current->getWidth() * TILE_WIDTH;
        int height = current->getHeight() * TILE_HEIGHT;
        float x1 = current->getX();
        float x2 = x1 + width;
        float y1 = current->getY();
        float y2 = y1 + height;

        if (x2 >= m_camX || x1 >= m_camX)
        {
            char tile = current->getTile();
            switch (tile)
            {
            case 'w':
                al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 0, 0));
                break;
            case 's':
                al_draw_filled_rectangle(x1, y1, x2, y2, al_map_rgb(255, 255, 0));
                break;
            }
        }
    }

    al_flip_display();
}