簡體   English   中英

libsdl類消耗所有JVM(阻止代碼的執行)

[英]libsdl class consumes all JVM (blocks the execution of code)

我使用圖形庫libsdl (C SDL for Java的綁定)以及多代理庫Jade。

我有一個代理創建一個用libsdl創建的GUI類,在創建之后,代理的代碼不再執行了。 實際上,唯一運行的是GUI類。

為什么?

有人知道如何解決這個問題嗎? 我希望GUI與我的程序的其他代理並行運行,但是一旦它被激活,它就需要所有JVM。

以下是我的代碼的相關部分(首先是代理):

package main;

import java.util.HashMap;
import java.util.Map;

import jade.core.Agent;
import jade.core.behaviours.TickerBehaviour;
import sdljava.SDLException;

public class Display extends Agent
{
    private static final long serialVersionUID = 1L;
    Map<String, Position> m_drones = new HashMap<String, Position>();
    GUI m_gui = null;

    protected void setup()
    {   
    for(int i = 0 ; i < Constants.numberDrones ; i++)
    {
        String name = "Drone" + Integer.toString(i);
        Position position = getFreePosition();  
        Object[] arguments = {i, position};

        try
        {
            this.getContainerController().createNewAgent(name, "main.Drone", arguments).start();
        }
        catch(Exception exception)
        {
            System.out.println("Erreur : " + exception.getMessage());
            System.exit(-1);    
        }

        m_drones.put(name, position);
    }

    try
    {
        m_gui = new GUI(m_drones);
    }
    catch (SDLException | InterruptedException e) 
    {
        e.printStackTrace();
    }

    addBehaviour(new RetrievePositions(this, Constants.retrievePositionsPeriod));
    addBehaviour(new UpdateGUI(this, Constants.updateGUIPeriod));
}

Map<String, Position> getDrones()
{
    return m_drones;
}

Position getFreePosition()
{
    int x, y;
    Position position = new Position(0, 0);

    do
    {
        x = (int) Math.floor(Math.random() * Constants.environmentWidth) * Constants.dotSize;
        y = (int) Math.floor(Math.random() * Constants.environmentHeight) * Constants.dotSize;

        position.setPosition(x, y);
    }
    while(m_drones.containsValue(position));

    return position;
    }
}

    class RetrievePositions extends TickerBehaviour 
    {   
        private static final long serialVersionUID = 1L;

        Display display = (Display) this.myAgent;
        Map<String, Position> drones = display.getDrones();

        public RetrievePositions(Agent agent, long period) 
        {
            super(agent, period);
        }

    public void onTick()
    {   
        System.out.println("retrievePositions");
    }
}

class UpdateGUI extends TickerBehaviour
{
    private static final long serialVersionUID = 1L;

    Display display = (Display) this.myAgent;

    public UpdateGUI(Agent agent, long period)
    {
        super(agent, period);   
    }

    protected void onTick() 
    {
        System.out.println("updateGUI");
    }
}

和GUI:

    package main;

import java.util.HashMap;
import java.util.Map;

import sdljava.SDLException;
import sdljava.SDLMain;
import sdljava.event.SDLEvent;
import sdljava.video.SDLRect;
import sdljava.video.SDLSurface;
import sdljava.video.SDLVideo;

public class GUI
{
    SDLSurface m_screen = null; 
    Map<String, SDLSurface> m_surfaces = new HashMap<String, SDLSurface>();
    boolean m_running = true;

    void initSurfaces(Map<String, Position> drones) throws SDLException
    {
        for(Map.Entry<String, Position> entry : drones.entrySet())
        {
            SDLSurface surface = SDLVideo.createRGBSurface(SDLVideo.SDL_HWSURFACE, Constants.dotSize, Constants.dotSize, 32, 0, 0, 0, 0);
            SDLRect rect = new SDLRect(entry.getValue().getX(), entry.getValue().getY());
            surface.fillRect(surface.mapRGB(Constants.droneRed, Constants.droneGreen, Constants.droneBlue));
            surface.blitSurface(m_screen, rect);
            m_surfaces.put(entry.getKey(), surface);
        }
    }

    public GUI(Map<String, Position> drones) throws SDLException, InterruptedException 
    {
        SDLMain.init(SDLMain.SDL_INIT_VIDEO);
        m_screen = SDLVideo.setVideoMode(Constants.environmentWidth * Constants.dotSize, 
        Constants.environmentHeight * Constants.dotSize, 32, SDLVideo.SDL_DOUBLEBUF | SDLVideo.SDL_HWSURFACE);
        SDLVideo.wmSetCaption("Flotte de drones en 2D", null);

        initSurfaces(drones);

        while(m_running) 
        {
            SDLEvent event = SDLEvent.pollEvent();

            if(event instanceof SDLEvent) 
            {
                switch (event.getType()) 
                {
                    case SDLEvent.SDL_QUIT:     
                        m_running = false;    
                    break;
                }
            }

            m_screen.flip();
        }

        freeSurfaces();
        SDLMain.quit();
    }

    public void updateGUI(Map<String, Position> drones) throws SDLException
    {
        for(Map.Entry<String, Position> entry : drones.entrySet())
        {
            SDLRect rect = new SDLRect(entry.getValue().getX(), entry.getValue().getY());
            m_surfaces.get(entry.getKey()).updateRect(rect);
            m_surfaces.get(entry.getKey()).blitSurface(m_screen, rect);
        }
    }

    void freeSurfaces() throws SDLException
    {
        for(Map.Entry<String, SDLSurface> entry : m_surfaces.entrySet())
        {
            m_surfaces.get(entry.getKey()).freeSurface();
        }

        m_screen.freeSurface();
    }
}

我想我找到了答案。 對於那些感興趣的人:Jade中的每個代理都在自己的線程中運行。 因此,為了允許Display(創建GUI的代理)繼續執行,我必須從他那里分離GUI。 我這樣做的方法是將GUI作為代理。 因此,從我通過調用其構造函數創建GUI的那一刻起,它就會啟動一個新線程並允許Display在該指令之后繼續運行。

所以答案是:使GUI成為代理。

public class GUI extends Agent
{
    ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM