Hi,
quanganht wrote:You have some really good snipets, Brendan! It would definetely help me alot later. Thanks
Thanks!
Something else I probably should've mentioned is double buffering: if it takes a bit of work to generate a frame of video data, then it's probably faster to generate the frame in (fast) RAM and then copy it to (slower) video display memory in one go. Of course this can be optimised too - keep track of which parts changed, and only send the changed data to display memory.
For a more extreme example (using a pair of buffers in RAM, and a "line changed" flag for each row of pixels):
Code: Select all
void blit_buffer_to_video(void) {
uint32_t offset = 0;
void *dest_address = video_display_memory;
void *temp_dest_address;
uint32_t next_dword;
for(y = 0; y < screen_height; y++) {
if(line_changed_flags[y] != 0) {
temp_dest_address = dest_address;
for(x = 0; x < screen_width; x += 4) {
next_dword = new_buffer[offset];
if( current_buffer[offset] != next_dword ) {
current_buffer[offset] = next_dword;
*(uint32_t *)temp_dest_address = next_dword;
}
offset++;
temp_dest_address += 4;
}
line_changed_flags[y] = 0;
} else {
offset += screen_width;
}
dest_address += bytes_per_line;
}
}
Basically, for each row of pixels that was changed, compare the old data with the new data and only send dwords that are different to video display memory.
Of course this adds a little overhead to your drawing functions (as they need to update the "line changed" flags), and the extra work involved in copying the data from the buffer to display memory (and the extra RAM accesses) makes it look like it'd be a lot slower; but in practice video display memory is so slow (compared to CPU and RAM speeds) that this can make a significant performance improvement (by avoiding as many writes to display memory as possible).
Cheers,
Brendan