0

I'm trying to create a overlay on top of a game with Pyglet. This example works for pygame, you are able to interact with the game and pygame still draws on top of the window and your game. Using pyglet, you get the transparent overlay but whenever you click into your game, you lose the focus of your pyglet window (which is fine) and the drawings are not shown on top of it anymore. Is there any way to solve that?

I've tried win32gui.BringWindowToTop(window._hwnd), which didn't worked unfortunately.

#!/usr/bin/env python
import pyglet

import win32gui
import win32con
import win32api

from time import sleep
from pyglet import gl


rgb = lambda rgba: [x / 255.0 for x in rgba]


def get_game_window(hwnd_name="MyGame"):
    while True:
        try:
            hwnd = win32gui.FindWindow(None, hwnd_name)
            window_rect = win32gui.GetWindowRect(hwnd)
            x = window_rect[0] - 5
            y = window_rect[1]
            width = window_rect[2] - x
            height = window_rect[3] - y
            return x, y, width, height, hwnd
        except:
            pass
        sleep(0.5)


def create_overlay(game_window):
    window = pyglet.window.Window(
        game_window[2], game_window[3], vsync=0, style=pyglet.window.Window.WINDOW_STYLE_BORDERLESS
    )
    window.set_mouse_visible(False)
    win32gui.SetWindowLong(
        window._hwnd, win32con.GWL_EXSTYLE,
        win32gui.GetWindowLong(window._hwnd, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED
    )
    win32gui.SetLayeredWindowAttributes(window._hwnd, win32api.RGB(255, 0, 128), 0, win32con.LWA_COLORKEY)
    gl.glClearColor(*rgb((255, 0, 128, 0)))
    return window


def update(_, window, label):
    gl.glClearColor(*rgb((255, 0, 128, 0)))
    window.clear()
    label.draw()


def main():
    window = create_overlay(get_game_window())
    label = pyglet.text.Label('Hello, Overlay!',
                              font_name='Arial',
                              font_size=15,
                              x=window.width / 2,
                              y=window.height / 2,
                              anchor_x='center',
                              anchor_y='center')
    pyglet.clock.schedule_interval(update, .005, window=window, label=label)
    pyglet.app.run()


if __name__ == "__main__":
    main()
Sansch
  • 61
  • 1
  • 5
  • *"drawings are not shown on top of it anymore. Is there any way to solve that?"* - Probably not. Never ever mix different windowing systems. Maybe it works today, that's luck, but it may break down and fail with the next release of one of the modules. Implement your application with Pyglet or with PyGame, but don't mix it. The behavior of "mixed" applications is random it is not specified yet and it will never be in the future. – Rabbid76 May 01 '20 at 16:16
  • Also, this code doesn\t work for me as there is no "MyGame" window. – Torxed May 02 '20 at 09:42

0 Answers0