PongBackground.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation.
  3. */
  4. #include <stdlib.h>
  5. #include <memory.h>
  6. #include "PongApp.h"
  7. #include "PongBackground.h"
  8. const char *themeTextures[THEME_COUNT][3]={
  9. { "background_space2.png", "background_space1.png", "planet_big.png"},
  10. // { "orange_bg_mountain.png", "orange_bg_space.png", "orange_planet_512x512.png"},
  11. // { "gray_bg_space.png", NULL, "gray_planet1_512x512.png"},
  12. };
  13. PongBackground::PongBackground( int effectIndex, GF::GameEngine *engine, PongApp *app ) {
  14. visible = true;
  15. fade = 0.0f;
  16. pongApp = app;
  17. m_engine = engine;
  18. for (int f=0; f<BG_SPRITECOUNT; f++)
  19. initSprite( &sprites[f], true );
  20. }
  21. void PongBackground::prepare() {
  22. spriteBatch = pongApp->getSpriteBatch();
  23. for(int i=0;i<THEME_COUNT;i++)
  24. {
  25. themeTextureBackground[i] = m_engine->loadGLTexture(themeTextures[i][0]);
  26. themeTextureLayer[i] = m_engine->loadGLTexture(themeTextures[i][1]);
  27. themeTexturePlanet[i] = m_engine->loadGLTexture(themeTextures[i][2]);
  28. #warning planet mipmaps are currently disabled
  29. #if 0
  30. #ifndef USE_GLES11
  31. glGenerateMipmap(GL_TEXTURE_2D); // generate mipmaps for planet sprites
  32. #endif
  33. #if defined(USE_GLES11) || defined(USE_GLES20)
  34. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  35. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
  36. #endif
  37. #endif
  38. }
  39. }
  40. PongBackground::~PongBackground() {
  41. release();
  42. }
  43. void PongBackground::release() {
  44. for(int i=0;i<THEME_COUNT;i++)
  45. {
  46. m_engine->releaseTexture(themeTextureBackground[i]);
  47. m_engine->releaseTexture(themeTextureLayer[i]);
  48. m_engine->releaseTexture(themeTexturePlanet[i]);
  49. }
  50. }
  51. void PongBackground::initSprite( BgSprite *sprite, bool anywhere ) {
  52. sprite->y = -1.0f + (rand() & 255) / 128.0f;
  53. if (anywhere==false)
  54. sprite->x = 1.5f + (rand() & 255 ) / 512.0f;
  55. else
  56. sprite->x = -1.0f + (rand() & 255) / 128.0f;
  57. sprite->speed = ((8+(rand()&255))/2000.0f);
  58. if ((rand() & 255 ) < 128)
  59. sprite->size = (0.1f + ((rand()&255)/255.0f))/4.0f;
  60. else
  61. sprite->size = (0.2f + ((rand()&255)/255.0f))/2.0f;
  62. sprite->distance = 6.0f + (rand()&255)/255.0f * 6.0f;
  63. }
  64. bool PongBackground::update( const float frameTime ) {
  65. if (visible) {
  66. fade+=frameTime;
  67. if (fade>1.0f) fade = 1.0f;
  68. } else {
  69. fade -= frameTime;
  70. if (fade<0.0f) return false;
  71. };
  72. for (int f=0; f<BG_SPRITECOUNT; f++) {
  73. sprites[f].x -= sprites[f].speed*frameTime;
  74. if (sprites[f].x < -1.5f) initSprite( &sprites[f], false );
  75. };
  76. int theme = 0;
  77. if(pongApp->getGame()) theme = pongApp->getGame()->getCurrentLevel()%THEME_COUNT;
  78. texture1 = themeTextureBackground[theme];
  79. texture2 = themeTextureLayer[theme];
  80. sprite1tex = themeTexturePlanet[theme];
  81. return true;
  82. }
  83. void PongBackground::render(float camx, float camy) {
  84. SpriteDrawInfo sdi;
  85. sdi.textureHandle = texture1;
  86. sdi.setTargetPos( camx/14.0f, camy/14.0f );
  87. sdi.setScale(3.2f,1.6f);
  88. spriteBatch->draw( &sdi );
  89. sdi.textureHandle = sprite1tex;
  90. for (int f=0; f<BG_SPRITECOUNT; f++) {
  91. sdi.setScale( 2.0f*sprites[f].size, 2.0f*sprites[f].size );
  92. sdi.setTargetPos( sprites[f].x + camx/sprites[f].distance,
  93. sprites[f].y + camy/sprites[f].distance );
  94. spriteBatch->draw( &sdi );
  95. }
  96. sdi.textureHandle = texture2;
  97. sdi.setScale(4.0f, 2.0f );
  98. sdi.setTargetPos(camx/4, camy/4);
  99. spriteBatch->draw(&sdi);
  100. }