feat: basic drawing

This commit is contained in:
Laureηt 2021-04-05 18:45:57 +02:00
parent 65c63a0327
commit 4b36703287
5 changed files with 257 additions and 22 deletions

View file

@ -0,0 +1,58 @@
package bzh.fainsin.sagittarius;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;
abstract class Entity {
// ---------- ATTRIBUTs ----------
protected Vector2 position;
protected float mass = 1.0f;
protected float angle;
protected Color color = Color.WHITE;
// ---------- CONSTRUCTORs ----------
protected Entity(Vector2 position, float mass) {
this.position = position;
this.mass = mass;
}
protected Entity(Vector2 position, float mass, Color color) {
this(position, mass);
this.color = color;
}
// ---------- GETs ----------
Vector2 getPosition() {
return this.position;
}
float getMass() {
return this.mass;
}
float getAngle() {
return this.angle;
}
Color getColor() {
return this.color;
}
// ---------- METHODs ----------
/**
* Return the euclidian distance to another entity
* @param entity
* @return distance to entity
*/
float distanceTo(Entity entity) {
return this.position.sub(entity.getPosition()).len();
}
}

View file

@ -0,0 +1,105 @@
package bzh.fainsin.sagittarius;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
class GameScreen extends ScreenAdapter {
// ---------- ATTRIBUTES ----------
// screen size
static final int WIDTH = 1920;
static final int HEIGHT = 1080;
// drawing stuff
private SpriteBatch batch;
private ShapeRenderer shapeRenderer;
private BitmapFont font;
// camera stuff
private Viewport viewport;
public static Camera camera;
// TODO: categorize better ?
private HUD hud = new HUD();
// ---------- METHODS ----------
private void clearScreen() {
Gdx.gl.glClearColor(Color.BLACK.r, Color.BLACK.g, Color.BLACK.b, Color.BLACK.a);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
private void update(float delta) {
SagittariusGame.update(delta);
hud.update();
camera.update();
}
@Override
public void render(float delta) {
update(delta);
clearScreen();
// ---------- batch draw ----------
batch.setProjectionMatrix(camera.combined);
batch.begin();
// ---------- HUD ----------
hud.render(batch, font);
batch.end();
// ---------- shapeRenderer draw ----------
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
// ---------- planets ----------
for (Planet planet : SagittariusGame.planetList) {
planet.render(shapeRenderer);
}
shapeRenderer.end();
}
@Override
public void show() {
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.position.set(WIDTH / 2, HEIGHT / 2, 0);
camera.update();
viewport = new FitViewport(WIDTH, HEIGHT, camera);
shapeRenderer = new ShapeRenderer();
batch = new SpriteBatch();
font = new BitmapFont();
}
@Override
public void resize(int width, int height) {
viewport.update(width, height);
camera.update();
batch.setProjectionMatrix(camera.combined);
shapeRenderer.setProjectionMatrix(camera.combined);
}
@Override
public void dispose() {
batch.dispose();
font.dispose();
shapeRenderer.dispose();
}
}

View file

@ -0,0 +1,23 @@
package bzh.fainsin.sagittarius;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
class HUD {
// ATTRIBUTS
private int frameRate;
// MÉTHODES
public void update() {
frameRate = Gdx.graphics.getFramesPerSecond();
}
public void render(Batch batch, BitmapFont font) {
font.draw(batch, frameRate + " fps", 3, GameScreen.HEIGHT - 3);
}
}

View file

@ -0,0 +1,42 @@
package bzh.fainsin.sagittarius;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;
class Planet extends Entity {
// ---------- ATTRIBUTEs ----------
private float radius = 10;
// ---------- CONSTRUCTORs ----------
Planet(Vector2 position, float mass, float radius) {
super(position, mass);
this.radius = radius;
}
Planet(Vector2 position, float mass, float radius, Color color) {
super(position, mass, color);
this.radius = radius;
}
// ---------- METHODS ----------
void render(ShapeRenderer shapeRenderer) {
shapeRenderer.setColor(color);
shapeRenderer.circle(this.position.x, this.position.y, radius);
}
void renderDebug(Batch batch, BitmapFont font) {
font.draw(batch, "x = " + (int) position.x + ", y = " + (int) position.y, position.x, position.y);
}
float getRadius() {
return this.radius;
}
}

View file

@ -1,33 +1,40 @@
package bzh.fainsin.sagittarius; package bzh.fainsin.sagittarius;
import com.badlogic.gdx.ApplicationAdapter; import java.util.ArrayList;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class SagittariusGame extends ApplicationAdapter { import com.badlogic.gdx.Game;
SpriteBatch batch; import com.badlogic.gdx.Gdx;
Texture img; import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
public class SagittariusGame extends Game {
// ---------- ATTRIBUTEs ----------
// Vectors
static Vector2 screenCursor;
static Vector2 worldCursor;
// Planets
static ArrayList<Planet> planetList;
// ---------- METHODs ----------
@Override @Override
public void create() { public void create() {
batch = new SpriteBatch(); setScreen(new GameScreen());
img = new Texture("badlogic.jpg");
planetList = new ArrayList<Planet>();
planetList.add( new Planet(new Vector2(100, 100), 1, 50) );
} }
@Override static void update(float delta) {
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1); // ---------- cursors ----------
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); screenCursor = new Vector2(Gdx.input.getX(), Gdx.input.getY());
batch.begin(); Vector3 unprojectedCursor = GameScreen.camera.unproject(new Vector3(screenCursor, 0));
batch.draw(img, 0, 0); worldCursor = new Vector2(unprojectedCursor.x, unprojectedCursor.y);
batch.end();
} }
@Override
public void dispose () {
batch.dispose();
img.dispose();
}
} }