@@ -15,13 +15,12 @@ cdef extern from "pysfml/graphics/DerivableDrawable.hpp":
1515 cdef cppclass DerivableDrawable:
1616 DerivableDrawable(object )
1717
18+ const Uint8* getPixelsPtr(object )
19+
1820cdef extern from " pysfml/graphics/DerivableRenderWindow.hpp" :
1921 cdef cppclass DerivableRenderWindow:
2022 DerivableRenderWindow(object )
2123
22- cdef extern from * :
23- ctypedef void * PyUnicodeObject
24-
2524from libc.stdlib cimport malloc, free
2625
2726__all__ = [' BlendMode' , ' PrimitiveType' , ' Color' , ' Rect' , ' Transform' ,
@@ -45,8 +44,7 @@ from pysfml.system cimport Vector2, Vector3
4544from pysfml.system cimport to_vector2i, to_vector2f
4645from pysfml.system cimport to_string, wrap_string
4746from pysfml.system cimport popLastErrorMessage, import_sfml__system
48- from pysfml.window cimport VideoMode, ContextSettings, Pixels, Window
49- from pysfml.window cimport wrap_pixels
47+ from pysfml.window cimport VideoMode, ContextSettings, Window
5048
5149import_sfml__system()
5250
@@ -484,6 +482,22 @@ cdef public class Image[type PyImageType, object PyImageObject]:
484482 p[0 ] = self .p_this[0 ]
485483 return wrap_image(p)
486484
485+ def __getbuffer__ (self , Py_buffer *buffer , int flags ):
486+ buffer .buf = < char * > self .p_this.getPixelsPtr()
487+ buffer .format = ' c'
488+ buffer .internal = NULL
489+ buffer .itemsize = 1
490+ buffer .len = self .p_this.getSize().x * self .p_this.getSize().y * 4
491+ buffer .ndim = 1
492+ buffer .obj = self
493+ buffer .readonly = 1
494+ buffer .shape = NULL
495+ buffer .strides = NULL
496+ buffer .suboffsets = NULL
497+
498+ def __releasebuffer__ (self , Py_buffer *buffer ):
499+ pass
500+
487501 @classmethod
488502 def create (cls , unsigned int width , unsigned int height , Color color = None ):
489503 cdef sf.Image * p = new sf.Image()
@@ -507,15 +521,12 @@ cdef public class Image[type PyImageType, object PyImageObject]:
507521 return wrap_image(p)
508522
509523 @classmethod
510- def from_pixels (cls , Pixels pixels ):
511- cdef sf.Image * p
512-
513- if pixels.p_array != NULL :
514- p = new sf.Image()
515- p.create(pixels.m_width, pixels.m_height, pixels.p_array)
516- return wrap_image(p)
524+ def from_pixels (cls , int width , int height , pixels ):
525+ cdef sf.Image * p = new sf.Image()
526+ cdef const sf.Uint8* pixels_ptr = getPixelsPtr(memoryview(pixels))
517527
518- raise ValueError (" Failed to create texture, invalid array (NULL)" )
528+ p.create(width, height, pixels_ptr)
529+ return wrap_image(p)
519530
520531 @classmethod
521532 def from_file (cls , filename ):
@@ -575,8 +586,7 @@ cdef public class Image[type PyImageType, object PyImageObject]:
575586
576587 property pixels :
577588 def __get__ (self ):
578- if self .p_this.getPixelsPtr():
579- return wrap_pixels(< Uint8* > self .p_this.getPixelsPtr(), self .width, self .height)
589+ return memoryview(self )
580590
581591 def flip_horizontally (self ):
582592 self .p_this.flipHorizontally()
@@ -722,16 +732,7 @@ cdef public class Texture[type PyTextureType, object PyTextureObject]:
722732 if len (args) > 2 :
723733 raise UserWarning (" Too much arguments provided. It requires at most two." )
724734
725- if type (args[0 ]) is Pixels:
726- if len (args) == 2 :
727- if type (args[1 ]) in [Vector2, tuple ]:
728- self .update_from_pixels(args[0 ], args[1 ])
729- else :
730- raise UserWarning (" The second argument must be either a sf.Vector2 or a tuple" )
731- else :
732- self .update_from_pixels(args[0 ])
733-
734- elif type (args[0 ]) is Image:
735+ if type (args[0 ]) is Image:
735736 if len (args) == 2 :
736737 if type (args[1 ]) in [Vector2, tuple ]:
737738 self .update_from_image(args[0 ], args[1 ])
@@ -752,13 +753,14 @@ cdef public class Texture[type PyTextureType, object PyTextureObject]:
752753 else :
753754 raise UserWarning (" The first argument must be either sf.Pixels, sf.Image or sf.Window" )
754755
756+ def update_from_pixels (self , pixels , *args ):
757+ cdef const sf.Uint8* pixels_ptr = getPixelsPtr(memoryview(pixels))
755758
756- def update_from_pixels (self , Pixels pixels , position = None ):
757- if not position:
758- self .p_this.update(pixels.p_array)
759+ if not args:
760+ self .p_this.update(pixels_ptr)
759761 else :
760- x, y = position
761- self .p_this.update(pixels.p_array, pixels.m_width, pixels.m_height, < unsigned int > x, < unsigned int > y)
762+ width, height, x, y = args
763+ self .p_this.update(pixels_ptr, width, height, x, y)
762764
763765 def update_from_image (self , Image image , position = None ):
764766 if not position:
0 commit comments