-
Notifications
You must be signed in to change notification settings - Fork 16
Basic Window Example
The standard Raylib Examples are provided in the Raylib-J Examples github repo. For this I'll be breaking down the example WindowTest.java and explaining as we go.
The following is the code I'll be referencing:
package core;
import com.creedvi.raylib.java.rlj.Raylib;
import com.creedvi.raylib.java.rlj.core.Color;
//Header comment omitted for brevity
public class WindowTest{
public static void main(String[] args) {
final int SCREEN_WIDTH = 800;
final int SCREEN_HEIGHT = 450;
Raylib rlj = new Raylib();
rlj.core.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Raylib-J [core] example -- basic window");
rlj.core.SetTargetFPS(60);
while(!rlj.core.WindowShouldClose()){
rlj.core.BeginDrawing();
rlj.core.ClearBackground(Color.RAYWHITE);
rlj.text.DrawText("Congrats! You created your first window!", 190, 200, 20, Color.LIGHTGRAY);
rlj.core.EndDrawing();
}
}
}
First we have our package, imports, class, and main method:
package core;
import com.creedvi.raylib.java.rlj.Raylib;
import com.creedvi.raylib.java.rlj.core.Color;
public class WindowTest{
public static void main(String[] args) {
After the main method we create two final int
s that store the width and height of our window:
final int SCREEN_WIDTH = 800;
final int SCREEN_HEIGHT = 450;
From there we get into the initialization code:
Raylib rlj = new Raylib();
rlj.core.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Raylib-J [core] example -- basic window");
rlj.core.SetTargetFPS(60);
The first line of this block creates a new instance of a Raylib object and by passing no arguments to the constructor sets everything to the default state. I personally always use my preferred shorthand "rlj" as the object name.
Next, we Initialize our window by accessing the InitWindow(int width, int height, String windowTitle)
method within the Core
module.
Lastly we set our Frames Per Second to 60FPS using the SetTargetFPS(int numberOfFrames)
method within the Core
module.
NOTE: Above is the preferred way to initialize Raylib-J if you want to conduct window flag or configuration changes. If the above mentioned doesn't apply the overload constructor
Raylib(int screenWidth, int screenHeight, String windowTitle)
can be used.
Next we begin our main game loop:
while(!rlj.core.WindowShouldClose()){
The Core
function WindowShouldClose()
returns true
if the program is going to exit via the exit key, so while the return is false we loop through the main game loop.
Normally the game loop is split into two halves: the update half and the render half. This example does not have any entities or variables that need updating so there is no update code to go through.
So we render everything to our screen:
rlj.core.BeginDrawing();
rlj.core.ClearBackground(Color.RAYWHITE);
rlj.text.DrawText("Congrats! You created your first window!", 190, 200, 20, Color.LIGHTGRAY);
rlj.core.EndDrawing();
The first thing to note is that you must call BeginDrawing()
at the beginning of your rendering section.
Next we clear the background of the window by calling ClearBackground(Color color)
in the Core
module.
Then we render our text to the screen by calling DrawText(String text, int positionX, int positionY, int fontSize, Color fontColor)
from the Text
module.
Lastly, we call the EndDrawing()
method from the Core
module to finish our rendering loop.
NOTE:
ClearBackgroundColor(Color color)
andDrawText(String text, int positionX, int positionY, int fontSize, Color fontColor)
take a Raylib color object as a parameter. You can define a custom Color by calling the constructorColor(int r, int g, int b, int a)
or use a predefined color.
Finally, we close out our loop, our main method, and our class:
}
}
}
And there you have it! Your first Raylib-J window!