Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Menu.h
- /////////////////////////////////
- #pragma once
- #include <SFML/Graphics.hpp>
- #include "Framework/Input.h"
- #include "Framework/AudioManager.h"
- #include "Framework/GameState.h"
- #include "Framework/Animation.h"
- #include "Framework/GameObject.h"
- #include <string>
- #include <iostream>
- class Menu :public GameObject
- {
- public:
- Menu(sf::RenderWindow* hwnd, Input* in, GameState* state);
- ~Menu();
- void handleInput(float dt);
- void update(float dt);
- void render();
- private:
- // Default functions for rendering to the screen.
- void beginDraw();
- void endDraw();
- // Default variables for level class.
- sf::RenderWindow* window;
- Input* input;
- GameState* gameState;
- AudioManager* audio;
- sf::Font font;
- sf::Text text;
- sf::Text text2;
- GameObject backgroundobj;
- Animation background;
- sf::Texture backgroundtexture;
- };
- Menu.cpp
- ////////////////////////////////////////////
- #include "Menu.h"
- Menu::Menu(sf::RenderWindow* hwnd, Input* in, GameState *state)
- {
- window = hwnd;
- input = in;
- gameState = state;
- // render text
- if (!font.loadFromFile("font/arial.ttf"))
- {
- std::cout << "Error loading font/n";
- }
- backgroundtexture.loadFromFile("gfx/menu.png");
- backgroundobj.setTexture(&backgroundtexture);
- backgroundobj.setPosition(0, 0);
- background.addFrame(sf::IntRect(0, 0, 500, 333));
- background.addFrame(sf::IntRect(0, 500, 500, 333));
- background.addFrame(sf::IntRect(0, 1000, 500, 333));
- background.addFrame(sf::IntRect(0, 1500, 500, 333));
- background.addFrame(sf::IntRect(0, 2000, 500, 333));
- background.addFrame(sf::IntRect(0, 2500, 500, 333));
- background.addFrame(sf::IntRect(0, 3000, 500, 333));
- background.addFrame(sf::IntRect(0, 3500, 500, 333));
- background.addFrame(sf::IntRect(0, 4000, 500, 333));
- background.addFrame(sf::IntRect(0, 4500, 500, 333));
- background.addFrame(sf::IntRect(0, 5000, 500, 333));
- background.addFrame(sf::IntRect(0, 5500, 500, 333));
- background.addFrame(sf::IntRect(0, 6000, 500, 333));
- background.addFrame(sf::IntRect(0, 6500, 500, 333));
- background.setFrameSpeed(0.1f);
- background.getCurrentFrame();
- text.setFont(font);
- text.setString("Hello World");
- text.setCharacterSize(24);
- text.setFillColor(sf::Color::Red);
- text.setPosition(500, 0);
- text2.setFont(font);
- text2.setString("Press Enter to play");
- text2.setCharacterSize(24);
- text2.setFillColor(sf::Color::Blue);
- text2.setPosition(0, 600);
- }
- Menu::~Menu()
- {
- }
- // handle user input
- void Menu::handleInput(float dt)
- {
- if (input->isKeyDown(sf::Keyboard::Enter))
- {
- gameState->setCurrentState(State::LEVEL);
- }
- }
- // Update game objects
- void Menu::update(float dt)
- {
- background.animate(dt);
- setTextureRect(background.getCurrentFrame());
- }
- // Render level
- void Menu::render()
- {
- beginDraw();
- window->draw(backgroundobj);
- window->draw(text);
- window->draw(text2);
- endDraw();
- }
- // Begins rendering to the back buffer. Background colour set to light blue.
- void Menu::beginDraw()
- {
- window->clear(sf::Color(100, 149, 237));
- }
- // Ends rendering to the back buffer, and swaps buffer to the screen.
- void Menu::endDraw()
- {
- window->display();
- }
- Animation.cpp
- //////////////////////////////////////////////////////
- #include "Animation.h"
- // Constructor. Sets initial values.
- Animation::Animation()
- {
- currentFrame = 0;
- elapsedTime = 0.f;
- isPlaying = true;
- isLooping = true;
- }
- // Adds animation frame. Rect object represent a single sprite frame
- void Animation::addFrame(sf::IntRect rect)
- {
- frames.push_back(rect);
- }
- // Returns the size of the animation. The number of frames.
- int Animation::getSize()
- {
- return (int)frames.size();
- }
- // Get current frame from the animation. Flip the returning frame if required.
- sf::IntRect Animation::getCurrentFrame()
- {
- frame = frames[currentFrame];
- if (flipped)
- {
- frame = sf::IntRect(frame.left + frame.width, frame.top, -frame.width, frame.height);
- }
- return frame;
- }
- // Check if the animation should progress, if so move to next frame, or loop back to the start
- void Animation::animate(float dt)
- {
- if (isPlaying)
- {
- elapsedTime += dt;
- if (elapsedTime >= animationSpeed)
- {
- currentFrame++;
- if (currentFrame >= (int)frames.size())
- {
- if (isLooping)
- {
- currentFrame = 0;
- }
- else
- {
- currentFrame--;
- }
- }
- elapsedTime = 0;
- }
- }
- }
- // Reset animation.
- void Animation::reset()
- {
- currentFrame = 0;
- elapsedTime = 0;
- }
- // Set animation speed, in Frames per Second.
- void Animation::setFrameSpeed(float speed)
- {
- animationSpeed = speed;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement