/* Filename: BasicLineBounce.cpp * * Author: Br. David Carlson * * Date: July 6, 2009 * * Description: This GDK program bounces a vertical lightray off the walls of the window. * A sound is made at each bounce off a wall. The user can press the ESC key to exit. */ #include "DarkGDK.h" const int START = 0; const int NORMAL = 1; const int INCOMING = 2; const int OUTGOING = 3; const DWORD black = dbRGB(0, 0, 0); const DWORD cyan = dbRGB(0, 250, 250); // Main function: void DarkGDK() { int CenterX, MinY, MaxY, FrontY, RearY, WallY, State, DeltaY, Count; const int RayLength = 99; // Must be odd. dbSetWindowTitle("Vertical Light Ray"); CenterX = dbScreenWidth() / 2; MinY = 0; MaxY = dbScreenHeight() - 1; dbInk(cyan, black); dbLoadSound("cowbell.wav", 1); // Sound file must be in the current directory for this to work. dbSyncOn(); dbSyncRate(1000); State = START; FrontY = MaxY; // Points to the next spot to color in for the lightray. RearY = MaxY; // Points to the last spot to color in for the lightray. Count = 1; DeltaY = -1; while (LoopGDK()) { dbCLS(); // Clear the screen. if (State == START) { dbLine(CenterX, RearY, CenterX, FrontY); Count++; FrontY += DeltaY; if (Count == RayLength) State = NORMAL; } else if (State == NORMAL) { dbLine(CenterX, RearY, CenterX, FrontY); FrontY += DeltaY; RearY += DeltaY; if (FrontY == MinY) { State = INCOMING; DeltaY = 1; WallY = MinY; dbPlaySound(1); } else if (FrontY == MaxY) { State = INCOMING; DeltaY = -1; WallY = MaxY; dbPlaySound(1); } } else if (State == INCOMING) { dbLine(CenterX, RearY, CenterX, WallY); FrontY += DeltaY; RearY -= DeltaY; if (FrontY == RearY) State = OUTGOING; } else if (State == OUTGOING) { dbLine(CenterX, WallY, CenterX, FrontY); FrontY += DeltaY; RearY -= DeltaY; if ((RearY == MinY) || (RearY == MaxY)) State = NORMAL; } dbSync(); } }