2020-01-18 02:28:44 +08:00
|
|
|
import sys
|
2023-08-25 07:30:54 +08:00
|
|
|
import pygame
|
|
|
|
import cv2
|
2020-01-18 02:28:44 +08:00
|
|
|
|
2022-05-03 07:48:04 +08:00
|
|
|
class Window:
|
2020-10-21 07:07:05 +08:00
|
|
|
def __init__(self, w, h, caption="window", double=False, halve=False):
|
2020-01-18 02:28:44 +08:00
|
|
|
self.w = w
|
|
|
|
self.h = h
|
2020-08-01 04:11:59 +08:00
|
|
|
pygame.display.init()
|
2020-01-18 02:28:44 +08:00
|
|
|
pygame.display.set_caption(caption)
|
|
|
|
self.double = double
|
2020-10-21 07:07:05 +08:00
|
|
|
self.halve = halve
|
2020-01-18 02:28:44 +08:00
|
|
|
if self.double:
|
2022-02-18 04:14:27 +08:00
|
|
|
self.rw, self.rh = w*2, h*2
|
2020-10-21 07:07:05 +08:00
|
|
|
elif self.halve:
|
2022-02-18 04:14:27 +08:00
|
|
|
self.rw, self.rh = w//2, h//2
|
2020-01-18 02:28:44 +08:00
|
|
|
else:
|
2022-02-18 04:14:27 +08:00
|
|
|
self.rw, self.rh = w, h
|
|
|
|
self.screen = pygame.display.set_mode((self.rw, self.rh))
|
|
|
|
pygame.display.flip()
|
|
|
|
|
|
|
|
# hack for xmonad, it shrinks the window by 6 pixels after the display.flip
|
|
|
|
if self.screen.get_width() != self.rw:
|
|
|
|
self.screen = pygame.display.set_mode((self.rw+(self.rw-self.screen.get_width()), self.rh+(self.rh-self.screen.get_height())))
|
|
|
|
pygame.display.flip()
|
2020-01-18 02:28:44 +08:00
|
|
|
|
|
|
|
def draw(self, out):
|
2020-05-19 05:37:47 +08:00
|
|
|
pygame.event.pump()
|
2020-01-18 02:28:44 +08:00
|
|
|
if self.double:
|
2020-05-19 05:37:47 +08:00
|
|
|
out2 = cv2.resize(out, (self.w*2, self.h*2))
|
2020-06-01 03:37:52 +08:00
|
|
|
pygame.surfarray.blit_array(self.screen, out2.swapaxes(0, 1))
|
2020-10-21 07:07:05 +08:00
|
|
|
elif self.halve:
|
|
|
|
out2 = cv2.resize(out, (self.w//2, self.h//2))
|
|
|
|
pygame.surfarray.blit_array(self.screen, out2.swapaxes(0, 1))
|
2020-01-18 02:28:44 +08:00
|
|
|
else:
|
2020-06-01 03:37:52 +08:00
|
|
|
pygame.surfarray.blit_array(self.screen, out.swapaxes(0, 1))
|
2020-01-18 02:28:44 +08:00
|
|
|
pygame.display.flip()
|
2020-05-19 05:37:47 +08:00
|
|
|
|
2020-01-18 02:28:44 +08:00
|
|
|
def getkey(self):
|
|
|
|
while 1:
|
|
|
|
event = pygame.event.wait()
|
2020-05-28 11:00:14 +08:00
|
|
|
if event.type == pygame.QUIT:
|
2020-01-18 02:28:44 +08:00
|
|
|
pygame.quit()
|
|
|
|
sys.exit()
|
2020-05-28 11:00:14 +08:00
|
|
|
if event.type == pygame.KEYDOWN:
|
2020-01-18 02:28:44 +08:00
|
|
|
return event.key
|
|
|
|
|
|
|
|
def getclick(self):
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
|
|
|
mx, my = pygame.mouse.get_pos()
|
|
|
|
return mx, my
|
2020-05-01 06:31:01 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import numpy as np
|
2020-05-19 05:37:47 +08:00
|
|
|
win = Window(200, 200, double=True)
|
2022-05-03 07:48:04 +08:00
|
|
|
img: np.ndarray = np.zeros((200, 200, 3), np.uint8)
|
2020-05-01 06:31:01 +08:00
|
|
|
while 1:
|
|
|
|
print("draw")
|
|
|
|
img += 1
|
|
|
|
win.draw(img)
|