Guest User

Untitled

a guest
Nov 20th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. #include <math.h>
  2. #include "glut.h"
  3.  
  4. #include "CGameHandler.h"
  5. #include "CModel.h"
  6. #include "CCube.h"
  7. #include "CPolygon.h"
  8.  
  9. //defintion of the global variables:
  10. CCube cube;
  11. int jumpincrease;//משתנה עזר הבודק אם עברו 200 פריימים
  12. double ro, cubespeedx;//ro- cube rotation speed, cubespeed - the amount of distance the cube makes in a single frame.
  13. bool r, cs, csy, csyf;//csy/csyf making sure that the cube goes up and down, r/cs making sure the cube will/not rotate/move.
  14.  
  15.  
  16. extern void renderBitmapString(float x, float y, void *font, char *string);
  17. extern void renderVerticalBitmapString(float x, float y, int bitmapHeight, void *font, char *string);
  18.  
  19. void CGameHandler::StartOfGame()
  20. {
  21. //variables for game handling
  22. font = def_font = (int)GLUT_BITMAP_HELVETICA_18;
  23. ChangeBackgroundColors(0.5, 0.75, 0.25);
  24.  
  25. //settings of the objects:
  26. cube.MoveModel(-0.5, -3, -10);
  27. r = true;
  28. cs = true;
  29. csy = false;
  30. csyf = false;
  31. ro = 0.5;
  32. cubespeedx = 0;
  33. jumpincrease = 0;
  34. }
  35.  
  36. //on each frame:
  37. void CGameHandler::DrawScene()
  38. {
  39. cube.DrawModel();
  40. }
  41.  
  42. void CGameHandler::LogicPart()
  43. {
  44. //שליטה על סיבוב הקובייה
  45. if (r)
  46. cube.RotateSelfModel(Y_AXIS, ro);
  47. //שליטה על תזוזת הקובייה
  48. if (cs)
  49. cube.MoveModel(cubespeedx, 0, 0);
  50.  
  51. //תנאים לקוביה היוצאת מן המסך
  52. if (cube.GetModelCenter().GetCoordinate(X_AXIS) >= 6.5)
  53. cube.MoveModel(-13 , 0, 0);
  54. if (cube.GetModelCenter().GetCoordinate(X_AXIS) <= -6.5)
  55. cube.MoveModel(13, 0, 0);
  56.  
  57. //הזזת הקובייה מעלה 200 פריימים
  58. if (csy && jumpincrease < 201)
  59. {
  60. jumpincrease++;
  61. cube.MoveModel(0, 0.004, 0);
  62. if (jumpincrease > 199)
  63. {
  64. csy = false;
  65. csyf = true;
  66. }
  67. }
  68. //הוזזת הקובייה מטה 200 פריימים
  69. if (csyf && jumpincrease < 202)
  70. {
  71. jumpincrease--;
  72. cube.MoveModel(0, -0.004, 0);
  73. if (jumpincrease < 1)
  74. csyf = false;
  75. }
  76.  
  77.  
  78.  
  79. }
  80.  
  81. void CGameHandler::WriteText()
  82. {
  83. font = (int)GLUT_BITMAP_TIMES_ROMAN_24; // font type and size
  84. glColor3f(0, 255, 100); // yellow color
  85. int x_pos = glutGet(GLUT_SCREEN_WIDTH) / 2 - 100; // to be at the center of the screen
  86. int y_pos = 30;
  87.  
  88. if (r)
  89. renderBitmapString(x_pos, 80, (void *)font, "The cube is rotating");
  90. else
  91. renderBitmapString(x_pos, 80, (void *)font, "The cube is not rotating");
  92.  
  93. if (cs)
  94. renderBitmapString(x_pos, 120, (void *)font, "The cube can move");
  95. else
  96. renderBitmapString(x_pos, 120, (void *)font, "The cube can't move");
  97.  
  98. if (cubespeedx == 0)
  99. renderBitmapString(x_pos, 100, (void *)font, "The Cube is stationary");
  100. else if (cubespeedx < 0)
  101. renderBitmapString(x_pos, 100, (void *)font, "The Cube is going left");
  102. else
  103. renderBitmapString(x_pos, 100, (void *)font, "The Cube is going right");
  104. }
  105.  
  106. //keys:
  107. void CGameHandler::KeyPressed(char key)
  108. {
  109. switch (key)
  110. {
  111. case ' '://spacebar
  112. r = !r;
  113. break;
  114. case 13: //Enter
  115. cs = !cs;
  116. break;
  117. case 27://Esc
  118. break;
  119. case '1':
  120. ro = 0.2;
  121. break;
  122. case '2':
  123. ro = 0.4;
  124. break;
  125. case '3':
  126. ro = 0.6;
  127. break;
  128. case '4':
  129. ro = 0.8;
  130. break;
  131. case '5':
  132. ro = 1.0;
  133. break;
  134. case 'r':
  135. StartOfGame();
  136. break;
  137. default:
  138. break;
  139. }
  140. }
  141.  
  142. void CGameHandler::SpecialKeyPressed(char key)
  143. {
  144. switch (key)
  145. {
  146. case GLUT_KEY_LEFT:
  147. cubespeedx -= 0.001;
  148. break;
  149. case GLUT_KEY_RIGHT:
  150. cubespeedx += 0.001;
  151. break;
  152. case GLUT_KEY_UP:
  153. if(jumpincrease == 0)
  154. csy = true;
  155. break;
  156. default:
  157. break;
  158. }
  159. }
  160.  
  161. //getting backgroundColor
  162. float CGameHandler::GetBeckgroundColorR()
  163. {
  164. return BGColor_R;
  165. }
  166.  
  167. float CGameHandler::GetBeckgroundColorG()
  168. {
  169. return BGColor_G;
  170. }
  171.  
  172. float CGameHandler::GetBeckgroundColorB()
  173. {
  174. return BGColor_B;
  175. }
  176.  
  177. //internal procedures:
  178. void CGameHandler::ChangeBackgroundColors(float R, float G, float B)
  179. {
  180. BGColor_R = R;
  181. BGColor_G = G;
  182. BGColor_B = B;
  183. }
Add Comment
Please, Sign In to add comment