id
stringlengths 36
36
| text
stringlengths 1
1.25M
|
---|---|
acaea77c-5a29-445e-b9c8-d823b58deb98
|
public static void main(String[] args) {
Disruptor<CalculateNumbersEvent> disruptor = new Disruptor<CalculateNumbersEvent>(CalculateNumbersEvent.EVENT_FACTORY, createExecutor(),
new SingleThreadedClaimStrategy(RING_SIZE),
new SleepingWaitStrategy());
disruptor.handleEventsWith(new FactorialCalculator(), new FibonacciCalculator())
.then(new HexRepresentationCalculator(), new BinaryRepresentationCalculator())
.then(new EventOutputHandler());
RingBuffer<CalculateNumbersEvent> ringBuffer = disruptor.start();
// Publishers claim events in sequence
long sequence = ringBuffer.next();
CalculateNumbersEvent event = ringBuffer.get(sequence);
event.setValue(5);
// make the event available to EventProcessors
ringBuffer.publish(sequence);
disruptor.publishEvent(new EventTranslator<CalculateNumbersEvent>() {
@Override
public void translateTo(CalculateNumbersEvent event, long sequence) {
event.setFired(new Date());
}
});
}
|
7af262c5-7665-44e4-b1de-8c2e0ce77bbb
|
@Override
public void translateTo(CalculateNumbersEvent event, long sequence) {
event.setFired(new Date());
}
|
6c2e0be3-1d6e-4bd5-8cf4-fdae56f8b914
|
private static Executor createExecutor() {
return Executors.newSingleThreadExecutor();
}
|
5954d046-dda3-449d-80d3-a4700c210823
|
public long getValue() {
return value;
}
|
727b4310-abe5-4eaf-b2ec-60ac8df8d4aa
|
public void setValue(final long value) {
this.value = value;
}
|
be0813a6-a9c7-47db-bb9b-f87e8d63ca32
|
public ValueEvent newInstance() {
return new ValueEvent();
}
|
7850ca45-468e-476f-b040-2656814afd86
|
@Override
public void onEvent(CalculateNumbersEvent event, long sequence, boolean endOfBatch) throws Exception {
long value = event.getValue();
long result = factorial(value);
event.setFactorialValue(new CalculatedNumber(result));
}
|
f5925f6e-3ebd-4bde-b287-0d7ee426a02d
|
private long factorial(long n) {
long result = 1;
for(int i = 2; i < n; i++){
result *= i;
}
return result;
}
|
c3f308b5-7b31-4039-b25b-3d58e6f915e0
|
public Date getFired() {
return fired;
}
|
7d1897dc-ddbe-4ef4-80ca-604a5337dba5
|
public void setFired(Date fired) {
this.fired = fired;
}
|
958831ad-27e0-4d83-8e01-0d7eea51950a
|
public long getValue() {
return value;
}
|
4bf463e7-5feb-46f2-99e8-2f8dd999c99b
|
public void setValue(long value) {
this.value = value;
}
|
02d2ea35-bbac-40ec-b547-338aef6ade1c
|
public CalculatedNumber getFibonacciValue() {
return fibonacciValue;
}
|
537213d0-a643-465e-b6f9-a4c4fa869e82
|
public void setFibonacciValue(CalculatedNumber fibonacciValue) {
this.fibonacciValue = fibonacciValue;
}
|
c429d5f9-4907-4dcb-a733-e633bc29396f
|
public CalculatedNumber getFactorialValue() {
return factorialValue;
}
|
67a81b68-8ae1-4907-9d71-73b78c1a33a9
|
public void setFactorialValue(CalculatedNumber factorialValue) {
this.factorialValue = factorialValue;
}
|
f2151996-bd46-4824-9dfd-5aaf84b9a5f0
|
public CalculateNumbersEvent newInstance() {
return new CalculateNumbersEvent();
}
|
15afe3ee-9d99-4606-beb9-662449f7b408
|
@Override
public String toString() {
return "CalculateNumbersEvent{" +
"fired=" + fired +
", value=" + value +
", fibonacciValue=" + fibonacciValue +
", factorialValue=" + factorialValue +
'}';
}
|
f96d1dbf-a9f5-4bae-8b4c-2bea019c83bd
|
@Override
public void onEvent(CalculateNumbersEvent event, long sequence, boolean endOfBatch) throws Exception {
System.out.println(event.toString());
}
|
4d0bfe16-04a6-4aaa-b63b-6de2d366f57d
|
@Override
public void onEvent(CalculateNumbersEvent event, long sequence, boolean endOfBatch) throws Exception {
long value = event.getValue();
long result = fibonacci(value);
event.setFibonacciValue(new CalculatedNumber(result));
}
|
56646af1-0537-441e-aaac-b68303f6854e
|
private long fibonacci(long n) {
if (n <= 1) return n;
else return fibonacci(n - 1) + fibonacci(n - 2);
}
|
fb50908d-127f-4bfa-9345-326b35dd639e
|
public CalculatedNumber(long value) {
this.value = value;
}
|
8db7401b-4bf1-4bcd-934b-e092e3130546
|
public String getBinaryRepresentation() {
return binaryRepresentation;
}
|
938810ad-0871-4e1b-b1d8-76aebe4a0b83
|
public void setBinaryRepresentation(String binaryRepresentation) {
this.binaryRepresentation = binaryRepresentation;
}
|
a38c9452-7e67-4813-a7e0-d5faad16114e
|
public String getHexRepresentation() {
return hexRepresentation;
}
|
a54d2167-4d3b-4781-aa08-8243262c6302
|
public void setHexRepresentation(String hexRepresentation) {
this.hexRepresentation = hexRepresentation;
}
|
4017a0c7-0c62-448f-bd29-0b98e6e3153f
|
public long getValue() {
return value;
}
|
460477a2-c2bf-40a2-a668-eca6f9004670
|
@Override
public String toString() {
return "CalculatedNumber{" +
"binaryRepresentation='" + binaryRepresentation + '\'' +
", hexRepresentation='" + hexRepresentation + '\'' +
'}';
}
|
769eccfc-193c-4f77-8c97-ba65da8070a3
|
public int closer(int i, int target, int step){
if(Math.abs(target-i) <= step ){ return target; }
if(i < target){
return i + step;
} else if(i > target){
return i - step;
} else {
return target;
}
}
|
9385a83f-4bab-4bac-88c1-741910c9c570
|
public UtilTest() {
}
|
980abda4-9ed2-4678-8109-d8fa7f5d3b77
|
@Test
public void testDrawString() {
assertTrue(closer(10, 5, 1) == 9);
assertTrue(closer(10, 5, 2) == 8);
assertTrue(closer(-10, 5, 1) == -9);
assertTrue(closer(-10, 5, 2) == -8);
assertTrue(closer(1, 2, 5) == 2);
assertTrue(closer(-1, 1, 1) == 0);
assertTrue(closer(0, 0, 1) == 0);
}
|
cf379a29-ef6e-4201-bfa9-7cc914a69612
|
public Player(int x, int y) {
this.x = x;
this.y = y;
tex = tx.getTexture("assets/dude.png");
this.q = new Quad(x, y, tex);
}
|
f1032e2f-1fbc-4a8b-8b63-b699bcd22310
|
public int[] getForce() {
return force;
}
|
9f4a7cbb-d082-4fab-9858-b5e94d818d42
|
public void addForce(int x, int y) {
this.force[0] += x;
this.force[1] += y;
}
|
cbf46113-b703-4dc0-a9de-582fade2d004
|
public void setDirection(int direction){
if(direction == LEFT){
this.tex = tx.getTexture("assets/dudeLeft.png");
} else {
this.tex = tx.getTexture("assets/dude.png");
}
}
|
69bdd1eb-9f59-4212-b78d-2753fc1c8543
|
@Override
public void draw() {
q.setPos(x, y);
q.setTexture(tex);
q.draw();
}
|
f24a98de-3cc2-4e6b-9a24-95d59f7f0640
|
public int lock(int i, int min, int max){
if(i > max){ i = max; } else if(i < min){ i = min; }
return i;
}
|
8daa867a-6072-423b-89d3-6179f8130a0a
|
public int closer(int i, int target, int step){
if(Math.abs(target-i) <= step ){ return target; }
if(i < target){
return i + step;
} else if(i > target){
return i - step;
} else {
return target;
}
}
|
5c5d30a1-790a-48fc-9c81-e041d1c800b6
|
public boolean isAirbourne(){
return force[1] > 0;
}
|
55f62d30-1e1a-4b48-9386-ba806b3cc60b
|
@Override
public void tick(State gamestate) {
this.force[0] = lock(this.force[0], -20, 20);
this.force[1] = lock(this.force[1], -50, 50);
// x
if(this.force[0] > 0){
// moving in positive x
this.x += this.force[0];
Entity xBlocker = gamestate.blockingEntity(this);
if(xBlocker != null){
int borderInset = (int)(int)this.getBounds().getMinX()-x;
this.x = (int)xBlocker.getBounds().getMinX()-(int)this.getBounds().getWidth()-borderInset;
}
} else if(this.force[0] < 0){
// moving in negative x
this.x += this.force[0];
Entity xBlocker = gamestate.blockingEntity(this);
if(xBlocker != null){
int borderInset = (int)this.getBounds().getMinX() - x;
this.x = (int)xBlocker.getBounds().getMaxX() - borderInset;
}
}
// y
if(this.force[1] > 0){
this.y += this.force[1];
if(gamestate.blockingEntity(this) != null){
this.y -= this.force[1];
}
} else if(this.force[1] < 0){
this.y += this.force[1];
Entity xBlocker = gamestate.blockingEntity(this);
if(xBlocker != null){
this.y = (int)xBlocker.getBounds().getMaxY();
}
if(gamestate.blockingEntity(this) != null){
this.y -= this.force[1];
}
}
// friction & gravity
force[0] = closer(force[0], 0, 2);
force[1] = closer(force[1], -40, 2);
// bounding
if(this.y <= 0){
this.y = 0;
}
}
|
c3fa2c28-10b0-4d40-9626-f7172762b25a
|
@Override
public Rectangle getBounds() {
return new Rectangle(x+15, y, 64-30, 57);
}
|
5f18cc5c-6d3b-4e33-b96e-aa602c1a04a0
|
public int getX(){ return x; }
|
c977c522-ba05-4afb-a876-781c9c5b9d0f
|
public int getY(){ return y; }
|
b23a3d53-9b16-4ac2-834c-bd01ec2d10a9
|
public Rectangle getBounds();
|
d6303859-042f-4b7f-bc67-a3c256d6b68c
|
public void tick(State gamestate);
|
53a52b3f-e37f-4660-8147-36ad5a06e969
|
public void draw();
|
d1406c48-ba99-49da-998d-b820106676ca
|
public Mock(int x, int y) {
this.x = x;
this.y = y;
tex = tx.getTexture("assets/sand.png");
this.q = new Quad(x, y, tex);
}
|
077a973a-0a64-4bd7-ba5c-b130dabc0809
|
@Override
public void draw() {
q.draw();
}
|
46d271f8-1b31-4383-82ec-3bd82ad268e0
|
@Override
public void tick(State gamestate) {
}
|
bd2ddec7-344a-474e-9ae0-96cca70c01e5
|
@Override
public Rectangle getBounds() {
return new Rectangle(x, y, 64, 64);
}
|
7ab45ad5-903f-43fb-b84a-8c2b4ebf82b8
|
public void draw();
|
859babfd-ab92-49e9-a542-0589f84800dc
|
public Quad(float x, float y, Texture tex){
this.x = x;
this.y = y;
this.tex = tex;
}
|
6ac70b71-4d30-41bd-9960-39753cfbc48f
|
public void setPos(int x, int y){
this.x = x;
this.y = y;
}
|
7809b2f0-3ab3-40ac-b42c-6aa8a8f663a0
|
public void setTexture(Texture t){
this.tex = t;
}
|
418c8e14-8786-47a1-b96b-98f8dcaf2dcb
|
@Override
public void draw(){
if(this.tex != null){
tex.bind();
}
GL11.glPushMatrix();
GL11.glTranslatef(x, y, 0);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 1);
GL11.glVertex2f(0, 0);
GL11.glTexCoord2f(1, 1);
GL11.glVertex2f(64, 0);
GL11.glTexCoord2f(1, 0);
GL11.glVertex2f(64, 64);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(0, 64);
GL11.glEnd();
GL11.glPopMatrix();
}
|
9c06221d-25a3-4d52-9438-28f44cb5d92e
|
public IsoGrid(){
TextureLoader tx = new TextureLoader();
Texture testTex = tx.getTexture("assets/iso_grass.png");
Texture testTex2 = tx.getTexture("assets/iso_grass2.png");
Texture flowers = tx.getTexture("assets/iso_grass3.png");
Texture flower = tx.getTexture("assets/iso_grass4.png");
Texture mushroom = tx.getTexture("assets/iso_grass5.png");
Texture clump = tx.getTexture("assets/iso_grass6.png");
Texture tex = null;
for(int y = 0; y < 50; y++){
for(int x = 0; x < 20; x++){
if(rnd.nextInt(100) > 98 ){
if(rnd.nextInt(100) > 70){
tex = mushroom;
} else {
tex = flowers;
}
} else if(rnd.nextInt(100) > 95 ){
if(rnd.nextInt(100) > 70){
tex = clump;
} else {
tex = flower;
}
} else if(rnd.nextInt(100) > 60 ){
tex = testTex2;
} else {
tex = testTex;
}
if(y % 2 == 0.0){
Drawable q = new IsoQuad(x*64, (y*16), tex);
pipe.add(q);
} else {
Drawable q = new IsoQuad((x*64)+32, (y*16), tex);
pipe.add(q);
}
}
}
}
|
816ab206-2fcc-4272-b8cb-05df7a95e059
|
@Override
public void draw() {
for(Drawable d : pipe){
d.draw();
}
}
|
5bea537b-4716-436b-b760-edd8ee036a23
|
public static void drawString(int x, int y, String text){
int tilesize = 16;
for(int i = 0; i < text.length(); ++i){
String character = "" + text.charAt(i);
TextTile tile = alphabetmap.get(character.toUpperCase());
if(tile == null){
tile = alphabetmap.get("?");
}
tile.setPosition(i * tilesize, y);
tile.draw();
}
}
|
2f6f1311-e350-421f-8795-22771ca8054c
|
public TextTile(int row, int col, Texture t){
this.x = 0;
this.y = 0;
this.row = row;
this.col = col;
this.tex = t;
}
|
cf17165e-f3d5-470e-8422-df774b4e6b0f
|
public void setPosition(int x, int y){
this.x = x;
this.y = y;
}
|
3c91ce0d-7b1f-4914-84a4-a1b6b1c7999a
|
@Override
public void draw(){
if(this.tex != null){
tex.bind();
}
GL11.glPushMatrix();
GL11.glTranslatef(x, y, 0);
GL11.glBegin(GL11.GL_QUADS);
float dist = 1f / numtiles;
float tileSize=16f;
GL11.glTexCoord2f(col*dist, dist*(row+1));
GL11.glVertex2f(0, 0);
GL11.glTexCoord2f(dist+(col*dist), dist*(row+1));
GL11.glVertex2f(tileSize, 0);
GL11.glTexCoord2f(dist+(col*dist), dist*row);
GL11.glVertex2f(tileSize, tileSize);
GL11.glTexCoord2f(col*dist, dist*row);
GL11.glVertex2f(0, tileSize);
GL11.glEnd();
GL11.glPopMatrix();
}
|
81bbaba0-7aba-4592-a9bb-ecb64597a07c
|
public IsoQuad(float x, float y, Texture tex){
this.x = x;
this.y = y;
this.tex = tex;
}
|
822e7100-00a8-42a5-bae1-5285fc75855c
|
public void draw(){
if(this.tex != null){
tex.bind();
}
GL11.glPushMatrix();
GL11.glTranslatef(x, y, 0);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 1);
GL11.glVertex2f(0, 0);
GL11.glTexCoord2f(1, 1);
GL11.glVertex2f(64, 0);
GL11.glTexCoord2f(1, 0);
GL11.glVertex2f(64, 32);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(0, 32);
GL11.glEnd();
GL11.glPopMatrix();
}
|
0ca6cc02-a379-4601-b8ef-3e04e9c86e1e
|
public static boolean isIn(Rectangle r, int pointX, int pointY){
return r.contains(pointX, pointY);
}
|
d7b889b5-6654-4b09-8492-bbfc6268f7b4
|
public Texture(int target, int textureID) {
this.target = target;
this.textureID = textureID;
}
|
6cdb2d10-846a-49da-800e-0ed852919704
|
public void bind() {
if(currentlyBoundTexture != textureID){
glBindTexture(target, textureID);
currentlyBoundTexture = textureID;
}
}
|
6d749e50-3df8-4a4a-92c5-47d115c4fc07
|
public void setHeight(int height) {
this.height = height;
setHeight();
}
|
f1b1092f-ffd1-41ea-a6a0-3f44df73067b
|
public void setWidth(int width) {
this.width = width;
setWidth();
}
|
09231bfb-f17f-4088-a3f1-c5848864b5f7
|
public int getImageHeight() {
return height;
}
|
92d61ea5-1cf3-4b79-b925-0a6701e29290
|
public int getImageWidth() {
return width;
}
|
6f884571-51fc-4744-99c6-523144ad2d3a
|
public float getHeight() {
return heightRatio;
}
|
4b0f80fb-9fb4-4b07-a187-5752cea6c248
|
public float getWidth() {
return widthRatio;
}
|
a19e2c87-9bd5-4523-bb3f-478aa696b18c
|
public void setTextureHeight(int texHeight) {
this.texHeight = texHeight;
setHeight();
}
|
f28ae038-6fe6-4b11-96cd-b24227a197ad
|
public void setTextureWidth(int texWidth) {
this.texWidth = texWidth;
setWidth();
}
|
41035c33-8813-461f-ba7d-77b17b250c56
|
private void setHeight() {
if (texHeight != 0) {
heightRatio = ((float) height) / texHeight;
}
}
|
351448a1-5a27-4ced-a4e7-5a7575b7fe6e
|
private void setWidth() {
if (texWidth != 0) {
widthRatio = ((float) width) / texWidth;
}
}
|
d9c016bd-1cd9-40ea-997e-9c30dac7dd41
|
public Engine(){
try {
Display.setDisplayMode(new DisplayMode(800,600));
Display.create();
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 0, 600, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
} catch (LWJGLException e) {
throw new RuntimeException(e);
}
}
|
d19a6d5e-296a-43c0-a83e-3fbee36aefb3
|
public void start() {
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
// init
gameState.setPlayer(new Player(0, 0));
gameState.getEntities().add(new Mock(200, 0));
// game loop
while (!Display.isCloseRequested()) {
Date d = new Date();
long startTime = d.getTime();
draw();
update();
handleInput();
d = new Date();
long endTime = d.getTime();
try{ Thread.sleep(33 - (endTime-startTime)); } catch(Exception e){ }
}
Display.destroy();
}
|
e79aad0b-f92e-40fe-8058-13aa8133c1d5
|
private void update(){
for(Entity e : gameState.getEntities()){
e.tick(gameState);
}
}
|
4536009d-387d-4960-81aa-b85390a4b2f1
|
private void draw(){
// Clear the screen and depth buffer
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glScalef(scale, scale, scale);
GL11.glTranslatef(-camerax, -cameray, 0);
// draw shit here
for(Entity e : gameState.getEntities()){
e.draw();
}
primitives.Util.drawString(10, 560, "pos " + gameState.getPlayer().getX() + " " + gameState.getPlayer().getY());
primitives.Util.drawString(10, 540, "force " + gameState.getPlayer().getForce()[0] + " " + gameState.getPlayer().getForce()[1]);
Display.update();
}
|
94843f9d-7ba5-40f7-9c2e-6d264e1152a9
|
public void handleInput(){
// mouse stuff apparently
if(Mouse.isButtonDown(0)){
System.out.println(String.format("Screen: %s/%s", Mouse.getX(), Mouse.getY()));
System.out.println(String.format("World : %s/%s", Mouse.getX()+camerax, Mouse.getY()+cameray));
gameState.getEntities().add(new Mock(Mouse.getX(), Mouse.getY()));
}
// handle movement keys
float increment = 7.0f;
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
// todo
camerax -= increment;
}
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
camerax += increment;
}
if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
cameray -= increment;
}
if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
cameray += increment;
}
if (Keyboard.isKeyDown(Keyboard.KEY_Q)) {
scale -= 0.1;
} else if (Keyboard.isKeyDown(Keyboard.KEY_E)) {
scale += 0.1;
}
if (Keyboard.isKeyDown(Keyboard.KEY_C )) {
System.out.println(String.format("Mouse: %s/%s", Mouse.getX(), Mouse.getY()));
System.out.println(String.format("Camera: %s/%s", camerax, cameray));
}
if (Keyboard.isKeyDown(Keyboard.KEY_SPACE) && gameState.getPlayer().isAirbourne() == false) {
gameState.getPlayer().addForce(0, 20);
}
if (Keyboard.isKeyDown(Keyboard.KEY_D )) {
gameState.getPlayer().setDirection(Player.RIGHT);
gameState.getPlayer().addForce(3, 0);
}
if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
gameState.getPlayer().setDirection(Player.LEFT);
gameState.getPlayer().addForce(-8, 0);
}
}
|
cdd508d1-4fb7-4073-a208-177d5ac138b8
|
public State() {
}
|
3a12a89c-4ff0-471f-83e4-0ebc6f144384
|
public TextureLoader getLoader() {
return loader;
}
|
beb94dda-3dbf-48e0-a1b3-9ab84831ca3b
|
public List<Entity> getEntities() {
return entities;
}
|
b6d1e322-c336-4a48-ab88-e5b2bbddcb70
|
public void setPlayer(Player p){
this.player = p;
this.entities.add(p);
}
|
d9949729-ee54-4551-80be-090a7113a403
|
public Player getPlayer(){
return this.player;
}
|
cecbf423-e783-4d17-986f-07509fc84e08
|
public boolean blocking(Entity a){
for(Entity ent : entities){
if(a != ent && a.getBounds().intersects(ent.getBounds())){
return true;
}
}
return false;
}
|
3d4c7d7f-cd8a-467c-ab66-b8666824e3cd
|
public Entity blockingEntity(Entity a){
for(Entity ent : entities){
if(a != ent && a.getBounds().intersects(ent.getBounds())){
return ent;
}
}
return null;
}
|
e632854f-899f-4399-b1ee-76ff5b2bbbb5
|
public static void main(String[] args) {
Engine e = new Engine();
e.start();
}
|
c0163276-fc76-428b-9144-08fb7cf87c3c
|
public TextureLoader() {
glAlphaColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[] {8,8,8,8},
true,
false,
ComponentColorModel.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
glColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[] {8,8,8,0},
false,
false,
ComponentColorModel.OPAQUE,
DataBuffer.TYPE_BYTE);
}
|
aead00b1-7ed8-4bb4-af67-fd631e7142ef
|
private int createTextureID() {
glGenTextures(textureIDBuffer);
return textureIDBuffer.get(0);
}
|
d4cff87c-6a2b-4565-a200-6dd3c52b7fe0
|
public Texture getTexture(String resourceName){
Texture tex = table.get(resourceName);
if (tex != null) {
return tex;
}
try{
tex = getTexture(resourceName, GL_TEXTURE_2D, GL_RGBA, GL_LINEAR, GL_LINEAR);
} catch(Exception e){
throw new RuntimeException(e);
}
table.put(resourceName,tex);
return tex;
}
|
53b9de31-b38b-4654-a233-1a80f2fb1bae
|
public Texture getTexture(String resourceName,
int target,
int dstPixelFormat,
int minFilter,
int magFilter) throws IOException {
int srcPixelFormat;
// create the texture ID for this texture
int textureID = createTextureID();
Texture texture = new Texture(target,textureID);
// bind this texture
glBindTexture(target, textureID);
BufferedImage bufferedImage = loadImage(resourceName);
texture.setWidth(bufferedImage.getWidth());
texture.setHeight(bufferedImage.getHeight());
if (bufferedImage.getColorModel().hasAlpha()) {
srcPixelFormat = GL_RGBA;
} else {
srcPixelFormat = GL_RGB;
}
// convert that image into a byte buffer of texture data
ByteBuffer textureBuffer = convertImageData(bufferedImage,texture);
if (target == GL_TEXTURE_2D) {
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilter);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilter);
}
// produce a texture from the byte buffer
glTexImage2D(target,
0,
dstPixelFormat,
get2Fold(bufferedImage.getWidth()),
get2Fold(bufferedImage.getHeight()),
0,
srcPixelFormat,
GL_UNSIGNED_BYTE,
textureBuffer );
return texture;
}
|
eb246eed-0c49-4ea4-b70f-371c41270f6e
|
private static int get2Fold(int fold) {
int ret = 2;
while (ret < fold) {
ret *= 2;
}
return ret;
}
|
6b5bdeb8-7c3c-4e23-ba49-8feef9828401
|
private ByteBuffer convertImageData(BufferedImage bufferedImage,Texture texture) {
ByteBuffer imageBuffer;
WritableRaster raster;
BufferedImage texImage;
int texWidth = 2;
int texHeight = 2;
// find the closest power of 2 for the width and height
// of the produced texture
while (texWidth < bufferedImage.getWidth()) {
texWidth *= 2;
}
while (texHeight < bufferedImage.getHeight()) {
texHeight *= 2;
}
texture.setTextureHeight(texHeight);
texture.setTextureWidth(texWidth);
// create a raster that can be used by OpenGL as a source
// for a texture
if (bufferedImage.getColorModel().hasAlpha()) {
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,texWidth,texHeight,4,null);
texImage = new BufferedImage(glAlphaColorModel,raster,false,new Hashtable());
} else {
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,texWidth,texHeight,3,null);
texImage = new BufferedImage(glColorModel,raster,false,new Hashtable());
}
// copy the source image into the produced image
Graphics g = texImage.getGraphics();
g.setColor(new Color(0f,0f,0f,0f));
g.fillRect(0,0,texWidth,texHeight);
g.drawImage(bufferedImage,0,0,null);
// build a byte buffer from the temporary image
// that be used by OpenGL to produce a texture.
byte[] data = ((DataBufferByte) texImage.getRaster().getDataBuffer()).getData();
imageBuffer = ByteBuffer.allocateDirect(data.length);
imageBuffer.order(ByteOrder.nativeOrder());
imageBuffer.put(data, 0, data.length);
imageBuffer.flip();
return imageBuffer;
}
|
fee8daaa-60ad-43cc-ae00-c1b71ae0f80a
|
private BufferedImage loadImage(String ref) throws IOException {
URL url = TextureLoader.class.getClassLoader().getResource(ref);
if (url == null) {
throw new IOException("Cannot find: " + ref);
}
Image img = new ImageIcon(url).getImage();
BufferedImage bufferedImage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics g = bufferedImage.getGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
return bufferedImage;
}
|
57151bca-484d-4644-8185-18da417921f0
|
public ListOfTasks() {
taskList = new ArrayList<Task>();
}
|
7da66d0e-4fa6-4435-8c66-25c79780c175
|
public ArrayList<Task> getTaskList() {
return taskList;
}
|
2f9daaa5-f6bd-40ff-a480-61e09b0ba9db
|
public void addTask(Task newTask) {
taskList.add(newTask);
}
|
f2ceaae4-37a2-45a0-a98f-6c078fad6c59
|
public void removeTask(Task task) {
taskList.remove(task);
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.