import org.lwjgl.LWJGLException; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode; import static org.lwjgl.opengl.GL11.*; public class game { public final String TITLE = "game"; public static final int WIDTH = 680; public static final int HEIGHT = 480; public world world; public game() { try { Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT)); Display.setTitle(TITLE); Display.setResizable(false); Display.create(); } catch (LWJGLException e) { System.out.println(e); } init(); initGL(); gameLoop(); } private void init() { world = new world(); } private void initGL() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, WIDTH, HEIGHT, 0, 1, -1); glMatrixMode(GL_MODELVIEW); glEnable(GL_TEXTURE_2D); } private void gameLoop() { while(!Display.isCloseRequested()) { render(); Display.update(); Display.sync(60); } } private void render() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); world.render(); } public static void main(String[] args) { new game(); } }
public enum blockType { DIRT,GRASS,BRICK,AIR; }
import java.util.ArrayList; import java.util.List; public class world { private List<block> blocks = new ArrayList<block>(); public world() { init(); } public void init() { blocks.add(new block(100,100,blockType.AIR)); blocks.add(new block(10,10,blockType.AIR)); blocks.add(new block(200,200,blockType.DIRT)); } public void render() { for(block block: blocks) { block.drawBlock(); } } }
import static org.lwjgl.opengl.GL11.*; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import org.newdawn.slick.opengl.Texture; import org.newdawn.slick.opengl.TextureLoader; public class block { private int x, y; // gets the type of block being rendered in world class private blockType type; // setTo the type of block e.g AIR private String fileName; //texture got from filename private Texture texture; public block(int x, int y, blockType type) { this.x = x; this.y = y; this.type = type; checkType(); init(); } private void init() { texture = loadTexture(fileName); } private void checkType() { switch (type) { case AIR: fileName = "air2"; break; case DIRT: fileName = "dirt"; break; case BRICK: fileName = "brick"; break; case GRASS: fileName = "grass"; break; } } public void drawBlock() { texture.bind(); glBegin(GL_QUADS); { glTexCoord2f(0,0); glVertex2i(x, y); glTexCoord2f(1,0); glVertex2i(x + 20, y); glTexCoord2f(1,1); glVertex2i(x + 20, y + 20); glTexCoord2f(0,1); glVertex2i(x, y + 20); } glEnd(); } private Texture loadTexture(String img) { try { return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/" + img + ".png"))); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } }
my code draws the boxes fine without the texure, but when it comes to adding the texture it displays them with bits cut out, i think it something to do with the glTexCoord(float,float); but im not to sure, thanks
textureError.jpg