~ubuntu-branches/ubuntu/quantal/zaz/quantal

« back to all changes in this revision

Viewing changes to src/level.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Miriam Ruiz
  • Date: 2009-08-31 20:08:58 UTC
  • Revision ID: james.westby@ubuntu.com-20090831200858-54lcmcrna6dwk3wr
Tags: upstream-0.2.9+dfsg1
ImportĀ upstreamĀ versionĀ 0.2.9+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Zaz
 
3
 * Copyright (C) Remigiusz Dybka 2009 <remigiusz.dybka@gmail.com>
 
4
 *
 
5
 Zaz is free software: you can redistribute it and/or modify it
 
6
 under the terms of the GNU General Public License as published by the
 
7
 Free Software Foundation, either version 3 of the License, or
 
8
 (at your option) any later version.
 
9
 
 
10
 Zaz is distributed in the hope that it will be useful, but
 
11
 WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
13
 See the GNU General Public License for more details.
 
14
 
 
15
 You should have received a copy of the GNU General Public License along
 
16
 with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 */
 
18
 
 
19
#include <fstream>
 
20
#include <sstream>
 
21
#include "level.h"
 
22
#include "error.h"
 
23
#include "common.h"
 
24
#include "textureloader.h"
 
25
 
 
26
Level::Level(Bezier paths[], int npaths, bool loop, bool invert, string name)
 
27
        :loop(loop), name(name), invert(invert), startFeedRate(1), endFeedRate(1),
 
28
        ballsToDraw(10), ballsFromStart(0), colors(3), backgroundTex(0)
 
29
{
 
30
 
 
31
}
 
32
 
 
33
Level::~Level()
 
34
{
 
35
    if (backgroundTex)
 
36
    {
 
37
        glDeleteTextures(1, &backgroundTex);
 
38
    }
 
39
}
 
40
 
 
41
void Level::NewLevel()
 
42
{
 
43
    loop = false;
 
44
    invert = false;
 
45
    name = "New Level";
 
46
    startFeedRate = 1.0;
 
47
    endFeedRate = 1.0;
 
48
    ballsToDraw = 20;
 
49
    ballsFromStart = 20;
 
50
    colors = 5;
 
51
    backgroundTex = 0;
 
52
    time = 180;
 
53
};
 
54
 
 
55
void Level::Save()
 
56
{
 
57
    Save(savePhilename.c_str());
 
58
}
 
59
 
 
60
void Level::Save(const char *philename)
 
61
{
 
62
    std::ofstream out(philename);
 
63
 
 
64
    // level name
 
65
    out << name << endl;
 
66
    // number of paths
 
67
    out << paths.size() << endl;
 
68
    // paths
 
69
    for (uint i = 0; i < paths.size(); ++i)
 
70
    {
 
71
        // number of points
 
72
        out << paths[i].points.size() << endl;
 
73
        for (uint p = 0; p < paths[i].points.size(); ++p)
 
74
        {
 
75
            BezierPoint pt = paths[i].points[p];
 
76
            out << pt.x << " " << pt.y << " " << pt.cx << " " << pt.cy << endl;
 
77
        }
 
78
    }
 
79
 
 
80
    out << loop << endl;
 
81
    out << invert << endl;
 
82
    out << startFeedRate << endl;
 
83
    out << endFeedRate << endl;
 
84
    out << ballsToDraw << endl;
 
85
    out << ballsFromStart << endl;
 
86
    out << colors << endl;
 
87
    out << time << endl;
 
88
 
 
89
}
 
90
 
 
91
void Level::LoadData(string philename)
 
92
{
 
93
    savePhilename = philename;
 
94
 
 
95
    std::ifstream in(philename.c_str());
 
96
 
 
97
    if (!in)
 
98
    {
 
99
        return;
 
100
    }
 
101
 
 
102
    paths.clear();
 
103
    int npaths;
 
104
 
 
105
    istringstream sl;
 
106
    string l;
 
107
 
 
108
    // name
 
109
    getline(in, l);
 
110
    Strip(l);
 
111
    name = l;
 
112
 
 
113
    // number of paths
 
114
    getline(in, l);
 
115
    sl.str(l);
 
116
    sl >> npaths;
 
117
 
 
118
    // paths
 
119
    for (int i = 0; i < npaths; ++i)
 
120
    {
 
121
        paths.push_back(Bezier());
 
122
 
 
123
        int pl = 0;
 
124
 
 
125
        getline(in, l);
 
126
        Strip(l);
 
127
        sl.clear();
 
128
        sl.str(l);
 
129
        sl >> pl;
 
130
 
 
131
        for (int p = 0; p < pl; ++p)
 
132
        {
 
133
            getline(in, l);
 
134
            sl.clear();
 
135
            sl.str(l);
 
136
 
 
137
            double x, y, cx, cy;
 
138
            sl >> x >> y >> cx >> cy;
 
139
            paths[i].points.push_back(BezierPoint (x, y, cx, cy));
 
140
        }
 
141
 
 
142
    }
 
143
 
 
144
    // loop
 
145
    getline(in, l);
 
146
    sl.clear();
 
147
    sl.str(l);
 
148
    sl >> loop;
 
149
 
 
150
    // invert
 
151
    getline(in, l);
 
152
    sl.clear();
 
153
    sl.str(l);
 
154
    sl >> invert;
 
155
 
 
156
    // startFeedRate
 
157
    getline(in, l);
 
158
    sl.clear();
 
159
    sl.str(l);
 
160
    sl >> startFeedRate;
 
161
 
 
162
    // endFeedRate
 
163
    getline(in, l);
 
164
    sl.clear();
 
165
    sl.str(l);
 
166
    sl >> endFeedRate;
 
167
 
 
168
    // ballsToDraw
 
169
    getline(in, l);
 
170
    sl.clear();
 
171
    sl.str(l);
 
172
    sl >> ballsToDraw;
 
173
 
 
174
    // ballsFromStart
 
175
    getline(in, l);
 
176
    sl.clear();
 
177
    sl.str(l);
 
178
    sl >> ballsFromStart;
 
179
 
 
180
    // colours
 
181
    getline(in, l);
 
182
    sl.clear();
 
183
    sl.str(l);
 
184
    sl >> colors;
 
185
 
 
186
    // time
 
187
    getline(in, l);
 
188
    sl.clear();
 
189
    sl.str(l);
 
190
    sl >> time;
 
191
 
 
192
}
 
193
 
 
194
Level::Level(const char *philename, bool skipGfx)
 
195
{
 
196
    NewLevel();
 
197
 
 
198
    string phn(philename);
 
199
    // strip philename of extensions
 
200
    phn = phn.substr(0, phn.find_last_of("."));
 
201
 
 
202
    LoadData(phn + ".lvl");
 
203
 
 
204
    if (!skipGfx)
 
205
    {
 
206
        phn+=".png";
 
207
        backgroundTex = LoadTextureFile(phn.c_str());
 
208
    }
 
209
}