I've been playing with drawing a vertical line quickly to an SDL Surface.
You access a specific pixel in an SDL surface by doing the following:
To draw a vertical line, I came up with the following code:Code:((unsigned int*)surface->pixels)[x + y*surface->pitch/4] = color;
It seems fast, and I haven't been able to improve upon it. The thing is, after each *cp = color, we need to jump, etc., which from what I've heard is rather expensive CPUtime wise.Code:void draw_vl(SDL_Surface * surface, int x, int y, int height, unsigned int color) { unsigned int res = surface->pitch/4; unsigned int* fp, *cp = ((unsigned int*)surface->pixels) + x + y*res; for (fp = cp + height*res; cp < fp; cp += res) { *cp = color; } }
I was thinking with a fixed maximum possible height, you could do something similar to the following (semi-pseudocode):
This would remove any comparison checks (if cp < fp), and all jumps except one. I could use some sort of code generator, but I'm wondering if there is a cleaner way of doing this, or a simpler way of drawing lines that is faster than my current method?Code:goto RATE_START + y*INSTRUCTIONS_PER_ITERATION RATE_START: *cp = color; cp += res; *cp = color; cp += res; *cp = color; ... Repeat 480 times (480 pixel tall image)
What is the fastest way to draw a vertical line?