annotate src/old_pc_options.c @ 0:c84446dfb3f5

initial add
author paulo@localhost
date Fri, 13 Mar 2009 00:39:12 -0700
parents
children
rev   line source
paulo@0 1 /* PC option screen for LOCKJAW, an implementation of the Soviet Mind Game
paulo@0 2
paulo@0 3 Copyright (C) 2006 Damian Yerrick <tepples+lj@spamcop.net>
paulo@0 4
paulo@0 5 This work is free software; you can redistribute it and/or modify
paulo@0 6 it under the terms of the GNU General Public License as published by
paulo@0 7 the Free Software Foundation; either version 2 of the License, or
paulo@0 8 (at your option) any later version.
paulo@0 9
paulo@0 10 This program is distributed in the hope that it will be useful,
paulo@0 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
paulo@0 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
paulo@0 13 GNU General Public License for more details.
paulo@0 14
paulo@0 15 You should have received a copy of the GNU General Public License
paulo@0 16 along with this program; if not, write to the Free Software
paulo@0 17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
paulo@0 18
paulo@0 19 Original game concept and design by Alexey Pajitnov.
paulo@0 20 The Software is not sponsored or endorsed by Alexey Pajitnov, Elorg,
paulo@0 21 or The Tetris Company LLC.
paulo@0 22
paulo@0 23 */
paulo@0 24
paulo@0 25 #include "ljpc.h"
paulo@0 26 #include <ctype.h>
paulo@0 27 #include "ljpath.h"
paulo@0 28 #include <string.h>
paulo@0 29 #include "options.h"
paulo@0 30 #include "ljlocale.h"
paulo@0 31
paulo@0 32 #define OPTIONS_OFFSET 0
paulo@0 33 #define optionsMenu (commonOptionsMenu + OPTIONS_OFFSET)
paulo@0 34
paulo@0 35
paulo@0 36
paulo@0 37 #if 0
paulo@0 38 static int getOptionValueByName(int row, const char *value) {
paulo@0 39 for (int x = 0; x < optionsMenu[row].nValues; ++x) {
paulo@0 40 if (!ustrcmp(value, optionsMenu[row].valueNames[x])) {
paulo@0 41 return x + optionsMenu[row].minValue;
paulo@0 42 }
paulo@0 43 }
paulo@0 44 return -1;
paulo@0 45 }
paulo@0 46
paulo@0 47 static void setOptionValueByNumber(struct LJPrefs *prefs,
paulo@0 48 int row, const char *value) {
paulo@0 49 if (value[0] >= '0' && value[0] <= '9') {
paulo@0 50 int num = atoi(value);
paulo@0 51 if (num >= optionsMenu[row].minValue
paulo@0 52 && num < optionsMenu[row].minValue + optionsMenu[row].nValues) {
paulo@0 53 prefs->number[row] = num;
paulo@0 54 }
paulo@0 55 }
paulo@0 56 }
paulo@0 57
paulo@0 58 static void setOptionValueByName(struct LJPrefs *prefs,
paulo@0 59 int row, const char *value) {
paulo@0 60 int num = getOptionValueByName(row, value);
paulo@0 61
paulo@0 62 if (num >= 0) {
paulo@0 63 prefs->number[row] = num;
paulo@0 64 }
paulo@0 65 }
paulo@0 66
paulo@0 67 static void setOptionValueByBoolean(struct LJPrefs *prefs,
paulo@0 68 int row, const char *value) {
paulo@0 69 int num = atoi(value);
paulo@0 70
paulo@0 71 if (num >= 0) {
paulo@0 72 prefs->number[row] = num ? 1 : 0;
paulo@0 73 }
paulo@0 74 }
paulo@0 75 #endif
paulo@0 76
paulo@0 77 /* parse_ini_line *******************
paulo@0 78 */
paulo@0 79 int parse_ini_line(const char *in, char *key, char *var, char *val)
paulo@0 80 {
paulo@0 81 int c;
paulo@0 82 char *kstart = key;
paulo@0 83
paulo@0 84 /* Skip whitespace before key */
paulo@0 85 while(*in && isspace(*in))
paulo@0 86 in++;
paulo@0 87
paulo@0 88 /* Parse key */
paulo@0 89 if(*in == '[') /* if we have a new key, load it */
paulo@0 90 {
paulo@0 91 in++;
paulo@0 92
paulo@0 93 /* Skip whitespace before actual key */
paulo@0 94 while(*in && isspace(*in))
paulo@0 95 in++;
paulo@0 96
paulo@0 97 for(c = *in++;
paulo@0 98 c != 0 && c != ']' && c != '\n' && c != '\r';
paulo@0 99 c = *in++)
paulo@0 100 {
paulo@0 101 if(!isspace(c))
paulo@0 102 *key++ = c;
paulo@0 103 }
paulo@0 104 *key = 0;
paulo@0 105 /* Strip whitespace after key */
paulo@0 106 do {
paulo@0 107 *key-- = 0;
paulo@0 108 } while(key >= kstart && isspace(*key));
paulo@0 109 }
paulo@0 110
paulo@0 111 /* Skip whitespace before variable */
paulo@0 112 while(*in && isspace(*in))
paulo@0 113 in++;
paulo@0 114 if(*in == 0) /* if there is no variable, don't do anything */
paulo@0 115 return 0;
paulo@0 116
paulo@0 117 for(c = *in++;
paulo@0 118 c != 0 && c != '=' && c != '\n' && c != '\r';
paulo@0 119 c = *in++)
paulo@0 120 {
paulo@0 121 if(!isspace(c))
paulo@0 122 {
paulo@0 123 *var++ = c;
paulo@0 124 }
paulo@0 125 }
paulo@0 126 *var = 0;
paulo@0 127
paulo@0 128 /* Skip whitespace before value */
paulo@0 129 while(*in && isspace(*in))
paulo@0 130 in++;
paulo@0 131
paulo@0 132 /* Get value */
paulo@0 133 kstart = val;
paulo@0 134 for(c = *in++;
paulo@0 135 c != 0 && c != '\n' && c != '\r';
paulo@0 136 c = *in++)
paulo@0 137 {
paulo@0 138 *val++ = c;
paulo@0 139 }
paulo@0 140 /* Strip whitespace after value */
paulo@0 141 do {
paulo@0 142 *val-- = 0;
paulo@0 143 } while(val >= kstart && isspace(*val));
paulo@0 144
paulo@0 145 return 0;
paulo@0 146 }
paulo@0 147
paulo@0 148 static const char ljIniName[] = "lj.ini";
paulo@0 149
paulo@0 150 /**
paulo@0 151 * Finds a FourCC in a list.
paulo@0 152 * @param needle the fourCC to search for
paulo@0 153 * @param haystack a list of fourCCs
paulo@0 154 * @param haystackLen the length of this list
paulo@0 155 * @return the index within haystack where needle was found
paulo@0 156 * or -1 if not found
paulo@0 157 */
paulo@0 158 int findFourCC(const char *needle,
paulo@0 159 const FourCC *haystack, size_t haystackLen) {
paulo@0 160 for (size_t i = 0; i < haystackLen; ++i) {
paulo@0 161 if (!strncmp(haystack[i].c, needle, 4)) {
paulo@0 162 return i;
paulo@0 163 }
paulo@0 164 }
paulo@0 165 return -1;
paulo@0 166 }
paulo@0 167
paulo@0 168 void loadOption(struct LJPrefs *prefs,
paulo@0 169 const char *name, const char *value) {
paulo@0 170 for (int i = 0; i < PC_OPTIONS_MENU_LEN; ++i) {
paulo@0 171 if (!strncmp(optionsMenu[i].name.c, name, 4)) {
paulo@0 172 if (isdigit(value[0])) {
paulo@0 173 unsigned long int n = strtoul(value, NULL, 10);
paulo@0 174 if (n >= optionsMenu[i].minValue
paulo@0 175 && n < optionsMenu[i].minValue + optionsMenu[i].nValues) {
paulo@0 176 prefs->number[i] = n;
paulo@0 177 } else {
paulo@0 178 allegro_message("ignoring out of range %s=%lu\n",
paulo@0 179 name, n);
paulo@0 180 }
paulo@0 181 } else if (optionsMenu[i].valueNames) {
paulo@0 182 int nValueNames = (optionsMenu[i].style == OPTSTYLE_FRAMES
paulo@0 183 ? 2
paulo@0 184 : optionsMenu[i].nValues);
paulo@0 185 int foundValue = findFourCC(value, optionsMenu[i].valueNames, nValueNames);
paulo@0 186 if (optionsMenu[i].style == OPTSTYLE_FRAMES
paulo@0 187 && foundValue > 0) {
paulo@0 188 foundValue = optionsMenu[i].nValues - 1;
paulo@0 189 }
paulo@0 190 prefs->number[i] = foundValue + optionsMenu[i].minValue;
paulo@0 191 }
paulo@0 192 return;
paulo@0 193 }
paulo@0 194 }
paulo@0 195 }
paulo@0 196
paulo@0 197 int loadOptions(struct LJPrefs *prefs) {
paulo@0 198 FILE *fp = ljfopen(ljIniName, "rt");
paulo@0 199 char key[1024], var[1024], val[1024], input_buf[1024];
paulo@0 200
paulo@0 201 if (!fp) return 0;
paulo@0 202
paulo@0 203 key[0] = 0;
paulo@0 204 var[0] = 0;
paulo@0 205 val[0] = 0;
paulo@0 206
paulo@0 207 while(fgets (input_buf, sizeof(input_buf), fp)) {
paulo@0 208 parse_ini_line(input_buf, key, var, val);
paulo@0 209
paulo@0 210 if(!ustrcmp ("Skin", var)) {
paulo@0 211 ustrzcpy(skinName, sizeof(skinName) - 1, val);
paulo@0 212 } else {
paulo@0 213 loadOption(prefs, var, val);
paulo@0 214 }
paulo@0 215 }
paulo@0 216 fclose(fp);
paulo@0 217
paulo@0 218 if (prefs->number[OPTIONS_SIDEWAYS_DELAY] < prefs->number[OPTIONS_SIDEWAYS_SPEED]) {
paulo@0 219 prefs->number[OPTIONS_SIDEWAYS_DELAY] = prefs->number[OPTIONS_SIDEWAYS_SPEED];
paulo@0 220 }
paulo@0 221
paulo@0 222 return 0;
paulo@0 223 }
paulo@0 224
paulo@0 225 void saveOption(int i, int n, FILE *out) {
paulo@0 226 char name[8], value[8];
paulo@0 227 int nameIdx = -1;
paulo@0 228
paulo@0 229 strncpy(name, optionsMenu[i].name.c, 4);
paulo@0 230 name[4] = 0;
paulo@0 231
paulo@0 232 if (optionsMenu[i].valueNames) {
paulo@0 233 if (optionsMenu[i].style == OPTSTYLE_FRAMES) {
paulo@0 234 if (n == optionsMenu[i].minValue) {
paulo@0 235 nameIdx = 0;
paulo@0 236 } else if (n == optionsMenu[i].minValue + optionsMenu[i].nValues - 1) {
paulo@0 237 nameIdx = 1;
paulo@0 238 }
paulo@0 239 } else {
paulo@0 240 nameIdx = n - optionsMenu[i].minValue;
paulo@0 241 }
paulo@0 242 if (nameIdx >= 0
paulo@0 243 && optionsMenu[i].valueNames[nameIdx].i == 0) {
paulo@0 244 nameIdx = -1;
paulo@0 245 }
paulo@0 246 }
paulo@0 247
paulo@0 248 if (nameIdx >= 0) {
paulo@0 249 strncpy(value, optionsMenu[i].valueNames[nameIdx].c, 4);
paulo@0 250 value[4] = 0;
paulo@0 251 fprintf(out, "%s=%s\n",
paulo@0 252 name, value);
paulo@0 253 } else {
paulo@0 254 fprintf(out, "%s=%d\n",
paulo@0 255 name, n);
paulo@0 256 }
paulo@0 257 }
paulo@0 258
paulo@0 259 void saveOptions(const struct LJPrefs *prefs) {
paulo@0 260 FILE *out = ljfopen(ljIniName, "wt");
paulo@0 261
paulo@0 262 if (out) {
paulo@0 263 for (int i = 0; i < PC_OPTIONS_MENU_LEN; ++i) {
paulo@0 264 saveOption(i, prefs->number[i], out);
paulo@0 265 }
paulo@0 266 fprintf(out, "Skin=%s\n",
paulo@0 267 skinName);
paulo@0 268 fclose(out);
paulo@0 269 }
paulo@0 270 }
paulo@0 271
paulo@0 272 void unpackOptions(LJView *v, const struct LJPrefs *prefs) {
paulo@0 273 unpackCommonOptions(v, prefs->number);
paulo@0 274
paulo@0 275 v->plat->nextAbove = prefs->number[OPTIONS_NEXT_ABOVE];
paulo@0 276 v->showTrails = prefs->number[OPTIONS_TRAILS];
paulo@0 277 autoPause = prefs->number[OPTIONS_AUTO_PAUSE];
paulo@0 278 }
paulo@0 279
paulo@0 280 /********************************************************************
paulo@0 281
paulo@0 282 Allegro based option drawing code
paulo@0 283
paulo@0 284 ********************************************************************/
paulo@0 285
paulo@0 286 #define OPTIONS_TOP 100
paulo@0 287 #define OPTIONS_ROW_HT 40
paulo@0 288 #define OPTIONS_ROW_LEFT 80
paulo@0 289 #define OPTIONS_ROW_MID 400
paulo@0 290 #define OPTIONS_ROW_RIGHT 760
paulo@0 291 #define OPTIONS_MENU_VIS 7
paulo@0 292 #define OPTIONS_FONT aver32
paulo@0 293
paulo@0 294
paulo@0 295 /**
paulo@0 296 * @param y number of option to draw
paulo@0 297 * @param hilite bitfield: 1=focused, 0=not
paulo@0 298 */
paulo@0 299 void optionsDrawRow(const unsigned char *prefs,
paulo@0 300 int dstY, int line, int value, int hilite) {
paulo@0 301 unsigned int ht = text_height(OPTIONS_FONT);
paulo@0 302 int buttonY = OPTIONS_TOP + OPTIONS_ROW_HT * dstY;
paulo@0 303 int rowBg = bgColor;
paulo@0 304 const char *nameText;
paulo@0 305 char altNameText[8];
paulo@0 306 char valueText[OPTIONS_VALUE_LEN];
paulo@0 307 const char *valueOverride = isDisabledOption(prefs, line);
paulo@0 308 int textcolor = fgColor;
paulo@0 309 const char *valueDesc = NULL;
paulo@0 310
paulo@0 311 {
paulo@0 312 nameText = ljGetFourCCName(optionsMenu[line].name);
paulo@0 313 if (!nameText) {
paulo@0 314 strncpy(altNameText, optionsMenu[line].name.c, 4);
paulo@0 315 altNameText[4] = 0;
paulo@0 316 nameText = altNameText;
paulo@0 317 }
paulo@0 318 }
paulo@0 319
paulo@0 320 if (valueOverride) {
paulo@0 321 hilite |= 2;
paulo@0 322 textcolor = makecol(128, 128, 128);
paulo@0 323 }
paulo@0 324
paulo@0 325 if (hilite == 3) {
paulo@0 326 rowBg = makecol(204, 204, 204);
paulo@0 327 } else if (hilite == 1) {
paulo@0 328 rowBg = hiliteColor;
paulo@0 329 }
paulo@0 330
paulo@0 331 // If the value of this option is within range, format it as a string.
paulo@0 332 if (value >= optionsMenu[line].minValue
paulo@0 333 && value < optionsMenu[line].minValue + optionsMenu[line].nValues) {
paulo@0 334 if (valueOverride) {
paulo@0 335 ustrzcpy(valueText, sizeof(valueText), "overridden");
paulo@0 336 valueDesc = valueOverride;
paulo@0 337 } else {
paulo@0 338 valueDesc = getOptionsValueStr(valueText, line + OPTIONS_OFFSET, value);
paulo@0 339 }
paulo@0 340 } else {
paulo@0 341 valueText[0] = '\0';
paulo@0 342 }
paulo@0 343
paulo@0 344 // draw current option
paulo@0 345 acquire_screen();
paulo@0 346 rectfill(screen,
paulo@0 347 OPTIONS_ROW_LEFT, buttonY,
paulo@0 348 OPTIONS_ROW_RIGHT - 1, buttonY + OPTIONS_ROW_HT - 1,
paulo@0 349 rowBg);
paulo@0 350 textout_ex(screen, OPTIONS_FONT, nameText,
paulo@0 351 OPTIONS_ROW_LEFT + 8, buttonY + (OPTIONS_ROW_HT - ht) / 2,
paulo@0 352 textcolor, rowBg);
paulo@0 353 textout_ex(screen, OPTIONS_FONT,
paulo@0 354 valueText,
paulo@0 355 OPTIONS_ROW_MID, buttonY + (OPTIONS_ROW_HT - ht) / 2,
paulo@0 356 textcolor, rowBg);
paulo@0 357
paulo@0 358 // For an enabled selected item, draw the frame
paulo@0 359 if (hilite == 1) {
paulo@0 360 rect(screen,
paulo@0 361 OPTIONS_ROW_LEFT, buttonY,
paulo@0 362 OPTIONS_ROW_RIGHT - 1, buttonY + OPTIONS_ROW_HT - 1,
paulo@0 363 fgColor);
paulo@0 364 }
paulo@0 365
paulo@0 366 // For a selected item, draw the help text
paulo@0 367 if (hilite & 1) {
paulo@0 368 buttonY = OPTIONS_TOP + OPTIONS_ROW_HT * OPTIONS_MENU_VIS;
paulo@0 369 rectfill(screen,
paulo@0 370 OPTIONS_ROW_LEFT, buttonY,
paulo@0 371 OPTIONS_ROW_RIGHT - 1, buttonY + OPTIONS_ROW_HT * 5 / 2 - 1,
paulo@0 372 bgColor);
paulo@0 373
paulo@0 374 const char *descText = ljGetFourCCDesc(optionsMenu[line].name);
paulo@0 375 if (descText) {
paulo@0 376 textout_ex(screen, OPTIONS_FONT, descText,
paulo@0 377 OPTIONS_ROW_LEFT,
paulo@0 378 buttonY + OPTIONS_ROW_HT - ht / 2,
paulo@0 379 textcolor, bgColor);
paulo@0 380 }
paulo@0 381 if (valueDesc) {
paulo@0 382 textout_ex(screen, OPTIONS_FONT, valueDesc,
paulo@0 383 OPTIONS_ROW_LEFT,
paulo@0 384 buttonY + 2 * OPTIONS_ROW_HT - ht / 2,
paulo@0 385 textcolor, bgColor);
paulo@0 386 }
paulo@0 387 }
paulo@0 388
paulo@0 389 release_screen();
paulo@0 390 }
paulo@0 391
paulo@0 392 void optionsClearRow(int dstY, int hilite) {
paulo@0 393 int buttonY = OPTIONS_TOP + OPTIONS_ROW_HT * dstY;
paulo@0 394 rectfill(screen,
paulo@0 395 OPTIONS_ROW_LEFT, buttonY,
paulo@0 396 OPTIONS_ROW_RIGHT - 1, buttonY + OPTIONS_ROW_HT * 5 / 2 - 1,
paulo@0 397 bgColor);
paulo@0 398 }
paulo@0 399
paulo@0 400 void optionsDrawPage(int page, const unsigned char *prefs) {
paulo@0 401 int nPages = 0;
paulo@0 402 for (; optionsPages[nPages].name; ++nPages) { }
paulo@0 403
paulo@0 404 vsync();
paulo@0 405 acquire_screen();
paulo@0 406 rectfill(screen, 320, 32, SCREEN_W - 1, 63, bgColor);
paulo@0 407 textprintf_right_ex(screen, aver32,
paulo@0 408 SCREEN_W - 16, 32, fgColor, -1,
paulo@0 409 "(%d/%d) %s",
paulo@0 410 page + 1, nPages, optionsPages[page].name);
paulo@0 411
paulo@0 412 for (int i = optionsPages[page].start;
paulo@0 413 i < optionsPages[page + 1].start; ++i) {
paulo@0 414 optionsDrawRow(prefs, i - optionsPages[page].start,
paulo@0 415 i, prefs[i], 0);
paulo@0 416 }
paulo@0 417 for (int i = optionsPages[page + 1].start;
paulo@0 418 i < optionsPages[page].start + OPTIONS_MENU_VIS; ++i) {
paulo@0 419 optionsClearRow(i - optionsPages[page].start, 0);
paulo@0 420 }
paulo@0 421 release_screen();
paulo@0 422 }
paulo@0 423
paulo@0 424 void optionsWinInit(void) {
paulo@0 425 acquire_screen();
paulo@0 426 clear_to_color(screen, bgColor);
paulo@0 427 textout_ex(screen, aver32, "LOCKJAW > Options", 16, 32, fgColor, -1);
paulo@0 428 textout_ex(screen, aver32, "Rotate: change page; Up/Down: select", 16, 522, fgColor, -1);
paulo@0 429 textout_ex(screen, aver32, "Left/Right change; Enter: exit", 16, 552, fgColor, -1);
paulo@0 430 release_screen();
paulo@0 431
paulo@0 432 }
paulo@0 433
paulo@0 434 void optionsIdle(void) {
paulo@0 435 rest(10);
paulo@0 436 }