1. pygame
  2. Untitled project
  3. pygame

Commits

pygame  committed 7fc3cab

import fixes

  • Participants
  • Parent commits 29ac8f0
  • Branches default

Comments (0)

Files changed (8)

File WHATSNEW

View file
 # BREAK = change breaks existing code
 # BUG	= fixed a bug that was (or could have been) crashing
 
+July 6, 2004
+    Sound object newstyle types
+    added Sound.get_length()
+
+July 5, 2004
+    Add Bo Jangeborg's cursor compile with added 'xor'
+    Add Bo Jangeborg's system cursors as compileable strings
+
 July 3, 2004
     Newstyle fonts crashing with bad filenames [BUG]
 

File lib/cursors.py

View file
   "                        ",
 )
 
+sizer_x_strings = (               #sized 24x16
+ "     X      X           ",
+ "    XX      XX          ",
+ "   X.X      X.X         ",
+ "  X..X      X..X        ",
+ " X...XXXXXXXX...X       ",
+ "X................X      ",
+ " X...XXXXXXXX...X       ",
+ "  X..X      X..X        ",
+ "   X.X      X.X         ",
+ "    XX      XX          ",
+ "     X      X           ",
+ "                        ",
+ "                        ",
+ "                        ",
+ "                        ",
+ "                        ",
+)
+sizer_y_strings = (               #sized 16x24
+ "     X          ",
+ "    X.X         ",
+ "   X...X        ",
+ "  X.....X       ",
+ " X.......X      ",
+ "XXXXX.XXXXX     ",
+ "    X.X         ",
+ "    X.X         ",
+ "    X.X         ",
+ "    X.X         ",
+ "    X.X         ",
+ "    X.X         ",
+ "    X.X         ",
+ "XXXXX.XXXXX     ",
+ " X.......X      ",
+ "  X.....X       ",
+ "   X...X        ",
+ "    X.X         ",
+ "     X          ",
+ "                ",
+ "                ",
+ "                ",
+ "                ",
+ "                ",
+)
+sizer_xy_strings = (               #sized 24x16
+ "XXXXXXXX                ",
+ "X.....X                 ",
+ "X....X                  ",
+ "X...X                   ",
+ "X..X.X                  ",
+ "X.X X.X                 ",
+ "XX   X.X    X           ",
+ "X     X.X  XX           ",
+ "       X.XX.X           ",
+ "        X...X           ",
+ "        X...X           ",
+ "       X....X           ",
+ "      X.....X           ",
+ "     XXXXXXXX           ",
+ "                        ",
+ "                        ",
+)
+textmarker_strings = (               #sized 8x16
+ "ooo ooo ",
+ "   o    ",
+ "   o    ",
+ "   o    ",
+ "   o    ",
+ "   o    ",
+ "   o    ",
+ "   o    ",
+ "   o    ",
+ "   o    ",
+ "   o    ",
+ "ooo ooo ",
+ "        ",
+ "        ",
+ "        ",
+ "        ",
+)
 
-def compile(strings, black, white):
-    """pygame.cursors.compile(strings, black, white) -> data, mask
+
+
+def compile(strings, black='X', white='.',xor='o'):
+   """pygame.cursor.compile(strings, black, white,xor) -> data, mask
 compile cursor strings into cursor data
 
 This takes a set of strings with equal length and computes
 pygame.mouse.set_cursor().
 """
 
-    #first check for consistent lengths
-    size = len(strings[0]), len(strings)
-    if size[0] % 8 or size[1] % 8:
-        raise ValueError, "cursor string sizes must be divisible by 8 "+`size`
-    for s in strings[1:]:
-        if len(s) != size[0]:
-            raise ValueError, "Cursor strings are inconsistent lengths"
+   #first check for consistent lengths
+   size = len(strings[0]), len(strings)
+   if size[0] % 8 or size[1] % 8:
+       raise ValueError, "cursor string sizes must be divisible by 8 "+`size`
+   for s in strings[1:]:
+       if len(s) != size[0]:
+           raise ValueError, "Cursor strings are inconsistent lengths"
 
-    #create the data arrays.
-    #this could stand a little optimizing
-    maskdata = []
-    filldata = []
-    maskitem = fillitem = 0
-    step = 8
-    for s in strings:
-        for c in s:
-            maskitem = maskitem << 1
-            fillitem = fillitem << 1
-            step = step - 1
-            if c == black:
-                maskitem = maskitem | 1
-            elif c == white:
-                maskitem = maskitem | 1
-                fillitem = fillitem | 1
-            if not step:
-                maskdata.append(maskitem)
-                filldata.append(fillitem)
-                maskitem = fillitem = 0
-                step = 8
-    return tuple(filldata), tuple(maskdata)
+   #create the data arrays.
+   #this could stand a little optimizing
+   maskdata = []
+   filldata = []
+   maskitem = fillitem = 0
+   step = 8
+   for s in strings:
+       for c in s:
+           maskitem = maskitem << 1
+           fillitem = fillitem << 1
+           step = step - 1
+           if c == black:
+               maskitem = maskitem | 1
+           elif c == white:
+               maskitem = maskitem | 1
+               fillitem = fillitem | 1
+           elif c == xor:
+               fillitem = fillitem | 1
+           if not step:
+               maskdata.append(maskitem)
+               filldata.append(fillitem)
+               maskitem = fillitem = 0
+               step = 8
+   return tuple(filldata), tuple(maskdata)
+
 
 
 

File readme.html

View file
     Dave Wallace, John Popplewell, Michael Urman,
     Andrew Straw, Michael Hudson, Ole Martin Bjoerndalen,
     Herv� Cauwelier, James Mazer, Lalo Martins,
-    Timothy Stranex, Chad Lester, Matthias Spillers</li>
+    Timothy Stranex, Chad Lester, Matthias Spillers,
+    Bo Jangeborg</li>
 <li>And our bug hunters above and beyond:
     Angus, Guillaume Proux, Frank Raiser,
     Austin Henry, Kaweh Kazemi, Arturo Aldama,

File readme.txt

View file
    Tyler, Nathaniel Pryce, Dave Wallace, John Popplewell, Michael Urman,
    Andrew Straw, Michael Hudson, Ole Martin Bjoerndalen, Herv� Cauwelier,
    James Mazer, Lalo Martins, Timothy Stranex, Chad Lester, Matthias
-   Spillers
+   Spillers, Bo Jangeborg
 
      And our bug hunters above and beyond: Angus, Guillaume Proux, Frank
    Raiser, Austin Henry, Kaweh Kazemi, Arturo Aldama, Mike Mulcheck, Rene

File src/ffmovie.c

View file
     }
    
     return movie;
-}
+}
+

File src/font.c

View file
 
 		/*check if it is a valid file, else SDL_ttf segfaults*/
 		test = fopen(filename, "rb");
-printf("filetest %p\n", test);
 		if(!test)
                 {
-printf("FAILED!\n");
 			PyErr_SetString(PyExc_IOError, "unable to read font filename");
                         return -1;
                 }
-printf("SUCCESS!\n");
 		fclose(test);
 
                 Py_BEGIN_ALLOW_THREADS
-printf("opening font %s %d\n", filename, fontsize);
                 font = TTF_OpenFont(filename, fontsize);
                 Py_END_ALLOW_THREADS
 	}
 	0,
 	(destructor)font_dealloc,
 	0,
-	0/*font_getattr*/, /*getattr*/
+	0, /*getattr*/
 	0,
 	0,
 	0,
 
 	if(!font)
 		return RAISE(PyExc_RuntimeError, "unable to load font.");
-printf("PyFont_New font=%p\n",font);
 	fontobj = (PyFontObject *)PyFont_Type.tp_new(&PyFont_Type, NULL, NULL);
-printf("PyFont_New fontobj=%p\n",fontobj);
 
 	if(fontobj)
 		fontobj->font = font;
 PYGAME_EXPORT
 void initfont(void)
 {
-	PyObject *module, *dict, *apiobj;
+	PyObject *module, *apiobj;
 	static void* c_api[PYGAMEAPI_FONT_NUMSLOTS];
 
 	PyFONT_C_API[0] = PyFONT_C_API[0]; /*clean an unused warning*/
 
-	PyType_Init(PyFont_Type);
         if (PyType_Ready(&PyFont_Type) < 0)
             return;
 
     /* create the module */
 	module = Py_InitModule3("font", font_builtins, doc_pygame_font_MODULE);
-	dict = PyModule_GetDict(module);
 	self_module = module;
 
-	PyDict_SetItemString(dict, "FontType", (PyObject *)&PyFont_Type);
-	PyDict_SetItemString(dict, "Font", (PyObject *)&PyFont_Type);
+        Py_INCREF((PyObject*)&PyFont_Type);
+	PyModule_AddObject(module, "FontType", (PyObject *)&PyFont_Type);
+        Py_INCREF((PyObject*)&PyFont_Type);
+	PyModule_AddObject(module, "Font", (PyObject *)&PyFont_Type);
 
 	/* export the c api */
 	c_api[0] = &PyFont_Type;
 	c_api[1] = PyFont_New;
 	c_api[2] = &font_initialized;
 	apiobj = PyCObject_FromVoidPtr(c_api, NULL);
-	PyDict_SetItemString(dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
-	Py_DECREF(apiobj);
+	PyModule_AddObject(module, PYGAMEAPI_LOCAL_ENTRY, apiobj);
 
 	/*imported needed apis*/
 	import_pygame_base();

File src/mixer.c

View file
 static int request_stereo = MIX_DEFAULT_CHANNELS;
 static int request_chunksize = MIX_DEFAULT_CHUNKSIZE;
 
+
+static int sound_init(PyObject* self, PyObject* arg, PyObject* kwarg);
+
+
 struct ChannelData
 {
     PyObject* sound;
 }
 
 
+    /*DOC*/ static char doc_snd_get_length[] =
+    /*DOC*/    "Sound.get_length() -> float\n"
+    /*DOC*/    "get the length of the Sound in seconds.\n"
+    /*DOC*/    "\n"
+    /*DOC*/    "Returns the number of seconds this Sound file has\n"
+    /*DOC*/    "of data.\n"
+    /*DOC*/ ;
 
+static PyObject* snd_get_length(PyObject* self, PyObject* args)
+{
+	Mix_Chunk* chunk = PySound_AsChunk(self);
+	int freq, channels, mixerbytes, numsamples;
+	Uint16 format;
 
-static PyMethodDef sound_builtins[] =
+	if(!PyArg_ParseTuple(args, ""))
+		return NULL;
+
+	MIXER_INIT_CHECK();
+
+	Mix_QuerySpec(&freq, &format, &channels);
+        if(format==AUDIO_S8 || format==AUDIO_U8)
+            mixerbytes = 1;
+        else
+            mixerbytes = 2;
+        numsamples = chunk->alen / mixerbytes / channels;
+
+	return PyFloat_FromDouble((float)numsamples / (float)freq);
+}
+
+
+
+static PyMethodDef sound_methods[] =
 {
 	{ "play", snd_play, 1, doc_snd_play },
 	{ "get_num_channels", snd_get_num_channels, 1, doc_snd_get_num_channels },
 	{ "set_volume", snd_set_volume, 1, doc_snd_set_volume },
 	{ "get_volume", snd_get_volume, 1, doc_snd_get_volume },
 
+        { "get_length", snd_get_length, 1, doc_snd_get_length },
+        
 	{ NULL, NULL }
 };
 
 
 /*sound object internals*/
 
-static void sound_dealloc(PyObject* self)
+static void sound_dealloc(PySoundObject* self)
 {
-    	Mix_Chunk* chunk = PySound_AsChunk(self);
-	Mix_FreeChunk(chunk);
-	PyObject_DEL(self);
+    	Mix_Chunk* chunk = PySound_AsChunk((PyObject*)self);
+        if(chunk)
+            Mix_FreeChunk(chunk);
+        if(self->weakreflist)
+            PyObject_ClearWeakRefs((PyObject*)self);
+	self->ob_type->tp_free((PyObject*)self);
 }
 
 
-static PyObject* sound_getattr(PyObject* self, char* attrname)
-{
-	return Py_FindMethod(sound_builtins, self, attrname);
-}
-
 
     /*DOC*/ static char doc_Sound_MODULE[] =
     /*DOC*/    "Sound objects represent actual sound data. Sound objects are\n"
 {
 	PyObject_HEAD_INIT(NULL)
 	0,
-	"Sound",
+	"pygame.mixer.Sound",
 	sizeof(PySoundObject),
 	0,
-	sound_dealloc,
+	(destructor)sound_dealloc,
 	0,
-	sound_getattr,
+	NULL,
 	NULL,					/*setattr*/
 	NULL,					/*compare*/
 	NULL,					/*repr*/
 	(hashfunc)NULL, 		/*hash*/
 	(ternaryfunc)NULL,		/*call*/
 	(reprfunc)NULL, 		/*str*/
-	0L,0L,0L,0L,
-	doc_Sound_MODULE /* Documentation string */
+	0L,0L,0L,
+    	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+	doc_Sound_MODULE, /* Documentation string */
+	0,					/* tp_traverse */
+	0,					/* tp_clear */
+	0,					/* tp_richcompare */
+	offsetof(PySoundObject, weakreflist),    /* tp_weaklistoffset */
+	0,					/* tp_iter */
+	0,					/* tp_iternext */
+	sound_methods,			        /* tp_methods */
+	0,				        /* tp_members */
+	0,				        /* tp_getset */
+	0,					/* tp_base */
+	0,					/* tp_dict */
+	0,					/* tp_descr_get */
+	0,					/* tp_descr_set */
+	0,					/* tp_dictoffset */
+	sound_init,			/* tp_init */
+	0,					/* tp_alloc */
+	PyType_GenericNew,	                /* tp_new */
 };
 
 
 }
 
 
-
+#if 0
     /*DOC*/ static char doc_Sound[] =
     /*DOC*/    "pygame.mixer.Sound(file) -> Sound\n"
     /*DOC*/    "load a new soundfile\n"
     /*DOC*/    "or a file-like object. The sound will be converted to match the\n"
     /*DOC*/    "current mode of the mixer.\n"
     /*DOC*/ ;
-
-static PyObject* Sound(PyObject* self, PyObject* arg)
+#endif
+static int sound_init(PyObject* self, PyObject* arg, PyObject* kwarg)
 {
-	PyObject* file, *final;
+	PyObject* file;
 	char* name = NULL;
 	Mix_Chunk* chunk;
-	SDL_RWops *rw;
-	if(!PyArg_ParseTuple(arg, "O", &file))
-		return NULL;
+printf("sound_init, start %p\n", self);
+    
+        ((PySoundObject*)self)->chunk = NULL;
 
-	MIXER_INIT_CHECK();
+        if(!PyArg_ParseTuple(arg, "O", &file))
+		return -1;
+
+        
+	if(!SDL_WasInit(SDL_INIT_AUDIO)) 
+        {
+		RAISE(PyExc_SDLError, "mixer system not initialized");
+                return -1;
+        }
+        
 	if(PyString_Check(file) || PyUnicode_Check(file))
 	{
 		if(!PyArg_ParseTuple(arg, "s", &name))
-			return NULL;
+			return -1;
 		Py_BEGIN_ALLOW_THREADS
 		chunk = Mix_LoadWAV(name);
 		Py_END_ALLOW_THREADS
 	}
 	else
 	{
+                SDL_RWops *rw;
 		if(!(rw = RWopsFromPython(file)))
-			return NULL;
+			return -1;
 		if(RWopsCheckPython(rw))
 			chunk = Mix_LoadWAV_RW(rw, 1);
 		else
 			Py_END_ALLOW_THREADS
 		}
 	}
+printf("sound_init, chunk %p\n", chunk);
 
 	if(!chunk)
-		return RAISE(PyExc_SDLError, SDL_GetError());
-
-	final = PySound_New(chunk);
-	if(!final)
-		Mix_FreeChunk(chunk);
-
-	return final;
+        {
+		RAISE(PyExc_SDLError, SDL_GetError());
+                return -1;
+        }
+        
+        ((PySoundObject*)self)->chunk = chunk;
+	return 0;
 }
 
 
 	{ "unpause", mixer_unpause, 1, doc_unpause },
 /*	{ "lookup_frequency", lookup_frequency, 1, doc_lookup_frequency },*/
 
-	{ "Sound", Sound, 1, doc_Sound },
-
 	{ NULL, NULL }
 };
 
 	if(!chunk)
 		return RAISE(PyExc_RuntimeError, "unable to create sound.");
 
-	soundobj = PyObject_NEW(PySoundObject, &PySound_Type);
+	soundobj = (PySoundObject *)PySound_Type.tp_new(&PySound_Type, NULL, NULL);
 	if(soundobj)
 		soundobj->chunk = chunk;
 
 PYGAME_EXPORT
 void initmixer(void)
 {
-	PyObject *module, *dict, *apiobj, *music;
+	PyObject *module, *dict, *apiobj, *music=NULL;
 	static void* c_api[PYGAMEAPI_MIXER_NUMSLOTS];
 
 	PyMIXER_C_API[0] = PyMIXER_C_API[0]; /*this cleans an unused warning*/
 
-	PyType_Init(PySound_Type);
+        if (PyType_Ready(&PySound_Type) < 0)
+            return;
 	PyType_Init(PyChannel_Type);
 
     /* create the module */
 	module = Py_InitModule3("mixer", mixer_builtins, doc_pygame_mixer_MODULE);
 	dict = PyModule_GetDict(module);
 
+	PyDict_SetItemString(dict, "Sound", (PyObject *)&PySound_Type);
 	PyDict_SetItemString(dict, "SoundType", (PyObject *)&PySound_Type);
 	PyDict_SetItemString(dict, "ChannelType", (PyObject *)&PyChannel_Type);
 
 	c_api[6] = autoquit;
 	apiobj = PyCObject_FromVoidPtr(c_api, NULL);
 	PyDict_SetItemString(dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
-	Py_DECREF(apiobj);
 
 	/*imported needed apis*/
 	import_pygame_base();
 	else /*music module not compiled? cleanly ignore*/
 	{
             current_music = NULL;
-            PyErr_Clear();
+           PyErr_Clear();
 	}
 }
-

File src/mixer.h

View file
 
 #include <Python.h>
 #include <SDL_mixer.h>
+#include <structmember.h>
 
 
 /* test mixer initializations */
 typedef struct {
   PyObject_HEAD
   Mix_Chunk* chunk;
+  PyObject *weakreflist;
 } PySoundObject;
 typedef struct {
   PyObject_HEAD