tbftss/src/system/init.c

314 lines
6.7 KiB
C
Raw Normal View History

2015-10-20 13:51:49 +02:00
/*
2016-02-21 16:50:27 +01:00
Copyright (C) 2015-2016 Parallel Realities
2015-10-20 13:51:49 +02:00
This program 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 2
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "init.h"
static void loadConfig(void);
void saveConfig(void);
static void initColor(SDL_Color *c, int r, int g, int b);
static void showLoadingStep(float step, float maxSteps);
2015-10-20 13:51:49 +02:00
void initSDL(void)
{
int rendererFlags, windowFlags;
/* do this here, so we don't destroy the save dir stored in app */
memset(&app, 0, sizeof(App));
/* done in src/plat/ */
2015-10-20 13:51:49 +02:00
createSaveFolder();
rendererFlags = SDL_RENDERER_ACCELERATED;
windowFlags = 0;
loadConfig();
if (app.vSync)
{
rendererFlags |= SDL_RENDERER_PRESENTVSYNC;
}
if (app.fullscreen)
{
windowFlags |= SDL_WINDOW_FULLSCREEN;
}
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0)
{
printf("Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
}
SDL_ShowCursor(0);
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024) == -1)
{
printf("Couldn't initialize SDL Mixer\n");
exit(1);
}
Mix_AllocateChannels(64);
Mix_Volume(-1, app.soundVolume * MIX_MAX_VOLUME / 10);
Mix_VolumeMusic(app.musicVolume * MIX_MAX_VOLUME / 10);
2016-02-22 13:47:41 +01:00
app.window = SDL_CreateWindow("TBFTSS - The Pandoran War", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, app.winWidth, app.winHeight, windowFlags);
2015-10-20 13:51:49 +02:00
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
app.renderer = SDL_CreateRenderer(app.window, -1, rendererFlags);
IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG);
if (TTF_Init() < 0)
{
printf("Couldn't initialize SDL TTF: %s\n", SDL_GetError());
exit(1);
}
app.backBuffer = SDL_CreateTexture(app.renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT);
app.scaleX = SCREEN_WIDTH;
app.scaleX /= app.winWidth;
app.scaleY = SCREEN_HEIGHT;
app.scaleY /= app.winHeight;
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Game scale factor: %.2f,%.2f\n", app.scaleX, app.scaleY);
}
void initGameSystem(void)
{
2016-02-27 13:14:29 +01:00
int step = 0;
int STEPS = 14;
2015-10-20 13:51:49 +02:00
initColor(&colors.red, 255, 0, 0);
initColor(&colors.orange, 255, 128, 0);
initColor(&colors.yellow, 255, 255, 0);
initColor(&colors.green, 0, 255, 0);
initColor(&colors.blue, 0, 0, 255);
initColor(&colors.cyan, 0, 255, 255);
initColor(&colors.purple, 255, 0, 255);
initColor(&colors.white, 255, 255, 255);
initColor(&colors.black, 0, 0, 0);
initColor(&colors.lightGrey, 192, 192, 192);
initColor(&colors.darkGrey, 128, 128, 128);
2016-02-27 13:14:29 +01:00
showLoadingStep(step++, STEPS);
initFonts();
2016-02-27 13:14:29 +01:00
showLoadingStep(step++, STEPS);
2015-11-23 15:52:11 +01:00
initInput();
2016-02-27 13:14:29 +01:00
showLoadingStep(step++, STEPS);
2015-10-20 13:51:49 +02:00
initLookups();
2016-02-27 13:14:29 +01:00
showLoadingStep(step++, STEPS);
2015-10-20 13:51:49 +02:00
initSounds();
2016-02-27 13:14:29 +01:00
showLoadingStep(step++, STEPS);
2015-10-20 13:51:49 +02:00
initWidgets();
2016-02-27 13:14:29 +01:00
showLoadingStep(step++, STEPS);
2015-10-20 13:51:49 +02:00
initGame();
2016-02-27 13:14:29 +01:00
showLoadingStep(step++, STEPS);
2015-10-20 13:51:49 +02:00
loadFighterDefs();
2016-02-27 13:14:29 +01:00
showLoadingStep(step++, STEPS);
2015-12-07 20:19:14 +01:00
loadCapitalShipDefs();
2016-02-27 13:14:29 +01:00
showLoadingStep(step++, STEPS);
2015-11-16 12:27:03 +01:00
loadItemDefs();
2016-02-27 13:14:29 +01:00
showLoadingStep(step++, STEPS);
2015-10-20 13:51:49 +02:00
initBulletDefs();
2016-02-27 13:14:29 +01:00
showLoadingStep(step++, STEPS);
2015-10-20 13:51:49 +02:00
initStarSystems();
2016-02-27 13:14:29 +01:00
showLoadingStep(step++, STEPS);
initChallenges();
showLoadingStep(step++, STEPS);
2015-10-20 13:51:49 +02:00
initBattle();
2016-02-27 13:14:29 +01:00
showLoadingStep(step++, STEPS);
2016-02-23 08:19:31 +01:00
initModalDialog();
2016-02-27 13:14:29 +01:00
showLoadingStep(step++, STEPS);
}
/*
* Just in case the initial loading takes a while on the target machine. The rest of the loading a pretty quick by comparison.
*/
static void showLoadingStep(float step, float maxSteps)
{
SDL_Rect r;
prepareScene();
r.w = SCREEN_WIDTH - 400;
r.h = 14;
r.x = (SCREEN_WIDTH / 2) - r.w / 2;
r.y = (SCREEN_HEIGHT / 2) - r.h / 2;
SDL_SetRenderDrawColor(app.renderer, 128, 128, 128, 255);
SDL_RenderDrawRect(app.renderer, &r);
r.w *= (step / maxSteps);
r.x += 2;
r.y += 2;
r.w -= 4;
r.h -= 4;
SDL_SetRenderDrawColor(app.renderer, 128, 196, 255, 255);
SDL_RenderFillRect(app.renderer, &r);
presentScene();
SDL_Delay(1);
2015-10-20 13:51:49 +02:00
}
2015-10-20 13:51:49 +02:00
static void initColor(SDL_Color *c, int r, int g, int b)
{
memset(c, 0, sizeof(SDL_Color));
c->r = r;
c->g = g;
c->b = b;
c->a = 255;
}
static void loadConfig(void)
{
cJSON *root;
char *text, *configFilename;
configFilename = getSaveFilePath("config.json");
if (fileExists(configFilename))
{
text = readFile(configFilename);
}
else
{
text = readFile(getFileLocation("data/app/config.json"));
2015-10-20 13:51:49 +02:00
}
root = cJSON_Parse(text);
app.winWidth = cJSON_GetObjectItem(root, "winWidth")->valueint;
app.winHeight = cJSON_GetObjectItem(root, "winHeight")->valueint;
app.vSync = cJSON_GetObjectItem(root, "vSync")->valueint;
app.fullscreen = cJSON_GetObjectItem(root, "fullscreen")->valueint;
app.musicVolume = cJSON_GetObjectItem(root, "musicVolume")->valueint;
app.soundVolume = cJSON_GetObjectItem(root, "soundVolume")->valueint;
cJSON_Delete(root);
free(text);
2015-11-02 20:07:38 +01:00
/* so that the player doesn't get confused if this is a new game */
saveConfig();
2015-10-20 13:51:49 +02:00
}
void saveConfig(void)
{
char *out, *configFilename;
cJSON *root;
configFilename = getSaveFilePath("config.json");
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Saving config ...");
root = cJSON_CreateObject();
cJSON_AddNumberToObject(root, "winWidth", app.winWidth);
cJSON_AddNumberToObject(root, "winHeight", app.winHeight);
cJSON_AddNumberToObject(root, "vSync", app.vSync);
cJSON_AddNumberToObject(root, "fullscreen", app.fullscreen);
cJSON_AddNumberToObject(root, "musicVolume", app.musicVolume);
cJSON_AddNumberToObject(root, "soundVolume", app.soundVolume);
out = cJSON_Print(root);
if (!writeFile(configFilename, out))
{
printf("Failed to save config\n");
}
cJSON_Delete(root);
free(out);
}
void cleanup(void)
{
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Cleaning up ...");
SDL_DestroyRenderer(app.renderer);
SDL_DestroyWindow(app.window);
destroyLookups();
destroyTextures();
2015-12-20 17:13:35 +01:00
expireTexts(1);
2015-10-20 13:51:49 +02:00
destroyFonts();
destroySounds();
destroyGame();
destroyFighterDefs();
2015-12-07 20:19:14 +01:00
destroyCapitalShipDefs();
2015-10-20 13:51:49 +02:00
destroyBulletDefs();
2015-11-16 12:27:03 +01:00
destroyItemDefs();
2015-10-20 13:51:49 +02:00
destroyStarSystems();
destroyBattle();
destroyGalacticMap();
destroyWidgets();
TTF_Quit();
SDL_Quit();
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Done");
}