vgroids.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * Simple OpenVG example which renders a bitmap background and a string of spheres following a
  3. * sine curve.
  4. *
  5. * This code runs about 25 fps on Nokia 500 device, 60 fps on Nokia E7 device.
  6. *
  7. */
  8. #include <QEvent>
  9. #include "game.h"
  10. #include "droidworld.h"
  11. #include <qDebug>
  12. // for readability, the screen and texture sizes are hard-coded
  13. const float BACKGROUND_WIDTH=1024;
  14. const float BALL_WIDTH=128.0f;
  15. const float BALL_HEIGHT=128.0f;
  16. const float SCREEN_WIDTH=640.0f;
  17. const float SCREEN_HEIGHT=360.0f;
  18. const float BACKGROUND_SCROLL_SPEED=50.0f;
  19. const float BALL_PHASE=0.0111f;
  20. const float BALL_SCALE=120.0f;
  21. // image resources
  22. VGImage bg;
  23. // Droid world
  24. DroidWorld *world;
  25. // Player object
  26. DroidObject *playerObject;
  27. /** walltime */
  28. float t;
  29. void game_init()
  30. {
  31. // Create the world.
  32. world = new DroidWorld(SCREEN_WIDTH, SCREEN_HEIGHT );
  33. // Add the player object
  34. playerObject = world->addObject( new DroidObject(world, ePLAYER ) );
  35. // add some random asteroids
  36. for (int f=0; f<8; f++)
  37. world->addObject( new DroidObject( world, eASTEROID ));
  38. // add a planet
  39. world->addObject( new DroidObject( world, ePLANET ));
  40. }
  41. void game_destroy()
  42. {
  43. delete world;
  44. }
  45. void game_prepare()
  46. {
  47. vgSeti(VG_RENDERING_QUALITY, VG_RENDERING_QUALITY_BETTER);
  48. vgSeti(VG_IMAGE_QUALITY, VG_IMAGE_QUALITY_BETTER);
  49. world->prepeare();
  50. bg = loadVGImage("images/orange_bg_mountain.png");
  51. }
  52. void game_release()
  53. {
  54. world->release();
  55. vgDestroyImage(bg); bg=VG_INVALID_HANDLE;
  56. }
  57. void game_update(float walltime)
  58. {
  59. t=walltime;
  60. static float extime = walltime;
  61. float deltaTime = walltime - extime;
  62. extime = walltime;
  63. if (deltaTime>0.25f) deltaTime = 0.25f;
  64. world->run( deltaTime );
  65. }
  66. void game_render()
  67. {
  68. // clear background using black
  69. VGfloat color[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
  70. vgSetfv( VG_CLEAR_COLOR, 4, color );
  71. vgClear( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  72. // render scrolling background bitmap (twice for looping effect)
  73. vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
  74. vgLoadIdentity();
  75. vgTranslate(-fmod(t*BACKGROUND_SCROLL_SPEED,BACKGROUND_WIDTH), 0);
  76. vgDrawImage(bg);
  77. vgTranslate(BACKGROUND_WIDTH, 0);
  78. vgDrawImage(bg);
  79. world->draw();
  80. /*
  81. // render sine spheres
  82. for(int i=-BALL_WIDTH/2;i<SCREEN_WIDTH+BALL_WIDTH/2;i+=BALL_WIDTH/4)
  83. {
  84. vgLoadIdentity();
  85. vgTranslate(i, (SCREEN_HEIGHT-BALL_HEIGHT)/2+sin(t+i*BALL_PHASE)*BALL_SCALE);
  86. vgDrawImage(ball);
  87. }
  88. */
  89. }
  90. void game_event(int type, float x, float y, float z)
  91. {
  92. switch (type) {
  93. case Qt::TouchPointPressed:
  94. world->addObject( new DroidObject( world, eASTEROID ))->getPosition() =
  95. QVector3D( (x/SCREEN_WIDTH-0.5f), -(y/SCREEN_HEIGHT - 0.5f), 0);
  96. break;
  97. /*
  98. Qt::TouchPointMoved 0x02 The touch point moved.
  99. Qt::TouchPointStationary 0x04 The touch point did not move.
  100. Qt::TouchPointReleased
  101. */
  102. }
  103. }