D.E.Games
Floppy - only 150 lines of code!
After working on my first Windows game, WormRaider, I wanted to expand into DirectX/OpenGL. I thought simple 2D sprite animation in DirectX would be a great place to start. Boy, was I wrong. After pulling out my hair with DirectX, I came across SDL... a fantastic game development library that makes Windows and DirectX programming as simple as it really should be. Here's a quick game I created in 2-3 hours while I was learning the tools. Nothing fancy, just a simple game of Reversi (as simple as DirectX). Try to get rid of all the "Anti-Floppys". You will need DirectX installed to play. Here's the code for the entire game... small is beautiful!
Source Code
#include "sdl.h"
#include <stdio.h>
#include <stdlib.h>

#define SCREEN_W	800
#define SCREEN_H	600
#define BASE_X	250
#define BASE_Y	5
#define OFFSET_X	52
#define OFFSET_Y	45
#define IMG_SIZE	64

SDL_Surface *	g_pMainSurface = NULL;
SDL_Event		g_Event;
SDL_Surface *	g_pBoard = NULL;
SDL_Surface *	g_pFloppy = NULL;
SDL_Surface *	g_pFloppyIcon = NULL;
SDL_Rect		g_Src,g_Dest;

const char * APP_TITLE = "Floppy vs. Anti-Floppy";
bool grid[12][10] = {
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0}};
Uint8 counter = 0;
bool gameover = false;

void DrawGrid(void);
void SwitchMice(Uint16 x, Uint16 y);
void SetBoard(int);

int main(int argc, char * argv[])
{
	SDL_Init(SDL_INIT_VIDEO);
	atexit(SDL_Quit);

	SDL_WM_SetCaption(APP_TITLE,APP_TITLE);
	g_pFloppyIcon = SDL_LoadBMP("FLOPPY.BMP");
	SDL_WM_SetIcon(g_pFloppyIcon,NULL);
	g_pMainSurface = SDL_SetVideoMode(SCREEN_W,SCREEN_H,0,SDL_ANYFORMAT);
	g_pBoard = SDL_LoadBMP("FloppyGameBoard.bmp");
	g_pFloppy = SDL_LoadBMP("Mice.bmp");
	SDL_SetColorKey(g_pFloppy,SDL_SRCCOLORKEY,0xB5B7C0);

	srand(SDL_GetTicks());
	SetBoard(15);

	for(;;)
	{
		if(SDL_PollEvent(&g_Event)==0)
		{
			if(gameover)
			{
				g_Src.x=64;g_Src.y=0;
				g_Src.w=g_Src.h=IMG_SIZE;
				g_Dest.w=g_Dest.h=IMG_SIZE;
				g_Dest.x=rand()%(SCREEN_W-g_Dest.w);
				g_Dest.y=rand()%(SCREEN_H-g_Dest.h);
				SDL_SetAlpha(g_pFloppy, SDL_SRCALPHA, rand()%255);
				SDL_BlitSurface(
				  g_pFloppy, &g_Src, g_pMainSurface, &g_Dest);
				SDL_UpdateRect(
				  g_pMainSurface, g_Dest.x, g_Dest.y, g_Dest.w, g_Dest.h);
			}
		}
		else
		{
			if(g_Event.type==SDL_QUIT||g_Event.type==SDL_KEYDOWN) break;
			if(g_Event.type==SDL_MOUSEBUTTONDOWN&&!gameover)
			{
				SwitchMice(g_Event.button.x,g_Event.button.y);
			}
		}
	}
	return(0);
}

void DrawGrid()
{
	//draw board
	g_Src.w=g_Dest.w=g_pBoard->w;
	g_Src.h=g_Dest.h=g_pBoard->h;
	g_Src.x=g_Src.y=g_Dest.x=g_Dest.y=0;
	SDL_BlitSurface(g_pBoard, &g_Src, g_pMainSurface, &g_Dest);
	//draw mice
	g_Src.w=g_Src.h=IMG_SIZE;
	g_Dest.w=g_Dest.h=IMG_SIZE;
	counter=0;
	for(int y=0; y<12 ;y++)
	{
		for(int x=0;x<10;x++)
		{
			if(grid[y][x])
			{
				g_Src.x=g_Src.y=0;counter++;
			}
			else
			{
				g_Src.x=IMG_SIZE;g_Src.y=0;
			}
			if(y%2!=0)
			{
				g_Dest.x=(x*OFFSET_X)-(OFFSET_X/2)+BASE_X;
			}
			else
			{
				g_Dest.x=(x*OFFSET_X)+BASE_X;
			}
			g_Dest.y=(y*OFFSET_Y)+BASE_Y;
			SDL_BlitSurface(g_pFloppy, &g_Src, g_pMainSurface, &g_Dest);
		}
	}
	SDL_UpdateRect(g_pMainSurface,0,0,0,0);
	if(counter==0){gameover=true;}
	else {gameover=false;}
}

void SwitchMice(Uint16 x, Uint16 y)
{
	if(x>225 && x<770 && y>20 && y<575)
	{
		y=(y-20)/OFFSET_Y;
		if(y%2==0)
		{
			x=(x-225-(OFFSET_X/2))/OFFSET_X;
		}
		else
		{
			x=(x-225)/OFFSET_X;
		}
		if(x>9)x=9;
		//switch mice
		grid[y][x]=!grid[y][x];
		if(y>0)
		{
			grid[y-1][x]=!grid[y-1][x];
			if (x>0) grid[y-1][x-1]=!grid[y-1][x-1];
			if (x<9) grid[y-1][x+1]=!grid[y-1][x+1];
		}
		if(y<11)
		{
			grid[y+1][x]=!grid[y+1][x];
			if (x>0) grid[y+1][x-1]=!grid[y+1][x-1];
			if (x<9) grid[y+1][x+1]=!grid[y+1][x+1];
		}
		if(x>0){grid[y][x-1]=!grid[y][x-1];}
		if(x<9){grid[y][x+1]=!grid[y][x+1];}
		//redraw screen
		DrawGrid();
	}
}

void SetBoard(int count)
{
	for(int i=0;i<count;i++)
	{
		SwitchMice((rand()%445)+225,(rand()%555)+20);
	}
}