diff --git a/src/ru/dmitriymx/game/engine/Sprite2.java b/src/ru/dmitriymx/game/engine/Sprite2.java index c73baf3..ea822c6 100644 --- a/src/ru/dmitriymx/game/engine/Sprite2.java +++ b/src/ru/dmitriymx/game/engine/Sprite2.java @@ -8,11 +8,11 @@ public class Sprite2 extends Texture { private int count_frames; private int[][] frames_coords; - private void prepare_sprite(int width, int height){ + private void prepare_sprite(int width, int height) { width_frame = width; height_frame = height; - count_frames = (int)(Math.floor(this.getWidth() / width_frame) * Math.floor(this.getHeight() / height_frame)); + count_frames = (int) (Math.floor(this.getWidth() / width_frame) * Math.floor(this.getHeight() / height_frame)); frames_coords = new int[4][count_frames]; } diff --git a/src/ru/dmitriymx/game/engine/Texture.java b/src/ru/dmitriymx/game/engine/Texture.java index bad6a64..21e8c15 100644 --- a/src/ru/dmitriymx/game/engine/Texture.java +++ b/src/ru/dmitriymx/game/engine/Texture.java @@ -12,113 +12,115 @@ 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 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 IntBuffer imageData; + private int rgb_array_size; - private void _init(BufferedImage image){ - bind_id = GL11.glGenTextures(); - this.image = image; - prepare_opengl(); - prepare_image(); - } + private void _init(BufferedImage image) { + bind_id = GL11.glGenTextures(); + this.image = image; + prepare_opengl(); + prepare_image(); + } - public Texture(BufferedImage image) { - _init(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(); - } - } + 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_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(); - } + 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); - } + 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 void bind() { + GL11.glBindTexture(GL11.GL_TEXTURE_2D, bind_id); + } - public int getWidth(){ - return width; - } + public int getWidth() { + return width; + } - public int getHeight(){ - return height; - } + public int getHeight() { + return height; + } - public BufferedImage getImage(){ - return image; - } + public BufferedImage getImage() { + return image; + } - public void setImage(BufferedImage image){ - this.image = image; - prepare_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 setImage(InputStream stream) { + try { + BufferedImage image = ImageIO.read(stream); + stream.close(); + setImage(image); + } catch (Exception e) { + e.printStackTrace(); + } + } - public void updateTexture(){ - prepare_image(); - } + public void updateTexture() { + prepare_image(); + } /** * Преобразует обычные пиксельные координаты в относительные + * * @param x * @return */ - public float floatX(int x){ - return one_pixel_w * x; - } + public float floatX(int x) { + return one_pixel_w * x; + } /** * Преобразует обычные пиксельные координаты в относительные + * * @param y * @return */ - public float floatY(int y){ - return one_pixel_h * y; + public float floatY(int y) { + return one_pixel_h * y; - } + } } diff --git a/src/ru/dmitriymx/game/mw/Prometheus.java b/src/ru/dmitriymx/game/mw/Prometheus.java index f549b3d..253ad35 100644 --- a/src/ru/dmitriymx/game/mw/Prometheus.java +++ b/src/ru/dmitriymx/game/mw/Prometheus.java @@ -7,7 +7,7 @@ import ru.dmitriymx.game.engine.Sprite; public class Prometheus implements TexturedObject { public static int K = 4; - private final double _180_DIV_PI = 180/Math.PI; + private final double _180_DIV_PI = 180 / Math.PI; private final int WIDTH, HEIGHT; private final int SPEED = 2 * K; private int x, y = 0; @@ -16,7 +16,7 @@ public class Prometheus implements TexturedObject { private boolean is_run = false; private boolean run_left = false; - public Prometheus(){ + public Prometheus() { WIDTH = 48; HEIGHT = 32; body = new Sprite(Prometheus.class.getResourceAsStream("/ru/dmitriymx/game/mw-prometheus-01.png"), WIDTH, HEIGHT); @@ -56,19 +56,17 @@ public class Prometheus implements TexturedObject { @Override public void render() { int _x = get_center_x(); - if(_x > Mouse.getX()){ + if (_x > Mouse.getX()) { sprite_inverse = true; - } - else if(_x < Mouse.getX()){ + } else if (_x < Mouse.getX()) { sprite_inverse = false; } - if(is_run){ - if((!run_left && !sprite_inverse) || (run_left && sprite_inverse)){ + if (is_run) { + if ((!run_left && !sprite_inverse) || (run_left && sprite_inverse)) { body.nextFrame(); gun.nextFrame(); - } - else if((run_left && !sprite_inverse) || (!run_left && sprite_inverse)){ + } else if ((run_left && !sprite_inverse) || (!run_left && sprite_inverse)) { body.prevFrame(); gun.prevFrame(); } @@ -84,33 +82,33 @@ public class Prometheus implements TexturedObject { this.x += oX; this.y += oY; - if(oX < 0){ + if (oX < 0) { run_left = true; } else { run_left = false; } } - public int getSpeed(){ + public int getSpeed() { return SPEED; } - public void setRun(boolean value){ + public void setRun(boolean value) { is_run = value; - if(!value){ + if (!value) { body.setFrame(0); gun.setFrame(0); } } - private void render_body(Sprite.Coords frame){ + private void render_body(Sprite.Coords frame) { body.bind(); GL11.glPushMatrix(); GL11.glTranslatef(x, y, 0); GL11.glBegin(GL11.GL_QUADS); - if(!sprite_inverse){ + if (!sprite_inverse) { GL11.glTexCoord2f(body.floatX(frame.x1), body.floatY(frame.y1)); GL11.glVertex2f(0, 0); GL11.glTexCoord2f(body.floatX(frame.x2), body.floatY(frame.y1)); @@ -134,10 +132,10 @@ public class Prometheus implements TexturedObject { GL11.glPopMatrix(); } - private void render_gun(Sprite.Coords frame){ + private void render_gun(Sprite.Coords frame) { int _x; int _y = y + (5 * K); - if(!sprite_inverse){ + if (!sprite_inverse) { _x = x + (19 * K); } else { @@ -147,7 +145,7 @@ public class Prometheus implements TexturedObject { int offcetY = -16 * K; int mX = Mouse.getX(); int mY = Display.getHeight() - Mouse.getY(); - float angle = (float)(Math.atan2(mY - _y, mX - _x) * _180_DIV_PI); + float angle = (float) (Math.atan2(mY - _y, mX - _x) * _180_DIV_PI); gun.bind(); GL11.glPushMatrix(); @@ -155,7 +153,7 @@ public class Prometheus implements TexturedObject { GL11.glRotatef(angle, 0, 0, 1); GL11.glBegin(GL11.GL_QUADS); - if(!sprite_inverse){ + if (!sprite_inverse) { GL11.glTexCoord2f(gun.floatX(frame.x1), gun.floatY(frame.y1)); GL11.glVertex2f(offcetX, offcetY); GL11.glTexCoord2f(gun.floatX(frame.x2), gun.floatY(frame.y1)); @@ -179,7 +177,7 @@ public class Prometheus implements TexturedObject { GL11.glPopMatrix(); } - private int get_center_x(){ + private int get_center_x() { return x + ((WIDTH * K) / 2); } }