Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - MrMode

Pages: [1]
1
Graphics / Re: SFML moving view bug?
« on: January 11, 2020, 04:46:25 pm »
Thank you for quick replay!
It works now

2
Graphics / SFML moving view bug?
« on: January 11, 2020, 03:12:34 pm »
Hello,
I am now  creating level editor for my game where u can make maps/levels etc. for a game I am making.
The problem is that then I move the view HUD moves as it should, by that I mean it "stays"  locked to a screen and everything seems fine, but then I try to press a sprite it does nothing, but then I tried to press around the sprite not the sprite it self and it works. It seems like sprite picture moved correctly but box which checks that if sprite is clicked(Don't know how it works just guessing) moved less or more and now is in incorrect place. Did anyone encountered this problem before?

Thanks in advance

if (_mousePosition.x > 0 && _mousePosition.x < _wSize.x && _mousePosition.y > 0 && _mousePosition.y < _wSize.y)
   {
      if (_mousePosition.x > _data->window.getSize().x - 20)
      {
         _view.move(300.0f * dt, 0);
         MoveHudAndItsElementsRight(dt);
      }
      else if (_mousePosition.x < 20)
      {
         _view.move(-300.0f * dt, 0);
         MoveHudAndItsElementsLeft(dt);

      }
      else if (_mousePosition.y < 20)
      {
         _view.move(0, -300.0f * dt);
         MoveHudAndItsElementsUp(dt);

      }
      else if (_mousePosition.y > _data->window.getSize().y - 20)
      {                                       
         _view.move(0, 300.0f * dt);
         MoveHudAndItsElementsDown(dt);

      }
   }
   _data->window.setView(_view);
}

void MapMaker::MoveHudAndItsElementsUp(float dt)
{
   _mainBar.move(0, -300.0f * dt);
   _tilesSelectBar.setPosition(_mainBar.getPosition().x, _mainBar.getPosition().y - _tilesSelectBar.getLocalBounds().height);
   _objectsSelectBar.setPosition(_tilesSelectBar.getPosition().x + _tilesSelectBar.getLocalBounds().width, _tilesSelectBar.getPosition().y);
   _greenTile1.setPosition(_mainBar.getPosition().x + 10, _mainBar.getPosition().y + 10);
   _rockTile1.setPosition(_greenTile1.getPosition().x + _greenTile1.getLocalBounds().width, _greenTile1.getPosition().y);

}

3
General / Game developments by example - handle Input Class
« on: December 08, 2018, 07:27:11 pm »
Hello started reading  a book Game development by example, and now read a topic about the handle input class, tried to make code work and it  doesn't, Syntax is ridiculously hard, so just asking for help to understand a code and fix it , to make it work.
 
here is the code in github
https://github.com/M0d3stas/game-by-example-4.git


4
General / Re: pointer to the Player object to set up Collision
« on: December 06, 2018, 05:04:10 pm »
I CANT DELETE POST.
FIXED IT BY USING THIS

5
General / pointer to the Player object to set up Collision
« on: December 06, 2018, 04:40:02 pm »
Hello, can some help me to set up collision for the player object, i can make collision work in the game class, but cant figure out how i have to do it correctly in the player class. m_PlyersBody can be used for checking collision, but it does nothing if i set up collision on the sides of the screen, so i decided to make a pointer to the m_player, with Game::getPlayer function. i made it auto just for simplicity  to store data of m_player in the m_player in the player.cpp. But it does not let me use any functions or methods to create collision function. What i am doing wrong? i jsut want to make it collide on the sides with if statement smth like this:
if(m_player.getPostion().x >608)
{
  m_player.move(-2,0);
}

full code here:
main.cpp
#include <SFML/Graphics.hpp>
#include "Window.h"
#include "Game.h"


void main(int argc, void** argv[]) {
        // Program entry point.
        Game game; // Creating our game object.
        while (!game.GetWindow()->IsDone()) {
                // Game loop.
                game.HandleInput();
                game.Update();
                game.Render();
                game.RestartClock();
        }
}

//window.h
#pragma once
#include <string>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

class Window
{
public:
        Window();
        Window(const std::string& title, const sf::Vector2u& size);
        ~Window();

        void BeginDraw();
        void EndDraw();

        void Update();

        bool IsDone();
        bool IsFullscreen();
        sf::RenderWindow* GetRenderWindow();
        sf::Vector2u GetWindowSize();

        void ToggleFullscreen();

        void Draw(sf::Drawable& l_drawable);
private:
        void Setup(const std::string title, const sf::Vector2u& size);
        void Create();
        void Destroy();

        sf::RenderWindow m_window;
        sf::Vector2u m_windowSize;
        std::string m_windowTitle;
        bool m_isDone;
        bool m_isFullscreen;
};
 
window.cpp
#include "Window.h"

Window::Window()
{
        Setup("Window", sf::Vector2u(640, 480));
}
Window::Window(const std::string& title, const sf::Vector2u& size)
{
        Setup(title, size);
}
Window::~Window() { Destroy(); }

void Window::Setup(const std::string title, const sf::Vector2u& size)
{
        m_windowTitle = title;
        m_windowSize = size;
        m_isFullscreen = false;
        m_isDone = false;
        m_window.setFramerateLimit(60);
        Create();
}

void Window::Create() {
        auto style = (m_isFullscreen ? sf::Style::Fullscreen
                : sf::Style::Default);
        m_window.create({ m_windowSize.x, m_windowSize.y, 32 },
                m_windowTitle, style);
}

void Window::Destroy() {
        m_window.close();
}

void Window::BeginDraw() { m_window.clear(sf::Color::Black); }
void Window::EndDraw() { m_window.display(); }

bool Window::IsDone() { return m_isDone; }
bool Window::IsFullscreen() { return m_isFullscreen; }

void Window::Draw(sf::Drawable& l_drawable) {
        m_window.draw(l_drawable);
}

sf::Vector2u Window::GetWindowSize() { return m_windowSize; }

void Window::ToggleFullscreen() {
        m_isFullscreen = !m_isFullscreen;
        Destroy();
        Create();
}

void Window::Update() {
        sf::Event event;
        while (m_window.pollEvent(event)) {
                if (event.type == sf::Event::Closed) { m_isDone = true; }
                else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::F5) { ToggleFullscreen(); }
        }
}

sf::RenderWindow* Window::GetRenderWindow()
{
        return &m_window;
}
//game.h
#pragma once
#include "Window.h"
#include "Player.h"
//#include "World.h"
//#include "Textbox.h"

class Game
{
public:
        Game();
        ~Game();

        void HandleInput();
        void Update();
        void Render();

        sf::Time GetElapsed();
        void RestartClock();

        Window* GetWindow();

        Player* GetPlayer();


private:
        Window m_window;
        Player m_player;
        sf::Clock m_clock;
        float m_elapsed;

        //World m_world;
        //Textbox m_textbox;
};
//game.cpp
#include "Game.h"

Game::Game() : m_window("Snake", sf::Vector2u(800, 600)), m_snake(m_world.GetBlockSize(), &m_textbox),m_world(sf::Vector2u(800, 600))
{
        m_clock.restart();
        srand(time(nullptr));

        m_textbox.Setup(5, 14, 350, sf::Vector2f(225, 0));
        m_elapsed = 0.0f;

        m_textbox.Add("Seeded random number generator with: " + std::to_string(time(nullptr)));
}

Game::~Game() {}

sf::Time Game::GetElapsed() { return m_clock.getElapsedTime(); }
void Game::RestartClock() { m_elapsed += m_clock.restart().asSeconds(); }
Window* Game::GetWindow() { return &m_window; }

void Game::HandleInput() {
        // Input handling.
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)
                && m_snake.GetPhysicalDirection() != Direction::Down) {
                m_snake.SetDirection(Direction::Up);
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)
                && m_snake.GetPhysicalDirection() != Direction::Up) {
                m_snake.SetDirection(Direction::Down);
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)
                && m_snake.GetPhysicalDirection() != Direction::Right) {
                m_snake.SetDirection(Direction::Left);
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)
                && m_snake.GetPhysicalDirection() != Direction::Left) {
                m_snake.SetDirection(Direction::Right);
        }
}

void Game::Update() {
        m_window.Update();
        float timestep = 1.0f / m_snake.GetSpeed();
        if (m_elapsed >= timestep) {
                m_snake.Tick();
                m_world.Update(m_snake);
                m_elapsed -= timestep;
                if (m_snake.HasLost()) {
                        m_textbox.Add("GAME OVER! Score: "
                                + std::to_string((long long)m_snake.GetScore()));
                        m_snake.Reset();
                }
        }
}

void Game::Render() {
        m_window.BeginDraw();
        // Render here.
        m_world.Render(*m_window.GetRenderWindow());
        m_snake.Render(*m_window.GetRenderWindow());
        m_textbox.Render(*m_window.GetRenderWindow());

        m_window.EndDraw();
}

//player.h
#pragma once
#include "SFML/Graphics.hpp"
#include "Window.h"

enum{moveLeft, moveRight};

class Player
{
public:
        Player();

        void Position(sf::Vector2f xy);
        void playerUpdate();
        void tick();
        void move();
        void checkCollision();
        void draw(sf::RenderWindow& l_window);

        //setters and getters
        void setHasLost(bool loosing);
        bool getHasLost();

        int  getPositionX();
        void setPositionX(int x);

private:
        sf::Vector2f xyPosition;
        int playersSpeed;
        int lives;
        int score;
        bool hasLost;
        sf::RectangleShape m_PlayersBody;
       
};
 

player.cpp
#include "Game.h"


Player::Player()
{
        Player::hasLost = false;
        Player::lives = 10;
        Player::playersSpeed = 2;
        Player::Position(sf::Vector2f(320, 440));
        m_PlayersBody.setSize(sf::Vector2f(32,32));
       
}

void Player::playerUpdate()
{
        tick();
       
}

void Player::Position(sf::Vector2f xy)
{
        xyPosition = xy;
}

void Player::tick()
{
        move();
        checkCollision();
}

void Player::move()
{

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
                       
                xyPosition.x -= playersSpeed;
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
                xyPosition.x += playersSpeed;
        }
}

void Player::checkCollision()
{
        auto m_player = Game::GetPlayer;
        if (m_PlayersBody.getPosition().x >= 608)
        {
                //m_player PROBLEM IN HERE DOES NOT LET USE FUNCTIONS/METHODS
                std::cout << m_PlayersBody.getPosition().x << std::endl;
        }
        else if (m_PlayersBody.getPosition().x <= 0)
        {
                std::cout << m_PlayersBody.getPosition().x << std::endl;
        }
}
void Player::draw(sf::RenderWindow& l_window)
{
        m_PlayersBody.setFillColor(sf::Color::Blue);
        m_PlayersBody.setPosition(xyPosition);
        l_window.draw(m_PlayersBody);
}

//setters && getters
void Player::setHasLost(bool loosing)
{
        hasLost = loosing;
}
bool Player::getHasLost()
{
        return hasLost;
}

void Player::setPositionX(int x)
{
        xyPosition.x = x;
}
int Player::getPositionX()
{
        return xyPosition.x;
}

thanks if some1 will find a time to check it out

6
Graphics / Re: Mouse hoovers over the rectangle
« on: November 30, 2018, 08:23:57 pm »
Yes i use views, i got it working thanks, but i think where should be better way to do this...
if (ShopKeeper1.shopOpen == true)
{
                        std::cout << "SHOP IS OPEN" << std::endl;
                       
if (shopOne.rect.getGlobalBounds().contains(sf::Mouse::getPosition(mainWindow).x -195,sf::Mouse::getPosition(mainWindow).y + 110 ))
                        {
                                std::cout << "MOUSE ON THE SHOP" << std::endl;
                        }
                       
                }
 

added mainWindow, its my sf::RenderWindow object, but had to add and minus the postitions to get it right.
As i said i think wher eshould be better way to do this.

7
Graphics / Mouse hoovers over the rectangle
« on: November 30, 2018, 07:44:21 pm »
Hello, i need help with mouse and rectangle collision. I had draw a rectangle if shop window should be open and tryed to get proper its locations x and location y, the problem is the mouse x and y position are global, while rectangle shows completele different  numbers,
Here is example of the code
       
if (ShopKeeper1.shopOpen == true)
{
                std::cout << "SHOP IS OPEN" << std::endl;
                std::cout << shopOne.rect.getGlobalBounds().top << std::endl;
                        if(shopOne.rect.getGlobalBounds().contains(sf::Mouse::getPosition().x,sf::Mouse::getPosition().y))
                        {
                                std::cout << "MOUSE ON THE SHOP" << std::endl;
                        }
                }

i can only get rectangles starting position which is 0, its width and height, if someone could help me out here would be appriciated.

8
General / Need help with collision
« on: November 26, 2018, 03:45:47 pm »
Hello, just started coding with SFML and got stuck with collision, hwo does it needed to be done properly?
Here is my code:
main.cpp
#include "SFML/Graphics.hpp"
#include <iostream>
#include "playerClass.h"
#include "platformClass.h"

int windowWidth = 800;
int windowHeight = 600;


int main()
{
        sf::RenderWindow  window(sf::VideoMode(windowWidth, windowHeight), "SFML");
        float startingX = 10, startingY = 10;
        bool playerLeft, playerRight, playerUp, playerDown, isCollidingLeft = false, isCollidingRight = false, isCollidingUp = false , isCollidingDown = false;
        PlayerClass playerObject(startingX,startingY);
       
        sf::Font consolasFont;
        consolasFont.loadFromFile("data/fonts/consola.ttf");
        sf::Text helloText("Hello knights", consolasFont,50);

        //loading kight texture
        sf::Texture knightTexture;
        knightTexture.loadFromFile("knight.png");
        sf::Sprite knightObj(knightTexture);
        //cube
        sf::RectangleShape cube1;
        cube1.setSize(sf::Vector2f(100, 100));
        cube1.setFillColor(sf::Color(2500, 0, 0, 255));
        cube1.setPosition(200, 200);

        while (window.isOpen())
        {

                sf::Event evnt;
                while (window.pollEvent(evnt))
                {
                        if (evnt.type == sf::Event::Closed)
                        {
                                window.close();
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                        {
                                window.close();
                        }
                        //if colliding stop moving from that side
                        if (knightObj.getGlobalBounds().intersects(cube1.getGlobalBounds()))
                        {
                                if (knightObj.getPosition().x > cube1.getPosition().x && knightObj.getGlobalBounds().intersects(cube1.getGlobalBounds()))
                                {
                                        isCollidingLeft = true;
                                        isCollidingRight = false;
                                        isCollidingUp = false;
                                        isCollidingDown = false;
                                        std::cout << "Player is collding from the left" << std::endl;
                                }
                                if (knightObj.getPosition().x < cube1.getPosition().x && knightObj.getGlobalBounds().intersects(cube1.getGlobalBounds()))
                                {
                                        isCollidingLeft = false;
                                        isCollidingRight = true;
                                        isCollidingUp = false;
                                        isCollidingDown = false;
                                        std::cout << "Player is collding from the right" << std::endl;
                                }
                                if (knightObj.getPosition().y < cube1.getPosition().y && knightObj.getGlobalBounds().intersects(cube1.getGlobalBounds()))
                                {
                                        isCollidingLeft = false;
                                        isCollidingRight = false;
                                        isCollidingUp = true;
                                        isCollidingDown = false;
                                        std::cout << "Player is collding from the right" << std::endl;
                                }
                                if (knightObj.getPosition().y < cube1.getPosition().y && knightObj.getGlobalBounds().intersects(cube1.getGlobalBounds()))
                                {
                                        isCollidingLeft = false;
                                        isCollidingRight = false;
                                        isCollidingUp = false;
                                        isCollidingDown = true;
                                        std::cout << "Player is collding from the right" << std::endl;
                                }
                        }
                        else if(!knightObj.getGlobalBounds().intersects(cube1.getGlobalBounds()))
                        {
                                isCollidingLeft = false;
                                isCollidingRight = false;
                                isCollidingUp = false;
                                isCollidingDown = false;
                        }

                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
                        {
                                playerLeft = true;
                        }
                        else
                        {
                                playerLeft = false;
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
                        {
                                playerRight = true;
                        }
                        else
                        {
                                playerRight = false;
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                        {
                                playerUp = true;
                        }
                        else
                        {
                                playerUp = false;
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
                        {
                                playerDown = true;
                        }
                        else
                        {
                                playerDown = false;
                        }
                        playerObject.update(playerUp, playerDown, playerRight, playerLeft, isCollidingLeft, isCollidingRight, isCollidingUp, isCollidingDown);
                }
               
                window.clear();
                //window.draw(helloText);
                window.draw(knightObj);
                window.draw(cube1);
                knightObj.move(sf::Vector2f(playerObject.getxVel(), playerObject.getyVel()));
                window.display();
       
        }
       
        return 0;
}
playerClass.cpp
#include "playerClass.h"
#include "platformClass.h"
#include <iostream>

PlayerClass::PlayerClass(float x, float y)
{
        PlayerClass::setXPos(x);
        PlayerClass::setYPos(y);
}
void PlayerClass::update(bool playerUp, bool playerDown, bool playerRight, bool playerLeft, bool isCollidingLeft, bool isCollidingRight, bool isCollidingUp, bool isCollidingDown)
{
        if (playerDown == false && playerUp == false && playerRight == false && playerLeft == false)
        {
                PlayerClass::setyVel(0);
                PlayerClass::setxVel(0);
                std::cout << "Player stands still" << std::endl;
        }
        if (playerUp)
        {
                if (isCollidingUp == false)
                {
                        PlayerClass::setyVel(-0.5);
                        PlayerClass::setFaceUp(true);
                        PlayerClass::setFaceDown(false);
                        PlayerClass::setFaceLeft(false);
                        PlayerClass::setFaceRight(false);
                        std::cout << "Player moves up" << std::endl;
                }
                else
                {
                        std::cout << "Player cant move up" << std::endl;
                }

        }
        if (playerDown)
        {
                if (!isCollidingDown)
                {
                        PlayerClass::setyVel(0.5);
                        PlayerClass::setFaceUp(false);
                        PlayerClass::setFaceDown(true);
                        PlayerClass::setFaceLeft(false);
                        PlayerClass::setFaceRight(false);
                        std::cout << "Player moves down" << std::endl;
                }
                else
                {

                }
               
        }
        if (playerRight)
        {
                if (!isCollidingRight)
                {
                        PlayerClass::setxVel(0.5);
                        PlayerClass::setFaceUp(false);
                        PlayerClass::setFaceDown(false);
                        PlayerClass::setFaceLeft(false);
                        PlayerClass::setFaceRight(true);
                        std::cout << "Player moves right" << std::endl;
                }
                else
                {
                        std::cout << "player cant move right object in the way" << std::endl;
                }
       
        }
        if (playerLeft)
        {
                if (!isCollidingLeft)
                {
                        PlayerClass::setxVel(-0.5);
                        PlayerClass::setFaceUp(false);
                        PlayerClass::setFaceDown(false);
                        PlayerClass::setFaceLeft(true);
                        PlayerClass::setFaceRight(false);
                        std::cout << "Player moves left" << std::endl;
                }
                else
                {
                        std::cout << "player cant move left object in the way" << std::endl;
                }
        }
        PlayerClass::setYPos(PlayerClass::getyVel());
        std::cout << PlayerClass::getYPos() << std::endl;

}
playerClass.h
#include "playerClass.h"
#include "platformClass.h"
#include <iostream>

PlayerClass::PlayerClass(float x, float y)
{
        PlayerClass::setXPos(x);
        PlayerClass::setYPos(y);
}
void PlayerClass::update(bool playerUp, bool playerDown, bool playerRight, bool playerLeft, bool isCollidingLeft, bool isCollidingRight, bool isCollidingUp, bool isCollidingDown)
{
        if (playerDown == false && playerUp == false && playerRight == false && playerLeft == false)
        {
                PlayerClass::setyVel(0);
                PlayerClass::setxVel(0);
                std::cout << "Player stands still" << std::endl;
        }
        if (playerUp)
        {
                if (isCollidingUp == false)
                {
                        PlayerClass::setyVel(-0.5);
                        PlayerClass::setFaceUp(true);
                        PlayerClass::setFaceDown(false);
                        PlayerClass::setFaceLeft(false);
                        PlayerClass::setFaceRight(false);
                        std::cout << "Player moves up" << std::endl;
                }
                else
                {
                        std::cout << "Player cant move up" << std::endl;
                }

        }
        if (playerDown)
        {
                if (!isCollidingDown)
                {
                        PlayerClass::setyVel(0.5);
                        PlayerClass::setFaceUp(false);
                        PlayerClass::setFaceDown(true);
                        PlayerClass::setFaceLeft(false);
                        PlayerClass::setFaceRight(false);
                        std::cout << "Player moves down" << std::endl;
                }
                else
                {

                }
               
        }
        if (playerRight)
        {
                if (!isCollidingRight)
                {
                        PlayerClass::setxVel(0.5);
                        PlayerClass::setFaceUp(false);
                        PlayerClass::setFaceDown(false);
                        PlayerClass::setFaceLeft(false);
                        PlayerClass::setFaceRight(true);
                        std::cout << "Player moves right" << std::endl;
                }
                else
                {
                        std::cout << "player cant move right object in the way" << std::endl;
                }
       
        }
        if (playerLeft)
        {
                if (!isCollidingLeft)
                {
                        PlayerClass::setxVel(-0.5);
                        PlayerClass::setFaceUp(false);
                        PlayerClass::setFaceDown(false);
                        PlayerClass::setFaceLeft(true);
                        PlayerClass::setFaceRight(false);
                        std::cout << "Player moves left" << std::endl;
                }
                else
                {
                        std::cout << "player cant move left object in the way" << std::endl;
                }
        }
        PlayerClass::setYPos(PlayerClass::getyVel());
        std::cout << PlayerClass::getYPos() << std::endl;

}
platformClass.h
#pragma once
#include "SFML/Graphics.hpp"

class Platform
{
private:
        float xPos;
        float yPos;
        float width;
        float height;
public:
        Platform();
        //setters
               

        //getters
        float getXPos()
        {
                return xPos;
        }
};
platform.cpp
#include "platformClass.h"

Platform::Platform()
{
        xPos = 100;
        yPos = 100;
        width = 100;
        height = 100;
}

Pages: [1]