diff --git a/Examples/Console/DateTime/DateTime.dpr b/Examples/Console/DateTime/DateTime.dpr new file mode 100644 index 0000000..3f591b8 --- /dev/null +++ b/Examples/Console/DateTime/DateTime.dpr @@ -0,0 +1,34 @@ +program DateTime; + +uses + KolibriOS, CRT; + +var + SystemDate: TSystemDate; + SystemTime: TSystemTime; + CursorPos: TConsolePoint; + +begin + ConsoleInit('Date/Time'); + + SetCursorHeight(0); + SetCursorPos(27, 11); + Write( + 'System Date and System Time'#10 + + ' ' + ); + CursorPos := GetCursorPos; + repeat + SystemDate := GetSystemDate; + SystemTime := GetSystemTime; + with SystemDate, SystemTime do + begin + Write('%02x.%02x.%02x', Day, Month, Year); + Write(' - %02x:%02x:%02x', Hours, Minutes, Seconds); + end; + SetCursorPos(CursorPos); + Sleep(50); + until KeyPressed; + + ConsoleExit(True); +end. diff --git a/Examples/ConsoleColors/ConsoleColors.dpr b/Examples/ConsoleColors/ConsoleColors.dpr new file mode 100644 index 0000000..67092e0 --- /dev/null +++ b/Examples/ConsoleColors/ConsoleColors.dpr @@ -0,0 +1,80 @@ +program ConsoleColors; + +uses + CRT; + +begin + ConsoleInit('Console Colors'); + + TextBackground(Black); + WriteLn('Black'); + TextBackground(Blue); + WriteLn('Blue'); + TextBackground(Green); + WriteLn('Green'); + TextBackground(Cyan); + WriteLn('Cyan'); + TextBackground(Red); + WriteLn('Red'); + TextBackground(Magenta); + WriteLn('Magenta'); + TextBackground(Brown); + WriteLn('Brown'); + TextBackground(LightGray); + WriteLn('LightGray'); + TextBackground(DarkGray); + WriteLn('DarkGray'); + TextBackground(LightBlue); + WriteLn('LightBlue'); + TextBackground(LightGreen); + WriteLn('LightGreen'); + TextBackground(LightCyan); + WriteLn('LightCyan'); + TextBackground(LightRed); + WriteLn('LightRed'); + TextBackground(LightMagenta); + WriteLn('LightMagenta'); + TextBackground(Yellow); + WriteLn('Yellow'); + TextBackground(White); + WriteLn('White'); + + ResetAttributes; + + TextColor(Black); + WriteLn('Black'); + TextColor(Blue); + WriteLn('Blue'); + TextColor(Green); + WriteLn('Green'); + TextColor(Cyan); + WriteLn('Cyan'); + TextColor(Red); + WriteLn('Red'); + TextColor(Magenta); + WriteLn('Magenta'); + TextColor(Brown); + WriteLn('Brown'); + TextColor(LightGray); + WriteLn('LightGray'); + TextColor(DarkGray); + WriteLn('DarkGray'); + TextColor(LightBlue); + WriteLn('LightBlue'); + TextColor(LightGreen); + WriteLn('LightGreen'); + TextColor(LightCyan); + WriteLn('LightCyan'); + TextColor(LightRed); + WriteLn('LightRed'); + TextColor(LightMagenta); + WriteLn('LightMagenta'); + TextColor(Yellow); + WriteLn('Yellow'); + TextColor(White); + WriteLn('White'); + + ReadKey; + + ConsoleExit(True); +end. diff --git a/Examples/ConsoleColors/build.bat b/Examples/ConsoleColors/build.bat new file mode 100644 index 0000000..836431b --- /dev/null +++ b/Examples/ConsoleColors/build.bat @@ -0,0 +1 @@ +@call "%~dp0..\..\Lib\build.bat" "%~dp0ConsoleColors" \ No newline at end of file diff --git a/Examples/DateTime/DateTime.dpr b/Examples/DateTime/DateTime.dpr index 43e9e10..e6c72dd 100644 --- a/Examples/DateTime/DateTime.dpr +++ b/Examples/DateTime/DateTime.dpr @@ -1,53 +1,34 @@ program DateTime; uses - KolibriOS; + KolibriOS, CRT; var - hConsole: Pointer; - ConsoleInit: procedure(WndWidth, WndHeight, ScrWidth, ScrHeight: LongWord; Caption: PKolibriChar); stdcall; - ConsoleExit: procedure(bCloseWindow: Cardinal); stdcall; - Printf: function(const Format: PKolibriChar): Integer; cdecl varargs; - GetConsoleCursorPos: procedure(X, Y: PInteger); stdcall; - SetConsoleCursorPos: procedure(X, Y: Integer); stdcall; - kbhit: function: Boolean; - SetConsoleCursorHeight: function(Height: Integer): Integer; stdcall; -// ConsoleCls: procedure; - SystemDate: TSystemDate; SystemTime: TSystemTime; - X, Y: Integer; + CursorPos: TConsolePoint; begin - hConsole := LoadLibrary('/sys/lib/console.obj'); - ConsoleInit := GetProcAddress(hConsole, 'con_init'); - ConsoleExit := GetProcAddress(hConsole, 'con_exit'); - GetConsoleCursorPos := GetProcAddress(hConsole, 'con_get_cursor_pos'); - SetConsoleCursorPos := GetProcAddress(hConsole, 'con_set_cursor_pos'); - SetConsoleCursorHeight := GetProcAddress(hConsole, 'con_set_cursor_height'); - Printf := GetProcAddress(hConsole, 'con_printf'); - KBHit := GetProcAddress(hConsole, 'con_kbhit'); + ConsoleInit('Date/Time'); - ConsoleInit($FFFFFFFF, $FFFFFFFF, $FFFFFFFF, $FFFFFFFF, 'Date/Time'); - - SetConsoleCursorHeight(0); - SetConsoleCursorPos(27, 11); - Printf( - 'SystemDate and SystemTime'#10 + - ' ' + SetCursorHeight(0); + SetCursorPos(27, 11); + Write( + 'System Date and System Time'#10 + + ' ' ); - GetConsoleCursorPos(@X, @Y); + CursorPos := GetCursorPos; repeat SystemDate := GetSystemDate; SystemTime := GetSystemTime; with SystemDate, SystemTime do begin - Printf('%02x.%02x.%02x', Day, Month, Year); - Printf(' - %02x:%02x:%02x', Hours, Minutes, Seconds); + Write('%02x.%02x.%02x', Day, Month, Year); + Write(' - %02x:%02x:%02x', Hours, Minutes, Seconds); end; - SetConsoleCursorPos(X, Y); + SetCursorPos(CursorPos); Sleep(50); - until KBHit; + until Escape; - ConsoleExit(1); + ConsoleExit(True); end. diff --git a/Examples/Examples.bpg b/Examples/Examples.bpg index 7378a4c..65222ef 100644 --- a/Examples/Examples.bpg +++ b/Examples/Examples.bpg @@ -9,8 +9,8 @@ MAKE = $(ROOT)\bin\make.exe -$(MAKEFLAGS) -f$** DCC = $(ROOT)\bin\dcc32.exe $** BRCC = $(ROOT)\bin\brcc32.exe $** #------------------------------------------------------------------------------ -PROJECTS = ColorButtons.exe DateTime.exe DrawImage.exe DrawImageEx.exe DrawText.exe GetCurrentDirectory.exe GetPixel.exe \ - GetPointOwner.exe Hello.exe LoadFile.exe ReadFolder.exe Screenshot.exe SetCursor.exe SetPixel.exe SetWindowPos.exe +PROJECTS = ColorButtons.exe ConsoleColors.exe DateTime.exe DrawImage.exe DrawImageEx.exe DrawText.exe GetCurrentDirectory.exe \ + GetPixel.exe GetPointOwner.exe Hello.exe HelloGUI.exe LoadFile.exe ReadFolder.exe Screenshot.exe SetCursor.exe SetPixel.exe SetWindowPos.exe #------------------------------------------------------------------------------ default: $(PROJECTS) #------------------------------------------------------------------------------ @@ -18,6 +18,9 @@ default: $(PROJECTS) ColorButtons.exe: ColorButtons\ColorButtons.dpr $(DCC) +ConsoleColors.exe: ConsoleColors\ConsoleColors.dpr + $(DCC) + DateTime.exe: DateTime\DateTime.dpr $(DCC) @@ -42,6 +45,9 @@ GetPointOwner.exe: GetPointOwner\GetPointOwner.dpr Hello.exe: Hello\Hello.dpr $(DCC) +HelloGUI.exe: Hello\HelloGUI.dpr + $(DCC) + LoadFile.exe: LoadFile\LoadFile.dpr $(DCC) @@ -58,6 +64,4 @@ SetPixel.exe: SetPixel\SetPixel.dpr $(DCC) SetWindowPos.exe: SetWindowPos\SetWindowPos.dpr - $(DCC) - - + $(DCC) \ No newline at end of file diff --git a/Examples/GetCurrentDir/GetCurrentDir.dpr b/Examples/GetCurrentDir/GetCurrentDir.dpr index 5528d65..d57b94f 100644 --- a/Examples/GetCurrentDir/GetCurrentDir.dpr +++ b/Examples/GetCurrentDir/GetCurrentDir.dpr @@ -1,7 +1,7 @@ program GetCurrentDir; uses - KolibriOS; + KolibriOS, CRT; const AppPath = PPKolibriChar(32); @@ -10,26 +10,16 @@ const BUFFER_SIZE = 256; var - hConsole: Pointer; - ConsoleInit: procedure(WndWidth, WndHeight, ScrWidth, ScrHeight: LongWord; Caption: PKolibriChar); stdcall; - ConsoleExit: procedure(bCloseWindow: Cardinal); stdcall; - printf: function(const Format: PKolibriChar): Integer; CDecl varargs; - Buffer: array[0..BUFFER_SIZE - 1] of Char; begin - hConsole := LoadLibrary('/sys/lib/console.obj'); - ConsoleInit := GetProcAddress(hConsole, 'con_init'); - ConsoleExit := GetProcAddress(hConsole, 'con_exit'); - printf := GetProcAddress(hConsole, 'con_printf'); - - ConsoleInit($FFFFFFFF, $FFFFFFFF, $FFFFFFFF, $FFFFFFFF, 'Get Current Directory'); + ConsoleInit('Get Current Directory'); GetCurrentDirectory(Buffer, BUFFER_SIZE); - printf('AppPath is "%s"'#10, AppPath^); - printf('CmdLine is "%s"'#10, CmdLine^); - printf('CurrentDirectory is "%s"'#10, Buffer); + Write('AppPath is "%s"'#10, AppPath^); + Write('CmdLine is "%s"'#10, CmdLine^); + Write('Current Directory is "%s"'#10, Buffer); - ConsoleExit(0); + ConsoleExit(False); end. diff --git a/Examples/Hello/Hello.dpr b/Examples/Hello/Hello.dpr index 655706c..290f47a 100644 --- a/Examples/Hello/Hello.dpr +++ b/Examples/Hello/Hello.dpr @@ -1,33 +1,13 @@ program Hello; uses - KolibriOS; - -var - WndLeft, WndTop, WndWidth, WndHeight: Integer; + CRT; begin - with GetScreenSize do - begin - WndWidth := Width div 4; - WndHeight := Height div 4; - WndLeft := (Width - WndWidth) div 2; - WndTop := (Height - WndHeight) div 2; - end; + ConsoleInit('Hello'); - while True do - case WaitEvent of - REDRAW_EVENT: - begin - BeginDraw; - DrawWindow(WndLeft, WndTop, WndWidth, WndHeight, 'Hello!', $00FFFFFF, - WS_SKINNED_FIXED + WS_CLIENT_COORDS + WS_CAPTION, CAPTION_MOVABLE); - EndDraw; - end; - KEY_EVENT: - GetKey; - BUTTON_EVENT: - if GetButton.ID = 1 then - TerminateThread; - end; + WriteLn('Hello, world!'); + + ReadKey; + ConsoleExit(True); end. diff --git a/Examples/Hello/HelloGUI.dpr b/Examples/Hello/HelloGUI.dpr new file mode 100644 index 0000000..626bcf6 --- /dev/null +++ b/Examples/Hello/HelloGUI.dpr @@ -0,0 +1,33 @@ +program HelloGUI; + +uses + KolibriOS; + +var + WndLeft, WndTop, WndWidth, WndHeight: Integer; + +begin + with GetScreenSize do + begin + WndWidth := Width div 4; + WndHeight := Height div 4; + WndLeft := (Width - WndWidth) div 2; + WndTop := (Height - WndHeight) div 2; + end; + + while True do + case WaitEvent of + REDRAW_EVENT: + begin + BeginDraw; + DrawWindow(WndLeft, WndTop, WndWidth, WndHeight, 'Hello!', $00FFFFFF, + WS_SKINNED_FIXED + WS_CLIENT_COORDS + WS_CAPTION, CAPTION_MOVABLE); + EndDraw; + end; + KEY_EVENT: + GetKey; + BUTTON_EVENT: + if GetButton.ID = 1 then + TerminateThread; + end; +end. diff --git a/Examples/Hello/build.bat b/Examples/Hello/build.bat index 4cd7f03..111a36a 100644 --- a/Examples/Hello/build.bat +++ b/Examples/Hello/build.bat @@ -1 +1,3 @@ -@call "%~dp0..\..\Lib\build.bat" "%~dp0Hello" \ No newline at end of file +@echo off +call "%~dp0..\..\Lib\build.bat" "%~dp0Hello" +call "%~dp0..\..\Lib\build.bat" "%~dp0HelloGUI" \ No newline at end of file diff --git a/Examples/LoadFile/LoadFile.dpr b/Examples/LoadFile/LoadFile.dpr index e1fc887..9343c0b 100644 --- a/Examples/LoadFile/LoadFile.dpr +++ b/Examples/LoadFile/LoadFile.dpr @@ -1,27 +1,17 @@ program LoadFileApp; uses - KolibriOS; + KolibriOS, CRT; var - hConsole: Pointer; - ConsoleInit: procedure(WndWidth, WndHeight, ScrWidth, ScrHeight: LongWord; Caption: PKolibriChar); stdcall; - ConsoleExit: procedure(bCloseWindow: Cardinal); stdcall; - WriteN: procedure(Str: PKolibriChar; Count: LongWord); stdcall; - FileSize: LongWord; Buffer: Pointer; begin - hConsole := LoadLibrary('/sys/lib/console.obj'); - ConsoleInit := GetProcAddress(hConsole, 'con_init'); - ConsoleExit := GetProcAddress(hConsole, 'con_exit'); - WriteN := GetProcAddress(hConsole, 'con_write_string'); - - ConsoleInit($FFFFFFFF, $FFFFFFFF, $FFFFFFFF, $FFFFFFFF, 'Load File'); + ConsoleInit('Load File'); Buffer := LoadFile('/sys/example.asm', FileSize); - WriteN(Buffer, FileSize); + WriteText(Buffer, FileSize); - ConsoleExit(0); + ConsoleExit(False); end. diff --git a/Examples/ReadFolder/ReadFolder.dpr b/Examples/ReadFolder/ReadFolder.dpr index 48a54f2..fd3f153 100644 --- a/Examples/ReadFolder/ReadFolder.dpr +++ b/Examples/ReadFolder/ReadFolder.dpr @@ -1,19 +1,13 @@ program ReadFolderApp; uses - KolibriOS; + KolibriOS, CRT; type TInt64Rec = packed record Lo, Hi: LongWord; end; -var - hConsole: Pointer; - ConsoleInit: procedure(WndWidth, WndHeight, ScrWidth, ScrHeight: LongWord; Caption: PKolibriChar); stdcall; - ConsoleExit: procedure(bCloseWindow: Cardinal); stdcall; - printf: function(const Format: PKolibriChar): Integer; cdecl varargs; - const FolderPath = '/sys'; @@ -23,46 +17,42 @@ var Pos: LongWord; begin - hConsole := LoadLibrary('/sys/lib/console.obj'); - ConsoleInit := GetProcAddress(hConsole, 'con_init'); - ConsoleExit := GetProcAddress(hConsole, 'con_exit'); - printf := GetProcAddress(hConsole, 'con_printf'); + ConsoleInit('Read Folder'); if ReadFolder(FolderPath, FolderInformation, 0, 0, 0, BlocksRead) = 0 then with FolderInformation do - begin - ConsoleInit($FFFFFFFF, $FFFFFFFF, $FFFFFFFF, {11 printf In Loop}11 * FileCount + {2 printf below}2 + 1, 'ReadFolder'); - printf('Folder "%s" contains %u files and/or folders.'#10, FolderPath, FileCount); - printf(#10); - end + Write('Folder "%s" contains %u files and/or folders.'#10#10, FolderPath, FileCount) else - begin - ConsoleInit($FFFFFFFF, $FFFFFFFF, $FFFFFFFF, $FFFFFFFF, 'Read Folder'); - printf('Folder "%s" can not be read.'#10, FolderPath); - end; + Write('Folder "%s" can not be read.'#10, FolderPath); Pos := 0; while ReadFolder(FolderPath, FolderInformation, 1, Pos, 0, BlocksRead) = 0 do begin with FolderInformation, FileInformation[0] do begin - printf('FileName = %s'#10, Name); + Write('FileName = %s'#10, Name); with Attributes do begin - printf('SizeLo = %u'#10, TInt64Rec(Size).Lo); - printf('SizeHi = %u'#10, TInt64Rec(Size).Hi); - with Modify.Date do printf('modifyDate = %02d.%02d.%02d'#10, Day, Month, Year); - with Modify.Time do printf('modifyTime = %02d:%02d:%02d'#10, Hours, Minutes, Seconds); - with Access.Date do printf('AccessDate = %02d.%02d.%02d'#10, Day, Month, Year); - with Access.Time do printf('AccessTime = %02d:%02d:%02d'#10, Hours, Minutes, Seconds); - with Creation.Date do printf('CreationDate = %02d.%02d.%02d'#10, Day, Month, Year); - with Creation.Time do printf('CreationTime = %02d:%02d:%02d'#10, Hours, Minutes, Seconds); - printf('Attributes = 0x%08x'#10, Attributes); + Write( 'SizeLo = %u'#10, TInt64Rec(Size).Lo); + Write( 'SizeHi = %u'#10, TInt64Rec(Size).Hi); + with Modify.Date do + Write('Modify Date = %02d.%02d.%02d'#10, Day, Month, Year); + with Modify.Time do + Write('Modify Time = %02d:%02d:%02d'#10, Hours, Minutes, Seconds); + with Access.Date do + Write('Access Date = %02d.%02d.%02d'#10, Day, Month, Year); + with Access.Time do + Write('Access Time = %02d:%02d:%02d'#10, Hours, Minutes, Seconds); + with Creation.Date do + Write('Creation Date = %02d.%02d.%02d'#10, Day, Month, Year); + with Creation.Time do + Write('Creation Time = %02d:%02d:%02d'#10, Hours, Minutes, Seconds); + Write( 'Attributes = 0x%08x'#10, Attributes); end; end; - printf(#10); + Write(#10); Inc(Pos); end; - ConsoleExit(0); + ConsoleExit(False); end. diff --git a/Lib/CRT.pas b/Lib/CRT.pas new file mode 100644 index 0000000..bcbf5cd --- /dev/null +++ b/Lib/CRT.pas @@ -0,0 +1,168 @@ +unit CRT; + +interface + +uses + KolibriOS; + +type + TConsolePoint = record + X, Y: Integer; + end; + +const + Black = 0; + Blue = 1; + Green = 2; + Cyan = 3; + Red = 4; + Magenta = 5; + Brown = 6; // none in KolibriOS? + LightGray = 7; + DarkGray = 8; // none in KolibriOS? + LightBlue = 9; + LightGreen = 10; + LightCyan = 11; + LightRed = 12; + LightMagenta = 13; + Yellow = 14; + White = 15; + +procedure ConsoleInit(Title: PKolibriChar); + +function GetCursorPos: TConsolePoint; +procedure SetCursorPos(X, Y: Integer); overload; +procedure SetCursorPos(const Point: TConsolePoint); overload; + +procedure ResetAttributes; +procedure TextAttribute(Color, Background: Integer); +procedure TextBackground(Color: Integer); +procedure TextColor(Color: Integer); + +function WriteLn(LineBreaks: Integer = 1): LongInt; overload; +function WriteLn(Text: PKolibriChar; LineBreaks: Integer = 1): LongInt; overload; + +var + ConsoleExit: procedure(CloseWindow: Boolean); stdcall; + KeyPressed: function: Boolean; + ReadKey: function: KolibriChar; stdcall; + SetCursorHeight: function(Height: Integer): Integer; stdcall; + Write: function(const Text: PKolibriChar): LongInt; cdecl varargs; + WriteText: procedure(Text: PKolibriChar; Length: LongWord); stdcall; + +implementation + +procedure ResetAttributes; +begin + Write(#27'[0m'); +end; + +procedure TextAttribute(Color, Background: Integer); +begin + TextColor(Color); + TextBackground(Background); +end; + +procedure TextBackground(Color: Integer); +const + Light = #27'[1m'; + Colors: array[Black..LightGray] of PKolibriChar = ( + #27'[40m', // Black + #27'[44m', // Blue + #27'[42m', // Green + #27'[46m', // Cyan + #27'[41m', // Red + #27'[45m', // Magenta + #27'[43m', // Brown + #27'[37m' // LightGray + ); +begin + case Color of + Black..LightGray: + Write(Colors[Color]); + DarkGray..White: + begin + Write(Colors[Color]); + Write(Light); + end; + end; +end; + +procedure TextColor(Color: Integer); +const + Light = #27'[5m'; + Colors: array[Black..LightGray] of PKolibriChar = ( + #27'[30m', // Black + #27'[34m', // Blue + #27'[32m', // Green + #27'[36m', // Cyan + #27'[31m', // Red + #27'[35m', // Magenta + #27'[33m', // Brown + #27'[37m' // LightGray + ); +begin + case Color of + Black..LightGray: + Write(Colors[Color]); + DarkGray..White: + begin + Write(Colors[Color]); + Write(Light); + end; + end; +end; + +function WriteLn(LineBreaks: Integer): LongInt; +var + I: Integer; +begin + Result := 0; + for I := 0 to LineBreaks - 1 do + Inc(Result, Write(#10)); +end; + +function WriteLn(Text: PKolibriChar; LineBreaks: Integer): LongInt; +begin + Result := Write(Text) + WriteLn(LineBreaks); +end; + +var + hConsole: Pointer; + ConsoleInitProc: procedure(WndWidth, WndHeight, ScrWidth, ScrHeight: LongInt; Caption: PKolibriChar); stdcall; + GetCursorPosProc: procedure(var X, Y: Integer); stdcall; + SetCursorPosProc: procedure(X, Y: Integer); stdcall; + +procedure ConsoleInit(Title: PKolibriChar); +begin + hConsole := LoadLibrary('/sys/lib/console.obj'); + ConsoleInitProc := GetProcAddress(hConsole, 'con_init'); + ConsoleExit := GetProcAddress(hConsole, 'con_exit'); + GetCursorPosProc := GetProcAddress(hConsole, 'con_get_cursor_pos'); + KeyPressed := GetProcAddress(hConsole, 'con_kbhit'); + ReadKey := GetProcAddress(hConsole, 'con_getch'); + SetCursorHeight := GetProcAddress(hConsole, 'con_set_cursor_height'); + SetCursorPosProc := GetProcAddress(hConsole, 'con_set_cursor_pos'); + Write := GetProcAddress(hConsole, 'con_printf'); + WriteText := GetProcAddress(hConsole, 'con_write_string'); + + ConsoleInitProc(-1, -1, -1, -1, Title); +end; + +function GetCursorPos: TConsolePoint; +begin + GetCursorPosProc(Result.X, Result.Y); +end; + +procedure SetCursorPos(X, Y: Integer); +begin + SetCursorPosProc(X, Y); +end; + +procedure SetCursorPos(const Point: TConsolePoint); +begin + with Point do + SetCursorPosProc(X, Y); +end; + +end. diff --git a/license.md b/license.md index bd851de..3953671 100644 --- a/license.md +++ b/license.md @@ -1,5 +1,5 @@ Copyright © 2017-2019 0CodErr -Copyright © 2020 Vladislav Javadov (aka Freeman) +Copyright © 2020 0CodErr, Nikolay Burshtyn (aka amber8706), Vladislav Javadov (aka Freeman) All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: