TXTPRNT.ASM 20 KB

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