def run(self) -> None: e = sdl2.SDL_Event() while not self.context.quit: while sdl2.SDL_PollEvent(ctypes.byref(e)) != 0: if e.type == sdl2.SDL_QUIT: self.context.quit = True self.world.handle_event(e) self.world.handle_keyboard(sdl2.SDL_GetKeyboardState(None)) self.world.clean() self.world.process_physics() self.world.detect_collisions() self.world.animate() sdl2.SDL_SetRenderDrawColor(self.context.renderer, 0xff, 0xff, 0xff, 0xff) sdl2.SDL_RenderClear(self.context.renderer) self.world.render(self.world.frame.center, self.world.camera.global_position(), self.world.camera.frame.size) self.ui.render(self.ui.frame.center, Vector2D(), self.world.camera.originalSize) sdl2.SDL_RenderPresent(self.context.renderer) self.exit()
def update(self) -> None: pos = self.container.position keys = sdl(sdl2.SDL_GetKeyboardState(None)) if keys[sdl2.SDL_SCANCODE_SPACE] == 1: if (sdl2.SDL_GetTicks() - self.last_shot) >= self.cooldown: # Player has two turrets self.shoot(pos.x + 25, pos.y - 20) self.shoot(pos.x - 25, pos.y - 20) self.last_shot = sdl2.SDL_GetTicks()
def update(self) -> None: con = self.container keys = sdl(sdl2.SDL_GetKeyboardState(None)) if keys[sdl2.SDL_SCANCODE_LEFT] == 1: if con.position.x - self.sprite_renderer.width/2 > 0: con.position.x -= self.speed * config.delta_time elif keys[sdl2.SDL_SCANCODE_RIGHT] == 1: if con.position.x + self.sprite_renderer.width/2 < config.SCREEN_WIDTH: con.position.x += self.speed * config.delta_time
def update(self): key_status = sdl2.SDL_GetKeyboardState(None) for btn_index in range(0, self.MAX_BUTTONS): self._button_pressed[btn_index] = 0 is_down = key_status[self._button_mapping[btn_index]] if is_down and not self._button_down[btn_index]: self._button_pressed[btn_index] = True self._button_down[btn_index] = is_down
def update(self, dtime): keyStates = sdl2.SDL_GetKeyboardState(None) if keyStates[sdl2.SDL_SCANCODE_A]: self.joints[0].increaseValue(dtime) if keyStates[sdl2.SDL_SCANCODE_D]: self.joints[0].decreaseValue(dtime) if keyStates[sdl2.SDL_SCANCODE_Q]: self.joints[1].increaseValue(dtime) if keyStates[sdl2.SDL_SCANCODE_E]: self.joints[1].decreaseValue(dtime) if keyStates[sdl2.SDL_SCANCODE_Z]: self.joints[2].increaseValue(dtime) if keyStates[sdl2.SDL_SCANCODE_C]: self.joints[2].decreaseValue(dtime)
def events(self): key_state = sdl2.SDL_GetKeyboardState(None) mouse_state = sdl2.SDL_GetMouseState(ctypes.byref(self.x), ctypes.byref(self.y)) events = sdl2.ext.get_events() for event in events: if event.type == sdl2.SDL_QUIT: self.running = False return False # elif event.type == sdl2.SDL_KEYDOWN: # if key_state[sdl2.SDL_SCANCODE_UP]: # self._updatedelay(self.delay + 10) # elif key_state[sdl2.SDL_SCANCODE_DOWN]: # self._updatedelay(self.delay - 10) return True
def __init__(self): logger.debug("Starting") self.init() self.config: dict = self.get_config() self.width: int = self.config["width"] self.height: int = self.config["height"] self.max_fps: float = self.config["max_fps"] self.show_fps: bool = self.config["show_fps"] self.caption: str = self.config["caption"] self.fullscreen: bool = self.config["fullscreen"] self.time_step: float = self.config["time_step"] self.time_speed: float = self.config["time_speed"] self.time_max_iter: int = self.config["time_max_iter"] self.window: sdl2.ext.Window = None self.renderer: GfxRenderer = None self.sprite_factory: sdl2.ext.SpriteFactory = None self.font_manager: sdl2.ext.FontManager = None self.frame_num: int = 0 self.init_display() self.init_renderer() self.init_sprite_factory() self.init_font_manager() self.display_loading_screen() self.init_heavy() self.fps_counter: FPSCounter = FPSCounter() self.feeder: TimeFeeder = TimeFeeder(self.time_step, self.time_speed) self.key_state: typing.Dict[int, bool] = sdl2.SDL_GetKeyboardState(None) self.mouse_state: typing.Tuple[int, int, int] = get_mouse_state() self.running: bool = False self.scenes: typing.Dict[str, "Scene"] = {} self.scene_switch_queue = collections.deque() self.active_scene: "Scene" = self.add_exit_scene() self.context: Context = self.get_initial_context() self.logger.debug("Initializing scenes") self.init_scenes() self.fps_counter.clear() self.time_time: float = time.monotonic() self.logger.info("All systems nominal") gc.collect()
def update(self, dtime): keyStates = sdl2.SDL_GetKeyboardState(None) if keyStates[sdl2.SDL_SCANCODE_V]: self.joints[0].increaseValue(dtime) if keyStates[sdl2.SDL_SCANCODE_N]: self.joints[0].decreaseValue(dtime) if keyStates[sdl2.SDL_SCANCODE_F]: self.joints[1].increaseValue(dtime) if keyStates[sdl2.SDL_SCANCODE_H]: self.joints[1].decreaseValue(dtime) if keyStates[sdl2.SDL_SCANCODE_R]: self.joints[2].increaseValue(dtime) if keyStates[sdl2.SDL_SCANCODE_Y]: self.joints[2].decreaseValue(dtime) if keyStates[sdl2.SDL_SCANCODE_G]: self.joints[3].increaseValue(dtime) if keyStates[sdl2.SDL_SCANCODE_J]: self.joints[3].decreaseValue(dtime) if keyStates[sdl2.SDL_SCANCODE_T]: self.joints[4].increaseValue(dtime) if keyStates[sdl2.SDL_SCANCODE_U]: self.joints[4].decreaseValue(dtime)
def handle_player_movement(self, controller): # Gets current keys pressed keystate = sdl2.SDL_GetKeyboardState(None) # Reset movement each frame up = False down = False left = False right = False running = False # W, A, S, D and arrow keys # move player up, left, down, and right, respectively if keystate[sdl2.SDL_SCANCODE_W] or keystate[sdl2.SDL_SCANCODE_UP]: up = True if keystate[sdl2.SDL_SCANCODE_A] or keystate[sdl2.SDL_SCANCODE_LEFT]: left = True if keystate[sdl2.SDL_SCANCODE_S] or keystate[sdl2.SDL_SCANCODE_DOWN]: down = True if keystate[sdl2.SDL_SCANCODE_D] or keystate[sdl2.SDL_SCANCODE_RIGHT]: right = True # Shift key to run if keystate[sdl2.SDL_SCANCODE_LSHIFT]\ or keystate[sdl2.SDL_SCANCODE_RSHIFT]: running = True # E for player interact action if keystate[sdl2.SDL_SCANCODE_E]: controller.interact_player() # TAB for displaying player inventory if keystate[sdl2.SDL_SCANCODE_TAB]: controller.display_inventory() controller.move_player(up, down, left, right, running)
def is_key_pressed(self, key): "returns True if given key is pressed" key = bytes(key, encoding='utf-8') scancode = sdl2.keyboard.SDL_GetScancodeFromName(key) return sdl2.SDL_GetKeyboardState(None)[scancode]
def handle_input(self): app = self.app # get and store mouse state # (store everything in parent app object so stuff can access it easily) mx, my = ctypes.c_int(0), ctypes.c_int(0) mouse = sdl2.mouse.SDL_GetMouseState(mx, my) app.left_mouse = bool(mouse & sdl2.SDL_BUTTON(sdl2.SDL_BUTTON_LEFT)) app.middle_mouse = bool(mouse & sdl2.SDL_BUTTON(sdl2.SDL_BUTTON_MIDDLE)) app.right_mouse = bool(mouse & sdl2.SDL_BUTTON(sdl2.SDL_BUTTON_RIGHT)) mx, my = int(mx.value), int(my.value) # if mouse hasn't moved since init, disregard SDL_GetMouseState if self.mouse_has_moved: app.mouse_x, app.mouse_y = mx, my elif mx != 0 or my != 0: self.mouse_has_moved = True # relative mouse move state mdx, mdy = ctypes.c_int(0), ctypes.c_int(0) sdl2.mouse.SDL_GetRelativeMouseState(mdx, mdy) if self.mouse_has_moved: app.mouse_dx, app.mouse_dy = int(mdx.value), int(mdy.value) if app.mouse_dx != 0 or app.mouse_dy != 0: app.keyboard_editing = False # dragging a dialog? if app.left_mouse and self.ui.active_dialog in self.ui.hovered_elements: self.ui.active_dialog.update_drag(app.mouse_dx, app.mouse_dy) # get keyboard state so later we can directly query keys ks = sdl2.SDL_GetKeyboardState(None) # get modifier states self.shift_pressed, self.alt_pressed, self.ctrl_pressed = False, False, False if ks[sdl2.SDL_SCANCODE_LSHIFT] or ks[sdl2.SDL_SCANCODE_RSHIFT]: self.shift_pressed = True if ks[sdl2.SDL_SCANCODE_LALT] or ks[sdl2.SDL_SCANCODE_RALT]: self.alt_pressed = True if ks[sdl2.SDL_SCANCODE_LCTRL] or ks[sdl2.SDL_SCANCODE_RCTRL]: self.ctrl_pressed = True # macOS: treat command as interchangeable with control, is this kosher? if platform.system() == 'Darwin' and (ks[sdl2.SDL_SCANCODE_LGUI] or ks[sdl2.SDL_SCANCODE_RGUI]): self.ctrl_pressed = True if app.capslock_is_ctrl and ks[sdl2.SDL_SCANCODE_CAPSLOCK]: self.ctrl_pressed = True # pack mods into a tuple to save listing em all out repeatedly mods = self.shift_pressed, self.alt_pressed, self.ctrl_pressed # get controller state if self.gamepad: self.gamepad_left_x = sdl2.SDL_JoystickGetAxis( self.gamepad, sdl2.SDL_CONTROLLER_AXIS_LEFTX) / 32768 self.gamepad_left_y = sdl2.SDL_JoystickGetAxis( self.gamepad, sdl2.SDL_CONTROLLER_AXIS_LEFTY) / -32768 for event in sdl2.ext.get_events(): if event.type == sdl2.SDL_QUIT: app.should_quit = True elif event.type == sdl2.SDL_WINDOWEVENT: if event.window.event == sdl2.SDL_WINDOWEVENT_RESIZED: # test window we create on init to detect resolution makes # SDL think we've resized main app window on first tick! if app.updates > 0: app.resize_window(event.window.data1, event.window.data2) elif event.type == sdl2.SDL_JOYBUTTONDOWN: if not app.gw.paused and app.gw.player: app.gw.player.button_pressed(event.jbutton.button) elif event.type == sdl2.SDL_JOYBUTTONUP: if not app.gw.paused and app.gw.player: self.app.gw.player.button_unpressed(event.jbutton.button) elif event.type == sdl2.SDL_KEYDOWN: keysym = self.get_keysym(event) # if console is up, pass input to it if self.ui.console.visible: self.ui.console.handle_input(keysym, *mods) # same with dialog box elif self.ui.active_dialog and self.ui.active_dialog is self.ui.keyboard_focus_element: self.ui.active_dialog.handle_input(keysym, *mods) # bail, process no further input #sdl2.SDL_PumpEvents() #return # handle text input if text tool is active elif self.ui.selected_tool is self.ui.text_tool and self.ui.text_tool.input_active: self.ui.text_tool.handle_keyboard_input(keysym, *mods) # see if there's a function for this bind and run it else: flist = self.get_bind_functions(keysym, *mods) for f in flist: # don't run any command whose menu bar item's dimmed / not allowed (ie wrong mode) if self.is_command_function_allowed(f): f() # if game mode active, pass to world as well as any binds if self.app.game_mode: self.app.gw.handle_input(event, *mods) # for key up events, use the same binds but handle them special case # TODO: once there are enough key up events, figure out a more # elegant way than this elif event.type == sdl2.SDL_KEYUP: keysym = self.get_keysym(event) if self.app.game_mode: self.app.gw.handle_input(event, *mods) # dismiss selector popup flist = self.get_bind_functions(keysym, *mods) if not flist: pass elif self.ui.active_dialog: # keyup shouldn't have any special meaning in a dialog pass elif self.BIND_game_grab in flist: if self.app.game_mode and not self.ui.active_dialog and self.app.gw.player: self.app.gw.player.button_unpressed(0) return elif self.BIND_toggle_picker in flist: # ..but only for default hold-to-show setting if self.ui.popup_hold_to_show: self.ui.popup.hide() elif self.BIND_select_or_paint in flist: app.keyboard_editing = True if not self.ui.selected_tool is self.ui.text_tool and not self.ui.text_tool.input_active: self.app.cursor.finish_paint() # # mouse events aren't handled by bind table for now # elif event.type == sdl2.SDL_MOUSEWHEEL: ui_wheeled = self.ui.wheel_moved(event.wheel.y) if not ui_wheeled: if self.app.can_edit: if event.wheel.y > 0: # only zoom in should track towards cursor app.camera.zoom(-self.wheel_zoom_amount, towards_cursor=True) elif event.wheel.y < 0: app.camera.zoom(self.wheel_zoom_amount) else: self.app.gw.mouse_wheeled(event.wheel.y) elif event.type == sdl2.SDL_MOUSEBUTTONUP: # "consume" input if UI handled it ui_unclicked = self.ui.unclicked(event.button.button) if ui_unclicked: sdl2.SDL_PumpEvents() return if self.app.game_mode: self.app.gw.unclicked(event.button.button) # LMB up: finish paint for most tools, end select drag if event.button.button == sdl2.SDL_BUTTON_LEFT: if self.ui.selected_tool is self.ui.select_tool and self.ui.select_tool.selection_in_progress: self.ui.select_tool.finish_select( self.shift_pressed, self.ctrl_pressed) elif not self.ui.selected_tool is self.ui.text_tool and not self.ui.text_tool.input_active: app.cursor.finish_paint() elif event.type == sdl2.SDL_MOUSEBUTTONDOWN: ui_clicked = self.ui.clicked(event.button.button) # don't register edit commands if a menu is up if ui_clicked or self.ui.menu_bar.active_menu_name or self.ui.active_dialog: sdl2.SDL_PumpEvents() if self.app.game_mode: self.app.gw.last_click_on_ui = True return # pass clicks through to game world if self.app.game_mode: if not ui_clicked: self.app.gw.clicked(event.button.button) # LMB down: start text entry, start select drag, or paint elif event.button.button == sdl2.SDL_BUTTON_LEFT: if not self.ui.active_art: return elif self.ui.selected_tool is self.ui.text_tool: # text tool: only start entry if click is outside popup if not self.ui.text_tool.input_active and \ not self.ui.popup in self.ui.hovered_elements: self.ui.text_tool.start_entry() elif self.ui.selected_tool is self.ui.select_tool: # select tool: accept clicks if they're outside the popup if not self.ui.select_tool.selection_in_progress and \ (not self.ui.keyboard_focus_element or \ (self.ui.keyboard_focus_element is self.ui.popup and \ not self.ui.popup in self.ui.hovered_elements)): self.ui.select_tool.start_select() else: app.cursor.start_paint() elif event.button.button == sdl2.SDL_BUTTON_RIGHT: if self.app.ui.active_art: self.ui.quick_grab() # none of the below applies to cases where a dialog is up if self.ui.active_dialog: sdl2.SDL_PumpEvents() return # directly query keys we don't want affected by OS key repeat delay # TODO: these are hard-coded for the moment, think of a good way # to expose this functionality to the key bind system def pressing_up(ks): return ks[sdl2.SDL_SCANCODE_W] or ks[sdl2.SDL_SCANCODE_UP] or ks[ sdl2.SDL_SCANCODE_KP_8] def pressing_down(ks): return ks[sdl2.SDL_SCANCODE_S] or ks[sdl2.SDL_SCANCODE_DOWN] or ks[ sdl2.SDL_SCANCODE_KP_2] def pressing_left(ks): return ks[sdl2.SDL_SCANCODE_A] or ks[sdl2.SDL_SCANCODE_LEFT] or ks[ sdl2.SDL_SCANCODE_KP_4] def pressing_right(ks): return ks[sdl2.SDL_SCANCODE_D] or ks[ sdl2.SDL_SCANCODE_RIGHT] or ks[sdl2.SDL_SCANCODE_KP_6] # prevent camera move if: console is up, text input is active, editing # is not allowed if self.shift_pressed and not self.alt_pressed and not self.ctrl_pressed and not self.ui.console.visible and not self.ui.text_tool.input_active and self.app.can_edit and self.ui.keyboard_focus_element is None: if pressing_up(ks): app.camera.pan(0, 1, True) if pressing_down(ks): app.camera.pan(0, -1, True) if pressing_left(ks): app.camera.pan(-1, 0, True) if pressing_right(ks): app.camera.pan(1, 0, True) if ks[sdl2.SDL_SCANCODE_X]: app.camera.zoom(-self.keyboard_zoom_amount, keyboard=True, towards_cursor=True) if ks[sdl2.SDL_SCANCODE_Z]: app.camera.zoom(self.keyboard_zoom_amount, keyboard=True) if self.app.can_edit and app.middle_mouse and (app.mouse_dx != 0 or app.mouse_dy != 0): app.camera.mouse_pan(app.mouse_dx, app.mouse_dy) # game mode: arrow keys and left gamepad stick move player if self.app.game_mode and not self.ui.console.visible and not self.ui.active_dialog and self.ui.keyboard_focus_element is None: if pressing_up(ks): # shift = move selected if self.shift_pressed and self.app.can_edit: app.gw.move_selected(0, 1, 0) elif not self.ctrl_pressed and app.gw.player: app.gw.player.move(0, 1) if pressing_down(ks): if self.shift_pressed and self.app.can_edit: app.gw.move_selected(0, -1, 0) elif not self.ctrl_pressed and app.gw.player: app.gw.player.move(0, -1) if pressing_left(ks): if self.shift_pressed and self.app.can_edit: app.gw.move_selected(-1, 0, 0) elif not self.ctrl_pressed and app.gw.player: app.gw.player.move(-1, 0) if pressing_right(ks): if self.shift_pressed and self.app.can_edit: app.gw.move_selected(1, 0, 0) elif not self.ctrl_pressed and app.gw.player: app.gw.player.move(1, 0) if abs(self.gamepad_left_x) > 0.15 and app.gw.player: app.gw.player.move(self.gamepad_left_x, 0) if abs(self.gamepad_left_y) > 0.15 and app.gw.player: app.gw.player.move(0, self.gamepad_left_y) sdl2.SDL_PumpEvents()
0x0000ff00, # gmask 0x00ff0000, # gmask 0xff000000, # amask ) assert surface return surface, data wheel_surface, wheel_data = image_to_surface( hue_wheel.render_wheel(hsbk[HSBK_BR:])[::-1, :, :]) wheel_texture = sdl2.SDL_CreateTextureFromSurface(renderer, wheel_surface) assert wheel_texture sdl2.SDL_FreeSurface(wheel_surface) del wheel_data keyboard_state = sdl2.SDL_GetKeyboardState(None) def process_click_or_drag(x, y, now): xy = numpy.array([x - 16 * ZOOM, 360 * ZOOM - y], numpy.double) hs, dist = hue_wheel.xy_to_hs(xy) if dist < 60. * ZOOM: # arbitrary extra space around wheel hsbk[:HSBK_BR] = hs for light in lights.values(): light.set_color(hsbk, now) return True # redraw return False redraw_timeout = 0. event = sdl2.SDL_Event()