Terrain.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. #include "Terrain.h"
  2. Terrain::Terrain()
  3. : m_size( 0 ), m_multi(MULTI), m_plot( 0x0 ), m_areaBody( 0x0 ),
  4. m_minX( 0.0f ), m_minY( 0.0f ), m_maxX( 0.0f ), m_maxY( 0.0f ),
  5. m_backTexture( 0x0 ), m_groundTexture( 0x0 ), m_bricks( 0x0 ), m_buff1( 0x0 ), m_buff2( 0x0 )
  6. {
  7. // Constructor
  8. }
  9. Terrain::~Terrain()
  10. {
  11. // Points
  12. if ( m_plot )
  13. {
  14. delete[] m_plot ;
  15. m_plot = 0x0 ;
  16. }
  17. // Textures
  18. if ( m_groundTexture )
  19. {
  20. SDL_FreeSurface( m_groundTexture );
  21. m_groundTexture = 0x0;
  22. }
  23. if ( m_backTexture )
  24. {
  25. SDL_FreeSurface( m_backTexture );
  26. m_backTexture = 0x0;
  27. }
  28. // Buffers
  29. if ( m_buff1 )
  30. {
  31. SDL_FreeSurface( m_buff1 );
  32. m_buff1 = 0x0;
  33. }
  34. if ( m_buff2 )
  35. {
  36. SDL_FreeSurface( m_buff2 );
  37. m_buff2 = 0x0;
  38. }
  39. if ( m_bricks )
  40. {
  41. SDL_FreeSurface( m_bricks );
  42. m_bricks = 0x0;
  43. }
  44. }
  45. void Terrain::defaultPlot()
  46. {
  47. // Safety
  48. if ( m_plot )
  49. {
  50. std::cout << "Données déjà existantes ; échec Terrain::defaultPlot()." << std::endl;
  51. return;
  52. }
  53. // Tableau
  54. m_size = 400;
  55. m_plot = new b2Vec2[m_size];
  56. // Premiers points
  57. m_plot[0].x = 0.0f;
  58. m_plot[0].y = 0.0f;
  59. m_plot[1].x = 0.0f;
  60. m_plot[1].y = 2.75f;
  61. // Remplissage
  62. for ( unsigned int i(2); i < m_size; i++ )
  63. {
  64. m_plot[i].x = 1.0f * i;
  65. m_plot[i].y = 2.75f + 1.2f * sin( 0.05f * std::pow( i, 1.35f ) );
  66. }
  67. }
  68. void Terrain::build( b2World &world )
  69. {
  70. // Check previous body
  71. if ( m_areaBody )
  72. {
  73. world.DestroyBody( m_areaBody );
  74. m_areaBody = nullptr;
  75. }
  76. // Build fixture
  77. b2BodyDef areaDef;
  78. areaDef.position.Set(0.0f, 0.0f);
  79. m_areaBody = world.CreateBody(&areaDef);
  80. b2ChainShape chain;
  81. b2Vec2 prev(m_plot[0]), next(m_plot[m_size - 1]);
  82. chain.CreateChain( m_plot, m_size, prev, next );
  83. m_areaBody->CreateFixture( &chain, 0.0f );
  84. }
  85. void Terrain::textureLoad( SDL_Surface* screen )
  86. {
  87. // Load texture ground
  88. if ( m_groundTexture == 0x0 )
  89. m_groundTexture = IMG_Load( "Textures/brick052b.jpg" );
  90. if ( m_groundTexture == 0x0 )
  91. std::cout << "Texture " << "Textures/brick052b.jpg" << " impossible à charger." << std::endl;
  92. // Load texture background
  93. if ( m_backTexture == 0x0 )
  94. m_backTexture = IMG_Load( "Textures/ChatoLine.jpg" );
  95. if ( m_backTexture == 0x0 )
  96. std::cout << "Texture " << "Textures/ChatoLine.jpg" << " impossible à charger." << std::endl;
  97. // Create buffers
  98. if ( m_buff1 == 0x0 )
  99. m_buff1 = SDL_CreateRGBSurface( SDL_HWSURFACE, screen->w, screen->h, 32, 0, 0, 0, 0);
  100. SDL_SetColorKey( m_buff1, SDL_SRCCOLORKEY, SDL_MapRGB(screen->format, 0, 0, 0) );// 255 128 0
  101. if ( m_buff2 == 0x0 )
  102. m_buff2 = SDL_CreateRGBSurface( SDL_HWSURFACE, screen->w, screen->h, 32, 0, 0, 0, 0);
  103. if ( m_bricks == 0x0 )
  104. m_bricks = SDL_CreateRGBSurface( SDL_HWSURFACE, screen->w, screen->h, 32, 0, 0, 0, 0);
  105. // Fill up bricks
  106. if ( m_groundTexture == 0x0 )
  107. return;
  108. SDL_Rect pos({0, 0, 0, 0});
  109. for ( pos.y = 0; pos.y < screen->h; pos.y += m_groundTexture->h )
  110. for ( pos.x = 0; pos.x < screen->w; pos.x += m_groundTexture->w )
  111. SDL_BlitSurface( m_groundTexture, 0x0, m_bricks, &pos );
  112. // Optimisation
  113. accelerateTexture( m_backTexture );
  114. accelerateTexture( m_groundTexture );
  115. accelerateTexture( m_bricks );
  116. accelerateTexture( m_buff1 );
  117. accelerateTexture( m_buff2 );
  118. }
  119. void Terrain::accelerateTexture( SDL_Surface* & text )
  120. {
  121. if ( !text )
  122. return;
  123. SDL_Surface* temp( text );
  124. text = SDL_DisplayFormat( temp );
  125. SDL_FreeSurface( temp );
  126. }
  127. void Terrain::draw( SDL_Surface* screen, b2Vec2 origin )
  128. {
  129. Sint16 xTab[4];
  130. Sint16 yTab[4];
  131. for ( unsigned int i(0); i < m_size - 1 ; i++ )
  132. {
  133. xTab[0] = xTab[3] = ( m_plot[i].x - origin.x ) * m_multi ;
  134. xTab[1] = xTab[2] = ( m_plot[i + 1].x - origin.x ) * m_multi ;
  135. if ( ! (xTab[1] > 0 && xTab[0] < screen->w ) )
  136. continue ;
  137. yTab[0] = ( m_plot[i].y - origin.y ) * m_multi ;
  138. yTab[1] = ( m_plot[i + 1].y - origin.y ) * m_multi ;
  139. yTab[2] = screen->h;
  140. yTab[3] = screen->h;
  141. filledPolygonRGBA( screen, xTab, yTab, 4, 255, 0, 0, 255);
  142. }
  143. }
  144. void Terrain::drawUni( SDL_Surface* screen, b2Vec2 origin )
  145. {
  146. // Variables
  147. m_vx.clear();
  148. m_vy.clear();
  149. Sint16 x, y;
  150. // Incrément
  151. unsigned int i(1);
  152. // Recherche du premier point dans le champ
  153. while ( m_plot[i].x < origin.x )
  154. i ++;
  155. // Revenir au dernier point hors champ
  156. i --;
  157. // Premiers points du polygone
  158. x = ( m_plot[i].x - origin.x ) * m_multi;
  159. m_vx.push_back( x );
  160. m_vy.push_back( screen-> h );
  161. // Ajout des points visibles
  162. for ( ; i < m_size && m_vx.back() < screen->w ; i++ )
  163. {
  164. x = ( m_plot[i].x - origin.x ) * m_multi;
  165. y = ( m_plot[i].y - origin.y ) * m_multi ;
  166. m_vx.push_back( x );
  167. m_vy.push_back( y );
  168. }
  169. // Ajout du dernier point
  170. m_vx.push_back( x );
  171. m_vy.push_back( screen-> h );
  172. // Dessin
  173. filledPolygonRGBA( screen, m_vx.data(), m_vy.data(), m_vx.size(), 255, 128, 0, 255 );
  174. }
  175. void Terrain::drawWithTexture( SDL_Surface* screen, b2Vec2 origin )
  176. {
  177. // Load texture
  178. if ( m_groundTexture == 0x0 )
  179. drawUni( screen, origin );
  180. // Variables
  181. m_vx.clear();
  182. m_vy.clear();
  183. Sint16 x, y;
  184. // Incrément
  185. unsigned int i(0);
  186. // Recherche du premier point dans le champ
  187. while ( m_plot[i].x < origin.x )
  188. i ++;
  189. // Revenir au dernier point hors champ
  190. //i --;
  191. // Premiers points du polygone
  192. x = ( m_plot[i].x - origin.x ) * m_multi;
  193. m_vx.push_back( x );
  194. m_vy.push_back( screen-> h - 5 );
  195. // Ajout des points visibles
  196. for ( ; i < m_size && m_vx.back() < screen->w ; i++ )
  197. {
  198. x = ( m_plot[i].x - origin.x ) * m_multi;
  199. y = ( m_plot[i].y - origin.y ) * m_multi ;
  200. m_vx.push_back( x );
  201. m_vy.push_back( y );
  202. }
  203. // Debug
  204. m_vx.pop_back();
  205. m_vy.pop_back();
  206. // Ajout du dernier point
  207. m_vx.push_back( ( m_plot[i - 2].x - origin.x ) * m_multi );
  208. m_vy.push_back( screen-> h - 5 );
  209. // Dessin
  210. texturedPolygon( screen, m_vx.data(), m_vy.data(), m_vx.size(), m_groundTexture, -origin.x * m_multi, origin.y * m_multi);
  211. polygonRGBA( screen, m_vx.data(), m_vy.data(), m_vx.size(), 255, 0, 0, 255 );
  212. }
  213. void Terrain::drawArtist( SDL_Surface* screen, b2Vec2 origin )
  214. {
  215. // Process first buffer
  216. SDL_Rect pos;
  217. pos.x = - ( origin.x - m_minX ) * ( m_backTexture->w - screen->w ) / ( m_maxX - m_minX );
  218. pos.y = - ( origin.y - m_minY ) * ( m_backTexture->h - screen->h ) / ( m_maxY - m_minY );
  219. //SDL_FillRect(m_buff1, 0, 0x00000000 );
  220. SDL_BlitSurface( m_backTexture, 0x0, screen, & pos );
  221. drawUni( screen, origin );
  222. // Process second buffer
  223. /*SDL_BlitSurface( m_bricks, 0x0, m_buff2, 0x0 );
  224. SDL_BlitSurface( m_buff1, 0x0, m_buff2, 0x0 );*/
  225. // Final rendering
  226. //SDL_BlitSurface( m_buff2, 0x0, screen, 0x0 );
  227. }
  228. void Terrain::load(std::string chemin)
  229. {
  230. // Flux
  231. std::ifstream loadStream( chemin.c_str() );
  232. // Lecture
  233. float buff;
  234. std::vector<float> data;
  235. while ( loadStream >> buff )
  236. data.push_back( buff );
  237. loadStream.close();
  238. // Remplacment
  239. if ( m_plot )
  240. {
  241. delete[] m_plot;
  242. m_plot = 0x0;
  243. }
  244. // Création
  245. m_size = data.size() / 2;
  246. m_plot = new b2Vec2[m_size];
  247. // Remplissage
  248. for ( unsigned int i(0); i < m_size; i++ )
  249. {
  250. m_plot[i].x = data[2*i];
  251. m_plot[i].y = data[2*i + 1];
  252. }
  253. // Ajustement du scrolling
  254. setMaxRect();
  255. }
  256. void Terrain::setMaxRect()
  257. {
  258. // Réinitialisation
  259. m_maxX = m_minX = m_maxY = m_minY = 0.0f;
  260. // Recherche des extremums
  261. for ( unsigned int i(0); i < m_size; i++ )
  262. {
  263. if ( m_plot[i].x > m_maxX )
  264. m_maxX = m_plot[i].x;
  265. if ( m_plot[i].x < m_minX )
  266. m_minX = m_plot[i].x;
  267. if ( m_plot[i].y > m_maxY )
  268. m_maxY = m_plot[i].y;
  269. if ( m_plot[i].y < m_minY )
  270. m_minY = m_plot[i].y;
  271. }
  272. // Ajustements
  273. m_minY -= 6.0f;
  274. }
  275. void Terrain::setMulti( float newMulti )
  276. {
  277. m_multi = newMulti;
  278. if ( m_multi <= 0.0f )
  279. m_multi = MULTI;
  280. }
  281. /// END OF FILE