home *** CD-ROM | disk | FTP | other *** search
/ Informática Multimedia: Special Games / INFESPGAMES.mdf / os2 / spl / src / spl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-26  |  7.8 KB  |  322 lines

  1. /* spl.cpp:  Spellbook control program
  2.  
  3.     Copyright (C) 1993, 1994 John-Marc Chandonia
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.  Changes-
  20.  
  21.  1.73 - 7/26/94 - fixed nasty memory allocation bugs
  22.  1.72 - 7/24/94 - fixed saving bugs
  23.  1.71 - 7/13/94 - can load several books simultaneously
  24.  1.7 - 5/15/94 - user can sort books by several methods
  25.  1.6 - 5/14/94 - user can select which details fields to show
  26.  1.5 - 4/16/94 - added working on-line help
  27.  1.41 - 3/22/94 - added quick saving/loading of master list
  28.  1.4 - 3/15/94 - change container view via menu/popup
  29.  1.3 - 2/28/94 - multithreaded spell filtering, saves window positions
  30.    in EA's, saves default positions for new, master windows.
  31.  1.2 - 2/24/94 - multithreaded book windows
  32.  1.1 - 2/17/94 - modularized; added .ini file for master window pos.
  33.  1.02 - 10/12/93 - don't load full spell description at startup
  34.  1.01 - 10/1/93 - added some error checking
  35.  1.00 - 8/25/93 - original version
  36. */
  37.  
  38. #include <stdlib.h>
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include "spl.hpp"
  42. #include "general.hpp"
  43. #include "spldlg.h"
  44. #include "bookwin.hpp"
  45. #include "splwin.hpp"
  46. #include "spldlgs.hpp"
  47. #include "splhelp.hpp"
  48. #include <stdarg.h>
  49.  
  50. // stuff of interest in several modules
  51. HAB hab;  // anchor block 
  52. HINI hini; // handle to ini
  53. BYTE xfer_buffer[10240];
  54. HPOINTER dragicon; // icon for dragging spells
  55. int windows=1000;  // used for creating new client windows
  56. spellbook *masterlist=NULL;
  57. HEV sem_object_started;
  58. HEV sem_master_loading;
  59. HWND hwndhelp;
  60. HWND hwndspellhelp;
  61.  
  62. // stuff that's defined in other modules
  63. extern bookwindow *first;
  64. extern spellwindow *firstspell;
  65.  
  66. // some file names
  67. char icon_file_name[256];
  68. char changed_file_name[256];
  69. char master_name[256];
  70. char quick_master_name[256];
  71.  
  72. // table of icons for various schools and spheres.
  73. int icons;
  74. HPOINTER *icontable;
  75. char **icontext;
  76.  
  77. // set up the icon table and the drag icon.
  78. void setupicons() {
  79.     FILE *infile;
  80.     char buffer[256];
  81.     int i,j,k;
  82.  
  83.     dragicon=WinLoadFileIcon((PSZ)"spl.ico",FALSE);
  84.  
  85.     // get spell icons out of file.
  86.     icons=0;
  87.     if ((infile=fopen(icon_file_name,"r"))==NULL) return;
  88.  
  89.     // count lines
  90.     while (!feof(infile)) {
  91.     fgets(buffer,256,infile);
  92.     if ((buffer[0]!=';') && (strlen(buffer)>2)) {
  93.         icons++;
  94.     }
  95.     }
  96.     rewind(infile);
  97.  
  98.     icontable=new HPOINTER[icons];
  99.     icontext=new PCHAR[icons];
  100.  
  101.     for (i=0; i<icons; i++) {
  102.     do {
  103.         fgets_no_cr(buffer,256,infile);
  104.     } while ((buffer[0]==';') || (strlen(buffer)<=1));
  105.  
  106.     // find first space in line
  107.     if (strchr(buffer,' ')==NULL) {
  108.         icons=0;
  109.         return;
  110.     }
  111.     // with no error handling whatsoever!
  112.     j=strlen(buffer)-strlen(strchr(buffer,' '));
  113.     icontext[i]=new char[j+1];
  114.     icontext[i][j]=(char)0;
  115.     strncpy(icontext[i],buffer,j);
  116.     icontable[i]=WinLoadFileIcon((PSZ)(buffer+j+1),FALSE);
  117.     }
  118.  
  119.     fclose(infile);
  120. }
  121.  
  122. // return the appropriate icon for a spell, from the icon table
  123. HPOINTER lookup_icon(spell *s) {
  124.     magespell *ms;
  125.     priestspell *ps;
  126.     int i;
  127.  
  128.     // if a priest spell, try to look it up by sphere
  129.     if (s->type=='P') {
  130.     ps=(priestspell *)s;
  131.     if (ps->sphere) {
  132.         for (i=0; i<icons; i++) 
  133.         if (strstr(ps->sphere,icontext[i])!=0)
  134.             return(icontable[i]);
  135.     }
  136.     }
  137.  
  138.     // try looking it up by school.
  139.     if ((s->type=='M') || (s->type=='P')) {
  140.     ms=(magespell *)s;
  141.     if (ms->school) {
  142.         for (i=0; i<icons; i++) 
  143.         if (strstr(ms->school,icontext[i])!=0)
  144.             return(icontable[i]);
  145.     }
  146.     }
  147.  
  148.     // return generic icon if nothing else works.
  149.     return(dragicon);
  150. }
  151.  
  152. // read in the ini file
  153. void load_ini() {
  154.     char *ini_name=INIFILE;
  155.  
  156.     hini = PrfOpenProfile(hab,ini_name);
  157.  
  158.     // find name of changed spellbook file
  159.     PrfQueryProfileString(hini,
  160.               "spellbook",
  161.               "changefile",
  162.               CHANGEFILE,
  163.               changed_file_name,
  164.               256);
  165.  
  166.     // find name of icon file
  167.     PrfQueryProfileString(hini,
  168.               "spellbook",
  169.               "iconfile",
  170.               ICONFILE,
  171.               icon_file_name,
  172.               256);
  173.     
  174.     // find name of master spell list
  175.     PrfQueryProfileString(hini,
  176.               "spellbook",
  177.               "masterlist",
  178.               ALLSPELLS,
  179.               master_name,
  180.               256);
  181.  
  182.     // find name of quick master spell list
  183.     PrfQueryProfileString(hini,
  184.               "spellbook",
  185.               "quickmaster",
  186.               QUICKALL,
  187.               quick_master_name,
  188.               256);
  189. }
  190.  
  191. // quick enabling of menu items
  192. VOID enable_menu_item( HWND hwndMenu, SHORT sIditem, BOOL bEnable) {
  193.   SHORT sFlag;
  194.  
  195.   if(bEnable)
  196.     sFlag = 0;
  197.   else
  198.     sFlag = MIA_DISABLED;
  199.  
  200.   WinSendMsg(hwndMenu, MM_SETITEMATTR, MPFROM2SHORT(sIditem, TRUE),
  201.                MPFROM2SHORT(MIA_DISABLED, sFlag));
  202. }
  203.  
  204. // checks or unchecks an item on a given menu
  205. void check_menu_item(HWND hwndframe, 
  206.              short submenu_id, 
  207.              short item_id, 
  208.              BOOL checked) {
  209.     MENUITEM mi;
  210.     HWND hwndmenu;
  211.  
  212.     mi.hwndSubMenu=NULL;
  213.  
  214.     hwndmenu=WinWindowFromID(hwndframe,
  215.                  FID_MENU);
  216.  
  217.     WinSendMsg(hwndmenu,
  218.            MM_QUERYITEM,
  219.            MPFROM2SHORT(submenu_id,TRUE),
  220.            MPFROMP(&mi));
  221.  
  222.     hwndmenu=mi.hwndSubMenu;
  223.  
  224.     WinCheckMenuItem(hwndmenu,item_id,checked);
  225. }
  226.  
  227. // print a message to the window's title
  228. void titleprintf(HWND hwndframe, char *fmt, ...) {
  229.     char buffer[256];
  230.  
  231.     va_list ap;
  232.     
  233.     va_start(ap,fmt);
  234.     vsprintf(buffer,fmt,ap);
  235.     va_end(ap);
  236.  
  237.     WinSetWindowText(hwndframe,(PSZ)buffer);
  238. }
  239.  
  240. // main program
  241. int main(int argc, char **argv) {
  242.     int i;
  243.     HMQ hand_mq;  // message queue 
  244.     QMSG q_mess;  // message queue 
  245.     HWND logo;
  246.  
  247.     // create semaphores
  248.     DosCreateEventSem(NULL,
  249.               &sem_object_started,
  250.               0,
  251.               TRUE);
  252.  
  253.     DosCreateMutexSem(NULL,
  254.               &sem_master_loading,
  255.               0,
  256.               FALSE);
  257.     
  258.     hab=WinInitialize(0);  // get anchor block 
  259.     hand_mq = WinCreateMsgQueue(hab, 0); // start queue 
  260.  
  261.     // register the classes 
  262.     if (!WinRegisterClass(
  263.               hab,
  264.               (PSZ) spbclass,
  265.               (PFNWP) book_window_func,
  266.               CS_SIZEREDRAW,
  267.               sizeof(bookwindow *)))
  268.     exit(1);
  269.     if (!WinRegisterClass(
  270.               hab,
  271.               (PSZ) splclass,
  272.               (PFNWP) spell_window_func,
  273.               CS_SIZEREDRAW,
  274.               sizeof(spellwindow *)))
  275.     exit(1);
  276.  
  277.  
  278.     // show copyright notice
  279.     logo=WinLoadDlg(HWND_DESKTOP,
  280.             HWND_DESKTOP,
  281.             about_dlg_func,
  282.             0L,
  283.             ABOUT_DLG,
  284.             NULL);
  285.  
  286.     // load in init file and icons
  287.     load_ini();
  288.     setupicons();
  289.  
  290.     // load help
  291.     init_help(hab);
  292.  
  293.     // create first spell book with master list
  294.     if (argc<2)
  295.     new bookwindow(NULL, true);
  296.     else
  297.     for (i=1; i<argc; i++) 
  298.         new bookwindow(argv[i],false);
  299.     
  300.     // dismiss dialog automatically
  301.     WinSendMsg(logo,WM_COMMAND,0,0);
  302.  
  303.     // message loop 
  304.     while(WinGetMsg(hab, &q_mess, 0L,0,0))
  305.     WinDispatchMsg(hab, &q_mess);
  306.  
  307.     // shut down all remaining windows and quit 
  308.     while (first!=NULL) delete first;
  309.     while (firstspell!=NULL) delete firstspell;
  310.     PrfCloseProfile(hini);
  311.     if (hwndhelp)
  312.     WinDestroyHelpInstance(hwndhelp);
  313.     if (hwndspellhelp)
  314.     WinDestroyHelpInstance(hwndspellhelp);
  315.     WinDestroyMsgQueue(hand_mq);
  316.     WinTerminate(hab);
  317.     return(0);
  318. }
  319.  
  320.  
  321.  
  322.