Commits

Anonymous committed 7231ae4

add "new_" to rect(), surface(), and event().
timer can handle multiple timers now too

Comments (0)

Files changed (16)

 
 
 Nov 15, 2000
- BREAK:	Moved pygame.music to a member of pygame.mixer
- BREAK:	Surface.blit takes a source rectangle instead of pos, size
- BUG:	pygame.display.set_clip() correctly accepts rectstyle arg
+	time.set_timer() now handles multiple timers
+	rect(), surface(), event() renamed to \
+		new_rect, new_surface(), new_event() [BREAK]
+	all new_XX() functions were added to pygame.locals
+	Moved pygame.music to a member of pygame.mixer [BREAK]
+	Surface.blit takes a source rectangle instead of pos, size [BREAK]
+ 	pygame.display.set_clip() correctly accepts rectstyle arg [BUG]
 	Added Surface.get_flags() and Surface.get_pitch()
 	Added set_cursor and get_cursor to pygame.mouse
 	New module, pygame.cursors, contains sample cursors
 	surfarray.blit_array() supports all bit depths
 
 Nov 11, 2000
- BUG:	pygame.display.set_mode() uses int, not short, for size
+ 	pygame.display.set_mode() uses int, not short, for size [BUG]
 
 Nov 10, 2000
 	Committed to CVS
 
 
 
- BREAK:	change breaks existing code
- BUG:	fixed a bug that was crashing
+BREAK = change breaks existing code
+BUG   = fixed a bug that was crashing

docs/ref/Surface.html

 this is done, the map_rgb() and unmap_rgb() will convert back and
 forth between the packed pixel values for you. Information on how
 the pixels are packed can be retreived from the get_masks(),
-get_losses() and get_shifts() routines.
-<br>&nbsp;<br>
-Here is the quick breakdown of how these work (don't worry if you
-don't quite understand this, it is only here for informational
+get_losses() and get_shifts() routines.<p>&nbsp;</p>Here is the quick breakdown of how packed pixels work (don't worry if
+you don't quite understand this, it is only here for informational
 purposes, it is not needed). Each colorplane mask can be used to
 isolate the values for a colorplane from the packed pixel color.
 Therefore PACKED_COLOR & RED_MASK == REDPLANE. Note that the
 colorplane and the actual color for that plane. Here are the
 final formulas used be map and unmap (not exactly, heh).
 PACKED_COLOR = RED>>losses[0]<<shifts[0] |
-GREEN>>losses[1]<<shifts[1] | BLUE>>losses[2]<<shifts[2]
+      GREEN>>losses[1]<<shifts[1] | BLUE>>losses[2]<<shifts[2]
 RED = PACKED_COLOR & masks[0] >> shifts[0] << losses[0]
 GREEN = PACKED_COLOR & masks[1] >> shifts[1] << losses[1]
 BLUE = PACKED_COLOR & masks[2] >> shifts[2] << losses[2]
 autoinitialize all imported pygame modules</td></tr>
 
 
+<tr><td><a href=#new_rect>new_rect</a></td><td> -
+create a new rectangle</td></tr>
+
+
+<tr><td><a href=#new_surface>new_surface</a></td><td> -
+Surface</td></tr>
+
+
 <tr><td><a href=#quit>quit</a></td><td> -
 uninitialize all pygame modules</td></tr>
 
 
-<tr><td><a href=#rect>rect</a></td><td> -
-create a new rectangle</td></tr>
-
-
 <tr><td><a href=#register_quit>register_quit</a></td><td> -
 routine to call when pyGame quits</td></tr>
 
 
-<tr><td><a href=#surface>surface</a></td><td> -
-Surface</td></tr>
-
-
 </table>
 
 <hr>
 since the.
 </ul><br>&nbsp;<br>
 
-<a name=quit><font size=+2><b>quit
+<a name=new_rect><font size=+2><b>new_rect
 </b></font><br><font size=+1><tt>
-pygame.quit() -> none
-</tt></font><ul>
-Uninitialize all pygame modules that have been initialized. Even
-if you initialized the module by hand, this <u>quit()</u> will
-uninitialize it for you.
-<br>&nbsp;<br>
-All the pygame modules are uninitialized automatically when your
-program exits, so you will usually not need this routine. If you
-program plans to keep running after it is done with pygame, then
-would be a good time to make this call.
-</ul><br>&nbsp;<br>
-
-<a name=rect><font size=+2><b>rect
-</b></font><br><font size=+1><tt>
-pygame.rect(rectstyle) -> Rect
+pygame.new_rect(rectstyle) -> Rect
 </tt></font><ul>
 Creates a new rectangle object. The given
 rectstyle represents one of the various ways of
 corner, and the width and height.
 </ul><br>&nbsp;<br>
 
-<a name=register_quit><font size=+2><b>register_quit
+<a name=new_surface><font size=+2><b>new_surface
 </b></font><br><font size=+1><tt>
-pygame.register_quit(callback) -> None
-</tt></font><ul>
-The given callback routine will be called when. pygame is
-quitting. Quit callbacks are served on a 'last in, first out'
-basis. Also be aware that your callback may be called more than
-once.
-</ul><br>&nbsp;<br>
-
-<a name=surface><font size=+2><b>surface
-</b></font><br><font size=+1><tt>
-pygame.surface(size, [flags, [depth|Surface, [masks]]]) ->
+pygame.new_surface(size, [flags, [depth|Surface, [masks]]]) ->
 </tt></font><ul>
 <br>&nbsp;<br>
 Creates a new surface object. Size is a 2-int-sequence containing
 for the flag.
 </ul><br>&nbsp;<br>
 
+<a name=quit><font size=+2><b>quit
+</b></font><br><font size=+1><tt>
+pygame.quit() -> none
+</tt></font><ul>
+Uninitialize all pygame modules that have been initialized. Even
+if you initialized the module by hand, this <u>quit()</u> will
+uninitialize it for you.
+<br>&nbsp;<br>
+All the pygame modules are uninitialized automatically when your
+program exits, so you will usually not need this routine. If you
+program plans to keep running after it is done with pygame, then
+would be a good time to make this call.
+</ul><br>&nbsp;<br>
+
+<a name=register_quit><font size=+2><b>register_quit
+</b></font><br><font size=+1><tt>
+pygame.register_quit(callback) -> None
+</tt></font><ul>
+The given callback routine will be called when. pygame is
+quitting. Quit callbacks are served on a 'last in, first out'
+basis. Also be aware that your callback may be called more than
+once.
+</ul><br>&nbsp;<br>
+
 
 <hr>
 

docs/ref/pygame_event.html

 <hr>
 
 <table>
-<tr><td><a href=#event>event</a></td><td> -
-create new event object</td></tr>
-
-
 <tr><td><a href=#event_name>event_name</a></td><td> -
 name for event type</td></tr>
 
 get all of an event type from the queue</td></tr>
 
 
+<tr><td><a href=#new_event>new_event</a></td><td> -
+create new event object</td></tr>
+
+
 <tr><td><a href=#peek>peek</a></td><td> -
 query if any of event types are waiting</td></tr>
 
 
 <hr>
 
-<a name=event><font size=+2><b>event
-</b></font><br><font size=+1><tt>
-pygame.event.event(type, dict) -> Event
-</tt></font><ul>
-Creates a new event object. The type should be one
-of SDL's event numbers, or above USER_EVENT. The
-given dictionary contains a list of readonly
-attributes that will be members of the event
-object.
-</ul><br>&nbsp;<br>
-
 <a name=event_name><font size=+2><b>event_name
 </b></font><br><font size=+1><tt>
 pygame.event.event_name(event type) -> string
 would call, 'pygame.event.get([KEYDOWN,KEYUP])'.
 </ul><br>&nbsp;<br>
 
+<a name=new_event><font size=+2><b>new_event
+</b></font><br><font size=+1><tt>
+pygame.event.new_event(type, dict) -> Event
+</tt></font><ul>
+Creates a new event object. The type should be one
+of SDL's event numbers, or above USER_EVENT. The
+given dictionary contains a list of readonly
+attributes that will be members of the event
+object.
+</ul><br>&nbsp;<br>
+
 <a name=peek><font size=+2><b>peek
 </b></font><br><font size=+1><tt>
 pygame.event.peek([type]) -> bool

docs/ref/pygame_time.html

 pygame.time.delay(millseconds) -> none
 </tt></font><ul>
 Will pause for a given number of milliseconds.
+The maximum resolution of this delay is 10 milliseconds. The
+time you request to delay will be truncated down to the nearest
+10 milliseconds. This will help <u>delay()</u> return a little before
+the requested time has passed, instead of a little afterwards.
 </ul><br>&nbsp;<br>
 
 <a name=get_ticks><font size=+2><b>get_ticks
 
 <a name=set_timer><font size=+2><b>set_timer
 </b></font><br><font size=+1><tt>
-pygame.time.set_timer([millseconds, eventid]) -> int
+pygame.time.set_timer(eventid, milliseconds) -> int
 </tt></font><ul>
-Every given number of milliseconds, a new event with
-the given event id will be placed on the event queue.
-The timer will run indefinitely, until <u>set_timer()</u> is
-called with no arguments. Calling with no arguments stops
-the running timer.
+Every event id can have a timer attached to it. Calling
+this will set the timer in milliseconds for that event.
+setting milliseconds to 0 or less will disable that timer.
+When a timer for an event is set, that event will be
+placed on the event queue every given number of
+milliseconds.
 </ul><br>&nbsp;<br>
 
 
 ALIEN_RELOAD   = 12
 EXPLODE_TIME   = 40
 MAX_EXPLOSIONS = 4
-SCREENRECT     = rect(0, 0, 640, 480)
+SCREENRECT     = new_rect(0, 0, 640, 480)
 ANIMCYCLE      = 18
 PLODECYCLE     = 7
 BULLET_OFFSET  = 11
     Snd.shot = load_sound('car_door.wav')
 
     # Create the background
-    background = pygame.surface(SCREENRECT.size)
+    background = new_surface(SCREENRECT.size)
     for x in range(0, SCREENRECT.width, Img.background.get_width()):
         background.blit(Img.background, (x, 0))
     screen.blit(background, (0,0))
         fontname = os.path.join('data', 'billbrdi.ttf')
         f = font.font(fontname, 100)
         text = f.render('Game Over', 1, (200, 200, 200))
-        textrect = rect((0, 0), text.get_size())
+        textrect = new_rect((0, 0), text.get_size())
         textrect.center = SCREENRECT.center
         screen.blit(text, textrect.topleft)
         pygame.display.flip()

examples/data/boom.wav

Binary file modified.

examples/data/explode.wav

Binary file removed.
 ALIEN_SPEED    = 6
 ALIEN_ODDS     = 45
 EXPLODE_TIME   = 6
-SCREENRECT     = rect(0, 0, 640, 480)
+SCREENRECT     = new_rect(0, 0, 640, 480)
 
 
 #some globals for friendly access
     Img.explosion = load_image('explosion1.gif', 1)
 
     # Create the background
-    background = pygame.surface(SCREENRECT.size)
+    background = new_surface(SCREENRECT.size)
     for x in range(0, SCREENRECT.width, Img.background.get_width()):
         background.blit(Img.background, (x, 0))
     screen.blit(background, (0,0))
     screen = pygame.display.set_mode(size)
 
     pygame.event.set_blocked(MOUSEMOTION) #keep our queue cleaner
-    pygame.time.set_timer(500, USEREVENT)
+    pygame.time.set_timer(USEREVENT, 500)
 
     while 1:
         event = pygame.event.poll()
 the local namespace for your module"""
 
 from constants import *
-from rect import rect
+from rect import new_rect
+from surface import new_surface
+from event import new_event
 	(hashfunc)NULL,			/*hash*/
 	(ternaryfunc)NULL,		/*call*/
 	(reprfunc)NULL,			/*str*/
+	0L,0L,0L,0L,
+	doc_pygame_event_EXTRA /* Documentation string */
 };
 
 
 /* event module functions */
 
 
-    /*DOC*/ static char doc_event[] =
-    /*DOC*/    "pygame.event.event(type, dict) -> Event\n"
+    /*DOC*/ static char doc_new_event[] =
+    /*DOC*/    "pygame.event.new_event(type, dict) -> Event\n"
     /*DOC*/    "create new event object\n"
     /*DOC*/    "\n"
     /*DOC*/    "Creates a new event object. The type should be one\n"
     /*DOC*/    "object.\n"
     /*DOC*/ ;
 
-static PyObject* event(PyObject* self, PyObject* arg)
+static PyObject* new_event(PyObject* self, PyObject* arg)
 {
 	PyObject* dict;
 	int type;
 
 static PyMethodDef event_builtins[] =
 {
-	{ "event", event, 1, doc_event },
+	{ "new_event", new_event, 1, doc_new_event },
 	{ "event_name", event_name, 1, doc_event_name },
 
 	{ "set_grab", set_grab, 1, doc_set_grab },
 	0,
 	sound_dealloc,	
 	0,
-	sound_getattr
+	sound_getattr,
+	NULL,					/*setattr*/
+	NULL,					/*compare*/
+	NULL,					/*repr*/
+	NULL,					/*as_number*/
+	NULL,					/*as_sequence*/
+	NULL,					/*as_mapping*/
+	(hashfunc)NULL,			/*hash*/
+	(ternaryfunc)NULL,		/*call*/
+	(reprfunc)NULL,			/*str*/
+	0L,0L,0L,0L,
+	doc_Sound_MODULE /* Documentation string */
 };
 
 
 
-
-
 /* channel object methods */
 
 
 	0,
 	channel_dealloc,	
 	0,
-	channel_getattr
+	channel_getattr,
+	NULL,					/*setattr*/
+	NULL,					/*compare*/
+	NULL,					/*repr*/
+	NULL,					/*as_number*/
+	NULL,					/*as_sequence*/
+	NULL,					/*as_mapping*/
+	(hashfunc)NULL,			/*hash*/
+	(ternaryfunc)NULL,		/*call*/
+	(reprfunc)NULL,			/*str*/
+	0L,0L,0L,0L,
+	doc_Channel_MODULE /* Documentation string */
 };
 
 
 
-
-
 /*mixer module methods*/
 
     /*DOC*/ static char doc_get_num_channels[] =
 
 /*module globals*/
 
-    /*DOC*/ static char doc_rect_new[] =
-    /*DOC*/    "pygame.rect(rectstyle) -> Rect\n"
+    /*DOC*/ static char doc_new_rect[] =
+    /*DOC*/    "pygame.new_rect(rectstyle) -> Rect\n"
     /*DOC*/    "create a new rectangle\n"
     /*DOC*/    "\n"
     /*DOC*/    "Creates a new rectangle object. The given\n"
     /*DOC*/    "corner, and the width and height.\n"
     /*DOC*/ ;
 
-static PyObject* rect_new(PyObject* self, PyObject* args)
+static PyObject* new_rect(PyObject* self, PyObject* args)
 {
 	GAME_Rect *argrect, temp;
 	if(!(argrect = GameRect_FromObject(args, &temp)))
 
 static PyMethodDef rect__builtins__[] =
 {
-	{ "rect", rect_new, 1, doc_rect_new }, 
+	{ "new_rect", new_rect, 1, doc_new_rect }, 
 	{NULL, NULL}
 };
 
     /*DOC*/    "forth between the packed pixel values for you. Information on how\n"
     /*DOC*/    "the pixels are packed can be retreived from the get_masks(),\n"
     /*DOC*/    "get_losses() and get_shifts() routines.\n"
-    /*DOC*/    "\n"
-    /*DOC*/    "Here is the quick breakdown of how these work (don't worry if you\n"
-    /*DOC*/    "don't quite understand this, it is only here for informational\n"
+    /*DOC*/ ;
+#if 0 /*extra help, only for docs*/
+    /*DOC*/ static char doc_Surface_EXTRA[] =
+    /*DOC*/    "Here is the quick breakdown of how packed pixels work (don't worry if\n"
+    /*DOC*/    "you don't quite understand this, it is only here for informational\n"
     /*DOC*/    "purposes, it is not needed). Each colorplane mask can be used to\n"
     /*DOC*/    "isolate the values for a colorplane from the packed pixel color.\n"
     /*DOC*/    "Therefore PACKED_COLOR & RED_MASK == REDPLANE. Note that the\n"
     /*DOC*/    "colorplane and the actual color for that plane. Here are the\n"
     /*DOC*/    "final formulas used be map and unmap (not exactly, heh).\n"
     /*DOC*/    "PACKED_COLOR = RED>>losses[0]<<shifts[0] |\n"
-    /*DOC*/    "GREEN>>losses[1]<<shifts[1] | BLUE>>losses[2]<<shifts[2]\n"
+    /*DOC*/    "      GREEN>>losses[1]<<shifts[1] | BLUE>>losses[2]<<shifts[2]\n"
     /*DOC*/    "RED = PACKED_COLOR & masks[0] >> shifts[0] << losses[0]\n"
     /*DOC*/    "GREEN = PACKED_COLOR & masks[1] >> shifts[1] << losses[1]\n"
     /*DOC*/    "BLUE = PACKED_COLOR & masks[2] >> shifts[2] << losses[2]\n"
     /*DOC*/    "There is also an alpha channel for some Surfaces. The alpha\n"
     /*DOC*/    "channel works this same exact way, and the map_rgba() and\n"
     /*DOC*/    "unmap_rgba() functions can be used to do the conversion for you.\n"
-    /*DOC*/ ;
+#endif
 
 
 static PyTypeObject PySurface_Type =
 	(hashfunc)NULL,			/*hash*/
 	(ternaryfunc)NULL,		/*call*/
 	(reprfunc)NULL,			/*str*/
+	0L,0L,0L,0L,
+	doc_Surface_MODULE /* Documentation string */
 };
 
 
 
 /* surface module functions */
 
-    /*DOC*/ static char doc_surface[] =
-    /*DOC*/    "pygame.surface(size, [flags, [depth|Surface, [masks]]]) ->\n"
+    /*DOC*/ static char doc_new_surface[] =
+    /*DOC*/    "pygame.new_surface(size, [flags, [depth|Surface, [masks]]]) ->\n"
     /*DOC*/    "Surface\n"
     /*DOC*/    "create a new Surface\n"
     /*DOC*/    "\n"
     /*DOC*/    "for the flag.\n"
     /*DOC*/ ;
 
-static PyObject* surface(PyObject* self, PyObject* arg)
+static PyObject* new_surface(PyObject* self, PyObject* arg)
 {
 	Uint32 flags = 0;
 	int width, height;
 
 static PyMethodDef surface_builtins[] =
 {
-	{ "surface", surface, 1, doc_surface },
+	{ "new_surface", new_surface, 1, doc_new_surface },
 	{ NULL, NULL }
 };
 
 #include "pygame.h"
 
 
-static int timer_event = SDL_NOEVENT;
-static Uint32 timer_callback(Uint32 interval)
+static SDL_TimerID event_timers[SDL_NUMEVENTS] = {NULL};
+
+static Uint32 timer_callback(Uint32 interval, void* param)
 {
 	if(SDL_WasInit(SDL_INIT_VIDEO))
 	{
-		SDL_Event event = {timer_event};
+		SDL_Event event = {(int)param};
 		SDL_PushEvent(&event);
 	}
 	return interval;
     /*DOC*/    "delay for a number of milliseconds\n"
     /*DOC*/    "\n"
     /*DOC*/    "Will pause for a given number of milliseconds.\n"
+    /*DOC*/    "The maximum resolution of this delay is 10 milliseconds. The\n"
+    /*DOC*/    "time you request to delay will be truncated down to the nearest\n"
+    /*DOC*/    "10 milliseconds. This will help delay() return a little before\n"
+    /*DOC*/    "the requested time has passed, instead of a little afterwards.\n"
     /*DOC*/ ;
 
 static PyObject* delay(PyObject* self, PyObject* arg)
 		return NULL;
 
 	Py_BEGIN_ALLOW_THREADS
-	SDL_Delay(ticks);
+	SDL_Delay((ticks/10)*10);
 	Py_END_ALLOW_THREADS
 
 	RETURN_NONE
 
 
     /*DOC*/ static char doc_set_timer[] =
-    /*DOC*/    "pygame.time.set_timer([millseconds, eventid]) -> int\n"
+    /*DOC*/    "pygame.time.set_timer(eventid, milliseconds) -> int\n"
     /*DOC*/    "control timer events\n"
     /*DOC*/    "\n"
-    /*DOC*/    "Every given number of milliseconds, a new event with\n"
-	/*DOC*/	   "the given event id will be placed on the event queue.\n"
-    /*DOC*/    "The timer will run indefinitely, until set_timer() is\n"
-    /*DOC*/    "called with no arguments. Calling with no arguments stops\n"
-    /*DOC*/    "the running timer.\n"
+    /*DOC*/    "Every event id can have a timer attached to it. Calling\n"
+    /*DOC*/    "this will set the timer in milliseconds for that event.\n"
+    /*DOC*/    "setting milliseconds to 0 or less will disable that timer.\n"
+    /*DOC*/    "When a timer for an event is set, that event will be\n"
+    /*DOC*/    "placed on the event queue every given number of\n"
+    /*DOC*/    "milliseconds.\n"
     /*DOC*/ ;
 
 static PyObject* set_timer(PyObject* self, PyObject* arg)
 {
+	SDL_TimerID newtimer;
 	int ticks = 0, event = SDL_NOEVENT;
-	if(!PyArg_ParseTuple(arg, "ii", &ticks, &event))
+	if(!PyArg_ParseTuple(arg, "ii", &event, &ticks))
 		return NULL;
 
+	if(event <= SDL_NOEVENT || event >= SDL_NUMEVENTS)
+		return RAISE(PyExc_ValueError, "Event id must be between NOEVENT(0) and NUMEVENTS(32)");
+
+	/*stop original timer*/
+	if(event_timers[event])
+	{
+		SDL_RemoveTimer(event_timers[event]);
+		event_timers[event] = NULL;
+	}
+
+	if(ticks <= 0)
+		RETURN_NONE
 
 	/*just doublecheck that timer is initialized*/
 	if(!SDL_WasInit(SDL_INIT_TIMER))
 			return RAISE(PyExc_SDLError, SDL_GetError());
 	}
 
-	if(!ticks || event == SDL_NOEVENT)
-		SDL_SetTimer(0, NULL);
-	else
-	{
-		timer_event = event;
-		SDL_SetTimer(ticks, timer_callback);
-	}
+	newtimer = SDL_AddTimer((ticks/10)*10, timer_callback, (void*)event);
+	if(!newtimer)
+		return RAISE(PyExc_SDLError, SDL_GetError());
+	event_timers[event] = newtimer;
 
 	RETURN_NONE
 }