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

Author Topic: drawing Text array to screen crashes window.  (Read 574 times)

0 Members and 1 Guest are viewing this topic.

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
drawing Text array to screen crashes window.
« on: August 26, 2023, 02:39:42 am »
I'm trying to learn to use sfml's networking to make a chat program.I got this to work in console so I decided to do it with a window and Text. I was planning on having an array of Text that displays the received messages. This is called "mess".Drawing the array to screen using a for loop results in the window crashing.This however doesn't happen for the first index of mess. I tried limiting the for loop to just 0 but this still results in a crash.by the way,the typing input does draw to screen with no problem.

#include <iostream>
#include <SFML\Network.hpp>
#include "main.h"
using namespace std;
#include <SFML\Graphics.hpp>
using namespace sf;






RenderWindow window(VideoMode(960,540),"");


int main()
{
       
       
        int currentline;
        Font font;
        font.loadFromFile("C:\\Windows\\Fonts\\comic.ttf");
        Text typing;
        Text mess[4000];
        for(int x=0;x<4000;x++)
        {
                mess[x].setFont(font);
                mess[x].setCharacterSize(24);
                mess[x].setFillColor(Color::Blue);
        }
        typing.setFont(font);
       
        typing.setCharacterSize(24);
        typing.setFillColor(Color::Blue);

       
        Clock clock;
       
       
       
       
       
       
       
        IpAddress ip=IpAddress::getLocalAddress();
        string text="Connected to:";
        char buffer[2000];
        size_t received;
       
        TcpSocket socket;
        char connectiontype,mode;
        cout<<"Enter(s) for server,Enter (c) for client.\n";
        cin>>connectiontype;

        if(connectiontype==&#39;s&#39;)
        {
                TcpListener listener;
                listener.listen(5300);
                listener.accept(socket);
                text+="Server";
               
               
        }
        else if(connectiontype==&#39;c&#39;)
        {
                socket.connect(ip,5300);
                text +="Client";
                       
        }
        mess[0].setString(text);
        string input;
        while(window.isOpen())
        {
               
               
                        if(Keyboard::isKeyPressed(Keyboard::Return))
                        {
                                string typed=typing.getString();
                                socket.send(&#39;~&#39;+typed.c_str(),typed.length()+1);       
                                typing.setString("");
                                input="";
                        }
                       
                        socket.receive(buffer,sizeof(buffer),received);
                       
                if(buffer[0]==&#39;~&#39;)     
                {
                        mess[currentline].setString(string(buffer));
                        buffer[0]=&#39;&#39;;
                        currentline++;
                }
               
               
               
                Event e;
               
                while(window.pollEvent(e))
                {
                        if(e.type==Event::Closed)
                                window.close();
               
               
               
                        if(e.type==Event::TextEntered)
                        {
                                input+=e.text.unicode;
                               
                                typing.setString(input);
                        }
                }
               
               
               
               
               
                window.clear(Color::Cyan);
               
                window.draw(mess[0]);
                //window.draw(mess[1]);
                for(int x=0;x<currentline;x++)
                {
                        mess[x].setPosition(0,mess[x].getCharacterSize()*currentline);
                        window.draw(mess[x]);
                }
                typing.setPosition(0,mess[0].getCharacterSize()*(currentline+1));      
                window.draw(typing);
               
                window.display();
        }
        return 0;
       
}      

kojack

  • Sr. Member
  • ****
  • Posts: 328
  • C++/C# game dev teacher.
    • View Profile
Re: drawing Text array to screen crashes window.
« Reply #1 on: August 26, 2023, 04:18:59 am »
The problem will be this line:
int currentline;

In C++, if you don't assign a value to a variable, it's value will be whatever was already in memory, meaning it is effectively random. (In debug builds with visual studio, it will have a special value to represent it wasn't initialised).
So your for loop (the one that renders the text) might try to read a billion or more text objects instead of 4000.

Set currentline to 0 at the start.


Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Re: drawing Text array to screen crashes window.
« Reply #2 on: August 26, 2023, 05:46:04 am »
That's strange.I though for sure the default value for integers was 0.Thanks!

kojack

  • Sr. Member
  • ****
  • Posts: 328
  • C++/C# game dev teacher.
    • View Profile
Re: drawing Text array to screen crashes window.
« Reply #3 on: August 26, 2023, 06:23:36 am »
Sometimes it is, sometimes it isn't. (C and C++ love to confuse us) :)

Static and global ints default to 0. But local ints (like currentline) don't have a default (just what's in memory already).

Hapax

  • Hero Member
  • *****
  • Posts: 3360
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: drawing Text array to screen crashes window.
« Reply #4 on: August 26, 2023, 08:23:59 pm »
You can force it to zero-initialise the int using:
int currentline{};

But, if you're going to do that, you might as well provide an actual explicit value anyway:
int currentline{ 0 };
or
int currentline = 0;
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Re: drawing Text array to screen crashes window.
« Reply #5 on: August 26, 2023, 09:26:41 pm »
Yeah,I think I'll be better off if I just make a habit of defining my variables on declaration no matter what. ::)Thanks again.