def main(): phrase = sys.argv[2] sdl.ttf.init() font = sdl.ttf.openFont(sys.argv[1], 72) rc = font.sizeUTF8(phrase) size = rc[1:] print(phrase) print(size) sdl.init(sdl.INIT_VIDEO) window = sdl.createWindow(phrase, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, size[0] + 20, size[1] + 20, sdl.WINDOW_SHOWN) renderer = sdl.createRenderer(window, -1, 0) renderer.setRenderDrawColor(0xff, 0xff, 0xff, 0xff) renderer.renderClear() text = font.renderUTF8_Blended(phrase, sdl.Color((0, 0, 0, 0xff)).cdata[0]) texture = renderer.createTextureFromSurface(text) # None means copy the whole texture: renderer.renderCopy(texture, None, (10, 10, size[0], size[1])) renderer.renderPresent() time.sleep(10) text.freeSurface() texture.destroyTexture() renderer.destroyRenderer() window.destroyWindow()
def init(self): sdl.init(sdl.INIT_EVERYTHING) sdl.image.init(sdl.image.INIT_PNG) sdl.ttf.init() self.window = sdl.createWindow(self.window_title, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, 1280, 720, sdl.WINDOW_HIDDEN) self.renderer = self.window.createRenderer(-1, sdl.RENDERER_PRESENTVSYNC)
def main(): """Prepare the display, load images, and get our programming running.""" global IMAGES, window, renderer os.environ["SDL_VIDEO_CENTERED"] = "True" sdl.init(sdl.INIT_VIDEO) try: window = sdl.createWindow(CAPTION.encode('utf-8'), sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED, SCREEN_SIZE[0], SCREEN_SIZE[1], 0) window = sdl.Window(window) renderer = sdl.Renderer(sdl.createRenderer(window, -1, 0)) IMAGES = load_resources() Control().main_loop() finally: sdl.quit()
__author__ = 'colby' import sdl # pysdl2-cffi import atexit import weakref assert sdl.init(0) == 0 assert sdl.image.init(0) == 0 atexit.register(sdl.quit) class SDLException(Exception): def __init__(self, message, include_get_error=True): if include_get_error: message += ": %s" % sdl.getError() Exception.__init__(message) self.message = message def check(handle, err): if handle == 0: raise SDLException(err) assert handle return handle _windows = weakref.WeakValueDictionary() class Window: sdl_destroyWindow = sdl.destroyWindow # moved here to prevent shutdown issues POS_CENTERED = sdl.WINDOWPOS_CENTERED POS_UNDEFINED = sdl.WINDOWPOS_UNDEFINED
import sdl, sys sdl.init(sdl.INIT_JOYSTICK + sdl.INIT_VIDEO) count = sdl.joy_get_count() if count == 0: print "No joysticks detected." sys.exit(1) for id in range(0, count): print "Joystick " + str(id) + " : " + sdl.joy_get_name(id) def handle(event, extra): print event, extra joy = sdl.joy_open(0) while 1: event = sdl.events_wait() handle(event, event.get_type()) print "-----------------------------------------------"
def main(): event = sdl.Event() sdl.init(sdl.INIT_VIDEO) # Initialize test framework # state = SDLTest_CommonCreateState(argv, sdl.INIT_VIDEO) # if not state: # return 1 # for (i = 1; i < argc;) { # # consumed = SDLTest_CommonArg(state, i) # if consumed == 0: # sdl.Log("Usage: %s %s\n" % (argv[0], SDLTest_CommonUsage(state))) # return 1 # i += consumed # if not SDLTest_CommonInit(state): # quit(2) drawstates = [DrawState()] for i in range(len(drawstates)): drawstate = drawstates[i] drawstate.window = sdl.createWindow("Scale %d" % i, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, sdl.WINDOW_SHOWN) drawstate.renderer = sdl.createRenderer(drawstate.window, -1, 0) drawstate.sprite = LoadTexture(drawstate.renderer, "icon.bmp", True) drawstate.background = LoadTexture(drawstate.renderer, "sample.bmp", False) if not drawstate.sprite or not drawstate.background: quit(2) rc, format, access, w, h = sdl.queryTexture(drawstate.sprite) drawstate.sprite_rect.w = w drawstate.sprite_rect.h = h drawstate.scale_direction = 1 # Main render loop frames = 0 then = sdl.getTicks() done = 0 while not done: # Check for events frames += 1 while sdl.pollEvent(event): if event.type == sdl.QUIT: done = 1 for i in range(len(drawstates)): if not drawstates[i].window: continue Draw(drawstates[i]) # Print out some timing information now = sdl.getTicks() if now > then: fps = (frames * 1000) / (now - then) sys.stderr.write("%2.2f frames per second\n" % (fps)) # TODO for x in drawstates: free stuff quit(0) return 0
def run(): frames = 0 last_ticks = 0 sdl.init(sdl.INIT_VIDEO) window = sdl.createWindow( "TMX Render Example", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, 1280, # ignored for fs desktop 720, # ignored for fs desktop sdl.WINDOW_SHOWN) renderer = sdl.createRenderer(window, -1, flags=0) window = sdl.Window(window) width, height = window.getWindowSize() # Logical size: width = width // 2 height = height // 2 renderer.renderSetLogicalSize(width, height) try: map = tmxrender.TMXRender(sys.argv[1]) except: sys.stderr.write("Pass the path of a .tmx map as the first argument.") raise # Load the map's image data map.load(renderer) pos = [0, 0] k_up, k_down, k_left, k_right = (sdl.SCANCODE_UP, sdl.SCANCODE_DOWN, sdl.SCANCODE_LEFT, sdl.SCANCODE_RIGHT) class Hero(object): def __init__(self): self.x = 0 self.y = 0 hero = Hero() event = sdl.Event() running = True while running: renderer.setRenderDrawColor(0, 0, 0, 255) renderer.renderClear() while event.pollEvent(): if event.type == sdl.QUIT: running = False break elif event.type == sdl.KEYDOWN: keysym = event.key.keysym if (event.key.keysym.scancode == sdl.SCANCODE_ESCAPE or event.key.keysym.sym == sdl.K_q): running = False break # Movement with arrow keys keystate, keystate_length = sdl.getKeyboardState() hero.x += (-1 * keystate[k_left]) + (1 * keystate[k_right]) hero.y += (-1 * keystate[k_up]) + (1 * keystate[k_down]) # Draw the map under the centered hero position pos[0] = int(hero.x - width / 2) pos[1] = int(hero.y - height / 2) map.render(renderer, pos) renderer.renderPresent() # Keep track of frame rate frames += 1 ticks = sdl.getTicks() if ticks - last_ticks > 1000: last_ticks = ticks print("Frames: %s" % frames) frames = 0
import sdl import time import random import math sdl.init(sdl.INIT_VIDEO) random.seed(int(time.time())) w = 640 h = 480 cx = w / 2 cy = h / 2 stars = [] VEL = 0 POS = 1 X = 0 Y = 1 for i in range(120): # c[0] is a list containing the star's velocity in [0] x and [1] y # c[1] is a list containing the position of the star [0] x and [1] y c = ([math.sin(random.random()) * 5, math.sin(random.random()) * 5], [cx, cy]) stars.append(c) state = 0 # surface = sdl.video_set_mode(w, h, 0, sdl.SWSURFACE) surface = sdl.video_set_mode(w, h, 0, sdl.FULLSCREEN)
import sdl import time import random import math sdl.init(sdl.INIT_VIDEO) random.seed(int(time.time())) w = 640 h = 480 cx = w / 2 cy = h / 2 stars = [] VEL = 0 POS = 1 X = 0 Y = 1 for i in range(120): # c[0] is a list containing the star's velocity in [0] x and [1] y # c[1] is a list containing the position of the star [0] x and [1] y c = ([math.sin(random.random()) * 5, math.sin(random.random()) * 5], [cx, cy]) stars.append(c) state = 0 #surface = sdl.video_set_mode(w, h, 0, sdl.SWSURFACE)
def __init__(self): self.frame = 0 self.surface = point.Surface() d = self.d = self.surface.d self.data = self.surface.data self.keys_down = set() self.cursor = P(0, 0) self.cursor_move = P(0, 0) self.cursor_surface = 2 self.show_material = True self.show_bounds = True self.show_paths = True self.show_jogs = True self.show_headings = True self.show_scans = True self.show_path_meets_bound = True self.show_path_leaves_bound = True sdl.init(sdl.INIT_EVERYTHING) sdl.ttf.init() sdl.setHint("render_scale_quality", "linear") self.window = sdl.createWindow("Test", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, d.x, d.y, sdl.WINDOW_RESIZABLE) self.render = self.window.createRenderer(-1, sdl.RENDERER_ACCELERATED) # self.render = self.window.createRenderer(-1, sdl.RENDERER_TARGETTEXTURE) # self.frame_texture = self.render.createTexture(sdl.PIXELFORMAT_ARGB8888, sdl.TEXTUREACCESS_TARGET, d.x, d.y) # self.overlay_texture = self.render.createTexture(sdl.PIXELFORMAT_ARGB8888, sdl.TEXTUREACCESS_TARGET, d.x, d.y) self.animation_textures = [] for frame in range(9): texture = self.render.createTexture(sdl.PIXELFORMAT_ARGB8888, sdl.TEXTUREACCESS_STATIC, d.x, d.y) self.animation_textures.append(texture) self.animation_textures[0].setTextureBlendMode(sdl.BLENDMODE_NONE) self.animation_textures[1].setTextureBlendMode(sdl.BLENDMODE_BLEND) self.animation_textures[2].setTextureBlendMode(sdl.BLENDMODE_BLEND) self.animation_textures[3].setTextureBlendMode(sdl.BLENDMODE_BLEND) self.animation_textures[4].setTextureBlendMode(sdl.BLENDMODE_BLEND) self.animation_textures[5].setTextureBlendMode(sdl.BLENDMODE_BLEND) self.animation_textures[6].setTextureBlendMode(sdl.BLENDMODE_BLEND) self.animation_textures[7].setTextureBlendMode(sdl.BLENDMODE_BLEND) self.animation_textures[8].setTextureBlendMode(sdl.BLENDMODE_BLEND) self.render.setRenderTarget(sdl.ffi.NULL) self._io_event = sdl.Event() self.event_map = { sdl.KEYDOWN: (self.io_keydown, lambda e: e.key), sdl.KEYUP: (self.io_keyup, lambda e: e.key), sdl.MOUSEMOTION: (self.io_mousemotion, lambda e: e.motion), sdl.MOUSEBUTTONDOWN: (self.io_mousedown, lambda e: e.button), sdl.MOUSEBUTTONUP: (self.io_mouseup, lambda e: e.button), sdl.MOUSEWHEEL: (self.io_mousewheel, lambda e: e.wheel), sdl.QUIT: (self.io_quit, lambda e: e.quit), sdl.SYSWMEVENT: (self.io_syswm, lambda e: e.syswm), sdl.WINDOWEVENT: (self.io_window, lambda e: e.window), }