Ooops
March 01, 2021, 10:53:21 PM

Author Topic: [bmx] Apple IIe Text Console by zoqfotpik [ 1+ years ago ]  (Read 464 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Apple IIe Text Console by zoqfotpik [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Apple IIe Text Console
Author : zoqfotpik
Posted : 1+ years ago

Description : This is a coordinate-addressable text console for roguelikes and whatever other console display needs you might have.  This is fast enough for just about any purpose but could be vastly sped up by getting rid of the cls statement, only plotting characters that changed in a given tick, and clearing each one by writing a black rectangle over it first.  Further huge speedup could be had by using a pixmap draw like Derron's drawimagetoimage or something similar, which will cut down the number of screen draws to one (but not necessary if you are plotting only changes.)

Code :
Code: BlitzMax
  1. ' Quick and Dirty Text Display Console.  For roguelikes or whatever else you might
  2. ' want.
  3.  
  4. Graphics 1280,960
  5. Global text:String[10000]       ' This is the text string.  It's not a 2D array to make it
  6.                                                         ' easier to display actual readable strings but is addressable
  7.                                                         ' by x and y with function plotchar.
  8. Global textimage:TImage = New TImage
  9.  
  10. ' The Apple IIe font as an image.
  11. ' Grab it here:  
  12. ' https://www.dropbox.com/s/s4flyf1345qjbb8/applefont-wide.png?dl=0
  13.  
  14. textimage = LoadImage("applefont-wide.png")
  15.  
  16. Cls
  17.  
  18. ' This is the order of characters as they appear in the font.  When drawing
  19. ' we use a text search to find where in the string the character is,
  20. ' then translate that into x and y coordinates so that we can clip
  21. ' the correct subimagerect out of the font bitmap for pasting.
  22. Global fontstring$ = " !~q#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz(|}~~"
  23. 'stringtotext("Test",1)
  24. Global curkey$
  25. Global cursorx=0
  26. Global cursory=0
  27.  
  28. Function drawapplefontchar(a$,x:Int,y:Int)
  29.         Local fontimagex:Int
  30.         Local fontimagey:Int
  31.         Local infontstring=fontstring.find(a$)
  32.         If infontstring > -1
  33.                 fontimagex = infontstring Mod 16
  34.                 fontimagey = infontstring / 16
  35.         EndIf
  36.         drawimagesubrect(textimage,x,y,fontimagex*16,64+fontimagey*32,16,32)
  37.        
  38.         End Function
  39.  
  40. While Not KeyDown(KEY_ESCAPE)
  41. curkey = GetChar()
  42. If curkey>0
  43.         cursorx = cursorx + 1
  44.         text[cursorx]=Chr(Int(curkey))
  45. EndIf
  46. Cls
  47. curchar=0
  48.  
  49. For j = 0 To 30
  50. For i = 0 To 80
  51. curchar=curchar+1
  52. dist=distance(i*16,j*32,MouseX(),MouseY())
  53. SetColor(255-dist,255-dist/2,255)
  54. drawapplefontchar text[curchar],i*16,j*32
  55.  
  56. Next
  57. Next
  58.  
  59. For i = 0 To 10
  60. plotchar(Chr(33+Rand(48)) ,Rand(80)-1,Rand(40)-1)
  61. Next
  62. Flip
  63. Wend
  64.  
  65. Function stringtotext(a$,num:Int)
  66.         For Local i:Int = 0 To Len(a$)-1
  67.         text[num+i]=Chr(a$[i])
  68.         Next
  69. End Function
  70.  
  71. Function plotchar(a$,x:Int,y:Int)
  72.         text[y*80+x] = a
  73. End Function
  74.  
  75. Function Distance#(x0#,y0#,x1#,y1#)
  76.         Local dx# = x0-x1
  77.         Local dy# = y0-y1
  78.         Return Sqr(dx*dx + dy*dy)
  79. End Function
  80.  
  81. Function DrawImageSubRect(Image:TImage, DrawX#, DrawY#, PartX#, PartY#, PartWidth#, PartHeight#, Frame# = 0)
  82. ' Not my code, someone else here did this a long time ago.  Draws a sub-rectangle of a given
  83. ' image to the screen.
  84.         Local OldX:Int
  85.         Local OldY:Int
  86.         Local OldWidth:Int
  87.         Local OldHeight:Int
  88.        
  89.         Local ViewportX:Int = DrawX
  90.         Local ViewportY:Int = DrawY
  91.        
  92.         ' Save current viewport settings
  93.         GetViewport(OldX, OldY, OldWidth, OldHeight)
  94.        
  95.         ' Calculate viewport coordinates based on image's handle       
  96.         If Image.Handle_X Then
  97.                 Local PercentX:Float
  98.                 PercentX = Float(Image.Handle_X) / Float(Image.Width)
  99.                 ViewportX = DrawX - (PercentX * PartWidth)
  100.         EndIf
  101.         If Image.Handle_Y Then
  102.                 Local PercentY:Float
  103.                 PercentY = Float(Image.Handle_Y) / Float(Image.Height)
  104.                 ViewportY = DrawY - (PercentY * PartHeight)
  105.         EndIf
  106.        
  107.         SetViewport(ViewportX, ViewportY, PartWidth, PartHeight)
  108.         DrawImage(Image, DrawX-PartX, DrawY-PartY, Frame)
  109.        
  110.         ' Restore old viewport settings
  111.         SetViewport(OldX, OldY, OldWidth, OldHeight)
  112. End Function


Comments :


AdamStrange(Posted 1+ years ago)

 Brilliant post :)Here's a slight different version that gives dynamic displays of different resolutions. It also allows for foreground and background colours!
Code: [Select]
' Quick and Dirty Text Display Console.  For roguelikes or whatever else you might want.

superstrict

'size of the window
Global ScreenWidth:Int = 1280
Global ScreenHeight:Int = 960

Global TextX:Int = 40
Global TextY:Int = 25

Graphics ScreenWidth, ScreenHeight

Global vdu:TVdu = New TVdu
vdu.init(ScreenWidth, ScreenHeight, TextX, TextY)
vdu.fontInit("applefont.png")



While Not KeyDown(KEY_ESCAPE)
vdu.DrawScreen()

Local k:Int

vdu.SetFcol(Rand(0,255), Rand(0,255), Rand(0,255))
vdu.SetBcol(Rand(0,255), Rand(0,255), Rand(0,255))
For k = 0 To 10
vdu.plotchar(Chr(Rand(33,127)) ,Rand(0, TextX-1), Rand(0, TextY-1))
Next

Flip
Wend

End

Type TVdu
'size of the display (usually 800x40)
Field TextXRes:Int
Field TextYRes:Int
Field TextXRes1:Int
Field TextYRes1:Int
Field TextMaxRes:Int

Field TextWidth:Float
Field TextHeight:Float

'where we put the chars
Field text:Int[]
'where we put the colours
Field fcol:Int[]
Field bcol:Int[]

field gFcol:Int
field gBcol:Int

'Load the font
field textimage:TImage = New TImage



Method Init(width:Int, height:Int, textx:Int, texty:int)
TextXRes = textx
TextYRes = texty
TextXRes1 = TextXRes-1
TextYRes1 = TextYRes-1
TextMaxRes = TextXRes * TextYRes

TextWidth = Float(Width) / Float(TextXRes)
TextHeight = Float(Height) / Float(TextYRes)

'redim the array to the new size
text = text[..TextMaxRes+1]
fCol = fCol[..TextMaxRes+1]
bCol = bCol[..TextMaxRes+1]
End Method


Method FontInit(FontName:String)
textimage = LoadImage(FontName)
End Method


method drawapplefontchar(char:Int, x:Int, y:Int)
Local fontimagex:Int
Local fontimagey:Int

If char > -1
fontimagex = char Mod 16
fontimagey = char Shr 4
EndIf

DrawSubImageRect textimage, x*TextWidth,y*TextHeight,TextWidth,TextHeight, fontimagex*16,64+fontimagey*32,16,32
End method


method DrawScreen()
Local curchar:Int = 0
Local x:Int
Local y:Int

Local fontimagex:Int
Local fontimagey:Int
Local xx:Float
Local yy:Float

For y = 0 To TextYRes1
For x = 0 To TextXRes1

If text[curchar] > -1
fontimagex = text[curchar] Mod 16
fontimagey = text[curchar] Shr 4

xx = x * TextWidth
yy = y * TextHeight

SetColor (Bcol[curchar] Shr 16) & 255, (Bcol[curchar] Shr 8) & 255, Bcol[curchar] & 255
DrawRect xx,yy,TextWidth,TextHeight
SetColor (Fcol[curchar] Shr 16) & 255, (Fcol[curchar] Shr 8) & 255, Fcol[curchar] & 255
DrawSubImageRect textimage, xx,yy,TextWidth,TextHeight, fontimagex*16,64+fontimagey*32,16,32
EndIf

curchar = curchar + 1
Next
Next
End method


method plotchar(a$, x:Int, y:Int)
Local pos:Int = (y * TextXRes) + x
If pos < 0 Then
pos = 0
Else If pos > TextMaxRes Then
pos = TextMaxRes
End If

text[pos] = Asc(a)
fcol[pos] = gFcol
bcol[pos] = gBcol
End method


method SetFCol(red:Int, green:Int, blue:Int)
gFCol = ((red & 255) Shl 16) | ((green & 255) Shl 8) | (blue & 255)
End method

method SetBCol(red:Int, green:Int, blue:Int)
gBCol = ((red & 255) Shl 16) | ((green & 255) Shl 8) | (blue & 255)
End method

End Type


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal