Init project
This commit is contained in:
39
src/ru/dmitriymx/game/Foxy.java
Normal file
39
src/ru/dmitriymx/game/Foxy.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
58
src/ru/dmitriymx/game/Main.java
Normal file
58
src/ru/dmitriymx/game/Main.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
66
src/ru/dmitriymx/game/Sprite.java
Normal file
66
src/ru/dmitriymx/game/Sprite.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
114
src/ru/dmitriymx/game/Texture.java
Normal file
114
src/ru/dmitriymx/game/Texture.java
Normal file
@@ -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;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user