public void start() { // set up the console handler console.setHandler(new ConsoleHandler()); // load the map map = MapLoader.loadMap("map_basic.txt"); // grab some entities StaticEntity chip = new StaticEntity(); chip.setPosition(new Coord(9, 5)); chip.addTexture(GameContext.getTextureLoader().getTexture("gtest/resources/chipA.png")); chip.addTexture(GameContext.getTextureLoader().getTexture("gtest/resources/chipB.png")); entities.add(chip); // create the player player = new Player(new Coord(0, 12)); player.setMapContext(map); entities.add(player); // set up camera GameContext.getCamera().setFollowing(player); // set up openGL GL11.glEnable(GL11.GL_TEXTURE_2D); GL11.glDisable(GL11.GL_DEPTH_TEST); GL11.glDisable(GL11.GL_LIGHTING); GL11.glEnable(GL11.GL_BLEND); GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); // game loop while (!Display.isCloseRequested()) { Date d = new Date(); long startTime = d.getTime(); draw(); update(); handleInput(); d = new Date(); long endTime = d.getTime(); try { Thread.sleep(50 - (endTime - startTime)); } catch (Exception e) { } if ((endTime - startTime) != 0) { GameContext.addToLog("fps", "" + 1000 / (endTime - startTime)); } } Display.destroy(); }
@Override public void handle(String message) { String[] tokens = message.split(" "); if (tokens[0].equals("load") && tokens.length == 2) { map = MapLoader.loadMap(tokens[1]); } else if (tokens[0].equals("scale") && tokens.length == 2) { scale = Integer.parseInt(tokens[1].trim()); } else if (tokens[0].equals("debug") && tokens.length == 2) { GameContext.setDebugMode(tokens[1].equals("on")); } else if (tokens[0].equals("reset") && tokens.length == 1) { GameContext.getCamera().setPosition(new Coord(0, 0)); GameContext.getCamera().setFollowing(player); editorMode = false; scale = 90; entities.remove(editorTag); editorTag = null; } else if (tokens[0].equals("editor") && tokens.length == 1) { GameContext.getCamera().getPosition().snap(); editorTag = new EditorTagEntity(GameContext.getCamera().getPosition()); entities.add(editorTag); GameContext.getCamera().setFollowing(editorTag); editorMode = true; } else if (tokens[0].equals("save") && tokens.length == 2) { MapLoader.saveMap(map, tokens[1].trim()); } else if (tokens[0].equals("move") && tokens.length == 2) { player.move(Integer.parseInt(tokens[1])); } }
private void draw() { // Clear the screen and depth buffer GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glLoadIdentity(); // set up GameContext.getCamera() GL11.glPushMatrix(); GL11.glScalef(scale, scale, scale); GL11.glTranslatef( -GameContext.getCamera().getPosition().getx(), -GameContext.getCamera().getPosition().gety(), 0); GL11.glTranslatef(4f, 3f, 0); map.draw(); if (editorMode) { Quad q = new Quad(editorTag.getPosition(), map.getLookupTile(currentEditorTile).getTexture()); GameContext.getPipe().addDrawable(q); } for (Entity ae : entities) { ae.draw(); } GameContext.getPipe().renderContent(); GL11.glPopMatrix(); // overlay console text GL11.glPushMatrix(); GL11.glScalef(2f, 2f, 2f); if (console.isEnabled()) { gtest.drawing.util.drawString(new Coord(0, 0), "> " + console.getText()); } if (GameContext.isDebugMode()) { gtest.drawing.util.drawString( new Coord(0, 285), "tiles drawn " + GameContext.getFromLog("tilesLastDrawn")); gtest.drawing.util.drawString( new Coord(0, 275), "textures bound " + GameContext.getFromLog("textureBinds")); gtest.drawing.util.drawString(new Coord(0, 265), "FPS " + GameContext.getFromLog("fps")); } GL11.glPopMatrix(); Display.update(); // clean up GameContext.getPipe().clear(); GameContext.addToLog("tilesLastDrawn", "0"); GameContext.addToLog("textureBinds", "0"); }