helpsys.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* $Id$
  2. * MegaZeux
  3. *
  4. * Copyright (C) 1996 Greg Janson
  5. * Copyright (C) 1998 Matthew D. Williams - dbwilli@scsn.net
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. // internal help system
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include "scrdisp.h"
  26. #include "helpsys.h"
  27. #include "data.h"
  28. #include "graphics.h"
  29. #include "world.h"
  30. int context = 72; // 72 = "No" context link
  31. char *help = NULL;
  32. int contexts[20];
  33. int curr_context = 0;
  34. void set_context(int con)
  35. {
  36. contexts[curr_context++] = context;
  37. context = con;
  38. }
  39. void pop_context(void)
  40. {
  41. context = contexts[--curr_context];
  42. }
  43. void help_system(World *mzx_world)
  44. {
  45. FILE *fp;
  46. int t1, t2;
  47. cursor_mode_types old_cmode = get_cursor_mode();
  48. int where;
  49. int offs;
  50. int size;
  51. char file[13];
  52. char file2[13];
  53. char label[20];
  54. // Reserve memory (24k)
  55. help = (char *)malloc(24576);
  56. // Search context links
  57. fp = fopen(help_file, "rb");
  58. if(fp == NULL)
  59. {
  60. free(help);
  61. return;
  62. }
  63. t1 = fgetw(fp);
  64. fseek(fp, t1 * 21 + 4 + context * 12, SEEK_SET);
  65. // At proper context info
  66. where = fgetd(fp); // Where file to load is
  67. size = fgetd(fp); // Size of file to load
  68. offs = fgetd(fp); //Offset within file of link
  69. // Jump to file
  70. fseek(fp, where, SEEK_SET);
  71. // Read it in
  72. fread(help, 1, size, fp);
  73. // Display it
  74. cursor_off();
  75. labelled:
  76. help_display(mzx_world, help, offs, file, label);
  77. // File?
  78. if(file[0])
  79. {
  80. //Yep. Search for file.
  81. fseek(fp, 2, SEEK_SET);
  82. for(t2 = 0; t2 < t1; t2++)
  83. {
  84. fread(file2, 1, 13, fp);
  85. if(!strcmp(file, file2)) break;
  86. fseek(fp, 8, SEEK_CUR);
  87. }
  88. if(t2 < t1)
  89. {
  90. //Found file.
  91. where = fgetd(fp);
  92. size = fgetd(fp);
  93. fseek(fp, where, SEEK_SET);
  94. fread(help, 1, size, fp);
  95. // Search for label
  96. for(t2 = 0; t2 < size; t2++)
  97. {
  98. if(help[t2] != 255) continue;
  99. if(help[t2 + 1] != ':') continue;
  100. if(!strcmp(help + t2 + 3, label)) break; // Found label!
  101. }
  102. if(t2 < size)
  103. {
  104. //Found label. t2 is offset.
  105. offs = t2;
  106. goto labelled;
  107. }
  108. }
  109. }
  110. //All done!
  111. fclose(fp);
  112. free(help);
  113. set_cursor_mode(old_cmode);
  114. }