2011-08-24 14:14:44 +02:00
|
|
|
/*
|
|
|
|
Copyright (C) 2003 Parallel Realities
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
2015-02-26 17:20:36 +01:00
|
|
|
as published by the Free Software Foundation; either version 3
|
2011-08-24 14:14:44 +02:00
|
|
|
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
|
2015-02-26 17:20:36 +01:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2011-08-24 14:14:44 +02:00
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2015-02-26 17:20:36 +01:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2011-08-24 14:14:44 +02:00
|
|
|
*/
|
|
|
|
|
2011-08-26 21:29:04 +02:00
|
|
|
#include "Starfighter.h"
|
|
|
|
|
|
|
|
#if LINUX
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
2011-08-24 14:14:44 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Initalises a whole load of variables
|
|
|
|
*/
|
|
|
|
void initVars()
|
|
|
|
{
|
|
|
|
srand(time(NULL));
|
|
|
|
|
2012-03-12 04:16:56 +01:00
|
|
|
for (int i = 0 ; i < (int)(screen->w * screen->h / 2400) ; i++)
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
2012-03-12 04:16:56 +01:00
|
|
|
star[i].x = rand() % screen->w;
|
|
|
|
star[i].y = rand() % screen->h;
|
2011-08-24 14:14:44 +02:00
|
|
|
star[i].speed = 1 + (rand() % 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// These are good values for sound and music
|
|
|
|
|
|
|
|
if (engine.useAudio)
|
|
|
|
{
|
2015-02-27 00:46:45 +01:00
|
|
|
Mix_Volume(-1, 50);
|
2011-08-24 14:14:44 +02:00
|
|
|
Mix_VolumeMusic((int)engine.musicVolume);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Something went wrong. This stops the game, present the error message and
|
|
|
|
prompts the user to press space or ctrl to exit the game. This is unlikely to
|
|
|
|
be seen by people unless something really stoopid happens!
|
|
|
|
*/
|
2011-08-24 14:42:59 +02:00
|
|
|
void showErrorAndExit(int errorId, const char *name)
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
2011-08-26 23:53:46 +02:00
|
|
|
clearScreen(black);
|
2011-08-24 14:14:44 +02:00
|
|
|
|
|
|
|
if (errorId != 2)
|
|
|
|
{
|
2011-08-26 23:53:46 +02:00
|
|
|
drawString("A file error has occurred", -1, 200, FONT_RED);
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Couldn't create or write to directory '%s'\n", name);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
char string[255];
|
|
|
|
|
|
|
|
switch(errorId)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
sprintf(string, "%s was not found in the Starfighter data package", name);
|
2011-08-26 23:53:46 +02:00
|
|
|
drawString(string, -1, 250, FONT_WHITE);
|
2012-02-26 22:25:57 +01:00
|
|
|
drawString("Please try again. If this error persists, contact the authors", -1, 275, FONT_WHITE);
|
2011-08-26 23:53:46 +02:00
|
|
|
drawString("or reinstall the game", -1, 300, FONT_WHITE);
|
2011-08-24 14:14:44 +02:00
|
|
|
break;
|
|
|
|
case 1:
|
2011-08-26 23:53:46 +02:00
|
|
|
drawString("Project: Starfighter encountered an error whilst", -1, 250, FONT_WHITE);
|
|
|
|
drawString("attempting to load game data. Please try running", -1, 275, FONT_WHITE);
|
|
|
|
drawString("the game again. If the errors persist, reinstall the game", -1, 300, FONT_WHITE);
|
2011-08-24 14:14:44 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
2011-08-26 23:53:46 +02:00
|
|
|
drawString("Project: Starfighter encountered a critical error", -1, 250, FONT_WHITE);
|
|
|
|
drawString("while attempting to perform a required program function.", -1, 275, FONT_WHITE);
|
2012-02-26 22:25:57 +01:00
|
|
|
drawString("Please contact the authors with details.", -1, 300, FONT_WHITE);
|
2011-08-24 14:14:44 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-08-26 23:53:46 +02:00
|
|
|
drawString("Project: Starfighter will now exit", -1, 450, FONT_WHITE);
|
|
|
|
drawString("Press Space to continue", -1, 475, FONT_WHITE);
|
2011-08-24 14:14:44 +02:00
|
|
|
|
2012-02-26 22:23:34 +01:00
|
|
|
updateScreen();
|
|
|
|
|
2013-09-30 16:52:43 +02:00
|
|
|
engine.keyState[KEY_ALTFIRE] = 0;
|
2011-08-24 14:14:44 +02:00
|
|
|
|
2013-09-30 16:52:43 +02:00
|
|
|
while (!engine.keyState[KEY_ALTFIRE])
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
|
|
|
getPlayerInput();
|
2011-08-26 23:53:46 +02:00
|
|
|
delayFrame();
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
This bit is just for Linux users. It attempts to get the user's
|
|
|
|
home directory, then creates the .parallelrealities and .parallelrealities/starfighter
|
|
|
|
directories so that saves and temporary data files can be written there. Good, eh? :)
|
|
|
|
*/
|
|
|
|
#if LINUX
|
2011-08-26 21:29:04 +02:00
|
|
|
static void setupUserHomeDirectory()
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
|
|
|
char *userHome;
|
|
|
|
|
|
|
|
char *name = getlogin();
|
|
|
|
|
|
|
|
passwd *pass;
|
|
|
|
|
|
|
|
if (name != NULL)
|
|
|
|
pass = getpwnam(name);
|
|
|
|
else
|
|
|
|
pass = getpwuid(geteuid());
|
|
|
|
|
|
|
|
if (pass == NULL)
|
|
|
|
{
|
|
|
|
printf("Couldn't determine the user home directory. Exitting.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
userHome = pass->pw_dir;
|
|
|
|
|
|
|
|
char dir[PATH_MAX];
|
|
|
|
strcpy(dir, "");
|
|
|
|
|
|
|
|
sprintf(dir, "%s/.parallelrealities", userHome);
|
|
|
|
if ((mkdir(dir, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) != 0) && (errno != EEXIST))
|
|
|
|
showErrorAndExit(2, dir);
|
|
|
|
|
|
|
|
sprintf(dir, "%s/.parallelrealities/starfighter", userHome);
|
|
|
|
if ((mkdir(dir, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) != 0) && (errno != EEXIST))
|
|
|
|
showErrorAndExit(2, dir);
|
|
|
|
|
|
|
|
sprintf(engine.userHomeDirectory, "%s/.parallelrealities/starfighter/", userHome);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
Chugg chugg chugg.... brrr... chugg chugg chugg...brrrrrr... chugg ch..
|
|
|
|
BRRRRRRRRRRRRRRRRRMMMMMMMMMMMMMMMMMMM!! Well, hopefully anyway! ;)
|
|
|
|
*/
|
|
|
|
void initSystem()
|
|
|
|
{
|
|
|
|
strcpy(engine.userHomeDirectory, "");
|
|
|
|
|
|
|
|
#if LINUX
|
|
|
|
setupUserHomeDirectory();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Initialize the SDL library */
|
2013-07-10 21:37:08 +02:00
|
|
|
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_JOYSTICK) < 0) {
|
2011-08-24 14:14:44 +02:00
|
|
|
printf("Couldn't initialize SDL: %s\n", SDL_GetError());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
char filename[PATH_MAX];
|
|
|
|
int fullScreen = 0, useSound = 1, useMusic = 1;
|
|
|
|
|
|
|
|
FILE *fp;
|
|
|
|
sprintf(filename, "%sconf", engine.userHomeDirectory);
|
|
|
|
fp = fopen(filename, "rb");
|
|
|
|
|
|
|
|
if (fp != NULL)
|
|
|
|
{
|
|
|
|
fscanf(fp, "%d %d %d", &fullScreen, &useSound, &useMusic);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2011-08-26 22:48:52 +02:00
|
|
|
engine.fullScreen = fullScreen;
|
|
|
|
engine.useSound = useSound;
|
|
|
|
engine.useMusic = useMusic;
|
2011-08-24 14:14:44 +02:00
|
|
|
|
2013-09-30 16:52:43 +02:00
|
|
|
screen = SDL_CreateRGBSurface(0, screenWidth, screenHeight, 32, 0xff0000, 0xff00, 0xff, 0xff000000);
|
2011-08-24 14:14:44 +02:00
|
|
|
|
2013-09-30 16:52:43 +02:00
|
|
|
if (!screen) {
|
|
|
|
printf("Couldn't create %ix%ix32 surface: %s\n", screenWidth, screenHeight, SDL_GetError());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
window = SDL_CreateWindow("Project: Starfighter", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screen->w, screen->h, 0);
|
|
|
|
|
|
|
|
if (window == NULL) {
|
|
|
|
printf("Could not create window: %s\n", SDL_GetError());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_SetWindowIcon(window, loadImage("gfx/alienDevice.png"));
|
|
|
|
SDL_SetWindowFullscreen(window, engine.fullScreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
|
|
|
|
|
|
|
|
renderer = SDL_CreateRenderer(window, -1, 0);
|
|
|
|
|
|
|
|
if (!renderer) {
|
|
|
|
printf("Could not create renderer: %s\n", SDL_GetError());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_RenderSetLogicalSize(renderer, screen->w, screen->h);
|
|
|
|
|
|
|
|
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, screen->w, screen->h);
|
2011-08-24 14:14:44 +02:00
|
|
|
|
2013-09-30 16:52:43 +02:00
|
|
|
if (!texture) {
|
|
|
|
printf("Couldn't create %ix%ix32 texture: %s\n", screen->w, screen->h, SDL_GetError());
|
2011-08-24 14:14:44 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (engine.useAudio)
|
|
|
|
{
|
2015-02-26 16:22:22 +01:00
|
|
|
if (Mix_OpenAudio(44100, AUDIO_S16, engine.useAudio * 2, 1024) < 0)
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
2015-02-26 16:22:22 +01:00
|
|
|
printf("Warning: Couldn't set 44100 Hz 16-bit stereo audio - Reason: %s\n", Mix_GetError());
|
2011-08-24 14:14:44 +02:00
|
|
|
printf("Sound and Music will be disabled\n");
|
2011-08-26 16:14:58 +02:00
|
|
|
engine.useAudio = false;
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_ShowCursor(SDL_DISABLE);
|
|
|
|
SDL_EventState(SDL_MOUSEMOTION, SDL_DISABLE);
|
2013-07-10 21:37:08 +02:00
|
|
|
|
|
|
|
SDL_JoystickEventState(SDL_ENABLE);
|
|
|
|
SDL_JoystickOpen(0);
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Removes [hopefully] all the resources that has been
|
|
|
|
loaded and created during the game. This is called by
|
|
|
|
atexit();
|
|
|
|
*/
|
|
|
|
void cleanUp()
|
|
|
|
{
|
2011-08-26 23:53:46 +02:00
|
|
|
freeGraphics();
|
|
|
|
SDL_FreeSurface(background);
|
2011-08-24 14:14:44 +02:00
|
|
|
freeSound();
|
|
|
|
resetLists();
|
|
|
|
delete(engine.bulletHead);
|
|
|
|
delete(engine.explosionHead);
|
|
|
|
delete(engine.collectableHead);
|
2011-08-26 23:53:46 +02:00
|
|
|
delete(bufferHead);
|
2011-08-24 14:14:44 +02:00
|
|
|
|
|
|
|
for (int i = 0 ; i < MAX_FONTSHAPES ; i++)
|
|
|
|
{
|
2011-08-26 23:53:46 +02:00
|
|
|
if (fontShape[i] != NULL)
|
|
|
|
SDL_FreeSurface(fontShape[i]);
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char filename[PATH_MAX];
|
|
|
|
strcpy(filename, "");
|
|
|
|
|
|
|
|
sprintf(filename, "%smusic.mod", engine.userHomeDirectory);
|
|
|
|
remove(filename);
|
|
|
|
|
|
|
|
sprintf(filename, "%smusic.s3m", engine.userHomeDirectory);
|
|
|
|
remove(filename);
|
|
|
|
|
|
|
|
if (engine.useAudio)
|
|
|
|
{
|
|
|
|
Mix_CloseAudio();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the config using current settings
|
|
|
|
FILE *fp;
|
|
|
|
sprintf(filename, "%sconf", engine.userHomeDirectory);
|
|
|
|
fp = fopen(filename, "wb");
|
|
|
|
if (fp != NULL)
|
|
|
|
{
|
2011-08-26 22:48:52 +02:00
|
|
|
fprintf(fp, "%d %d %d\n", engine.fullScreen, engine.useSound, engine.useMusic);
|
2011-08-24 14:14:44 +02:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Error saving config\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Quit();
|
2012-03-06 00:20:42 +01:00
|
|
|
printf("Thank You for playing Starfighter\n");
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
|
|
|
|