PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Tutorial 4 online

Hey guys,

The fourth tutorial is now online, covering pointers, buffers and sprites. Enjoy.
URL is: http://pizza.hvu.nl/~kouwenhs .

- Jacco.
Moose or Chuck · 24 Jul 2001, 09:38 UTC
Ya, good to hear. Another tutorial.

Now i can write my next too :-P ~
Hey you should request a thousand e-mails first.
Moose or Chuck · 24 Jul 2001, 10:17 UTC
Now that's a good idea! ~
More good stuff Jacco.  I've just read your Tutorial #4 and I have a few comments.

1) The article explains the use of the new[] operator but it doesn't mention delete[].

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

BYTE *pbByte = (BYTE*)0x10000000;
WORD *pwWord = (WORD*)0x10000000;
DWORD *pdwDWord = (DWORD*)0x10000000;

   pbByte = pbByte + 1;
   pwWord = pwWord + 1;
   pdwDWord = pdwDWord + 1;

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.

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.

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.

5) There's a typo in the code mentioned in the section titled "Pointer Magic".  The line should read:
PIXEL* source = secondarybuffer;

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.

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.


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!


Digby,

I fixed the things that you mentioned. I think this is quite important, since these are really the basics that people should understand. I appreciate your feedback greatly.

- Jacco.
Yeah, #3 is a biggie. The Windows CE processors are much more picky about alignment than the x86 family. Thus an app may run great in the emulator, but crash on a real device. I also discovered that the MIPS processor was very picky about alignment performance-wise. In one app I dynamically allocated string buffers to fit their contents. The code ran very slow on MIPS devices until I changed my allocation routines to allocate memory in chunks of 8 bytes. That alignment got MIPS back up in performance relative to the ARM and SH3 processors.
This doesn't directly deal with your document, but I have found the ARM processor to be the most forgiving, versatile processor of the three. MIPS has the alignment-performance issue, and SH3 apps(probably because of the compiler) may crash in certain circumstances due to compiler optimizations. Unfortunately any non-trivial app must be tested and even debugged on all three types of devices.

Dan East
Dan,

You said you aligned your strings to 8 byte boundaries. Does it matter, speed wise, whether you use 8 byte or, say, 4 byte boundaries? In that case I suppose I can get some more performance from the Nutcracker too (and maybe from MikMod, wich would also be nice).

- Jacco.
I just looked at my code to see what it was I did. Based on experimentation, I had to allocate (with new) in multiples of 16 (my earlier post is incorrect) to prevent a huge performance degradation with MIPS. I also tried 2, 4 and 8 byte chunks with no positive results. We have the same medical software running on a host of devices, CE 2.0, 2.11, 2.11 Pro and 3.0, SH3, ARM and MIPS.  The MIPS devices (a Cassopeia E-105 PSPC and NEC MobilePro HPC/Pro) both ground to a halt (like 3-5 times slower) when repeatedly allocating data that was not on that boundary (we're talking about thousands of such allocations at a time).
Here's the type of code I used:
[fixed]
#if defined(_WIN32_WCE)&&defined(_MIPS_)
pNewData=new BYTE[16-(len%16)+len];
#else
pNewData=new BYTE[len];
#endif
[/fixed]

The memory is used primarily for strings (TCHARs).
I don't have a MIPS device handy to experiment with, but it should be easy to do some trials to see what happens. You would have to do thousands of iterations to make the effect most noticeable.

Dan East
So the slowdown occurs only when performing the actual 'new', not when accessing the memory? I mean, a memcpy from or to a 2-byte aligned address is not slower than one to a 16 byte aligned address?
I don't do that many new's in The Nutcracker, so it's not really an issue for me in that case.
No, it was in accessing the memory after it had been allocated. Actually, I never did a test to see if the allocation time was affected either way. The accessing of the memory down the road was the most critical part.

Dan East
Well in that case I'll optimize my code. :) Thanks for the tip.
Do you test on a MIPS device? I thought you had an iPaq. It's a shame a developer needs to own one of every Windows CE device they want to target, to work out its idiosyncrasies.

Dan East
I have an iPaq, indeed, but this modification can be tested by my betatesters. It's certainly not an ideal situation however, I agree.
Dan,

I am in the process of optimizing The Nutcracker a bit; I now store my screens (7*8 tiles) in a 8*8 buffer to be able to use <<3 instead of *7; I added a lookup-table for the tons of /40's I do (tiles are 40 pixels wide), and now I wanted to add this optimized memory allocation stuff for the Mips.

I just read over your post again, and first now I noticed that you allocate chunks of 16bytes. I thought you where allocating at 16byte boundaries, so I wondered how you were going to fix that when you free memory. But no, you just allocate 16byte chunks, at random addresses.

I don't understand that. I mean, I can imagine that allocating at a nice address matters, but the size of the chunks? Very odd.

Anyway, I'll just add it with a nice #ifdef around it for the Casio's. :)

- Jacco.
Jacco,

I've never seen the PQ source, but from the code Dan posted I don't understand the benefit of that scheme either.  He's just making sure that each allocated chunk is an even multiple of 16 bytes.

The issue I was referring to in my comments was that some CPUs require you to access data types on a sizeof(data type) byte aligned boundary.  I don't understand how Dan's code guarantees that will happen.

The way I handle memory alignment issues is to write a wrapper around malloc that allocates additional bytes to every request and then slide the starting address returned by malloc up a bit until it is on the alignment boundary.  I then store the original address returned by malloc in the 4 bytes immediately before the newly aligned address.  The free routine then has to back up 4 bytes, dereference this address to get the stored actual block address, and then pass that to free().

For larger buffers (for .wav, .bmp, or surface data) I just use VirtualAlloc.  It only allows you to allocate with a 4K granularity, but the addresses are guaranteed to be aligned on 4K byte boundaries.  Also, when you use VirtualAlloc it is very fast (no heap walking looking for free blocks) and will never cause heap fragmentation.

Here's some code that does the memory allocation on n-byte boundaries.  Just change the constant, c_ulAlign, to whatever you want.

const unsigned long c_ulAlign = 16;
const unsigned long c_ulAlignExtra = (c_ulAlign - 1) + sizeof(void*);

void * malloc_align (size_t size)
{
unsigned long ulBaseAddr;
unsigned long ulRetAddr;

   ulBaseAddr = (unsigned long)malloc(size + c_ulAlignExtra);
   ulRetAddr = (ulBaseAddr + c_ulAlignExtra) & ~(c_ulAlign - 1);
   *(void**)(ulRetAddr - sizeof(void*)) = (void*)ulBaseAddr;
   
   return((void*)ulRetAddr);
}

void free_align (void* pv)
{
   free(*(void**)((unsigned long)pv - sizeof(void*)));
}



Digby,

Thanks for that snippet. It's a cool idea to add the original address that way. I will use this in my game.
This weekend I nearly doubled the speed of my game by fixing a stupid little bug btw. :) I was building a LUT using 256 sin's each frame, even if I wasn't using the LUT. I like to find things like that. Speed problems are gone now. :)

- Jacco.
Yeah, I'm still looking for that Sleep(50) in PQ. :)
Digby, that allocation scheme was used in a different app, not PQ.
This thread ought to be in the developer's forum.

Dan East
Christian Harju · 17 Aug 2001, 08:12 UTC
Where can i find this tutorial, because the pizza.hvu.nl server isnt up at all.