Skip to content

Commit

Permalink
Shapelist optimize (#1694)
Browse files Browse the repository at this point in the history
* Color.random

* Initial work

* More fixes

* Enable add mode

* Create vertex data using the modified colors

* Clean up properties etc

* Tweak skyline example

* Make lines_buffered smoother

* Docstrings and module cleanup

* Groups are now only based on the primitive mode
  • Loading branch information
einarf committed Apr 9, 2023
1 parent ca38749 commit b38bd08
Show file tree
Hide file tree
Showing 6 changed files with 590 additions and 326 deletions.
7 changes: 4 additions & 3 deletions arcade/examples/lines_buffered.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(self, width, height, title):
Set up the application.
"""
super().__init__(width, height, title)
self.set_vsync(True)

self.shape_list = ShapeElementList()
point_list = ((0, 50),
Expand Down Expand Up @@ -72,9 +73,9 @@ def on_draw(self):
self.shape_list.draw()

def on_update(self, delta_time):
self.shape_list.angle += 1
self.shape_list.center_x += 0.1
self.shape_list.center_y += 0.1
self.shape_list.angle += 1 * 60 * delta_time
self.shape_list.center_x += 0.1 * 60 * delta_time
self.shape_list.center_y += 0.1 * 60 * delta_time


def main():
Expand Down
48 changes: 20 additions & 28 deletions arcade/examples/shape_list_demo_skylines.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@
create_rectangle_filled,
create_rectangles_filled_with_colors,
)
import time

SCREEN_WIDTH = 1200
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Skyline Using Buffered Shapes"


def make_star_field(star_count):
""" Make a bunch of circles for stars. """

"""Make a bunch of circles for stars"""
shape_list = ShapeElementList()

for star_no in range(star_count):
for _ in range(star_count):
x = random.randrange(SCREEN_WIDTH)
y = random.randrange(SCREEN_HEIGHT)
radius = random.randrange(1, 4)
Expand All @@ -41,8 +39,7 @@ def make_skyline(width, skyline_height, skyline_color,
gap_chance=0.70, window_chance=0.30, light_on_chance=0.5,
window_color=(255, 255, 200), window_margin=3, window_gap=2,
cap_chance=0.20):
""" Make a skyline """

"""Make a skyline of buildings"""
shape_list = ShapeElementList()

# Add the "base" that we build the buildings on
Expand Down Expand Up @@ -74,11 +71,8 @@ def make_skyline(width, skyline_height, skyline_color,
y2 = skyline_height + building_height

skyline_point_list.append([x1, y1])

skyline_point_list.append([x1, y2])

skyline_point_list.append([x2, y2])

skyline_point_list.append([x2, y1])

for i in range(4):
Expand All @@ -92,6 +86,7 @@ def make_skyline(width, skyline_height, skyline_color,
y1 = y2 = building_center_y + building_height / 2
y3 = y1 + building_width / 2

# Roof
shape = create_polygon([[x1, y1], [x2, y2], [x3, y3]], skyline_color)
shape_list.append(shape)

Expand Down Expand Up @@ -141,44 +136,41 @@ def __init__(self):
""" Initializer """
# Call the parent class initializer
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
# Enable vertical sync to make scrolling smoother
self.set_vsync(True)

self.stars = make_star_field(150)
self.skyline1 = make_skyline(SCREEN_WIDTH * 5, 250, (80, 80, 80))
self.skyline2 = make_skyline(SCREEN_WIDTH * 5, 150, (50, 50, 50))

self.background_color = arcade.color.BLACK

def setup(self):
""" Set up the game and initialize the variables. """

def on_draw(self):
"""
Render the screen.
"""

# This command has to happen before we start drawing

start_time = int(round(time.time() * 1000))
"""Draw to screen"""
self.clear()

self.stars.draw()
self.skyline1.draw()
self.skyline2.draw()
end_time = int(round(time.time() * 1000))
total_time = end_time - start_time

arcade.draw_text(f"Time: {total_time}", 10, 10, arcade.color.WHITE)

def on_update(self, delta_time):
""" Movement and game logic """
self.skyline1.center_x -= 0.5
self.skyline2.center_x -= 1
"""Per frame update logic"""
# Scroll each shape list with a slight offset to give a parallax effect
self.skyline1.center_x -= 0.5 * 60 * delta_time
self.skyline2.center_x -= 1 * 60 * delta_time

def on_mouse_drag(self, x: int, y: int, dx: int, dy: int, buttons: int, modifiers: int):
"""Make it possible scroll the scene around by dragging the mouse"""
self.skyline1.center_x += dx
self.skyline1.center_y += dy

self.skyline2.center_x += dx
self.skyline2.center_y += dy


def main():
window = MyGame()
window.setup()
arcade.run()
window.run()


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion arcade/resources/system/shaders/shape_element_list_vs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ void main() {
-sin(angle), cos(angle)
);
gl_Position = window.projection * window.view * vec4(Position + (rotate * in_vert), 0.0, 1.0);
v_color = in_color;
v_color = in_color / 255.0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ out vec4 v_color;

void main() {
gl_Position = window.projection * window.view * vec4(in_vert, 0.0, 1.0);
v_color = in_color;
v_color = in_color / 255.0; // Normalize the color
}
Loading

0 comments on commit b38bd08

Please sign in to comment.