Skip to content

Instantly share code, notes, and snippets.

@aeroaks
Created October 31, 2014 10:42
Show Gist options
  • Save aeroaks/f02256395008d73704f7 to your computer and use it in GitHub Desktop.
Save aeroaks/f02256395008d73704f7 to your computer and use it in GitHub Desktop.
SDL2 Hello World
"""Simple example for using sdl2 directly.
Event prints out on the terminal
event.type - type of event
event.key... - symbolic key code for checking which key is checked
event.mouse.x/y - x and y position of the mouse within the window.
Used help from PySDL2 readthedocs page.
Window background code is commented out as it requires a BMP file. Just rename actual file/filename in the code and comment out.
"""
import os
import sys
import ctypes
import sdl2
def run():
sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO)
window = sdl2.SDL_CreateWindow(b"Hello World",
sdl2.SDL_WINDOWPOS_CENTERED,
sdl2.SDL_WINDOWPOS_CENTERED,
592, 460, sdl2.SDL_WINDOW_SHOWN)
#~ fname = os.path.join(os.path.dirname(os.path.abspath(__file__)),
#~ "resources", "programmer.bmp")
#~ image = sdl2.SDL_LoadBMP(fname.encode("utf-8"))
windowsurface = sdl2.SDL_GetWindowSurface(window)
#~ sdl2.SDL_BlitSurface(image, None, windowsurface, None)
sdl2.SDL_UpdateWindowSurface(window)
#~ sdl2.SDL_FreeSurface(image)
running = True
event = sdl2.SDL_Event()
while running:
while sdl2.SDL_PollEvent(ctypes.byref(event)) != 0:
print(event.type, event.key.keysym.sym, event.motion.x,
event.motion.y)
if event.type == sdl2.SDL_QUIT:
running = False
break
sdl2.SDL_Delay(10)
sdl2.SDL_DestroyWindow(window)
sdl2.SDL_Quit()
return 0
if __name__ == "__main__":
sys.exit(run())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment