-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLWJGL_Sprite.java
156 lines (111 loc) · 3.2 KB
/
LWJGL_Sprite.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import java.io.FileInputStream;
import java.io.IOException;
import org.lwjgl.opengl.GL11;
public class LWJGL_Sprite {
private LWJGL_Texture texture;
private float u1, v1, u2, v2, scalex, scaley;
private float x, y, offsetx, offsety;
public LWJGL_Sprite(String filename) {
try {
texture = new LWJGL_Texture(new FileInputStream(filename));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(0);
}
offsetx = 0;
offsety = 0;
scalex = 1.0f;
scaley = 1.0f;
}
public void init(){
this.setU1(0.0f); this.setV1(0.0f);
this.setU2(1.0f); this.setV2(1.0f);
texture.init();
}
public void init(float u1, float v1, float u2, float v2){
this.setU1(u1); this.setV1(v1);
this.setU2(u2); this.setV2(v2);
texture.init();
}
public void draw(float x, float y) {
this.x = x;
this.y = y;
texture.bind();
GL11.glPushMatrix();
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(u1, v1); GL11.glVertex2f(x - offsetx , y - offsety );
GL11.glTexCoord2f(u1, v2); GL11.glVertex2f(x - offsetx , y - offsety + texture.getHeight() * scaley * (v2-v1) );
GL11.glTexCoord2f(u2, v2); GL11.glVertex2f(x - offsetx + texture.getWidth() * scalex * (u2-u1), y - offsety + texture.getHeight() * scaley * (v2-v1) );
GL11.glTexCoord2f(u2, v1); GL11.glVertex2f(x - offsetx + texture.getWidth() * scalex * (u2-u1), y - offsety );
GL11.glEnd();
GL11.glPopMatrix();
texture.unbind();
}
public void draw() {
texture.bind();
GL11.glPushMatrix();
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(u1, v1); GL11.glVertex2f(x - offsetx , y - offsety );
GL11.glTexCoord2f(u1, v2); GL11.glVertex2f(x - offsetx , y - offsety + texture.getHeight() * scaley * (v2-v1) );
GL11.glTexCoord2f(u2, v2); GL11.glVertex2f(x - offsetx + texture.getWidth() * scalex * (u2-u1), y - offsety + texture.getHeight() * scaley * (v2-v1) );
GL11.glTexCoord2f(u2, v1); GL11.glVertex2f(x - offsetx + texture.getWidth() * scalex * (u2-u1), y - offsety );
GL11.glEnd();
GL11.glPopMatrix();
texture.unbind();
}
public float getU1() {
return u1;
}
public void setU1(float u) {
this.u1 = u;
}
public float getV1() {
return v1;
}
public void setV1(float v) {
this.v1 = v;
}
public float getU2() {
return u2;
}
public void setU2(float u) {
this.u2 = u;
}
public float getV2() {
return v2;
}
public void setV2(float v) {
this.v2 = v;
}
public float getScaleX() {
return scalex;
}
public void setScaleX(float scalex) {
this.scalex = scalex;
}
public float getScaleY() {
return scaley;
}
public void setScaleY(float scaley) {
this.scaley = scaley;
}
public float getOffsetX() {
return offsetx;
}
public void setOffsetX(float offsetx) {
this.offsetx = offsetx;
}
public float getOffsetY() {
return offsety;
}
public void setOffsetY(float offsety) {
this.offsety = offsety;
}
public int getWidth(){
return texture.getWidth();
}
public int getHeight(){
return texture.getHeight();
}
}