![]() |
TX Library Help – Version: 00173a, Revision: 174
|
00001 //{=========================================================================== 00008 // (C) Георгий Шкуратов, 7 класс, 2009 00009 //}=========================================================================== 00010 00011 #include "TXLib.h" 00012 00013 #if !defined (_TX_VER) || (_TX_VER < 0x172a0000) 00014 #error Must use TXLib.h version >= 1.72 to compile this. -- Ded 00015 #endif 00016 00017 //============================================================================ 00018 00019 double Random (double min, double max); 00020 POINT TxGetWindowOrg(); 00021 00022 unsigned long Time = 0; 00023 00024 POINT WindowSize = {0}; 00025 00026 //============================================================================ 00027 00028 struct Ball_t 00029 { 00030 double x_; 00031 double y_; 00032 double vx_; 00033 double vy_; 00034 double r_; 00035 COLORREF color_; 00036 00037 Ball_t(); 00038 00039 void draw (bool shading) const; 00040 void move (const POINT& d); 00041 }; 00042 00043 //---------------------------------------------------------------------------- 00044 00045 Ball_t::Ball_t() : 00046 x_ (Random (100, WindowSize.x - 100)), 00047 y_ (Random (100, WindowSize.y - 100)), 00048 vx_ (Random ( 5, 15)), 00049 vy_ (Random ( 5, 15)), 00050 r_ (Random ( 10, 30)), 00051 color_ (RGB (Random (50, 255), Random (50, 255), Random (50, 255))) 00052 {} 00053 00054 //---------------------------------------------------------------------------- 00055 00056 void Ball_t::draw (bool shading) const 00057 { 00058 if (shading) 00059 { 00060 double r = txExtractColor (color_, TX_RED) / 255.0, 00061 g = txExtractColor (color_, TX_GREEN) / 255.0, 00062 b = txExtractColor (color_, TX_BLUE) / 255.0; 00063 00064 for (double t = 0; t <= 1; t += 1.0/10) 00065 { 00066 double c = pow (t + 0.05, 0.25); 00067 txColor (r*c, g*c, b*c); 00068 txFillColor (r*c, g*c, b*c); 00069 txCircle (x_, y_, r_ * (1-t) + 0.5); 00070 00071 if (GetTickCount() > Time) break; 00072 } 00073 } 00074 else 00075 { 00076 txSetColor (color_); 00077 txSetFillColor (color_); 00078 txCircle (x_, y_, r_); 00079 } 00080 } 00081 00082 //---------------------------------------------------------------------------- 00083 00084 #define AY ( 0.7 ) 00085 #define DT ( 1.0 ) 00086 #define ELASTICITY ( 0.6 + Random (-0.2, +0.1) ) 00087 00088 void Ball_t::move (const POINT& d) 00089 { 00090 x_ -= d.x; 00091 y_ -= d.y; 00092 00093 y_ += vy_ * DT; 00094 x_ += vx_ * DT; 00095 00096 vy_ += AY; 00097 00098 if (x_ < 0 + r_) { x_ = 0 + r_; vx_ = -vx_ * ELASTICITY + d.x; } 00099 if (y_ < 0 + r_) { y_ = 0 + r_; vy_ = -vy_ * ELASTICITY + d.y; } 00100 if (x_ > WindowSize.x - r_) { x_ = WindowSize.x - r_; vx_ = -vx_ * ELASTICITY + d.x; } 00101 if (y_ > WindowSize.y - r_) { y_ = WindowSize.y - r_; vy_ = -vy_ * ELASTICITY + d.y; } 00102 } 00103 00104 //============================================================================ 00105 00106 POINT TxGetWindowOrg() 00107 { 00108 RECT r = {0, 0, 0, 0}; 00109 GetWindowRect (txWindow(), &r); 00110 POINT org = { r.left, r.top }; 00111 return org; 00112 } 00113 00114 inline double Random (double min, double max) 00115 { 00116 return min + (max - min) * rand() / RAND_MAX; 00117 } 00118 00119 //============================================================================ 00120 00121 #define SLEEP_TIME 20 00122 00123 int main() 00124 { 00125 srand ((unsigned) time (NULL)); 00126 00127 txCreateWindow (700, 500); 00128 00129 WindowSize = txGetExtent(); 00130 POINT org1 = TxGetWindowOrg(); 00131 00132 Ball_t balls[20]; 00133 00134 txBegin(); 00135 00136 while (!GetAsyncKeyState (VK_ESCAPE)) 00137 { 00138 Time = GetTickCount() + SLEEP_TIME; 00139 00140 txSetFillColor (TX_BLACK); 00141 if (!GetAsyncKeyState (VK_CONTROL)) txClear(); 00142 00143 txDrawText (0, WindowSize.y * 4/10, WindowSize.x, WindowSize.y * 6/10, 00144 "Move window, shake the balls.\n" "[Ctrl] paints, [Esc] exits"); 00145 00146 POINT org0 = org1; org1 = TxGetWindowOrg(); 00147 POINT d = { org1.x - org0.x, org1.y - org0.y }; 00148 00149 for (unsigned i = 0; i < SIZEARR (balls); i++) balls[i].move (d); 00150 for (unsigned i = 0; i < SIZEARR (balls); i++) balls[i].draw (true); 00151 00152 txSleep (0); 00153 00154 char s[100] = ""; sprintf (s, "+%03lu ms free", Time - GetTickCount()); 00155 SetWindowText (txWindow(), s); 00156 00157 while (GetTickCount() < Time) Sleep (0); 00158 } 00159 00160 txEnd(); 00161 00162 _txExit = true; 00163 return 0; 00164 }