File: main.cpp

package info (click to toggle)
brainparty 0.61%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,604 kB
  • ctags: 2,193
  • sloc: cpp: 13,002; objc: 252; makefile: 60
file content (154 lines) | stat: -rw-r--r-- 3,960 bytes parent folder | download | duplicates (6)
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
// Brain Party
// Copyright (C) 2010 Paul Hudson (http://www.tuxradar.com/brainparty)

// Brain Party is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#include <unistd.h>
#include "SDL.h"
#include "SDL_opengl.h" 
#include "SDL_mixer.h" 
#include "SDL_ttf.h" 

#include "BPGame.h"

BPGame* Game;

int main(int argc, char *argv[]) {
	if ( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0 ) {
		fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
		exit(1);
	}
	
	// init all the SDL stuff
	putenv((char*)"SDL_VIDEO_WINDOW_POS");
	putenv((char*)"SDL_VIDEO_CENTERED=1");

	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
		
	SDL_WM_SetCaption("Brain Party", "Brain Party");
	SDL_WM_SetIcon(SDL_LoadBMP("/usr/share/games/brainparty/icon.bmp"), NULL);
	
	Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 2048);
	TTF_Init();
	
 	SDL_Surface* screen = SDL_SetVideoMode(320, 480, 24, SDL_OPENGL);

	// clear the screen
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// load the splash screen texture
	Texture* tex_splash = new Texture("hudzillagames", 320, 480);

	// set up all the OpenGL stuff
	glViewport(0, 0, 320, 480);
	
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	
	glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
	glEnable(GL_LINE_SMOOTH);
	
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, 320, 480, 0, -100.0f, 100.0f);
	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	// now render the splash screen...
	tex_splash->Draw(0, 0);

	// ...update the screen
	SDL_GL_SwapBuffers();
	
	// and flush out any SDL events that are waiting - this makes the window activate
	SDL_Event event;
	while (SDL_PollEvent(&event)) { }
	
	// load all the game data
	Game = new BPGame();
	Game->Init(320, 480);
	
	// finally sleep for a second so the splash screen is visible
	SDL_Delay(1000);
	
	// free up the memory; the splash screen isn't used again
	delete tex_splash;


	float seconds_elapsed;
	int previous_time = SDL_GetTicks();
	
	bool mouse_down = false;
	
	while (true) {	
		int new_time = SDL_GetTicks();
		int ticks_elapsed = new_time - previous_time;
		previous_time = new_time;
		
		while (SDL_PollEvent(&event)) {
			switch (event.type) {
				case SDL_MOUSEBUTTONDOWN:
					mouse_down = true;
					Game->TouchStart(event.button.x, event.button.y);
					break;
				case SDL_MOUSEBUTTONUP:
					mouse_down = false;
					Game->TouchStop(event.button.x, event.button.y);
					break;
				case SDL_MOUSEMOTION:
					if (mouse_down) Game->TouchDrag(event.motion.x, event.motion.y);
					break;
				case SDL_KEYUP:
					if (Game->ShowingClearScores) {
						Game->ShowingMessageBox = false;
						Game->ShowingClearScores = false;
						
						if (event.key.keysym.sym == (Uint16)'y') {
							Game->ExecuteResetScores();
						}
					}
					
					break;
				case SDL_QUIT:
					exit(0);
			}
		}
		
		float target_time = 16;

		if (ticks_elapsed < target_time) {
			SDL_Delay(target_time - ticks_elapsed);
		}

		seconds_elapsed = ticks_elapsed / 1000.0f;
		Game->Update(seconds_elapsed);
		Game->Draw();
		SDL_GL_SwapBuffers();
	}
	
	delete Game;

	TTF_Quit();
	Mix_CloseAudio();
	SDL_Quit();

        return EXIT_SUCCESS;
}