public class CrossAndCircles extends ApplicationAdapter{
OrthographicCamera camera;
SpriteBatch batch;
ShapeRenderer renderer;
Array<Rectangle> array;
Vector3 touchPos;
int turn, cellSize, touchCount;
float tempX, tempY;
#Override
public void create () {
batch = new SpriteBatch();
renderer = new ShapeRenderer();
camera = new OrthographicCamera();
camera.setToOrtho(false, 480, 480);
touchCount = 0;
cellSize = 480/3-5;
array = new Array<Rectangle>();
//cell's coordinates
Rectangle oneOne = new Rectangle(0, 0 ,cellSize, cellSize);
Rectangle oneTwo = new Rectangle(0, cellSize+5, cellSize, cellSize);
Rectangle oneThree = new Rectangle(0, 480-cellSize, cellSize, cellSize);
Rectangle twoOne = new Rectangle(cellSize+5, 0 , cellSize, cellSize);
Rectangle twoTwo = new Rectangle(cellSize+5, cellSize+5, cellSize, cellSize);
Rectangle twoThree = new Rectangle(cellSize+10, 480-cellSize, cellSize, cellSize);
Rectangle threeOne = new Rectangle(480-cellSize, 0 , cellSize, cellSize);
Rectangle threeTwo = new Rectangle (480-cellSize, cellSize+5, cellSize, cellSize);
Rectangle threeThree = new Rectangle(480-cellSize, 480-cellSize, cellSize, cellSize);
array.add(oneOne); array.add(oneTwo); array.add(oneThree); array.add(twoOne);
array.add(twoTwo); array.add(twoThree); array.add(threeOne); array.add(threeTwo); array.add(threeThree);
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
//our field
batch.setProjectionMatrix(camera.combined);
renderer.begin(ShapeType.Filled);
renderer.setColor(Color.BLACK);
renderer.rect(480/3-5, 0, 10, 480);
renderer.rect(480/3*2-5, 0, 10, 480);
renderer.rect(0, 480 / 3 - 10, 480, 10);
renderer.rect(0, 480 / 3 * 2 - 10, 480, 10);
renderer.end();
//if user touch a rectangle draw a circle or a cross
if(Gdx.input.isTouched()){
for (Rectangle rect:array) {
if (rect.contains(Gdx.input.getX(), Gdx.input.getY())) {
tempX = rect.x;
tempY = rect.y;
break;
}}
touchCount++;
}
if (touchCount>0 && touchCount<=9){
renderer.begin(ShapeType.Filled);
renderer.rect(tempX+20,tempY+20,0, 0, 10, cellSize, 1, 1, -45);
renderer.end();}
}
public void dispose(){super.dispose(); renderer.dispose();}
}
Help me with my Tic tac toe game. I've still added only one stick but it appears only twice, and when I'm clicking to the upper cells it appears in lowers. And I don't know why the first stick is not saved, it just change it's place. What's wrong in my logic?
there is difference in touch and screen coordinates,
refer :
https://github.com/libgdx/libgdx/wiki/Coordinate-systems
Related
I'm trying to display a simple textured trapezoid (a road in perspective). For this I'm using SpriteBatch.draw with vertexes array as a parameter. But the result is unexpected.
What I expected:
What I got:
What exactly gone wrong? Or maybe I'm using the wrong method?
Here is the code:
#Override
public void create () {
texture = new Texture("road.jpg");
spriteBatch = new SpriteBatch();
float color = Color.toFloatBits(255, 255, 255, 255);
verts = new float[]{
0, 0, color, 0, 0,
Gdx.graphics.getWidth()/2-100, 300, color, 0, 1,
Gdx.graphics.getWidth()/2+100, 300, color, 1, 1,
Gdx.graphics.getWidth(), 0, color, 1, 0,
};
shapeRenderer = new ShapeRenderer();
}
#Override
public void render () {
Gdx.gl.glClearColor(0, 0.5f, 1f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.rect(0, 0, 640, 300);
shapeRenderer.end();
spriteBatch.begin();
spriteBatch.draw(texture, verts, 0, verts.length);
spriteBatch.end();
}
From documentation
draw(Texture texture, float[] spriteVertices, int offset, int count)
Draws a rectangle using the given vertices.
LibGDX draws these rectangles as two triangles, so it splits the texture. The problem is that these triangles in rectangle are the same size, but in trapezoid they are not, so they become distorted.
Solution would be to use projection or mesh.
So, I'm working on a game with OpenGL and Java, and I've decided to use JSFML for my windowing, text, image loading, etc... And, i'm having some small issues.
The OpenGL drawing works fine until I try to play with depth then it just doesn't work!
Here's my code so far:
import java.io.*;
import org.jsfml.graphics.*;
import org.jsfml.window.*;
import static org.lwjgl.opengl.GL11.*;
//import static org.lwjgl.util.glu.GLU.*;
public class Main
{
static Image icon;
static Texture playert;
static long a, b, c, d;
static float e;
public static void main(String[] args) throws Exception
{
c = 0l;
icon = new Image();
icon.loadFromStream(new FileInputStream("Icon.png"));
ContextSettings contextSettings;
contextSettings = new ContextSettings (24, 8, 0, 2, 1);
VideoMode v = new VideoMode(800, 600);
RenderWindow window = new RenderWindow(new VideoMode(800, 600), "SFML Game", org.jsfml.window.WindowStyle.DEFAULT, contextSettings);
window.setIcon(icon);
window.setFramerateLimit(120);
Context.getContext().setActive(true);
Text pauseMessage = new Text();
Font font = new Font();
font.loadFromStream(new FileInputStream("Minecraftia.ttf"));
pauseMessage.setFont(font);
pauseMessage.setCharacterSize(20);
pauseMessage.setPosition(0.f, 0.f);
pauseMessage.setColor(new Color(250, 250, 250));
pauseMessage.setString("This is Mission:Iinfinity, prototyped in JSFML");
Text fps = new Text();
fps.setFont(font);
fps.setCharacterSize(20);
fps.setPosition(0.f, 20.f);
fps.setColor(new Color(250, 250, 250));
fps.setString("FPS: " + c);
org.lwjgl.opengl.GLContext.useContext(Context.getContext());
glDepthMask(true);
glClearDepth(1.f);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float ratio = window.getSize().x / window.getSize().y;
glFrustum(-ratio, ratio, -1.f, 1.f, 1.f, 500.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
a = System.currentTimeMillis();
d = System.currentTimeMillis();
while (true)
{
window.clear();
if(b - a >= 1000)
{
a = b;
fps.setString("FPS: " + c);
c = 0;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
window.pushGLStates();
window.draw(pauseMessage);
window.draw(fps);
window.popGLStates();
glPushMatrix();
glTranslatef(0.0f, 0.0f, -10f);
//glRotatef(10f, 0.f, 0.f, 1.f);
glBegin(GL_POLYGON);
glVertex3f(-0.5f, -0.5f, 0);
glVertex3f(-0.5f, 0.5f, 0);
glVertex3f(0.5f, 0.5f, 0);
glVertex3f(0.5f, -0.5f, 0);
glEnd();
glPopMatrix();
window.display();
if(Keyboard.isKeyPressed(Keyboard.Key.ESCAPE))
{
window.close();
System.exit(0);
}
b = System.currentTimeMillis();
c++;
}
}
}
EDIT: Ok, fixed the ContextSettings but that cube still stubbornly refuses to draw further than 1 coord deep or 1 coord back
Of course it is not working, you are constructing the ContextSettings class object this way:
contextSettings = new ContextSettings(0, 4, 2, 32, 32);
The meaning of each parameter in the constructor is, in order:
int depthBits
int stencilBits
int antialiasingLevel
int majorVersion
int minorVersion
You are requesting a render-context that has a 0-bit depth buffer, 4-bit stencil buffer, 2x MSAA, and OpenGL 32.32!
Consider something a little bit more sane like this:
contextSettings = new ContextSettings (24, 8, 0, 2, 1);
24-bit Depth, 8-Bit Stencil, 0x MSAA, OpenGL 2.1.
im drawing a basic grid using a nested for loop (libgdx) but when i test it on android it keeps on flickering i have tried many different ways but still cant get it to stop the flickering.. here is my code
public void create() {
rend = new ShapeRenderer();
screenH = Gdx.graphics.getHeight();
screenH = (int) (screenH*(0.32));
System.out.println(screenH);
screenW = Gdx.graphics.getWidth();
screenW = (int) (screenW*(0.322));
System.out.println(screenW);
batch = new SpriteBatch();
dots = new Rectangle();
storeV();
render method
public void render() {
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
//Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
rend.begin(ShapeType.Filled);
batch.setColor(Color.WHITE);
rend.rect(100, 100, 200, 200);
for (int i=0; i<4; i++){
for (int j=0;j<4;j++){
//batch.draw(dot, posx, posy);
rend.setColor(Color.WHITE);
rend.rect(posx, posy, 16, 16);
posx+=screenW;
}
posx=0;
rend.rect(posx, posy, 16, 16);
// batch.draw(dot, posx, posy);
posy+=screenH;
}
rend.end();
batch.begin();
batch.end();
}
as you see i have tried drawing a rectangle and using a texture but both happen to flicker. the rectangle drawn outside the loop dosent flicker.
and here is my storeV()
public void storeV(){
for (int i=0; i<4; i++){
Xpoints[i]+=screenW*i;
}
for (int i=0; i<4; i++){
Ypoints[i]+=screenH*i;
}
So, I'm using LibGdx and trying to use their Rectangle class as a bounds for button pressing onn the touch screen. It works perfectly on a 1:1 scale, but when I put the game on my Phone (With a smaller screen) than the images get scaled and drawn properly, but the Rectangles don't. So I tried keeping my Rectangles at their normal scale, and "upscaling" the touch screen's XY Coords, but I guess I'm not doing that right, cause it doesn't work.
optionsMenu = new Vector<Rectangle>();
optionsMenu.add(new Rectangle(100 + (120 * 0), 100, 100, 100));
optionsMenu.add(new Rectangle(100 + (120 * 1), 100, 100, 100));
optionsMenu.add(new Rectangle(100 + (120 * 2), 100, 100, 100));
optionsMenu.add(new Rectangle(100 + (120 * 3), 100, 100, 100));
That's how i'm initalizing my bounding Rectangles.
This is how I'm initalizing my camera:
camera = new OrthographicCamera();
camera.setToOrtho(true, 800, 480);
This is how I'm drawing my buttons:
spriteBatch.draw(buttonImage, optionsMenu.get(2).bounds.getX(),
optionsMenu.get(2).bounds.getY(), optionsMenu.get(2).bounds.getWidth(),
optionsMenu.get(2).bounds.getHeight(), 0, 0, buttonImage.getWidth(),
buttonImage.getHeight(), false, true);
And this is how I do my touch screen logic:
public boolean tap(float x, float y, int count, int button) {
Vector3 temp = new Vector3(x, y, 0);
camera.unproject(temp);
float scalePos = (int) ((float) Gdx.graphics.getWidth()/800.0f);
temp.x = temp.x * scalePos;
temp.y = temp.y * scalePos;
if(optionsMenu.get(0).bounds.contains(temp.x, temp.y)){
//do sutff
}
else if(optionsMenu.get(1).bounds.contains(temp.x, temp.y)){
//do other stuff
}
return false;
}
A few days ago I ran into the same problem. This is what I did to solve it:
If you are using a viewport, then you should add that data to the camera.unproject call, to make sure that the viewport is being taken into account.
For example:
camera.unproject(lastTouch,viewport.x,viewport.y,viewport.width,viewport.height);
To debug the rectangle bounds and the touch position, I used this method to draw them into the screen:
private static ShapeRenderer debugShapeRenderer = new ShapeRenderer();
public static void showDebugBoundingBoxes(List<Rectangle> boundingBoxes) {
debugShapeRenderer.begin(ShapeType.Line); // make sure to end the spritebatch before you call this line
debugShapeRenderer.setColor(Color.BLUE);
for (Rectangle rect : boundingBoxes) {
debugShapeRenderer.rect(rect.x, rect.y, rect.width, rect.height);
}
debugShapeRenderer.end();
}
I have an animation, and would like to make it disappear progressively when it reaches the left side of the display area.
AffineTransform at = new AffineTransform();
at.scale(-1, 1);
at.translate((-clip.x + (clip.x - xPositionToPixel(imgX))), clip.y - 1);
if ((clip.x - clip.width) < (at.getTranslateX() - bufImg.getWidth())) {
g2d.drawImage(bufImg, at, null);
} else {
at = new AffineTransform();
at.scale(-1, 1);
at.translate((-clip.x + (clip.x - xPositionToPixel(imgX))), clip.y - 1);
g2d.drawImage(bufImg, (int)at.getTranslateX(), clip.y - 1, (int)(bufImg.getWidth() - (xPositionToPixel(imgX) + bufImg.getWidth())), clip.height, null);
}
I'm drawing the animation from right to left, that is the reason why i scale and translate each the coordinate. clip.x is the start of the display area, and imgX is the new x coordinate.
Thanks for your help.
I tried several way of achieving what i want and the closest is this :
AffineTransform at = new AffineTransform();
at.scale(-1, 1);
at.translate((-clip.x + (clip.x - xPositionToPixel(imgX))), clip.y - 1);
if ((clip.x - clip.width) < (at.getTranslateX() - bufImg.getWidth())) {
g2d.drawImage(bufImg, at, null);
} else {
at = new AffineTransform();
at.scale(-1, 1);
at.translate((-clip.x + (clip.x - xPositionToPixel(imgX))), clip.y - 1);
g2d.drawImage(bufImg, (int)at.getTranslateX(), clip.y - 1, (int)(bufImg.getWidth() - (xPositionToPixel(imgX) + bufImg.getWidth())), clip.height, null);
}
But still not a good soluation has i'm just shrinking the width of my image, but still draw it entirely.
Still don't know what exactly the problem is (animation or transform?), here's a simple snippet to play with:
final JButton button = new JButton("Swinging!");
button.setSize(button.getPreferredSize());
button.doLayout();
final BufferedImage image = new BufferedImage(
button.getWidth(), button.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
button.paint(g);
g.dispose();
final Point location = new Point(500, 100);
final JPanel panel = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D gd = (Graphics2D) g.create();
gd.translate(location.x, location.y);
gd.scale(-1, 1);
gd.drawImage(image, 0, 0, this);
gd.dispose();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(button.getWidth()* 10, button.getHeight() * 20);
}
};
ActionListener l = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
location.x -= 5;
panel.repaint();
if (location.x < - button.getWidth()) {
((Timer) e.getSource()).stop();
}
}
};
new Timer(100, l).start();