Thomas Allen avatar Thomas Allen committed 172de03

Re-added terrain gen files

Comments (0)

Files changed (4)

src/com/xardov/terrain/gen/Main.java

+package com.xardov.terrain.gen;
+
+import org.lwjgl.LWJGLException;
+import org.lwjgl.opengl.Display;
+import org.lwjgl.opengl.DisplayMode;
+import org.lwjgl.opengl.GL11;
+
+public class Main
+{
+	private final int WIDTH = 640, HEIGHT = 480;
+	private final int FPS = 60;
+	
+	public boolean paused = false;
+	
+	public Map map = new Map();
+	
+	Sprite sprite;
+	
+	public void init()
+	{
+		try 
+		{
+			Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
+			Display.create();
+		} catch (LWJGLException e) 
+		{
+			e.printStackTrace();
+			System.exit(0);
+		}
+		
+		GL11.glMatrixMode(GL11.GL_PROJECTION);
+		GL11.glLoadIdentity();
+		GL11.glOrtho(0, WIDTH, 0, HEIGHT, 1, -1);
+		GL11.glMatrixMode(GL11.GL_MODELVIEW);
+		
+		map.init();
+		
+		sprite = new Sprite(0, 20);
+		try {
+			sprite.loadImage("assets/graphics/alien.png", 43, 29);
+		} catch (Exception e) {
+			// TODO: handle exception
+		}
+		
+		while(!Display.isCloseRequested())
+		{
+			GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
+			
+			render();
+			
+			Display.update();
+		}
+		
+		Display.destroy();
+	}
+	
+	public void render()
+	{
+		map.render();
+		sprite.render();
+	}
+	
+	public static void main(String[] args) 
+	{
+		Main main = new Main();
+		main.init();
+	}
+
+}

src/com/xardov/terrain/gen/Map.java

+package com.xardov.terrain.gen;
+
+public class Map 
+{
+	public static final int WIDTH = 40, HEIGHT = 30;
+	public static final int TILE_WIDTH = 16;
+	
+	public Tile terrain[][] = new Tile[WIDTH][HEIGHT];
+	
+	public Map()
+	{
+	}
+	
+	public void init()
+	{
+		genTerrain();
+	}
+	
+	public void genTerrain()
+	{
+		for(int i = 0; i < WIDTH; i++)
+			for(int k = 0; k < HEIGHT; k++)
+			{
+				terrain[i][k] = new Tile(i * TILE_WIDTH, k * TILE_WIDTH);
+			}
+	}
+	
+	public void render()
+	{
+		for(int i = 0; i < WIDTH; i++)
+			for(int k = 0; k < HEIGHT; k++)
+			{
+				terrain[i][k].render();
+			}
+	}
+}

src/com/xardov/terrain/gen/Sprite.java

+package com.xardov.terrain.gen;
+
+import java.io.IOException;
+
+import org.lwjgl.opengl.GL11;
+import org.newdawn.slick.opengl.Texture;
+import org.newdawn.slick.opengl.TextureLoader;
+
+public class Sprite 
+{
+	Texture bodyTexture;
+	
+	public float x, y; 
+	private int width, height;
+	
+	public Sprite(int x, int y)
+	{
+		this.x = x;
+		this.y = y;
+	}
+	
+	public void loadImage(String filePath, int width, int height) throws IOException
+	{		
+		bodyTexture = TextureLoader.getTexture("PNG", Main.class.getClassLoader().
+														getResourceAsStream(filePath));
+		
+		this.width = width;
+		this.height = height;
+	}
+	
+	public void render()
+	{
+		GL11.glPushMatrix();
+		
+		bodyTexture.bind();
+		
+		GL11.glBegin(GL11.GL_QUADS);
+		{
+			GL11.glColor3f(1.0f, 1.0f, 1.0f);
+			
+			GL11.glTexCoord2f(0, 0);
+			GL11.glVertex2f(x, y);
+			
+			GL11.glTexCoord2f(1, 0);
+			GL11.glVertex2f(x + bodyTexture.getTextureWidth(), y);
+			
+			GL11.glTexCoord2f(0, 1);
+			GL11.glVertex2f(x, y - bodyTexture.getTextureHeight());
+			
+			GL11.glTexCoord2f(1, 1);
+			GL11.glVertex2f(x + bodyTexture.getTextureWidth(), 
+							y - bodyTexture.getTextureHeight());
+		}
+		GL11.glEnd();
+		
+		//drawHitbox(0, 0, x + bodyTexture.getWidth(), y - bodyTexture.getHeight());
+		
+		GL11.glPopMatrix();
+	}
+	
+	private void drawHitbox(float x1, float y1, float x2, float y2)
+	{
+		GL11.glBegin(GL11.GL_LINES);
+		{
+			GL11.glColor3f(1.0f, 0.0f, 0.0f);
+			
+			GL11.glVertex2f(x1, y1);
+			GL11.glVertex2f(x2, y2);
+		}
+		GL11.glEnd();
+	}
+}

src/com/xardov/terrain/gen/Tile.java

+package com.xardov.terrain.gen;
+
+public class Tile extends Sprite
+{
+	public Tile(int x, int y)
+	{
+		super(x, y);
+		
+		try 
+		{
+			loadImage("assets/graphics/grass.png", 16, 16);
+		} catch (Exception e) 
+		{
+			e.printStackTrace();
+			System.exit(0);
+		}
+	}
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.