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 - KokosNaPalmie

Pages: [1]
1
General / Re: How to make spawn delay to my object
« on: March 15, 2020, 01:29:39 am »
I am a newbie too, but try to use sf::Clock, with function getElapsedTime(), should work
Example code:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cstdlib>
int main()
{
    sf::RenderWindow window(sf::VideoMode(1024, 600), "Example code");

    sf::RectangleShape shape(sf::Vector2f(128, 75));     // rectangle for ex
    shape.setOrigin(sf::Vector2f(128 / 2.f, 75 / 2.f));
    shape.setPosition(512, 300);
    shape.setFillColor(sf::Color::Black);

    sf::Clock clock;
    float time_interval;

    while (window.isOpen())
    {
        time_interval = clock.getElapsedTime().asSeconds();

        sf::Event event;

        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed) // closing the window
                window.close();
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) // resetting time
        {
            shape.setFillColor(sf::Color::Black);
            clock.restart();
        }

        if (time_interval > 3) // checking if time is more than 3 seconds
        {
            shape.setFillColor(sf::Color::Blue);
        }

        system("cls");
        std::cout << time_interval << '\n'; // printing time

        window.clear(sf::Color::White);

        window.draw(shape);

        window.display();
    }
}

if you want to see more about that, see some youtube tutorials (like Suraj Sharma's SFML RPG)

3
System / Re: Mouse wheel scroll detection not working
« on: March 15, 2020, 12:59:23 am »
OK, I solved this by getting sf::Event evnt from the main program class, not using the local one

void Options::CheckEvents(sf::Event &evnt) // evnt is not from 'this' class, but from the main class that controls the whole program
{

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) // quit check
    {
        this->quit = true;
    }

    if (evnt.type == sf::Event::MouseWheelScrolled)
    {
        if (evnt.mouseWheelScroll.delta > 0) // moving up
        {
            view->move(sf::Vector2f(0, -this->view_speed * this->dt)); // dt is delta time
        }
        else if (evnt.mouseWheelScroll.delta < 0) // moving down
        {
            view->move(sf::Vector2f(0, this->view_speed * this->dt));  // dt is delta time
        }
    }
}

4
Window / Mouse wheel scroll detection still not working
« on: March 14, 2020, 08:20:58 pm »
I'm trying to make settings state for my game and my mouse scroll detection is not working. I tried to find solution on sfml website, youtube and forums, even this one, but the only answer I got was
"Flush your cout buffer if you want it to print something. << std::endl or << std::flush", and the post is now far away from interest
Here's the function I made to move a view:
void Options::CheckEvents()
{
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))   // quit check
        this->quit = true;

    while(window->pollEvent(this->evnt))
    {
        if (this->evnt.type == sf::Event::MouseWheelScrolled)
        {
            std::cout << "[DEBUG]: mouse wheel scrolled\n"; // this one prints, but not always
                                                            // (for example 4 prints per 10s of scrolling)

            if (evnt.mouseWheelScroll.delta < 0 && view->getCenter().y > WinHeight / 2) // if view center is not too far away
            {
                std::cout << "[DEBUG]: View moved up\n"; // not printing, even when I remove this ^ check
                view->move(sf::Vector2f(0, -view_speed * this->dt));
            }
            if (evnt.mouseWheelScroll.delta > 0 && view->getCenter().y < WinHeight * 2) // if view center is not too far away
            {
                std::cout << "[DEBUG]: View moved down\n"; // not printing, even when I remove this ^ check
                view->move(sf::Vector2f(0,  view_speed * this->dt));
            }
        }
    }
}

And for good news, this code works in main class

5
System / Re: Mouse wheel scroll detection not working
« on: March 13, 2020, 11:49:49 pm »
But how to scroll down the window? And for good news, the view is actually moving by 10 pixels onlywhen mouse position is on special screen position related to window

6
System / Mouse wheel scroll detection not working
« on: March 13, 2020, 04:04:06 pm »
I'm trying to make settings state for my game and my mouse scroll detection is not working. I tried to find solution on sfml website, youtube and forums but my code still doesn't want to work
Here's the function I made to move a view:
void Options::CheckEvents()
{
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))   // quit check
        this->quit = true;

    while(window->pollEvent(this->evnt))
    {
        if (this->evnt.type == sf::Event::MouseWheelScrolled)
        {
            std::cout << "[DEBUG]: mouse wheel scrolled\n"; // this one prints, but not always
                                                            // (for example 4 prints per 10s of scrolling)

            if (evnt.mouseWheelScroll.delta < 0 && view->getCenter().y > WinHeight / 2) // if view center is not too far away
            {
                std::cout << "[DEBUG]: View moved up\n"; // not printing, even when I remove this ^ check
                view->move(sf::Vector2f(0, -view_speed * this->dt));
            }
            if (evnt.mouseWheelScroll.delta > 0 && view->getCenter().y < WinHeight * 2) // if view center is not too far away
            {
                std::cout << "[DEBUG]: View moved down\n"; // not printing, even when I remove this ^ check
                view->move(sf::Vector2f(0,  view_speed * this->dt));
            }
        }
    }
}

Additionally when I move the code out of pollEvent function the view moves only when I move my mouse

7
Window / Re: Mouse Position not working
« on: March 09, 2020, 10:27:20 pm »
Could this be the reason for using the same names for the window?

Things::Things(sf::RenderWindow* window)
{
    this->window = window;
    this->arial.loadFromFile("Fonts/arial.ttf"); // loading fonts
    this->comicsans.loadFromFile("Fonts/comic.ttf");
    this->ethnocen.loadFromFile("Fonts/ethnocen.ttf");
}

8
Window / Re: Mouse Position not working
« on: March 09, 2020, 10:09:21 pm »
OK, I solved this problem by getting my mouse position from game state, not from the button's parent class.
Now my code looks like this:
   if mouse is on a button:
bool SomeText::isMouseInvaded(sf::Vector2i mouse_position)
{
    if      ( mouse_position.x >= this->text.getPosition().x - textRect.width / 2  &&
              mouse_position.x <= this->text.getPosition().x + textRect.width / 2  &&
              mouse_position.y >= this->text.getPosition().y - textRect.height / 2 &&
              mouse_position.y <= this->text.getPosition().y + textRect.height / 2 &&
              this->if_halfed )                                                                  return true; //if_halfed means if origin is set in the middle or in the top-left corner
    else if ( mouse_position.x >= this->text.getPosition().x                                   &&
              mouse_position.x <= this->text.getPosition().x + textRect.width                  &&
              mouse_position.y >= this->text.getPosition().y + textRect.top                    &&
              mouse_position.y <= this->text.getPosition().y + textRect.height + textRect.top  &&
              !this->if_halfed)                                                                  return true;
    else                                                                                         return false;
}
   and a fragment from function checking that event:
if (this->things["QUIT_BUTTON"]->isClicked_Left(mPosWindow))    // quitting the game
        this->quit = true;

9
Window / Re: Mouse Position not working
« on: March 09, 2020, 06:35:31 pm »
I used the debugger, and I got
#0 0x6e185ab0   sf::Window::getSystemHandle(this=0xbaadf00d) (D:\Programming\C++\Releases\_Sources\SFML\src\SFML\Window\Window.cpp:390)
error (segment fault)

Also deleted all pointers to window, but it still doesn't want to work

10
Window / Mouse Position not working
« on: March 08, 2020, 09:13:07 pm »
I'm working on a game and have a problem with mouse position relative to screen, window and view
void Things::UpdateMousePos() // parent class to objects like buttons, texts etc.
{
    this->mPosScreen = sf::Mouse::getPosition(); // working on fullscreen
    this->mPosWindow = sf::Mouse::getPosition(); // not working
    this->mPosView = this->window->mapPixelToCoords(sf::Mouse::getPosition(*this->window)); // crashing the game
}

I've searched quite a lot, but even the view tutorial on sfml site hasn't resolved it

Pages: [1]