Skip to content

Commit

Permalink
Split defender example into one showing bloom, and one showing minimap.
Browse files Browse the repository at this point in the history
  • Loading branch information
pvcraven committed Apr 12, 2020
1 parent 2640024 commit be29168
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 93 deletions.
63 changes: 3 additions & 60 deletions arcade/experimental/bloom_defender.py
Expand Up @@ -29,12 +29,8 @@
PLAYING_FIELD_WIDTH = 5000
PLAYING_FIELD_HEIGHT = 800

# Size of the minimap
MINIMAP_HEIGHT = SCREEN_HEIGHT / 4

# Size of the playing field.
# This, plus the minimap height, should add up to the height of the screen.
MAIN_SCREEN_HEIGHT = SCREEN_HEIGHT - MINIMAP_HEIGHT
MAIN_SCREEN_HEIGHT = SCREEN_HEIGHT

# How far away from the edges do we get before scrolling?
VIEWPORT_MARGIN = SCREEN_WIDTH / 2 - 50
Expand Down Expand Up @@ -181,23 +177,9 @@ def __init__(self, width, height, title):
# Set the background color
arcade.set_background_color(arcade.color.BLACK)

# --- Mini-map related ---
self.minimap_color_attachment = None
self.minimap_screen = None
self.quad_fs = None
self.mini_map_quad = None

program = self.ctx.load_program(
vertex_shader="simple_shader.vert",
fragment_shader="simple_shader.frag")
self.minimap_color_attachment = self.ctx.texture((SCREEN_WIDTH, SCREEN_HEIGHT))
self.minimap_screen = self.ctx.framebuffer(color_attachments=[self.minimap_color_attachment])
# --- Bloom related ---
self.play_screen_color_attachment = self.ctx.texture((SCREEN_WIDTH, SCREEN_HEIGHT))
self.play_screen = self.ctx.framebuffer(color_attachments=[self.play_screen_color_attachment])
self.mini_map_quad = geometry.quad_fs(program, size=(2.0, 0.5), pos=(0.0, 0.75))
self.play_screen_quad = geometry.quad_fs(program, size=(2.0, 1.5), pos=(0.0, 0.0))

# --- Bloom related ---
self.glow = postprocessing.Glow((SCREEN_WIDTH // 8, SCREEN_HEIGHT // 8))

def setup(self):
Expand Down Expand Up @@ -235,20 +217,6 @@ def on_draw(self):
# This command has to happen before we start drawing
arcade.start_render()

# --- Mini-map related ---

# Draw to the frame buffer used in the minimap
self.minimap_screen.use()
self.minimap_screen.clear()

arcade.set_viewport(0,
PLAYING_FIELD_WIDTH,
0,
SCREEN_HEIGHT)

self.enemy_sprite_list.draw()
self.player_list.draw()

# --- Bloom related ---

# Draw to the 'glow' layer
Expand Down Expand Up @@ -285,31 +253,6 @@ def on_draw(self):
# Draw the ground
arcade.draw_line(0, 0, PLAYING_FIELD_WIDTH, 0, arcade.color.WHITE)

# Draw a background for the minimap
arcade.draw_rectangle_filled(SCREEN_WIDTH - SCREEN_WIDTH / 2 + self.view_left,
SCREEN_HEIGHT - SCREEN_HEIGHT / 8 + self.view_bottom,
SCREEN_WIDTH,
SCREEN_HEIGHT / 4,
arcade.color.DARK_GREEN)

# --- Mini-map related ---

# Draw the minimap
self.minimap_color_attachment.use(0)
self.mini_map_quad.render()

# Draw a rectangle showing where the screen is
width_ratio = SCREEN_WIDTH / PLAYING_FIELD_WIDTH
height_ratio = MINIMAP_HEIGHT / PLAYING_FIELD_HEIGHT
width = width_ratio * SCREEN_WIDTH
height = height_ratio * MAIN_SCREEN_HEIGHT
x = (self.view_left + SCREEN_WIDTH / 2) * width_ratio + self.view_left
y = self.view_bottom + height / 2 + (SCREEN_HEIGHT - MINIMAP_HEIGHT) + self.view_bottom * height_ratio

arcade.draw_rectangle_outline(center_x=x, center_y=y,
width=width, height=height,
color=arcade.color.WHITE)

except Exception:
import traceback
traceback.print_exc()
Expand Down Expand Up @@ -357,7 +300,7 @@ def on_update(self, delta_time):

# Scroll up
self.view_bottom = DEFAULT_BOTTOM_VIEWPORT
top_boundary = self.view_bottom + SCREEN_HEIGHT - TOP_VIEWPORT_MARGIN - MINIMAP_HEIGHT
top_boundary = self.view_bottom + SCREEN_HEIGHT - TOP_VIEWPORT_MARGIN
if self.player_sprite.top > top_boundary:
self.view_bottom += self.player_sprite.top - top_boundary

Expand Down
38 changes: 5 additions & 33 deletions arcade/experimental/minimap_defender.py
@@ -1,12 +1,10 @@
"""
Defender Clone.
This example shows how to:
1. Create a mini-map
2. Create a 'bloom' or 'glow' effect.
This example shows how to create a mini-map
If Python and Arcade are installed, this example can be run from the command line with:
python -m arcade.examples.defender
python -m arcade.examples.minimap_defender
"""

import arcade
Expand All @@ -16,10 +14,6 @@
# --- Minimap Related ---
from arcade.experimental import geometry

# --- Bloom related ---
from arcade.experimental import postprocessing
import pyglet.gl as gl

# Size/title of the window
SCREEN_WIDTH = 1280
SCREEN_HEIGHT = 720
Expand All @@ -29,6 +23,7 @@
PLAYING_FIELD_WIDTH = 5000
PLAYING_FIELD_HEIGHT = 800

# --- Mini-map related ---
# Size of the minimap
MINIMAP_HEIGHT = SCREEN_HEIGHT / 4

Expand Down Expand Up @@ -192,13 +187,7 @@ def __init__(self, width, height, title):
fragment_shader="simple_shader.frag")
self.minimap_color_attachment = self.ctx.texture((SCREEN_WIDTH, SCREEN_HEIGHT))
self.minimap_screen = self.ctx.framebuffer(color_attachments=[self.minimap_color_attachment])
self.play_screen_color_attachment = self.ctx.texture((SCREEN_WIDTH, SCREEN_HEIGHT))
self.play_screen = self.ctx.framebuffer(color_attachments=[self.play_screen_color_attachment])
self.mini_map_quad = geometry.quad_fs(program, size=(2.0, 0.5), pos=(0.0, 0.75))
self.play_screen_quad = geometry.quad_fs(program, size=(2.0, 1.5), pos=(0.0, 0.0))

# --- Bloom related ---
self.glow = postprocessing.Glow((SCREEN_WIDTH // 8, SCREEN_HEIGHT // 8))

def setup(self):
""" Set up the game and initialize the variables. """
Expand Down Expand Up @@ -249,21 +238,6 @@ def on_draw(self):
self.enemy_sprite_list.draw()
self.player_list.draw()

# --- Bloom related ---

# Draw to the 'glow' layer
self.play_screen.use()
self.play_screen.clear()

arcade.set_viewport(self.view_left,
SCREEN_WIDTH + self.view_left,
self.view_bottom,
SCREEN_HEIGHT + self.view_bottom)

# Draw all the sprites on the screen
self.star_sprite_list.draw()
self.bullet_sprite_list.draw()

# Draw the ground
arcade.draw_line(0, 0, PLAYING_FIELD_WIDTH, 0, arcade.color.WHITE)

Expand All @@ -275,11 +249,9 @@ def on_draw(self):
self.view_bottom,
SCREEN_HEIGHT + self.view_bottom)

gl.glDisable(gl.GL_BLEND)
self.glow.render(self.play_screen_color_attachment, self)
gl.glEnable(gl.GL_BLEND)

self.star_sprite_list.draw()
self.enemy_sprite_list.draw()
self.bullet_sprite_list.draw()
self.player_list.draw()

# Draw the ground
Expand Down

0 comments on commit be29168

Please sign in to comment.