/src/NaiveEmitter.cpp

https://github.com/onedayitwillmake/NaiveEmitter · C++ · 147 lines · 94 code · 27 blank · 26 comment · 10 complexity · c904416f16f98c831c7d9502818f8515 MD5 · raw file

  1. /**
  2. * EclipseLovesCinder example application
  3. *
  4. **********************************************************
  5. **********************************************************
  6. ********************* IMPORTANT **************************
  7. * On first run:
  8. * - Select Project -> Clean...
  9. *
  10. * - If change the project name, you have to tell the debugger where the new one is:
  11. * Go into Run -> Debug Configurations
  12. * Set where the application lives to something like this
  13. * Debug/{APPNAME}/Contents/MacOS/{APPNAME}
  14. *
  15. **********************************************************
  16. **********************************************************
  17. *
  18. * This project is released under public domain, do whatever with it.
  19. *
  20. *
  21. * Mario Gonzalez
  22. * http://onedayitwillmake
  23. */
  24. #include "cinder/app/AppBasic.h"
  25. #include "cinder/gl/gl.h"
  26. #include "cinder/gl/Texture.h"
  27. #include "cinder/ImageIo.h"
  28. #include "cinder/Vector.h"
  29. #include "cinder/Color.h"
  30. #include "cinder/app/MouseEvent.h"
  31. #include "cinder/Rand.h"
  32. #include "Resources.h"
  33. #include <sstream>
  34. #include <vector>
  35. #include <gl.h>
  36. #include "ParticleSystem.h"
  37. class NaiveEmitter : public ci::app::AppBasic {
  38. public:
  39. void setup();
  40. void prepareSettings( ci::app::AppBasic::Settings *settings );
  41. void mouseDown( ci::app::MouseEvent event );
  42. void update();
  43. void draw();
  44. void shutdown();
  45. ci::gl::Texture texture;
  46. std::vector<particle::ParticleSystem*> emitterList;
  47. size_t totalParticleCount;
  48. };
  49. void NaiveEmitter::prepareSettings( ci::app::AppBasic::Settings *settings ) {
  50. settings->setWindowSize( 800, 600 );
  51. }
  52. void NaiveEmitter::setup() {
  53. totalParticleCount = 0;
  54. // Test loading a texture
  55. ci::gl::Texture::Format format;
  56. format.enableMipmapping( false );
  57. format.setMinFilter( GL_NEAREST );
  58. format.setMagFilter( GL_NEAREST );
  59. texture = ci::gl::Texture( ci::loadImage( loadResource( RES_WHEEL ) ), format );
  60. for( size_t i = 0; i < 300; ++i ) {
  61. emitterList.push_back( new particle::ParticleSystem() );
  62. }
  63. }
  64. void NaiveEmitter::mouseDown( ci::app::MouseEvent event ) {
  65. for(std::vector< particle::ParticleSystem*>::iterator itr = emitterList.begin(); itr != emitterList.end(); ++itr ) {
  66. (*itr)->clear();
  67. }
  68. }
  69. void NaiveEmitter::update() {
  70. totalParticleCount = 0;
  71. for(std::vector<particle::ParticleSystem*>::iterator itr = emitterList.begin(); itr != emitterList.end(); ++itr ) {
  72. (*itr)->update();
  73. totalParticleCount += (*itr)->particles.size();
  74. }
  75. }
  76. void NaiveEmitter::draw() {
  77. // clear out the window with black
  78. ci::Color aColor = ci::Color( 0, 0, 0 );
  79. ci::gl::clear( ci::Color( 0, 0, 0 ) );
  80. ci::gl::enableAdditiveBlending();
  81. if( emitterList.size() == 0 ) return;
  82. particle::ParticleSystem* emitter = emitterList.at( ci::Rand::randInt(0, emitterList.size() - 1 ) );
  83. if ( texture ) {
  84. ci::gl::color( ci::ColorA(1.0f, 1.0f, 1.0f, 1.0f) );
  85. const float scale = ci::Rand::randFloat(0.1, 1.5);
  86. const float halfWidth = texture.getCleanWidth() / 2.0f * scale;
  87. const float halfHeight = texture.getCleanHeight() / 2.0f * scale;
  88. ci::Vec2f pos = getMousePos();
  89. const ci::Area srcArea = ci::Area( texture.getCleanBounds() );
  90. ci::Rectf destRect = ci::Rectf( pos.x-halfWidth, pos.y-halfHeight, pos.x + halfWidth, pos.y + halfHeight);
  91. const ci::Rectf srcCoords = texture.getAreaTexCoords( srcArea );
  92. // Add a particle to any random emitter
  93. emitter->add( pos, ci::Rand::randVec2f() * 1.5, srcCoords, destRect );
  94. texture.enableAndBind();
  95. glEnableClientState( GL_VERTEX_ARRAY );
  96. glEnableClientState( GL_TEXTURE_COORD_ARRAY );
  97. glEnableClientState( GL_COLOR_ARRAY );
  98. for(std::vector<particle::ParticleSystem*>::const_iterator itr = emitterList.begin(); itr != emitterList.end(); ++itr ) {
  99. glVertexPointer( 2, GL_FLOAT, 0, &((*itr)->verts)[0] );
  100. glTexCoordPointer( 2, GL_FLOAT, 0, &((*itr)->texCoords)[0] );
  101. glColorPointer( 4, GL_FLOAT, 0, &((*itr)->colors)[0].r );
  102. glDrawArrays( GL_TRIANGLES, 0, (*itr)->verts.size() / 2 );
  103. }
  104. glDisableClientState( GL_VERTEX_ARRAY );
  105. glDisableClientState( GL_TEXTURE_COORD_ARRAY );
  106. glDisableClientState( GL_COLOR_ARRAY );
  107. texture.disable();
  108. ci::gl::color( ci::ColorA(1.0f, 1.0f, 1.0f, 1.0f) );
  109. ci::gl::enableAlphaBlending();
  110. static ci::Font font = ci::Font( "monaco", 10.0f );
  111. std::stringstream ss;
  112. ss << totalParticleCount << std::endl;
  113. ci::gl::drawString( ss.str(), ci::Vec2i(5, 5), ci::ColorA(1,1,1,1), font );
  114. }
  115. }
  116. void NaiveEmitter::shutdown() {
  117. std::cout << "Shutdown..." << std::endl;
  118. AppBasic::shutdown();
  119. }
  120. CINDER_APP_BASIC( NaiveEmitter, ci::app::RendererGl )