PySDL3 Desktop: Setup for Python PySDL3 для Desktop: Настройка для Python
A guide to configuring and running SDL3 applications with Python. Руководство по настройке и запуску приложений SDL3 на языке Python.
1. Installation 1. Установка
Ensure you have Python installed. Install the PySDL3 library using pip:
Убедитесь, что у вас установлен Python. Установите библиотеку PySDL3 через pip:
pip install pysdl3
2. Project Structure 2. Структура проекта
Create a folder named pysdl3-app with the following hierarchy:
Создайте папку с именем pysdl3-app со следующей иерархией:
pysdl3-app/
└── main.py
3. Source Code (Callback System) 3. Исходный код (Система Callbacks)
PySDL3 relies on the SDL3 callback-based architecture. Copy this code into main.py:
PySDL3 опирается на архитектуру обратных вызовов (callbacks) SDL3. Скопируйте этот код в файл main.py:
import ctypes
import os
# Required for the SDL3 Callback-based App framework
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)()
@sdl3.SDL_AppInit_func
def SDL_AppInit(appstate, argc, argv):
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.SDL_CreateWindowAndRenderer("PySDL3 App".encode(), 500, 500, 0, window, renderer):
return sdl3.SDL_APP_FAILURE
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, 38, 43, 51, 255)
sdl3.SDL_RenderClear(renderer)
rect = sdl3.SDL_FRect(100.0, 50.0, 200.0, 60.0)
sdl3.SDL_SetRenderDrawColor(renderer, 220, 80, 80, sdl3.SDL_ALPHA_OPAQUE)
sdl3.SDL_RenderFillRect(renderer, rect)
sdl3.SDL_RenderPresent(renderer)
return sdl3.SDL_APP_CONTINUE
@sdl3.SDL_AppQuit_func
def SDL_AppQuit(appstate, result):
sdl3.SDL_DestroyRenderer(renderer)
sdl3.SDL_DestroyWindow(window)
sdl3.SDL_Quit()
4. Running the Application 4. Запуск приложения
Simply run the script using your terminal:
Просто запустите скрипт через терминал:
python main.py
or:
или:
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 Поддержка криптовалютой