style(all)
This commit is contained in:
@@ -8,11 +8,11 @@ public class Sprite2 extends Texture {
|
|||||||
private int count_frames;
|
private int count_frames;
|
||||||
private int[][] frames_coords;
|
private int[][] frames_coords;
|
||||||
|
|
||||||
private void prepare_sprite(int width, int height){
|
private void prepare_sprite(int width, int height) {
|
||||||
width_frame = width;
|
width_frame = width;
|
||||||
height_frame = height;
|
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];
|
frames_coords = new int[4][count_frames];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,113 +12,115 @@ import java.nio.ByteOrder;
|
|||||||
import java.nio.IntBuffer;
|
import java.nio.IntBuffer;
|
||||||
|
|
||||||
public class Texture {
|
public class Texture {
|
||||||
private BufferedImage image;
|
private BufferedImage image;
|
||||||
private int width, height;
|
private int width, height;
|
||||||
private int bind_id;
|
private int bind_id;
|
||||||
private float one_pixel_w, one_pixel_h;
|
private float one_pixel_w, one_pixel_h;
|
||||||
|
|
||||||
private IntBuffer imageData;
|
private IntBuffer imageData;
|
||||||
private int rgb_array_size;
|
private int rgb_array_size;
|
||||||
|
|
||||||
private void _init(BufferedImage image){
|
private void _init(BufferedImage image) {
|
||||||
bind_id = GL11.glGenTextures();
|
bind_id = GL11.glGenTextures();
|
||||||
this.image = image;
|
this.image = image;
|
||||||
prepare_opengl();
|
prepare_opengl();
|
||||||
prepare_image();
|
prepare_image();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Texture(BufferedImage image) {
|
public Texture(BufferedImage image) {
|
||||||
_init(image);
|
_init(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Texture(InputStream stream) {
|
public Texture(InputStream stream) {
|
||||||
try {
|
try {
|
||||||
BufferedImage image = ImageIO.read(stream);
|
BufferedImage image = ImageIO.read(stream);
|
||||||
stream.close();
|
stream.close();
|
||||||
_init(image);
|
_init(image);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void prepare_opengl(){
|
private void prepare_opengl() {
|
||||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, bind_id);
|
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_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_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_S, GL11.GL_REPEAT);
|
||||||
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
|
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void prepare_image(){
|
private void prepare_image() {
|
||||||
if(width != image.getWidth() || height != image.getHeight()){
|
if (width != image.getWidth() || height != image.getHeight()) {
|
||||||
width = image.getWidth();
|
width = image.getWidth();
|
||||||
height = image.getHeight();
|
height = image.getHeight();
|
||||||
one_pixel_w = (100F / width) / 100F;
|
one_pixel_w = (100F / width) / 100F;
|
||||||
one_pixel_h = (100F / height) / 100F;
|
one_pixel_h = (100F / height) / 100F;
|
||||||
rgb_array_size = width * height;
|
rgb_array_size = width * height;
|
||||||
imageData = ByteBuffer.allocateDirect(4 * rgb_array_size).order(ByteOrder.nativeOrder()).asIntBuffer();
|
imageData = ByteBuffer.allocateDirect(4 * rgb_array_size).order(ByteOrder.nativeOrder()).asIntBuffer();
|
||||||
}else{
|
} else {
|
||||||
imageData.clear();
|
imageData.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
int[] rgb_array = new int[rgb_array_size];
|
int[] rgb_array = new int[rgb_array_size];
|
||||||
image.getRGB(0, 0, width, height, rgb_array, 0, width);
|
image.getRGB(0, 0, width, height, rgb_array, 0, width);
|
||||||
imageData.put(rgb_array);
|
imageData.put(rgb_array);
|
||||||
imageData.position(0).limit(rgb_array_size);
|
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);
|
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(){
|
public void bind() {
|
||||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, bind_id);
|
GL11.glBindTexture(GL11.GL_TEXTURE_2D, bind_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getWidth(){
|
public int getWidth() {
|
||||||
return width;
|
return width;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getHeight(){
|
public int getHeight() {
|
||||||
return height;
|
return height;
|
||||||
}
|
}
|
||||||
|
|
||||||
public BufferedImage getImage(){
|
public BufferedImage getImage() {
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setImage(BufferedImage image){
|
public void setImage(BufferedImage image) {
|
||||||
this.image = image;
|
this.image = image;
|
||||||
prepare_image();
|
prepare_image();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setImage(InputStream stream){
|
public void setImage(InputStream stream) {
|
||||||
try{
|
try {
|
||||||
BufferedImage image = ImageIO.read(stream);
|
BufferedImage image = ImageIO.read(stream);
|
||||||
stream.close();
|
stream.close();
|
||||||
setImage(image);
|
setImage(image);
|
||||||
} catch(Exception e){
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateTexture(){
|
public void updateTexture() {
|
||||||
prepare_image();
|
prepare_image();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Преобразует обычные пиксельные координаты в относительные
|
* Преобразует обычные пиксельные координаты в относительные
|
||||||
|
*
|
||||||
* @param x
|
* @param x
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public float floatX(int x){
|
public float floatX(int x) {
|
||||||
return one_pixel_w * x;
|
return one_pixel_w * x;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Преобразует обычные пиксельные координаты в относительные
|
* Преобразует обычные пиксельные координаты в относительные
|
||||||
|
*
|
||||||
* @param y
|
* @param y
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public float floatY(int y){
|
public float floatY(int y) {
|
||||||
return one_pixel_h * y;
|
return one_pixel_h * y;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import ru.dmitriymx.game.engine.Sprite;
|
|||||||
|
|
||||||
public class Prometheus implements TexturedObject {
|
public class Prometheus implements TexturedObject {
|
||||||
public static int K = 4;
|
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 WIDTH, HEIGHT;
|
||||||
private final int SPEED = 2 * K;
|
private final int SPEED = 2 * K;
|
||||||
private int x, y = 0;
|
private int x, y = 0;
|
||||||
@@ -16,7 +16,7 @@ public class Prometheus implements TexturedObject {
|
|||||||
private boolean is_run = false;
|
private boolean is_run = false;
|
||||||
private boolean run_left = false;
|
private boolean run_left = false;
|
||||||
|
|
||||||
public Prometheus(){
|
public Prometheus() {
|
||||||
WIDTH = 48;
|
WIDTH = 48;
|
||||||
HEIGHT = 32;
|
HEIGHT = 32;
|
||||||
body = new Sprite(Prometheus.class.getResourceAsStream("/ru/dmitriymx/game/mw-prometheus-01.png"), WIDTH, HEIGHT);
|
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
|
@Override
|
||||||
public void render() {
|
public void render() {
|
||||||
int _x = get_center_x();
|
int _x = get_center_x();
|
||||||
if(_x > Mouse.getX()){
|
if (_x > Mouse.getX()) {
|
||||||
sprite_inverse = true;
|
sprite_inverse = true;
|
||||||
}
|
} else if (_x < Mouse.getX()) {
|
||||||
else if(_x < Mouse.getX()){
|
|
||||||
sprite_inverse = false;
|
sprite_inverse = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(is_run){
|
if (is_run) {
|
||||||
if((!run_left && !sprite_inverse) || (run_left && sprite_inverse)){
|
if ((!run_left && !sprite_inverse) || (run_left && sprite_inverse)) {
|
||||||
body.nextFrame();
|
body.nextFrame();
|
||||||
gun.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();
|
body.prevFrame();
|
||||||
gun.prevFrame();
|
gun.prevFrame();
|
||||||
}
|
}
|
||||||
@@ -84,33 +82,33 @@ public class Prometheus implements TexturedObject {
|
|||||||
this.x += oX;
|
this.x += oX;
|
||||||
this.y += oY;
|
this.y += oY;
|
||||||
|
|
||||||
if(oX < 0){
|
if (oX < 0) {
|
||||||
run_left = true;
|
run_left = true;
|
||||||
} else {
|
} else {
|
||||||
run_left = false;
|
run_left = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSpeed(){
|
public int getSpeed() {
|
||||||
return SPEED;
|
return SPEED;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRun(boolean value){
|
public void setRun(boolean value) {
|
||||||
is_run = value;
|
is_run = value;
|
||||||
|
|
||||||
if(!value){
|
if (!value) {
|
||||||
body.setFrame(0);
|
body.setFrame(0);
|
||||||
gun.setFrame(0);
|
gun.setFrame(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void render_body(Sprite.Coords frame){
|
private void render_body(Sprite.Coords frame) {
|
||||||
body.bind();
|
body.bind();
|
||||||
GL11.glPushMatrix();
|
GL11.glPushMatrix();
|
||||||
GL11.glTranslatef(x, y, 0);
|
GL11.glTranslatef(x, y, 0);
|
||||||
|
|
||||||
GL11.glBegin(GL11.GL_QUADS);
|
GL11.glBegin(GL11.GL_QUADS);
|
||||||
if(!sprite_inverse){
|
if (!sprite_inverse) {
|
||||||
GL11.glTexCoord2f(body.floatX(frame.x1), body.floatY(frame.y1));
|
GL11.glTexCoord2f(body.floatX(frame.x1), body.floatY(frame.y1));
|
||||||
GL11.glVertex2f(0, 0);
|
GL11.glVertex2f(0, 0);
|
||||||
GL11.glTexCoord2f(body.floatX(frame.x2), body.floatY(frame.y1));
|
GL11.glTexCoord2f(body.floatX(frame.x2), body.floatY(frame.y1));
|
||||||
@@ -134,10 +132,10 @@ public class Prometheus implements TexturedObject {
|
|||||||
GL11.glPopMatrix();
|
GL11.glPopMatrix();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void render_gun(Sprite.Coords frame){
|
private void render_gun(Sprite.Coords frame) {
|
||||||
int _x;
|
int _x;
|
||||||
int _y = y + (5 * K);
|
int _y = y + (5 * K);
|
||||||
if(!sprite_inverse){
|
if (!sprite_inverse) {
|
||||||
_x = x + (19 * K);
|
_x = x + (19 * K);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@@ -147,7 +145,7 @@ public class Prometheus implements TexturedObject {
|
|||||||
int offcetY = -16 * K;
|
int offcetY = -16 * K;
|
||||||
int mX = Mouse.getX();
|
int mX = Mouse.getX();
|
||||||
int mY = Display.getHeight() - Mouse.getY();
|
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();
|
gun.bind();
|
||||||
GL11.glPushMatrix();
|
GL11.glPushMatrix();
|
||||||
@@ -155,7 +153,7 @@ public class Prometheus implements TexturedObject {
|
|||||||
GL11.glRotatef(angle, 0, 0, 1);
|
GL11.glRotatef(angle, 0, 0, 1);
|
||||||
|
|
||||||
GL11.glBegin(GL11.GL_QUADS);
|
GL11.glBegin(GL11.GL_QUADS);
|
||||||
if(!sprite_inverse){
|
if (!sprite_inverse) {
|
||||||
GL11.glTexCoord2f(gun.floatX(frame.x1), gun.floatY(frame.y1));
|
GL11.glTexCoord2f(gun.floatX(frame.x1), gun.floatY(frame.y1));
|
||||||
GL11.glVertex2f(offcetX, offcetY);
|
GL11.glVertex2f(offcetX, offcetY);
|
||||||
GL11.glTexCoord2f(gun.floatX(frame.x2), gun.floatY(frame.y1));
|
GL11.glTexCoord2f(gun.floatX(frame.x2), gun.floatY(frame.y1));
|
||||||
@@ -179,7 +177,7 @@ public class Prometheus implements TexturedObject {
|
|||||||
GL11.glPopMatrix();
|
GL11.glPopMatrix();
|
||||||
}
|
}
|
||||||
|
|
||||||
private int get_center_x(){
|
private int get_center_x() {
|
||||||
return x + ((WIDTH * K) / 2);
|
return x + ((WIDTH * K) / 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user