From 6ed58b4568ba736b8eb19704986a7d767eff20df Mon Sep 17 00:00:00 2001 From: dimkauzh Date: Mon, 15 Jan 2024 13:59:28 +0100 Subject: [PATCH] v4.3.0 --- pdm.lock | 28 +++++++++- pyproject.toml | 9 +++- tests/pygame_opengl.py | 111 ++++++++++++++++++---------------------- tests/sdl2_pygame.py | 113 +++++++++++++++++++++++------------------ tests/tests.py | 0 5 files changed, 149 insertions(+), 112 deletions(-) create mode 100644 tests/tests.py diff --git a/pdm.lock b/pdm.lock index 81d8c9b7..41f140eb 100644 --- a/pdm.lock +++ b/pdm.lock @@ -5,7 +5,7 @@ groups = ["default", "lint"] strategy = ["cross_platform", "inherit_metadata"] lock_version = "4.4.1" -content_hash = "sha256:2cc65d13980dee81530360d7be13370e7009521b9e4ef660b008b4c280689e18" +content_hash = "sha256:0a61b7e7d7c2bef79b009a85abf58c48f7198fb95c988d99da96e375012ef580" [[package]] name = "black" @@ -343,6 +343,32 @@ files = [ {file = "pymunk-6.6.0.tar.gz", hash = "sha256:89be7b6ba237e313c440edfb99612de59bf119e43976d5c76802907cb7a3911c"}, ] +[[package]] +name = "pysdl2" +version = "0.9.16" +summary = "Python SDL2 bindings" +groups = ["default"] +files = [ + {file = "PySDL2-0.9.16.tar.gz", hash = "sha256:1027406badbecdd30fe56e800a5a76ad7d7271a3aec0b7acf780ee26a00f2d40"}, +] + +[[package]] +name = "pysdl2-dll" +version = "2.28.5" +summary = "Pre-built SDL2 binaries for PySDL2" +groups = ["default"] +files = [ + {file = "pysdl2-dll-2.28.5.tar.gz", hash = "sha256:0015912b9ed50e5e45a6dbd85a4a3f67b7ace796cac4a055fb5424800f4a63e7"}, + {file = "pysdl2_dll-2.28.5-py2.py3-none-macosx_10_11_universal2.whl", hash = "sha256:2242a86f86e1170145d485911803a814cb2c1e17b44aca42290e71f9d839ccf7"}, + {file = "pysdl2_dll-2.28.5-py2.py3-none-macosx_10_11_x86_64.whl", hash = "sha256:e8d7688173b30a77067011ad2b7d30f4c2ed417613ed489a117a3a608f9479b3"}, + {file = "pysdl2_dll-2.28.5-py2.py3-none-manylinux2014_i686.whl", hash = "sha256:3ac04353b17832ecedcab7903e8fc1e556645417d34898bbfbb193e3c44caea9"}, + {file = "pysdl2_dll-2.28.5-py2.py3-none-manylinux2014_x86_64.whl", hash = "sha256:99e3baa4155fa1a6e544daeca81c136e6b5ea4915b2c1574fa821c819021878a"}, + {file = "pysdl2_dll-2.28.5-py2.py3-none-manylinux_2_28_aarch64.whl", hash = "sha256:83f0fe6c5ceeb1b18779577038e14e937311f523d2129cd4da7cde3e01ada92a"}, + {file = "pysdl2_dll-2.28.5-py2.py3-none-manylinux_2_28_x86_64.whl", hash = "sha256:fc08fb6a53e72c7199215535711169c95fe62ea2af9d2ae0ddede2db5de71cf4"}, + {file = "pysdl2_dll-2.28.5-py2.py3-none-win32.whl", hash = "sha256:6911eff43668ee717f9c2e06d0ed1b93d43c07a439bd0def5604beeb00dfc931"}, + {file = "pysdl2_dll-2.28.5-py2.py3-none-win_amd64.whl", hash = "sha256:5c245e18249b6c458a7b93b120afdd06487dfa4f19c8174b4d6de970f63e3142"}, +] + [[package]] name = "python-i18n" version = "0.3.9" diff --git a/pyproject.toml b/pyproject.toml index b58cbcf8..0f8899af 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -44,7 +44,13 @@ classifiers = [ "Topic :: Multimedia :: Graphics", "Topic :: Multimedia :: Video", ] -dependencies = ["pygame-ce>=2.4.0", "pygame-gui>=0.6.9", "pymunk>=6.6.0"] +dependencies = [ + "pygame-ce>=2.4.0", + "pygame-gui>=0.6.9", + "pymunk>=6.6.0", + "pysdl2>=0.9.16", + "pysdl2-dll>=2.28.5", +] includes = ["**/*.png", "**/*.ttf"] [tool.pdm] @@ -66,4 +72,5 @@ example2 = "python src/fusionengine/examples/example2.py" example3 = "python src/fusionengine/examples/example3.py" example4 = "python src/fusionengine/examples/example4.py" example5 = "python src/fusionengine/examples/example5.py" +rewrite = "python tests/rewrite.py" lint = "black ." diff --git a/tests/pygame_opengl.py b/tests/pygame_opengl.py index bf5cc66b..29a299b5 100644 --- a/tests/pygame_opengl.py +++ b/tests/pygame_opengl.py @@ -1,84 +1,73 @@ import pygame -import pygame.locals as pl +from pygame.locals import * from OpenGL.GL import * -from OpenGL.GLU import * -import imgui -from imgui.integrations.pygame import PygameRenderer - -verticies = ( - (1, -1, -1), - (1, 1, -1), - (-1, 1, -1), - (-1, -1, -1), - (1, -1, 1), - (1, 1, 1), - (-1, -1, 1), - (-1, 1, 1), -) -edges = ( - (0, 1), - (0, 3), - (0, 4), - (2, 1), - (2, 3), - (2, 7), - (6, 3), - (6, 4), - (6, 7), - (5, 1), - (5, 4), - (5, 7), -) - - -def Cube(): - glBegin(GL_LINES) - for edge in edges: - for vertex in edge: - glVertex3fv(verticies[vertex]) +from OpenGL.GLUT import * +from OpenGL.GLU import gluPerspective +from PIL import Image +from fusionengine import DEBUGIMAGE + + +def load_texture(filename): + image = Image.open(filename) + texture_data = image.tobytes("raw", "RGBA", 0, -1) + + texture_id = glGenTextures(1) + glBindTexture(GL_TEXTURE_2D, texture_id) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) + glTexImage2D( + GL_TEXTURE_2D, + 0, + GL_RGBA, + image.width, + image.height, + 0, + GL_RGBA, + GL_UNSIGNED_BYTE, + texture_data, + ) + + return texture_id + + +def draw_textured_quad(texture_id): + glBindTexture(GL_TEXTURE_2D, texture_id) + glBegin(GL_QUADS) + glTexCoord2f(0, 0) + glVertex2f(-1, -1) + + glTexCoord2f(1, 0) + glVertex2f(1, -1) + + glTexCoord2f(1, 1) + glVertex2f(1, 1) + + glTexCoord2f(0, 1) + glVertex2f(-1, 1) + glEnd() def main(): pygame.init() display = (800, 600) - pygame.display.set_mode(display, pl.DOUBLEBUF | pl.OPENGL) - + pygame.display.set_mode(display, DOUBLEBUF | OPENGL) gluPerspective(45, (display[0] / display[1]), 0.1, 50.0) - glTranslatef(0.0, 0.0, -5) - imgui.create_context() - renderer = PygameRenderer() + texture_id = load_texture(DEBUGIMAGE) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: - imgui.get_io().keys_down[pl.K_ESCAPE] = True - - imgui.new_frame() + pygame.quit() + quit() - # ImGui UI - imgui.begin("Controls") - if imgui.button("Rotate"): - glRotatef(90, 0, 1, 0) - imgui.end() - - glRotatef(1, 3, 1, 1) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) - Cube() - - imgui.render() - renderer.render(imgui.get_draw_data()) - + draw_textured_quad(texture_id) pygame.display.flip() pygame.time.wait(10) - imgui.get_io().keys_down[pl.K_ESCAPE] = True # Handle ESC key for ImGui - renderer.shutdown() - imgui.destroy_context() - pygame.quit() - if __name__ == "__main__": main() diff --git a/tests/sdl2_pygame.py b/tests/sdl2_pygame.py index f40fc195..4aa2390c 100644 --- a/tests/sdl2_pygame.py +++ b/tests/sdl2_pygame.py @@ -1,50 +1,65 @@ import pygame -import pygame._sdl2 as pgsdl2 -import sdl2 - -# Initialize SDL2 subsystems -sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO) - -# Create a SDL2 window -sdl_window = sdl2.SDL_CreateWindow( - b"Pygame with SDL2 Window", - sdl2.SDL_WINDOWPOS_UNDEFINED, - sdl2.SDL_WINDOWPOS_UNDEFINED, - 800, - 600, - sdl2.SDL_WINDOW_SHOWN, -) - -# Initialize Pygame -pygame.init() - -# Get the SDL window handle from Pygame -sdl_window_handle = pygame._sdl2.video.getSDLWindow(sdl_window) - -# Bind the Pygame display surface to the SDL2 window -display = pygame.display.set_mode( - (800, 600), flags=pygame.SDL_WINDOW_OPENGL, hwnd=sdl_window_handle -) - -# Run the game loop -running = True -while running: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False - - # Clear the display - display.fill((0, 0, 0)) - - # Draw a red rectangle using Pygame - pygame.draw.rect(display, (255, 0, 0), (100, 100, 200, 150)) - - # Update the display - pygame.display.flip() - -# Clean up -pygame.quit() - -# Destroy the SDL2 window -sdl2.SDL_DestroyWindow(sdl_window) -sdl2.SDL_Quit() +from pygame.locals import * +from sdl2 import * + + +def main(): + pygame.init() + pygame.display.set_caption("Mixed Rendering Example") + + width, height = 800, 600 + pygame_window = pygame.display.set_mode( + (width, height), pygame.DOUBLEBUF | pygame.HWSURFACE + ) + + SDL_Init(SDL_INIT_VIDEO) + sdl_window = SDL_CreateWindowFrom(pygame.display.get_wm_info()["window"]) + renderer = SDL_CreateRenderer( + sdl_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC + ) + + running = True + clock = pygame.time.Clock() + + # Create a Pygame surface for rendering + pygame_surface = pygame.Surface((width, height), pygame.SRCALPHA) + + # Create a PySDL2 texture for rendering + texture = SDL_CreateTexture( + renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, width, height + ) + + while running: + for event in pygame.event.get(): + if event.type == QUIT: + running = False + + # PySDL2 rendering + SDL_SetRenderTarget(renderer, texture) + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0) + SDL_RenderClear(renderer) + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 128) + SDL_RenderDrawLine(renderer, 0, 0, width, height) + SDL_RenderDrawLine(renderer, width, 0, 0, height) + SDL_SetRenderTarget(renderer, None) + + # Update the Pygame surface with the PySDL2 texture pixels + pixels, pitch = SDL_LockTexture(texture, None, None) + pygame_surface = pygame.image.fromstring(pixels, (width, height), "RGBA", True) + SDL_UnlockTexture(texture) + + # Draw the Pygame surface onto the Pygame window + pygame_window.blit(pygame_surface, (0, 0)) + pygame.display.flip() + + clock.tick(60) + + SDL_DestroyTexture(texture) + SDL_DestroyRenderer(renderer) + SDL_DestroyWindow(sdl_window) + SDL_Quit() + pygame.quit() + + +if __name__ == "__main__": + main() diff --git a/tests/tests.py b/tests/tests.py new file mode 100644 index 00000000..e69de29b