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.


Topics - DBD/Entropy

Pages: [1]
1
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

2
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]
anything