Block Pixels

In order to give our game the feel of an oldstyle arcade game we are going to draw everything with blocks of 4x4 pixels to simulate the blocky appearance of very early computer monitors. To do this, we'll need a new pixel drawing routine which will view the screen as divided into fewer pixels, each of a larger size. Each call to the function will fill in a block of 4x4 ordinary pixels.

Since it will be useful to have access to the new width and height of our screen in terms of the block pixels, this should be calculated somewhere, and it makes sense to put this computation in an initialisation routine, along with the other code which initialises SDL.

The code for computing the new width and height of the screen in terms of block pixels is pretty straightforward. Once computed, we store the values in global variables so that they can be accessed anywhere. Global variables are generally discouraged by most expert programmers, but whilst our program remains simple they won't cause us a problem. The alternative is to pass them as parameters (perhaps as part of a struct) to all the functions that need them.

#define BLOCK 4 //each block of pixels is BLOCKxBLOCK ordinary pixels
int blockw, blockh;
int bytesPerPix;

bytesPerPix=screen->format->BytesPerPixel;
blockw = screen->pitch/bytesPerPix/BLOCK;
blockh = screen->h/BLOCK;

Here is the function which draws-na our block pixels:

void blockPixel(int x, int y, Uint8 r, Uint8 g, Uint8 b)
{
   int itimesw;
   int xblock=x*BLOCK;
   int yblock=y*screen->pitch/bytesPerPix*BLOCK; 

   for (int i=0; i<BLOCK; i++)
   {
      itimesw = i*screen->pitch/bytesPerPix;
      for (int j=0; j<BLOCK; j++)
      {
	  switch (bytesPerPix)
  	  {
      	     case 1:
      	     {
                 setpixel8(xblock+j, yblock+itimesw, r, g, b); break;
	     }
            case 2:
            {
                 setpixel16(xblock+j, yblock+itimesw, r, g, b); break;
            }
            case 4:
            {
                 setpixel32(xblock+j, yblock+itimesw, r, g, b); break;
            }
         }
      }
   }
}

Here is the updated version of our program.