Advertisement
Guest User

FastConsole.bas

a guest
Jan 19th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include once "CRT.bi"
  2. #include once "Windows.bi"
  3.  
  4. #define ConColor(_Back,_Fore) ((_Fore) OR ((_Back) shl 4))
  5. #define ConBlock(_Char, _Color) ((_Char) OR ((_Color) SHR 8)
  6.  
  7. #define BlockPtr(X, Y) ((X) + ((Y) * pCon.iWidth))
  8. #define CharPtr(X, Y) (((X) + ((Y) * pCon.iWidth)) SHL 1)
  9. #define AttrPtr(X, Y) (((X) + ((Y) * pCon.iWidth)) SHL 1) + 1
  10.  
  11. Type TxtGFX
  12.     ImgWidth as uShort
  13.     ImgHeight as uShort
  14.     Image as uByte Ptr
  15. End Type
  16.  
  17. namespace pCon
  18.     Dim as Integer iWidth, iHeight
  19.  
  20.     Dim Shared as HANDLE hOldScreen
  21.     Dim Shared as uShort Ptr pNewScreen
  22.  
  23.     Function Create(iWid as Integer = 80, iHei as Integer = 25) as uShort Ptr
  24.         If iWid <= 8 or iHei <= 4 Then
  25.             MessageBox(0, "ConsoleCreate: Bad console size selected", null, MB_ICONERROR)
  26.             return 0
  27.         End If
  28.        
  29.         iWidth = iWid: iHeight = iHei
  30.         width iWidth, iHeight
  31.        
  32.         hOldScreen = GetStdHandle(STD_OUTPUT_HANDLE)
  33.         pNewScreen = malloc(iWidth*iHeight*2)
  34.  
  35.         ' Hide the cursor in the console
  36.         Dim as CONSOLE_CURSOR_INFO BlankCursor
  37.         BlankCursor.dwSize = 100
  38.         BlankCursor.bVisible = FALSE
  39.         SetConsoleCursorInfo(hOldScreen, @BlankCursor)
  40.  
  41.         return pNewScreen
  42.     End Function    
  43.    
  44.     Sub Update
  45.         Dim as COORD ConSize, BuffStart
  46.         BuffStart.X = 0: BuffStart.Y = 0
  47.         ConSize.X = iWidth: ConSize.Y = iHeight
  48.        
  49.         Dim as SMALL_RECT WriteRegion
  50.         WriteRegion.Left = 0: WriteRegion.Top = 0
  51.         WriteRegion.Right = iWidth-1: WriteRegion.Bottom = iHeight-1
  52.        
  53.         ' Convert from DOS format to Windows console format
  54.         Dim as CHAR_INFO tNative(iWidth * iHeight)
  55.         For X as Integer = 0 to (iWidth * iHeight)
  56.             tNative(X).Char.AsciiChar = pNewScreen[X] AND &HFF
  57.             tNative(X).Attributes = pNewScreen[X] SHR 8
  58.         Next X
  59.        
  60.         WriteConsoleOutput(hOldScreen, @tNative(0), ConSize, BuffStart, @WriteRegion)
  61.     End Sub
  62.    
  63.     Sub ClearCon(ConColor as uByte = &H0F, ConChar as uByte = 32)
  64.         Dim as uShort CharBlock = (ConColor SHL 8) OR ConChar
  65.         For x as Integer = 0 To (iHeight * iWidth)-1
  66.             pNewScreen[X] = CharBlock
  67.         Next X
  68.     End Sub
  69.    
  70.     Sub DrawRect(X as Short, Y as Short, iWid as Short, iHei as Short, ConColor as uByte, ConChar as uByte)
  71.         Dim as uShort CharBlock = (ConColor SHL 8) OR ConChar
  72.        
  73.         var RectPart = (X + (Y * iWidth))
  74.         var RectY = 0
  75.         Do
  76.             For RectX as Integer = 0 to iWid-1
  77.                 pNewScreen[RectPart+RectX] = CharBlock
  78.             Next RectX
  79.             RectPart += iWidth: RectY += 1
  80.         Loop until (RectY = iHei)
  81.     End Sub
  82.    
  83.     Sub PrintCon(X as short, Y as short, TextInput as String)
  84.         var ScreenPos = ((X + (Y * iWidth)) SHR 1)
  85.         Dim as uByte Ptr pScreenPtr = Cast(uByte Ptr, pNewScreen)
  86.        
  87.         For TextPos as Integer = 0 to Len(TextInput)-1
  88.             If TextInput[TextPos] = 10 Then
  89.                 ScreenPos += iWidth SHL 1
  90.             ElseIf TextInput[TextPos] <> 13 Then
  91.                 pScreenPtr[ScreenPos] = TextInput[TextPos]
  92.                 ScreenPos += 2
  93.             End If
  94.         Next TextPos
  95.     End Sub
  96.    
  97.     Sub Destroy()
  98.         Free(pNewScreen)
  99.         iWidth = 0: iHeight = 0
  100.     End Sub
  101.    
  102.     ' Graphics routines
  103.     Function CreateImage(ImgWidth as Short, ImgHeight as Short) as TxtGFX
  104.         Dim as TxtGFX NewImg
  105.         With NewImg
  106.             .ImgWidth = ImgWidth
  107.             .ImgHeight = ImgHeight
  108.             .Image = malloc(ImgWidth*ImgHeight)
  109.         End With
  110.         Return NewImg
  111.     End Function
  112.    
  113.     Sub DestroyImage(OldImage as TxtGFX)
  114.         With OldImage
  115.             .ImgWidth = 0
  116.             .ImgHeight = 0
  117.             .Image = 0
  118.         End With
  119.     End Sub
  120.    
  121.     Sub GetGfx(X as Short, Y as Short, Byref ImageBuff as TxtGFX)
  122.         Dim as Short ImagePos = 0
  123.         var ScreenStart = BlockPtr(X, Y)
  124.         With ImageBuff
  125.             For OffSetY as Integer = X to X+.ImgWidth-1
  126.                 For OffSetX as Integer = Y to Y+.ImgHeight-1
  127.                     .Image[ImagePos] = pNewScreen[BlockPtr(OffSetX, OffSetY)]
  128.                     ImagePos += 1
  129.                 Next OffSetX
  130.             Next OffsetY
  131.         End With
  132.     End Sub
  133.    
  134.     Sub PutGfx(X as Short, Y as Short, Byref ImageBuff as TxtGFX)
  135.         Dim as Short ImagePos = 0
  136.         var ScreenStart = BlockPtr(X, Y)
  137.         With ImageBuff
  138.             For OffSetY as Integer = X to X+.ImgWidth-1
  139.                 For OffSetX as Integer = Y to Y+.ImgHeight-1
  140.                     pNewScreen[BlockPtr(OffSetX, OffSetY)] = .Image[ImagePos]
  141.                     ImagePos += 1
  142.                 Next OffsetX
  143.             Next OffSetY
  144.         End With
  145.     End Sub
  146. end namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement