Jan Polak
Jan Polak

Reputation: 111

LibGDX render not called

i have a problem with LibGDX. It is blank project witch just connects to other program using Sockets. When i try to launch it, it just hangs the program is not responding. The socket connection is established and working. Thanks for help ~BeefEX

package com.beefcodes.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class MyGdxGame extends ApplicationAdapter {

    SpriteBatch batch;
    BitmapFont font;

    String lastMsg = "test";

    Socket socket;
    PrintWriter output;
    BufferedReader input;

    @Override
    public void create () {
        batch = new SpriteBatch();
        font = new BitmapFont();
        try {
            socket = new Socket("localhost", 540);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void render () {
        if (input == null && output == null) {
            try {
                output = new PrintWriter(socket.getOutputStream(), true);
                input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        String s;
        try {
            if ((s = input.readLine()) != null)
                lastMsg = s;
        } catch (IOException e) {
            e.printStackTrace();
        }
        font.draw(batch, lastMsg, 100, 100);
        batch.end();
    }
}

Upvotes: 1

Views: 155

Answers (1)

Barodapride
Barodapride

Reputation: 3723

Remove this from the render method

 if (input == null && output == null) {
        try {
            output = new PrintWriter(socket.getOutputStream(), true);
            input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

You don't want to be creating 60 PrintWriters and 60 BufferedReaders every second. Also, I don't even see where these are being used. In any case, maybe this can be done once in the create method instead.

Upvotes: 0

Related Questions