Yes, but the compiler cannot know everything. So in this case with SDL for example, do you mean I should use memcpy(dest, source, 3); instead of *(long*)dest = *(long*)source? The original probem is to copy a specific color, a BPP bytes big array containing the values for the different channels, into each pixel of an image. Also each pixel takes up BPP bytes of memory. The thing is that it is allowed to copy BPP bytes (4 in this case), but it only has to copy NC*sizeof(T) bytes (3 in this case, the last byte is unused), where NC is the number of channels and T the data type containing the value for each channel.
Currently I have this inline member function to do this for me:
Code:
template<class T, uint NC, size_t BPP>
inline void mp_image<T, NC, BPP>::CopyColor(byte *dest, byte *source)
{
uint i = 0;
while ( i < (NC * sizeof(T))/sizeof(int)*sizeof(int) ||
i < NC * sizeof(T) && i < BPP/sizeof(int)*sizeof(int) ) {
*(int*)dest = *(int*)source;
dest += sizeof(int);
i += sizeof(int);
}
if ( i < (NC * sizeof(T))/sizeof(short)*sizeof(short) ||
i < NC * sizeof(T) && i < BPP/sizeof(short)*sizeof(short) ) {
*(short*)dest = *(short*)source;
dest += sizeof(short);
i += sizeof(short);
}
if ( i < NC * sizeof(T)) {
*dest = *source;
dest++;
i++;
}
}
I don't know if this is good or if it is just stupid, but in the case with SDL this function would just take and copy one int. byte here is an integer datat type, same as unsigned char.