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 - DBD/Entropy

Pages: [1]
1
DotNet / Re: Mouse Scroll Direction
« on: February 07, 2024, 10:52:32 pm »
Ill look into the multiple wheels thing.

The way events in C#-SFML work is the following:
The Window has a property public event "Eventname" for example MouseWheelScrolled, and you then add your event function onto this property like this:
_window.MouseWheelScrolled += EventHandler.OnMouseScroll:
assuming the guys at SFML did their job this should prevent bad inputs.

*A few minutes after typing*
I have put this in front of the switch statement, but this doesnt resolve the issue (as i personally expected) but at least people cant just scroll with their horizontal axis;
if (mouseEvent.Wheel == Mouse.Wheel.HorizontalWheel) return;

2
DotNet / Re: Mouse Scroll Direction
« on: February 07, 2024, 07:23:38 pm »
Does this mean you have two wheels or maybe a multi-directional ball/wheel?

No, many mouses (that i know) support pressing your mouse wheel to the side to scroll sideways

In addition, are you certain that the only time this function is called is when the event type is correct?
The codebase is a very small private project, with 6 C# files (and technically one c++ function) with maybe 600 lines combined at max. Assuming you mean Project.ZoomView(), yes that is only called from that event, since it is a binding for the EventHandler Class (Which contains the event handling functions). This is what it looks like (_view and _window are my view and window, and _zoom is a float that keeps track of the zooming i have done to make resetting the zoom possible)
    public static void ZoomView(float delta)
    {
        _zoom += delta;
        _view.Zoom(_zoom);
        _window.SetView(_view);
    }

Note that, per wheel, it can only go in one direction: positive or negative.
It also doesn't look like you're checking which wheel is being scrolled.

1. I have only one mouse wheel, so I don't need to check which wheel is being scrolled (for now).
2. I am aware that my delta is a one-dimensional value, and you can probably tell from the info i have given until now, that this should work perfectly fine.

3
DotNet / Re: Mouse Scroll Direction
« on: February 07, 2024, 11:33:21 am »
What i can say, is that the scroll direction does not directly translate into the zoom direction.
Basically if you scroll up and down and left and right a bit, you can get it to change the zoom direction, but any scrolling between these changes are going into the same zoom direction.
To the outsider its more or less random.

4
DotNet / Mouse Scroll Direction
« on: February 07, 2024, 02:01:01 am »
I am currently working on my camera control and am now tripping over the zooming. Ideally i want to be able to zoom in with a upward mouse scroll and down with a downward mouse scroll. After going through the docs i find this https://www.sfml-dev.org/documentation/2.6.1/structsf_1_1Event_1_1MouseWheelScrollEvent.php. And based on that information i have created this absolute "masterpiece":

public static void OnMouseScroll(object? sender, EventArgs e)
{
    if (sender == null) return;
    var window = (RenderWindow)sender;
    var mouseEvent = (MouseWheelScrollEventArgs)e;
    switch (mouseEvent.Delta)
    {
         case > 0:
             Program.ZoomView(.01f);
             break;
         case < 0:
             Program.ZoomView(-.01f);
             break;
    }
}
Note: Program.ZoomView() does what it says: it zooms my view.

The zooming itself works just fine, with one exception:
It doesnt matter where i scroll, the way it scroll is kind of random (meaning i haven't found a pattern)

Any ideas what might cause this or how to fix it are appreciated.
Second Note: I am using SFML 2.5.1 as that is the newest C# binding that NuGet provides

5
Graphics / Re: Stack Overflow in sfml-graphics-2.dll
« on: November 17, 2023, 09:21:59 pm »
Update, after a lot of searching i found that the recursion is indeed caused by my stupid ass. The error lies with the Cell::Draw(...) function calling target.draw(*this)

6
Graphics / Stack Overflow in sfml-graphics-2.dll
« on: November 17, 2023, 04:22:31 pm »
I am currently working on a Conways game of life clone. Now while the project starts correctly, meaning i get a window that opens, the window closes after around a second. My Visual Studio debugger then informs me of a stack overflow, since i want to know what happens and where it happens, look at my main threads callstack, which tells me that it consists (as far as it is showing me) only of  "sfml-graphics-2.dll!00007ffc917be63e()" The thing is i am probably intermediate in c++ and a absolute noob with sfml, but i have a feeling that there is some infinite recursion in the dll. If the reason of the error is with me, please inform me of a fix.
I have attached the 3 currently existing c++ files. If the headers are needed as well or any questions to my code arise, please ask.
Thanks for any help in advance.

Code:

//Cell.cpp
#include "Cell.hpp"


void Cell::draw(sf::RenderTarget& target, sf::RenderStates states) const {
        if (this != nullptr)
                target.draw(*this);
}

Cell::Cell() : Cell(sf::Vector2f(1, 1), 1) {
}

Cell::Cell(sf::Vector2f position, float size) {
        this->setFillColor(sf::Color::Black);
        this->setOutlineColor(sf::Color(100, 100, 100));
        this->setPosition(position);
        this->setSize(sf::Vector2f(size, size));
        this->isAliveNext = false;
        this->isAlive = false;
        this->aliveNeighborCount = 0;
        Cell* emptyCells[8];
        this->setNeighborCells(emptyCells);
}

void Cell::setNeighborCells(Cell* cells[8]) {
        for (int i = 0; i < 8; i++) {
                neighborCells[i] = cells[i];
        }
}

void Cell::prepareTick() {
        if (this->isAlive) {
                if (this->aliveNeighborCount != 3 && this->aliveNeighborCount != 2) this->isAliveNext = false;
                else this->isAliveNext = true;
        }
        else {
                if (this->aliveNeighborCount == 3) this->isAliveNext = true;
                else this->isAliveNext = false;
        }
}

void Cell::advanceTick() {
        if (this->isAlive != this->isAliveNext)
        {
                if (this->isAlive) {
                        for (Cell* cell : neighborCells)
                                if (cell != nullptr)
                                        cell++;
                }
                else {
                        for (Cell* cell : neighborCells)
                                if (cell != nullptr)
                                        cell--;
                }
        }
        this->isAlive = this->isAliveNext;
        this->setFillColor((this->isAlive) ? sf::Color::White : sf::Color::Black);
}

Cell Cell::operator++ () {
        this->aliveNeighborCount++;
        return *this;
}

Cell Cell::operator-- () {
        this->aliveNeighborCount--;
        return *this;
}

void Cell::drawCell(sf::RenderTarget& target, sf::RenderStates states) {
        this->draw(target, states);
}
 
//Field.cpp
#include "Field.hpp"

void Field::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
        for (std::vector<Cell*> cellRow : this->cellGrid) {
                for (Cell* cell : cellRow) {
                        cell->drawCell(target, states);
                }
        }
}

Field::Field(sf::Vector2i fieldSize, float commonCellSize)
{
        this->cellGrid.resize(fieldSize.x);
        for (int x = 0; x < this->cellGrid.size(); x++) {
                this->cellGrid[x].resize(fieldSize.y);
                for (int y = 0; y < this->cellGrid[x].size(); y++) {
                        this->cellGrid[x][y] = new Cell(sf::Vector2f(x * commonCellSize, y * commonCellSize), commonCellSize);
                }
        }
        for (int x = 0; x < this->cellGrid.size(); x++) {
                for (int y = 0; y < this->cellGrid[x].size(); y++) {
                        Cell* neighbors[8] = {
                                (x > 0 && y > 0) ? this->cellGrid[x - 1][y - 1] : nullptr,
                                (y > 0) ? this->cellGrid[x][y - 1] : nullptr,
                                (x < this->cellGrid.size() - 1 && y > 0) ? this->cellGrid[x + 1][y - 1] : nullptr,
                                (x > 0) ? this->cellGrid[x - 1][y] : nullptr,
                                (x < this->cellGrid.size() - 1) ? this->cellGrid[x + 1][y] : nullptr,
                                (x > 0 && y < this->cellGrid[x].size() - 1) ? this->cellGrid[x - 1][y + 1] : nullptr,
                                (y < this->cellGrid[x].size() - 1) ? this->cellGrid[x][y + 1] : nullptr,
                                (x < this->cellGrid.size() - 1 && y < this->cellGrid[x].size() - 1) ? this->cellGrid[x + 1][y + 1] : nullptr
                        };
                        this->cellGrid[x][y]->setNeighborCells(neighbors);
                }
        }
}

void Field::tick()
{
        for (std::vector<Cell*>& cellRow : this->cellGrid) {
                for (Cell*& cell : cellRow) {
                        cell->prepareTick();
                }
        }
        for (std::vector<Cell*>& cellRow : this->cellGrid) {
                for (Cell*& cell : cellRow) {
                        cell->advanceTick();
                }
        }
}

void Field::drawField(sf::RenderTarget& target, sf::RenderStates states)
{
        target.draw(*this);
}
//main.cpp
#pragma once

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include "Field.hpp"

int main(int, char const**)
{
        // Create the main window
        sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "SFML window", sf::Style::Fullscreen);
        sf::Vector2i fieldSize(1000, 1000);
        float commonCellSize{ 100 };
        Field gameField(fieldSize, commonCellSize);
        sf::View gameView(sf::Vector2f(fieldSize.x * commonCellSize, fieldSize.y * commonCellSize),
                sf::Vector2f(fieldSize.x * commonCellSize / 2, fieldSize.y * commonCellSize / 2));

        gameView.setViewport(sf::FloatRect(.9, .9, .85, .85));
        window.setView(gameView);

        float viewSpeed = 10.0f;

        // Start the game loop
        while (window.isOpen())
        {
                // Process events
                sf::Event event;
                while (window.pollEvent(event))
                {
                        // Close window: exit
                        if (event.type == sf::Event::Closed) {
                                window.close();
                        }

                        // Escape pressed: exit
                        if (event.type == sf::Event::KeyPressed) {
                                switch (event.key.code)
                                {
                                case sf::Keyboard::Escape:
                                        window.close();
                                        break;
                                case sf::Keyboard::Up:
                                        gameView.move(sf::Vector2f(0, -commonCellSize));
                                        break;
                                case sf::Keyboard::Down:
                                        gameView.move(sf::Vector2f(0, commonCellSize));
                                        break;
                                case sf::Keyboard::Left:
                                        gameView.move(sf::Vector2f(-commonCellSize, 0));
                                        break;
                                case sf::Keyboard::Right:
                                        gameView.move(sf::Vector2f(commonCellSize, 0));
                                        break;
                                default:
                                        break;
                                }
                        }

                        if (event.type == sf::Event::MouseWheelScrolled) {
                                if (event.mouseWheelScroll.delta > 0) {
                                        gameView.zoom(1.1);
                                }
                                else {
                                        gameView.zoom(0.9);
                                }
                        }

                        sf::Vector2i mousePosition = sf::Mouse::getPosition();
                        // Check Wether mouse is close to border in a direction then move view in that direction
                        sf::Vector2u windowSize = window.getSize();
                        sf::Vector2f edgeThreshold(windowSize.x * 0.3, windowSize.y * 0.3);

                        // Check if the mouse is near the left edge
                        if (mousePosition.x < edgeThreshold.x) {
                                gameView.move(-viewSpeed, 0);
                        }
                        // Check if the mouse is near the right edge
                        else if (mousePosition.x > windowSize.x - edgeThreshold.x) {
                                gameView.move(viewSpeed, 0);
                        }

                        // Check if the mouse is near the top edge
                        if (mousePosition.y < edgeThreshold.y) {
                                gameView.move(0, -viewSpeed);
                        }
                        // Check if the mouse is near the bottom edge
                        else if (mousePosition.y > windowSize.y - edgeThreshold.y) {
                                gameView.move(0, viewSpeed);
                        }

                        /* Implment PreGame
                        if (event.type == sf::Event::MouseButtonPressed) {
                                if (event.mouseButton.button == sf::Mouse::Left) {
                                        sf::Vector2i mousePos = sf::Mouse::getPosition(window);
                                        sf::Vector2f worldPos = window.mapPixelToCoords(mousePos);
                                        gameField.setCellAlive(worldPos);
                                }
                        }
                        */

                }

                // Clear screen
                window.clear();

                gameField.drawField(window, sf::RenderStates::Default);

                // Update the window
                window.display();
        }

        return EXIT_SUCCESS;
}
 

Pages: [1]