B
B
Bulat Antonov2021-10-16 01:05:56
3D
Bulat Antonov, 2021-10-16 01:05:56

How to fix out of memory error?

I'm making a 3d program:

#include <SFML/Graphics.hpp>
#include <cstdlib>
#include <cmath>
int WIDTH = 800;
int HEIGHT = 600;
float p_x_pos=0,p_y_pos=0;
int m_p_x = 0, m_p_y = 0;
int map[10][10] = {
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
    {0, 1, 0, 0, 0, 0, 1, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
    {0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
    {0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
    {0, 0, 0, 0, 1, 0, 0, 0, 0, 0}
};

sf::Color cast_ray(float x, float y, float angle, int max_dist) {
    float x_pos = x;
    float y_pos = y;
    float x_step = sin(angle);
    float y_step = cos(angle);
    for (int i = 0; i < max_dist; i++) {
        x_pos += x_step;
        y_pos += y_step;
        int x_ = x_pos / x_step;
        int y_ = y_pos / y_step;
        //printf("%f %f\n", x_pos, y_pos);
        try{
        if (map[x_][y_] == 1) {
            return sf::Color(255, 255, 255);
        }
        } catch(std::exception ex){
            
        }
    }
    return sf::Color(0, 0, 0, 0);
}
int main() {
    //window
    sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT, 32), "3D Game");

    //fps text
    sf::Text fps;
    fps.setPosition(20, 0);
    sf::Font font;
    if (!font.loadFromFile("font.ttf")) {
    }
    fps.setFont(font);
    fps.setCharacterSize(52);
    fps.setColor(sf::Color(0, 0, 0));
    short fps_num = 0;
    int c_fps = 0;
    int fps_n = 0;
    int fps0 = 0, fps1 = 0, fps2 = 0, fps3 = 0, fps4 = 0;

    //clock time
    sf::Clock clock;
    sf::Time time;

    //window is open while
    while (window.isOpen()) {

        //events
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
            if (event.type == sf::Event::MouseMoved) {
                m_p_x = sf::Mouse::getPosition().x;
                m_p_y = sf::Mouse::getPosition().y;
            }
        }

        //time=clock.restart();
        time = clock.restart();

        //clear
        window.clear(sf::Color(0, 100, 255));

        //drawing
        for (int i = 0; i < 180; i++) {
            sf::RectangleShape pix;
            pix.setPosition(i * 10, 400);
            pix.setSize(sf::Vector2f(10, 10));
            pix.setFillColor(cast_ray(p_x_pos, p_y_pos, i, 10));
            window.draw(pix);
        }
        //printf("%f %f\n",p_x_pos,p_y_pos);
        
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                sf::RectangleShape pix;
                pix.setPosition(i * 10, 500 + j * 10);
                pix.setSize(sf::Vector2f(10, 10));


                if (map[i][j] == 1) {
                    pix.setFillColor(sf::Color(255, 0, 0));
                } else {
                    pix.setFillColor(sf::Color(0, 255, 0));
                }
                window.draw(pix);
            }
        }
        //fps text creating
        c_fps = 1 / time.asSeconds();
        fps.setString("fps: " + std::to_string(fps_n));

        switch (fps_num) {
            case 0:
                fps0 = c_fps;
                break;
            case 1:
                fps1 = c_fps;
                break;
            case 2:
                fps2 = c_fps;
                break;
            case 3:
                fps3 = c_fps;
                break;
            case 4:
                fps4 = c_fps;
                fps_n = (fps0 + fps1 + fps2 + fps3 + fps4) / 5;
                fps_num = -1;

                break;
        }
        fps_num++;
        window.draw(fps);

        //display
        window.display();
    }
    return 0;
}

it works great but when i decide to change the position of the player it throws an out of memory error. and yes i know what that means

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2021-10-16
@boulat5280

So if you know, then why don't you check that your x_ and y_ lie within the array?
For example, when calling cast_ray(5, 5, 0, 10), the last point to be checked will have the coordinates x_ == 5 and y_ == 15.
PS Although stop, there is generally nonsense written there. With angle == 0, there will be division by zero (moreover, division is not needed there at all). Then, the angle in degrees is passed into the function (judging by the range 0:180, and the sin and cos functions take radians.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question