* * *

Author Topic: ASCII characters  (Read 850 times)

copY

  • New member
  • *
  • Posts: 8
ASCII characters
« on: April 24, 2017, 07:32:38 pm »
Hi! Im trying to write a basic XO application, but when im trying to draw the space itself, where the 2 players gonna play the program writes letters instead of just writing out what is in the ' '-s. Any solution for this? As u can see in the code the characters are what i want to write out. Thank You.
Sorry for my bad english.
~copY

Bart

  • Hero Member
  • *****
  • Posts: 2673
    • Bart en Mariska's Webstek
Re: ASCII characters
« Reply #1 on: April 24, 2017, 07:50:54 pm »
First of all,the characters you want to display is NOT ASCII.
Second: the codepage the console uses is not the default system codepage, but OEM codepage.

This uit might be of help:
Code: Pascal  [Select]
  1. unit setdefaultcodepages;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows;
  7.  
  8. implementation
  9.  
  10. Const
  11.   LF_FACESIZE = 32;
  12.  
  13. Type
  14.   CONSOLE_FONT_INFOEX = record
  15.     cbSize      : ULONG;
  16.     nFont       : DWORD;
  17.     dwFontSizeX : SHORT;
  18.     dwFontSizeY : SHORT;
  19.     FontFamily  : UINT;
  20.     FontWeight  : UINT;
  21.     FaceName    : array [0..LF_FACESIZE-1] of WCHAR;
  22.   end;
  23.  
  24. { Only supported in Vista and onwards!}
  25.  
  26. function SetCurrentConsoleFontEx(hConsoleOutput: HANDLE; bMaximumWindow: BOOL; var CONSOLE_FONT_INFOEX): BOOL; stdcall; external 'kernel32.dll' name 'SetCurrentConsoleFontEx';
  27. var
  28.   New_CONSOLE_FONT_INFOEX : CONSOLE_FONT_INFOEX;
  29.  
  30. initialization
  31.   //writeln('SetDefaultCodepages unit initialization: DefaultSystemCodePage = ',DefaultSystemCodePage);
  32.   {$ifdef DisableUTF8RTL}
  33.   SetConsoleOutputCP(DefaultSystemCodePage);
  34.   SetTextCodepage(Output, DefaultSystemCodePage);
  35.   {$else}
  36.   SetConsoleOutputCP(cp_utf8);
  37.   SetTextCodepage(Output, cp_utf8);
  38.   {$endif}
  39.  
  40.   FillChar(New_CONSOLE_FONT_INFOEX, SizeOf(CONSOLE_FONT_INFOEX), 0);
  41.   New_CONSOLE_FONT_INFOEX.cbSize := SizeOf(CONSOLE_FONT_INFOEX);
  42. //  New_CONSOLE_FONT_INFOEX.FaceName := 'Lucida Console';
  43.   New_CONSOLE_FONT_INFOEX.FaceName := 'Consolas';
  44.   New_CONSOLE_FONT_INFOEX.dwFontSizeX := 8;
  45.   New_CONSOLE_FONT_INFOEX.dwFontSizeY := 16;
  46.  
  47.   SetCurrentConsoleFontEx(StdOutputHandle, False, New_CONSOLE_FONT_INFOEX);
  48. end.
  49.  

Now the console will understand UTF8 (which is what you are writing).

You need to add a dependency on LazUtils to your project.
(Use: Menu->Project->Project inspector)

Bart

copY

  • New member
  • *
  • Posts: 8
Re: ASCII characters
« Reply #2 on: April 24, 2017, 07:53:26 pm »
Thank you man. :)

Bart

  • Hero Member
  • *****
  • Posts: 2673
    • Bart en Mariska's Webstek
Re: ASCII characters
« Reply #3 on: April 24, 2017, 07:55:49 pm »
You posted this message 3 times.
This is an absolute no-no.

Bart

copY

  • New member
  • *
  • Posts: 8
Re: ASCII characters
« Reply #4 on: April 24, 2017, 08:22:50 pm »
I need a little bit more help looks like, because i added a package to my project named LazUtils, but is that all i have to do? If you have a little time for this please help me out:)
~copY

Bart

  • Hero Member
  • *****
  • Posts: 2673
    • Bart en Mariska's Webstek
Re: ASCII characters
« Reply #5 on: April 24, 2017, 11:24:48 pm »
Just add setdefaultcodepages to the uses clause of your program, that should be enough (IIRC).

Bart

copY

  • New member
  • *
  • Posts: 8
Re: ASCII characters
« Reply #6 on: April 25, 2017, 03:47:47 pm »
yo man, if you are reading this, can you illustrate or something what should i do? still not working at all. Working vry well until i dont use crt, after that the characters change.
~copY

Bart

  • Hero Member
  • *****
  • Posts: 2673
    • Bart en Mariska's Webstek
Re: ASCII characters
« Reply #7 on: April 25, 2017, 06:39:39 pm »
Code: Pascal  [Select]
  1. program cp;
  2. {$codepage utf8}
  3. {$mode objfpc}
  4. {$h+}
  5. uses
  6.   Sysutils, setdefaultcodepages, LazUtf8;
  7. begin
  8.   writeln('┌─┐');
  9.   writeln('│ │');
  10.   writeln('├─┤');
  11.   writeln('└─┘');
  12.   writeln('╔═╗');
  13.   writeln('║ ║');
  14.   writeln('╠═╣');
  15.   writeln('╚═╝');
  16. end.
  17.  

And it outputs:

Code: [Select]
C:\Users\Bart\LazarusProjecten\bugs\Utf8\cpstring>utf8
┌─┐
│ │
├─┤
└─┘
╔═╗
║ ║
╠═╣
╚═╝

Note: The preview in this forum isn't 100% accurate. In the console the lines actually line up and make contact.

Bart

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus