Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

In pyglet, I want to create an image buffer in memory, then set the bytes manually, then draw it. I tried making a 3x3 red square like this in my draw() function:

imageData = pyglet.image.ImageData(3, 3, 'RGB', [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0 ])
imageData.blit(10, 10)

...but at runtime, Python complains:

ctypes.ArgumentError: argument 9: <type 'exceptions.TypeError'>: wrong type

Is this the right approach? Am I missing a step? How can I fix this?

share|improve this question
1  
Are you sure the error corresponds with the code you have posted above? I do not know python but I don't see anything you have posted above with 9 arguments. AFAIK it looks like one of your arguments are not using the correct type. –  Grey May 21 '13 at 23:53
    
I agree with Code Assassin. The error seems to have to do with what you typed, wrong arguments? –  CPP_Person May 22 '13 at 0:16
1  
Well, I guess I am just assuming it is the problem as the error doesn't show up if I comment those lines... pastebin.com/iveDhYcB is the full source –  Mossen May 22 '13 at 0:38
add comment

1 Answer

Yes, you can do this, but as the error tells you, you must use ctypes instead of Python types for it.

RGB values are unsigned byte triplets, which means c_ubyte in ctypes, also available in pyglet.gl as GL_ubyte. So you can do this:

pixels = [
    255, 0, 0,      0, 255, 0,      0, 0, 255,     # RGB values range from
    255, 0, 0,      255, 0, 0,      255, 0, 0,     # 0 to 255 for each color
    255, 0, 0,      255, 0, 0,      255, 0, 0,     # component.
]
rawData = (GLubyte * len(pixels))(*pixels)
imageData = pyglet.image.ImageData(3, 3, 'RGB', rawData)

The above is the recommended ctypes syntax, which generates an array of GLubyte with the right size and then populates it with your pixel data.

share|improve this answer
    
I've changed your colors a little from all red to illustrate the pixel order on rendering. –  Ingmar Hupp Nov 12 '13 at 20:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.