From 69313b0984532dc5257f83207e25437b7cd20396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Mon, 6 Apr 2020 15:45:05 +0200 Subject: [PATCH] Add antialiasing --- src/ui/Ui.py | 31 ++++++++++++++++++++++--------- src/ui/UiConsole.py | 5 +++-- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/ui/Ui.py b/src/ui/Ui.py index 379c90e..a8ed265 100644 --- a/src/ui/Ui.py +++ b/src/ui/Ui.py @@ -1,4 +1,5 @@ from enum import Enum +from pathlib import Path import pygame @@ -10,7 +11,7 @@ from src.ui.UiText import UiText class Ui(): - def __init__(self, rightUiWidth, leftUiWidth, screenHeight, timer, font=None): + def __init__(self, rightUiWidth, leftUiWidth, screenHeight, timer, font=None, antialias=True): self.elements = pygame.sprite.Group() self.leftUiWidth = leftUiWidth @@ -19,26 +20,38 @@ class Ui(): self.barHeight = 25 + self.antialias = antialias + + fontName = "FiraCode-Light.ttf" + fontFolder = "" + fontFile = "" + try: + fontFolder = Path("./data/fonts") + fontFile = fontFolder / fontName + except IOError: + print("Cannot load texture from " + fontFolder + ". Exiting...") + exit(1) + fontPath = str(fontFile.resolve()) if font is None: - font = pygame.font.Font(None, self.barHeight) + font = pygame.font.Font(fontPath, int(self.barHeight / 1.5)) self.font = font self.timer = timer self.timerTextView = UiText(pygame.Rect(0, 0, leftUiWidth, self.barHeight), font=self.font, text=timer.getPrettyTime(), textColor=Colors.WHITE.value, - backgroundColor=Colors.GRAY.value) + backgroundColor=Colors.GRAY.value, antialias=self.antialias) self.isDayTextView = UiText( pygame.Rect(0, self.timerTextView.rect.y + self.barHeight, leftUiWidth, self.barHeight), text="Day", - font=self.font, backgroundColor=Colors.GRAY.value, textColor=Colors.WHITE.value) + font=self.font, backgroundColor=Colors.GRAY.value, textColor=Colors.WHITE.value, antialias=self.antialias) self.healthTextView = UiText(pygame.Rect(0, 0, rightUiWidth, self.barHeight), text="Health points", - font=self.font, textColor=Colors.WHITE.value) + font=self.font, textColor=Colors.WHITE.value, antialias=self.antialias) self.healthBar = UiBar( pygame.Rect(0, self.healthTextView.rect.y + self.barHeight, rightUiWidth, self.barHeight)) self.hungerTextView = UiText( pygame.Rect(0, self.healthBar.rect.y + self.barHeight, rightUiWidth, self.barHeight), - text="Hunger", font=self.font, textColor=Colors.WHITE.value) + text="Hunger", font=self.font, textColor=Colors.WHITE.value, antialias=self.antialias) self.hungerBar = UiBar( pygame.Rect(0, self.hungerTextView.rect.y + self.barHeight, rightUiWidth, self.barHeight), initialFilledPercent=0, @@ -46,14 +59,14 @@ class Ui(): self.staminaTextView = UiText( pygame.Rect(0, self.hungerBar.rect.y + self.barHeight, rightUiWidth, self.barHeight), text="Stamina", - font=self.font, textColor=Colors.WHITE.value) + font=self.font, textColor=Colors.WHITE.value, antialias=self.antialias) self.staminaBar = UiBar( pygame.Rect(0, self.staminaTextView.rect.y + self.barHeight, rightUiWidth, self.barHeight), filledBarColor=Colors.GREEN.value) self.thirstTextView = UiText( pygame.Rect(0, self.staminaBar.rect.y + self.barHeight, rightUiWidth, self.barHeight), text="Thirst", - font=self.font, textColor=Colors.WHITE.value) + font=self.font, textColor=Colors.WHITE.value, antialias=self.antialias) self.thirstBar = UiBar( pygame.Rect(0, self.thirstTextView.rect.y + self.barHeight, rightUiWidth, self.barHeight), initialFilledPercent=0, @@ -61,7 +74,7 @@ class Ui(): self.console = UiConsole(pygame.Rect(0, self.timerTextView.rect.h + self.isDayTextView.rect.h, leftUiWidth, screenHeight - self.timerTextView.rect.h - self.isDayTextView.rect.h), - font=self.font) + font=self.font, antialias=self.antialias) def updateConsoleBasedOnPlayerStats(self, statistics: Statistics): consoleLines = ["Health: " + str(statistics.hp), "Hunger: " + str(statistics.hunger), diff --git a/src/ui/UiConsole.py b/src/ui/UiConsole.py index d1afc11..48ca682 100644 --- a/src/ui/UiConsole.py +++ b/src/ui/UiConsole.py @@ -4,7 +4,7 @@ from src.ui.UiElement import UiElement class UiConsole(UiElement): - def __init__(self, rect: pygame.Rect, bgColor=(125, 125, 125), textColor=(255, 255, 255), font=None): + def __init__(self, rect: pygame.Rect, bgColor=(125, 125, 125), textColor=(255, 255, 255), font=None, antialias=True): super().__init__(rect) self.textColor = textColor @@ -12,6 +12,7 @@ class UiConsole(UiElement): font = pygame.font.Font(None, 25) self.font = font self.bgColor = bgColor + self.antialias = antialias self.image = pygame.Surface((rect.width, rect.height)) self.image.fill(bgColor) @@ -54,7 +55,7 @@ class UiConsole(UiElement): howMuchRowIsFilled = 0 words = line.split(' ') for word in words: - wordImage = self.font.render(' ' + word, False, self.textColor) + wordImage = self.font.render(' ' + word, self.antialias, self.textColor) if howMuchRowIsFilled + wordImage.get_width() <= self.consoleWidth: row.blit(wordImage, (howMuchRowIsFilled, 0)) howMuchRowIsFilled += wordImage.get_width()