Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********************************************
- ** MAIN
- **********************************************/
- #include <iostream>
- #include "Game.hpp"
- int main() {
- try {
- Game game; // Create the game instance
- game.run(); // Enter the main loop of the game
- return 0; // Exit with success status
- }
- catch (const std::exception& e) {
- std::cerr << "An exception has occurred: " << e.what() << std::endl;
- return 1; // Exit with error status
- }
- catch (...) {
- std::cerr << "An unknown exception has occurred." << std::endl;
- return 2; // Exit with error status indicating an unknown exception
- }
- }
- /*********************************************
- ** GAME.HPP
- **********************************************/
- // Game.hpp
- #ifndef GAME_HPP
- #define GAME_HPP
- #include <SFML/Graphics.hpp>
- #include "HUD.hpp"
- #include "Player.hpp"
- #include "AssetManager.hpp"
- #include "Utilities.hpp"
- class Game {
- public:
- Game();
- void run();
- private:
- sf::RenderWindow window;
- Player player;
- HUD hud;
- sf::View uiView, dyView, miniMap;
- sf::Sprite bg;
- sf::RectangleShape rectPl; // Placeholder for the player in the minimap
- sf::Clock clock;
- void initialize();
- void processEvents();
- void update(sf::Time deltaTime);
- void render();
- void setupViews(const sf::Vector2u& windowSize);
- void setupBackground();
- void setupMiniMap();
- };
- #endif // GAME_HPP
- /*********************************************
- ** GAME.CPP
- **********************************************/
- // Game.cpp
- #include "Game.hpp"
- Game::Game() : window(sf::VideoMode(640, 480), "Testing") {
- initialize();
- }
- void Game::initialize() {
- setupBackground();
- setupViews(window.getSize());
- setupMiniMap();
- // Initialize player and hud if needed here
- }
- void Game::setupViews(const sf::Vector2u& windowSize) {
- uiView.setSize(sf::Vector2f(windowSize));
- uiView.setViewport(sf::FloatRect(0.f, 0.f, 1.f, 1.f));
- dyView.setSize(sf::Vector2f(windowSize));
- dyView.setViewport(sf::FloatRect(0.f, 0.f, 1.f, 1.f));
- }
- void Game::setupBackground() {
- bg.setTexture(AssetManager::GetTexture(Utils::GetWorkingDirectory() + "Media/Backgrounds/bg3-1.png"));
- bg.setPosition(0, 0);
- bg.setOrigin(0, 0);
- }
- void Game::setupMiniMap() {
- miniMap.setViewport(sf::FloatRect(0.75F, 0.f, 0.25F, 0.25F));
- // Setup miniMap size based on the background size
- miniMap.setSize(sf::Vector2f(bg.getLocalBounds().width, bg.getLocalBounds().height));
- }
- void Game::run() {
- sf::Clock clock;
- while (window.isOpen()) {
- sf::Time deltaTime = clock.restart();
- processEvents();
- update(deltaTime);
- render();
- }
- }
- void Game::processEvents() {
- sf::Event event;
- while (window.pollEvent(event)) {
- if (event.type == sf::Event::Closed) {
- window.close();
- }
- // Add more event handling here as needed
- }
- // Additional real-time input handling could go here or inside Player::handleInput
- }
- void Game::update(sf::Time deltaTime) {
- // Update game state, player, HUD, etc.
- hud.update(deltaTime); // Assuming hud.update needs deltaTime
- player.update(deltaTime, window); // This is indicative; actual Player::update might differ
- }
- void Game::render() {
- window.clear();
- // Set view to default for drawing background and reset for UI components
- window.setView(window.getDefaultView());
- window.draw(bg);
- // Example of how to switch views
- // if (player.getPosition().x < 320) { window.setView(window.getDefaultView()); }
- // else { window.setView(dyView); }
- // Update dyView center if necessary based on player position
- player.draw(window); // Delegate drawing the player
- // Drawing minimap, UI elements, etc. could follow here
- window.setView(uiView);
- hud.draw(window); // Delegate drawing HUD
- window.display();
- }
- /*********************************************
- ** HUD.HPP
- **********************************************/
- // HUD.hpp
- #ifndef HUD_HPP
- #define HUD_HPP
- #include <SFML/Graphics.hpp>
- #include "AssetManager.hpp"
- #include "Utilities.hpp"
- class HUD {
- public:
- HUD();
- void update(const sf::Time& deltaTime);
- void draw(sf::RenderWindow& window);
- private:
- sf::Font font;
- sf::Text oneUp, score, tDelta, tElapsed, tElapsed2;
- sf::RectangleShape lifeBar;
- void setupText();
- void setupLifeBar();
- };
- #endif // HUD_HPP
- /*********************************************
- ** HUD.CPP
- **********************************************/
- // HUD.cpp
- #include "HUD.hpp"
- HUD::HUD() {
- setupText();
- setupLifeBar();
- }
- void HUD::setupText() {
- font = AssetManager::GetFont(Utils::GetWorkingDirectory() + "Media/Fonts/KOMIKAP_.ttf");
- oneUp.setFont(font);
- oneUp.setString("1 UP");
- oneUp.setCharacterSize(30U);
- oneUp.setFillColor(sf::Color::White);
- oneUp.setPosition(sf::Vector2f(50,10));
- score.setFont(font);
- score.setString("12345");
- score.setCharacterSize(20U);
- score.setFillColor(sf::Color::White);
- score.setPosition(sf::Vector2f(50,50));
- tDelta.setFont(font);
- tDelta.setCharacterSize(10U);
- tDelta.setFillColor(sf::Color::Red);
- tDelta.setPosition(sf::Vector2f(50.f, 300.f));
- tElapsed.setFont(font);
- tElapsed.setCharacterSize(10U);
- tElapsed.setFillColor(sf::Color::Red);
- tElapsed.setPosition(sf::Vector2f(50.f,325.f));
- tElapsed2.setFont(font);
- tElapsed2.setCharacterSize(10U);
- tElapsed2.setFillColor(sf::Color::Red);
- tElapsed2.setPosition(sf::Vector2f(50.f,350.f));
- }
- void HUD::setupLifeBar() {
- lifeBar.setSize(sf::Vector2f(96.f, 10.f));
- lifeBar.setFillColor(sf::Color::Green);
- lifeBar.setOrigin(lifeBar.getSize()*0.5F);
- }
- void HUD::update(const sf::Time& deltaTime) {
- // Update dynamic text (e.g., timers, scores) based on game state
- // Example: Update deltaTime text
- tDelta.setString("Delta: " + std::to_string(deltaTime.asSeconds()) + "s");
- }
- void HUD::draw(sf::RenderWindow& window) {
- // Draw all HUD elements
- window.draw(oneUp);
- window.draw(score);
- window.draw(tDelta);
- window.draw(tElapsed); // Note: You'll need to update tElapsed and tElapsed2 as needed
- window.draw(tElapsed2);
- window.draw(lifeBar); // Note: Position needs to be updated based on the game state
- }
- /*********************************************
- ** PLAYER.HPP
- **********************************************/
- // Player.hpp
- #ifndef PLAYER_HPP
- #define PLAYER_HPP
- #include <SFML/Graphics.hpp>
- #include "Animator.hpp"
- class Player {
- public:
- Player(const sf::Texture& texture);
- void handleInput();
- void update(sf::Time deltaTime);
- void draw(sf::RenderWindow& window);
- private:
- sf::Sprite sprite;
- Animator animator;
- sf::Vector2f velocity;
- void setupAnimations();
- };
- #endif // PLAYER_HPP
- /*********************************************
- ** PLAYER.CPP
- **********************************************/
- // Player.cpp
- #include "Player.hpp"
- Player::Player(const sf::Texture& texture) : sprite(texture), velocity(0.f, 0.f) {
- setupAnimations();
- }
- void Player::setupAnimations() {
- // Assuming animations have been loaded into `animator`
- // Example:
- // Animation walkAnimation;
- // walkAnimation.texture = &someTexture;
- // walkAnimation.frames.push_back(sf::IntRect(...));
- // animator.AddAnimation("Walk", walkAnimation);
- }
- void Player::handleInput() {
- // Handle player input and adjust velocity or switch animations accordingly
- // Example:
- // if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { ... }
- }
- void Player::update(sf::Time deltaTime) {
- // Update the player's position and animation
- sprite.move(velocity * deltaTime.asSeconds());
- animator.Update(deltaTime);
- animator.ApplyToSprite(sprite);
- }
- void Player::draw(sf::RenderWindow& window) {
- window.draw(sprite);
- }
- /*********************************************
- ** ANIMATOR.hPP
- **********************************************/
- // Animator.hpp
- #ifndef ANIMATOR_HPP
- #define ANIMATOR_HPP
- #include <SFML/Graphics.hpp>
- #include <map>
- #include <string>
- #include <vector>
- class Animation {
- public:
- std::vector<sf::IntRect> frames;
- const sf::Texture* texture;
- sf::Time duration;
- bool loop;
- Animation() : texture(nullptr), duration(sf::seconds(1)), loop(true) {}
- };
- class Animator {
- public:
- Animator() : currentAnimation(nullptr) {}
- void AddAnimation(const std::string& name, const Animation& animation);
- void SwitchAnimation(const std::string& name);
- void Update(sf::Time deltaTime);
- void ApplyToSprite(sf::Sprite& sprite) const;
- private:
- std::map<std::string, Animation> animations;
- const Animation* currentAnimation;
- sf::Time currentTime;
- size_t currentFrame;
- void reset();
- };
- #endif // ANIMATOR_HPP
- /*********************************************
- ** ANIMATOR.cPP
- **********************************************/
- // Animator.cpp
- #include "Animator.hpp"
- void Animator::AddAnimation(const std::string& name, const Animation& animation) {
- animations[name] = animation;
- }
- void Animator::SwitchAnimation(const std::string& name) {
- auto it = animations.find(name);
- if (it != animations.end()) {
- currentAnimation = &it->second;
- reset();
- }
- }
- void Animator::Update(sf::Time deltaTime) {
- if (!currentAnimation) return;
- currentTime += deltaTime;
- float scaledTime = currentTime.asSeconds() / currentAnimation->duration.asSeconds();
- size_t numFrames = currentAnimation->frames.size();
- currentFrame = static_cast<size_t>(scaledTime * numFrames);
- if (currentAnimation->loop)
- currentFrame %= numFrames;
- else if (currentFrame >= numFrames)
- currentFrame = numFrames - 1;
- }
- void Animator::ApplyToSprite(sf::Sprite& sprite) const {
- if (!currentAnimation) return;
- sprite.setTexture(*currentAnimation->texture);
- sprite.setTextureRect(currentAnimation->frames[currentFrame]);
- }
- void Animator::reset() {
- currentTime = sf::Time::Zero;
- currentFrame = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement