Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. Menu.h
  2. /////////////////////////////////
  3.  
  4. #pragma once
  5. #include <SFML/Graphics.hpp>
  6. #include "Framework/Input.h"
  7. #include "Framework/AudioManager.h"
  8. #include "Framework/GameState.h"
  9. #include "Framework/Animation.h"
  10. #include "Framework/GameObject.h"
  11. #include <string>
  12. #include <iostream>
  13. class Menu :public GameObject
  14. {
  15. public:
  16. Menu(sf::RenderWindow* hwnd, Input* in, GameState* state);
  17. ~Menu();
  18.  
  19. void handleInput(float dt);
  20. void update(float dt);
  21. void render();
  22.  
  23. private:
  24. // Default functions for rendering to the screen.
  25. void beginDraw();
  26. void endDraw();
  27.  
  28. // Default variables for level class.
  29. sf::RenderWindow* window;
  30. Input* input;
  31. GameState* gameState;
  32. AudioManager* audio;
  33. sf::Font font;
  34. sf::Text text;
  35. sf::Text text2;
  36. GameObject backgroundobj;
  37.  
  38. Animation background;
  39. sf::Texture backgroundtexture;
  40.  
  41.  
  42. };
  43.  
  44.  
  45. Menu.cpp
  46. ////////////////////////////////////////////
  47. #include "Menu.h"
  48.  
  49.  
  50.  
  51. Menu::Menu(sf::RenderWindow* hwnd, Input* in, GameState *state)
  52. {
  53.  
  54. window = hwnd;
  55. input = in;
  56. gameState = state;
  57.  
  58. // render text
  59. if (!font.loadFromFile("font/arial.ttf"))
  60. {
  61. std::cout << "Error loading font/n";
  62. }
  63. backgroundtexture.loadFromFile("gfx/menu.png");
  64.  
  65. backgroundobj.setTexture(&backgroundtexture);
  66. backgroundobj.setPosition(0, 0);
  67.  
  68. background.addFrame(sf::IntRect(0, 0, 500, 333));
  69. background.addFrame(sf::IntRect(0, 500, 500, 333));
  70. background.addFrame(sf::IntRect(0, 1000, 500, 333));
  71. background.addFrame(sf::IntRect(0, 1500, 500, 333));
  72. background.addFrame(sf::IntRect(0, 2000, 500, 333));
  73. background.addFrame(sf::IntRect(0, 2500, 500, 333));
  74. background.addFrame(sf::IntRect(0, 3000, 500, 333));
  75. background.addFrame(sf::IntRect(0, 3500, 500, 333));
  76. background.addFrame(sf::IntRect(0, 4000, 500, 333));
  77. background.addFrame(sf::IntRect(0, 4500, 500, 333));
  78. background.addFrame(sf::IntRect(0, 5000, 500, 333));
  79. background.addFrame(sf::IntRect(0, 5500, 500, 333));
  80. background.addFrame(sf::IntRect(0, 6000, 500, 333));
  81. background.addFrame(sf::IntRect(0, 6500, 500, 333));
  82. background.setFrameSpeed(0.1f);
  83.  
  84. background.getCurrentFrame();
  85.  
  86. text.setFont(font);
  87. text.setString("Hello World");
  88. text.setCharacterSize(24);
  89. text.setFillColor(sf::Color::Red);
  90. text.setPosition(500, 0);
  91.  
  92. text2.setFont(font);
  93. text2.setString("Press Enter to play");
  94. text2.setCharacterSize(24);
  95. text2.setFillColor(sf::Color::Blue);
  96. text2.setPosition(0, 600);
  97. }
  98.  
  99.  
  100. Menu::~Menu()
  101. {
  102.  
  103. }
  104.  
  105.  
  106. // handle user input
  107. void Menu::handleInput(float dt)
  108. {
  109. if (input->isKeyDown(sf::Keyboard::Enter))
  110. {
  111. gameState->setCurrentState(State::LEVEL);
  112. }
  113. }
  114.  
  115. // Update game objects
  116. void Menu::update(float dt)
  117. {
  118. background.animate(dt);
  119. setTextureRect(background.getCurrentFrame());
  120. }
  121.  
  122. // Render level
  123. void Menu::render()
  124. {
  125. beginDraw();
  126. window->draw(backgroundobj);
  127. window->draw(text);
  128. window->draw(text2);
  129. endDraw();
  130. }
  131.  
  132. // Begins rendering to the back buffer. Background colour set to light blue.
  133. void Menu::beginDraw()
  134. {
  135. window->clear(sf::Color(100, 149, 237));
  136. }
  137.  
  138. // Ends rendering to the back buffer, and swaps buffer to the screen.
  139. void Menu::endDraw()
  140. {
  141. window->display();
  142. }
  143.  
  144.  
  145.  
  146. Animation.cpp
  147. //////////////////////////////////////////////////////
  148. #include "Animation.h"
  149.  
  150. // Constructor. Sets initial values.
  151. Animation::Animation()
  152. {
  153. currentFrame = 0;
  154. elapsedTime = 0.f;
  155. isPlaying = true;
  156. isLooping = true;
  157. }
  158.  
  159. // Adds animation frame. Rect object represent a single sprite frame
  160. void Animation::addFrame(sf::IntRect rect)
  161. {
  162. frames.push_back(rect);
  163. }
  164.  
  165. // Returns the size of the animation. The number of frames.
  166. int Animation::getSize()
  167. {
  168. return (int)frames.size();
  169. }
  170.  
  171. // Get current frame from the animation. Flip the returning frame if required.
  172. sf::IntRect Animation::getCurrentFrame()
  173. {
  174. frame = frames[currentFrame];
  175. if (flipped)
  176. {
  177.  
  178. frame = sf::IntRect(frame.left + frame.width, frame.top, -frame.width, frame.height);
  179. }
  180. return frame;
  181. }
  182.  
  183. // Check if the animation should progress, if so move to next frame, or loop back to the start
  184. void Animation::animate(float dt)
  185. {
  186. if (isPlaying)
  187. {
  188. elapsedTime += dt;
  189. if (elapsedTime >= animationSpeed)
  190. {
  191. currentFrame++;
  192. if (currentFrame >= (int)frames.size())
  193. {
  194. if (isLooping)
  195. {
  196. currentFrame = 0;
  197. }
  198. else
  199. {
  200. currentFrame--;
  201. }
  202. }
  203. elapsedTime = 0;
  204. }
  205. }
  206. }
  207.  
  208. // Reset animation.
  209. void Animation::reset()
  210. {
  211. currentFrame = 0;
  212. elapsedTime = 0;
  213. }
  214.  
  215. // Set animation speed, in Frames per Second.
  216. void Animation::setFrameSpeed(float speed)
  217. {
  218. animationSpeed = speed;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement