tbftss/src/game/options.c

207 lines
5.1 KiB
C
Raw Normal View History

2015-10-20 13:51:49 +02:00
/*
2019-01-01 17:14:11 +01:00
Copyright (C) 2015-2019 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 "options.h"
static void changeWindowSize(char *value);
static void changeSoundVolume(char *value);
static void changeMusicVolume(char *value);
static void changeFullscreen(char *value);
static void ok(void);
2016-03-04 23:11:13 +01:00
static void controlsOK(void);
static void drawMain(void);
static void controls(void);
2015-10-20 13:51:49 +02:00
static void (*returnFromOptions)(void);
2016-03-04 23:11:13 +01:00
static int show;
2016-07-19 10:26:19 +02:00
static char *OPTIONS_TEXT;
static char *RESOLUTION_TEXT;
2015-10-20 13:51:49 +02:00
void initOptions(void (*rtn)(void))
{
char optionStr[MAX_NAME_LENGTH];
2016-03-03 19:03:07 +01:00
2015-10-20 13:51:49 +02:00
selectWidget("windowSize", "options");
2016-03-03 19:03:07 +01:00
2015-10-20 13:51:49 +02:00
getWidget("windowSize", "options")->onChange = changeWindowSize;
getWidget("soundVolume", "options")->onChange = changeSoundVolume;
getWidget("musicVolume", "options")->onChange = changeMusicVolume;
getWidget("fullscreen", "options")->onChange = changeFullscreen;
2016-03-04 23:11:13 +01:00
getWidget("controls", "options")->action = controls;
2015-10-20 13:51:49 +02:00
getWidget("ok", "options")->action = ok;
2016-03-04 23:11:13 +01:00
getWidget("ok", "controls")->action = controlsOK;
2016-03-03 19:03:07 +01:00
2015-10-20 13:51:49 +02:00
sprintf(optionStr, "%d x %d", app.winWidth, app.winHeight);
setWidgetOption("windowSize", "options", optionStr);
2016-03-03 19:03:07 +01:00
2015-10-20 13:51:49 +02:00
sprintf(optionStr, "%d", app.soundVolume);
setWidgetOption("soundVolume", "options", optionStr);
2016-03-03 19:03:07 +01:00
2015-10-20 13:51:49 +02:00
sprintf(optionStr, "%d", app.musicVolume);
setWidgetOption("musicVolume", "options", optionStr);
2016-03-03 19:03:07 +01:00
2015-10-20 13:51:49 +02:00
setWidgetOption("fullscreen", "options", app.fullscreen ? "On" : "Off");
2016-07-19 10:26:19 +02:00
OPTIONS_TEXT = _("Options");
RESOLUTION_TEXT = _("Note: you must restart the game for window size and fullscreen options to take effect.");
2016-03-03 19:03:07 +01:00
#if FIXED_RESOLUTION
getWidget("windowSize", "options")->enabled = 0;
getWidget("fullscreen", "options")->enabled = 0;
2016-07-19 10:26:19 +02:00
RESOLUTION_TEXT = _("Note: this device does not support changing the screen resolution.");
2016-03-03 19:03:07 +01:00
#endif
2015-10-20 13:51:49 +02:00
returnFromOptions = rtn;
2016-03-04 23:11:13 +01:00
show = SHOW_MAIN;
2015-10-20 13:51:49 +02:00
}
void drawOptions(void)
2016-03-04 23:11:13 +01:00
{
switch (show)
{
case SHOW_MAIN:
drawMain();
break;
case SHOW_CONTROLS:
drawControls();
break;
}
}
static void drawMain(void)
2015-10-20 13:51:49 +02:00
{
SDL_Rect r;
2018-12-12 09:32:32 +01:00
2015-10-20 13:51:49 +02:00
SDL_SetRenderDrawBlendMode(app.renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, 128);
SDL_RenderFillRect(app.renderer, NULL);
SDL_SetRenderDrawBlendMode(app.renderer, SDL_BLENDMODE_NONE);
2018-12-17 09:30:47 +01:00
SDL_SetRenderTarget(app.renderer, app.uiBuffer);
2016-03-03 19:03:07 +01:00
2015-10-20 13:51:49 +02:00
r.w = 500;
r.h = 600;
2018-12-12 09:32:32 +01:00
r.x = (UI_WIDTH / 2) - r.w / 2;
r.y = (UI_HEIGHT / 2) - r.h / 2;
2016-03-03 19:03:07 +01:00
2018-12-11 09:24:25 +01:00
SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, 255);
2015-10-20 13:51:49 +02:00
SDL_RenderFillRect(app.renderer, &r);
SDL_SetRenderDrawColor(app.renderer, 200, 200, 200, 255);
SDL_RenderDrawRect(app.renderer, &r);
2016-03-03 19:03:07 +01:00
2018-12-12 09:32:32 +01:00
drawText(UI_WIDTH / 2, 70, 28, TA_CENTER, colors.white, OPTIONS_TEXT);
2016-03-03 19:03:07 +01:00
2015-10-20 13:51:49 +02:00
SDL_SetRenderDrawColor(app.renderer, 128, 128, 128, 255);
SDL_RenderDrawLine(app.renderer, r.x, 120, r.x + r.w, 120);
2016-03-03 19:03:07 +01:00
drawWidgets("options");
2018-12-06 09:37:19 +01:00
app.textWidth = r.w - 100;
2018-12-12 09:32:32 +01:00
drawText(UI_WIDTH / 2, r.y + r.h - 135, 16, TA_CENTER, colors.yellow, RESOLUTION_TEXT);
2018-12-06 09:37:19 +01:00
app.textWidth = 0;
2018-12-12 09:32:32 +01:00
SDL_SetRenderTarget(app.renderer, app.backBuffer);
2015-10-20 13:51:49 +02:00
}
void updateCustomResolutionOption(void)
{
Widget *w;
char value[MAX_NAME_LENGTH];
int i;
sprintf(value, "%d x %d", app.winWidth, app.winHeight);
w = getWidget("windowSize", "options");
for (i = 0 ; i < w->numOptions - 1 ; i++)
{
if (strcmp(w->options[i], value) == 0)
{
w->numOptions--;
return;
}
}
w->options[w->numOptions - 1] = malloc(strlen(value) + 1);
strcpy(w->options[w->numOptions - 1], value);
}
2016-03-04 23:11:13 +01:00
static void controls(void)
{
initControlsDisplay();
show = SHOW_CONTROLS;
}
2015-10-20 13:51:49 +02:00
static void changeWindowSize(char *value)
{
sscanf(value, "%d x %d", &app.winWidth, &app.winHeight);
2018-12-17 09:30:47 +01:00
SDL_SetWindowSize(app.window, app.winWidth, app.winHeight);
app.uiOffset.x = (app.winWidth / 2) - (UI_WIDTH / 2);
app.uiOffset.y = (app.winHeight / 2) - (UI_HEIGHT / 2);
SDL_DestroyTexture(app.backBuffer);
app.backBuffer = SDL_CreateTexture(app.renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, app.winWidth, app.winHeight);
initGraphics();
initStars();
2015-10-20 13:51:49 +02:00
}
static void changeSoundVolume(char *value)
{
app.soundVolume = atoi(value);
2016-03-03 19:03:07 +01:00
2015-10-20 13:51:49 +02:00
Mix_Volume(-1, app.soundVolume * MIX_MAX_VOLUME / 10);
}
static void changeMusicVolume(char *value)
{
app.musicVolume = atoi(value);
2016-03-03 19:03:07 +01:00
2015-10-20 13:51:49 +02:00
Mix_VolumeMusic(app.musicVolume * MIX_MAX_VOLUME / 10);
}
static void changeFullscreen(char *value)
{
app.fullscreen = strcmp(value, "On") == 0;
SDL_SetWindowFullscreen(app.window, app.fullscreen? SDL_WINDOW_FULLSCREEN : 0);
2015-10-20 13:51:49 +02:00
}
static void ok(void)
{
saveConfig();
2016-03-03 19:03:07 +01:00
2015-10-20 13:51:49 +02:00
returnFromOptions();
}
2016-03-04 23:11:13 +01:00
static void controlsOK(void)
{
show = SHOW_MAIN;
}