Help me, please, to draw a text #20
Answered
by
Aermoss
8Observer8
asked this question in
Q&A
-
Hello, I try to draw a text like this: But it draws nothing: I initialized the TTF library: # Initialize the TTF library
if not sdl3.TTF_Init():
sdl3.SDL_Log("Couldn't initialize TTF: %s".encode() % sdl3.SDL_GetError())
return sdl3.SDL_APP_FAILURE I loaded a texture: font = sdl3.TTF_OpenFont("C:/Windows/Fonts/arial.ttf".encode(), 28)
if not font:
sdl3.SDL_Log("Error: %s".encode() % sdl3.SDL_GetError())
return sdl3.SDL_APP_FAILURE
textColor = sdl3.SDL_Color(10, 10, 10)
surface = sdl3.TTF_RenderText_Blended(font, "Hello World!".encode(), 12, textColor)
textTexture = sdl3.SDL_CreateTextureFromSurface(renderer, surface)
sdl3.SDL_DestroySurface(surface) I try to draw a texture: rect = sdl3.SDL_FRect()
rect.x = 20
rect.y = 20
sdl3.SDL_GetTextureSize(textTexture, ctypes.c_float(rect.w), ctypes.c_float(rect.h))
sdl3.SDL_RenderTexture(renderer, textTexture, None, rect) I tried to use sdl3.SDL_RenderTexture(renderer, textTexture, None, rect)
print(sdl3.SDL_GetError()) 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
# Initialize the TTF library
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("Draw a text using PySDL3!".encode(), 350, 350, 0, window, renderer):
sdl3.SDL_Log("Couldn't create window/renderer: %s".encode() % sdl3.SDL_GetError())
return sdl3.SDL_APP_FAILURE
sdl3.SDL_SetRenderVSync(renderer, 1) # Turn on vertical sync
font = sdl3.TTF_OpenFont("C:/Windows/Fonts/arial.ttf".encode(), 28)
if not font:
sdl3.SDL_Log("Error: %s".encode() % sdl3.SDL_GetError())
return sdl3.SDL_APP_FAILURE
textColor = sdl3.SDL_Color(10, 10, 10)
surface = sdl3.TTF_RenderText_Blended(font, "Hello World!".encode(), 12, 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, 150, 200, 170, sdl3.SDL_ALPHA_OPAQUE)
sdl3.SDL_RenderClear(renderer)
rect = sdl3.SDL_FRect()
rect.x = 20
rect.y = 20
sdl3.SDL_GetTextureSize(textTexture, ctypes.c_float(rect.w), ctypes.c_float(rect.h))
sdl3.SDL_RenderTexture(renderer, textTexture, None, rect)
print(sdl3.SDL_GetError())
sdl3.SDL_RenderPresent(renderer)
return sdl3.SDL_APP_CONTINUE
@sdl3.SDL_AppQuit_func
def SDL_AppQuit(appstate, result):
global textTexture
# SDL will clean up the window/renderer for us
sdl3.SDL_DestroyTexture(textTexture) |
Beta Was this translation helpful? Give feedback.
Answered by
Aermoss
Mar 21, 2025
Replies: 1 comment 1 reply
-
The width, height = ctypes.c_float(), ctypes.c_float()
sdl3.SDL_GetTextureSize(textTexture, ctypes.byref(width), ctypes.byref(height))
rect = sdl3.SDL_FRect(20.0, 20.0, width.value, height.value)
sdl3.SDL_RenderTexture(renderer, textTexture, None, rect) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
8Observer8
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
w
andh
arguments of SDL_GetTextureSize function are ctypes float pointers so you need to usectypes.byref
instead ofctypes.c_float
and you need to create new variables to pass as pointers because thew
andh
members of SDL_FRect struct are python floats.