Skip to content

Commit

Permalink
set filter mode bilinear (default) or none on sprites
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Wiggins committed Aug 16, 2009
1 parent b219918 commit 731755e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
15 changes: 14 additions & 1 deletion cocos/sprite.py
Expand Up @@ -71,8 +71,9 @@ class Sprite( BatchableNode, pyglet.sprite.Sprite):
'''

BLEND_STANDARD, BLEND_ADDITIVE = range(2)
FILTER_BILINEAR, FILTER_NONE = range(2)

def __init__( self, image, position=(0,0), rotation=0, scale=1, opacity = 255, color=(255,255,255), blend_mode = BLEND_STANDARD, anchor = None ):
def __init__( self, image, position=(0,0), rotation=0, scale=1, opacity = 255, color=(255,255,255), blend_mode = BLEND_STANDARD, filter_mode = FILTER_BILINEAR, anchor = None ):
'''Initialize the sprite
:Parameters:
Expand Down Expand Up @@ -136,6 +137,9 @@ def __init__( self, image, position=(0,0), rotation=0, scale=1, opacity = 255, c
#: blend mode: BLEND_STANDARD (use alpha value) or BLEND_ADDITIVE (use color overlay, ignore alpha)
self.blend_mode = blend_mode

#: filter mode: FILTER_BILINEAR (best for scaling) or FILTER_NONE (best for 1:1 pixel-perfect images)
self.filter_mode = filter_mode


def contains(self, x, y):
'''Test whether this (untransformed) Sprite contains the pixel coordinates
Expand Down Expand Up @@ -197,9 +201,18 @@ def setup_blending_mode(self):
else:
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

def setup_filter_mode(self):
if self.filter_mode == Sprite.FILTER_BILINEAR:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
elif self.filter_mode == Sprite.FILTER_NONE:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)

def draw(self):
self._group.set_state()
self.setup_blending_mode()
self.setup_filter_mode()
if self._vertex_list is not None:
self._vertex_list.draw(GL_QUADS)
self._group.unset_state()
Expand Down
12 changes: 11 additions & 1 deletion samples/demo_blending.py → samples/demo_blending_filtering.py
Expand Up @@ -24,7 +24,7 @@ def __init__( self ):

self.batch = pyglet.graphics.Batch()

self.text_title = pyglet.text.Label("Blending demo",
self.text_title = pyglet.text.Label("Blending and filtering demo",
font_size=32,
x=5,
y=director.get_window_size()[1],
Expand Down Expand Up @@ -57,6 +57,16 @@ def __init__( self, index=1 ):
self.sprite2.y = director.get_window_size()[0] / 2
self.add( self.sprite2 )

self.sprite3 = Sprite(self.image, blend_mode=Sprite.BLEND_STANDARD, filter_mode=Sprite.FILTER_NONE)
self.sprite3.x = director.get_window_size()[0] / 2 - 100
self.sprite3.y = director.get_window_size()[0] / 2 - 150
self.add( self.sprite3 )

self.sprite4 = Sprite(self.image, blend_mode=Sprite.BLEND_ADDITIVE, filter_mode=Sprite.FILTER_NONE)
self.sprite4.x = director.get_window_size()[0] / 2 + 100
self.sprite4.y = director.get_window_size()[0] / 2 - 150
self.add( self.sprite4 )

def on_key_release( self, keys, mod ):
# LEFT: go to previous scene
# RIGTH: go to next scene
Expand Down

0 comments on commit 731755e

Please sign in to comment.