English 中文(简体)
青op 偶尔放慢
原标题:GameLoop Thread occasionally slow

对于目前为安乐器撰写的游戏Im,我叫“RenderView”一门课,这门课是校对和更新的,并做了一切。 “Game thread只是更新方法,没有做任何事情”,这有时是班级标志。 在我的关系上,游戏时间为30点。 在整个届会期间,我听到了两点信息。 难道有人告诉我,我如何能够优化这一类别,或者如果我不喜欢做什么,或者说它完全正常?

我的守则如下:

public class RenderView extends SurfaceView implements Runnable {

    public final String classTAG = this.getClass().getSimpleName();
    Game game;
    Bitmap framebuffer;
    Thread gameloop;
    SurfaceHolder holder;
    boolean running;

    int sleepTime;
    int numberOfFramesSkipped;
    long beginTime;
    long endTime;
    long lastTime;
    int differenceTime;
    int framePeriod;
    Canvas canvas;
    int frameCount;
    WSLog gameEngineLog;

    public RenderView(Game game, Bitmap framebuffer) {
        super(game);
        this.game = game;
        this.framebuffer = framebuffer;
        this.holder = getHolder();
        framePeriod = 1000/game.getFramesPerSecond();
        lastTime = System.currentTimeMillis();
        gameEngineLog = game.getGameEngineLog();
    }

    @Override
    public void run() {
        while(running == true) {
            if(holder.getSurface().isValid()) {

                beginTime = System.currentTimeMillis();
                numberOfFramesSkipped = 0;

                game.getCurrentScreen().update();
                game.getCurrentScreen().render(); // Draw out everything to the current virtual screen (the bitmap)
                game.getGraphics().renderFrameBuffer(); // Actually draw everything to the real screen (combine both bitmaps)

                canvas = holder.lockCanvas();
                if(canvas != null) { // Fix for mysterious bug ( FATAL EXCEPTION: Thread)
                    // The viewing area of our virtual screen on our real screen
                    canvas.drawBitmap(framebuffer, null, game.getWSScreen().getGameScreenextendeddst(), null);
                    holder.unlockCanvasAndPost(canvas);
                }
                else {
                    gameEngineLog.e(classTAG, "Surface has not been created or otherwise cannot be edited");
                }

                endTime = System.currentTimeMillis();;
                differenceTime = (int) (endTime - beginTime);
                sleepTime = (int) (framePeriod - differenceTime);

                if(sleepTime > 0) {
                    try {
                        Thread.sleep(sleepTime);
                    } catch (InterruptedException exception) {
                        exception.printStackTrace();
                    }
                }
                else {
                    while(sleepTime < 0 && numberOfFramesSkipped < game.getMaxFrameSkippes()) {
                        gameEngineLog.d(classTAG, "Game thread is only updating the update method and is not rendering anything");
                        try {
                            Thread.sleep(5);
                        } 
                        catch (InterruptedException exception) {
                            exception.printStackTrace();
                        }
                        game.getCurrentScreen().update();
                        sleepTime += framePeriod;
                        numberOfFramesSkipped++;
                    }
                }

             // Frame Per Second Count
                frameCount++;

                if(lastTime + 1000 < System.currentTimeMillis()) {
                    game.getGameEngineLog().d(classTAG, "REAL FPS: " + frameCount);
                    lastTime = System.currentTimeMillis();
                    frameCount = 0;
                }

            }
        }

    }

    public void resume() { 
        running = true;
        gameloop = new Thread(this);
        gameloop.start();         
    }   

    public void pause() { 
        running = false;                        
        while(running == true) {
            try {
                gameloop.join();
                running = false;
            } 
            catch (InterruptedException e) {
            }
        }
        gameloop = null;
    }

}

这里是图象学班的代码(斜体刚回归一个图形物体):

public class Graphics {

    public final String classTAG = this.getClass().getSimpleName();
    Game game;
    Canvas frameBuffer;
    Canvas canvasGameScreenextended;
    Canvas canvasGameScreen; // Used for customeScreen
    Bitmap gameScreenextended;
    Bitmap gameScreen;
    Rect gameScreendst;
    Rect gameScreenextendeddst;
    WSLog gameEngineLog;

    Graphics(Game game, Bitmap framebuffer, Bitmap gameScreen) {
        this.game = game;
        // Initialize canvases to render to
        frameBuffer = new Canvas(framebuffer);
        canvasGameScreen = new Canvas(gameScreen);
        // Initialize images to be rendered to our composition
        this.gameScreen = gameScreen;
        // Set up the Log
        gameEngineLog = game.getGameEngineLog();
    }

    public void resetCanvasGameScreenextended() {
        // This method has to be called each time the screen scaling type changes
        canvasGameScreenextended = new Canvas(game.getWSScreen().getGameScreenextended());
        gameScreenextended = game.getWSScreen().getGameScreenextended();
    }

    public Canvas getCanvasGameScreenextended() {
        return canvasGameScreenextended;
    }

    public Canvas getCanvasGameScreen() {
        return canvasGameScreen;
    }

    public void renderFrameBuffer() {
        // Composition
        // First layer (bottom)
        frameBuffer.drawBitmap(gameScreen, null, game.getWSScreen().getGameScreendst(), null);
        // Second layer (top)
        frameBuffer.drawBitmap(gameScreenextended, null, game.getWSScreen().getGameScreenextendeddst(), null);
    }

    public void clearFrameBuffer() {
        canvasGameScreen.drawColor(Color.BLACK);
        //canvasGameScreenextended.drawColor(Color.BLACK);
        gameScreenextended.eraseColor(Color.TRANSPARENT); // Make top layer transparent
    }

}

这里的屏幕舱(CurrentScreen(CurrentScreen)法将一个屏幕物体退回):

public class Screen {

    public final String classTAG = this.getClass().getSimpleName();
    protected final Game game;
    protected final Graphics graphics;

    protected Screen(Game game) {
        this.game = game;
        this.graphics = game.getGraphics();
        //game.getInput().reset();
    }

    public void update() {

    }

    public void render() {

    }

    /** Initialize all the sensory that should be used within this screen.*/

    public void resume() {

    }

    public void pause() {
        game.getInput().useAccelerometer(false);
        game.getInput().useKeyboard(false);
        game.getInput().useTouchscreen(false);
    }

    public void onDispose() {
        game.getGraphics().clearFrameBuffer();
    }

    public void setScreenResizeType(int screenResizeType) {

    }

筛选班延长,使(制)方法与诸如:

graphics.getCanvasGameScreen().drawRect(play, red);

难点是,当我凌驾于(......)方法之上,不把任何代码放在该方法上时,日志总是用电击: “模仿只是更新方法,而不是做任何事情”。 什么样的巫术?

非常感激帮助!

最佳回答

我从你的更新职位了解到,实际上不存在任何问题。 相反,你的密码误用了这一信息。

这是因为你检查了<代码>if(sleeptime > 0),这样,如果病情非常快而且睡觉的话。 时间是零,你获得这一信息。 Just change it to if (sleeptime >= 0).

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签