Advertisement
Guest User

DazKins

a guest
Apr 18th, 2012
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. package src;
  2.  
  3. import java.io.IOException;
  4. import org.lwjgl.LWJGLException;
  5. import org.lwjgl.input.Keyboard;
  6. import org.lwjgl.opengl.Display;
  7. import org.lwjgl.opengl.DisplayMode;
  8. import org.newdawn.slick.opengl.Texture;
  9. import org.newdawn.slick.opengl.TextureLoader;
  10. import org.newdawn.slick.util.ResourceLoader;
  11. import static org.lwjgl.opengl.GL11.*;
  12. import src.Image2D;
  13.  
  14.  
  15. public class TwoDtile {
  16. //GLOBAL VARS
  17. public DisplayMode display = new DisplayMode(800,600);
  18. public boolean resizeable = false;
  19. public boolean running = true;
  20. tiles[] tiles = new tiles[16];
  21. int tiley = 0;
  22. int tilex = 0;
  23. Texture woodTexture;
  24. Texture grassTexture;
  25. int[] map = {1,2,1,2,
  26. 1,2,1,2,
  27. 1,2,1,2,
  28. 1,2,1,2};
  29. int type;
  30. int tileSize = 20;
  31. int mapLength = 4;
  32. int mapHeight = 4;
  33. Texture tileGrass;
  34. Texture tileWood;
  35. Texture sheetTexture;
  36. //
  37. public static void main(String args[]){
  38. new TwoDtile();
  39. }
  40. public void init(){
  41. glMatrixMode(GL_PROJECTION);
  42. glLoadIdentity();
  43. glOrtho(0, 800, 600, 0, 1, -1);
  44. glMatrixMode(GL_MODELVIEW);
  45. loadTexture();
  46. Image2D sheet = new Image2D(sheetTexture);
  47. tileGrass = (Texture) sheet.getSubImage(17,0,16,16);
  48. tileWood = (Texture) sheet.getSubImage(0, 0, 16, 16);
  49. }
  50. public void loadTexture(){
  51. try {
  52. sheetTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/tiles.png"));
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. System.exit(-1);
  56. }
  57. }
  58. public void input(){
  59. //KEY VARS
  60. boolean keyEsc = Keyboard.isKeyDown(Keyboard.KEY_ESCAPE);
  61. //
  62.  
  63. if(keyEsc){
  64. Display.destroy();
  65. System.exit(0);
  66. }
  67. }
  68. public void tileGen(){
  69. for(int x=0;x<mapLength;x++){
  70. for(int y=0;y<mapHeight;y++){
  71. Image2D img = (Image2D) getImageForTile( map[x]);
  72. img.draw( x * tileSize, y * tileSize );
  73. }
  74. }
  75. }
  76. public Texture getImageForTile(int tile){
  77. if(tile==1)
  78. return grassTexture;
  79. if(tile==2)
  80. return woodTexture;
  81. else
  82. return null;
  83. }
  84. public TwoDtile(){
  85. try{
  86. Display.setDisplayMode(display);
  87. Display.setResizable(resizeable);
  88. Display.create();
  89. }catch(LWJGLException e){
  90. System.err.println("Could not initialise the display");
  91. }
  92. init();
  93. loadTexture();
  94. tileGen();
  95. while(running){
  96. input();
  97.  
  98.  
  99. if(Display.isCloseRequested()){
  100. running=false;
  101. }
  102. Display.sync(30);
  103. Display.update();
  104. }
  105. Display.destroy();
  106. System.exit(0);
  107. }
  108. public void tiles(int xpos,int ypos){
  109. glColor3f(0.0f,0.0f,0.0f);
  110. glBegin(GL_QUADS);
  111. glTexCoord2f(0,0);
  112. glVertex2i(xpos,ypos);
  113. glTexCoord2f(1,0);
  114. glVertex2i(xpos+tileSize,ypos);
  115. glTexCoord2f(1,1);
  116. glVertex2i(xpos+tileSize,ypos+tileSize);
  117. glTexCoord2f(0,1);
  118. glVertex2i(xpos,ypos+tileSize);
  119. glEnd();
  120. }
  121. public class tiles{
  122. public int y;
  123. public int x;
  124. public tiles(int x,int y){
  125. this.x=x;
  126. this.y=y;
  127. }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement