home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 1.ddi / ACLOCK.ZIP / ACLOCK.CPP next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  8.4 KB  |  327 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2.  
  3. #include <alloc.h>
  4. #include <owl.h>
  5. #include <window.h>
  6. #include <dos.h>
  7. #include <stdlib.h>
  8. #include <math.h>
  9. #include <mmsystem.h>
  10. #include <stdio.h>
  11. #include <bios.h>>
  12. #include "aclock.h"
  13. #include "bitmap.h"
  14.  
  15. const float pi2 = 2*3.141592;
  16.  
  17. // Implementation of CompatibleDC and Bitmap classes.
  18.  
  19. class CompatibleDC
  20. {
  21.     private:
  22.     HDC hDCMem;
  23.     public:
  24.     // Create a memory device context, specify a selected object,
  25.     // and set the DC's mapping mode.
  26.     CompatibleDC( HDC hDC )
  27.     {
  28.         hDCMem = CreateCompatibleDC( hDC );
  29.         SetMapMode( hDCMem, GetMapMode( hDC ) );
  30.     }
  31.     ~CompatibleDC( void ) { DeleteDC( hDCMem ); };
  32.     HDC Get_hDCMem( void ) { return hDCMem; }
  33. };
  34.  
  35. // draw a bitmap
  36. void FAR Bitmap::Display( HDC hDC, short xStart, short yStart)
  37. {
  38.     POINT ptSize, ptOrigin;
  39.  
  40.     CompatibleDC MemoryDC( hDC );
  41.     HDC hDCMem = MemoryDC.Get_hDCMem();
  42.     SelectObject( hDCMem, hBitmap );
  43.  
  44.     ptSize = GetSize( hDC );
  45.     ptOrigin.x = 0;
  46.     ptOrigin.y = 0;
  47.     DPtoLP( hDCMem, &ptOrigin, 1 );
  48.  
  49.     BitBlt( hDC, xStart, yStart, ptSize.x, ptSize.y,
  50.         hDCMem, ptOrigin.x, ptOrigin.y, SRCCOPY );
  51. }
  52.  
  53.  
  54. //Animated class animates a set of bitmaps
  55. class Animated
  56. {
  57. private:
  58.   int numBmps;     //number of bitmaps
  59.   Bitmap **pBmps; //handles to bitmaps
  60. public:
  61.   int delayTicks;   //amount to delay between frames
  62.             //18.2 ticks/sec
  63.   int delayEnd;     //amount to delay after sequence
  64.   virtual void Display(HDC hDC, int x, int y);
  65.   Animated(HINSTANCE hInst, int numB, char *name);
  66.   virtual ~Animated();
  67. };
  68.  
  69.  
  70. //Create an animated sequence
  71. Animated::Animated(HINSTANCE hInst, int numB, char *name)
  72. {
  73.   int j;
  74.   char resName[40];
  75.  
  76.   //create array of numB Bitmap*'s
  77.   pBmps = new (Bitmap (*[numB]));
  78.  
  79.   //load in bitmap resources
  80.   numBmps = numB;
  81.   for (j=0; j<numB; j++) {
  82.     sprintf(resName, "%s%d", name, j+1);
  83.     pBmps[j] = new Bitmap(hInst, resName );
  84.   }
  85. }
  86.  
  87. Animated::~Animated()
  88. {
  89.    for (int j=0; j<numBmps ;j++ )
  90.       delete pBmps[j];
  91. }
  92.  
  93. //Draw the series of bitmaps
  94. void Animated::Display(HDC hDC, int x, int y)
  95. {
  96.   int j;
  97.   long endTime;
  98.  
  99.   //draw each bitmap and then delay
  100.   for (j=0; j<numBmps; j++) {
  101.     pBmps[j]->Display(hDC, x, y);
  102.     //delay for delayTicks clock ticks (18.2/sec)
  103.     endTime = biostime(0, 0L) + delayTicks;
  104.     while (biostime(0, 0L) < endTime);
  105.  
  106.   }
  107.   //delay for ending amount
  108.   endTime = biostime(0, 0L) + delayEnd;
  109.   while (biostime(0, 0L) < endTime)
  110.     Yield();              //be nice to Windows
  111. }
  112.  
  113.  
  114. // Application is based on TApplication
  115. _CLASSDEF(TAClockApp)
  116. class TAClockApp : public TApplication
  117. {
  118. public:
  119.   TAClockApp(LPSTR AName, HINSTANCE hInstance, HINSTANCE hPrevInstance,
  120.     LPSTR lpCmdLine, int nCmdShow)
  121.     : TApplication(AName, hInstance, hPrevInstance, lpCmdLine, nCmdShow) {};
  122.   virtual void InitMainWindow();
  123.  
  124.   friend class TAClockWindow;
  125. };
  126.  
  127.  
  128. //Clock window is based on TWindow
  129. class TAClockWindow : public TWindow
  130. {
  131. private:
  132.   Bitmap *phbmFace;        //clock face bitmap
  133.   POINT Center;            //center of clock
  134.   int    Radius;            //clock radius
  135.   struct time  LastTime;   //last time drawn
  136.   HPEN hpHour, hpMinute;   //pens for clock hands
  137.   Animated *panCuckoo;     //cuckoo sequence
  138.  
  139. public:
  140.  
  141.   TAClockWindow(PTWindowsObject AParent, LPSTR ATitle);
  142.   virtual ~TAClockWindow();
  143.   virtual void SetupWindow();
  144.   virtual void Paint(HDC PaintDC, PAINTSTRUCT _FAR & PaintInfo);
  145.   virtual void WMTimer(RTMessage Msg) = [WM_FIRST + WM_TIMER];
  146.   virtual void CMAbout(RTMessage  Msg) =
  147.     [CM_FIRST + CM_ABOUT];
  148.   virtual void CMEffectChime(RTMessage Msg) = [CM_FIRST + CM_EFFECTCHIME];
  149.   virtual void CMEffectCuckoo(RTMessage Msg) = [CM_FIRST + CM_EFFECTCUCKOO];
  150.   void PaintHands(HDC hDC, struct time theTime);
  151.   void Chime();
  152.   void Cuckoo();
  153. };
  154.  
  155.  
  156. TAClockWindow::~TAClockWindow()
  157. {
  158.   //get rid of timer
  159.   KillTimer(HWindow ,1);
  160.   //delete brushes
  161.   DeleteObject(hpMinute);
  162.   DeleteObject(hpHour);
  163.   // delete bitmaps
  164.   delete phbmFace;
  165.   // deleted Animated object
  166.   delete panCuckoo;
  167. }
  168.  
  169.  
  170. //load sequences and set size in the constructor
  171. TAClockWindow::TAClockWindow(PTWindowsObject AParent, LPSTR ATitle) :
  172.   TWindow(AParent, ATitle)
  173. {
  174.   AssignMenu("TOOL_MENU");
  175.   PTAClockApp Application = (PTAClockApp) GetApplication();
  176.   //load face bitmap and cuckoo sequence
  177.   phbmFace = new Bitmap(Application->hInstance, "FACE_BMP");
  178.   panCuckoo = new Animated(Application->hInstance, 8, "CUCKOO");
  179.   //set frame delay time
  180.   panCuckoo->delayTicks = 5;
  181.   panCuckoo->delayEnd = 48;
  182.   //Set the window size
  183.   HDC TheDC = GetDC(HWindow);
  184.   POINT temp = phbmFace->GetSize(TheDC);
  185.   //set to size of bitmap plus fudge factor
  186.   Attr.H = temp.y + 40;
  187.   Attr.W = temp.x + 2;
  188.   Attr.Style &= ~(WS_THICKFRAME | WS_MAXIMIZEBOX);
  189.   ReleaseDC(HWindow, TheDC);
  190.   Center.x = temp.x / 2;
  191.   Center.y = temp.y / 2;
  192.   Radius = 3*(Center.x < Center.y ? Center.x : Center.y) / 4;
  193. }
  194.  
  195. //do any hWindow required tasks here
  196. void TAClockWindow::SetupWindow()
  197. {
  198.   //set the timer - get called every minute
  199.   if (!SetTimer(HWindow, 1, 60000U, NULL))
  200.     MessageBox(HWindow, "Too Many Timers", "Cuckoo Clock", MB_ICONEXCLAMATION | MB_OK);
  201.   //create the pens for the clock hands
  202.   hpHour = CreatePen(PS_SOLID, 4, 0x00FFFF);
  203.   hpMinute = CreatePen(PS_SOLID, 4, 0xFF00FFUL);
  204. }
  205.  
  206.  
  207. //Paint the clock background
  208. void TAClockWindow::Paint(HDC PaintDC, PAINTSTRUCT _FAR &)
  209. {
  210.   //paint the face
  211.   phbmFace->Display(PaintDC, 0, 0);
  212.   //paint the first time
  213.   gettime(&LastTime);
  214.   PaintHands(PaintDC, LastTime);
  215. }
  216.  
  217.  
  218. void TAClockWindow::PaintHands(HDC hDC, struct time theTime)
  219. {
  220.   float hourAngle, minuteAngle;
  221.   POINT hourPt, minutePt;
  222.  
  223.   //compute the location of the hands
  224.   minuteAngle = theTime.ti_min * pi2 / 60;
  225.   hourAngle = (theTime.ti_hour % 12) * pi2 / 12 + minuteAngle/12;
  226.   hourPt.x = (Radius/2)*sin(hourAngle) + Center.x;
  227.   hourPt.y = (-Radius/2)*cos(hourAngle) + Center.y;
  228.   minutePt.x = Radius*sin(minuteAngle) + Center.x;
  229.   minutePt.y = -Radius*cos(minuteAngle) + Center.y;
  230.  
  231.   //now draw them.  Note use of XOR
  232.   SetROP2(hDC, R2_XORPEN);
  233.   SelectObject(hDC, hpHour);
  234.   MoveTo(hDC, Center.x, Center.y);
  235.   LineTo(hDC, hourPt.x, hourPt.y);
  236.   SelectObject(hDC, hpMinute);
  237.   MoveTo(hDC, Center.x, Center.y);
  238.   LineTo(hDC, minutePt.x, minutePt.y);
  239. }
  240.  
  241. void TAClockWindow::Chime(void)
  242. {
  243.   //stop any currently playing sound
  244.   sndPlaySound(NULL,0);
  245.   //play a sound, based on the WIN.INI entry
  246.   //to use a disk file, specify the disk file instead of the
  247.   //WIN.INI entry, such as "tada.wav"
  248.   sndPlaySound("SystemAsterisk", SND_ASYNC);
  249. }
  250.  
  251.  
  252. void TAClockWindow::Cuckoo(void)
  253. {
  254.   //stop any currently playing sound
  255.   sndPlaySound(NULL,0);
  256.   //play a sound, based on the WIN.INI entry
  257.   sndPlaySound("SystemExclamation", SND_ASYNC);
  258.   // do cute graphics
  259.   HDC hDC = GetDC(HWindow);
  260.   panCuckoo->Display(hDC, 55, 125);
  261.   //force a repaint of the whole thing
  262.   InvalidateRect(HWindow, NULL, 1);
  263.   ReleaseDC(HWindow, hDC);
  264. }
  265.  
  266. //Called each minute - paints hands
  267. void TAClockWindow::WMTimer(RTMessage)
  268. {
  269.   HDC hDC = GetDC(HWindow);
  270.   PaintHands(hDC, LastTime);
  271.   gettime(&LastTime);
  272.   PaintHands(hDC, LastTime);
  273.   ReleaseDC(HWindow, hDC);
  274.  
  275.   //Every hour play a chime
  276.   if (LastTime.ti_min == 0)
  277.      Chime();
  278.  
  279.   //if midnight, time for the cuckoo
  280.   if ((LastTime.ti_hour == 0) && (LastTime.ti_min == 0))
  281.      Cuckoo();
  282. }
  283.  
  284. //About dialog box
  285. void TAClockWindow::CMAbout(RTMessage)
  286. {
  287.   GetApplication()->ExecDialog(new TDialog(this, "ABOUT"));
  288. }
  289.  
  290. // Menu item Effect:Chime
  291. void TAClockWindow::CMEffectChime(RTMessage)
  292. {
  293.    Chime();
  294. }
  295.  
  296. // Menu item Effect:Cuckoo
  297. void TAClockWindow::CMEffectCuckoo(RTMessage)
  298. {
  299.    Cuckoo();
  300. }
  301.  
  302.  
  303. void TAClockApp::InitMainWindow()
  304. {
  305.   MainWindow = new TAClockWindow(NULL, "Cuckoo Clock");
  306. }
  307.  
  308. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  309.   LPSTR lpCmdLine, int nCmdShow)
  310. {
  311.   HINSTANCE hBorLibrary;
  312.  
  313.   //Load Borland custom controls
  314.   hBorLibrary = LoadLibrary("bwcc.dll");
  315.   if((int) hBorLibrary <= 32)
  316.      MessageBox(NULL, "Unable to load Borland Controls", "System Error", MB_OK | MB_ICONHAND);
  317.  
  318.   TAClockApp AClockApp("Cuckoo Clock", hInstance, hPrevInstance,
  319.     lpCmdLine, nCmdShow);
  320.   AClockApp.Run();
  321.  
  322.   //Free Borland custom controls
  323.   if((int) hBorLibrary > 32)
  324.      FreeLibrary(hBorLibrary);
  325.   return AClockApp.Status;
  326. }
  327.