parallax.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * Scrolling plane with perspective projection
  3. *
  4. */
  5. #include "game.h"
  6. // for readability, the screen and texture sizes are hard-coded
  7. const float SCREEN_WIDTH=640.0f;
  8. const float SCREEN_HEIGHT=360.0f;
  9. const float TEXTURE_WIDTH=128.0f;
  10. const float TEXTURE_HEIGHT=128.0f;
  11. const float SCROLL_SPEED=500.0f;
  12. const VGfloat backgroundColor[4] = { 0.0f, 0.2f, 0.3f, 1.0f };
  13. const float z = 0.25;
  14. // projection matrix, which is dynamically updated for scrolling
  15. VGfloat matrix[3*3]={
  16. SCREEN_WIDTH/TEXTURE_WIDTH*z, 0, 0,
  17. SCREEN_WIDTH/TEXTURE_WIDTH/4.0, SCREEN_HEIGHT/TEXTURE_HEIGHT/4.0, 0.5/(TEXTURE_WIDTH),
  18. 0, 3, z,
  19. };
  20. VGImage image;
  21. /** walltime */
  22. float t;
  23. void game_init()
  24. {
  25. }
  26. void game_destroy()
  27. {
  28. }
  29. void game_prepare()
  30. {
  31. vgSeti(VG_RENDERING_QUALITY, VG_RENDERING_QUALITY_FASTER);
  32. vgSeti(VG_IMAGE_QUALITY, VG_IMAGE_QUALITY_NONANTIALIASED);
  33. image = loadVGImage("images/test.png");
  34. }
  35. void game_release()
  36. {
  37. vgDestroyImage(image);
  38. }
  39. void game_update(float walltime)
  40. {
  41. t=walltime;
  42. }
  43. void game_render()
  44. {
  45. // clear background
  46. vgSetfv(VG_CLEAR_COLOR, 4, backgroundColor);
  47. vgClear(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  48. vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
  49. for(float x=-SCREEN_WIDTH-fmod(t*SCROLL_SPEED, SCREEN_WIDTH);x<=2*SCREEN_WIDTH;x+=SCREEN_WIDTH)
  50. {
  51. matrix[6]=x*z; // X scrolling
  52. vgLoadMatrix(matrix);
  53. vgDrawImage(image);
  54. }
  55. }
  56. void game_event(int type, float x, float y, float z)
  57. {
  58. }