SDL3 Desktop: PySDL3_ttf Setup SDL3 для Desktop: Настройка PySDL3_ttf
A guide to using the SDL3_ttf font extension library with Python and PySDL3. Руководство по использованию библиотеки расширения шрифтов SDL3_ttf с Python и PySDL3.
pip install PySDL3==0.9.11b1).
Предварительное условие: Убедитесь, что PySDL3 установлен (например, pip install PySDL3==0.9.11b1).
1. Assets 1. Ресурсы
Before creating the files, download the required asset: Перед созданием файлов скачайте необходимый ресурс:
- Download the TTF file: LiberationSans-Regular.zip Скачайте TTF-файл: LiberationSans-Regular.zip
- Note: This is a free file taken from this link. Примечание. Это бесплатный файл, который был взят по ссылке.
2. Project Structure 2. Структура проекта
Create a project folder named ttf-pysdl3. You can keep your assets in an assets/fonts directory.
Создайте папку проекта с именем ttf-pysdl3. Вы можете хранить свои ресурсы в директории assets/fonts.
ttf-pysdl3/
├── assets/fonts/
│ └── LiberationSans-Regular.ttf
└── main.py
3. Python Implementation 3. Реализация на Python
PySDL3 relies on ctypes to interface with the underlying SDL3 C libraries. Use the following code in your main.py:
PySDL3 использует ctypes для взаимодействия с базовыми библиотеками SDL3 C. Используйте следующий код в вашем main.py:
import ctypes
import os
os.environ["SDL_MAIN_USE_CALLBACKS"] = "1"
os.environ["SDL_RENDER_DRIVER"] = "opengl"
import sdl3
renderer = ctypes.POINTER(sdl3.SDL_Renderer)()
window = ctypes.POINTER(sdl3.SDL_Window)()
textTexture = None
@sdl3.SDL_AppInit_func
def SDL_AppInit(appstate, argc, argv):
global textTexture
if not sdl3.SDL_Init(sdl3.SDL_INIT_VIDEO):
sdl3.SDL_Log("Couldn't initialize SDL: %s".encode() % sdl3.SDL_GetError())
return sdl3.SDL_APP_FAILURE
if not sdl3.TTF_Init():
sdl3.SDL_Log("Couldn't initialize TTF: %s".encode() % sdl3.SDL_GetError())
return sdl3.SDL_APP_FAILURE
if not sdl3.SDL_CreateWindowAndRenderer("PySDL3 TTF".encode(), 400, 200, 0, window, renderer):
return sdl3.SDL_APP_FAILURE
# Load font (Ensure the path is correct for your system)
font = sdl3.TTF_OpenFont("assets/fonts/LiberationSans-Regular.ttf".encode(), 48)
if not font:
return sdl3.SDL_APP_FAILURE
textColor = sdl3.SDL_Color(255, 255, 255, 255)
surface = sdl3.TTF_RenderText_Blended(font, "Hello PySDL3!".encode(), 0, textColor)
textTexture = sdl3.SDL_CreateTextureFromSurface(renderer, surface)
sdl3.SDL_DestroySurface(surface)
return sdl3.SDL_APP_CONTINUE
@sdl3.SDL_AppEvent_func
def SDL_AppEvent(appstate, event):
if sdl3.SDL_DEREFERENCE(event).type == sdl3.SDL_EVENT_QUIT:
return sdl3.SDL_APP_SUCCESS
return sdl3.SDL_APP_CONTINUE
@sdl3.SDL_AppIterate_func
def SDL_AppIterate(appstate):
sdl3.SDL_SetRenderDrawColor(renderer, 20, 20, 20, 255)
sdl3.SDL_RenderClear(renderer)
width, height = ctypes.c_float(), ctypes.c_float()
sdl3.SDL_GetTextureSize(textTexture, ctypes.byref(width), ctypes.byref(height))
destRect = sdl3.SDL_FRect(50.0, 75.0, width.value, height.value)
sdl3.SDL_RenderTexture(renderer, textTexture, None, destRect)
sdl3.SDL_RenderPresent(renderer)
return sdl3.SDL_APP_CONTINUE
@sdl3.SDL_AppQuit_func
def SDL_AppQuit(appstate, result):
global textTexture
sdl3.SDL_DestroyTexture(textTexture)
sdl3.TTF_Quit()
4. Running the Script 4. Запуск скрипта
Simply run the script via your terminal:
Просто запустите скрипт через терминал:
py main.py
Support My Work Поддержать проект
If these tutorials helped you, consider buying me a coffee! Если эти туториалы вам помогли, вы можете поддержать автора.
Sberbank
Direct transfer via phone number Перевод по номеру телефона
Bybit (USDT TRC20)
Support via Cryptocurrency Поддержка криптовалютой