Sliding.cpp
上传用户:sz83729876
上传日期:2013-03-07
资源大小:4140k
文件大小:4k
源码类别:

OpenGL

开发平台:

Windows_Unix

  1. #include "../../main.h"
  2. #include "sliding.h"
  3. #include "core.cpp"
  4. #include "rendering.cpp"
  5. extern NIMG Sliding;
  6. GLuint texture[1];
  7. GLfloat xpos=0;
  8. GLuint  base;      
  9. GLfloat fMove=0;
  10. GLfloat rot=0;
  11. BYTE map[50][41];
  12. GLfloat brmap[50][41];
  13. GLint nmap[50][41];
  14. GLuint speedmap[50][2];
  15. UINT Tick1=0, Tick2=0;   // used to help keep speed constant
  16. UINT DTick;
  17. namespace sliding
  18. {
  19. void Init() {
  20. LoadLevel();
  21. }
  22. GLvoid glSetChar(unsigned char ch)        
  23. {
  24. glCallList(base+ch); 
  25. }
  26. void RenderLevelScene() 
  27. {
  28. // glClear(GL_COLOR_BUFFER_BIT );
  29. // glLoadIdentity();
  30. glBindTexture(GL_TEXTURE_2D, texture[0]);       
  31. glColor4f(0,0.5f,0,0.75f);
  32. glTranslatef(-20,-20,-36+fMove);
  33. for(int y=0;y<40;y++)
  34. {
  35. for(int x=0;x<50;x++)
  36. {
  37. if(brmap[x][y]<0.75f)
  38. {
  39. glColor4f(0,0.5f,0,brmap[x][y]);
  40. }
  41. else
  42. {
  43. glColor4f(brmap[x][y]/2,brmap[x][y],brmap[x][y]/2,0.75f);
  44. }
  45. glSetChar(map[x][y]);
  46. glTranslatef(0,0,40);
  47. glSetChar(map[x][y]);
  48. glTranslatef(0,0,40);
  49. glSetChar(map[x][y]);
  50. glTranslatef(0,0,40);
  51. glSetChar(map[x][y]);
  52. glTranslatef(0,0,40);
  53. glSetChar(map[x][y]);
  54. glTranslatef(0,0,40);
  55. glSetChar(map[x][y]);
  56. glTranslatef(0.8,0,-200);
  57. }
  58. glTranslatef(-40,1,0);
  59. }
  60. // SwapBuffers(g_hDC);
  61. }
  62. GLvoid BuildFont(GLvoid)  
  63. {
  64. base = glGenLists(256);  
  65. float cx;                           
  66. float cy;                           
  67. for(GLuint loop=0; loop<256; loop++)           
  68. {
  69. cx=float(loop%16)/16.0f;        
  70. cy=float(loop/16)/16.0f;        
  71. glNewList(base+loop,GL_COMPILE); 
  72. glBegin(GL_TRIANGLE_STRIP);              
  73. glTexCoord2f(cx,1-cy-0.0625f);  
  74. glVertex3f(0,0,0);                
  75. glTexCoord2f(cx+0.0625f,1-cy-0.0625f);
  76. glVertex3f(1,0,0); 
  77. glTexCoord2f(cx,1-cy);  
  78. glVertex3f(0,1,0);       
  79. glTexCoord2f(cx+0.0625f,1-cy);  
  80. glVertex3f(1,1,0);              
  81. glEnd();                      
  82. glEndList();                    
  83. }
  84. }
  85. GLvoid KillFont(GLvoid)            
  86. {
  87.    glDeleteLists(base, 256);        
  88. }
  89. void UnloadLevelTextures()
  90. {
  91. glDeleteTextures (1, &texture[0]);
  92. KillFont();
  93. }
  94. void LoadLevelTextures()
  95. {
  96. texture[0] = Sliding.ID;
  97. glBindTexture(GL_TEXTURE_2D, texture[0]);
  98. BuildFont();
  99. }
  100. void LoadLevel()
  101. {
  102. srand( (unsigned)time( NULL ) );
  103. LoadLevelTextures();
  104. for(int y=0;y<40;y++)
  105. for(int x=0;x<50;x++)
  106. {
  107. int i=rand()%10;
  108. if(i<2)
  109. map[x][y]=0;
  110. else
  111. map[x][y]=rand()%256;
  112. brmap[x][y]=0.35f+float(rand()%51)/100;
  113. nmap[x][y]=(rand()%5)-2;
  114. speedmap[x][0]=1+rand()%5;
  115. speedmap[x][1]=0;
  116. }
  117. Tick1 = GetTickCount ( );
  118. Tick2 = GetTickCount ( );
  119. }
  120. void UnloadLevel()
  121. {
  122. UnloadLevelTextures();
  123. }
  124. void ProcessLevel()
  125. {
  126. Tick1 = Tick2;
  127. Tick2 = GetTickCount ( );
  128. DTick += Tick2 - Tick1;
  129. fMove-=0.005f*float(Tick2 - Tick1);
  130. // DTick += 30;
  131. // fMove-=0.005f*float(30);
  132. // rot+=0.05f*float(Tick2 - Tick1);
  133. if(fMove<-200.0f)
  134. fMove=-120.0f;
  135. if(DTick>25)
  136. {
  137. DTick-=25;
  138. for(int x=0;x<50;x++)
  139. {
  140. int m;
  141. speedmap[x][1]++;
  142. if(speedmap[x][1]>speedmap[x][0])
  143. {
  144. speedmap[x][1]=0;
  145. m=1;
  146. }
  147. else
  148. m=0;
  149. for(int y=0;y<40;y++)
  150. {
  151. if(m)
  152. {
  153. map[x][y]=map[x][y+1];
  154. brmap[x][y]=brmap[x][y+1];
  155. nmap[x][y]=nmap[x][y+1];
  156. }
  157. brmap[x][y]+=(float(nmap[x][y])*0.01f);
  158. if(brmap[x][y]>0.95f || brmap[x][y]<0.05f)
  159. nmap[x][y]=nmap[x][y]*(-1);
  160. }
  161. }
  162. for(x=0;x<50;x++)
  163. {
  164. int i=rand()%1000;
  165. if(i<300)
  166. {
  167. map[x][40]=0;
  168. if((rand()%10)<2)
  169. speedmap[x][0]=1+rand()%5;
  170. }
  171. else
  172. {
  173. if(i==300 && x<40)
  174. {
  175. int f=(rand()%5)-2;
  176. brmap[x][40]=1.0f;
  177. nmap[x][40]=f;
  178. map[x++][40]='A';
  179. brmap[x][40]=1.0f;
  180. nmap[x][40]=f;
  181. map[x++][40]='R';
  182. brmap[x][40]=1.0f;
  183. nmap[x][40]=f;
  184. map[x++][40]='R';
  185. brmap[x][40]=1.0f;
  186. nmap[x][40]=f;
  187. map[x++][40]='O';
  188. brmap[x][40]=1.0f;
  189. nmap[x][40]=f;
  190. map[x][40]='W';
  191. }
  192. else
  193. {
  194. map[x][40]=rand()%256;
  195. brmap[x][40]=0.35f+float(rand()%51)/100;
  196. nmap[x][40]=(rand()%5)-2;
  197. }
  198. }
  199. }
  200. }
  201. RenderLevelScene();
  202. }
  203. }