diff --git a/data/widgets/controls.json b/data/widgets/controls.json new file mode 100644 index 0000000..9bd9c41 --- /dev/null +++ b/data/widgets/controls.json @@ -0,0 +1,111 @@ +[ + { + "name" : "fire", + "group" : "controls", + "type" : "WT_CONTROL_CONFIG", + "x" : 0, + "y" : 0, + "w" : 175, + "h": 34 + }, + { + "name" : "accelerate", + "group" : "controls", + "type" : "WT_CONTROL_CONFIG", + "x" : 0, + "y" : 0, + "w" : 175, + "h": 34 + }, + { + "name" : "missile", + "group" : "controls", + "type" : "WT_CONTROL_CONFIG", + "x" : 0, + "y" : 0, + "w" : 175, + "h": 34 + }, + { + "name" : "boost", + "group" : "controls", + "type" : "WT_CONTROL_CONFIG", + "x" : 0, + "y" : 0, + "w" : 175, + "h": 34 + }, + { + "name" : "brake", + "group" : "controls", + "type" : "WT_CONTROL_CONFIG", + "x" : 0, + "y" : 0, + "w" : 175, + "h": 34 + }, + { + "name" : "ecm", + "group" : "controls", + "type" : "WT_CONTROL_CONFIG", + "x" : 0, + "y" : 0, + "w" : 175, + "h": 34 + }, + { + "name" : "target", + "group" : "controls", + "type" : "WT_CONTROL_CONFIG", + "x" : 0, + "y" : 0, + "w" : 175, + "h": 34 + }, + { + "name" : "radar", + "group" : "controls", + "type" : "WT_CONTROL_CONFIG", + "x" : 0, + "y" : 0, + "w" : 175, + "h": 34 + }, + { + "name" : "guns", + "group" : "controls", + "type" : "WT_CONTROL_CONFIG", + "x" : 0, + "y" : 0, + "w" : 175, + "h": 34 + }, + { + "name" : "prevFighter", + "group" : "controls", + "type" : "WT_CONTROL_CONFIG", + "x" : 0, + "y" : 0, + "w" : 175, + "h": 34 + }, + { + "name" : "nextFighter", + "group" : "controls", + "type" : "WT_CONTROL_CONFIG", + "x" : 0, + "y" : 0, + "w" : 175, + "h": 34 + }, + { + "name" : "ok", + "group" : "controls", + "type" : "WT_BUTTON", + "text" : "OK", + "x" : -1, + "y" : 650, + "w" : 100, + "h": 34 + } +] diff --git a/src/defs.h b/src/defs.h index 63fc269..e45cda7 100644 --- a/src/defs.h +++ b/src/defs.h @@ -128,8 +128,8 @@ enum CONTROL_MISSILE, CONTROL_GUNS, CONTROL_RADAR, - CONTROL_NEXT_FIGHTER, CONTROL_PREV_FIGHTER, + CONTROL_NEXT_FIGHTER, CONTROL_MAX }; @@ -271,8 +271,7 @@ enum WT_IMG_BUTTON, WT_SELECT, WT_SELECT_BUTTON, - WT_KEY_CONFIG, - WT_MOUSE_CONFIG + WT_CONTROL_CONFIG }; enum diff --git a/src/game/controls.c b/src/game/controls.c deleted file mode 100644 index fc6a176..0000000 --- a/src/game/controls.c +++ /dev/null @@ -1,71 +0,0 @@ -/* -Copyright (C) 2015-2016 Parallel Realities - -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 "controls.h" - -int isKeyControl(int type) -{ - return app.keyboard[app.keyControls[type]]; -} - -void clearControl(int type) -{ - app.keyboard[app.keyControls[type]] = 0; -} - -void initControlsDisplay(void) -{ - getWidget("keyMissile", "controls")->value = app.keyControls[CONTROL_MISSILE]; - getWidget("keyBoost", "controls")->value = app.keyControls[CONTROL_BOOST]; - getWidget("keyECM", "controls")->value = app.keyControls[CONTROL_ECM]; - getWidget("keyBrake", "controls")->value = app.keyControls[CONTROL_BRAKE]; - getWidget("keyRadar", "controls")->value = app.keyControls[CONTROL_RADAR]; - getWidget("keyTarget", "controls")->value = app.keyControls[CONTROL_TARGET]; - getWidget("keyGuns", "controls")->value = app.keyControls[CONTROL_GUNS]; - getWidget("keyPrevFighter", "controls")->value = app.keyControls[CONTROL_PREV_FIGHTER]; - getWidget("keyNextFighter", "controls")->value = app.keyControls[CONTROL_NEXT_FIGHTER]; -} - -void drawControls(void) -{ - SDL_Rect r; - - 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); - - r.w = 800; - r.h = 650; - r.x = (SCREEN_WIDTH / 2) - r.w / 2; - r.y = (SCREEN_HEIGHT / 2) - r.h / 2; - - SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, 0); - SDL_RenderFillRect(app.renderer, &r); - SDL_SetRenderDrawColor(app.renderer, 200, 200, 200, 255); - SDL_RenderDrawRect(app.renderer, &r); - - drawText(SCREEN_WIDTH / 2, 50, 28, TA_CENTER, colors.white, _("Controls")); - - SDL_SetRenderDrawColor(app.renderer, 128, 128, 128, 255); - SDL_RenderDrawLine(app.renderer, r.x, r.y + 65, r.x + r.w, r.y + 65); - - drawWidgets("controls"); -} diff --git a/src/system/controls.c b/src/system/controls.c new file mode 100644 index 0000000..8d9e581 --- /dev/null +++ b/src/system/controls.c @@ -0,0 +1,128 @@ +/* +Copyright (C) 2015-2016 Parallel Realities + +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 "controls.h" + +static const char *controlName[CONTROL_MAX]; +static Widget *controlWidget[CONTROL_MAX]; +static const char *widgetNames[] = {"fire", "accelerate", "boost", "ecm", "brake", "target", "missile", "guns", "radar", "nextFighter", "prevFighter"}; + +void initControls(void) +{ + int i; + + controlName[CONTROL_FIRE] = _("Fire"); + controlName[CONTROL_ACCELERATE] = _("Accelerate"); + controlName[CONTROL_BOOST] = _("Boost"); + controlName[CONTROL_ECM] = _("ECM"); + controlName[CONTROL_BRAKE] = _("Brake"); + controlName[CONTROL_TARGET] = _("Select Target"); + controlName[CONTROL_MISSILE] = _("Fire Missile"); + controlName[CONTROL_GUNS] = _("Cycle Guns"); + controlName[CONTROL_RADAR] = _("Cycle Radar"); + controlName[CONTROL_NEXT_FIGHTER] = _("Next Fighter"); + controlName[CONTROL_PREV_FIGHTER] = _("Previous Fighter"); + + for (i = 0 ; i < CONTROL_MAX ; i++) + { + controlWidget[i] = getWidget(widgetNames[i], "controls"); + controlWidget[i]->numOptions = 2; + controlWidget[i]->options = malloc(2 * sizeof(char*)); + controlWidget[i]->options[0] = malloc(sizeof(char) * MAX_NAME_LENGTH); + controlWidget[i]->options[1] = malloc(sizeof(char) * MAX_NAME_LENGTH); + strcpy(controlWidget[i]->options[0], ""); + strcpy(controlWidget[i]->options[1], ""); + } +} + +int isKeyControl(int type) +{ + return app.keyboard[app.keyControls[type]]; +} + +void clearControl(int type) +{ + app.keyboard[app.keyControls[type]] = 0; +} + +void initControlsDisplay(void) +{ + int i; + + for (i = 0 ; i < CONTROL_MAX ; i++) + { + if (app.mouseControls[i] != 0) + { + sprintf(controlWidget[i]->options[0], "%s", SDL_GetScancodeName(app.keyControls[i])); + } + + if (app.mouseControls[i] != -1) + { + sprintf(controlWidget[i]->options[1], "Btn %d", app.mouseControls[i]); + } + } +} + +void drawControls(void) +{ + int i; + SDL_Rect r; + + 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); + + r.w = 800; + r.h = 650; + r.x = (SCREEN_WIDTH / 2) - r.w / 2; + r.y = (SCREEN_HEIGHT / 2) - r.h / 2; + + SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, 0); + SDL_RenderFillRect(app.renderer, &r); + SDL_SetRenderDrawColor(app.renderer, 200, 200, 200, 255); + SDL_RenderDrawRect(app.renderer, &r); + + drawText(SCREEN_WIDTH / 2, 50, 28, TA_CENTER, colors.white, _("Controls")); + + SDL_SetRenderDrawColor(app.renderer, 128, 128, 128, 255); + SDL_RenderDrawLine(app.renderer, r.x, r.y + 65, r.x + r.w, r.y + 65); + + r.x += 25; + r.y = 125; + + for (i = 0 ; i < CONTROL_MAX ; i++) + { + drawText(r.x, r.y, 20, TA_LEFT, colors.white, controlName[i]); + + controlWidget[i]->rect.x = r.x + 175; + controlWidget[i]->rect.y = r.y; + + r.y += 50; + + if (r.y > 400) + { + r.y = 125; + r.x += 400; + } + } + + drawWidgets("controls"); +} diff --git a/src/game/controls.h b/src/system/controls.h similarity index 100% rename from src/game/controls.h rename to src/system/controls.h diff --git a/src/system/init.c b/src/system/init.c index 1914da1..507c0b8 100644 --- a/src/system/init.c +++ b/src/system/init.c @@ -133,7 +133,8 @@ void initGameSystem(void) initStats, initBattle, initModalDialog, - initBackground + initBackground, + initControls }; numInitFuns = sizeof(initFuncs) / sizeof(void*); diff --git a/src/system/init.h b/src/system/init.h index 6d9faea..78133cb 100644 --- a/src/system/init.h +++ b/src/system/init.h @@ -48,6 +48,7 @@ extern void initChallenges(void); extern void initWidgets(void); extern void initBackground(void); extern void initResources(void); +extern void initControls(void); extern void destroyLookups(void); extern void destroyFonts(void); extern void destroySounds(void); diff --git a/src/system/lookup.c b/src/system/lookup.c index 04aef2e..f499d10 100644 --- a/src/system/lookup.c +++ b/src/system/lookup.c @@ -91,8 +91,7 @@ void initLookups(void) addLookup("WT_BUTTON", WT_BUTTON); addLookup("WT_SELECT", WT_SELECT); addLookup("WT_IMG_BUTTON", WT_IMG_BUTTON); - addLookup("WT_KEY_CONFIG", WT_KEY_CONFIG); - addLookup("WT_MOUSE_CONFIG", WT_MOUSE_CONFIG); + addLookup("WT_CONTROL_CONFIG", WT_CONTROL_CONFIG); addLookup("SIDE_ALLIES", SIDE_ALLIES); addLookup("SIDE_PIRATE", SIDE_PIRATE); diff --git a/src/system/widgets.c b/src/system/widgets.c index 68a8c90..7a5c7c3 100644 --- a/src/system/widgets.c +++ b/src/system/widgets.c @@ -147,16 +147,24 @@ void drawWidgets(const char *group) drawText(w->rect.x + w->rect.w - 10, w->rect.y + 2, 20, TA_RIGHT, colors.white, w->options[w->currentOption]); break; - case WT_KEY_CONFIG: + case WT_CONTROL_CONFIG: SDL_RenderDrawRect(app.renderer, &w->rect); - drawText(w->rect.x + 25, w->rect.y + 2, 20, TA_LEFT, colors.white, w->text); - drawText(w->rect.x + w->rect.w - 25, w->rect.y + 2, 20, TA_RIGHT, colors.white, "%d", w->value); - break; - - case WT_MOUSE_CONFIG: - SDL_RenderDrawRect(app.renderer, &w->rect); - drawText(w->rect.x + 25, w->rect.y + 2, 20, TA_LEFT, colors.white, w->text); - drawText(w->rect.x + w->rect.w - 25, w->rect.y + 2, 20, TA_RIGHT, colors.white, "%d", w->value); + if (strlen(w->options[0]) && strlen(w->options[1])) + { + drawText(w->rect.x + (w->rect.w / 2), w->rect.y + 2, 20, TA_CENTER, colors.white, "%s or %s", w->options[0], w->options[1]); + } + else if (strlen(w->options[0])) + { + drawText(w->rect.x + (w->rect.w / 2), w->rect.y + 2, 20, TA_CENTER, colors.white, "%s", w->options[0]); + } + else if (strlen(w->options[1])) + { + drawText(w->rect.x + (w->rect.w / 2), w->rect.y + 2, 20, TA_CENTER, colors.white, "%s", w->options[1]); + } + else + { + drawText(w->rect.x + (w->rect.w / 2), w->rect.y + 2, 20, TA_CENTER, colors.white, ""); + } break; } @@ -335,9 +343,7 @@ static void loadWidgetSet(char *filename) createOptions(w, cJSON_GetObjectItem(node, "options")->valuestring); break; - case WT_KEY_CONFIG: - case WT_MOUSE_CONFIG: - STRNCPY(w->text, _(cJSON_GetObjectItem(node, "text")->valuestring), MAX_NAME_LENGTH); + case WT_CONTROL_CONFIG: w->rect.w = cJSON_GetObjectItem(node, "w")->valueint; w->rect.h = cJSON_GetObjectItem(node, "h")->valueint; break;