home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11607 < prev    next >
Encoding:
Internet Message Format  |  1992-07-25  |  4.0 KB

  1. Path: sparky!uunet!cs.utexas.edu!torn!cunews!revcan!micor!uuisis!synapse!rod.willmot
  2. From: rod.willmot@synapse.isis.org (Rod Willmot) 
  3. Newsgroups: comp.lang.c
  4. Subject: Curious bug...
  5. Message-ID: <224.1366.uupcb@synapse.isis.org>
  6. Date: 24 Jul 92 19:16:00 GMT
  7. Distribution: world
  8. Organization: SYNAPSE BBS - GATINEAU, QUEBEC - 819-561-4321
  9. Reply-To: rod.willmot@synapse.isis.org (Rod Willmot) 
  10. Lines: 69
  11.  
  12. TO: pegasus@csd4.csd.uwm.edu 
  13.  
  14. To David R Hucke on the subject of his Curious bug:
  15.  
  16. DR>What happens is this:  When I run the program in the QC Environment it
  17. DR>works fine.  The characters are correct, and the colors are not correct.
  18. DR>When I run the program from DOS (outside of the QC Environment) the
  19. DR>characters are correct but the colors are not correct.
  20.  
  21. First, some tips:  (1) when doing this kind of thing, make sure 
  22. your data is always unsigned.  (2) Remember to open files in binary 
  23. mode, not just in "r".  (3) Since you start with shorts you should 
  24. continue with shorts, instead of switching to ints (just to avoid 
  25. flames from the boring).  (3) Throw out the IDE... (Seriously, your 
  26. productivity will soar, especially with a programmer's editor like 
  27. Multi-Edit.)  (4) Use a file viewer that can work in hex mode, like 
  28. LIST.COM, to see what's *really* in the data file you're trying to 
  29. read in.  This can tell you whether the x byte really comes first 
  30. in each byte pair, as you think it does. 
  31.  
  32. Actually that last point makes me wonder about your lines,
  33. DR>  fscanf(file,"%c%c",&x,&y);
  34. DR>  menu[j][i] = (short)((y<<8)+x);
  35. The structure of video memory is such that the colour byte comes 
  36. first, followed by the high byte.   The question is whether TheDraw 
  37. writes it out that way.  If TheDraw treats the screen as an array 
  38. of bytes, it probably does; if it treats it as an array of ints, it 
  39. may not.  For example, suppose you construct an int buffer with the
  40. value 0xF00F in each int; this is 0xF0, 0x0F in alternating byte 
  41. pairs.  If you write this to disk as a byte buffer, the order of 
  42. bytes is preserved, but if you write it to disk as an int buffer, 
  43. the order of ints is REVERSED.  (This is not true of all systems,
  44. but it's something to watch out for.)
  45.  
  46. Personally I find it highly inefficient to fscanf a file like this.
  47. You'll have better performance (and tighter control over the 
  48. results) if you read the whole file into a dynamically-allocated
  49. buffer, in one shot; then throw it onscreen and free the buffer.
  50. Examine your data file and see if the byte pairs are 
  51. attribute/character, or character/attribute.  If the former, which
  52. models video memory, you may find it easier to treat the screen as 
  53. an array of bytes (4000 bytes instead of 2000 shorts).  Just 
  54. declare the buffer as a byte array, then copy its contents to video 
  55. memory as sequential pairs of bytes.  However, if the data is in
  56. character/attribute order, this means it's been written to disk as 
  57. shorts and so you can read it in as shorts as well -- unreversing 
  58. it so to speak.  In this case, you treat the screen as an array of 
  59. shorts and you declare your buffer as an array of shorts, then read 
  60. in your file and copy it over as shorts.  (I'll say it again:  if 
  61. the byte order is reversed in the data file, you can unreverse it 
  62. simply by reading it in as shorts, i.e. into a short buffer.  If 
  63. it's not reversed, read it into a byte buffer.)
  64.  
  65. Have I confused you even more?  I'll be happy to help, since I use 
  66. QC 2.5 too.  One final point I'd like to make:  the best approach 
  67. to making menus is NOT to store them in a data file, since that way 
  68. you're frozen to one set of colours.  This gets important if you 
  69. want other people to use your programs and maybe configure their 
  70. own colours.  It doesn't take long to write your own little 
  71. functions to store a window area, clear it, draw a box there, and so 
  72. on; then you can write a menu system that performs those chores on 
  73. the data in a menu structure.  Performance ends up being better 
  74. than if you were reading it from disk, and you have more 
  75. flexibility.
  76.  
  77. rod.willmot@synapse.isis.org    (Rod Willmot)
  78. ---
  79.  . DeLuxe. 1.20 #2423 . 
  80.            
  81.