2TXTPRNT.ASM 20 KB

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