[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Math.Vector2()



Hi List,

I think it's a bug in the lib.
The inplace operators didn't increment the reference count.
But according to the Docs [1] they should return a "New reference".
patch included.

cheers,
Lorenz

[1] http://docs.python.org/2/c-api/number.html#PyNumber_InPlaceAdd


On 07/12/2013 02:18 AM, Aikiman wrote:
heres the code, just one class and main. Ive commented out the problem code
so you need to uncomment to see the errors. Like I said anything where
vec*=vec or vec+=vec causes malloc errors. Thanks for helping.

import pygame, math, random

pygame.init()

class Circle(pygame.sprite.Sprite):
     def __init__(self, screen):
         pygame.sprite.Sprite.__init__(self)
         self.screen = screen
         self.position =
pygame.math.Vector2(random.randrange(20,self.screen.get_width()),
self.screen.get_height()/3)
         self.velocity = pygame.math.Vector2(0.0, 0.0)
         self.acceleration = pygame.math.Vector2(0.0, 0.1)
         self.netForce =  pygame.math.Vector2(0.0, 0.0)
         self.x = random.randrange(20,self.screen.get_width())
         self.y = self.screen.get_height()/2
         self.radius = random.randrange(5,30)
         self.image = pygame.Surface((self.radius*2,self.radius*2))
         self.image.set_colorkey((0,0,0))
         self.image.set_alpha(120)
         self.mass = self.radius/15.0
         pygame.draw.circle(self.image, (175,255,0),
(self.radius,self.radius), self.radius)
         self.image = self.image.convert_alpha()
         self.rect = self.image.get_rect()
         self.rect.center = self.position

     def update(self):
         self.calcPos()
         self.checkBounds()
         self.rect.center = self.position
         #self.netForce *= 0.0


     def calcPos(self):
         self.acceleration = self.netForce
         #self.velocity += self.acceleration
         #self.position += self.velocity


     def applyForce(self, force):
         force /self.mass
         #self.netForce += force

     def checkBounds(self):
         if self.position[1] > self.screen.get_height():
             self.acceleration[1] *= -1.0
             self.position[1] = self.screen.get_height()
         if self.position[0] > self.screen.get_width():
             self.acceleration[0] *= -1.0
             self.position[0] = self.screen.get_width()
         if self.position[1] < 0:
             self.acceleration[1] *= -1.0
         if self.position[0] < 0:
             self.acceleration[0] *= -1.0

def main():
     screen = pygame.display.set_mode((600,400))
     background = pygame.Surface((screen.get_size()))
     background.fill((150,150,150))
     background = background.convert()

     circleGRP = pygame.sprite.Group() #Add balls
     for x in range(10):
         circleGRP.add(Circle(screen))

     wind = pygame.math.Vector2(1.0, 0)
     gravity = pygame.math.Vector2(0, 1.0)

     clock = pygame.time.Clock()
     mainLoop = True

     while mainLoop:
         clock.tick(30) #Clock
         for event in pygame.event.get(): #Key events
             if event.type == pygame.QUIT:
                 mainLoop = False
             elif event.type == pygame.KEYDOWN:
                 if event.key == pygame.K_ESCAPE:
                     mainLoop = False
             elif event.type == pygame.MOUSEBUTTONDOWN: #Add wind
                 if pygame.mouse.get_pressed()[0]:
                     for circle in circleGRP:
                         circle.applyForce(wind)

#----------------------------------------------------------------------------
         for circle in circleGRP: #Add gravity
             gravity = gravity * circle.mass
             circle.applyForce(gravity)
             #pass

             #circleX = circle.dx * -1 #Add drag
             #circleY = circle.dy * -1
             #drag = (circleX/80* circle.mass* (circle.radius/5), circleY/80*
circle.mass* (circle.radius/5))
             #circle.applyForce(drag)

#----------------------------------------------------------------------------
         circleGRP.update()
         screen.blit(background, (0,0))
         circleGRP.draw(screen)
         pygame.display.flip()

     pygame.quit()

if __name__ == "__main__":
     main()




--
View this message in context: http://pygame-users.25799.x6.nabble.com/pygame-Math-Vector2-tp798p801.html
Sent from the pygame-users mailing list archive at Nabble.com.


diff -r 62f61425b394 src/math.c
--- a/src/math.c	Sun Jul 28 20:44:26 2013 -0700
+++ b/src/math.c	Tue Aug 13 15:39:45 2013 +0200
@@ -542,8 +542,10 @@
     else
         op |= OP_ARG_UNKNOWN;
 
-    if (op & OP_INPLACE)
+    if (op & OP_INPLACE) {
         ret = vec;
+        Py_INCREF(ret);
+    }
     else if (op != (OP_MUL | OP_ARG_VECTOR) &&
              op != (OP_MUL | OP_ARG_VECTOR | OP_ARG_REVERSE)) {
         ret = (PyVector*)PyVector_NEW(dim);
@@ -606,9 +608,7 @@
             ret->coords[i] = floor(vec_coords[i] * tmp);
         break;
     default:
-        if (!(op & OP_INPLACE)) {
-            Py_XDECREF(ret);
-        }
+        Py_XDECREF(ret);
         Py_INCREF(Py_NotImplemented);
         return Py_NotImplemented;
     }