Задача: Постепенное затемнение
Исходник: Постепенное затмение, язык: C++ [code #134, hits: 12221]
автор: - [добавлен: 17.05.2006]
  1. #include <graphics.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <math.h>
  6. #include <dos.h>
  7. // this function initializes graphics mode
  8. // it will work only if you're using Borland C++ compiler & BGI drivers
  9. // if you're using another compiler you should overwrite body of this function
  10. void init_gr(void)
  11. {
  12. /* request autodetection */
  13. int gdriver = DETECT, gmode, errorcode;
  14. /* initialize graphics mode */
  15. initgraph(&gdriver, &gmode, "");
  16. /* read result of initialization */
  17. errorcode = graphresult();
  18. if (errorcode != grOk) /* an error occurred */
  19. {
  20. printf("Graphics error: %s\n", grapherrormsg(errorcode));
  21. printf("Press any key to halt:");
  22. getch();
  23. exit(1); /* return with error code */
  24. }
  25. }
  26. // this function shuts graphics mode down
  27. // it will work only if you're using Borland C++ compiler & BGI drivers
  28. // if you're using another compiler you should overwrite body of this function
  29. void end_gr(void)
  30. {
  31. closegraph();
  32. }
  33. // this function set filling style
  34. // it will work only if you're using Borland C++ compiler & BGI drivers
  35. // if you're using another compiler you should overwrite body of this function
  36. void SetFillStyle(int pattern,int color)
  37. {
  38. setfillstyle(pattern,color);
  39. }
  40. // this function draws a bar
  41. // it will work only if you're using Borland C++ compiler & BGI drivers
  42. // if you're using another compiler you should overwrite body of this function
  43. void Bar(int x1, int y1, int x2, int y2)
  44. {
  45. bar(x1,y1,x2,y2);
  46. }
  47. // this function sets palette entry using RGB color coding
  48. // it will work only if you're using Borland C++ compiler & BGI drivers
  49. // if you're using another compiler you should overwrite body of this function
  50. void SetRGBPalette(int colornum, int red, int green, int blue)
  51. {
  52. setrgbpalette(colornum,red,green,blue);
  53. }
  54. // this function reads palette
  55. // it will work only if you're using Borland C++ compiler & BGI drivers
  56. // if you're using another compiler you should overwrite body of this function
  57. void GetPalette(struct palettetype far *palette)
  58. {
  59. getpalette(palette);
  60. }
  61. #define N 30 // steps of fading to black
  62. struct palettetype pal;
  63. struct {int R;int G;int B;} colors[16];
  64. int main(void)
  65. {
  66. // initializing graphics mode
  67. init_gr();
  68. // creating palette
  69. GetPalette(&pal);
  70. for(int i=0;i<pal.size-1;i++)
  71. {
  72. colors[i+1].R=0;
  73. colors[i+1].G=30-2*i;
  74. colors[i+1].B=15+i*2;
  75. SetRGBPalette(pal.colors[i+1],colors[i+1].R,colors[i+1].G,colors[i+1].B);
  76. }
  77. // drawing picture
  78. for (i=1;i<16;i++)
  79. {
  80. SetFillStyle(SOLID_FILL,i);
  81. Bar((i-1)*43,0,i*43,479);
  82. }
  83. // fading to balck
  84. for (int j=0;j<N;j++)
  85. {
  86. //delay(50);
  87. for (i=1;i<pal.size;i++)
  88. SetRGBPalette(pal.colors[i],
  89. colors[i].R*(1-(float)j/N),
  90. colors[i].G*(1-(float)j/N),
  91. colors[i].B*(1-(float)j/N));
  92. }
  93. // and back to the light
  94. for (j=0;j<N;j++)
  95. {
  96. //delay(50);
  97. for (i=1;i<pal.size;i++)
  98. SetRGBPalette(pal.colors[i],
  99. colors[i].R*(float)j/N,
  100. colors[i].G*(float)j/N,
  101. colors[i].B*(float)j/N);
  102. }
  103. /* clean up */
  104. getch();
  105. end_gr();
  106. return 0;
  107. }
//////////////////////////////////////////////////////////////////////////////
//
// Fade to black (slowly turning the light off)
// (c) Johna Smith, 1996
//
// Method description:
// We need only to decrease Red, Green and Blue components for each color
// in palette. For example, if Red component of the first color was
// initially 60 and we want to turn the light off in 30 steps we should
// decrease Red component by 60/30=2 by step.
// To increase productivity of this program you shouldn't use standart
// BGI functions. Use direct methods instead.
//
//////////////////////////////////////////////////////////////////////////////

+добавить реализацию