def handle_window_leave(window): window_id = window.windowID [window_rect] = [ window["internal_rect"] for window in App.windows if window["window_id"] == window_id ] x = ctypes.c_int() y = ctypes.c_int() sdl2.SDL_GetMouseState(ctypes.byref(x), ctypes.byref(y)) abs_x = window_rect.x + x.value x_left = abs_x < window_rect.x + window_rect.w / 2 offset_x = -3 if x_left else 3 abs_x = abs_x + offset_x abs_y = window_rect.y + y.value y_up = abs_y < window_rect.y + window_rect.h / 2 offset_y = -3 if y_up else 3 abs_y = abs_y + offset_y matching = [ window for window in App.windows if App.point_in_rect((abs_x, abs_y), window["internal_rect"]) ] if matching: window = matching[0] rect = window["internal_rect"] sdl2.SDL_WarpMouseInWindow(window["window"], abs_x - rect.x, abs_y - rect.y)
def process_inputs(self): io = imgui.get_io() s_w = ctypes.pointer(ctypes.c_int(0)) s_h = ctypes.pointer(ctypes.c_int(0)) sdl2.SDL_GetWindowSize(self.window, s_w, s_h) w = s_w.contents.value h = s_h.contents.value f_w = ctypes.pointer(ctypes.c_int(0)) f_h = ctypes.pointer(ctypes.c_int(0)) sdl2.SDL_GL_GetDrawableSize(self.window, f_w, f_h) f_w = f_w.contents.value f_h = f_h.contents.value io.display_size = w, h io.display_fb_scale = compute_fb_scale((w, h), (f_w, f_h)) current_time = sdl2.SDL_GetTicks() / 1000.0 if self._gui_time: # print("1") self.io.delta_time = current_time - self._gui_time else: # print("2") self.io.delta_time = 1. / 60. if self.io.delta_time == 0.0: self.io.delta_time = 1. / 60. # print("") # print(f"{self.io.delta_time:.32f}") # print(f"{current_time:.32f}") # if self._gui_time: # print(f"{self._gui_time:.32f}") self._gui_time = current_time mx = ctypes.pointer(ctypes.c_int(0)) my = ctypes.pointer(ctypes.c_int(0)) mouse_mask = sdl2.SDL_GetMouseState(mx, my) if sdl2.SDL_GetWindowFlags(self.window) & sdl2.SDL_WINDOW_MOUSE_FOCUS: io.mouse_pos = mx.contents.value, my.contents.value else: io.mouse_pos = -1, -1 io.mouse_down[0] = self._mouse_pressed[0] or ( mouse_mask & sdl2.SDL_BUTTON(sdl2.SDL_BUTTON_LEFT)) != 0 io.mouse_down[1] = self._mouse_pressed[1] or ( mouse_mask & sdl2.SDL_BUTTON(sdl2.SDL_BUTTON_RIGHT)) != 0 io.mouse_down[2] = self._mouse_pressed[2] or ( mouse_mask & sdl2.SDL_BUTTON(sdl2.SDL_BUTTON_MIDDLE)) != 0 self._mouse_pressed = [False, False, False] io.mouse_wheel = self._mouse_wheel self._mouse_wheel = 0
def mouse_position(self): """Current mouse position in screen coordinates. :returns: Current mouse position in screen coordinates. :rtype: tuple """ x, y = ctypes.c_int(0), ctypes.c_int(0) sdl.SDL_GetMouseState(ctypes.byref(x), ctypes.byref(y)) return (x.value, y.value)
async def mousewheel(self, event): x = ctypes.c_int() y = ctypes.c_int() sdl2.SDL_GetMouseState(ctypes.byref(x), ctypes.byref(y)) pt = self.point(x.value, y.value) found = self.find(pt, scrollable=True) if found is None or found.disabled: found = self.view await found.mousewheel(Point(-event.x, event.y)) self.needs_render = True
def handle_mouse(): global x global y global x_hold global y_hold x, y = sdl2.c_int(), sdl2.c_int() sdl2.SDL_GetMouseState(x, y) x, y = float(x.value), float(y.value) if debug: print("Mouse position: ", x, y, "Mouse down: ", is_mouse_down)
def get_mouse_position(self): """Retrieve the mouse position in screen space. Returns ------- hienoi.Vector2i The mouse position. """ position_x = ctypes.c_int() position_y = ctypes.c_int() sdl2.SDL_GetMouseState(ctypes.byref(position_x), ctypes.byref(position_y)) return Vector2i(position_x.value, position_y.value)
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 getMousePos(): """Obtain current mouse position.""" x, y = ctypes.c_int(0), ctypes.c_int(0) sdl2.SDL_GetMouseState(ctypes.byref(x), ctypes.byref(y)) return (x.value, y.value)
def getPosicaoPonteiro(): x, y = ctypes.c_int(), ctypes.c_int() sdl2.SDL_GetMouseState(ctypes.byref(x), ctypes.byref(y)) return y.value, x.value
def StartGame(self, ischeck): '''starting the actual game''' self.r.w.show() Running = flag = True mouse = False i_ = 0 scoreholder = sdl2.SDL_Rect(0, 0, 200, 200) lim = random.randint(gameinfo.BLOCKSIZE, 900) move = gameinfo.STABLE d = 0 delay_ = gameinfo.DELAY1 xco = 0 x, y = ctypes.pointer(ctypes.c_int()), ctypes.pointer(ctypes.c_int()) while Running: C = sdl2.SDL_GetTicks() if self.snake.head == None: Running = False i_ += 1 self.r.clear(gameinfo.COLOR_GRID["black"]) self.r.renderall(self.snake, self.rows, self.g) events = sdl.get_events() for e in events: if e.type == sdl2.SDL_QUIT: Running = False return False break elif e.type == sdl2.SDL_KEYDOWN and self.snake.head != None: mouse = False k = e.key.keysym.sym if k == sdl2.SDLK_SPACE: while Running: events_ext = sdl.get_events() for e in events_ext: if e.type == sdl2.SDL_KEYDOWN: Running = False break Running = True elif k == sdl2.SDLK_RIGHT: if flag: t_stamp = sdl2.SDL_GetTicks() mul = 1 move = gameinfo.RIGHT flag = False elif k == sdl2.SDLK_LEFT: if flag: t_stamp = sdl2.SDL_GetTicks() mul = 1 move = gameinfo.LEFT flag = False elif k == sdl2.SDLK_q: return False break elif e.type == sdl2.SDL_KEYUP: if (e.key.keysym.sym == sdl2.SDLK_RIGHT and move == gameinfo.RIGHT) or ( e.key.keysym.sym == sdl2.SDLK_LEFT and move == gameinfo.LEFT): flag = True move = gameinfo.STABLE elif e.type == sdl2.SDL_MOUSEMOTION: sdl2.SDL_GetMouseState(x, y) if self.snake.RADIUS < x.contents.value < gameinfo.WINDOW_WIDTH - self.snake.RADIUS and self.snake.head: if not mouse: if self.snake.head[ 0] - gameinfo.BLOCKSIZE < x.contents.value < self.snake.head[ 0] + gameinfo.BLOCKSIZE: mouse = True self.snake.lasthead = self.snake.head[0] self.snake.head[0] = x.contents.value else: self.snake.lasthead = self.snake.head[0] self.snake.head[0] = x.contents.value else: mouse = False if self.rows.row: if self.rows.row[0].pos > lim: self.rows.mountrow(self.snake) lim = random.randint(2 * gameinfo.BLOCKSIZE, 900) random.seed(random.random()) if random.randint(1, 500) == random.randint( 1, 500) and self.rows.row[ 0].pos > gameinfo.BLOCKSIZE + gameinfo.BONUSRADIUS: goody = ob.Goody(random.randint(1, 3)) self.g.append(goody) else: self.rows.mountrow(self.snake) if ischeck: if self.snake.l == 1: self.snake.collect(6) if self.snake.head: if move: if sdl2.SDL_GetTicks() - t_stamp > gameinfo.MIN_T_STAMP: t_stamp = sdl2.SDL_GetTicks() mul += 1 self.snake.lasthead = self.snake.head[0] self.snake.move(move, mul) for r in self.rows.row: if self.snake.PASS > r.pos > self.snake.head[ 1] - self.snake.RADIUS: for bl in range(gameinfo.MAX_PER_ROW): if gameinfo.BLOCKSTART_IMG[bl] < self.snake.head[ 0] < gameinfo.BLOCKEND_IMG[bl]: if r.a[bl] > 0: self.snake.head[0] = self.snake.lasthead mouse = False break break for h in self.g: if h.pos > (self.snake.head[1] + self.snake.RADIUS) or h.pos < ( self.snake.head[1] - self.snake.RADIUS): continue for c in range(len(h.co)): if self.snake.head[0] - self.snake.RADIUS < h.co[ c] < self.snake.head[0] + self.snake.RADIUS: val = h.val[c] h.co.remove(h.co[c]) h.val.remove(h.val[c]) self.snake.collect(val) h.num -= 1 if h.num == 0: self.g.remove(h) break if (self.snake.score - d) > gameinfo.SCORE_DIFF: d = self.snake.score if delay_ == gameinfo.DELAY1: self.snake.s += 1 delay_ = gameinfo.DELAY2 elif delay_ == gameinfo.DELAY2: delay_ = gameinfo.DELAY1 delay = sdl2.SDL_GetTicks() - C if delay < delay_: sdl2.SDL_Delay(delay_ - delay) self.r.present() self.r.clear(gameinfo.COLOR_GRID["black"]) self.r.present() while True: events = sdl.get_events() for e in events: if e.type == sdl2.SDL_QUIT: return False elif e.type == sdl2.SDL_KEYDOWN: if e.key.keysym.sym == sdl2.SDLK_q: return False else: return True elif e.type == sdl2.SDL_MOUSEBUTTONDOWN: return True return True
def _get_mouse_position(self): if self._id is None: return (0, 0) x, y = ctypes.c_int(), ctypes.c_int() sdl2.SDL_GetMouseState(ctypes.byref(x), ctypes.byref(y)) return x.value, y.value
def main(): nodes = None edges = None if len(sys.argv) >= 2 and Path(sys.argv[1]).exists(): map_file = Path(sys.argv[1]) with map_file.open() as roadmap_input: nodes, edges = read_map(roadmap_input) # print(nodes, edges) else: nodes = [ Vector(100.0, 100.0), Vector(200.0, 200.0), Vector(300.0, 100.0), Vector(400, 200) ] edges = [[0, 1], [1, 2], [2, 0], [1, 3], [2, 3]] wps = [] for n in nodes: wps.append(WayPoint(n.x, n.y)) for e in edges: edge = WayPointEdge(wps[e[0]], wps[e[1]]) wps[e[0]].neighbors.append(edge) wps[e[1]].neighbors.append(edge) # print(Vector.distance(wps[e[0]].pos, wps[e[1]].pos)) rp = RoutePlanner(wps) start = Vector(0.0, 0.0) goal = Vector(500.0, 200.0) route_plan = rp.plan(start, goal) sdl2.sdl2_load( ctypes.util.find_library('SDL2'), # '/usr/local/lib/libSDL2.dylib' gfx_libpath=ctypes.util.find_library('SDL2_gfx')) sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING) window = sdl2.SDL_CreateWindow(b"A* demonstration", 0, 0, WINDOW_W, WINDOW_H, sdl2.SDL_WINDOW_OPENGL) renderer = sdl2.SDL_CreateRenderer(window, -1, 0) mouse_x = ctypes.c_int() mouse_y = ctypes.c_int() fps_delay = 100 event = sdl2.SDL_Event() done = False while not done: while sdl2.SDL_PollEvent(ctypes.byref(event)) != 0: # 'type' and 'timestamp' are common members for all SDL Event structs. event_type = event.common.type # event_timestamp = event.common.timestamp # print("Event : type=0x%s, timestamp=%s" % (event_type, event_timestamp) ) if event_type == sdl2.SDL_KEYDOWN: if event.key.keysym.sym == sdl2.SDLK_ESCAPE: done = True if event.key.keysym.sym == sdl2.SDLK_SPACE: route_plan = rp.plan(start, goal) sdl2.SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF) sdl2.SDL_RenderClear(renderer) mouse_state = sdl2.SDL_GetMouseState(ctypes.byref(mouse_x), ctypes.byref(mouse_y)) if mouse_state == (1 << (sdl2.SDL_BUTTON_LEFT - 1)): prev_x = start.x prev_y = start.y start.x = float(mouse_x.value) start.y = float(mouse_y.value) if start.x != prev_x or start.y != prev_y: route_plan = rp.plan(start, goal) elif mouse_state == (1 << (sdl2.SDL_BUTTON_RIGHT - 1)): prev_x = goal.x prev_y = goal.y goal.x = float(mouse_x.value) goal.y = float(mouse_y.value) if goal.x != prev_x or goal.y != prev_y: route_plan = rp.plan(start, goal) map_renderer.render_waypoints(renderer, wps) map_renderer.render_route_plan(renderer, start, goal, route_plan) sdl2.SDL_RenderPresent(renderer) sdl2.SDL_Delay(fps_delay) sdl2.SDL_DestroyRenderer(renderer) sdl2.SDL_DestroyWindow(window) sdl2.SDL_Quit()