Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions examples/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,20 @@

MAX_ENTITIES = options.entities
if MAX_ENTITIES <= 500:
print("The number of entities must be greater than 500.")
sys.exit(1)
sys.exit("The number of entities must be greater than 500.")

if options.walltime:
print("Benchmarking wall clock time...\n")
time_query = time.time
else:
time_query = time.process_time

if options.plot:
try:
from matplotlib import pyplot as plt
except ImportError:
sys.exit("The matplotlib module is required for plotting results.")


##########################
# Simple timing decorator:
Expand Down Expand Up @@ -169,14 +174,7 @@ def three_comp_query():

if not options.plot:
print("\nRun 'benchmark.py --help' for details on plotting this benchmark.")

if options.plot:
try:
from matplotlib import pyplot as plt
except ImportError:
print("\nThe matplotlib module is required for plotting results.")
sys.exit(1)

else:
lines = []
for num, result in results.items():
x, y = zip(*sorted(result.items()))
Expand Down
16 changes: 7 additions & 9 deletions examples/benchmark_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@

import esper

try:
from matplotlib import pyplot
except ImportError:
print("The matplotlib module is required for this benchmark.")
raise Exception

######################
# Commandline options:
######################
Expand All @@ -25,9 +19,13 @@
(options, arguments) = parser.parse_args()

MAX_ENTITIES = options.entities
if MAX_ENTITIES <= 50:
print("The number of entities must be greater than 500.")
sys.exit(1)
if MAX_ENTITIES <= 500:
sys.exit("The number of entities must be greater than 500.")

try:
from matplotlib import pyplot
except ImportError:
sys.exit("The matplotlib module is required for plotting results.")


##########################
Expand Down
15 changes: 12 additions & 3 deletions examples/pygame_example.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
# /// script
# requires-python = ">=3.8"
# dependencies = [
# "esper",
# "pygame",
# ]
# ///
from pathlib import Path
import pygame

import esper


FPS = 60
RESOLUTION = 720, 480

# Parent dir of this script, to find the PNGs regardless of cwd
path = Path(__file__).parent

##################################
# Define some Components:
Expand Down Expand Up @@ -81,10 +90,10 @@ def run():
# Initialize Esper world, and create a "player" Entity with a few Components.
player = esper.create_entity()
esper.add_component(player, Velocity(x=0, y=0))
esper.add_component(player, Renderable(image=pygame.image.load("redsquare.png"), posx=100, posy=100))
esper.add_component(player, Renderable(image=pygame.image.load(path / "redsquare.png"), posx=100, posy=100))
# Another motionless Entity:
enemy = esper.create_entity()
esper.add_component(enemy, Renderable(image=pygame.image.load("bluesquare.png"), posx=400, posy=250))
esper.add_component(enemy, Renderable(image=pygame.image.load(path / "bluesquare.png"), posx=400, posy=250))

# Create some Processor instances, and asign them to be processed.
render_processor = RenderProcessor(window=window)
Expand Down
9 changes: 8 additions & 1 deletion examples/pyglet_example.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# /// script
# requires-python = ">=3.8"
# dependencies = [
# "esper",
# "pyglet",
# ]
# ///
import pyglet
import esper

Expand Down Expand Up @@ -29,8 +36,8 @@ class MovementProcessor:
def __init__(self, minx, maxx, miny, maxy):
super().__init__()
self.minx = minx
self.miny = miny
self.maxx = maxx
self.miny = miny
self.maxy = maxy

def process(self, dt):
Expand Down
18 changes: 14 additions & 4 deletions examples/pysdl2_example.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
# /// script
# requires-python = ">=3.8"
# dependencies = [
# "esper",
# "pysdl2",
# ]
# ///
from pathlib import Path
from sdl2 import *
import sdl2.ext as ext
import esper


RESOLUTION = 720, 480

# Parent dir of this script, to find the PNGs regardless of cwd
path = Path(__file__).parent

##################################
# Define some Components:
Expand Down Expand Up @@ -66,7 +76,7 @@ def process(self):
destination.y = int(rend.y)
destination.w = rend.w
destination.h = rend.h
SDL_RenderCopy(self.renderer.renderer, rend.texture, None, destination)
SDL_RenderCopy(self.renderer.sdlrenderer, rend.texture, None, destination)
self.renderer.present()


Expand All @@ -76,7 +86,7 @@ def process(self):
def texture_from_image(renderer, image_name):
"""Create an SDL2 Texture from an image file"""
soft_surface = ext.load_image(image_name)
texture = SDL_CreateTextureFromSurface(renderer.renderer, soft_surface)
texture = SDL_CreateTextureFromSurface(renderer.sdlrenderer, soft_surface)
SDL_FreeSurface(soft_surface)
return texture

Expand All @@ -94,11 +104,11 @@ def run():
# Initialize Esper world, and create a "player" Entity with a few Components.
player = esper.create_entity()
esper.add_component(player, Velocity(x=0, y=0))
esper.add_component(player, Renderable(texture=texture_from_image(renderer, "redsquare.png"),
esper.add_component(player, Renderable(texture=texture_from_image(renderer, str(path / "redsquare.png")),
width=64, height=64, posx=100, posy=100))
# Another motionless Entity:
enemy = esper.create_entity()
esper.add_component(enemy, Renderable(texture=texture_from_image(renderer, "bluesquare.png"),
esper.add_component(enemy, Renderable(texture=texture_from_image(renderer, str(path / "bluesquare.png")),
width=64, height=64, posx=400, posy=250))

# Create some Processor instances, and asign them to be processed.
Expand Down