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 updateLogic(dt): deltax = (mx - mouseX) / 3.0 deltay = (my - mouseY) / 3.0 sdl2.SDL_WarpMouseInWindow(window, mx, my) ObjectRotate(camera, deltay, 0, 0) ObjectRotate(camPos, 0, -deltax, 0) if keyPressed[KEY_W]: ObjectMove(camPos, -10 * dt) if keyPressed[KEY_A]: ObjectStrafe(camPos, 10 * dt) if keyPressed[KEY_D]: ObjectStrafe(camPos, -10 * dt) if keyPressed[KEY_S]: ObjectMove(camPos, 10 * dt) ObjectTurn(cube, dt * 45) framerate = int(ViewerGetFramesPerSecond(viewer)) HUDTextSetText(text, str(framerate)) Update(dt)
def __init__(self, w, h, title): self.windowWidth = w self.windowHeight = h self.windowTitle = title self.halfWindowWidth = self.windowWidth / 2 self.halfWindowHeight = self.windowHeight / 2 sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO) self.window = sdl2.SDL_CreateWindow(self.windowTitle, sdl2.SDL_WINDOWPOS_CENTERED, sdl2.SDL_WINDOWPOS_CENTERED, self.windowWidth, self.windowHeight, sdl2.SDL_WINDOW_SHOWN) sdl2.SDL_WarpMouseInWindow(self.window, self.halfWindowWidth, self.halfWindowHeight) sdl2.SDL_ShowCursor(0) self.start()
def run(): # Create the environment, in which our particles will exist. world = sdl2.ext.World() # Set up the globally available information about the current mouse # position. We use that information to determine the emitter # location for new particles. world.mousex = 400 world.mousey = 300 # Create the particle engine. It is just a simple System that uses # callback functions to update a set of components. engine = sdl2.ext.particles.ParticleEngine() # Bind the callback functions to the particle engine. The engine # does the following on processing: # 1) reduce the life time of each particle by one # 2) create a list of particles, which's life time is 0 or below. # 3) call createfunc() with the world passed to process() and # the list of dead particles # 4) call updatefunc() with the world passed to process() and the # set of particles, which still are alive. # 5) call deletefunc() with the world passed to process() and the # list of dead particles. deletefunc() is respsonible for # removing the dead particles from the world. engine.createfunc = createparticles engine.updatefunc = updateparticles engine.deletefunc = deleteparticles world.add_system(engine) # We create all particles at once before starting the processing. # We also could create them in chunks to have a visually more # appealing effect, but let's keep it simple. createparticles(world, None, 300) # Initialize the video subsystem, create a window and make it visible. sdl2.ext.init() window = sdl2.ext.Window("Particles", size=(800, 600)) window.show() # Create a hardware-accelerated sprite factory. The sprite factory requires # a rendering context, which enables it to create the underlying textures # that serve as the visual parts for the sprites. renderer = sdl2.ext.Renderer(window) factory = sdl2.ext.SpriteFactory(sdl2.ext.TEXTURE, renderer=renderer) # Create a set of images to be used as particles on rendering. The # images are used by the ParticleRenderer created below. images = (factory.from_image(RESOURCES.get_path("circle.png")), factory.from_image(RESOURCES.get_path("square.png")), factory.from_image(RESOURCES.get_path("star.png")) ) # Center the mouse on the window. We use the SDL2 functions directly # here. Since the SDL2 functions do not know anything about the # sdl2.ext.Window class, we have to pass the window's SDL_Window to it. sdl2.SDL_WarpMouseInWindow(window.window, world.mousex, world.mousey) # Hide the mouse cursor, so it does not show up - just show the # particles. sdl2.SDL_ShowCursor(0) # Create the rendering system for the particles. This is somewhat # similar to the SoftSpriteRenderer, but since we only operate with # hundreds of particles (and not sprites with all their overhead), # we need an own rendering system. particlerenderer = ParticleRenderSystem(renderer, images) world.add_system(particlerenderer) # The almighty event loop. You already know several parts of it. running = True while running: for event in sdl2.ext.get_events(): if event.type == sdl2.SDL_QUIT: running = False break if event.type == sdl2.SDL_MOUSEMOTION: # Take care of the mouse motions here. Every time the # mouse is moved, we will make that information globally # available to our application environment by updating # the world attributes created earlier. world.mousex = event.motion.x world.mousey = event.motion.y # We updated the mouse coordinates once, ditch all the # other ones. Since world.process() might take several # milliseconds, new motion events can occur on the event # queue (10ths to 100ths!), and we do not want to handle # each of them. For this example, it is enough to handle # one per update cycle. sdl2.SDL_FlushEvent(sdl2.SDL_MOUSEMOTION) break world.process() sdl2.SDL_Delay(1) sdl2.ext.quit() return 0
CameraSetViewDepth(camera, 500) CameraSetFocal(camera, 80) ViewerSetCamera(viewer, camera) font = WindowsBitmapfontCreate('Arial', 20, 0, 128) text = HUDTextCreate(font, 'Xtreme3D 3.6', front) HUDTextSetColor(text, c_white, 0.5) ObjectSetPosition(text, 20, 20, 0) keyPressed = [False] * 512 mouseX = 0 mouseY = 0 mx = windowWidth / 2 my = windowHeight / 2 sdl2.SDL_WarpMouseInWindow(window, mx, my) sdl2.SDL_ShowCursor(0) def updateLogic(dt): deltax = (mx - mouseX) / 3.0 deltay = (my - mouseY) / 3.0 sdl2.SDL_WarpMouseInWindow(window, mx, my) ObjectRotate(camera, deltay, 0, 0) ObjectRotate(camPos, 0, -deltax, 0) if keyPressed[KEY_W]: ObjectMove(camPos, -10 * dt) if keyPressed[KEY_A]: ObjectStrafe(camPos, 10 * dt) if keyPressed[KEY_D]: ObjectStrafe(camPos, -10 * dt) if keyPressed[KEY_S]: ObjectMove(camPos, 10 * dt)
def setMouse(self, x, y): sdl2.SDL_WarpMouseInWindow(self.window, x, y)