![]() |
TX Library Help – Version: 00173a, Revision: 174
|
00001 //{=========================================================================== 00011 // $Copyright: (C) Ded (Ilya Dedinsky, http://txlib.ru) <mail@txlib.ru> $ 00012 //}=========================================================================== 00013 00014 // Press Alt+Ctrl+Shift+F12 to view this info in About Box 00015 00016 #define __MODULE "Example06" 00017 #define __VERSION "1.0" 00018 #define __AUTHOR "Ilya Dedinsky" 00019 #define __DESCRIPTION "Пример использования библиотеки TXLib" 00020 00021 //---------------------------------------------------------------------------- 00022 00023 #include <conio.h> 00024 #include "TXLib.h" 00025 00026 //---------------------------------------------------------------------------- 00027 00028 void DanceMan (int x, int y, int sizeX, int sizeY, double jump, 00029 COLORREF color, COLORREF bkColor, int delay); 00030 00031 void JumpMan (int x, int y, int sizeX, int sizeY, double jump, 00032 COLORREF color, COLORREF bkColor, int jumps, int delay); 00033 00034 void MoveMan (int fromX, int fromY, int toX, int toY, 00035 int sizeX, int sizeY, COLORREF color, COLORREF bkColor, 00036 int time, int steps); 00037 00038 void DrawMan (int x, int y, int sizeX, int sizeY, COLORREF color, 00039 double hand = 0, double legs = 0, double head = 0, double twist = 0); 00040 00041 void AppearEarth (int x, int y, int sizeX, int sizeY, COLORREF from, COLORREF to, 00042 int time, int steps); 00043 00044 void DrawEarth (int x, int y, int sizeX, int sizeY, COLORREF color); 00045 00046 void AppearText (int x, int y, const char* text, COLORREF from, COLORREF to, 00047 int time, int steps); 00048 00049 void DrawHello (int x, int y, const char* text, int size, COLORREF color); 00050 00051 void UnwindFlag (int x, int y, int fromSizeX, int toSizeX, int sizeY, 00052 COLORREF color, COLORREF bkColor, int time, int steps); 00053 00054 void DrawFlag (int x, int y, int sizeX, int sizeY, COLORREF color, COLORREF bkColor); 00055 00056 void DrawFrame (int sizeX, int sizeY, int size, COLORREF color); 00057 00058 int kbget(); 00059 00060 //---------------------------------------------------------------------------- 00061 00062 int main() 00063 { 00064 txCreateWindow (800, 600); 00065 txTextCursor (false); 00066 00067 int sizeX = txGetExtentX(), sizeY = txGetExtentY(); 00068 int centerX = (sizeX+1)/2, centerY = (sizeY+1)/2; 00069 00070 DrawFrame (sizeX, sizeY, 10, TX_WHITE); 00071 00072 txSelectFont ("Times New Roman", 60); 00073 txSetTextAlign (TA_CENTER); 00074 00075 AppearText (centerX, sizeY*4/5, "\"Hello, world!\\n\" :)", TX_BLACK, TX_LIGHTGREEN, 5000, 100); 00076 AppearEarth (centerX, centerY, sizeX/2, sizeY/2, TX_BLACK, TX_LIGHTCYAN, 5000, 100); 00077 00078 MoveMan (20, centerY - sizeY/4, centerX - sizeX/50, centerY - sizeY/4, sizeX/40, sizeY/15, TX_YELLOW, TX_BLACK, 3000, 100); 00079 txSleep (150); 00080 JumpMan (centerX - sizeX/50, centerY - sizeY/4, sizeX/40, sizeY/15, 0.25, TX_YELLOW, TX_BLACK, 10, 100); 00081 00082 txPlaySound ("tada.wav"); // Windows < 7 00083 txPlaySound ("C:\\Windows\\Media\\tada.wav"); // Windows >= 7 00084 00085 UnwindFlag (centerX, centerY - sizeY/4, 0, sizeX/20, sizeY/10, TX_YELLOW, TX_BLACK, 500, 100); 00086 00087 DanceMan (centerX - sizeX/50, centerY - sizeY/4, sizeX/40, sizeY/15, 0.25, TX_YELLOW, TX_BLACK, 200); 00088 00089 return 0; 00090 } 00091 00092 //---------------------------------------------------------------------------- 00093 00094 void JumpMan (int x, int y, int sizeX, int sizeY, double jump, 00095 COLORREF color, COLORREF bkColor, int jumps, int delay) 00096 { 00097 DrawMan (x, y, sizeX, sizeY, TX_BLACK, 0, 0, 0, 0); 00098 00099 txBegin(); 00100 00101 for (int i = 0; i < jumps && !_kbhit(); i++) 00102 { 00103 DrawMan (x, y - (int)(i%2 * jump*10), sizeX, sizeY, color, 00104 (i%2-0.5) * jump/3, 0, (i%2-0.5) * -jump/3, 0); 00105 00106 txSleep (delay); 00107 00108 DrawMan (x, y - (int)(i%2 * jump*10), sizeX, sizeY, bkColor, 00109 (i%2-0.5) * jump/3, 0, (i%2-0.5) * -jump/3, 0); 00110 } 00111 00112 kbget(); 00113 00114 DrawMan (x, y - (int)(jumps%2 * jump*10), sizeX, sizeY, color, 00115 (jumps%2-0.5) * jump/3, 0, (jumps%2-0.5) * -jump/3, 0); 00116 00117 txEnd(); 00118 } 00119 00120 //---------------------------------------------------------------------------- 00121 00122 void DanceMan (int x, int y, int sizeX, int sizeY, double jump, 00123 COLORREF color, COLORREF bkColor, int delay) 00124 { 00125 DrawMan (x, y, sizeX, sizeY, TX_BLACK, 0, 0, 0, 0); 00126 00127 txBegin(); 00128 00129 int i = 0; 00130 for (; !_kbhit(); i++) 00131 { 00132 DrawMan (x, y - (int)(i%2 * jump*10), sizeX, sizeY, color, 00133 (i%2-0.5) * jump/3, 0, (i%2-0.5) * -jump/3, (i%2-0.5) * jump); 00134 00135 txSleep (delay); 00136 00137 DrawMan (x, y - (int)(i%2 * jump*10), sizeX, sizeY, bkColor, 00138 (i%2-0.5) * jump/3, 0, (i%2-0.5) * -jump/3, (i%2-0.5) * jump); 00139 } 00140 00141 kbget(); 00142 00143 DrawMan (x, y - (int)(i%2 * jump*10), sizeX, sizeY, color, 00144 (i%2-0.5) * jump/3, 0, (i%2-0.5) * -jump/3, (i%2-0.5) * jump); 00145 00146 txEnd(); 00147 } 00148 00149 //---------------------------------------------------------------------------- 00150 00151 void MoveMan (int fromX, int fromY, int toX, int toY, 00152 int sizeX, int sizeY, COLORREF color, COLORREF bkColor, 00153 int time, int steps) 00154 { 00155 txBegin(); 00156 00157 for (int i = 0; i <= steps && !_kbhit(); i++) 00158 { 00159 int x = fromX + (toX - fromX) * i/steps, 00160 y = fromY + (toY - fromY) * i/steps; 00161 00162 DrawMan (x, y - i%6, sizeX, sizeY, color, i%3*0.02, i%3*-0.1, i%3*0.1, 0); 00163 00164 txSleep (time / steps); 00165 00166 DrawMan (x, y - i%6, sizeX, sizeY, bkColor, i%3*0.02, i%3*-0.1, i%3*0.1, 0); 00167 } 00168 00169 kbget(); 00170 00171 DrawMan (toX, toY, sizeX, sizeY, color, 0, 0, 0); 00172 00173 txEnd(); 00174 } 00175 00176 //---------------------------------------------------------------------------- 00177 00178 void UnwindFlag (int x, int y, int fromSizeX, int toSizeX, int sizeY, 00179 COLORREF color, COLORREF bkColor, int time, int steps) 00180 { 00181 txBegin(); 00182 00183 for (int i = 0; i <= steps && !_kbhit(); i++) 00184 { 00185 int sizeX = fromSizeX + (toSizeX - fromSizeX) * i/steps; 00186 00187 DrawFlag (x, y, sizeX, sizeY, color, bkColor); 00188 00189 txSleep (time / steps); 00190 00191 DrawFlag (x, y, sizeX, sizeY, bkColor, bkColor); 00192 } 00193 00194 kbget(); 00195 00196 DrawFlag (x, y, toSizeX, sizeY, color, bkColor); 00197 00198 txEnd(); 00199 } 00200 00201 //---------------------------------------------------------------------------- 00202 00203 void DrawMan (int x, int y, int sizeX, int sizeY, COLORREF color, 00204 double hand, double legs, double head, double twist) 00205 { 00206 txSetColor (color); 00207 txSetFillColor (color); 00208 00209 txLine (x + twist*sizeX, y - 0.35*sizeY, x, y - 0.7*sizeY); 00210 00211 txLine (x + twist*sizeX, y - 0.35*sizeY, x - (0.5 + legs) * sizeX, y); 00212 txLine (x + twist*sizeX, y - 0.35*sizeY, x + (0.5 + legs) * sizeX, y); 00213 00214 txLine (x, y - 0.65*sizeY, x - sizeX/2, y - 0.4*sizeY); 00215 txLine (x, y - 0.65*sizeY, x + sizeX/1.2, y - (0.7 + hand) * sizeY); 00216 00217 txCircle (x, y - sizeY + (0.3 + head) * sizeX, 0.3*sizeX); 00218 } 00219 00220 //---------------------------------------------------------------------------- 00221 00222 void DrawEarth (int x, int y, int sizeX, int sizeY, COLORREF color) 00223 { 00224 txSetColor (color); 00225 for (int rx = sizeX/2; rx >= 0; rx -= sizeX/9) txEllipse (x-rx, y-sizeY/2, x+rx, y+sizeY/2); 00226 for (int ry = sizeY/2; ry >= 0; ry -= sizeY/6) txEllipse (x-sizeX/2, y-ry, x+sizeX/2, y+ry); 00227 txLine (x - sizeX/2, y, x + sizeX/2, y); 00228 } 00229 00230 //---------------------------------------------------------------------------- 00231 00232 void AppearEarth (int x, int y, int sizeX, int sizeY, COLORREF from, COLORREF to, 00233 int time, int steps) 00234 { 00235 int r0 = txExtractColor (from, TX_RED), r1 = txExtractColor (to, TX_RED), 00236 g0 = txExtractColor (from, TX_GREEN), g1 = txExtractColor (to, TX_GREEN), 00237 b0 = txExtractColor (from, TX_BLUE), b1 = txExtractColor (to, TX_BLUE); 00238 00239 for (int i = 0; i <= steps && !_kbhit(); i++) 00240 { 00241 int r = r0 + (r1 - r0) * i/steps, 00242 g = g0 + (g1 - g0) * i/steps, 00243 b = b0 + (b1 - b0) * i/steps; 00244 00245 DrawEarth (x, y, sizeX, sizeY, RGB (r, g, b)); 00246 00247 Sleep (time / steps); 00248 } 00249 00250 kbget(); 00251 00252 DrawEarth (x, y, sizeX, sizeY, to); 00253 } 00254 00255 //---------------------------------------------------------------------------- 00256 00257 void AppearText (int x, int y, const char* text, COLORREF from, COLORREF to, 00258 int time, int steps) 00259 { 00260 int r0 = txExtractColor (from, TX_RED), r1 = txExtractColor (to, TX_RED), 00261 g0 = txExtractColor (from, TX_GREEN), g1 = txExtractColor (to, TX_GREEN), 00262 b0 = txExtractColor (from, TX_BLUE), b1 = txExtractColor (to, TX_BLUE); 00263 00264 for (int i = 0; i <= steps && !_kbhit(); i++) 00265 { 00266 int r = r0 + (r1 - r0) * i/steps, 00267 g = g0 + (g1 - g0) * i/steps, 00268 b = b0 + (b1 - b0) * i/steps; 00269 00270 txSetColor (RGB (r, g, b)); 00271 txTextOut (x, y, text); 00272 00273 Sleep (time / steps); 00274 } 00275 00276 kbget(); 00277 00278 txSetColor (to); 00279 txTextOut (x, y, text); 00280 } 00281 00282 //---------------------------------------------------------------------------- 00283 00284 void DrawFlag (int x, int y, int sizeX, int sizeY, COLORREF color, COLORREF bkColor) 00285 { 00286 txSetColor (color); 00287 txSetFillColor (bkColor); 00288 00289 txLine (x, y, x, y - sizeY); 00290 txRectangle (x, y - sizeY/2, x + sizeX, y - sizeY); 00291 00292 txSelectFont ("Times New Roman", sizeX/2 + 1); 00293 txTextOut (x + sizeX/2, y - sizeY*7/8, "C++"); 00294 } 00295 00296 //---------------------------------------------------------------------------- 00297 00298 void DrawHello (int x, int y, const char* text, int size, COLORREF color) 00299 { 00300 txSetColor (color); 00301 00302 txSelectFont ("Times New Roman", size); 00303 txSetTextAlign (TA_CENTER); 00304 00305 txTextOut (x, y, text); 00306 } 00307 00308 //---------------------------------------------------------------------------- 00309 00310 void DrawFrame (int sizeX, int sizeY, int size, COLORREF color) 00311 { 00312 txSetColor (color); 00313 txSetFillColor (TX_TRANSPARENT); 00314 00315 txRectangle (size, size, sizeX-size, sizeY-size); 00316 } 00317 00318 //---------------------------------------------------------------------------- 00319 00320 int kbget() 00321 { 00322 int ch = 0; 00323 while (_kbhit()) ch = _getch(); 00324 return ch; 00325 } 00326 00327 00328 00329