File: main.cpp

package info (click to toggle)
supertransball2 1.5-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,336 kB
  • ctags: 477
  • sloc: cpp: 5,665; makefile: 45
file content (290 lines) | stat: -rw-r--r-- 6,882 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#ifdef _WIN32
#include <windows.h>
#include <windowsx.h>
#else
#include <sys/time.h>
#include <time.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include "SDL/SDL.h"
#include "SDL_mixer.h"
#include "sge.h"

#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "fonts.h"
#include "list.h"

#include "tiles.h"
#include "maps.h"
#include "transball.h"
#include "game.h"

/*						GLOBAL VARIABLES INITIALIZATION:							*/ 

int SCREEN_X=320;
int SCREEN_Y=240;
int PIXEL_SIZE=1;
int COLOUR_DEPTH=32;

#ifdef _WIN32
//bool fullscreen=true;
bool fullscreen=true;
#else
bool fullscreen=false;
#endif

/* Redrawing constant: */ 
const int REDRAWING_PERIOD=18;

/* Frames per second counter: */ 
int frames_per_sec=0;
int frames_per_sec_tmp=0;
int init_time=0;

char *datadir="/usr/share/games/supertransball2/";
char *confdir;

/* Surfaces: */ 
SDL_Surface *screen_sfc=0,*buffer_screen_sfc=0;

TRANSBALL *game=0;


#ifndef _WIN32
struct timeval init_tick_count_value;

void setupTickCount()
{
	gettimeofday(&init_tick_count_value, NULL);
}

long GetTickCount()
{
	struct timeval now;
	gettimeofday(&now, NULL);
	struct timeval diff;
	diff.tv_sec = now.tv_sec - init_tick_count_value.tv_sec;
	diff.tv_usec = now.tv_usec - init_tick_count_value.tv_usec;
	if (diff.tv_usec < 0)
	{
		diff.tv_sec--;
		diff.tv_usec+=1000000;
	}
	return diff.tv_sec*1000 + diff.tv_usec/1000;
}
#endif

void pause(unsigned int time)
{
	unsigned int initt=GetTickCount();

	while((GetTickCount()-initt)<time);
} /* pause */ 


SDL_Surface* initializeSDL(int moreflags)
{
	SDL_Surface *screen;

	//int flags = SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_HWPALETTE;
	int flags = SDL_HWPALETTE|moreflags;
	if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)<0) return 0;
	atexit(SDL_Quit);
	SDL_WM_SetCaption("Super Transball 2 v1.5", 0);
	SDL_ShowCursor(SDL_DISABLE);
	SDL_EnableUNICODE(1);

	screen = SDL_SetVideoMode(SCREEN_X*PIXEL_SIZE, SCREEN_Y*PIXEL_SIZE, COLOUR_DEPTH, flags);
	buffer_screen_sfc = SDL_CreateRGBSurface(0,SCREEN_X,SCREEN_Y,COLOUR_DEPTH,0,0,0,0);

	pause(200);

	if (Mix_OpenAudio(22050, AUDIO_S16, 2, 1024)) {
		return 0;
	} /* if */ 

	return screen;
} /* initializeSDL */ 


void finalizeSDL()
{
	SDL_FreeSurface(buffer_screen_sfc);

	Mix_CloseAudio();
	SDL_Quit();
} /* finalizeSDL */ 



#ifdef _WIN32
int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    LPSTR lpCmdLine, int nCmdShow)
{
#else
int main(int argc, char** argv)
{
	setupTickCount();
#endif

        mode_t mode;
        FILE *cfg;
	confdir=(char*)malloc(strlen(getenv("HOME"))+50); strcpy(confdir,getenv("HOME"));
        strcat(confdir,"/.supertransball2");
	mode=S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP;
	mkdir(confdir,mode);
	chdir(confdir);
	mkdir("replays",mode);
	mkdir("high",mode);

	cfg=fopen("transball.cfg", "r");
	if (!cfg)
	{
		cfg=fopen("transball.cfg", "w");
                fprintf(cfg,"%s","113 97 111 112 32 13 282");
	}

	fclose(cfg);
	
	printf("Configuration: %s\n",confdir);
        printf("Data: %s\n",datadir);
        chdir(datadir);

	int time,act_time;
	SDL_Event event;
    bool quit = false;

	if (!load_configuration()) save_configuration();

	time=init_time=GetTickCount();
	screen_sfc = initializeSDL((fullscreen ? SDL_FULLSCREEN : 0));
	if (screen_sfc==0) return 0;

	if (!fonts_initialization()) return 0;

	while (!quit) {
		while( SDL_PollEvent( &event ) ) {
            switch( event.type ) {
                /* Keyboard event */
                case SDL_KEYDOWN:
					{
						SDLMod modifiers;

						modifiers=SDL_GetModState();

						if (event.key.keysym.sym==SDLK_F12) quit = true;

						
						if ((modifiers&KMOD_ALT)!=0) {
							if (event.key.keysym.sym==SDLK_RETURN) {
								/* Toogle FULLSCREEN mode: */ 
								finalizeSDL();
								if (game!=0) game->free_sounds();
								if (fullscreen) fullscreen=false;
										   else fullscreen=true;
								screen_sfc = initializeSDL((fullscreen ? SDL_FULLSCREEN : 0));
								if (screen_sfc==0) return 0;
								/* Reload: */ 
								if (game!=0) game->load_sounds();
								save_configuration();
							} /* if */ 

							if (event.key.keysym.sym==SDLK_1) {
								/* x1 windowed mode: */ 
								finalizeSDL();
								if (game!=0) game->free_sounds();
								PIXEL_SIZE=1;
								screen_sfc = initializeSDL((fullscreen ? SDL_FULLSCREEN : 0));
								if (screen_sfc==0) return 0;
								/* Reload: */ 
								if (game!=0) game->load_sounds();
								save_configuration();
							} /* if */ 

							if (event.key.keysym.sym==SDLK_2) {
								/* x2 mode: */ 
								finalizeSDL();
								if (game!=0) game->free_sounds();
								PIXEL_SIZE=2;
								screen_sfc = initializeSDL((fullscreen ? SDL_FULLSCREEN : 0));
								if (screen_sfc==0) return 0;
								/* Reload: */ 
								if (game!=0) game->load_sounds();
								save_configuration();
							} /* if */ 

							if (event.key.keysym.sym==SDLK_3) {
								/* x3 mode: */ 
								finalizeSDL();
								if (game!=0) game->free_sounds();
								PIXEL_SIZE=3;
								screen_sfc = initializeSDL((fullscreen ? SDL_FULLSCREEN : 0));
								if (screen_sfc==0) return 0;
								/* Reload: */ 
								if (game!=0) game->load_sounds();
								save_configuration();
							} /* if */ 

							if (event.key.keysym.sym==SDLK_4) {
								/* x4 mode: */ 
								finalizeSDL();
								if (game!=0) game->free_sounds();
								PIXEL_SIZE=4;
								screen_sfc = initializeSDL((fullscreen ? SDL_FULLSCREEN : 0));
								if (screen_sfc==0) return 0;
								/* Reload: */ 
								if (game!=0) game->load_sounds();
								save_configuration();
							} /* if */ 
						} /* if */ 

					}

                    break;

                /* SDL_QUIT event (window close) */
                case SDL_QUIT:
                    quit = true;
                    break;
            } /* switch */ 
        } /* while */ 

		act_time=GetTickCount();
		if (act_time-time>=REDRAWING_PERIOD)
		{

			frames_per_sec_tmp+=1;
			if ((act_time-init_time)>=1000) {
				frames_per_sec=frames_per_sec_tmp;
				frames_per_sec_tmp=0;
				init_time=act_time;
			} /* if */ 

			time+=REDRAWING_PERIOD;
			if ((act_time-time)>2*REDRAWING_PERIOD) time=act_time;
		
			if (PIXEL_SIZE==1) {
				if (!gamecycle(screen_sfc,SCREEN_X,SCREEN_Y)) quit=true;	
			} else {
				if (!gamecycle(buffer_screen_sfc,SCREEN_X,SCREEN_Y)) quit=true;	
				sge_transform(buffer_screen_sfc,screen_sfc,0,float(PIXEL_SIZE),float(PIXEL_SIZE),0,0,0,0,0);	
			} /* if */ 
			SDL_Flip(screen_sfc);
		}
		SDL_Delay(1);
	}

	fonts_termination();

	finalizeSDL();

	return 0;
}