Skip to content

Commit

Permalink
#2 Simplified ScreenBufferPixels[] code by making it 2-dimensional, o…
Browse files Browse the repository at this point in the history
…ffset frame based rendering by border width
  • Loading branch information
hagronnestad committed Oct 21, 2019
1 parent e55a801 commit 41c5202
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
8 changes: 4 additions & 4 deletions ComputerSystems/Commodore64/FormC64Screen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ private void pScreen_DragEnter(object sender, DragEventArgs e) {
}
}

public void SetPixels(Bitmap b, Color[] pixels) {
public void SetPixels(Bitmap b, Color[,] pixels) {
var width = b.Width;
var height = b.Height;

Expand All @@ -271,9 +271,9 @@ public void SetPixels(Bitmap b, Color[] pixels) {

var index = (y * width) + x;

ptr[(x * 3) + y * stride] = pixels[index].B;
ptr[(x * 3) + y * stride + 1] = pixels[index].G;
ptr[(x * 3) + y * stride + 2] = pixels[index].R;
ptr[(x * 3) + y * stride] = pixels[y, x].B;
ptr[(x * 3) + y * stride + 1] = pixels[y, x].G;
ptr[(x * 3) + y * stride + 2] = pixels[y, x].R;
}

}
Expand Down
37 changes: 25 additions & 12 deletions ComputerSystems/Commodore64/VicIi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class VicIi {
private const byte REGISTER_INTERRUPT_STATUS_0x19 = 0x19;
private const byte REGISTER_INTERRUPT_CONTROL_0x1A = 0x1A;

public Color[] ScreenBufferPixels { get; }
public Color[,] ScreenBufferPixels { get; }

private readonly TvSystem _tvSystem;
private int _rasterLineToGenerateInterruptAt = 0;
Expand Down Expand Up @@ -87,20 +87,23 @@ public enum TvSystem {

public const int FULL_WIDTH = 504;
public const int FULL_WIDTH_CYCLES = 63;
public const int USABLE_WIDTH_BORDER = 320;
public const int USABLE_WIDTH_BORDER = 403;
public const int USABLE_WIDTH = 320;
public const int USABLE_WIDTH_CYCLES = 40;

public const int FULL_HEIGHT_NTSC = 262;
public const int FULL_HEIGHT_PAL = 312;
public const int USABLE_HEIGHT = 200;
public const int USABLE_HEIGHT_BORDER = 200;
public const int USABLE_HEIGHT_BORDER = 284;

public int CurrentLine = 0;
public int CurrentLineCycle = 0;

public bool InVerticalBlank => CurrentLine >= 300 || CurrentLine <= 15;
public bool InBorder => (CurrentLineCycle >= 50 && CurrentLineCycle <= 92) || (CurrentLineCycle >= 412 && CurrentLineCycle <= 454);

public bool InBorder =>
(CurrentLineCycle >= 50 && CurrentLineCycle <= 92) ||
(CurrentLineCycle >= 412 && CurrentLineCycle <= 454);

public int TotalCycles = 0;

Expand All @@ -121,19 +124,21 @@ public enum TvSystem {
public VicIi(TvSystem tvSystem) {
_tvSystem = tvSystem;

ScreenBufferPixels = new Color[USABLE_WIDTH_BORDER * USABLE_HEIGHT_BORDER];
ScreenBufferPixels = new Color[USABLE_HEIGHT_BORDER, USABLE_WIDTH_BORDER];
}

public int X = 0;
public int Y = 0;

public void Cycle() {

// Every cycle draws 8 pixels to the screen

//if (InBorder && CurrentLine >= 0 && CurrentLine <= 200) {
// var bgColor = Colors.FromByte((byte)(_registers[0x20] & 0b00001111));

// for (int i = 0; i < 8; i++) {
// ScreenBufferPixels[Y * 320 + X + i] = bgColor;
// ScreenBufferPixels[Y, X + i] = bgColor;
// }

// X += 8;
Expand Down Expand Up @@ -166,35 +171,43 @@ public void Cycle() {
CurrentLine = 0;

OnLastScanLine?.Invoke(this, null);

// Frame based character mode rendering
UpdateScreenBufferPixels();
}
}

TotalCycles++;
}

private void RenderBorder() {

}

private void RenderCharacterMode() {

}



public void UpdateScreenBufferPixels() {
var bgColor = Colors.FromByte((byte)(C64.Vic._registers[0x21] & 0b00001111));

var borderWidth = (USABLE_WIDTH_BORDER - USABLE_WIDTH) / 2;

for (var i = 0; i < 1000; i++) {
var petsciiCode = vicRead((ushort)(getScreenMemoryPointer() + i));
var fgColor = Colors.FromByte((byte)(C64.Memory[C64MemoryOffsets.SCREEN_COLOR_RAM + i] & 0b00001111));
//var fgColor = Colors.FromByte((byte)(vicRead((ushort)(0x0800 + i)) & 0b00001111));

var line = (i / 40);
var characterInLine = i % 40;
var indexLineOffset = (2560 * line) + (8 * characterInLine);

for (int row = 0; row <= 7; row++) {
var charRow = vicRead((ushort)(getCharacterMemoryPointer() + (petsciiCode * 8) + row));

var indexRowOffset = indexLineOffset + (USABLE_WIDTH_BORDER * row);

for (int col = 0; col <= 7; col++) {
var indexPixelOffset = indexRowOffset + col;

ScreenBufferPixels[indexPixelOffset] = charRow.IsBitSet(7 - (BitIndex)col) ? fgColor : bgColor;
ScreenBufferPixels[borderWidth + (line * 8) + row, borderWidth + (characterInLine * 8) + col] = charRow.IsBitSet(7 - (BitIndex)col) ? fgColor : bgColor;
}

}
Expand Down

0 comments on commit 41c5202

Please sign in to comment.