by Digby » Jul 24, 2001 @ 11:36am
More good stuff Jacco. I've just read your Tutorial #4 and I have a few comments.<br><br>1) The article explains the use of the new[] operator but it doesn't mention delete[].<br><br>2) The article didn't cover one of the most important points of C/C++ pointers, and that is pointer arithmetic. Novice programmers should understand that when using the pre/post increment/decrement operators or any of the math operators (+/-*) that the value of the pointer variable is changed by the size of the data type to which it points - not by the value of the operation. For example<br><br>BYTE *pbByte = (BYTE*)0x10000000;<br>WORD *pwWord = (WORD*)0x10000000;<br>DWORD *pdwDWord = (DWORD*)0x10000000;<br><br> pbByte = pbByte + 1;<br> pwWord = pwWord + 1;<br> pdwDWord = pdwDWord + 1;<br><br>After the addition operation above, pbByte now contains 0x10000001, pwWord contains 0x10000002, and pdwDWord contains 0x10000004. This can really confuse people who look at the code and see the value of 1 being added to pointer and the resulting variable will sometimes increases by more than 1.<br><br>3) There is the issue of alignment. Attempts to dereference a WORD or DWORD pointer at an odd-address will cause an exception with some CPUs. This might be harder to explain than it's worth for newbies though.<br><br>4) In the paragraph explaining how to use a spin loop to update the frame every 30 msec, why do you need to call update() in the loop? Incidentally, you've left off the () in the update call.<br><br>5) There's a typo in the code mentioned in the section titled "Pointer Magic". The line should read: <br>PIXEL* source = secondarybuffer;<br><br>6) It might be worthwhile to add a section about NULL pointers and checking for NULL before dereferencing (and what happens if you don't do this). A lot of APIs denote failure by returning a NULL pointer value, including the memory allocators. Since Pocket PCs will fail memory allocation far more frequently (limited phys RAM, no paging to disk) than code running on PC, this might be worth mentioning.<br><br>7) This is really trivial but in the line of code that tests for a transparent pixel there's no need to recalculate the source pixel value - you have that already in spritepixel. This brings up another point, you started the article with a nice progression of pointer arithmetic, to post-increment operators, to using memcpy(). Did you consider doing the same for the sprite code? You might want to add sample code that shows how to copy the sprite more efficiently than calculating the source and destination address within the inner loop. I'm not saying remove the example you have because it is very readable and easy to understand, but you might add an optimized example and then the reader can compare the two.<br><br><br>Again, it's really cool that you're doing these series of tutorials for the people who want to dabble in game programming for their Pocket PC devices. Keep up the good work!<br><br><br>