edit.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #include "edit.h"
  2. Editor::Editor()
  3. : m_multi( MULTI_DEFAULT ), m_last_save( 0 ), m_help( true ), m_leftPoint( 1 ),
  4. m_index("Niveaux/Level.idx"), m_name(NAME_DEFAULT),
  5. m_screen(nullptr)
  6. {
  7. // Construire les trois points de base
  8. m_vx.push_back(0.0f);
  9. m_vy.push_back(-1.0f);
  10. m_vx.push_back(0.0f);
  11. m_vy.push_back(0.0f);
  12. m_vx.push_back(6.0f);
  13. m_vy.push_back(0.0f);
  14. }
  15. Editor::~Editor()
  16. {
  17. if ( m_screen )
  18. SDL_FreeSurface( m_screen );
  19. }
  20. bool Editor::init()
  21. {
  22. m_screen = SDL_SetVideoMode(1366, 768, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
  23. if ( !m_screen )
  24. {
  25. std::cout << "Unable to set 1366x768 video: " << SDL_GetError() << std::endl;
  26. return false;
  27. }
  28. return true;
  29. }
  30. void Editor::addPlot( int ex, int ey )
  31. {
  32. // Change coordinates
  33. float fx( 1.0f / m_multi * ex);
  34. float fy( 1.0f / m_multi * ey);
  35. // Create a plot
  36. if ( fx > m_vx.back() )
  37. {
  38. m_vx.push_back(fx);
  39. m_vy.push_back(fy);
  40. return;
  41. }
  42. // Move a point
  43. unsigned int id_mov( m_leftPoint );
  44. float dist1( abs(m_vx[id_mov] - fx) );
  45. float dist2;
  46. if ( id_mov < m_vx.size() - 1 )
  47. {
  48. dist2 = abs(m_vx[id_mov + 1] - fx);
  49. while ( id_mov < m_vx.size() - 1 && dist2 <= dist1 )
  50. {
  51. dist1 = dist2;
  52. id_mov++;
  53. dist2 = abs(m_vx[id_mov] - fx);
  54. }
  55. if ( id_mov > 3 )
  56. id_mov --;
  57. else
  58. return;
  59. }
  60. m_vx[id_mov] = fx;
  61. m_vy[id_mov] = fy;
  62. }
  63. void Editor::removePlot()
  64. {
  65. if ( m_vx.size() > 3 )
  66. {
  67. m_vx.pop_back();
  68. m_vy.pop_back();
  69. }
  70. }
  71. void Editor::zoomIn()
  72. {
  73. m_multi += 25;
  74. }
  75. void Editor::zoomOut()
  76. {
  77. if ( m_multi > 25 )
  78. m_multi -= 25;
  79. }
  80. void Editor::draw( int ix, int iy )
  81. {
  82. // Variables
  83. Sint16 x1, y1, x2, y2;
  84. float fx( 1.0f / m_multi * ix);
  85. float fy( 1.0f / m_multi * iy);
  86. // Incrément
  87. unsigned int i(m_leftPoint);
  88. // Recherche du premier hors champ vers la gauche depuis le dernier point connu
  89. while ( i > 1 && m_vx[i] > fx )
  90. i --;
  91. // Recherche du premier point dans le champ
  92. while ( m_vx[i] < fx && i < m_vx.size() )
  93. i ++;
  94. // Mise a jour de l'indice optimal
  95. m_leftPoint = i;
  96. if ( m_leftPoint > 1 && m_leftPoint >= m_vx.size() )
  97. m_leftPoint = m_vx.size() - 1;
  98. // Premiers points du polygone
  99. x1 = ( m_vx[i-1] - fx ) * m_multi;
  100. y1 = ( m_vy[i-1] - fy ) * m_multi;
  101. // Nettoyage
  102. SDL_FillRect(m_screen, 0, SDL_MapRGB(m_screen->format, 0, 0, 128));
  103. // Dessin des points visibles
  104. for ( ; i < m_vx.size() && x1 < m_screen->w ; i++ )
  105. {
  106. x2 = ( m_vx[i] - fx ) * m_multi;
  107. y2 = ( m_vy[i] - fy ) * m_multi;
  108. lineRGBA( m_screen, x1, y1, x2, y2, 255, 255, 0, 255);
  109. x1 = x2;
  110. y1 = y2;
  111. }
  112. // Repère pour le liner
  113. rectangleRGBA( m_screen, 100, 323, 100 + m_multi * 150 / 100, 323 + m_multi * 71 / 100, 255, 128, 0, 255 );
  114. // Infos
  115. if ( m_help )
  116. {
  117. stringRGBA(m_screen, 16, 7, ("Nom de niveau : " + m_name).c_str(), 0, 255, 0, 255 );
  118. stringRGBA(m_screen, 16, 16, ("Deniere sauvegarde : " + std::to_string(m_last_save)).c_str(), 0, 255, 0, 255 );
  119. stringRGBA(m_screen, 16, 25, ("Zoom : " + std::to_string(m_multi)).c_str(), 0, 255, 0, 255 );
  120. stringRGBA(m_screen, 16, 35, "Sauvegarder : s", 0, 255, 0, 255);
  121. stringRGBA(m_screen, 16, 44, "Detruire dernier point : z", 0, 255, 0, 255);
  122. stringRGBA(m_screen, 16, 53, "Deplacer camera : clic droit", 0, 255, 0, 255);
  123. stringRGBA(m_screen, 16, 62, "Ajoute point : clic gauche", 0, 255, 0, 255);
  124. stringRGBA(m_screen, 16, 71, "Activer ou desactiver l'aide : h", 0, 255, 0, 255);
  125. stringRGBA(m_screen, 16, 80, "Charger un niveau : c", 0, 255, 0, 255);
  126. }
  127. // Actualisation
  128. SDL_Flip(m_screen);
  129. }
  130. void Editor::save()
  131. {
  132. // Nom de niveau
  133. if ( m_name == NAME_DEFAULT )
  134. SF_keyName( m_name, m_screen );
  135. // Nom de fichier
  136. std::string chemin( m_name + EXT );
  137. // Index update
  138. SF_addFile( m_index, chemin );
  139. // Chemin
  140. chemin = FOLDER + chemin;
  141. // Flux
  142. std::ofstream writeStream( chemin.c_str() );
  143. // Ecriture
  144. for ( unsigned int i(0); i < m_vx.size(); i ++ )
  145. writeStream << m_vx[i] << ' ' << m_vy[i] << std::endl;
  146. // Sauvegardé !
  147. m_last_save = SDL_GetTicks();
  148. }
  149. void Editor::load()
  150. {
  151. // Pick up a level
  152. std::string chemin;
  153. SF_choose( m_index, chemin, m_screen );
  154. // If given up
  155. if ( chemin == "" )
  156. return;
  157. else
  158. {
  159. chemin.erase( chemin.size() - 4 );
  160. m_name = chemin;
  161. }
  162. // Flux
  163. chemin = FOLDER + chemin + EXT;
  164. std::ifstream loadStream( chemin.c_str() );
  165. // Lecture
  166. float buff;
  167. std::vector<float> data;
  168. while ( loadStream >> buff )
  169. data.push_back( buff );
  170. loadStream.close();
  171. // Remplacment
  172. if ( !m_vx.empty() )
  173. {
  174. m_vx.clear();
  175. m_vy.clear();
  176. }
  177. // Remplissage
  178. for ( unsigned int i(0); i < data.size() / 2; i++ )
  179. {
  180. m_vx.push_back( data[2*i] );
  181. m_vy.push_back( data[2*i + 1] );
  182. }
  183. }
  184. void Editor::toggleHelp()
  185. {
  186. m_help = !m_help;
  187. }