def post_event(self, event, *args): ev = SDL.SDL_Event() ev.type = SDL.SDL_USEREVENT ev.user.code = self.known_events[event] argcopy = tuple(args) argid = self.event_data_counter self.event_data_counter += 1 data1 = ctypes.cast(argid, ctypes.c_void_p) self.event_data[argid] = argcopy ev.user.data1 = data1 SDL.SDL_PushEvent(SDL.pointer(ev))
def blit_surface(self, surface, x, y, anchor_x='left', anchor_y='bottom'): """Blit the entire image to the window with upper left corner at the position specified. """ w = surface.contents.w h = surface.contents.h y = self.height - y if anchor_y == 'bottom': y -= h elif anchor_y == 'center': y -= h // 2 if anchor_x == 'center': x -= w // 2 elif anchor_x == 'right': x -= w destrect = SDL.SDL_Rect(x, y, 0, 0) SDL.SDL_UpperBlit(surface, None, self.screen, SDL.pointer(destrect))
def main_loop(): ev = SDL.SDL_Event() oldtime = SDL.SDL_GetTicks() maxfps = 100 global quit while not quit: while SDL.SDL_PollEvent(SDL.pointer(ev)) == 1: handle_event(ev) newtime = SDL.SDL_GetTicks() dt = newtime - oldtime oldtime = newtime waittime = 1000 / maxfps - dt if waittime > 0: SDL.SDL_Delay(waittime)
def clear_rect(self, x, y, w, h): destrect = SDL.SDL_Rect(x, self.height - y - h, w, h) print "clearing %s" % str((x, self.height - y - h, w, h)) SDL.SDL_FillRect(self.screen, SDL.pointer(destrect), 0)
def clip(self, x, y, w, h): destrect = SDL.SDL_Rect(x, self.height - y - h, w, h) SDL.SDL_SetClipRect(self.screen, SDL.pointer(destrect))
def main_loop(): ev = SDL.SDL_Event() oldtime = SDL.SDL_GetTicks() global screen image = SDL.IMG_Load('example2.png') if not image: print "failed to load image" return maxfps = 100 global quit while not quit: SDL.SDL_BlitSurface(image, None, screen, None) SDL.SDL_Flip( screen ) while SDL.SDL_PollEvent(SDL.pointer(ev)) == 1: handle_event(ev) newtime = SDL.SDL_GetTicks() dt = newtime - oldtime oldtime = newtime waittime = 1000 / maxfps - dt if waittime > 0: SDL.SDL_Delay(waittime)
def event_loop(self): self.running = True ev = SDL.SDL_Event() while self.running: self.on_draw() self.flip() while SDL.SDL_WaitEvent(SDL.pointer(ev)) == 1: if ev.type == SDL.SDL_KEYDOWN: self.controller.process_key_press(ev.key.keysym.sym, ev.key.keysym.mod) break elif ev.type == SDL.SDL_MOUSEMOTION: pass elif ev.type == SDL.SDL_QUIT: self.quit() elif ev.type == SDL.SDL_USEREVENT: argid = int(ev.user.data1) user_data = self.event_data.pop(argid) # print "User event! code=%d data1=%s user_data=%s" % (ev.user.code, ev.user.data1, str(user_data)) func_name = self.event_code_to_callback[ev.user.code] callback = getattr(self, func_name) retval = callback(*user_data) if retval == "force redraw": break UpdateManager.process_tasks()