TXTPRNT.ASM 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. ;
  2. ; Command & Conquer Red Alert(tm)
  3. ; Copyright 2025 Electronic Arts Inc.
  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 3 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, see <http://www.gnu.org/licenses/>.
  17. ;
  18. ;***************************************************************************
  19. ;** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S **
  20. ;***************************************************************************
  21. ;* *
  22. ;* Project Name : Westwood 32 bit Library *
  23. ;* *
  24. ;* File Name : TXTPRNT.ASM *
  25. ;* *
  26. ;* Programmer : Phil W. Gorrow *
  27. ;* *
  28. ;* Start Date : January 17, 1995 *
  29. ;* *
  30. ;* Last Update : January 17, 1995 [PWG] *
  31. ;* *
  32. ;*-------------------------------------------------------------------------*
  33. ;* Functions: *
  34. ;* MCGA_Print -- Assembly MCGA text print routine *
  35. ;* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - *
  36. IDEAL
  37. P386
  38. MODEL USE32 FLAT
  39. ;INCLUDE "mcgaprim.inc"
  40. ;INCLUDE ".\gbuffer.inc"
  41. GLOBAL MCGA_Print : NEAR
  42. STRUC GraphicViewPort
  43. GVPOffset DD ? ; offset to virtual viewport
  44. GVPWidth DD ? ; width of virtual viewport
  45. GVPHeight DD ? ; height of virtual viewport
  46. GVPXAdd DD ? ; x mod to get to next line
  47. GVPXPos DD ? ; x pos relative to Graphic Buff
  48. GVPYPos DD ? ; y pos relative to Graphic Buff
  49. GVPBuffPtr DD ? ; ptr to associated Graphic Buff
  50. ENDS
  51. ;*=========================================================================*
  52. ;* Extern the font pointer which is defined by the font class *
  53. ;*=========================================================================*
  54. GLOBAL FontPtr:DWORD
  55. GLOBAL FontXSpacing:DWORD
  56. GLOBAL FontYSpacing:DWORD
  57. GLOBAL ColorXlat:BYTE
  58. ;*=========================================================================*
  59. ;* Define the necessary equates for structures and bounds checking *
  60. ;*=========================================================================*
  61. ; The header of the font file looks like this:
  62. ; UWORD FontLength; 0
  63. ; BYTE FontCompress; 2
  64. ; BYTE FontDataBlocks; 3
  65. ; UWORD InfoBlockOffset; 4
  66. ; UWORD OffsetBlockOffset; 6
  67. ; UWORD WidthBlockOffset; 8
  68. ; UWORD DataBlockOffset; 10
  69. ; UWORD HeightOffset; 12
  70. ; For this reason the following equates have these values:
  71. FONTINFOBLOCK EQU 4
  72. FONTOFFSETBLOCK EQU 6
  73. FONTWIDTHBLOCK EQU 8
  74. FONTDATABLOCK EQU 10
  75. FONTHEIGHTBLOCK EQU 12
  76. FONTINFOMAXHEIGHT EQU 4
  77. FONTINFOMAXWIDTH EQU 5
  78. LOCALS ??
  79. ;*=========================================================================*
  80. ;* Define the color xlate table in the data segment *
  81. ;*=========================================================================*
  82. DATASEG
  83. ColorXlat DB 000H,001H,002H,003H,004H,005H,006H,007H
  84. DB 008H,009H,00AH,00BH,00CH,00DH,00EH,00FH
  85. DB 001H,000H,000H,000H,000H,000H,000H,000H
  86. DB 000H,000H,000H,000H,000H,000H,000H,000H
  87. DB 002H,000H,000H,000H,000H,000H,000H,000H
  88. DB 000H,000H,000H,000H,000H,000H,000H,000H
  89. DB 003H,000H,000H,000H,000H,000H,000H,000H
  90. DB 000H,000H,000H,000H,000H,000H,000H,000H
  91. DB 004H,000H,000H,000H,000H,000H,000H,000H
  92. DB 000H,000H,000H,000H,000H,000H,000H,000H
  93. DB 005H,000H,000H,000H,000H,000H,000H,000H
  94. DB 000H,000H,000H,000H,000H,000H,000H,000H
  95. DB 006H,000H,000H,000H,000H,000H,000H,000H
  96. DB 000H,000H,000H,000H,000H,000H,000H,000H
  97. DB 007H,000H,000H,000H,000H,000H,000H,000H
  98. DB 000H,000H,000H,000H,000H,000H,000H,000H
  99. DB 008H,000H,000H,000H,000H,000H,000H,000H
  100. DB 000H,000H,000H,000H,000H,000H,000H,000H
  101. DB 009H,000H,000H,000H,000H,000H,000H,000H
  102. DB 000H,000H,000H,000H,000H,000H,000H,000H
  103. DB 00AH,000H,000H,000H,000H,000H,000H,000H
  104. DB 000H,000H,000H,000H,000H,000H,000H,000H
  105. DB 00BH,000H,000H,000H,000H,000H,000H,000H
  106. DB 000H,000H,000H,000H,000H,000H,000H,000H
  107. DB 00CH,000H,000H,000H,000H,000H,000H,000H
  108. DB 000H,000H,000H,000H,000H,000H,000H,000H
  109. DB 00DH,000H,000H,000H,000H,000H,000H,000H
  110. DB 000H,000H,000H,000H,000H,000H,000H,000H
  111. DB 00EH,000H,000H,000H,000H,000H,000H,000H
  112. DB 000H,000H,000H,000H,000H,000H,000H,000H
  113. DB 00FH
  114. CODESEG
  115. ;***************************************************************************
  116. ;* MCGA_PRINT -- Assembly MCGA text print routine *
  117. ;* *
  118. ;* *
  119. ;* *
  120. ;* INPUT: *
  121. ;* *
  122. ;* OUTPUT: *
  123. ;* *
  124. ;* PROTO: *
  125. ;* *
  126. ;* WARNINGS: *
  127. ;* *
  128. ;* HISTORY: *
  129. ;* 01/17/1995 PWG : Created. *
  130. ;*=========================================================================*
  131. PROC MCGA_Print C near
  132. USES ebx,ecx,edx,esi,edi
  133. ARG this:DWORD
  134. ARG string:DWORD
  135. ARG x_pixel:DWORD
  136. ARG y_pixel:DWORD
  137. ARG fcolor:DWORD
  138. ARG bcolor:DWORD
  139. LOCAL infoblock:DWORD ; pointer to info block
  140. LOCAL offsetblock:DWORD ; pointer to offset block (UWORD *)
  141. LOCAL widthblock:DWORD ; pointer to width block (BYTE *)
  142. LOCAL heightblock:DWORD ; pointer to height block (UWORD *)
  143. LOCAL curline:DWORD ; pointer to first column of current row.
  144. LOCAL bufferwidth:DWORD ; width of buffer (vpwidth + Xadd)
  145. LOCAL nextdraw:DWORD ; bufferwidth - width of cur character.
  146. LOCAL startdraw:DWORD ; where next character will start being drawn.
  147. LOCAL char:DWORD ; current character value.
  148. LOCAL maxheight:BYTE ; max height of characters in font.
  149. LOCAL bottomblank:BYTE ; amount of empty space below current character.
  150. LOCAL charheight:BYTE ; true height of current character.
  151. LOCAL vpwidth:DWORD
  152. LOCAL vpheight:DWORD
  153. LOCAL original_x:DWORD ; Starting X position.
  154. ;-------------------------------- Where to draw -----------------------------------------------
  155. ; Set up memory location to start drawing.
  156. mov ebx,[this] ; get a pointer to dest
  157. mov eax,[(GraphicViewPort ebx).GVPHeight] ; get height of viewport
  158. mov [vpheight],eax ; save off height of viewport
  159. mov eax,[(GraphicViewPort ebx).GVPWidth] ; get width of viewport
  160. mov [vpwidth],eax ; save it off for later
  161. add eax,[(GraphicViewPort ebx).GVPXAdd] ; add in xadd for bytes_per_line
  162. mov [bufferwidth],eax ; save it off for later use.
  163. mul [y_pixel] ; multiply rowsize * y_pixel start.
  164. mov edi,[(GraphicViewPort ebx).GVPOffset] ; get start of the viewport
  165. add edi,eax ; add y position to start of vp
  166. mov [curline],edi ; save 0,y address for line feed stuff.
  167. add edi,[x_pixel] ; add to get starting column in starting row.
  168. mov [startdraw],edi ; save it off.
  169. mov eax,[x_pixel]
  170. mov [original_x],eax
  171. ;-------------------------------- Create block pointers ----------------------------------------
  172. ; Get the pointer to the font.
  173. ; We could check for NULL but why waste the time.
  174. ; It is up to programmer to make sure it is set.
  175. mov esi,[FontPtr] ; Get the font pointer
  176. or esi,esi
  177. jz ??overflow
  178. ; Set up some pointers to the different memory blocks.
  179. ; esi (FontPtr) is added to each to get the true address of each block.
  180. ; Many registers are used for P5 optimizations.
  181. ; ebx is used for InfoBlock which is then used in the next section.
  182. movzx eax,[WORD PTR esi+FONTOFFSETBLOCK] ; get offset to offset block
  183. movzx ebx,[WORD PTR esi+FONTINFOBLOCK] ; get offset to info block (must be ebx for height test)
  184. movzx ecx,[WORD PTR esi+FONTWIDTHBLOCK] ; get offset to width block
  185. movzx edx,[WORD PTR esi+FONTHEIGHTBLOCK] ; get offset to height block
  186. add eax,esi ; add offset of FontPtr to offset block
  187. add ebx,esi ; add offset of FontPtr to info block
  188. add ecx,esi ; add offset of FontPtr to width block
  189. add edx,esi ; add offset of FontPtr to height block
  190. mov [offsetblock],eax ; save offset to offset block
  191. mov [infoblock],ebx ; save offset to info block
  192. mov [widthblock],ecx ; save offset to width block
  193. mov [heightblock],edx ; save offset to height block
  194. ;------------------------------------------ Test for fit ----------------------------------------------
  195. ; Test to make sure the height of the max character will fit on this line
  196. ; and and not fall out of the viewport.
  197. ; remember we set ebx to FONTINFOBLOCK above.
  198. movzx eax,[BYTE PTR ebx + FONTINFOMAXHEIGHT]; get the max height in font.
  199. mov [maxheight],al ; save it for later use.
  200. add eax,[y_pixel] ; add current y_value.
  201. cmp eax,[vpheight] ; are we over the edge?
  202. jg ??overflow ; if so, we're outa here.
  203. mov [y_pixel],eax ; save for next line feed. y value for next line.
  204. cld ; Make sure we are always forward copying.
  205. ;------------------------ Set palette foreground and background ----------------------------------
  206. mov eax,[fcolor] ; foreground color
  207. mov [ColorXlat+1],al
  208. mov [ColorXlat+16],al
  209. mov eax,[bcolor] ; background color
  210. mov [ColorXlat],al
  211. ;-------------------------------------------------------------------------------------------------
  212. ;----------------------------------------- Main loop ----------------------------------------------
  213. ; Now we go into the main loop of reading each character in the string and doing
  214. ; something with it.
  215. ??next_char:
  216. ; while (*string++)
  217. xor eax,eax ; zero out since we will just load al.
  218. mov esi,[string] ; get address on next character.
  219. lodsb ; load the character into al.
  220. test eax,0FFH ; test to see if character is a NULL
  221. jz ??done ; character is NULL, get outa here.
  222. mov edi,[startdraw] ; Load the starting address.
  223. mov [string],esi ; save index into string. (incremented by lodsb)
  224. cmp al,10 ; is the character a line feed?
  225. je ??line_feed ; if so, go to special case.
  226. cmp al,13 ; is the character a line feed?
  227. je ??line_feed ; if so, go to special case.
  228. mov [char],eax ; save the character off for later reference.
  229. mov ebx,eax ; save it in ebx for later use also.
  230. add eax,[widthblock] ; figure address of width of character.
  231. mov ecx,[x_pixel] ; get current x_pixel.
  232. movzx edx,[BYTE PTR eax] ; get the width of the character in dl.
  233. add ecx,edx ; add width of char to current x_pixel.
  234. mov eax,[FontXSpacing]
  235. add ecx,eax
  236. add [startdraw],edx ; save start draw for next character.
  237. add [startdraw],eax ; adjust for the font spacing value
  238. cmp ecx,[vpwidth] ; is the pixel greater then the vp width?
  239. jg ??force_line_feed ; if so, force a line feed.
  240. mov [x_pixel],ecx ; save value of start of next character.
  241. mov ecx,[bufferwidth] ; get amount to next y same x (one row down)
  242. sub ecx,edx ; take the current width off.
  243. mov [nextdraw],ecx ; save it to add to edi when done with a row.
  244. ; At this point we got the character. It is now time to find out specifics
  245. ; about drawing the darn thing.
  246. ; ebx = char so they can be used as an indexes.
  247. ; edx = width of character for loop later.
  248. ; get offset of data for character into esi.
  249. shl ebx,1 ; mult by 2 to later use as a WORD index.
  250. mov esi,[offsetblock] ; get pointer to begining of offset block.
  251. add esi,ebx ; index into offset block.
  252. movzx esi,[WORD PTR esi] ; get true offset into data block from FontPtr.
  253. add esi,[FontPtr] ; Now add FontPtr address to get true address.
  254. ; Get top and bottom blank sizes and the true height of the character.
  255. add ebx,[heightblock] ; point ebx to element in height array.
  256. mov al,[ebx+1] ; load the data height into dl.
  257. mov cl,[ebx] ; load the first data row into cl.
  258. mov bl,[maxheight] ; get the max height of characters.
  259. mov [charheight],al ; get number of rows with data.
  260. add al,cl ; add the two heights.
  261. sub bl,al ; subract topblank + char height from maxheight.
  262. mov [bottomblank],bl ; save off the number of blank rows on the bottom.
  263. ; leaving this section:
  264. ; dl is still the width of the character.
  265. ; cl is the height of the top blank area.
  266. mov ebx,OFFSET ColorXlat ; setup ebx for xlat commands.
  267. mov dh,dl ; save the width of the character to restore each loop.
  268. cmp cl,0 ; is there any blank rows on top?
  269. jz ??draw_char ; if not go and draw the real character.
  270. xor eax,eax ; get color 0 for background.
  271. xlat [ebx] ; get translated color into al
  272. test al,al ; is it transparent black
  273. jnz ??loop_top ; if not go and write the color
  274. ;----------------------------------------- skip Top blank area ----------------------------------------
  275. ; this case, the top is transparrent, but we need to increase our dest pointer to correct row.
  276. movzx eax,cl ; get number of rows into eax;
  277. mov ecx,edx ; save width since edx will be destroyed by mul.
  278. mul [bufferwidth] ; multiply that by the width of the buffer.
  279. mov edx,ecx ; restore the width
  280. add edi,eax ; update the pointer.
  281. jmp short ??draw_char ; now go draw the character.
  282. ;----------------------------------------- fill Top blank area ----------------------------------------
  283. ; edi was set a long time ago.
  284. ; al is the translated color
  285. ??loop_top:
  286. stosb ; store the value
  287. dec dh ; decrement our width.
  288. jnz ??loop_top ; if more width, continue on.
  289. add edi,[nextdraw] ; add amount for entire row.
  290. dec cl ; decrement or row count
  291. mov dh,dl ; restore width in dh for loop.
  292. jz ??draw_char ; we are done here, go draw the character.
  293. jmp short ??loop_top ; go back to top of loop.
  294. ;----------------------------------------- Draw character ----------------------------------------------
  295. ??draw_char:
  296. movzx ecx,[charheight] ; get the height of character to count down rows.
  297. test ecx,ecx ; is there any data? (blank would not have any)
  298. jz ??next_char ; if no data, go on to next character.
  299. ??while_data:
  300. lodsb ; get byte value from font data
  301. mov ah,al ; save hinibble
  302. and eax,0F00FH ; mask of low nibble in al hi nibble in ah.
  303. xlat [ebx] ; get new color
  304. test al,al ; is it a transparent?
  305. jz short ??skiplo ; skip over write
  306. mov [es:edi],al ; write it out
  307. ??skiplo:
  308. inc edi
  309. dec dh ; decrement our width.
  310. jz short ??nextrow ; check if done with width of char
  311. mov al,ah ; restore to get
  312. ; test the time difference between looking up in a large table when shr al,4 is not done as
  313. ; compared to using only a 16 byte table when using the shr al,4
  314. ;shr al,4 ; shift the hi nibble down to low nibble
  315. xlat [ebx] ; get new color
  316. test al,al ; is it a transparent?
  317. jz short ??skiphi ; skip over write
  318. mov [es:edi],al ; write it out
  319. ??skiphi:
  320. inc edi
  321. dec dh ; decrement our width.
  322. jnz short ??while_data ; check if done with width of char
  323. ??nextrow:
  324. add edi,[nextdraw] ; go to next line.
  325. dec ecx ; decrement the number of rows to go
  326. mov dh,dl ; restore our column count for row.
  327. jnz ??while_data ; more data for character.
  328. ; Now it is time to setup for clearing out the bottom of the character.
  329. movzx ecx,[bottomblank] ; get amount on bottom that is blank
  330. cmp ecx,0 ; if there is no blank bottom...
  331. jz ??next_char ; then skip to go to next character
  332. xor eax,eax ; get color 0 for background.
  333. xlat [ebx] ; get translated color into al
  334. test al,al ; is it transparent black
  335. jz ??next_char ; skip the top black section to let the background through
  336. mov dh,dl ; restore width in dh for loop.
  337. ;----------------------------------------- Blank below character -----------------------------------
  338. ??loop_bottom:
  339. stosb ; store the value
  340. dec dh ; decrement our width.
  341. jnz ??loop_bottom ; if more width, continue on.
  342. add edi,[nextdraw] ; add amount for entire row.
  343. mov dh,dl ; restore width in dh for loop.
  344. dec cl ; decrement or row count
  345. jz ??next_char ; we are done here, go to the next character.
  346. jmp short ??loop_bottom ; go back to top of loop.
  347. ;----------------------------------- end of next_char (main) loop ------------------------------------
  348. ;-------------------------------------------------------------------------------------------------
  349. ;----------------------------------- special case line feeds ----------------------------------------
  350. ??force_line_feed:
  351. ; decrement pointer *string so that it will be back at same character
  352. ; when it goes through the loop.
  353. mov eax,[string] ; get string pointer.
  354. dec eax ; decrement it to point to previos char
  355. mov [string],eax ; save it back off.
  356. xor eax,eax
  357. ; Now go into the line feed code.....
  358. ??line_feed:
  359. mov bl,al
  360. mov edx,[y_pixel] ; get the current y pixel value.
  361. movzx ecx,[maxheight] ; get max height for later use.
  362. add ecx,[FontYSpacing]
  363. add edx,ecx ; add max height to y_pixel
  364. cmp edx,[vpheight] ; are we over the edge?
  365. jg ??overflow ; if so, we are outa here.
  366. mov eax,[bufferwidth] ; get bytes to next line.
  367. mov edi,[curline] ; get start of current line.
  368. mul ecx ; mult max height * next line.
  369. add edi,eax ; add adjustment to current line.
  370. add [y_pixel],ecx ; increment to our next y position.
  371. ;;; DRD
  372. mov [curline],edi ; save it off for next line_feed.
  373. ; Move the cursor to either the left edge of the screen
  374. ; or the left margin of the print position depending
  375. ; on whether <CR> or <LF> was specified. <CR> = left margin
  376. ; <LF> = left edge of screen
  377. xor eax,eax
  378. cmp bl,10
  379. je ??lfeed
  380. mov eax,[original_x]
  381. ??lfeed:
  382. mov [x_pixel],eax ; zero out x_pixel
  383. add edi,eax
  384. ;;; DRD mov [curline],edi ; save it off for next line_feed.
  385. mov [startdraw],edi ; save it off so we know where to draw next char.w
  386. jmp ??next_char
  387. ??overflow:
  388. mov [startdraw],0 ; Indicate that there is no valid next pos.
  389. ??done:
  390. mov eax,[startdraw] ; return this so calling routine
  391. ret ; can figure out where to draw next.
  392. ENDP MCGA_Print
  393. ;***************************************************************************
  394. ;* GET_FONT_PALETTE_PTR -- Returns a pointer to the 256 byte font palette *
  395. ;* *
  396. ;* INPUT: none *
  397. ;* *
  398. ;* OUTPUT: none *
  399. ;* *
  400. ;* PROTO: void *Get_Font_Palette_Ptr(void); *
  401. ;* *
  402. ;* HISTORY: *
  403. ;* 08/18/1995 PWG : Created. *
  404. ;*=========================================================================*
  405. GLOBAL Get_Font_Palette_Ptr:NEAR
  406. PROC Get_Font_Palette_Ptr C near
  407. mov eax, OFFSET ColorXlat
  408. ret
  409. ENDP Get_Font_Palette_Ptr
  410. END