This repository has been archived by the owner on Dec 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
149 additions
and
112 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
Empty file.