1

据我了解,它应该尝试每秒重绘 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();
            }
}
4

1 回答 1

0

尝试

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

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

于 2014-05-22T09:51:26.553 回答