Skip to content

Commit

Permalink
Character as value type
Browse files Browse the repository at this point in the history
  • Loading branch information
piot committed Mar 24, 2015
1 parent 2b6333e commit aafae85
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 48 deletions.
22 changes: 4 additions & 18 deletions src/ConsoleCharacter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,13 @@

namespace Netcurses
{
public class ConsoleCharacter
public struct ConsoleCharacter
{
public char Character { get; set; }
public char Character;

public ConsoleColor Foreground { get; set; }
public ConsoleColor Foreground;

public ConsoleColor Background { get; set; }

public void Set (ConsoleCharacter ch)
{
Character = ch.Character;
Foreground = ch.Foreground;
Background = ch.Background;
}

public void Clear (ConsoleColor foreground, ConsoleColor background)
{
Character = ' ';
Foreground = foreground;
Background = background;
}
public ConsoleColor Background;

public bool IsSame (ConsoleCharacter ch)
{
Expand Down
6 changes: 3 additions & 3 deletions src/ConsoleScrollWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public ConsoleScrollWindow (int width, int height)
window = new ConsoleWindow (width, height);
}

void Newline()
void Newline ()
{
var y = window.Y;
if (y == window.Height - 1) {
Expand All @@ -25,7 +25,7 @@ void Newline()
window.Move (0, y);
}

public void AddStringEx(string s)
public void AddStringEx (string s)
{
for (var i = 0; i < s.Length; ++i) {
var ch = s [i];
Expand All @@ -48,7 +48,7 @@ public void AddString (string s, bool scroll)
}
var first = s.Substring (0, clamp);
var second = s.Substring (clamp);
AddStringEx(first);
AddStringEx (first);
if (!clamped) {
return;
}
Expand Down
28 changes: 21 additions & 7 deletions src/ConsoleUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,40 @@ public class ConsoleUpdater
{
readonly ConsoleWindow screenWindow = new ConsoleWindow (80, 25);

void SetCursorPosition (int x, int y)
{
Console.SetCursorPosition (x, y);
}

void SetCharacterAndColor (ConsoleCharacter targetChar, ConsoleCharacter sourceChar)
{
Console.BackgroundColor = sourceChar.Background;
Console.ForegroundColor = sourceChar.Foreground;
Console.Write (sourceChar.Character);
}

public void Update (ConsoleWindow window)
{
Console.CursorVisible = false;
int lastX = -1;
int lastY = -1;


var chars = window.ConsoleCharacters;
for (var y = 0; y < window.Height; ++y) {
for (var x = 0; x < window.Width; ++x) {
var sourceChar = chars [y * window.Width + x];
var targetChar = screenWindow.ConsoleCharacters [y * screenWindow.Width + x];
var sourceIndex = window.GetCharacterIndex (x, y);
var sourceChar = window.ConsoleCharacters [sourceIndex];
var targetIndex = screenWindow.GetCharacterIndex (x, y);
var targetChar = screenWindow.ConsoleCharacters [targetIndex];
if (!sourceChar.IsSame (targetChar)) {
if (x != lastX || y != lastY) {
Console.SetCursorPosition (x, y);
SetCursorPosition (x, y);
}
Console.BackgroundColor = sourceChar.Background;
Console.ForegroundColor = sourceChar.Foreground;
Console.Write (sourceChar.Character);
SetCharacterAndColor (targetChar, sourceChar);
lastX = x + 1;
lastY = y;
targetChar.Set (sourceChar);
screenWindow.ConsoleCharacters [targetIndex] = sourceChar;
}
}
}
Expand Down
52 changes: 35 additions & 17 deletions src/ConsoleWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ public ConsoleWindow (int width, int height)
this._width = width;
this._height = height;
Foreground = ConsoleColor.White;
Background = ConsoleColor.Cyan;


characters = new ConsoleCharacter[width * height];
for (var i = 0; i < Length; ++i) {
characters [i] = new ConsoleCharacter ();
characters [i] = new ConsoleCharacter {
Foreground = ConsoleColor.White,
Background = ConsoleColor.Cyan,
};

}
}
Expand All @@ -53,24 +57,31 @@ public void Copy (ConsoleWindow window, int xOffset, int yOffset)

for (var y = 0; y < minHeight; ++y) {
for (var x = 0; x < minWidth; ++x) {
GetCharacter (x + xOffset, y + yOffset).Set (window.GetCharacter (x, y));
var sourceIndex = window.GetCharacterIndex (x, y);
if (sourceIndex < 0) {
continue;
}
var targetIndex = GetCharacterIndex (x + xOffset, y + yOffset);
if (targetIndex < 0) {
continue;
}
characters [targetIndex] = window.characters [sourceIndex];
}
}
}

ConsoleCharacter GetCharacter (int x, int y)
public int GetCharacterIndex (int x, int y)
{
if (x < 0 || x >= _width || y < 0 || y >= _height) {
return null;
return -1;
}
var consoleCharacter = characters [y * _width + x];
return consoleCharacter;
return y * _width + x;
}

public void Clear ()
{
foreach (var ch in characters) {
ch.Clear (Foreground, Background);
for (var i = 0; i < characters.Length; ++i) {
characters [i] = new ConsoleCharacter () { Foreground = Foreground, Background = Background, Character = ' ' };
}
}

Expand All @@ -89,7 +100,11 @@ public void Fill (int xStart, int yStart, int width, int height)
var fillHeight = Math.Min (height, _height - top);
for (var x = 0; x < fillWidth; ++x) {
for (var y = 0; y < fillHeight; ++y) {
GetCharacter (x + left, y + top).Clear (Foreground, Background);
characters [GetCharacterIndex (x + left, y + top)] = new ConsoleCharacter () {
Foreground = Foreground,
Background = Background,
Character = ' '
};
}
}
}
Expand All @@ -102,13 +117,13 @@ public void Move (int x, int y)

public void SetCharacter (int x, int y, char ch)
{
var character = GetCharacter (x, y);
if (character == null) {
var characterIndex = GetCharacterIndex (x, y);
if (characterIndex < 0) {
return;
}
character.Character = ch;
character.Foreground = Foreground;
character.Background = Background;
characters [characterIndex].Character = ch;
characters [characterIndex].Foreground = Foreground;
characters [characterIndex].Background = Background;
}


Expand Down Expand Up @@ -144,16 +159,19 @@ public void RepeatCharacterVertical (char c, int length)
public void ClearLine ()
{
for (var x = X; x < Width; ++x) {

GetCharacter (x, Y).Clear (Foreground, Background);
characters [GetCharacterIndex (x, Y)] = new ConsoleCharacter () {
Foreground = Foreground,
Background = Background,
Character = ' '
};
}
}

public void ScrollUp ()
{
for (var y = 0; y < Height - 1; ++y) {
for (var x = 0; x < Width; ++x) {
GetCharacter (x, y).Set (GetCharacter (x, y + 1));
characters [GetCharacterIndex (x, y)] = characters [GetCharacterIndex (x, y + 1)];
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static void Main (string[] args)
const int diff = 40;

for (var anim = 0; anim < 100; ++anim) {
win.Background = ConsoleColor.Black;
win.Background = ConsoleColor.Blue ;
win.Clear ();
var x = (int)(Math.Sin (anim / 10.0f) * diff + diff);

Expand All @@ -42,10 +42,9 @@ public static void Main (string[] args)

}
scrolly.AddString ("This is a test", true);

win.Move (x, 10);
win.AddString ("Hello world!");
win.Copy (scrolly.Window, x / 4, x / 10);
win.Copy (scrolly.Window, 40 - x / 6, 10 - x / 3);
screen.Update (win);
Thread.Sleep (50);
}
Expand Down

0 comments on commit aafae85

Please sign in to comment.