Show Posts
|
|
Pages: [1]
|
|
2
|
Java Game APIs & Engines / OpenGL Development / Re: get stuck all time
|
on: 2012-02-07 17:08:59
|
fine , here's the classes: BLOCK class: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| package mineCraft2d;
import static mineCraft2d.World.BLOCK_SIZE; 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 BlockType type = BlockType.AIR; private float x; private float y; private Texture texture = null; public Block(BlockType type, float x, float y) { super(); this.type = type; this.x = x; this.y = y; try { this.texture = TextureLoader.getTexture("PNG",new FileInputStream(new File(type.location))); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
public void draw() { texture.bind(); glLoadIdentity(); glTranslatef(x,y,0); glBegin(GL_QUADS); glTexCoord2f(0,0); glVertex2f(0,0); glTexCoord2f(1,0); glVertex2f(BLOCK_SIZE,0); glTexCoord2f(1,1); glVertex2f(BLOCK_SIZE,BLOCK_SIZE); glTexCoord2f(0,1); glVertex2f(0,BLOCK_SIZE); glEnd(); glLoadIdentity(); } public BlockType getType() { return type; }
public void setType(BlockType type) { this.type = type; }
public float getX() { return x; }
public void setX(float x) { this.x = x; }
public float getY() { return y; }
public void setY(float y) { this.y = y; } } |
BlockGrid class: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package mineCraft2d;
import static mineCraft2d.World.*;
public class BlockGrid { private Block[][] blocks = new Block[BLOCKS_WIDTH][BLOCKS_HEIGHT]; public BlockGrid() { for(int i=0 ; i<BLOCKS_WIDTH - 1 ; i++) { for(int j=0 ; j<BLOCKS_HEIGHT - 1 ; j++) { blocks[i][j] = new Block(BlockType.AIR,i * BLOCK_SIZE,j * BLOCK_SIZE); } } } public void setAt(int x , int y , BlockType b) { blocks[x][y] = new Block(b,x * BLOCK_SIZE,y * BLOCK_SIZE); } public Block getAt(int x , int y) { return blocks[x][y]; } public void draw() { for(int i=0 ; i<BLOCKS_WIDTH - 1 ; i++) { for(int j=0 ; j<BLOCKS_HEIGHT - 1 ; j++) { blocks[i][j].draw(); } } } } |
blockType class: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package mineCraft2d;
public enum BlockType {
STONE("res/stone.png") , AIR("res/air.png") , GRASS("res/grass.png") , DIRT("res/dirt.png"); public final String location; BlockType(String location) { this.location = location; } } |
Boot class: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| package mineCraft2d;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.input.Mouse; import org.lwjgl.opengl.*; import org.lwjgl.*;
public class Boot { BlockGrid grid; public Boot() { try { Display.setDisplayMode(new DisplayMode(640,480)); Display.setTitle("Minecraft 2D"); Display.create(); }catch(LWJGLException e) { e.printStackTrace(); } grid = new BlockGrid(); grid.setAt(10,10,BlockType.STONE); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0,640,480,0,1,-1); glMatrixMode(GL_MODELVIEW); glEnable(GL_TEXTURE_2D); while(!Display.isCloseRequested()) { glClear(GL_COLOR_BUFFER_BIT); input(); grid.draw(); Display.update(); Display.sync(60); } Display.destroy(); } private void input() { int mousex = Mouse.getX(); int mousey = 480 - Mouse.getY() - 1; boolean mouseClicked = Mouse.isButtonDown(0); if(mouseClicked) { int grid_x = Math.round(mousex / World.BLOCKS_WIDTH); int grid_y = Math.round(mousex / World.BLOCKS_HEIGHT); grid.setAt(grid_x, grid_y, BlockType.STONE); } } public static void main(String[] args) { new Boot(); } } |
World class: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package mineCraft2d;
public class World {
public static final int BLOCK_SIZE = 32; public static final int BLOCKS_WIDTH = 24 , BLOCKS_HEIGHT = 20; } |
that's it. 
|
|
|
|
|
3
|
Java Game APIs & Engines / OpenGL Development / get stuck all time
|
on: 2012-02-06 13:07:41
|
|
Hello, i use LWJGL(opengl) and i learn from this tutorial:http://www.youtube.com/watch?v=7G_xtkvyleM from the beginning everything worked just fine until now:( i dont know why every time i play this mini game(mine craft 2d) , the window is black and everything get stuck. why is that happening ? is that a problem with my processor(intel coreTM 2) ? or maybe its the display adapters(NVIDIA GeForce 9500 GT)? help please ! ty. and if you wanna see my code just ask.
|
|
|
|
|
4
|
Java Game APIs & Engines / OpenGL Development / get stuck all time
|
on: 2012-02-06 13:06:57
|
|
Hello, i use LWJGL(opengl) and i learn from this tutorial:http://www.youtube.com/watch?v=7G_xtkvyleM from the beginning everything worked just fine until now:( i dont know why every time i play this mini game(mine craft 2d) , the window is black and everything get stuck. why is that happening ? is that a problem with my processor(intel coreTM 2) ? or maybe its the display adapters(NVIDIA GeForce 9500 GT)? help please ! ty. and if you wanna see my code just ask.
|
|
|
|
|
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|