PocketMatrix forums

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

Recording mousemovements

Hi!

Ive created a little game(Kai-Bong):
http://www.pocketmatrix.com/forums/view ... 9&start=15

Now, some people would like a replay function.
What's the best way to record the mouse/pen- movements?
I had this in my mind:
a C-struct:

t=struct{
TIME t
POINT p
}

And onmousemove a new variable of this struct
in an linked list.

Is that a good idea?
Why a linked list ?
I'm starting programmer, but I use something similiar in my game. I use std::vector<strPointTime>.
A linked list or stack is a good solution. However, it may grow to be quite large. Unfortunately, I can't think of an alternative solution. Vector would simply be way to slow, as it's an array that rebuilds itself every time you add an item. Bad, very bad.
The vector doesn't have to grow every time you add an item. You can allocate it in blocks, if I'm not mistaken. So you could allocate room for 100 records, and when that fills up, it'll automatically free space for up 100 more, etc. I haven't tried this myself, but I've read about it... Having to rebuild the list every time would most definitely be terrible for performance.
Vector has exactly the same order of operations for growing as linked list. Anyone using the STL should know this. If you are using the STL and you do not, then you need to take a minute to brush up on the performance guarantees of the various operations on the various containers. If you are not using the STL, and particularly if you labour under the misconception that some containers and operations are slower than they actually are, then you need to start using the STL.
I'm fairly new to C++ and STL, so my interpretation may be perhaps flawed. According to The C++ Standard Library: A Tutorial and Reference;


capacity() returns the number of elements a vector could contain in its actual memory. If you exceed the capacity(), the vector has to reallocate its internal memory.
The capacity of a vector is important for two reasons:
1. Reallocation invalidates all references, pointers, and iterators for elements of the vector.
2. Reallocation takes time.
Thus, if a program managed pointers, references, or iterators into a vector, or if speed is a goal, it is important to take the capacity into account. (149)

...

Regarding performance, you should consider that inserting and removing happens faster when
- Elements are inserted or removed at the end
- The capacity is large enough on entry
- Multiple elements are inserted by a single call rather than by multiple calls
Inserting or removing elements invalidates references, pointers, and iterators that refer to the following elements. If an insertion causes reallocation, it invalidates all references, iterators, and pointers. (153)


On page 166-167, the book describes the differences between Lists and Vectors. For Lists, insertion and removal is much faster at any position, because it does not invalidate pointers or references. However, Lists do not provide random access, and accessing specific elements in a List is slow. On the other hand, random access in a Vector is quite fast.

Obviously you can create a Vector with a large capacity to begin with, but in the case of the original post, I seriously doubt he'll know how much capacity would be required. Also, it sounds like he won't make use of random access, either.

So yes, I was wrong when I stated that "an array that rebuilds itself every time you add an item." However, I wasn't too far from the truth.