LWJGL窗口每秒只更新一次帧?

时间:2013-10-28 01:14:44

标签: java refresh lwjgl

根据我的理解,它应该尝试每秒重绘窗口100次。相反,它确切地每秒更新一次。这只是从今天开始,据我所知,我没有对会影响此代码的代码进行任何更改。难道它在任何循环中花费了特别长的时间,或者其中一个限制时间?

        public class Main {

            Texture texture;
            int p1y = 3, p1x = 3;
            float VEL = 2.5f; //3.25 orig
            int[][] mapArray = new int[16][16];
            boolean[][] mapBool = new boolean[16][16];
            boolean[] contBool = new boolean[12];
            String tile = "/res/tile.jpg";
            String background = "res/background.jpg";
            boolean drewStuff = false;
            int count = 0;

            private void updateLoop() {
                if(Keyboard.isKeyDown(Keyboard.KEY_W))
                    p1y -= 1;
                if(Keyboard.isKeyDown(Keyboard.KEY_S))
                    p1y += 1;
                if(Keyboard.isKeyDown(Keyboard.KEY_A))
                    p1x -= 1;
                if(Keyboard.isKeyDown(Keyboard.KEY_D))
                    p1x += 1;
                specifics.start();
                }
            Timer specifics = new Timer(20, new ActionListener() {
                        public void actionPerformed(ActionEvent evx) {
                            for(int i = 1; i > 0; i--) {
                                try {
                                    setThings();
                                } catch (IOException e1) {
                                    e1.printStackTrace();
                                }
                            }
                        }
                        });

            public void setThings() throws IOException {
                for(int i = mapArray.length - 1; i > 0; i--) {
                    for(int j = mapArray[i].length - 1; j > 0; j--) {
                        mapArray[i][j] = i * 40;
                    }
                }
                for(int i = contBool.length - 1; i > 0; i--) {
                    contBool[i] = true;
                }
            }

            private void create(){
                try{
                    Display.setDisplayMode(new DisplayMode(800, 600));
                    Display.setTitle("Electric Maze");
                    Display.create();
                    Display.setVSyncEnabled(true);
                    Display.setInitialBackground(220, 220, 220);
                    initGL();
                    while(!Display.isCloseRequested()){
                        updateLoop();
                        render();
                        Display.update();
                        Display.sync(100);  
                    }
                    Display.destroy();
                    System.exit(0);
                }catch(LWJGLException e){

                }

            }
            private void initGL(){
                glEnable(GL_TEXTURE_2D);
                glClearColor(0f, 0f, 0f, 0f);
                glEnable(GL_BLEND);
                glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
                glViewport(0, 0, 800, 600);

                glMatrixMode(GL_PROJECTION);
                glLoadIdentity();
                glOrtho(0, 800, 600, 0, -1, 1);
                glMatrixMode(GL_MODELVIEW);

                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                glLoadIdentity();
            }

            public void drawLaser(int x, int y, int size, int dir, int loc) {
                    if(mapBool[x][y] == false) {
                            for(int i = y; count < size; i++) {
                                if(mapBool[x][i] == true)size = count;
                                size = count;
                                System.out.println(count + " - " + size);
                                count++;
                        }
                        glBegin(GL_QUADS);
                            glVertex2f((mapArray[x][x + 1] + 11), mapArray[y + 1][y]);
                            glVertex2f(mapArray[x][x + 1] + 5, mapArray[y + 1][y]);
                            glVertex2f(mapArray[x][x + 1] + 5, mapArray[y + 1][y] + 40 * size);
                            glVertex2f((mapArray[x][x + 1] + 11), mapArray[y + 1][y] + 40 * size);
                        glEnd();
                    }else if(mapBool[x][y] == true)contBool[loc] = false;
            }

            public void drawTile(int x, int y, int sizeX, int sizeY, String path) {
                try {
                    texture = TextureLoader.getTexture("JPG", ResourceLoader.getResourceAsStream(path));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                glBegin(GL_QUADS);
                    texture.bind();
                    glTexCoord2f(0, 0);
                    glVertex2f(mapArray[x][x + 1], mapArray[y + 1][y]);
                    glTexCoord2f(1, 0);
                    glVertex2f(mapArray[x][x + 1] + sizeX * 40, mapArray[y + 1][y]);
                    glTexCoord2f(1, 1);
                    glVertex2f(mapArray[x][x + 1] + sizeX * 40, mapArray[y + 1][y] + sizeY * 40);   //Should use accordingly sized texture when scaling
                    glTexCoord2f(0, 1);
                    glVertex2f(mapArray[x][x + 1], mapArray[y + 1][y] + sizeY * 40);
                    texture.release();
                glEnd();
                mapBool[x][y] = true;
            }

            public void drawPlayer() {
                int x = p1x;
                int y = p1y;
                try {
                    texture = TextureLoader.getTexture("JPG", ResourceLoader.getResourceAsStream("res/player.jpg"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                glBegin(GL_QUADS);
                    texture.bind();
                    glTexCoord2f(0, 0);
                    glVertex2f(mapArray[x][x + 1], mapArray[y + 1][y]);
                    glTexCoord2f(1, 0);
                    glVertex2f(mapArray[x][x + 1] + 40, mapArray[y + 1][y]);
                    glTexCoord2f(1, 1);
                    glVertex2f(mapArray[x][x + 1] + 40, mapArray[y + 1][y] + 40);   //Should use accordingly sized texture when scaling
                    glTexCoord2f(0, 1);
                    glVertex2f(mapArray[x][x + 1], mapArray[y + 1][y] + 40);
                    texture.release();
                glEnd();
                mapBool[x][y] = true;
            }

            private void render(){
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                glColor3f(240, 240, 240);

                for(int i = contBool.length - 1; i > 0; i--) {
                    contBool[i] = true;
                }

                /*glBegin(GL_QUADS);
                    glVertex2f(x, y);
                    glVertex2f(x+width, y);
                    glVertex2f(x+width, y+height); //How it works. Just in case. Because you're stupid, Joel.
                    glVertex2f(x, y+height);         
                glEnd();*/

                drawTile(0, 1, 15, 16, background);
                drawPlayer();

                for(int i = 14; i > 0; i--) {
                    drawTile(0, i, 1, 1, tile);
                    drawTile(14, i, 1, 1, tile);
                    drawTile(i, 13, 1, 1, tile);
                    drawTile(i, 1, 1, 1, tile);
                }

                drawTile(4, 8, 1, 1, tile);
                drawTile(7, 5, 1, 1, tile);
                drawLaser(4, 2, 5, 1, 2);
                drawLaser(7, 2, 8, 1, 3);
            }

            public static void main(String[] args) {
                new Main().create();
            }
}

1 个答案:

答案 0 :(得分:0)

尝试

Display.setDisplayMode(new DisplayMode(800, 600, 32, DisplayMode.REFRESH_RATE_UNKNOWN)); instead of Display.setDisplayMode(new DisplayMode(800, 600));

一些不相关的东西: 我今年14岁,同样有2年的编程经验。您想给我发电子邮件或联系方式吗?我有一个几乎完全相同的编程学习经验。

相关问题