summaryrefslogtreecommitdiffstats
path: root/chicken-wranglers/src/main/game.cpp
blob: e168503a8a506f836f8456b3b361cec12550a74c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/****************************************************************************
**
** This file is a part of QtChickenWranglers.
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).*
** All rights reserved.
** Contact:  Nokia Corporation (qt-info@nokia.com)
**
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in the
**     documentation and/or other materials provided with the distribution.
**
**   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
**     the names of its contributors may be used to endorse or promote
**     products derived from this software without specific prior written
**     permission.
**
**  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
**  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
**  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
**  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
**  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
**  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
**  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
**  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
**  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
**  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
**  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
**  POSSIBILITY OF SUCH DAMAGE."
**
****************************************************************************/


#include <QtDeclarative>
#include <QtGui/QApplication>
#include <QtOpenGL/QGLWidget>

#include "charactermodel.h"
#include "connectivitylistmodel.h"
#include "global.h"
#include "settings.h"
#include "soundmanager.h"
#include "gameclient.h"
#include "gamehost.h"
#include "soundmanager.h"

#include "game.h"

Game::Game(const GraphicsMode &mode, QObject *parent)
    : QObject(parent), m_gameClient(0), m_gameHost(0),
      m_connectivity("bluetooth"), m_mode(None)
{
    qmlRegisterType<Global>("game.types", 1, 0, "Global");
    qmlRegisterType<Game>("game.types", 1, 0, "Game");
    qmlRegisterType<GameClient>("game.types", 1, 0, "GameClient");
    qmlRegisterType<GameHost>("game.types", 1, 0, "GameHost");
    // FIXME: Register this model later when GameClient is created.
    qmlRegisterType<ConnectivityListModel>("game.types", 1, 0, "ConnectivityListModel");

    m_view = new QDeclarativeView;
    if (mode == OpenGL) {
        //XXX: for some reason, setting the GLWidget is not enabling
        //ogl in Symbian
        if (Global::environment() == Global::Symbian) {
            QApplication::setGraphicsSystem("opengl");
        } else {
            m_view->setViewport(new QGLWidget);
        }
    } else {
        QApplication::setGraphicsSystem("raster");
    }

    m_view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
    m_view->rootContext()->setContextProperty("game", this);
    m_view->rootContext()->setContextProperty("settings", Settings::instance());
}

Game::~Game()
{
    delete m_gameHost;
    delete m_gameClient;
    delete m_view;
}

void Game::setMode(Mode mode)
{
    if (mode == ClientMode)
        startClientMode();
    else if (mode == HostMode)
        startHostMode();
}

void Game::startClientMode()
{
    m_mode = ClientMode;
    if (m_gameClient) {
        emit modeChanged();
        return;
    }

    if (!m_gameClient) {
        m_gameClient = new GameClient(m_view->rootContext(), m_connectivity, this);
        m_view->rootContext()->setContextProperty("gameClient", m_gameClient);
        connect(m_gameClient, SIGNAL(ended()), this, SLOT(onClientModeEnded()));
    } else
        ;//TODO: client has not a way to update the connectivity

    emit modeChanged();
}

void Game::startHostMode()
{
    m_mode = HostMode;
    if (m_gameHost) {
        m_gameHost->setConnectivity(m_connectivity);
        emit modeChanged();
        return;
    }

    if (!m_gameHost) {
        m_gameHost = new GameHost(m_view->rootContext(), m_connectivity, this);
        m_view->rootContext()->setContextProperty("gameHost", m_gameHost);
        connect(m_gameHost, SIGNAL(ended()), this, SLOT(onHostModeEnded()));
    }


    emit modeChanged();
}

void Game::start(const QSize &displaySize)
{
#ifdef CW_DEBUG
    m_view->setSource(Global::rootDirectory().absoluteFilePath() + "/src/qml/main.qml");
#else
    m_view->setSource(QUrl("qrc:/main.qml"));
#endif

    QObject::connect(m_view->engine(), SIGNAL(quit()), m_view, SLOT(close()));

    if (displaySize.isValid()) {
        m_view->resize(displaySize);
        m_view->show();
    } else {
        m_view->showFullScreen();
    }
}

QSize Game::displaySize() const
{
    return m_view->size();
}

void Game::onHostModeEnded()
{
    m_mode = None;
}

void Game::onClientModeEnded()
{
    //TODO: client has not a way to update the connectivity
    if (m_gameClient) {
        delete m_gameClient;
        m_gameClient = 0;
    }
    m_mode = None;
}

void Game::startBackgroundSound()
{
    SoundManager::play(SoundManager::Background);
}

int Game::environment()
{
    return Global::environment();
}

QString Game::connectivity() const
{
    return m_connectivity;
}

void Game::setConnectivity(const QString &type)
{
    if (m_connectivity == type)
        return;

    m_connectivity = type;
    if (m_mode == HostMode) {
        onHostModeEnded();
        startHostMode();
    } else {
        onClientModeEnded();
        startClientMode();
    }
}