home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / Epoc / Palmtime / files / FrotzCE2_src.ZIP / FrotzCE / FrotzCEOS.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-23  |  9.9 KB  |  434 lines

  1. /*
  2.  * FrotzCEOS.cpp
  3.  *
  4.  * Windows CE Frotz OS interface
  5.  *
  6.  */
  7.  
  8. #include "StdAfx.h"
  9.  
  10. #include "FrotzCE.h"
  11. #include "MainFrm.h"
  12. #include "FrotzCEDoc.h"
  13. #include "FrotzCEView.h"
  14. #include "StoryInfo.h"
  15.  
  16. static BOOL bWasUsingColours;
  17. static int nOldFontHeight;
  18. static int nOldFontWidth;
  19.  
  20. extern "C" 
  21. {
  22. #include "Frotz/frotz.h"
  23.  
  24. extern char euro_substitute[];
  25.  
  26. void     os_draw_picture (int n, int x, int y) {}
  27. void     os_finish_with_sample (void) {}
  28. int      os_font_data (int n, int *height, int *width) { return 0; }
  29. int      os_peek_colour (void) { return 0; }
  30. int      os_picture_data (int n, int *height, int *width) { return 0; }
  31. void    os_process_arguments (int argc, char *argv[]) {}
  32. void     os_prepare_sample (int n) {}
  33. void     os_set_font (int font) {}
  34. void     os_start_sample (int n, int r, int vol) {}
  35. void     os_stop_sample (void) {}
  36. void     os_wait_sample (void) {}
  37. }
  38.  
  39. extern StoryInfo acInfocomReleases[NO_INFOCOM_STORIES];
  40.  
  41. extern "C" void os_beep (int n) 
  42. {
  43.     // Produce a beep
  44.     ::MessageBeep( 0xFFFFFFFF );
  45. }
  46.  
  47. extern "C" int os_char_width (int c)
  48. {
  49.     // Gap between sentences?
  50.     if (c == 9)    return 2;
  51.     // Paragraph indentation?
  52.     else if (c == 11) return 3;
  53.     // European char?
  54.     else if (FROTZCEAPP->m_bUseEuroChars && c >= EURO_MIN && c <= EURO_MAX)
  55.     {
  56.         return (euro_substitute[2 * (c - EURO_MIN) + 1] == ' ') ? 1 : 2;
  57.     }
  58.     else return 1;
  59. }
  60.  
  61. extern "C" void os_cursor_off (void) 
  62. {
  63.     // Turn off cursor
  64.     FROTZCEVIEW->SetCursorState( FALSE );
  65. }
  66.  
  67. extern "C" void os_cursor_on (void) 
  68. {
  69.     // Turn on cursor
  70.     FROTZCEVIEW->SetCursorState( TRUE );
  71. }
  72.  
  73. extern "C" void os_display_char (int c)
  74. {
  75.     // European char?
  76.     if (FROTZCEAPP->m_bUseEuroChars && c >= EURO_MIN && c <= EURO_MAX)
  77.     {
  78.     int c1 = euro_substitute[2 * (c - EURO_MIN)];
  79.     int c2 = euro_substitute[2 * (c - EURO_MIN) + 1];
  80.  
  81.         os_display_char( c1 );
  82.  
  83.         if (c2 != ' ') c = c2;
  84.         else return;
  85.  
  86.     }
  87.  
  88.     // Display char and update screen
  89.     FROTZCEVIEW->DisplayChar( (char) c );
  90. }
  91.  
  92. extern "C" void os_display_string (const char *s)
  93. {
  94.     // Display string
  95.     FROTZCEVIEW->DisplayString( s );
  96. }
  97.  
  98. extern "C" void os_erase_area (int top, int left, int bottom, int right) 
  99. {
  100.     // Erase specified screen area
  101.     FROTZCEVIEW->EraseScreenArea( --top, --left, --bottom, --right );
  102. }
  103.  
  104. extern "C" void os_fatal (const char *s)
  105. {
  106. CString strError = s;
  107.  
  108.     // Display error
  109.     AfxMessageBox( strError );
  110.  
  111.     // Z machine running?
  112.     if (FROTZCEAPP->m_bZmachineRunning)
  113.     {
  114.         // Reset memory
  115.         reset_memory ();
  116.  
  117.         // Flag that Z machine is no longer running
  118.         FROTZCEAPP->m_bZmachineRunning = FALSE;
  119.  
  120.         // Reset display
  121.         os_reset_screen ();
  122.     }
  123.  
  124.     // Stop execution of Z machine thread 
  125.     AfxEndThread(1);
  126. }
  127.  
  128. extern "C" void os_init_screen (void)
  129. {
  130.     // Save current settings
  131.     bWasUsingColours = FROTZCEAPP->m_bUseColours;
  132.     nOldFontHeight = FROTZCEAPP->m_nFontHeight;
  133.     nOldFontWidth = FROTZCEAPP->m_nFontWidth;
  134.  
  135.     // Try to recognise story
  136.     for (int nStoryNo = 0; nStoryNo < NO_INFOCOM_STORIES; nStoryNo++)
  137.     {
  138.         // Found story information?
  139.         if (acInfocomReleases[nStoryNo].nRelease == h_release
  140.             && strcmp( acInfocomReleases[nStoryNo].achSerialNo, (char *) h_serial ) == 0)
  141.         {
  142.         CString strStoryName = acInfocomReleases[nStoryNo].achStoryName;
  143.  
  144.             // Check if story is supported
  145.             if ((acInfocomReleases[nStoryNo].nSettings & STORY_NOSUPPORT) != 0)
  146.             {
  147.             CString strDisplay = strStoryName + TEXT( " is not supported by FrotzCE" );
  148.  
  149.                 // Display message
  150.                 AfxMessageBox( strDisplay );
  151.                 return;
  152.             }
  153.  
  154.             // Check if story is supported
  155.             if ((acInfocomReleases[nStoryNo].nSettings & STORY_LIMITEDSUPPORT) != 0)
  156.             {
  157.             CString strDisplay = strStoryName 
  158.                 + TEXT( " is not properly supported by FrotzCE" );
  159.  
  160.                 // Display message
  161.                 AfxMessageBox( strDisplay );
  162.             }
  163.  
  164.             // Story requires small font?
  165. /*FredB            if ((acInfocomReleases[nStoryNo].nSettings & STORY_SMALLFONT) != 0
  166.                 && FROTZCEAPP->m_nFontHeight == 0)
  167.             {
  168.                 FROTZCEAPP->m_nFontHeight = TERMINAL_FONT_HEIGHT;
  169.                 FROTZCEAPP->m_nFontWidth = TERMINAL_FONT_WIDTH;
  170.             }*/
  171.  
  172.             // Story requires small font?
  173. /*FredB            if ((acInfocomReleases[nStoryNo].nSettings & STORY_NOCOLOURS) != 0)
  174.             {
  175.                 FROTZCEAPP->m_bUseColours = FALSE;
  176.             }*/
  177.  
  178.             // Story uses graphics chars?
  179.             if ((acInfocomReleases[nStoryNo].nSettings & STORY_GFXCHARS) != 0)
  180.             {
  181.                 FROTZCEAPP->m_bUseEuroChars = FALSE;
  182.             }
  183.  
  184.             // Set window title
  185.             MAINFRAME->SetWindowText( strStoryName );
  186.             break;
  187.         }
  188.     }
  189.  
  190.     // Update menus
  191.     MAINFRAME->SendMessage( WM_UPDATE_MENU );
  192.       
  193.     // Initialise screen
  194.     FROTZCEVIEW->InitialiseScreen();
  195.  
  196.     // Set screen dimensions
  197.     h_screen_width = h_screen_cols = FROTZCEVIEW->GetWidth();
  198.     h_screen_height = h_screen_rows = FROTZCEVIEW->GetHeight();
  199.  
  200.     // Set config flags
  201.     if (h_version == V3 && FROTZCEAPP->m_bTandy)
  202.         h_config |= CONFIG_TANDY;
  203.  
  204.     if (h_version == V3)
  205.         h_config |= CONFIG_SPLITSCREEN;
  206.           
  207.     if (h_version >= V4)
  208.     {
  209.         h_config |= CONFIG_FIXED | CONFIG_TIMEDINPUT;
  210.  
  211.         if (FROTZCEAPP->m_bUseStyles)
  212.             h_config |= CONFIG_BOLDFACE | CONFIG_EMPHASIS;
  213.     }
  214.     
  215.     if (h_version >= V5 && FROTZCEAPP->m_bUseColours)
  216.         h_config |= CONFIG_COLOUR;
  217.           
  218.     /* Handle various game flags. These flags are set if the game wants
  219.     to use certain features. The flags must be cleared if the feature
  220.     is not available. */
  221.           
  222.     if (h_flags & GRAPHICS_FLAG)
  223.         h_flags &= ~GRAPHICS_FLAG;
  224.           
  225.     if (h_version == V3 && (h_flags & OLD_SOUND_FLAG))
  226.         h_flags &= ~OLD_SOUND_FLAG;
  227.           
  228.     if (h_flags & SOUND_FLAG)
  229.         h_flags &= ~SOUND_FLAG;
  230.           
  231.     if (h_flags & MOUSE_FLAG)
  232.         h_flags &= ~MOUSE_FLAG;
  233.  
  234.     h_flags &= ~MENU_FLAG;
  235.           
  236.     /* Set the interpreter number (a constant telling the game which
  237.     operating system it runs on) and the interpreter version. The
  238.     interpreter number has effect on all V6 games and Beyond Zork. */
  239.           
  240.     h_interpreter_number = INTERP_MSDOS;
  241.     h_interpreter_version = 'E';
  242.  
  243.     // Show cursor
  244.     os_cursor_on();
  245.  
  246.     // Flag that initialised
  247.     FROTZCEAPP->m_bInitialised = TRUE;
  248. }
  249.  
  250. extern "C" void os_more_prompt (void) 
  251. {
  252. int nRow, nCol;
  253.  
  254.     // Save cursor position
  255.     FROTZCEVIEW->GetCursorPos( &nRow, &nCol );
  256.  
  257.     // Hide cursor
  258.     os_cursor_off();
  259.  
  260.     // Display prompt
  261.     os_set_text_style( REVERSE_STYLE );
  262.     os_display_string( "[MORE]" );
  263.     os_set_text_style( NORMAL_STYLE );
  264.     os_read_key( 0 );
  265.  
  266.     // Show cursor
  267.     os_cursor_on();
  268.  
  269.     // Remove prompt
  270.     FROTZCEVIEW->SetCursorPos( nRow, nCol );
  271.     os_display_string( "      " );
  272.     FROTZCEVIEW->SetCursorPos( nRow, nCol );
  273. }
  274.  
  275. extern "C" int os_random_seed (void) 
  276. CTime cTime = CTime::GetCurrentTime();    
  277.  
  278.     // Return current time as random seed
  279.     return (int) cTime.GetTime();
  280. }
  281.  
  282. extern "C" int os_read_file_name (char *name, const char *default_name, int type)
  283. {
  284. CString strFileName = default_name;
  285. CFileDialog dlgFile( TRUE, NULL, (LPCTSTR) strFileName );
  286. CString strDefDir = FROTZCEAPP->m_strDefaultDir;
  287. int nReturn = 1;
  288.  
  289.     // Hide cursor
  290.     os_cursor_off();
  291.  
  292.     // Set initial directory
  293.     dlgFile.m_ofn.lpstrInitialDir = strDefDir.GetBuffer( strDefDir.GetLength() + 1 );
  294.  
  295.     // Process file dialog
  296.     if (dlgFile.DoModal() == IDOK)
  297.     {
  298.     LPTSTR pszDefDir;
  299.     int i;
  300.  
  301.         // Get file name from dialog
  302.         strFileName = dlgFile.GetPathName();
  303.  
  304.         // Copy file name
  305.         strDefDir.ReleaseBuffer();
  306.         strDefDir = strFileName;
  307.         // Go backwards in file name until first '\' is found
  308.         i = strDefDir.GetLength();
  309.         for (pszDefDir = strDefDir.GetBuffer( i + 1 ); i >= 0; i--)
  310.         {
  311.             // Not a directory specifier?
  312.             if (pszDefDir[i] != _T('\\') && pszDefDir[i] != _T('/'))
  313.             {
  314.                 // Erase it
  315.                 pszDefDir[i] = 0;
  316.             }
  317.             else 
  318.             {
  319.                 if (i != 0) pszDefDir[i] = 0;
  320.                 break;
  321.             }
  322.         }
  323.         // Resize string
  324.         strDefDir.ReleaseBuffer();
  325.  
  326.         // Save it
  327.         FROTZCEAPP->m_strDefaultDir = strDefDir;
  328.  
  329.         // Convert wide char file name to single byte chars
  330.         for (i = 0; i < strFileName.GetLength(); i++)
  331.         {
  332.             name[i] = (char) strFileName[i];
  333.         }
  334.         name[strFileName.GetLength()] = '\0';
  335.     }
  336.     else 
  337.     {
  338.         strDefDir.ReleaseBuffer();
  339.         nReturn = 0;
  340.     }
  341.  
  342.     // Make sure we've still got the focus
  343.     FROTZCEVIEW->SendMessage( WM_TAKE_FOCUS );
  344.  
  345.     // Show cursor
  346.     os_cursor_on();
  347.  
  348.     return nReturn;
  349. }
  350.  
  351. extern "C" int os_read_key (int timeout)
  352. {
  353.     // Get a keypress
  354.     return FROTZCEVIEW->GetKey( timeout );
  355. }
  356.  
  357. extern "C" int os_read_line (int max, char *buf, int timeout, int width, int continued)
  358. {
  359.     // Get a line from the keyboard
  360.     return FROTZCEVIEW->GetLine( buf, max, timeout );
  361. }
  362.  
  363. extern "C" void os_reset_screen (void) 
  364. {
  365.     // Clear screen
  366.     FROTZCEVIEW->ClearScreen( TRUE );
  367.  
  368.     // Turn off cursor
  369.     os_cursor_off();
  370.  
  371.     // Reset screen
  372.     FROTZCEVIEW->ResetScreen();
  373.  
  374.     // Close document
  375.     FROTZCEVIEW->GetDocument()->OnNewDocument();
  376.  
  377.     // Update menus
  378.     MAINFRAME->SendMessage( WM_UPDATE_MENU );
  379.  
  380.     // Set window title
  381.     MAINFRAME->SetWindowText( TEXT( "FrotzCE" ) );
  382.  
  383.     // Flag that not initialised
  384.     FROTZCEAPP->m_bInitialised = FALSE;
  385.  
  386.     // Restore previous settings
  387.     FROTZCEAPP->m_bUseEuroChars = TRUE;
  388.     FROTZCEAPP->m_bUseColours = bWasUsingColours;
  389.     FROTZCEAPP->m_nFontHeight = nOldFontHeight;
  390.     FROTZCEAPP->m_nFontWidth = nOldFontWidth;
  391. }
  392.  
  393. extern "C" void os_scroll_area (int top, int left, int bottom, int right, int units)
  394. {
  395.     // Scroll an area of the screen
  396.     FROTZCEVIEW->ScrollScreenArea( --top, --left, --bottom, --right, units );
  397. }
  398.  
  399. extern "C" void os_set_colour (int fg, int bg) 
  400. {
  401.     FROTZCEVIEW->SetTextColour( fg, bg );
  402. }
  403.  
  404. extern "C" void os_set_cursor (int y, int x) 
  405. {
  406.     // Set the cursor position
  407.     FROTZCEVIEW->SetCursorPos( --y, --x );
  408. }
  409.  
  410. extern "C" void    os_set_text_style (int style) 
  411. {
  412.     // Set the text style
  413.     FROTZCEVIEW->SetTextStyle( style );
  414. }
  415.  
  416. extern "C" int os_string_width (const char *s)
  417. {
  418. int width;
  419.  
  420.     // Process string
  421.     for (width = 0; *s; s++) 
  422.     {
  423.         if (*s != NEW_FONT && *s != NEW_STYLE)
  424.         {
  425.             // Get width of character
  426.             width += os_char_width ((unsigned char) *s);
  427.         }
  428.         else s++;
  429.     }
  430.  
  431.     return width;
  432. }
  433.