implement the main game loop and the game over screen

This commit is contained in:
2025-03-23 23:13:14 +03:00
parent a2b730df22
commit feba6acc36
3 changed files with 122 additions and 11 deletions

View File

@@ -1,6 +1,7 @@
using static Game.I18n;
using System.Runtime.InteropServices;
using System.Text;
using static Game.UI.TextDisplay;
namespace Game.UI {
public class TextDisplay {
@@ -18,7 +19,7 @@ namespace Game.UI {
private int currentFgColor = 0xffffff, currentBgColor = 0;
private int cursorRow = 0, cursorCol = 0;
private int windowWidth = 0, windowHeight = 0;
public int windowWidth = 0, windowHeight = 0;
public bool InitWindow() {
if (Console.IsOutputRedirected) {
@@ -40,7 +41,7 @@ namespace Game.UI {
Console.WriteLine(GetMessage(Message.ERROR_GETCONSOLEMODE));
return false;
}
inConsoleMode |= Win32Imports.ENABLE_VIRTUAL_TERMINAL_INPUT;
//inConsoleMode |= Win32Imports.ENABLE_VIRTUAL_TERMINAL_INPUT;
//inConsoleMode &= ~Win32Imports.ENABLE_LINE_INPUT;
outConsoleMode |= Win32Imports.ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if (!Win32Imports.SetConsoleMode(consoleInHandle, inConsoleMode)) {
@@ -123,7 +124,7 @@ namespace Game.UI {
WriteColored(bottomText, 0x4d4dc0, bgColor);
}
void DrawTextBox(HAlignment hAlign, VAlignment vAlign, BoxType boxType, int y, int x, string text, int borderColor, int textColor, int bgColor = -1, int preferredWidth = -1) {
public void DrawTextBox(HAlignment hAlign, VAlignment vAlign, BoxType boxType, int y, int x, string text, int borderColor, int textColor, int bgColor = -1, int preferredWidth = -1) {
int maxWidth = windowWidth;
switch (hAlign) {
case HAlignment.Left:
@@ -148,21 +149,61 @@ namespace Game.UI {
maxHeight = y - 1;
break;
}
string[] lines = WordWrap(text, (preferredWidth == -1 ? maxWidth : preferredWidth) - 4, out bool overflow);
string[] lines = WordWrap(text, (preferredWidth == -1 ? maxWidth : preferredWidth) - 2, out bool overflow);
bool reflowed = false;
if (preferredWidth != -1 && lines.Length > maxHeight - 4) {
if (preferredWidth != -1 && lines.Length > maxHeight - 2) {
reflowed = true;
lines = WordWrap(text, maxWidth - 4, out overflow);
lines = WordWrap(text, maxWidth - 2, out overflow);
}
int actualWidth = Math.Min(preferredWidth == -1 ? maxWidth : (reflowed ? maxWidth : preferredWidth), lines.Max(s => s.Length) + 4);
int actualHeight = Math.Min(maxHeight, lines.Length + 4);
int actualWidth = Math.Min(preferredWidth == -1 ? maxWidth : (reflowed ? maxWidth : preferredWidth), lines.Max(s => s.Length) + 4) + 1;
int actualHeight = Math.Min(maxHeight, lines.Length + 2);
int left = hAlign == HAlignment.Left ? x : (hAlign == HAlignment.Right ? x - actualWidth + 1 : x - (actualWidth - 1) / 2);
int top = vAlign == VAlignment.Top ? y : (vAlign == VAlignment.Bottom ? y - actualHeight + 1 : y - (actualHeight - 1) / 2);
DrawBox(boxType, top, left, actualHeight, actualWidth, borderColor, bgColor);
SetColors(textColor, -1);
for (int i = 0; i < actualHeight - 4; ++i) {
SetCursor(top + 2 + i, left + 2);
for (int i = 0; i < actualHeight - 2; ++i) {
SetCursor(top + 1 + i, left + 2);
WriteColored(lines[i], textColor, bgColor);
}
}
public void DrawText(HAlignment hAlign, VAlignment vAlign, int y, int x, string text, int textColor, int bgColor = -1) {
int maxWidth = windowWidth;
switch (hAlign) {
case HAlignment.Left:
maxWidth = windowWidth - x + 1;
break;
case HAlignment.Center:
maxWidth = Math.Min(x - 1, windowWidth - x + 1) * 2;
break;
case HAlignment.Right:
maxWidth = x - 1;
break;
}
int maxHeight = windowHeight;
switch (vAlign) {
case VAlignment.Top:
maxHeight = windowHeight - y + 1;
break;
case VAlignment.Center:
maxHeight = Math.Min(y - 1, windowHeight - y + 1) * 2;
break;
case VAlignment.Bottom:
maxHeight = y - 1;
break;
}
string[] lines = WordWrap(text, maxWidth, out bool overflow);
int actualWidth = Math.Min(maxWidth, lines.Max(s => s.Length));
int actualHeight = Math.Min(maxHeight, lines.Length);
int left = hAlign == HAlignment.Left ? x : (hAlign == HAlignment.Right ? x - actualWidth + 1 : x - (actualWidth - 1) / 2);
int top = vAlign == VAlignment.Top ? y : (vAlign == VAlignment.Bottom ? y - actualHeight + 1 : y - (actualHeight - 1) / 2);
SetColors(textColor, -1);
for (int i = 0; i < actualHeight; ++i) {
SetCursor(top + i, left);
WriteColored(lines[i], textColor, bgColor);
}
}
@@ -253,7 +294,7 @@ namespace Game.UI {
int remaining = width;
foreach (string word in text.Split(new char[] {' ', '\n'}, StringSplitOptions.RemoveEmptyEntries)) {
if (word.Length <= remaining) {
if (line.Length == 0 || line[^1] != ' ') {
if (line.Length != 0 && line[^1] != ' ') {
line.Append(' ');
remaining -= 1;
}