commit c8ce51596e0f490f7be9a2e4ec17f9ff0ce7c659 Author: DmitriyMX Date: Wed Oct 9 09:17:49 2013 +0400 Init project diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9270f1c --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +## Common ## +lib/ +target/ +*.rar + +## IDEA ## +.idea/ +out/ +*.iml + +## Eclipse ## +.settings/ +bin/ +.classpath +.project diff --git a/build.properties b/build.properties new file mode 100644 index 0000000..55410cf --- /dev/null +++ b/build.properties @@ -0,0 +1,10 @@ +source.dir = src +resource.dir = resource +lib.dir = lib + +target.dir = target +classes.dir = ${target.dir}/classes + +lwjgl.version = 2.9.0 +jinput.version = 2.0.5 +launch4j.version = 3.1.0-beta2 \ No newline at end of file diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..fa632fd --- /dev/null +++ b/build.xml @@ -0,0 +1,163 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -Djava.library.path=lib/native + + + + + \ No newline at end of file diff --git a/resource/exe/icon.ico b/resource/exe/icon.ico new file mode 100644 index 0000000..06a8468 Binary files /dev/null and b/resource/exe/icon.ico differ diff --git a/resource/java/ru/dmitriymx/game/foxy.png b/resource/java/ru/dmitriymx/game/foxy.png new file mode 100644 index 0000000..80c237a Binary files /dev/null and b/resource/java/ru/dmitriymx/game/foxy.png differ diff --git a/resource/java/ru/dmitriymx/game/foxy2.png b/resource/java/ru/dmitriymx/game/foxy2.png new file mode 100644 index 0000000..b70edf4 Binary files /dev/null and b/resource/java/ru/dmitriymx/game/foxy2.png differ diff --git a/resource/java/ru/dmitriymx/game/foxy3.png b/resource/java/ru/dmitriymx/game/foxy3.png new file mode 100644 index 0000000..c27a509 Binary files /dev/null and b/resource/java/ru/dmitriymx/game/foxy3.png differ diff --git a/resource/java/ru/dmitriymx/game/texture.png b/resource/java/ru/dmitriymx/game/texture.png new file mode 100644 index 0000000..aa4431a Binary files /dev/null and b/resource/java/ru/dmitriymx/game/texture.png differ diff --git a/src/ru/dmitriymx/game/Foxy.java b/src/ru/dmitriymx/game/Foxy.java new file mode 100644 index 0000000..446f577 --- /dev/null +++ b/src/ru/dmitriymx/game/Foxy.java @@ -0,0 +1,39 @@ +package ru.dmitriymx.game; + +import org.lwjgl.opengl.GL11; + +public class Foxy { + private Sprite texture; + private int x, y; + + public Foxy(){ + texture = new Sprite(Foxy.class.getResourceAsStream("/ru/dmitriymx/game/foxy2.png"), 38, 33); + x = y = 0; + } + + public void render(){ + Sprite.Coords frame = texture.getFrame(); + final int K = 2; + + GL11.glColor3f(1f, 1f, 1f); + texture.bind(); + + GL11.glBegin(GL11.GL_QUADS); + + GL11.glTexCoord2f(texture.floatX(frame.x1),texture.floatY(frame.y1)); + GL11.glVertex2f(0, 0); + + GL11.glTexCoord2f(texture.floatX(frame.x2),texture.floatY(frame.y1)); + GL11.glVertex2f(texture.getWidthSprite()*K, 0); + + GL11.glTexCoord2f(texture.floatX(frame.x2),texture.floatY(frame.y2)); + GL11.glVertex2f(texture.getWidthSprite()*K, texture.getHeightSprite()*K); + + GL11.glTexCoord2f(texture.floatX(frame.x1),texture.floatY(frame.y2)); + GL11.glVertex2f(0, texture.getHeightSprite()*K); + + GL11.glEnd(); + + texture.nextFrame(); + } +} diff --git a/src/ru/dmitriymx/game/Main.java b/src/ru/dmitriymx/game/Main.java new file mode 100644 index 0000000..811c9e5 --- /dev/null +++ b/src/ru/dmitriymx/game/Main.java @@ -0,0 +1,58 @@ +package ru.dmitriymx.game; + +import org.lwjgl.LWJGLException; +import org.lwjgl.opengl.Display; +import org.lwjgl.opengl.DisplayMode; +import org.lwjgl.opengl.GL11; + +public class Main { + private Foxy foxy; + + private void init_display(int width, int height){ + try { + Display.setDisplayMode(new DisplayMode(width, height)); + Display.setVSyncEnabled(true); + Display.create(); + } catch (LWJGLException e) { + e.printStackTrace(); + System.exit(-1); + } + } + + private void init_opengl(int width, int height) { + GL11.glEnable(GL11.GL_TEXTURE_2D); + GL11.glClearColor(0, 0, 0.3f, 0); + + GL11.glEnable(GL11.GL_BLEND); + GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); + + GL11.glMatrixMode(GL11.GL_PROJECTION); + GL11.glLoadIdentity(); + GL11.glOrtho(0, width, height, 0, 1, -1); + GL11.glMatrixMode(GL11.GL_MODELVIEW); + } + + private void render(){ + foxy.render(); + } + + public void start(int width, int height){ + init_display(width, height); + init_opengl(width, height); + + foxy = new Foxy(); + + while(!Display.isCloseRequested()){ + GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); + render(); + Display.update(); + Display.sync(12); + } + + Display.destroy(); + } + + public static void main(String[] args){ + new Main().start(800,600); + } +} diff --git a/src/ru/dmitriymx/game/Sprite.java b/src/ru/dmitriymx/game/Sprite.java new file mode 100644 index 0000000..538316a --- /dev/null +++ b/src/ru/dmitriymx/game/Sprite.java @@ -0,0 +1,66 @@ +package ru.dmitriymx.game; + +import java.awt.image.BufferedImage; +import java.io.InputStream; + +public class Sprite extends Texture { + public class Coords{ + int x1,y1,x2,y2; + Coords(int x1, int y1, int x2, int y2){ + this.x1 = x1; + this.y1 = y1; + this.x2 = x2; + this.y2 = y2; + } + } + private Coords[] frame; + private int max_frames; + private int current_frame = 0; + private int width, height; + + private void _init(){ + double v1 = Math.floor(this.getWidth() / width); + double v2 = Math.floor(this.getHeight() / height); + max_frames = (int)(v1 * v2); + + frame = new Coords[max_frames]; + for(int i = 0; i < max_frames; i++){ + frame[i] = new Coords(i*width, i*height, (i+1)*width, (i+1)*height); + } + } + + public Sprite(BufferedImage image, int width, int height) { + super(image); + this.width = width; + this.height = height; + _init(); + } + + public Sprite(InputStream stream, int width, int height) { + super(stream); + this.width = width; + this.height = height; + _init(); + } + + public Coords getFrame(){ + return frame[current_frame]; + } + + public void nextFrame(){ + if(current_frame == (max_frames-1)){ + current_frame = 0; + } else { + current_frame++; + } + System.out.println(current_frame); + } + + public int getWidthSprite(){ + return width; + } + + public int getHeightSprite(){ + return height; + } +} diff --git a/src/ru/dmitriymx/game/Texture.java b/src/ru/dmitriymx/game/Texture.java new file mode 100644 index 0000000..fee4abc --- /dev/null +++ b/src/ru/dmitriymx/game/Texture.java @@ -0,0 +1,114 @@ +package ru.dmitriymx.game; + +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL12; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.IntBuffer; + +public class Texture { + private BufferedImage image; + private int width, height; + private int bind_id; + private float one_pixel_w, one_pixel_h; + + private IntBuffer imageData; + private int rgb_array_size; + + private void _init(BufferedImage image){ + bind_id = GL11.glGenTextures(); + this.image = image; + prepare_opengl(); + prepare_image(); + } + + public Texture(BufferedImage image) { + _init(image); + } + + public Texture(InputStream stream) { + try { + BufferedImage image = ImageIO.read(stream); + stream.close(); + _init(image); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void prepare_opengl(){ + GL11.glBindTexture(GL11.GL_TEXTURE_2D, bind_id); + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT); + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT); + } + + private void prepare_image(){ + if(width != image.getWidth() || height != image.getHeight()){ + width = image.getWidth(); + height = image.getHeight(); + one_pixel_w = (100F / width) / 100F; + one_pixel_h = (100F / height) / 100F; + rgb_array_size = width * height; + imageData = ByteBuffer.allocateDirect(4 * rgb_array_size).order(ByteOrder.nativeOrder()).asIntBuffer(); + }else{ + imageData.clear(); + } + + int[] rgb_array = new int[rgb_array_size]; + image.getRGB(0, 0, width, height, rgb_array, 0, width); + imageData.put(rgb_array); + imageData.position(0).limit(rgb_array_size); + GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, imageData); + } + + public void bind(){ + GL11.glBindTexture(GL11.GL_TEXTURE_2D, bind_id); + } + + public int getWidth(){ + return width; + } + + public int getHeight(){ + return height; + } + + public BufferedImage getImage(){ + return image; + } + + public void setImage(BufferedImage image){ + this.image = image; + prepare_image(); + } + + public void setImage(InputStream stream){ + try{ + BufferedImage image = ImageIO.read(stream); + stream.close(); + setImage(image); + } catch(Exception e){ + e.printStackTrace(); + } + } + + public void updateTexture(){ + prepare_image(); + } + + public float floatX(int x){ + return one_pixel_w * x; + } + + public float floatY(int y){ + return one_pixel_h * y; + + } +}