I cannot draw a PNG-file #19
Answered
by
Aermoss
8Observer8
asked this question in
Q&A
-
Hello, I try to draw a PNG-file but it draws nothing: # pip install PySDL3
# py main.py
import ctypes
import os
import time
os.environ["SDL_MAIN_USE_CALLBACKS"] = "1"
import sdl3
renderer = ctypes.POINTER(sdl3.SDL_Renderer)()
window = ctypes.POINTER(sdl3.SDL_Window)()
spriteTexture = None
@sdl3.SDL_AppInit_func
def SDL_AppInit(appstate, argc, argv):
global spriteTexture
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("Sprite animation, PySDL3".encode(), 380, 380, 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
texturePath = "./assets/sprites/warrior/idle.png".encode()
spriteTexture = sdl3.IMG_LoadTexture(renderer, texturePath)
if not spriteTexture:
sdl3.SDL_Log("Error: %s".encode() % sdl3.SDL_GetError())
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):
global frames
global frameIndex
global spriteTexture
sdl3.SDL_SetRenderDrawColor(renderer, 33, 33, 33, sdl3.SDL_ALPHA_OPAQUE)
sdl3.SDL_RenderClear(renderer) # Start with a blank canvas
srcRect = sdl3.SDL_FRect(0, 0, 96, 96)
destRect = sdl3.SDL_FRect(100, 100, 96, 96)
sdl3.SDL_RenderTexture(renderer, spriteTexture, srcRect, destRect)
sdl3.SDL_RenderPresent(renderer)
return sdl3.SDL_APP_CONTINUE
@sdl3.SDL_AppQuit_func
def SDL_AppQuit(appstate, result):
global spriteTexture
sdl3.SDL_DestroyTexture(spriteTexture) When I add a colored rectangle it draws a texture with a white color: srcRect = sdl3.SDL_FRect(0, 0, 96, 96)
destRect = sdl3.SDL_FRect(100, 100, 96, 96)
sdl3.SDL_RenderTexture(renderer, spriteTexture, srcRect, destRect)
sdl3.SDL_SetRenderDrawColor(renderer, 100, 255, 100, sdl3.SDL_ALPHA_OPAQUE)
rect = sdl3.SDL_FRect(50, 200, 280, 30)
sdl3.SDL_RenderFillRect(renderer, rect)
sdl3.SDL_RenderPresent(renderer)
return sdl3.SDL_APP_CONTINUE I call sdl3.SDL_RenderTexture(renderer, spriteTexture, srcRect, destRect)
print(sdl3.SDL_GetError()) It prints: |
Beta Was this translation helpful? Give feedback.
Answered by
Aermoss
Mar 21, 2025
Replies: 1 comment 1 reply
-
Try using an another render driver by setting 'SDL_RENDER_DRIVER' envrionment variable to 'opengl', 'vulkan', 'software', etc. import os
os.environ["SDL_RENDER_DRIVER"] = "opengl" |
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
Try using an another render driver by setting 'SDL_RENDER_DRIVER' envrionment variable to 'opengl', 'vulkan', 'software', etc.