def start_sdl(self): sdl2.ext.init() self.window = sdl2.ext.Window("RotaryEncoder", size=(1280, 1024), flags=sdl2.SDL_WINDOW_FULLSCREEN) self.window.show() self.renderer = sdl2.ext.Renderer(self.window) self.factory = sdl2.ext.SpriteFactory(renderer=self.renderer) sdl2.SDL_ShowCursor(0)
def __init__(self, **kwargs): super().__init__(**kwargs) if sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO) != 0: raise ValueError("Failed to initialize sdl2") # Configure OpenGL context sdl2.video.SDL_GL_SetAttribute(sdl2.SDL_GL_CONTEXT_MAJOR_VERSION, self.gl_version[0]) sdl2.video.SDL_GL_SetAttribute(sdl2.SDL_GL_CONTEXT_MINOR_VERSION, self.gl_version[1]) sdl2.video.SDL_GL_SetAttribute(sdl2.SDL_GL_CONTEXT_PROFILE_MASK, sdl2.SDL_GL_CONTEXT_PROFILE_CORE) sdl2.video.SDL_GL_SetAttribute( sdl2.SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG, 1) sdl2.video.SDL_GL_SetAttribute(sdl2.SDL_GL_DOUBLEBUFFER, 1) sdl2.video.SDL_GL_SetAttribute(sdl2.SDL_GL_DEPTH_SIZE, 24) # Display/hide mouse cursor sdl2.SDL_ShowCursor( sdl2.SDL_ENABLE if self.cursor else sdl2.SDL_DISABLE) # Configure multisampling if self.samples > 1: sdl2.video.SDL_GL_SetAttribute(sdl2.SDL_GL_MULTISAMPLEBUFFERS, 1) sdl2.video.SDL_GL_SetAttribute(sdl2.SDL_GL_MULTISAMPLESAMPLES, self.samples) # Built the window flags flags = sdl2.SDL_WINDOW_OPENGL if self.fullscreen: # Use primary desktop screen resolution flags |= sdl2.SDL_WINDOW_FULLSCREEN_DESKTOP else: if self.resizable: flags |= sdl2.SDL_WINDOW_RESIZABLE # Create the window self.window = sdl2.SDL_CreateWindow( self.title.encode(), sdl2.SDL_WINDOWPOS_UNDEFINED, sdl2.SDL_WINDOWPOS_UNDEFINED, self.width, self.height, flags, ) if not self.window: raise ValueError("Failed to create window:", sdl2.SDL_GetError()) self.context = sdl2.SDL_GL_CreateContext(self.window) sdl2.video.SDL_GL_SetSwapInterval(1 if self.vsync else 0) self.ctx = moderngl.create_context(require=self.gl_version_code) self.set_default_viewport() self.print_context_info()
def __init__(self): """ Initializes sdl2, sets up key and mouse events and creates a ``moderngl.Context`` using the context sdl2 createad. Using the sdl2 window requires sdl binaries and PySDL2. """ super().__init__() self.window_closing = False self.tmp_size_x = c_int() self.tmp_size_y = c_int() print("Using sdl2 library version:", self.get_library_version()) if sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO) != 0: raise ValueError("Failed to initialize sdl2") sdl2.video.SDL_GL_SetAttribute(sdl2.SDL_GL_CONTEXT_MAJOR_VERSION, self.gl_version.major) sdl2.video.SDL_GL_SetAttribute(sdl2.SDL_GL_CONTEXT_MINOR_VERSION, self.gl_version.minor) sdl2.video.SDL_GL_SetAttribute(sdl2.SDL_GL_CONTEXT_PROFILE_MASK, sdl2.SDL_GL_CONTEXT_PROFILE_CORE) sdl2.video.SDL_GL_SetAttribute( sdl2.SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG, 1) sdl2.video.SDL_GL_SetAttribute(sdl2.SDL_GL_DOUBLEBUFFER, 1) sdl2.video.SDL_GL_SetAttribute(sdl2.SDL_GL_DEPTH_SIZE, 24) sdl2.SDL_ShowCursor( sdl2.SDL_ENABLE if self.cursor else sdl2.SDL_DISABLE) if self.samples > 1: sdl2.video.SDL_GL_SetAttribute(sdl2.SDL_GL_MULTISAMPLEBUFFERS, 1) sdl2.video.SDL_GL_SetAttribute(sdl2.SDL_GL_MULTISAMPLESAMPLES, self.samples) flags = sdl2.SDL_WINDOW_OPENGL if self.fullscreen: flags |= sdl2.SDL_WINDOW_FULLSCREEN_DESKTOP else: if self.resizable: flags |= sdl2.SDL_WINDOW_RESIZABLE self.window = sdl2.SDL_CreateWindow(self.title.encode(), sdl2.SDL_WINDOWPOS_UNDEFINED, sdl2.SDL_WINDOWPOS_UNDEFINED, self.width, self.height, flags) if not self.window: raise ValueError("Failed to create window:", sdl2.SDL_GetError()) self.context = sdl2.SDL_GL_CreateContext(self.window) sdl2.video.SDL_GL_SetSwapInterval(1 if self.vsync else 0) self.ctx = moderngl.create_context(require=self.gl_version.code) context.WINDOW = self self.fbo = self.ctx.screen self.set_default_viewport()
def __init__(self): # Setup GUI sdl2.ext.init() self.window = sdl2.ext.Window("Sensor", size=(1920, 1080), flags=sdl2.SDL_WINDOW_FULLSCREEN) self.font_manager = sdl2.ext.FontManager( font_path='fonts/OpenSans-Regular.ttf', size=90) self.renderer = sdl2.ext.Renderer(self.window) self.factory = sdl2.ext.SpriteFactory(renderer=self.renderer) self.window.show() sdl2.SDL_ShowCursor(0)
def __init__(self, **kwargs): super().__init__(**kwargs) if sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO) != 0: raise ValueError("Failed to initialize sdl2") sdl2.video.SDL_GL_SetAttribute(sdl2.SDL_GL_CONTEXT_MAJOR_VERSION, self.gl_version[0]) sdl2.video.SDL_GL_SetAttribute(sdl2.SDL_GL_CONTEXT_MINOR_VERSION, self.gl_version[1]) sdl2.video.SDL_GL_SetAttribute(sdl2.SDL_GL_CONTEXT_PROFILE_MASK, sdl2.SDL_GL_CONTEXT_PROFILE_CORE) sdl2.video.SDL_GL_SetAttribute( sdl2.SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG, 1) sdl2.video.SDL_GL_SetAttribute(sdl2.SDL_GL_DOUBLEBUFFER, 1) sdl2.video.SDL_GL_SetAttribute(sdl2.SDL_GL_DEPTH_SIZE, 24) sdl2.SDL_ShowCursor( sdl2.SDL_ENABLE if self.cursor else sdl2.SDL_DISABLE) if self.samples > 1: sdl2.video.SDL_GL_SetAttribute(sdl2.SDL_GL_MULTISAMPLEBUFFERS, 1) sdl2.video.SDL_GL_SetAttribute(sdl2.SDL_GL_MULTISAMPLESAMPLES, self.samples) flags = sdl2.SDL_WINDOW_OPENGL if self.fullscreen: flags |= sdl2.SDL_WINDOW_FULLSCREEN_DESKTOP else: if self.resizable: flags |= sdl2.SDL_WINDOW_RESIZABLE self._window = sdl2.SDL_CreateWindow( self.title.encode(), sdl2.SDL_WINDOWPOS_UNDEFINED, sdl2.SDL_WINDOWPOS_UNDEFINED, self.width, self.height, flags, ) if not self._window: raise ValueError("Failed to create window:", sdl2.SDL_GetError()) self._context = sdl2.SDL_GL_CreateContext(self._window) sdl2.video.SDL_GL_SetSwapInterval(1 if self.vsync else 0) if self._create_mgl_context: self.init_mgl_context() self.set_default_viewport()
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
def cursor(self, value: bool): sdl2.SDL_ShowCursor(sdl2.SDL_ENABLE if value else sdl2.SDL_DISABLE) self._cursor = value
async def run_gui(window, renderer, client): running = True in_present = False buttons = 0 renderer.clear() sdl2.SDL_ShowCursor(0) factory = sdl2.ext.SpriteFactory(sdl2.ext.TEXTURE, renderer=renderer) pformat = sdl2.pixels.SDL_AllocFormat(sdl2.pixels.SDL_PIXELFORMAT_RGB888) while running: evs = client.nextEvents() need_update = len(evs) > 0 for ev in evs: if ev[0] == EV_RESIZE: pass elif ev[0] == EV_UPDATE_RECT: port, buf, pitch = ev[1] texture = factory.create_texture_sprite( renderer, (port.w, port.h), pformat=sdl2.pixels.SDL_PIXELFORMAT_RGB888, access=render.SDL_TEXTUREACCESS_STREAMING) tport = sdl2.SDL_Rect() tport.x = 0 tport.y = 0 tport.w = port.w tport.h = port.h render.SDL_UpdateTexture(texture.texture, tport, buf, pitch) renderer.copy(texture, (0, 0, port.w, port.h), (port.x, port.y, port.w, port.h)) elif ev[0] == EV_COPY_RECT: srcx, srcy, x, y, width, height = ev[1] texture = factory.from_surface(window.get_surface()) renderer.copy(texture, (srcx, srcy, width, height), (x, y, width, height)) elif ev[0] == EV_FILL_RECT: port, color = ev[1] pcolor = sdl2.pixels.SDL_MapRGBA(pformat, color.r, color.g, color.b, color.a) renderer.fill(port, pcolor) if need_update: in_present = True renderer.present() events = sdl2.ext.get_events() for event in events: if event.type == sdl2.SDL_QUIT: running = False break if in_present is False: continue if event.type == sdl2.SDL_MOUSEMOTION: x = event.motion.x y = event.motion.y client.pointerEvent(x, y, buttons) if event.type == sdl2.SDL_MOUSEBUTTONDOWN: e = event.button if e.button == 1: buttons |= 1 elif e.button == 2: buttons |= 2 elif e.button == 3: buttons |= 4 elif e.button == 4: buttons |= 8 elif e.button == 5: buttons |= 16 client.pointerEvent(e.x, e.y, buttons) if event.type == sdl2.SDL_MOUSEBUTTONUP: e = event.button if e.button == 1: buttons &= ~1 elif e.button == 2: buttons &= ~2 elif e.button == 3: buttons &= ~4 elif e.button == 4: buttons &= ~8 elif e.button == 5: buttons &= ~16 client.pointerEvent(e.x, e.y, buttons) await asyncio.sleep(0.01)
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) ObjectTurn(cube, dt * 45)
def onMouseButtonDown(self, button): self.mouseLook = not self.mouseLook sdl2.SDL_ShowCursor(not self.mouseLook) self.setMouseToCenter()