home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 8 Other
/
08-Other.zip
/
QUIX.ZIP
/
QUIX.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-09-12
|
21KB
|
584 lines
/*
** Simple Quix Demo program for Presentation Manager.
**
** Written by:
** Mitchell Fisher
** 5914 W. Golden Lane
** Glendale, AZ 85302
**
** (602) 435-8723
**
** Special thanks to Charles Petzold and Microsoft for such great
** documentation from their books.
*/
#define INCL_WIN
#define INCL_GPI
#define INCL_DOS
#define BACK_BASE IDM_B_WHITE
#define FORE_BASE IDM_F_WHITE
#define TAIL_BASE 10
#include <os2.h>
#include <stdlib.h>
#include "Quix.h"
#define TIMER1 1
#define MAX_LINES 60
/*
Global Variable
*/
SHORT cxNew, /* New X size of client window */
cyNew, /* New Y size of client window */
idTimer,
sMaxCurrentLines = IDM_30,
fQuixUpdate,
fRandom = 1, // 1 == true for random colors
fColorFlag = 1, // 1 == true (Color on)
sBackGround, // Background Color
sForeGround, // ForeGround Color
sQuixDirx1,
sQuixDirx2,
sQuixDiry1,
sQuixDiry2;
LONG Color = CLR_BLACK,
Colors[MAX_LINES + 1],
ColorTable[] =
{
CLR_WHITE,
CLR_BLACK,
CLR_BLUE,
CLR_RED,
CLR_PINK,
CLR_GREEN,
CLR_CYAN,
CLR_YELLOW,
CLR_DARKGRAY,
CLR_DARKBLUE,
CLR_DARKRED,
CLR_DARKPINK,
CLR_DARKGREEN,
CLR_DARKCYAN,
CLR_BROWN,
CLR_PALEGRAY
};
POINTL ptlQuixPoint1,
ptlQuixPoint2;
MRESULT EXPENTRY ClientWndProc(HWND, USHORT, MPARAM, MPARAM);
VOID DrawLines(HPS);
VOID QuixInitialize(VOID);
SHORT GetQuixStep(VOID);
int main(void)
{
static char szClientClass[] = "Simple QUIX Demo";
static ULONG flFrameFlags = FCF_TITLEBAR | FCF_SYSMENU |
FCF_SIZEBORDER | FCF_MINMAX |
FCF_SHELLPOSITION | FCF_TASKLIST |
FCF_MENU;
HAB hab;
HPS hps;
HMQ hmq;
HWND hwndFrame,
hwndClient;
ULONG ulTime;
QMSG qmsg;
hab = WinInitialize(0);
hmq = WinCreateMsgQueue(hab, 0);
ulTime = WinGetCurrentTime(hab);
srand((unsigned)ulTime);
WinRegisterClass(hab, szClientClass, ClientWndProc, CS_SIZEREDRAW, 0);
hwndFrame = WinCreateStdWindow(HWND_DESKTOP, WS_VISIBLE,
&flFrameFlags, szClientClass, NULL, 0L,
NULL, ID_RESOURCE, &hwndClient);
/*
Set the timer to signal at 2000 milliseconds for color change
*/
WinStartTimer(hab, hwndClient, TIMER1, 2000);
hps = WinGetPS(hwndClient);
for (;;)
{
if ( WinPeekMsg(hab, &qmsg, NULL, 0, 0, PM_NOREMOVE) )
{
WinGetMsg(hab, &qmsg, NULL, 0, 0);
WinDispatchMsg(hab, &qmsg);
}
if ( qmsg.msg == WM_QUIT )
break;
DosSleep((LONG)1);
DrawLines(hps);
}
WinReleasePS(hps);
/*
Stop the timer
*/
WinStopTimer(hab, hwndClient, TIMER1);
WinDestroyWindow(hwndFrame);
WinDestroyMsgQueue(hmq);
WinTerminate(hab);
return(0);
}
SHORT GetQuixStep(VOID)
{
static SHORT Array[] = {-8, -6, -4, -3, -2, -1, 1, 2, 3, 4, 6, 8};
SHORT sIndex;
sIndex = rand() % 12;
return(Array[sIndex]);
}
VOID QuixInitialize(void)
{
/*
Define Step and Direction
*/
sQuixDirx1 = GetQuixStep();
sQuixDirx2 = GetQuixStep();
sQuixDiry1 = GetQuixStep();
sQuixDiry2 = GetQuixStep();
ptlQuixPoint1.x = (rand() % cxNew);
ptlQuixPoint1.y = (rand() % cyNew);
ptlQuixPoint2.x = (rand() % cxNew);
ptlQuixPoint2.y = (rand() % cyNew);
fQuixUpdate = 1;
}
VOID DrawLines(HPS hps)
{
static POINTL aP1[MAX_LINES + 1],
aP2[MAX_LINES + 1];
static SHORT sIndexHead = 0, /* Index within aP1 or aP2 array */
sIndexTail = 0,
fLoopFlag = 0; /* When the array has been filed */
/* If QuixInitialize has been called, reset Indexes */
if ( fQuixUpdate )
{
fQuixUpdate = 0;
sIndexHead = 0;
sIndexTail = 0;
fLoopFlag = 0;
}
/* Move points and draw line between them */
ptlQuixPoint1.x += sQuixDirx1;
ptlQuixPoint1.y += sQuixDiry1;
ptlQuixPoint2.x += sQuixDirx2;
ptlQuixPoint2.y += sQuixDiry2;
if ( (ptlQuixPoint1.x) <= 1 || (ptlQuixPoint1.x >= cxNew - 1) )
{
ptlQuixPoint1.x = (ptlQuixPoint1.x <= 1) ? 2 : cxNew - 2;
sQuixDirx1 = -sQuixDirx1;
}
if ( (ptlQuixPoint1.y) <= 1 || (ptlQuixPoint1.y >= cyNew - 1) )
{
ptlQuixPoint1.y = (ptlQuixPoint1.y <= 1) ? 2 : cyNew - 2;
sQuixDiry1 = -sQuixDiry1;
}
if ( (ptlQuixPoint2.x) <= 1 || (ptlQuixPoint2.x >= cxNew - 1) )
{
ptlQuixPoint2.x = (ptlQuixPoint2.x <= 1) ? 2 : cxNew - 2;
sQuixDirx2 = -sQuixDirx2;
}
if ( (ptlQuixPoint2.y) <= 1 || (ptlQuixPoint2.y >= cyNew - 1) )
{
ptlQuixPoint2.y = (ptlQuixPoint2.y <= 1) ? 2 : cyNew - 2;
sQuixDiry2 = -sQuixDiry2;
}
GpiMove(hps, &ptlQuixPoint1);
GpiSetColor(hps, ColorTable[Color]);
GpiLine(hps, &ptlQuixPoint2);
Colors[sIndexHead] = Color;
aP1[sIndexHead].x = ptlQuixPoint1.x;
aP1[sIndexHead].y = ptlQuixPoint1.y;
aP2[sIndexHead].x = ptlQuixPoint2.x;
aP2[sIndexHead].y = ptlQuixPoint2.y;
sIndexHead++;
if ( sIndexHead == sMaxCurrentLines - 1 )
{
fLoopFlag = 1;
sIndexHead = 0;
}
if ( fLoopFlag )
{
GpiMove(hps, &aP1[sIndexTail]);
GpiSetColor(hps, ColorTable[sBackGround - BACK_BASE]);
GpiLine(hps, &aP2[sIndexTail]);
sIndexTail++;
if ( sIndexTail == sMaxCurrentLines - 1)
sIndexTail = 0;
}
}
MRESULT EXPENTRY ClientWndProc(HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
{
static HPS hps;
static HWND hwndMenu;
static SHORT mbAnswer,
i;
static RECTL rcl;
switch (msg)
{
case WM_CREATE:
hwndMenu = WinWindowFromID(
WinQueryWindow(hwnd, QW_PARENT, FALSE),
FID_MENU);
sBackGround = IDM_B_WHITE;
sForeGround = IDM_F_BLACK;
return(0);
case WM_INITMENU:
WinSendMsg(hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT(sMaxCurrentLines, TRUE),
MPFROM2SHORT(MIA_CHECKED, MIA_CHECKED));
WinSendMsg(hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT(sBackGround, TRUE),
MPFROM2SHORT(MIA_CHECKED, MIA_CHECKED));
if ( fRandom ) // Random is off
{
WinSendMsg(hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT(IDM_F_RANDOM, TRUE),
MPFROM2SHORT(MIA_CHECKED, MIA_CHECKED));
for ( i = IDM_F_WHITE; i < IDM_F_RANDOM; i++ )
{
WinSendMsg(hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT(i, TRUE),
MPFROM2SHORT(MIA_DISABLED, MIA_DISABLED));
}
}
else
{
WinSendMsg(hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT(IDM_F_RANDOM, TRUE),
MPFROM2SHORT(MIA_CHECKED, 0));
for ( i = IDM_F_WHITE; i < IDM_F_RANDOM; i++ )
{
WinSendMsg(hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT(i, TRUE),
MPFROM2SHORT(MIA_DISABLED, 0));
}
}
WinSendMsg(hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT(sForeGround, TRUE),
MPFROM2SHORT(MIA_CHECKED, MIA_CHECKED));
return(0);
/*
DISABLE corresponding foreground and background colors
*/
WinSendMsg(hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT(sForeGround - BACK_BASE, TRUE),
MPFROM2SHORT(MIA_DISABLED, MIA_DISABLED));
WinSendMsg(hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT(sBackGround + BACK_BASE, TRUE),
MPFROM2SHORT(MIA_DISABLED, MIA_DISABLED));
case WM_SIZE:
hps = WinBeginPaint(hwnd, NULL, NULL);
WinQueryWindowRect(hwnd, &rcl);
WinFillRect(hps, &rcl, ColorTable[sBackGround - BACK_BASE]);
WinEndPaint(hps);
cxNew = SHORT1FROMMP(mp2);
cyNew = SHORT2FROMMP(mp2);
QuixInitialize();
return(0);
case WM_PAINT:
hps = WinBeginPaint(hwnd, NULL, NULL);
WinQueryWindowRect(hwnd, &rcl);
WinFillRect(hps, &rcl, ColorTable[sBackGround - BACK_BASE]);
WinEndPaint(hps);
return(0);
case WM_COMMAND:
{
switch (COMMANDMSG(&msg)->cmd)
{
case IDM_EXIT:
mbAnswer = WinMessageBox(HWND_DESKTOP, hwnd, "Thanks for trying out the Quix Demo!",
"Quix Demo", 0, MB_ENTER | MB_INFORMATION);
if ( mbAnswer == MBID_ENTER )
{
WinPostMsg(hwnd, WM_QUIT, 0L, 0L);
return(0);
}
return(0);
case IDM_10: /* Tail size of 10, 20, 30, etc... */
case IDM_20:
case IDM_30:
case IDM_40:
case IDM_50:
case IDM_60:
WinSendMsg(hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT(sMaxCurrentLines, TRUE),
MPFROM2SHORT(MIA_CHECKED, 0));
sMaxCurrentLines = COMMANDMSG(&msg)->cmd;
hps = WinGetPS(hwnd);
WinQueryWindowRect(hwnd, &rcl);
WinFillRect(hps, &rcl, ColorTable[sBackGround - BACK_BASE]);
WinReleasePS(hps);
QuixInitialize();
WinSendMsg(hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT(sMaxCurrentLines, TRUE),
MPFROM2SHORT(MIA_CHECKED, MIA_CHECKED));
return(0);
case IDM_B_WHITE:
case IDM_B_BLACK:
case IDM_B_BLUE:
case IDM_B_RED:
case IDM_B_PINK:
case IDM_B_GREEN:
case IDM_B_CYAN:
case IDM_B_YELLOW:
case IDM_B_DARKGRAY:
case IDM_B_DARKBLUE:
case IDM_B_DARKRED:
case IDM_B_DARKPINK:
case IDM_B_DARKGREEN:
case IDM_B_DARKCYAN:
case IDM_B_BROWN:
case IDM_B_PALEGRAY:
/*
Clear old Checkmark
*/
WinSendMsg(hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT(sBackGround, TRUE),
MPFROM2SHORT(MIA_CHECKED, 0));
/*
Clear Corresponding ForeGround Color
*/
WinSendMsg(hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT(sBackGround + BACK_BASE, TRUE),
MPFROM2SHORT(MIA_DISABLED, 0));
sBackGround = COMMANDMSG(&msg)->cmd;
/*
Set new checkmark
*/
WinSendMsg(hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT(sBackGround, TRUE),
MPFROM2SHORT(MIA_CHECKED, MIA_CHECKED));
/*
DISABLE corresponding foreground color
*/
WinSendMsg(hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT((sBackGround + BACK_BASE), TRUE),
MPFROM2SHORT(MIA_DISABLED, MIA_DISABLED));
hps = WinGetPS(hwnd);
WinQueryWindowRect(hwnd, &rcl);
WinFillRect(hps, &rcl, ColorTable[sBackGround - BACK_BASE]);
WinReleasePS(hps);
QuixInitialize();
return(0);
case IDM_F_WHITE:
case IDM_F_BLACK:
case IDM_F_BLUE:
case IDM_F_RED:
case IDM_F_PINK:
case IDM_F_GREEN:
case IDM_F_CYAN:
case IDM_F_YELLOW:
case IDM_F_DARKGRAY:
case IDM_F_DARKBLUE:
case IDM_F_DARKRED:
case IDM_F_DARKPINK:
case IDM_F_DARKGREEN:
case IDM_F_DARKCYAN:
case IDM_F_BROWN:
case IDM_F_PALEGRAY:
/*
Clear old Checkmark
*/
WinSendMsg(hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT(sForeGround, TRUE),
MPFROM2SHORT(MIA_CHECKED, 0));
/*
Clear corresponding background color
*/
WinSendMsg(hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT(sForeGround - BACK_BASE, TRUE),
MPFROM2SHORT(MIA_DISABLED, 0));
sForeGround = COMMANDMSG(&msg)->cmd;
Color = sForeGround - FORE_BASE;
hps = WinGetPS(hwnd);
GpiSetColor(hps, ColorTable[sForeGround - FORE_BASE]);
WinReleasePS(hps);
QuixInitialize();
/*
Set new checkmark
*/
WinSendMsg(hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT(sForeGround, TRUE),
MPFROM2SHORT(MIA_CHECKED, MIA_CHECKED));
/*
DISABLE corresponding background color
*/
WinSendMsg(hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT(sForeGround - BACK_BASE, TRUE),
MPFROM2SHORT(MIA_DISABLED, MIA_DISABLED));
hps = WinGetPS(hwnd);
WinQueryWindowRect(hwnd, &rcl);
WinFillRect(hps, &rcl, ColorTable[sBackGround - BACK_BASE]);
WinReleasePS(hps);
return(0);
case IDM_F_RANDOM:
if ( !fRandom )
{
WinSendMsg(hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT(IDM_F_RANDOM, TRUE),
MPFROM2SHORT(MIA_CHECKED, MIA_CHECKED));
for ( i = IDM_F_WHITE; i < IDM_F_RANDOM; i++ )
{
WinSendMsg(hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT(i, TRUE),
MPFROM2SHORT(MIA_DISABLED, MIA_DISABLED));
}
fRandom = 1;
fColorFlag = 1;
}
else
{
WinSendMsg(hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT(IDM_F_RANDOM, TRUE),
MPFROM2SHORT(MIA_CHECKED, 0));
for ( i = IDM_F_WHITE; i < IDM_F_RANDOM; i++ )
{
if ( i == (sBackGround + BACK_BASE) ) // Skip back corresponding color
continue;
WinSendMsg(hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT(i, TRUE),
MPFROM2SHORT(MIA_DISABLED, 0));
}
fRandom = 0;
fColorFlag = 0;
Color = ColorTable[sBackGround - BACK_BASE];
}
// DISABLE corresponding background color
WinSendMsg(hwndMenu, MM_SETITEMATTR,
MPFROM2SHORT(sForeGround - BACK_BASE, TRUE),
MPFROM2SHORT(MIA_DISABLED, MIA_DISABLED));
return(0);
}
}
case WM_TIMER:
sQuixDirx1 = GetQuixStep();
sQuixDirx2 = GetQuixStep();
sQuixDiry1 = GetQuixStep();
sQuixDiry2 = GetQuixStep();
Color = (rand() % 16);
do
{
Color = (rand() % 16);
} while (Color == sBackGround - BACK_BASE);
if ( !fColorFlag )
Color = sForeGround - FORE_BASE;
return(0);
}
return(WinDefWindowProc(hwnd, msg, mp1, mp2));
}