Skip to content
Menu
The Renaissance Raven
  • Game Development
    • Project 1 – ???
    • Unity Tutorials
  • Electronics and Programming
  • Leisure
  • Blog
The Renaissance Raven

*libGDX* Camera work and Writing Text

In this post we are going to be focusing on creating a camera to view our screen and a very simple act of writing text to appear on screen when touched. First if you are following from the previous post there is something that you will need to change in the main game class:

1
2
3
4
5
6
7
8
9
    //Public void create() is ran when the program is started.
public void create () {
//Creates the SpriteBatch for gameBatch to use
gameBatch = new SpriteBatch();
//Creates the font, which is the default font for LIBgdx.
gameFont = new BitmapFont();
//this will set the screen to our next class "MainMenuScreen"
this.setScreen(new MainMenuScreen(this));
}

This change is needed so that we may “pass” the SpriteBatch and Font that we made into our game screen so that we can use them. Next in the MainGameScreen Class creating a placeholder to “hold” the game was passed so that we can access it, and creating a camera to view the screen with.

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    //creates a placeholder for the game we are passing (Switch AdlermacGame to name of your Game Class!)
private AdlermacGame game;

//Defining the camera to be used
private OrthographicCamera camera;

//called upon bein set as a screen (Switch AdlermacGame to name of your Game Class!)
public MainMenuScreen(AdlermacGame game){
//assigns the game we passed to the placeholder
this.game=game;
//creates the camera
camera = new OrthographicCamera();
//Sets the screen to be 800 width by 480 height
camera.setToOrtho(false,800,480);
}

Next we will be editing the Render method in the MainScreenClass for two reasons, creating a colored background and adding some text. The code is shown and explained below:

 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
//this is called every time the game is rendered
@Override
public void render(float deltaTime){
//Sets the color to be applied after clearing the screen (R,G,B,A)
Gdx.gl.glClearColor(0,0,255,1);
//Clears the screen
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

//updates camera view
camera.update();
//initiates the SpriteBatch from the main Game class
game.gameBatch.begin();
//draws the following text with the default font(our batch, text, x, y)
game.gameFont.draw(game.gameBatch, "Hey! I'm Text",200,200);

//Code snippet that displays when touched
if(Gdx.input.isTouched()){
game.gameFont.draw(game.gameBatch, "and I'm Touched!",500,200);
}

//ends the SpriteBatch
game.gameBatch.end();

//displays the deltaTime
System.out.println("deltaTime: " + deltaTime);
//shows what the Frames Per Second is
System.out.println("FPS: " + 1f/deltaTime);
}

This code should display “and im touched!” only when the screen is touched (clicked), upon running you should acheive the following results:

Thus we have now created a way to show text when you desire and also change the background color. This is the end of this tutorial, and for the double checkers or people wanting the hole file, here it is. The final look of the MainMenuScreen:

 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
package com.mygdx.adlermac;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;

public class MainMenuScreen implements Screen {

//creates a placeholder for the game we are passing (Switch AdlermacGame to name of your Game Class!)
private AdlermacGame game;

//Defining the camera to be used
private OrthographicCamera camera;

//called upon bein set as a screen (Switch AdlermacGame to name of your Game Class!)
public MainMenuScreen(AdlermacGame game){
//assigns the game we passed to the placeholder
this.game=game;
//creates the camera
camera = new OrthographicCamera();
//Sets the screen to be 800 width by 480 height
camera.setToOrtho(false,800,480);
}

//this is called every time the game is rendered
@Override
public void render(float deltaTime){
//Sets the color to be applied after clearing the screen (R,G,B,A)
Gdx.gl.glClearColor(0,0,255,1);
//Clears the screen
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

//updates camera view
camera.update();
//initiates the SpriteBatch from the main Game class
game.gameBatch.begin();
//draws the following text with the default font(our batch, text, x, y)
game.gameFont.draw(game.gameBatch, "Hey! I'm Text",200,200);

//Code snippet that displays when touched
if(Gdx.input.isTouched()){
game.gameFont.draw(game.gameBatch, "and I'm Touched!",500,200);
}

//ends the SpriteBatch
game.gameBatch.end();

//displays the deltaTime
System.out.println("deltaTime: " + deltaTime);
//shows what the Frames Per Second is
System.out.println("FPS: " + 1f/deltaTime);
}

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

}

@Override
public void show(){

}

@Override
public void hide(){

}

@Override
public void pause(){

}

@Override
public void resume(){

}

@Override
public void dispose(){

}
}

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Project Hasseu – Tutorial 2 – Programming Sentry Turrets
  • Project Hasseu – Tutorial 1 – Programming a machine gun / projectile launcher
  • Project 1 – Update 3
  • Unity – Adding text above game objects without canvas
  • Book Review – A Theory of Fun for Game Design by Raph Koster.

Recent Comments

    Archives

    • February 2021
    • January 2021
    • September 2020
    • August 2020
    • March 2020
    • February 2020
    • April 2017
    • March 2017
    • August 2015
    • July 2015
    • June 2015
    • March 2015
    • February 2015

    Categories

    • Reading
    • Uncategorized

    Meta

    • Log in
    • Entries feed
    • Comments feed
    • WordPress.org
    ©2025 The Renaissance Raven | Powered by WordPress and Superb Themes!
    Menu
    The Renaissance Raven
    • Game Development
      • Project 1 – ???
      • Unity Tutorials
    • Electronics and Programming
    • Leisure
    • Blog