From d2d2f2fdc16063baff200c6902ea98c5c0ac11f0 Mon Sep 17 00:00:00 2001 From: Faraphel Date: Sun, 19 Feb 2023 12:32:06 +0100 Subject: [PATCH] drawing is now done with batch (speeding up the drawing by 50% to 300%) --- NOTE.md | 1 - main.pyw | 3 +- source/gui/scene/Game.py | 67 ++++++++++++++++++++++++---------- source/gui/scene/MainMenu.py | 37 ++++++++++++++----- source/gui/scene/RoomCreate.py | 25 ++++++++++--- source/gui/scene/RoomJoin.py | 32 +++++++++++----- source/gui/scene/Settings.py | 33 ++++++++++++++--- 7 files changed, 146 insertions(+), 52 deletions(-) diff --git a/NOTE.md b/NOTE.md index c6190d4..341b491 100644 --- a/NOTE.md +++ b/NOTE.md @@ -1,6 +1,5 @@ A faire : - Faire une scène incluant par défaut les boutons "Retour" -- Utiliser des batchs - Ajouter un moyen d'annuler les évenements de se propager aux widgets en dessous - Police d'écriture diff --git a/main.pyw b/main.pyw index 8f2050c..9fb57af 100644 --- a/main.pyw +++ b/main.pyw @@ -5,7 +5,8 @@ from source.gui.window import GameWindow # Create a new window -window = GameWindow(resizable=True, vsync=True, caption="Bataille Navale") +window = GameWindow(resizable=True, vsync=False, caption="Bataille Navale") +window.set_minimum_size(720, 480) window.add_scene(MainMenu) # Start the event loop diff --git a/source/gui/scene/Game.py b/source/gui/scene/Game.py index 1022148..f2a9d31 100644 --- a/source/gui/scene/Game.py +++ b/source/gui/scene/Game.py @@ -1,5 +1,7 @@ from typing import TYPE_CHECKING +import pyglet + from source.gui.scene.abc import Scene from source.gui import widget, texture from source import core @@ -13,6 +15,13 @@ class Game(Scene): def __init__(self, window: "Window", **kwargs): super().__init__(window, **kwargs) + self.batch_label = pyglet.graphics.Batch() + self.batch_button_background = pyglet.graphics.Batch() + self.batch_input_background = pyglet.graphics.Batch() + self.batch_grid_background = pyglet.graphics.Batch() + self.batch_grid_line = pyglet.graphics.Batch() + self.batch_grid_cursor = pyglet.graphics.Batch() + self.background = self.add_widget( widget.Image, @@ -28,6 +37,10 @@ class Game(Scene): style=texture.Grid.Style1, rows=8, columns=8, + + background_batch=self.batch_grid_background, + line_batch=self.batch_grid_line, + cursor_batch=self.batch_grid_cursor, ) self.grid_enemy = self.add_widget( @@ -37,6 +50,10 @@ class Game(Scene): style=texture.Grid.Style1, rows=8, columns=8, + + background_batch=self.batch_grid_background, + line_batch=self.batch_grid_line, + cursor_batch=self.batch_grid_cursor, ) self.name_ally = self.add_widget( @@ -47,6 +64,8 @@ class Game(Scene): text="Raphael", font_size=20, anchor_x="center", anchor_y="center", + + batch=self.batch_label, ) self.name_enemy = self.add_widget( @@ -57,6 +76,8 @@ class Game(Scene): text="Leo", font_size=20, anchor_x="center", anchor_y="center", + + batch=self.batch_label, ) self.score_ally = self.add_widget( @@ -66,7 +87,9 @@ class Game(Scene): text="7", font_size=25, - anchor_x="center", anchor_y="center" + anchor_x="center", anchor_y="center", + + batch=self.batch_label, ) self.score_enemy = self.add_widget( @@ -76,7 +99,9 @@ class Game(Scene): text="5", font_size=25, - anchor_x="center", anchor_y="center" + anchor_x="center", anchor_y="center", + + batch=self.batch_label, ) self.chat_log = self.add_widget( @@ -86,7 +111,9 @@ class Game(Scene): text="FARAPHEL - HELLO BILLY\nLEO - HELLO BOLLO", anchor_x="left", anchor_y="baseline", - multiline=True + multiline=True, + + batch=self.batch_label, ) self.chat_input = self.add_widget( @@ -94,7 +121,10 @@ class Game(Scene): x=10, y=10, width=0.5, height=50, - style=texture.Button.Style1 + style=texture.Button.Style1, + + background_batch=self.batch_input_background, + label_batch=self.batch_label, ) self.button_save = self.add_widget( @@ -104,7 +134,10 @@ class Game(Scene): label_text="Sauvegarder", - style=texture.Button.Style1 + style=texture.Button.Style1, + + background_batch=self.batch_button_background, + label_batch=self.batch_label, ) self.button_quit = self.add_widget( @@ -114,7 +147,10 @@ class Game(Scene): label_text="Quitter", - style=texture.Button.Style1 + style=texture.Button.Style1, + + background_batch=self.batch_button_background, + label_batch=self.batch_label, ) self.board_ally = core.Board(rows=8, columns=8) @@ -123,17 +159,10 @@ class Game(Scene): def on_draw(self): self.background.draw() - self.grid_ally.draw() - self.grid_enemy.draw() + self.batch_button_background.draw() + self.batch_input_background.draw() + self.batch_grid_background.draw() + self.batch_grid_line.draw() + self.batch_grid_cursor.draw() - self.name_ally.draw() - self.name_enemy.draw() - - self.score_ally.draw() - self.score_enemy.draw() - - self.chat_log.draw() - self.chat_input.draw() - - self.button_save.draw() - self.button_quit.draw() + self.batch_label.draw() diff --git a/source/gui/scene/MainMenu.py b/source/gui/scene/MainMenu.py index 98257e0..2f9e730 100644 --- a/source/gui/scene/MainMenu.py +++ b/source/gui/scene/MainMenu.py @@ -1,8 +1,9 @@ from typing import TYPE_CHECKING +import pyglet + from source.gui.scene.abc import Scene from source.gui import widget, scene, texture -from source.utils.dict import dict_add_prefix if TYPE_CHECKING: from source.gui.window import Window @@ -12,28 +13,39 @@ class MainMenu(Scene): def __init__(self, window: "Window", *args, **kwargs): super().__init__(window, *args, **kwargs) + self.batch_button_background = pyglet.graphics.Batch() + self.batch_label = pyglet.graphics.Batch() + self.background = self.add_widget( widget.Image, + x=0.0, y=0.0, width=1.0, height=1.0, + image=texture.Background.main ) self.title = self.add_widget( widget.Text, + x=50, y=0.85, + text="Bataille Navale", - font_size=50 + font_size=50, + + batch=self.batch_label ) self.game_create = self.add_widget( widget.Button, - x=50, y=0.45, width=0.3, height=0.1, label_text="Créer une salle", label_font_size=20, - style=texture.Button.Style1 + style=texture.Button.Style1, + + background_batch=self.batch_button_background, + label_batch=self.batch_label ) self.game_create.add_listener("on_click_release", lambda *_: self.window.set_scene(scene.RoomCreate)) @@ -46,7 +58,10 @@ class MainMenu(Scene): label_text="Rejoindre une salle", label_font_size=20, - style=texture.Button.Style1 + style=texture.Button.Style1, + + background_batch=self.batch_button_background, + label_batch=self.batch_label ) self.game_join.add_listener("on_click_release", lambda *_: self.window.set_scene(scene.RoomJoin)) @@ -59,14 +74,16 @@ class MainMenu(Scene): label_text="Paramètres", label_font_size=20, - style=texture.Button.Style1 + style=texture.Button.Style1, + + background_batch=self.batch_button_background, + label_batch=self.batch_label ) self.settings.add_listener("on_click_release", lambda *_: self.window.set_scene(scene.Settings)) def on_draw(self): self.background.draw() - self.title.draw() - self.game_create.draw() - self.game_join.draw() - self.settings.draw() + + self.batch_button_background.draw() + self.batch_label.draw() diff --git a/source/gui/scene/RoomCreate.py b/source/gui/scene/RoomCreate.py index ee95e50..018d90f 100644 --- a/source/gui/scene/RoomCreate.py +++ b/source/gui/scene/RoomCreate.py @@ -21,13 +21,19 @@ class RoomCreate(Scene): ip_address: str = r.content.decode('utf8') port: int = 52321 + self.batch_button_background = pyglet.graphics.Batch() + self.batch_label = pyglet.graphics.Batch() + self.back = self.add_widget( widget.Button, x=20, y=20, width=0.2, height=0.1, label_text="Retour", - style=texture.Button.Style1 + style=texture.Button.Style1, + + background_batch=self.batch_button_background, + label_batch=self.batch_label ) from source.gui.scene import MainMenu @@ -35,23 +41,30 @@ class RoomCreate(Scene): self.label_ip = self.add_widget( widget.Text, + x=0.5, y=0.55, + anchor_x="center", anchor_y="center", text=f"Votre IP - {ip_address}:{port}", - font_size=20 + font_size=20, + + batch=self.batch_label ) self.description = self.add_widget( widget.Text, + x=0.5, y=0.45, + anchor_x="center", anchor_y="center", - text="En attente d'un second joueur..." + text="En attente d'un second joueur...", + + batch=self.batch_label ) self.thread = network.Host(window=self.window, daemon=True, username="Host") self.thread.start() def on_draw(self): - self.back.draw() - self.label_ip.draw() - self.description.draw() + self.batch_button_background.draw() + self.batch_label.draw() diff --git a/source/gui/scene/RoomJoin.py b/source/gui/scene/RoomJoin.py index e4a34aa..bdb2e6e 100644 --- a/source/gui/scene/RoomJoin.py +++ b/source/gui/scene/RoomJoin.py @@ -5,7 +5,6 @@ import pyglet from source import network from source.gui.scene.abc import Scene from source.gui import widget, texture -from source.utils.dict import dict_add_prefix if TYPE_CHECKING: from source.gui.window import Window @@ -15,13 +14,20 @@ class RoomJoin(Scene): def __init__(self, window: "Window", *args, **kwargs): super().__init__(window, *args, **kwargs) + self.batch_button_background = pyglet.graphics.Batch() + self.batch_input_background = pyglet.graphics.Batch() + self.batch_label = pyglet.graphics.Batch() + self.back = self.add_widget( widget.Button, x=20, y=20, width=0.2, height=0.1, label_text="Retour", - style=texture.Button.Style1 + style=texture.Button.Style1, + + background_batch=self.batch_button_background, + label_batch=self.batch_label ) from source.gui.scene import MainMenu @@ -33,7 +39,10 @@ class RoomJoin(Scene): regex=r"\d{1,3}(\.\d{1,3}){3}", - style=texture.Input.Style1 + style=texture.Input.Style1, + + background_batch=self.batch_input_background, + label_batch=self.batch_label ) self.entry_port = self.add_widget( @@ -42,7 +51,10 @@ class RoomJoin(Scene): regex=r"\d{0,5}", - style=texture.Input.Style1 + style=texture.Input.Style1, + + background_batch=self.batch_input_background, + label_batch=self.batch_label ) self.connect = self.add_widget( @@ -51,7 +63,10 @@ class RoomJoin(Scene): label_text="Se connecter", - style=texture.Button.Style1 + style=texture.Button.Style1, + + background_batch=self.batch_button_background, + label_batch=self.batch_label ) self.connect.add_listener("on_click_release", lambda *_: network.Client( @@ -62,7 +77,6 @@ class RoomJoin(Scene): ).start()) def on_draw(self): - self.back.draw() - self.entry_ip.draw() - self.entry_port.draw() - self.connect.draw() + self.batch_button_background.draw() + self.batch_input_background.draw() + self.batch_label.draw() diff --git a/source/gui/scene/Settings.py b/source/gui/scene/Settings.py index c766828..cde00fb 100644 --- a/source/gui/scene/Settings.py +++ b/source/gui/scene/Settings.py @@ -1,8 +1,9 @@ from typing import TYPE_CHECKING +import pyglet + from source.gui.scene.abc import Scene from source.gui import widget, texture -from source.utils.dict import dict_add_prefix if TYPE_CHECKING: from source.gui.window import Window @@ -12,13 +13,22 @@ class Settings(Scene): def __init__(self, window: "Window", *args, **kwargs): super().__init__(window, *args, **kwargs) + self.batch_button_background = pyglet.graphics.Batch() + self.batch_scroller_background = pyglet.graphics.Batch() + self.batch_scroller_cursor = pyglet.graphics.Batch() + self.batch_checkbox = pyglet.graphics.Batch() + self.batch_label = pyglet.graphics.Batch() + self.back = self.add_widget( widget.Button, x=20, y=20, width=0.2, height=0.1, label_text="Retour", - style=texture.Button.Style1 + style=texture.Button.Style1, + + background_batch=self.batch_button_background, + label_batch=self.batch_label ) from source.gui.scene import MainMenu @@ -29,7 +39,9 @@ class Settings(Scene): x=0.45, y=0.45, width=0.1, height=0.1, - style=texture.Checkbox.Style1 + style=texture.Checkbox.Style1, + + batch=self.batch_checkbox ) self.scroller = self.add_widget( @@ -40,9 +52,18 @@ class Settings(Scene): style=texture.Scroller.Style1, text_transform=lambda value: round(value, 2), + + background_batch=self.batch_scroller_background, + cursor_batch=self.batch_scroller_cursor, + label_batch=self.batch_label ) def on_draw(self): - self.checkbox.draw() - self.scroller.draw() - self.back.draw() + self.batch_button_background.draw() + + self.batch_scroller_background.draw() + self.batch_scroller_cursor.draw() + + self.batch_checkbox.draw() + + self.batch_label.draw()