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

« back to all changes in this revision

Viewing changes to src/main.cpp

  • Committer: Package Import Robot
  • Author(s): Miriam Ruiz
  • Date: 2011-11-16 02:28:10 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20111116022810-d3tehh974q280vit
Tags: 1.0.0~dfsg1-1
* New Upstream Release
* Upgraded Standards-Version from 3.8.3 to 3.9.2
* New homepage: http://phuzzboxmedia.com/index.php/games/open-sourced-zaz
* Refreshed patches
* Moved package to DebSrc 3

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include "gameloop.h"
22
22
#include "mainmenu.h"
23
23
#include "hiscores.h"
 
24
#include "tests.h"
 
25
#include "profile.h"
 
26
#include "levelset.h"
 
27
#include "splash.h"
24
28
#include <fstream>
25
29
#include <vector>
26
30
#include <sys/stat.h>
27
31
#include <ctime>
 
32
#include <sstream>
 
33
#include <cstdlib>
 
34
 
 
35
#include "directorylister.h"
28
36
 
29
37
Scenes::Settings settings;
30
38
HiScores hiScores;
31
39
 
32
40
SDL_Surface *screen;
33
41
FTFont *font; // the default font for the game/engine/universe
34
 
FTFont *font2;
 
42
//FTFont *font2;
35
43
FTFont *font3;
 
44
FTFont *font4;
36
45
 
37
46
bool wantReinit;
 
47
bool resReset;
38
48
bool audioHardwareInitialized;
39
 
SDL_Rect **screenModes;
 
49
SDL_Rect **screenModes = NULL;
 
50
GLuint pointer = 0;
 
51
int pointerFrame = 0;
 
52
double pointerRot = 0.0;
40
53
 
41
54
int main (int argc, char *argv[])
42
55
{
 
56
    bool showSplash = SHOW_SPLASH;
43
57
    bool editor = false;
44
58
    bool testplay = false;
 
59
    bool tests = false;
45
60
    char *editorPhilename = 0;
46
61
    char *testplayPhilename = 0;
47
62
 
48
 
#ifdef ENABLE_NLS
49
 
    setlocale(LC_CTYPE, "");
50
 
    setlocale(LC_MESSAGES, "");
51
 
    bindtextdomain(PACKAGE, LOCALEDIR);
52
 
    textdomain(PACKAGE);
53
 
#endif
 
63
    resReset = false;
54
64
 
55
65
    if (argc > 1)
56
66
    {
69
79
        {
70
80
            testplayPhilename = argv[2];
71
81
        }
 
82
 
 
83
        if (strcmp(argv[1], "-t") == 0)
 
84
            tests = true;
 
85
 
 
86
        if ((strcmp(argv[1], "-d") == 0) && (argc == 3))
 
87
        {
 
88
            settings.forcedDir = argv[2];
 
89
        }
72
90
    }
73
91
 
74
 
    // create a game directory
 
92
    try
 
93
    {
 
94
        char err[256];
 
95
        // create a game directory
75
96
#ifndef WIN32
76
 
    string dir = Settings::getDefaultDirectory ();
77
 
    mkdir(dir.c_str(), S_IRUSR | S_IWUSR | S_IXUSR);
 
97
        string dir = Settings::getDefaultDirectory ();
 
98
        mkdir(dir.c_str(), S_IRUSR | S_IWUSR | S_IXUSR);
78
99
#else
79
 
    _mkdir(Settings::getDefaultDirectory().c_str());
 
100
        wchar_t wd[PATH_MAX];
 
101
        MultiByteToWideChar(CP_UTF8, 0, Settings::getDefaultDirectory().c_str(), -1, wd, MAX_PATH);
 
102
        _wmkdir(wd);
80
103
#endif
81
104
 
82
 
    // load default settings for things we might not ask too quickly about
83
 
    settings.get("mouseSensivity", "5");
84
 
    settings.get("musicVolume", "80");
85
 
    settings.get("sfxVolume", "80");
 
105
        // load default settings for things we might not ask too quickly about
 
106
        settings.get("mouseSensivity", "5");
 
107
        settings.get("keyboardSensivity", "5");
 
108
        settings.get("musicVolume", "80");
 
109
        settings.get("sfxVolume", "80");
 
110
        settings.get("disableSpeedup", "FALSE");
86
111
 
87
 
    do
88
 
    {
89
 
        wantReinit = false;
90
112
        if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0)
91
113
        {
92
 
            printf("Could not initialize SDL:%s\n", SDL_GetError());
93
 
            SDL_Quit();
94
 
 
95
 
            return 1;
 
114
            sprintf(err, "Could not initialize SDL:%s\n", SDL_GetError());
 
115
            ERR(err);
96
116
        }
97
117
 
98
 
        Uint32 fullscreen_flag = 0;
99
 
 
100
 
        if (settings.getb("fullscreen", false))
101
 
            fullscreen_flag = SDL_FULLSCREEN;
102
 
 
103
 
        const SDL_VideoInfo *vi = SDL_GetVideoInfo();
104
 
        uint bpp = vi->vfmt->BitsPerPixel;
105
 
 
106
 
        switch (bpp)
 
118
        do
107
119
        {
108
 
        case 16:
109
 
            SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 4 );
110
 
            SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 4 );
111
 
            SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 4 );
112
 
            SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
113
 
            SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
114
 
            break;
115
 
 
116
 
        case 24:
117
 
        case 32:
118
 
            SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
119
 
            SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
120
 
            SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
 
120
            wantReinit = false;
 
121
 
 
122
            Uint32 fullscreen_flag = 0;
 
123
 
 
124
            if (settings.getb("fullscreen", false))
 
125
                fullscreen_flag = SDL_FULLSCREEN;
 
126
 
 
127
            const SDL_VideoInfo *vi = SDL_GetVideoInfo();
 
128
            uint bpp = vi->vfmt->BitsPerPixel;
 
129
 
 
130
            switch (bpp)
 
131
            {
 
132
            case 16:
 
133
                SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 4 );
 
134
                SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 4 );
 
135
                SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 4 );
 
136
                SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
 
137
                SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
 
138
                break;
 
139
 
 
140
            case 24:
 
141
            case 32:
 
142
                SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
 
143
                SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
 
144
                SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
121
145
//                      SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
122
 
            SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
123
 
            SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
124
 
            break;
125
 
 
126
 
        default:
127
 
            cout << "This program requires at least 16bit display" << endl;
128
 
            return 1;
129
 
        }
130
 
 
131
 
        screenModes=SDL_ListModes(NULL, SDL_OPENGL | SDL_FULLSCREEN|SDL_HWSURFACE);
132
 
        string res = settings.get("resolution", "640x480");
133
 
 
134
 
        int vmx = atoi(res.c_str());
135
 
        int vmy = atoi(res.substr(res.find_first_of("x") + 1).c_str());
136
 
 
137
 
        screen = SDL_SetVideoMode(vmx, vmy, bpp, SDL_OPENGL | fullscreen_flag);
138
 
 
139
 
        if (screen == NULL)
140
 
        {
141
 
            printf("%s \n", SDL_GetError());
142
 
            return 1;
143
 
        }
144
 
 
145
 
        SDL_WM_SetCaption("Zaz", NULL);
146
 
 
147
 
        glColor3f(1.0, 0, 1.0);
148
 
        font = new FTTextureFont(settings.getCFilename ("FreeSans.ttf"));
149
 
        if (font->Error())
150
 
            return 1;
151
 
 
152
 
 
153
 
        font2 = new FTPixmapFont(settings.getCFilename ("FreeMonoBold.ttf"));
154
 
        if (font->Error())
155
 
            return 1;
156
 
 
157
 
        font3 = new FTTextureFont(settings.getCFilename ("font1.ttf"));
158
 
        if (font->Error())
159
 
            return 1;
160
 
 
161
 
        font->FaceSize(24);
162
 
        font2->FaceSize(12);
163
 
        font3->FaceSize(24);
164
 
 
165
 
        try
166
 
        {
 
146
                SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
 
147
                SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
 
148
                break;
 
149
 
 
150
            default:
 
151
                ERR("This program requires at least 16bit display");
 
152
            }
 
153
 
 
154
            if (!screenModes)
 
155
                screenModes=SDL_ListModes(NULL, SDL_OPENGL | SDL_FULLSCREEN|SDL_HWSURFACE);
 
156
 
 
157
            string res = settings.get("resolution", "640x480");
 
158
 
 
159
            int vmx = atoi(res.c_str());
 
160
            int vmy = atoi(res.substr(res.find_first_of("x") + 1).c_str());
 
161
 
 
162
            screen = SDL_SetVideoMode(vmx, vmy, bpp, SDL_OPENGL | fullscreen_flag);
 
163
 
 
164
            if (screen == NULL)
 
165
            {
 
166
                // fallback to a safe resolution
 
167
                screen = SDL_SetVideoMode(640, 480, bpp, SDL_OPENGL);
 
168
                cout << "Falling back to 640x480 resolution" << endl;
 
169
                if (!screen)
 
170
                {
 
171
                    sprintf(err, "%s \n", SDL_GetError());
 
172
                    ERR(err);
 
173
                    return 1;
 
174
                }
 
175
 
 
176
                // if fallback worked, let's save the changes
 
177
                settings.setb("fullscreen", false);
 
178
                settings.set("resolution", "640x480");
 
179
            }
 
180
 
 
181
            SDL_WM_SetCaption("Zaz", NULL);
 
182
 
 
183
            glColor3f(1.0, 0, 1.0);
 
184
            font = new FTTextureFont(settings.getCFilename ("FreeSans.ttf"));
 
185
            if (font->Error())
 
186
                ERR("Could not open FreeSans.ttf [" + settings.getFilename ("FreeSans.ttf") + "]");
 
187
 
 
188
            font3 = new FTTextureFont(settings.getCFilename ("font1.ttf"));
 
189
            if (font->Error())
 
190
                ERR("Could not open font1.ttf [" + settings.getFilename ("font1.ttf") + "]");
 
191
 
 
192
            font4 = new FTTextureFont(settings.getCFilename ("FreeMonoBold.ttf"));
 
193
            if (font->Error())
 
194
                ERR("Could not open FreeMonoBold.ttf [" + settings.getFilename ("FreeMonoBold.ttf") + "]");
 
195
 
 
196
            font->FaceSize(24);
 
197
            font3->FaceSize(24);
 
198
            font4->FaceSize(24);
 
199
 
 
200
            // load profile
 
201
            profile = Profile(settings.get("last_profile", ""));
 
202
            settings.set("last_profile", profile.getName());
 
203
            profile.Save();
 
204
 
167
205
            if (testplay)
168
206
            {
169
207
                if (testplayPhilename != 0)
170
208
                {
171
 
                    Level level(settings.getCFilename(testplayPhilename));
172
 
                    Game game(&settings, screen, level, 0, 3, 5);
 
209
                    cout << "Testing:" << testplayPhilename << endl;
 
210
                    Level level(testplayPhilename, true);
 
211
                    Game game(screen, level, 0, 3, 5);
173
212
                    game.Run();
174
213
                }
175
214
                else
176
215
                {
177
 
                    cout << "Must give a level to play" << endl;
 
216
                    ERR("Must give a level to play");
178
217
                }
179
218
 
 
219
                showSplash = false;
180
220
            };
181
221
 
182
222
            if (editor)
188
228
                    editorPhilename = defPhn;
189
229
                }
190
230
 
191
 
                Level level(editorPhilename);
192
 
                Editor ed(&settings, screen, level);
 
231
 
 
232
                Level level(editorPhilename, true);
 
233
                Editor ed(screen, level);
193
234
                ed.Run();
194
 
            }
195
 
 
196
 
            if (!editor && !testplay)
197
 
            {
198
 
                MainMenu m(&settings, screen);
 
235
                showSplash = false;
 
236
            }
 
237
 
 
238
            if (tests)
 
239
            {
 
240
                Tests t(screen);
 
241
                t.Run();
 
242
                showSplash = false;
 
243
            }
 
244
 
 
245
            if (showSplash)
 
246
            {
 
247
                Splash s(screen);
 
248
 
 
249
                s.Run();
 
250
                /*stringstream str;
 
251
                str << "0:::0:0:0:0:000:000/n";
 
252
                s.Export("phbx.ogv", str, 1);
 
253
                return 0;
 
254
                */
 
255
                showSplash = false;
 
256
 
 
257
            }
 
258
 
 
259
            if (!editor && !testplay && !tests)
 
260
            {
 
261
                MainMenu m(screen);
199
262
                m.Run();
200
263
            }
201
264
 
202
 
        }
203
 
        catch (Error e)
204
 
        {
205
 
            cout << e.file << ":" << e.line << ":" << e.msg << endl;
206
 
        }
207
 
 
208
 
        delete font;
209
 
        delete font2;
210
 
        delete font3;
211
 
        SDL_Quit();
212
 
    }
213
 
    while (wantReinit);
 
265
            delete font;
 
266
            delete font3;
 
267
            delete font4;
 
268
//            SDL_Quit();
 
269
 
 
270
            profile.Save();
 
271
        }
 
272
        while (wantReinit);
 
273
    }
 
274
    catch (Error e)
 
275
    {
 
276
        ostringstream ss;
 
277
        ss << e.file << ":" << e.line << ":" << e.msg;
 
278
#ifdef WIN32
 
279
        const char caption[] = "Zaz exception";
 
280
 
 
281
        int l1 = MultiByteToWideChar(CP_UTF8, 0, ss.str().c_str(), -1, 0, 0);
 
282
        wchar_t wmsg[l1];
 
283
 
 
284
        int l2 = MultiByteToWideChar(CP_UTF8, 0, caption, -1, 0, 0);
 
285
        wchar_t wcap[l2];
 
286
 
 
287
        MultiByteToWideChar(CP_UTF8, 0, ss.str().c_str(), -1, wmsg, l1);
 
288
        MultiByteToWideChar(CP_UTF8, 0, caption, -1, wcap, l2);
 
289
 
 
290
        MessageBoxW(0, wmsg, wcap, MB_OK | MB_ICONERROR);
 
291
#endif
 
292
        cout << ss.str() << endl;
 
293
        return(1);
 
294
    }
214
295
 
215
296
    settings.Save();
 
297
    profile.Save();
 
298
 
 
299
    if (Scenes::mixer)
 
300
        delete Scenes::mixer;
 
301
 
 
302
    SDL_Quit();
216
303
 
217
304
    return(0);
218
305
}