I've been wanting to do learn some SDL, but as I never did like to play the same game as all the other kids, I find it both boring and confusing, thus I've setup the minimum amount of code I need, and what's left is simply to come up with some interesting algorithms to manipulate all the pixels on an individual basis.
I goes without saying that this can quite quickly become quite the task, thus I will eventually look into the "correct" way of doing things, but for now I'm simply interested in the hypothetical question that I'm now going to present.
Code:
#include <stdint.h>
const HEIGHT = 1080;
const WIDTH = 1920;
static uint8_t pixmap[ HEIGHT ][ WIDTH ];
// assume that pixmap has been filled with random 8 bit values.
void Shade()
{
int count;
for(int y = HEIGHT; y < HEIGHT + HEIGHT; y++)
{
for(int x = WIDTH; x < WIDTH + WIDTH; x++)
{
count = 8;
if( pixmap[ ( y - 1 ) % HEIGHT ][ ( x - 1 ) % WIDTH ] ) count--;
if( pixmap[ ( y - 1 ) % HEIGHT ][ x - WIDTH ] ) count--;
if( pixmap[ ( y - 1 ) % HEIGHT ][ ( x + 1 ) % WIDTH ] ) count--;
if( pixmap[ y - HEIGHT ][ ( x - 1 ) % WIDTH ] ) count--;
if( pixmap[ y - HEIGHT ][ ( x + 1 ) % WIDTH ] ) count--;
if( pixmap[ ( y + 1 ) % HEIGHT ][ ( x - 1 ) % WIDTH ] ) count--;
if( pixmap[ ( y + 1 ) % HEIGHT ][ x - WIDTH ] ) count--;
if( pixmap[ ( y + 1 ) % HEIGHT ][ ( x + 1 ) % WIDTH ] ) count--;
pixmap[ y - HEIGHT ][ x - WIDTH ] = 0xff >> count;
}
}
}
The above code, which is for now just an example, designed to illustrate how a relatively simple task can become quite the burden, should do nothing more than count the non zero neighbors of each individual pixel, and from that assign a values to said pixel.
I've had to jump through a few hoop to avoid getting out of bounds, which I'm sure there must be a better way of doing, and it is of course quite obvious that this "algorithm" if you wish, is very resource intensive.
So, is there anything I haven't thought of, which can be done to optimize this?
Best regards
edit:
Just tested it for the fun of it, and there is a problem which I cannot figure out. Running the function more than once should yield the same result, but it does not.