ellipse.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * Tunnel effect made with stroked ellipses.
  3. * Demonstrates different dash pattern effects.
  4. *
  5. */
  6. #include "game.h"
  7. // for readability, the screen and texture sizes are hard-coded
  8. const float SCREEN_WIDTH=640.0f;
  9. const float SCREEN_HEIGHT=360.0f;
  10. const int AMOUNT=30;
  11. const VGfloat backgroundColor[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  12. struct DemoMode
  13. {
  14. VGCapStyle cap;
  15. VGint dashPatternLength;
  16. const float *dashPattern;
  17. };
  18. const float dashPatternDot[]={ 0.0f, 12.0f };
  19. const float dashPatternLine[]={ 15.0f, 5.0f };
  20. const float dashPatternDotLine[]={ 15.0f, 10.0f, 0.0f, 10.0f, 0.0f, 10.0f, };
  21. const DemoMode modes[]={
  22. { VG_CAP_BUTT, 0, NULL},
  23. { VG_CAP_ROUND, 2, dashPatternDot}, // dash length of 0.0 combined with VG_CAP_ROUND gives a round dot
  24. { VG_CAP_BUTT, 2, dashPatternLine},
  25. { VG_CAP_ROUND, 6, dashPatternDotLine},
  26. };
  27. int currentMode=0;
  28. VGPaint paint;
  29. VGPath ellipse;
  30. /** walltime */
  31. float t;
  32. void game_init()
  33. {
  34. }
  35. void game_destroy()
  36. {
  37. }
  38. void game_prepare()
  39. {
  40. vgSeti(VG_RENDERING_QUALITY, VG_RENDERING_QUALITY_BETTER);
  41. paint = vgCreatePaint();
  42. vgSetParameteri(paint, VG_PAINT_TYPE, VG_PAINT_TYPE_COLOR);
  43. vgSetColor(paint, 0xFFFF00FF);
  44. vgSetPaint(paint, VG_STROKE_PATH);
  45. ellipse = vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F, 1.0f, 0.0f, 0, 0, VG_PATH_CAPABILITY_ALL);
  46. vguEllipse(ellipse, 0, 0, 100, 100);
  47. }
  48. void game_release()
  49. {
  50. vgDestroyPath(ellipse);
  51. vgDestroyPaint(paint);
  52. }
  53. void game_update(float walltime)
  54. {
  55. t=walltime*4.0f;
  56. }
  57. void game_render()
  58. {
  59. // clear background using black
  60. vgSetfv(VG_CLEAR_COLOR, 4, backgroundColor);
  61. vgClear(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  62. // set line style here, Qt painter modifies stroke
  63. vgSetPaint(paint, VG_STROKE_PATH);
  64. vgSeti(VG_STROKE_CAP_STYLE, modes[currentMode].cap);
  65. vgSetfv(VG_STROKE_DASH_PATTERN, modes[currentMode].dashPatternLength, modes[currentMode].dashPattern);
  66. vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
  67. vgSeti(VG_BLEND_MODE, VG_BLEND_SCREEN);
  68. for(int i=0;i<AMOUNT;i++)
  69. {
  70. float j = (AMOUNT-i)+(1.0f-fmod(t,1.0f));
  71. int alpha=i*255/(AMOUNT-1);
  72. if(alpha>255) alpha=255;
  73. vgSetColor(paint, 0xe0d32200 + alpha);
  74. vgLoadIdentity();
  75. vgTranslate(SCREEN_WIDTH/2,SCREEN_HEIGHT/2);
  76. float s=(0.00025f+1.0f/(j))*30.0f;
  77. vgSetf(VG_STROKE_LINE_WIDTH, s+1);
  78. vgScale(s,s);
  79. vgTranslate(cos(t*.63f+j*.356f)*25, sin(t*.833f+j*.417f)*20);
  80. vgDrawPath(ellipse, VG_STROKE_PATH);
  81. }
  82. }
  83. void game_event(int type, float x, float y, float z)
  84. {
  85. if(type==TOUCH_PRESS)
  86. {
  87. currentMode = (currentMode+1) % (sizeof(modes)/sizeof(modes[0]));
  88. }
  89. }