From: Mark B. <mb...@0x...> - 2000-06-03 17:40:05
|
On Sat, 03 Jun 2000, "David Clark wrote: > My understanding is that the following minimal test program should > work. Unfortunately, it doesn't. > > #! /usr/bin/env python > > import sdl > sdl.init(sdl.INIT_VIDEO) > video = sdl.video_set_mode(200,200,16,sdl.SWSURFACE) > video[100][100] = video.map_rgb((255,255,255)) > video.update_rect((0,0,0,0)) > while 1: > pass > > This should plot a white pixel at the center of the surface, but no > white pixel appears. This is strange; I remember testing this back > when it was introduced in 0.0.3, and it worked fine then. Am I doing > something wrong? This is what you would intuitively expect, I imagine, but it's never worked this way. video[100] this returns a list of of the pixels at 101 video[100][100] indexes the 101st element of the list returned from video[100] So when you do video[100][100] = value, what you're doing is assigning value to this element in the list returned by video[100]. However, this list doesn't directly map to the memory used by video. In order to get the behavior you're looking for, you'd need to do something like: y_list = video[100] y_list[100] = video.map_rgb((255, 255, 255)) video[100] = y_list Cleary for one pixel, it's advantegous to use set_at. For linear access cases, it's faster to do stuff like: y = 0 for y_list in video: for x in y_list: x = get_some_color() video[y] = y_list y = y + 1 For less linear access cases, but still in loop, it's better to do something like: s_copy = video[:] some_loop_construct: s_copy[y][x] = some_color x = some_number() y = some_number() s_copy[y][x] = another_color video[:] = s_copy Generally speaking, if you can get your code into a for x in some_range it'll be faster. If you can't, then it's a good idea to copy the buffer, and then copy it back when you're done. If you're not doing a lot of drawing, set_at is probably a good idea. |