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
package com.powersource.gdxtest;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Gdx2DPixmap;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class GdxTest implements ApplicationListener {
	private OrthographicCamera camera;
	private SpriteBatch batch;
	private Texture texture;
	private Sprite sprite;
	public float w = 0;
	public float h = 0;
	public float xpos;
	public float ypos;
	public float dirx = 0;
	public float diry = 0;
	public float diag = 0;
	public float xprevpos = 0;
	public float yprevpos = 0;
	public float mouseSetX = 0;
	public float mouseSetY = 0;

	@Override
	public void create() {
		w = Gdx.graphics.getWidth();
		h = Gdx.graphics.getHeight();

		camera = new OrthographicCamera(1, h / w);
		batch = new SpriteBatch();

		texture = new Texture(Gdx.files.internal("data/libgdx.png"));
		texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

		TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);

		sprite = new Sprite(region);
		sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());
		//sprite.setSize(1f, 1f * sprite.getHeight() / sprite.getWidth());
		sprite.setOrigin(sprite.getWidth() / 2, sprite.getHeight() / 2);
		float xpos = -sprite.getWidth() / 2;
		float ypos = -sprite.getHeight() / 2;
		sprite.setPosition(xpos, ypos);
	}

	@Override
	public void dispose() {
		batch.dispose();
		texture.dispose();
	}

	@Override
	public void render() {
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

		batch.setProjectionMatrix(camera.combined);
		batch.begin();

		// egen
		w = Gdx.graphics.getWidth();
		h = Gdx.graphics.getHeight();

		if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
			dirx += 1;
		}
		if (Gdx.input.isKeyPressed(Keys.LEFT)) {
			dirx -= 1;
		}
		if (Gdx.input.isKeyPressed(Keys.UP)) {
			diry += 1;
		}
		if (Gdx.input.isKeyPressed(Keys.DOWN)) {
			diry -= 1;
		}
		if ((dirx == 1 || dirx == -1) && (diry == 1 || diry == -1)) {
			diag = (float) Math.sqrt(0.5);
		} else {
			diag = 1;
		}
		xpos += diag * dirx * 1f / Gdx.graphics.getWidth();
		// p("delta-xpos: " + diag * dirx); // if you want percentage of window
		// add
		// / Gdx.graphics.getWidth()
		ypos += diag * diry * 1f / Gdx.graphics.getHeight();
		// p("delta-ypos: " + diag * diry);
		dirx = diry = 0;

		sprite.setPosition(xpos, ypos);

		if (Gdx.input.isButtonPressed(Buttons.LEFT)) {
			mouseSetX = (Gdx.input.getX() - (w / 2f)) / w;
			mouseSetY = ((h - Gdx.input.getY()) - (h / 2f)) / h;
			p("((h - Gdx.input.getY()) - (h / 2f)) / h");
			p("h: " + h);
			p("getY: " + Gdx.input.getY());
			p("width: " + Gdx.graphics.getWidth());
			p("height: " + Gdx.graphics.getHeight());
			p("mouseSetY * h: " + mouseSetY*h);
			sprite.setPosition(mouseSetX, mouseSetY);
		}

		sprite.draw(batch);
		batch.end();
	}

	public void p(String print) {
		System.out.println(print);
	}

	@Override
	public void resize(int width, int height) {
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}
}