drawing is now done with batch (speeding up the drawing by 50% to 300%)

This commit is contained in:
Faraphel 2023-02-19 12:32:06 +01:00
parent 75c1d4b6e8
commit d2d2f2fdc1
7 changed files with 146 additions and 52 deletions

View file

@ -1,6 +1,5 @@
A faire : A faire :
- Faire une scène incluant par défaut les boutons "Retour" - 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 - Ajouter un moyen d'annuler les évenements de se propager aux widgets en dessous
- Police d'écriture - Police d'écriture

View file

@ -5,7 +5,8 @@ from source.gui.window import GameWindow
# Create a new window # 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) window.add_scene(MainMenu)
# Start the event loop # Start the event loop

View file

@ -1,5 +1,7 @@
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
import pyglet
from source.gui.scene.abc import Scene from source.gui.scene.abc import Scene
from source.gui import widget, texture from source.gui import widget, texture
from source import core from source import core
@ -13,6 +15,13 @@ class Game(Scene):
def __init__(self, window: "Window", **kwargs): def __init__(self, window: "Window", **kwargs):
super().__init__(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( self.background = self.add_widget(
widget.Image, widget.Image,
@ -28,6 +37,10 @@ class Game(Scene):
style=texture.Grid.Style1, style=texture.Grid.Style1,
rows=8, columns=8, 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( self.grid_enemy = self.add_widget(
@ -37,6 +50,10 @@ class Game(Scene):
style=texture.Grid.Style1, style=texture.Grid.Style1,
rows=8, columns=8, 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( self.name_ally = self.add_widget(
@ -47,6 +64,8 @@ class Game(Scene):
text="Raphael", text="Raphael",
font_size=20, font_size=20,
anchor_x="center", anchor_y="center", anchor_x="center", anchor_y="center",
batch=self.batch_label,
) )
self.name_enemy = self.add_widget( self.name_enemy = self.add_widget(
@ -57,6 +76,8 @@ class Game(Scene):
text="Leo", text="Leo",
font_size=20, font_size=20,
anchor_x="center", anchor_y="center", anchor_x="center", anchor_y="center",
batch=self.batch_label,
) )
self.score_ally = self.add_widget( self.score_ally = self.add_widget(
@ -66,7 +87,9 @@ class Game(Scene):
text="7", text="7",
font_size=25, 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( self.score_enemy = self.add_widget(
@ -76,7 +99,9 @@ class Game(Scene):
text="5", text="5",
font_size=25, 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( self.chat_log = self.add_widget(
@ -86,7 +111,9 @@ class Game(Scene):
text="FARAPHEL - HELLO BILLY\nLEO - HELLO BOLLO", text="FARAPHEL - HELLO BILLY\nLEO - HELLO BOLLO",
anchor_x="left", anchor_y="baseline", anchor_x="left", anchor_y="baseline",
multiline=True multiline=True,
batch=self.batch_label,
) )
self.chat_input = self.add_widget( self.chat_input = self.add_widget(
@ -94,7 +121,10 @@ class Game(Scene):
x=10, y=10, width=0.5, height=50, 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( self.button_save = self.add_widget(
@ -104,7 +134,10 @@ class Game(Scene):
label_text="Sauvegarder", 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( self.button_quit = self.add_widget(
@ -114,7 +147,10 @@ class Game(Scene):
label_text="Quitter", 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) self.board_ally = core.Board(rows=8, columns=8)
@ -123,17 +159,10 @@ class Game(Scene):
def on_draw(self): def on_draw(self):
self.background.draw() self.background.draw()
self.grid_ally.draw() self.batch_button_background.draw()
self.grid_enemy.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.batch_label.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()

View file

@ -1,8 +1,9 @@
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
import pyglet
from source.gui.scene.abc import Scene from source.gui.scene.abc import Scene
from source.gui import widget, scene, texture from source.gui import widget, scene, texture
from source.utils.dict import dict_add_prefix
if TYPE_CHECKING: if TYPE_CHECKING:
from source.gui.window import Window from source.gui.window import Window
@ -12,28 +13,39 @@ class MainMenu(Scene):
def __init__(self, window: "Window", *args, **kwargs): def __init__(self, window: "Window", *args, **kwargs):
super().__init__(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( self.background = self.add_widget(
widget.Image, widget.Image,
x=0.0, y=0.0, width=1.0, height=1.0, x=0.0, y=0.0, width=1.0, height=1.0,
image=texture.Background.main image=texture.Background.main
) )
self.title = self.add_widget( self.title = self.add_widget(
widget.Text, widget.Text,
x=50, y=0.85, x=50, y=0.85,
text="Bataille Navale", text="Bataille Navale",
font_size=50 font_size=50,
batch=self.batch_label
) )
self.game_create = self.add_widget( self.game_create = self.add_widget(
widget.Button, widget.Button,
x=50, y=0.45, width=0.3, height=0.1, x=50, y=0.45, width=0.3, height=0.1,
label_text="Créer une salle", label_text="Créer une salle",
label_font_size=20, 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)) 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_text="Rejoindre une salle",
label_font_size=20, 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)) 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_text="Paramètres",
label_font_size=20, 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)) self.settings.add_listener("on_click_release", lambda *_: self.window.set_scene(scene.Settings))
def on_draw(self): def on_draw(self):
self.background.draw() self.background.draw()
self.title.draw()
self.game_create.draw() self.batch_button_background.draw()
self.game_join.draw() self.batch_label.draw()
self.settings.draw()

View file

@ -21,13 +21,19 @@ class RoomCreate(Scene):
ip_address: str = r.content.decode('utf8') ip_address: str = r.content.decode('utf8')
port: int = 52321 port: int = 52321
self.batch_button_background = pyglet.graphics.Batch()
self.batch_label = pyglet.graphics.Batch()
self.back = self.add_widget( self.back = self.add_widget(
widget.Button, widget.Button,
x=20, y=20, width=0.2, height=0.1, x=20, y=20, width=0.2, height=0.1,
label_text="Retour", 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 from source.gui.scene import MainMenu
@ -35,23 +41,30 @@ class RoomCreate(Scene):
self.label_ip = self.add_widget( self.label_ip = self.add_widget(
widget.Text, widget.Text,
x=0.5, y=0.55, x=0.5, y=0.55,
anchor_x="center", anchor_y="center", anchor_x="center", anchor_y="center",
text=f"Votre IP - {ip_address}:{port}", text=f"Votre IP - {ip_address}:{port}",
font_size=20 font_size=20,
batch=self.batch_label
) )
self.description = self.add_widget( self.description = self.add_widget(
widget.Text, widget.Text,
x=0.5, y=0.45, x=0.5, y=0.45,
anchor_x="center", anchor_y="center", 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 = network.Host(window=self.window, daemon=True, username="Host")
self.thread.start() self.thread.start()
def on_draw(self): def on_draw(self):
self.back.draw() self.batch_button_background.draw()
self.label_ip.draw() self.batch_label.draw()
self.description.draw()

View file

@ -5,7 +5,6 @@ import pyglet
from source import network from source import network
from source.gui.scene.abc import Scene from source.gui.scene.abc import Scene
from source.gui import widget, texture from source.gui import widget, texture
from source.utils.dict import dict_add_prefix
if TYPE_CHECKING: if TYPE_CHECKING:
from source.gui.window import Window from source.gui.window import Window
@ -15,13 +14,20 @@ class RoomJoin(Scene):
def __init__(self, window: "Window", *args, **kwargs): def __init__(self, window: "Window", *args, **kwargs):
super().__init__(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( self.back = self.add_widget(
widget.Button, widget.Button,
x=20, y=20, width=0.2, height=0.1, x=20, y=20, width=0.2, height=0.1,
label_text="Retour", 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 from source.gui.scene import MainMenu
@ -33,7 +39,10 @@ class RoomJoin(Scene):
regex=r"\d{1,3}(\.\d{1,3}){3}", 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( self.entry_port = self.add_widget(
@ -42,7 +51,10 @@ class RoomJoin(Scene):
regex=r"\d{0,5}", 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( self.connect = self.add_widget(
@ -51,7 +63,10 @@ class RoomJoin(Scene):
label_text="Se connecter", 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( self.connect.add_listener("on_click_release", lambda *_: network.Client(
@ -62,7 +77,6 @@ class RoomJoin(Scene):
).start()) ).start())
def on_draw(self): def on_draw(self):
self.back.draw() self.batch_button_background.draw()
self.entry_ip.draw() self.batch_input_background.draw()
self.entry_port.draw() self.batch_label.draw()
self.connect.draw()

View file

@ -1,8 +1,9 @@
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
import pyglet
from source.gui.scene.abc import Scene from source.gui.scene.abc import Scene
from source.gui import widget, texture from source.gui import widget, texture
from source.utils.dict import dict_add_prefix
if TYPE_CHECKING: if TYPE_CHECKING:
from source.gui.window import Window from source.gui.window import Window
@ -12,13 +13,22 @@ class Settings(Scene):
def __init__(self, window: "Window", *args, **kwargs): def __init__(self, window: "Window", *args, **kwargs):
super().__init__(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( self.back = self.add_widget(
widget.Button, widget.Button,
x=20, y=20, width=0.2, height=0.1, x=20, y=20, width=0.2, height=0.1,
label_text="Retour", 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 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, 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( self.scroller = self.add_widget(
@ -40,9 +52,18 @@ class Settings(Scene):
style=texture.Scroller.Style1, style=texture.Scroller.Style1,
text_transform=lambda value: round(value, 2), 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): def on_draw(self):
self.checkbox.draw() self.batch_button_background.draw()
self.scroller.draw()
self.back.draw() self.batch_scroller_background.draw()
self.batch_scroller_cursor.draw()
self.batch_checkbox.draw()
self.batch_label.draw()