diff options
author | RaindropsSys <contact@minteck.org> | 2023-09-03 14:39:50 +0200 |
---|---|---|
committer | RaindropsSys <contact@minteck.org> | 2023-09-03 14:39:50 +0200 |
commit | 153d21ace9801ac665e2d7f99c967147d1214f29 (patch) | |
tree | f363dccf520fbc2c1670d2308b73294fa9ab5676 /src | |
parent | 6a20ab31ef4d9f6bb0e008b9f1b8d3fbeb6bd956 (diff) | |
download | blocks-153d21ace9801ac665e2d7f99c967147d1214f29.tar.gz blocks-153d21ace9801ac665e2d7f99c967147d1214f29.tar.bz2 blocks-153d21ace9801ac665e2d7f99c967147d1214f29.zip |
Updated 16 files and added 5 files (automated)
Diffstat (limited to 'src')
-rw-r--r-- | src/audio.py | 10 | ||||
-rw-r--r-- | src/debug.py | 66 | ||||
-rw-r--r-- | src/display.py | 16 | ||||
-rw-r--r-- | src/game.py | 25 | ||||
-rw-r--r-- | src/helper.py | 18 | ||||
-rw-r--r-- | src/loader.py | 2 | ||||
-rw-r--r-- | src/menu.py | 43 | ||||
-rw-r--r-- | src/pause.py | 8 | ||||
-rw-r--r-- | src/window.py | 11 |
9 files changed, 170 insertions, 29 deletions
diff --git a/src/audio.py b/src/audio.py index 1d8f725..169c886 100644 --- a/src/audio.py +++ b/src/audio.py @@ -1,10 +1,14 @@ import pygame import random -enable_audio = False +enable_audio = True + +try: + pygame.mixer.init() +except pygame.error: + print("WARNING: Failed to initialize DSP mixer.") + enable_audio = False -pygame.mixer.pre_init(48000, -16, 2, 2048) -pygame.mixer.init() pygame.mixer.set_num_channels(3) pygame.mixer.stop() diff --git a/src/debug.py b/src/debug.py new file mode 100644 index 0000000..b60e808 --- /dev/null +++ b/src/debug.py @@ -0,0 +1,66 @@ +import pygame +import constants + +def show_debug(clock, extended=False): + screen = pygame.display.get_surface() + + if extended: + accelerates = [] + info = pygame.display.Info() + + if info.hw == 1: + accelerates.append("HARDWARE") + if info.blit_hw == 1: + accelerates.append("HW_BLIT") + if info.blit_hw_CC == 1: + accelerates.append("HW_BLIT_CK") + if info.blit_hw_A == 1: + accelerates.append("HW_BLIT_RGBA") + if info.blit_sw == 1: + accelerates.append("BLIT") + if info.blit_sw_CC == 1: + accelerates.append("BLIT_CK") + if info.blit_sw_A == 1: + accelerates.append("BLIT_RGBA") + + width = screen.get_size()[0] + height = width / (16/9) + + if width > screen.get_size()[0] or height > screen.get_size()[1]: + height = screen.get_size()[1] + width = height * (16/9) + + offset_x = screen.get_size()[0] / 2 - width / 2 + offset_y = screen.get_size()[1] / 2 - height / 2 + scaling = ((width / 1280) + (height / 720)) / 2 + + text = [ + f"Blocks {constants.VERSION}, Pygame {pygame.version.ver}, SDL {pygame.version.SDL}", + f"{str(round(clock.get_fps()))} FPS", + f"", + f"Graphics:", + f" Driver: {pygame.display.get_driver()}, VRAM: {info.video_mem} MB, {info.bitsize}bit color", + f" Acceleration: {','.join(accelerates)}", + f" Window: {pygame.display.get_window_size()[0]}x{pygame.display.get_window_size()[1]}, Scaling factor: {round(scaling, 3)}x", + f"", + f"Audio:", + f" Mixer: {pygame.mixer.get_sdl_mixer_version()}", + f" Channels:" + ] + + for i in range(pygame.mixer.get_num_channels()): + channel = pygame.mixer.Channel(i) + txt = " " + str(i) + ": [" + str(round(channel.get_volume(), 3)) + "] " + + if channel.get_busy(): + sound = channel.get_sound() + txt += hex(id(sound)) + " (" + str(round(sound.get_length(), 3)) + ")" + else: + txt += " <idle>" + + text.append(txt) + else: + text = [f"{str(round(clock.get_fps()))} FPS"] + + for i in range(len(text)): + screen.blit(pygame.font.SysFont("Arial", 14).render(text[i], True, (255, 255, 255), (0, 0, 0)), (5, screen.get_size()[1] - 20 - ((len(text) - 1 - i) * 15))) diff --git a/src/display.py b/src/display.py index 536b2f3..ad4c48b 100644 --- a/src/display.py +++ b/src/display.py @@ -5,16 +5,12 @@ import pause import picker as picker_ui import time import save +import debug opacity = 0 -def draw(canvas, screen, need_update_world, world, mouse, loaded_chunks, zoom, offset, block_coordinates, paused, screen_blocks, selected_block, picker, save_data): - global v1 - global v2 - global last_v1 - global last_v2 - - blocks = [] # TODO: Figure out what chunks to get and how to get them +def draw(canvas, screen, need_update_world, world, mouse, loaded_chunks, zoom, offset, block_coordinates, paused, screen_blocks, selected_block, picker, save_data, focused, clock): + blocks = [] start = time.time() timings = {} @@ -204,7 +200,9 @@ def draw(canvas, screen, need_update_world, world, mouse, loaded_chunks, zoom, o timings['screen_display_final'] = (time.time() - tstart) * 1000 tstart = time.time() - pygame.display.flip() + if focused: + debug.show_debug(clock) + pygame.display.flip() timings['screen_display_flip'] = (time.time() - tstart) * 1000 if opacity < 1: @@ -218,4 +216,4 @@ def draw(canvas, screen, need_update_world, world, mouse, loaded_chunks, zoom, o print(timings) print("-----------------------") - return canvas, screen, need_update_world, world, mouse, loaded_chunks, zoom, offset, block_coordinates, paused, screen_blocks, selected_block, picker, blocks, save_data + return canvas, screen, need_update_world, world, mouse, loaded_chunks, zoom, offset, block_coordinates, paused, screen_blocks, selected_block, picker, blocks, save_data, focused, clock diff --git a/src/game.py b/src/game.py index d91e378..82330b7 100644 --- a/src/game.py +++ b/src/game.py @@ -10,8 +10,13 @@ import save import time import helper +clock = pygame.time.Clock() + def run(screen, save_data): + global clock + first = True + focused = True canvas = pygame.Surface((1280, 720)) running = True zoom = (1280.0, 720.0) @@ -40,13 +45,18 @@ def run(screen, save_data): if not paused: audio.play_music() + pygame.event.set_allowed([pygame.APPMOUSEFOCUS, pygame.APPINPUTFOCUS, pygame.WINDOWRESIZED, pygame.WINDOWEXPOSED, pygame.WINDOWENTER, pygame.WINDOWLEAVE, pygame.WINDOWFOCUSLOST, pygame.WINDOWFOCUSGAINED, pygame.WINDOWICCPROFCHANGED, pygame.WINDOWTAKEFOCUS, pygame.WINDOWMINIMIZED, pygame.MOUSEMOTION, pygame.MOUSEBUTTONDOWN, pygame.KEYDOWN, pygame.QUIT]) for event in pygame.event.get(): + if event.type == pygame.WINDOWFOCUSLOST: + focused = False + if event.type == pygame.WINDOWFOCUSGAINED: + focused = True if event.type == pygame.MOUSEMOTION: pygame.mouse.set_visible(paused or picker) - mouse = pygame.mouse.get_pos() + mouse = helper.get_real_mouse(screen) if event.type == pygame.MOUSEBUTTONDOWN: left, middle, right = pygame.mouse.get_pressed() - print(pygame.mouse.get_pos()) + print(helper.get_real_mouse(screen)) if paused: if left: @@ -92,7 +102,7 @@ def run(screen, save_data): if len(chunk) - 1 < screen_blocks[i][3] + 1 < 65: chunk.append([["air" for _ in range(16)] for _ in range(16)]) - if len(chunk) - 1 >= screen_blocks[i][3] + 1 and chunk[screen_blocks[i][3] + 1][screen_blocks[i][7]][screen_blocks[i][6]] == "air": + if len(chunk) - 1 >= screen_blocks[i][3] + 1 and chunk[screen_blocks[i][3] + 1][screen_blocks[i][7]][screen_blocks[i][6]] == "air" and chunk[screen_blocks[i][3] + 1][screen_blocks[i][6]][screen_blocks[i][7]] != selected_block: audio.play_sfx(block_list[selected_block]['sounds'][0]) chunk[screen_blocks[i][3] + 1][screen_blocks[i][6]][screen_blocks[i][7]] = selected_block need_update_world = True @@ -143,14 +153,17 @@ def run(screen, save_data): if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: if picker: + clock.tick(60) audio.play_sfx("back") picker = False else: paused = not paused if paused: + clock.tick(25) audio.play_sfx("menu") audio.pause_music() else: + clock.tick(60) audio.play_sfx("back") audio.unpause_music() pygame.mouse.set_visible(paused) @@ -183,7 +196,9 @@ def run(screen, save_data): zoom, offset = zoom_util.zoom_reset() need_update_world = True if event.type == pygame.QUIT: - pause.save_and_quit(screen, save_data, loaded_chunks) + pause.save_and_quit(screen, save_data, loaded_chunks, False) running = False - canvas, screen, need_update_world, world, mouse, loaded_chunks, zoom, offset, block_coordinates, paused, screen_blocks, selected_block, picker, blocks, save_data = display.draw(canvas, screen, need_update_world, world, mouse, loaded_chunks, zoom, offset, block_coordinates, paused, screen_blocks, selected_block, picker, save_data) + canvas, screen, need_update_world, world, mouse, loaded_chunks, zoom, offset, block_coordinates, paused, screen_blocks, selected_block, picker, blocks, save_data, focused, clock = display.draw(canvas, screen, need_update_world, world, mouse, loaded_chunks, zoom, offset, block_coordinates, paused, screen_blocks, selected_block, picker, save_data, focused, clock) + + clock.tick(60) diff --git a/src/helper.py b/src/helper.py index 46f831a..2f4e53e 100644 --- a/src/helper.py +++ b/src/helper.py @@ -70,4 +70,20 @@ def get_chunk_regions(chunks): "range": (16 * x, 16 * y, 15 + 16 * x, 15 + 16 * y) }) - return data
\ No newline at end of file + return data + +def get_real_mouse(screen): + width = screen.get_size()[0] + height = width / (16/9) + + if width > screen.get_size()[0] or height > screen.get_size()[1]: + height = screen.get_size()[1] + width = height * (16/9) + + offset_x = screen.get_size()[0] / 2 - width / 2 + offset_y = screen.get_size()[1] / 2 - height / 2 + scaling = ((width / 1280) + (height / 720)) / 2 + + pos = pygame.mouse.get_pos() + + return (int((pos[0] - offset_x) / scaling), int((pos[1] - offset_y) / scaling)) diff --git a/src/loader.py b/src/loader.py index 02da9e0..75d7701 100644 --- a/src/loader.py +++ b/src/loader.py @@ -1,7 +1,7 @@ import pygame from math import floor -texture_map = pygame.image.load('./assets/textures/intro_atlas.png') +texture_map = pygame.image.load('./assets/textures/intro_atlas.png').convert_alpha() preloaded = [] for i in range(8): diff --git a/src/menu.py b/src/menu.py index 1b877ac..49e263f 100644 --- a/src/menu.py +++ b/src/menu.py @@ -5,19 +5,28 @@ import helper import audio import save import constants +import debug + +clock = pygame.time.Clock() def show(screen): running = True + focused = True opacity = 0 audio.play_menu(True) while running: + pygame.event.set_allowed([pygame.APPMOUSEFOCUS, pygame.APPINPUTFOCUS, pygame.WINDOWRESIZED, pygame.WINDOWEXPOSED, pygame.WINDOWENTER, pygame.WINDOWLEAVE, pygame.WINDOWFOCUSLOST, pygame.WINDOWFOCUSGAINED, pygame.WINDOWICCPROFCHANGED, pygame.WINDOWTAKEFOCUS, pygame.WINDOWMINIMIZED, pygame.MOUSEBUTTONDOWN, pygame.MOUSEMOTION, pygame.QUIT]) for event in pygame.event.get(): + if event.type == pygame.WINDOWFOCUSLOST: + focused = False + if event.type == pygame.WINDOWFOCUSGAINED: + focused = True if event.type == pygame.MOUSEMOTION: pygame.mouse.set_visible(True) if event.type == pygame.MOUSEBUTTONDOWN: left, middle, right = pygame.mouse.get_pressed() - mouse = pygame.mouse.get_pos() + mouse = helper.get_real_mouse(screen) print(mouse) if left: @@ -43,7 +52,12 @@ def show(screen): while run and not done: canvas.fill("black") + pygame.event.set_allowed([pygame.APPMOUSEFOCUS, pygame.APPINPUTFOCUS, pygame.WINDOWRESIZED, pygame.WINDOWEXPOSED, pygame.WINDOWENTER, pygame.WINDOWLEAVE, pygame.WINDOWFOCUSLOST, pygame.WINDOWFOCUSGAINED, pygame.WINDOWICCPROFCHANGED, pygame.WINDOWTAKEFOCUS, pygame.WINDOWMINIMIZED, pygame.QUIT, pygame.KEYDOWN]) for event in pygame.event.get(): + if event.type == pygame.WINDOWFOCUSLOST: + focused = False + if event.type == pygame.WINDOWFOCUSGAINED: + focused = True if event.type == pygame.QUIT: run = False quit = True @@ -77,7 +91,11 @@ def show(screen): scaled_win = pygame.transform.scale(canvas, (width, height)) screen.blit(scaled_win, (screen.get_size()[0] / 2 - width / 2, screen.get_size()[1] / 2 - height / 2)) - pygame.display.flip() + if focused: + debug.show_debug(clock) + pygame.display.flip() + + clock.tick(25) if quit: pygame.quit() @@ -90,6 +108,7 @@ def show(screen): scaled_win = pygame.transform.scale(canvas, (width, height)) screen.blit(scaled_win, (screen.get_size()[0] / 2 - width / 2, screen.get_size()[1] / 2 - height / 2)) + debug.show_debug(clock) pygame.display.flip() audio.wait_for_sfx() @@ -124,12 +143,17 @@ def show(screen): while run and not done: canvas.fill("black") + pygame.event.set_allowed([pygame.APPMOUSEFOCUS, pygame.APPINPUTFOCUS, pygame.WINDOWRESIZED, pygame.WINDOWEXPOSED, pygame.WINDOWENTER, pygame.WINDOWLEAVE, pygame.WINDOWFOCUSLOST, pygame.WINDOWFOCUSGAINED, pygame.WINDOWICCPROFCHANGED, pygame.WINDOWTAKEFOCUS, pygame.WINDOWMINIMIZED, pygame.QUIT, pygame.MOUSEBUTTONDOWN, pygame.KEYDOWN]) for event in pygame.event.get(): + if event.type == pygame.WINDOWFOCUSLOST: + focused = False + if event.type == pygame.WINDOWFOCUSGAINED: + focused = True if event.type == pygame.QUIT: run = False quit = True elif event.type == pygame.MOUSEBUTTONDOWN: - cursor = pygame.mouse.get_pos() + cursor = helper.get_real_mouse(screen) for i in range(len(saves)): save_name = saves[i] @@ -161,7 +185,11 @@ def show(screen): scaled_win = pygame.transform.scale(canvas, (width, height)) screen.blit(scaled_win, (screen.get_size()[0] / 2 - width / 2, screen.get_size()[1] / 2 - height / 2)) - pygame.display.flip() + if focused: + debug.show_debug(clock) + pygame.display.flip() + + clock.tick(25) if quit: pygame.quit() elif done: @@ -173,6 +201,7 @@ def show(screen): scaled_win = pygame.transform.scale(canvas, (width, height)) screen.blit(scaled_win, (screen.get_size()[0] / 2 - width / 2, screen.get_size()[1] / 2 - height / 2)) + debug.show_debug(clock) pygame.display.flip() loaded = save.load_world(helper.get_data_path() + "/saves/" + world_name) @@ -227,10 +256,14 @@ def show(screen): screen.blit(result, (screen.get_size()[0] / 2 - width / 2, screen.get_size()[1] / 2 - height / 2)) audio.play_menu() - pygame.display.flip() + if focused: + debug.show_debug(clock) + pygame.display.flip() if opacity < 1: opacity += 1/30 if opacity >= 1: opacity = 1 + + clock.tick(25) diff --git a/src/pause.py b/src/pause.py index a68ea1f..84a7242 100644 --- a/src/pause.py +++ b/src/pause.py @@ -31,8 +31,9 @@ def click(mouse, screen, paused, keep, save_data, regions): return mouse, screen, paused, keep -def save_and_quit(screen, path, regions): - audio.play_sfx("save") +def save_and_quit(screen, path, regions, sfx=True): + if sfx: + audio.play_sfx("save") canvas = pygame.Surface((1280, 720)) canvas.fill("black") @@ -52,6 +53,7 @@ def save_and_quit(screen, path, regions): pygame.display.flip() audio.stop(1) - audio.wait_for_sfx() + if sfx: + audio.wait_for_sfx() save.save_world(path, regions)
\ No newline at end of file diff --git a/src/window.py b/src/window.py index fd35f39..4df6065 100644 --- a/src/window.py +++ b/src/window.py @@ -1,6 +1,13 @@ +import sys + import pygame def init(): pygame.display.set_caption("Blocks") - pygame.mouse.set_visible(False) - pygame.mouse.set_pos((0, 0))
\ No newline at end of file + + if sys.platform == "darwin": + pygame.display.set_icon(pygame.image.load('./assets/textures/icon-mac.png').convert_alpha()) + else: + pygame.display.set_icon(pygame.image.load('./assets/textures/icon.png').convert_alpha()) + + pygame.mouse.set_visible(True) |