Java LWJGL Slick UnicodeFont acting up - java

I'm having some problems displaying text on the screen, in the past I have just used sprite based text, however this time I want to use UnicodeFont. TrueTypeFonts draw perfectly however its deprecated.
When I try to draw the UnicodeFont it seems to be affected by the characters I use. for example If I draw the string "stackoverflow" the text and the box will draw, if I try "stackoverflowcom" the box will not draw.
A barebones version of my source code is below. On line ~74 I call uniFont.drawString(0, 0,"stackoverflow"); , if the com (or anything really) the box will not be drawn.
edit. > You can use the boolean tryUnicode to swap between true and unicode.
Ive tried sticking them in to seperate display lists but it made no difference.
Could anyone offer an insight in to why this is happening?
Thanks
import java.awt.Font;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import static org.lwjgl.opengl.GL11.*;
public class Game {
private UnicodeFont uniFont;
private TrueTypeFont truFont;
public static void main(String[] argv) {
Game game = new Game();
game.start();
}
public void start()
{
initGL(600, 600);
initFonts();
while(!Display.isCloseRequested()) //display not closed
{
render();
Display.update();
Display.sync(60);
}
Display.destroy();
System.exit(0);
}
private void render()
{
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(-0.25f,0.7f,0);
glScalef(0.001f,-0.001f,0.001f);
glEnable(GL_BLEND);
boolean tryUnicode = false;
if(tryUnicode)
{
uniFont.drawString(0, 0,"stackoverflow");
//EDIT.. glDisable texture is required here.
}else
{
glScalef(1.1f,1.1f,1f);
truFont.drawString(0, 0, "stackoverflow truFont");
}
glDisable(GL_BLEND);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.25f,0,0);
glColor3f(0.5f, 0f, 0f);
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(0, 0,0.0f);
glVertex3f(0.5f, 0,0f);
glVertex3f(0f,0.5f,0f);
glVertex3f(0.5f, 0.5f,0f);
glEnd();
glPopMatrix();
}
private void initGL(int width, int height) {
try {
Display.setDisplayMode(new DisplayMode(width,height));
Display.create();
//Display.setVSyncEnabled(true);
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
glEnable(GL11.GL_TEXTURE_2D);
glShadeModel(GL11.GL_SMOOTH);
glEnable(GL11.GL_DEPTH_TEST);
glDisable(GL11.GL_LIGHTING);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1);
glEnable(GL_BLEND);
glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_BLEND);
glMatrixMode(GL11.GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1, 1);
glMatrixMode(GL11.GL_MODELVIEW);
}
private void initFonts() {
Font awtFont = new Font("", Font.PLAIN,55);
truFont = new TrueTypeFont(awtFont, true);
uniFont = new UnicodeFont(awtFont, 128, false, false);
uniFont.addAsciiGlyphs();
uniFont.addGlyphs(400,600); // Setting the unicode Range
uniFont.getEffects().add(new ColorEffect(java.awt.Color.white));
try {
uniFont.loadGlyphs();
} catch (SlickException e) {};
}
}

It seems I have this error now, it will only draw the outlined rectangle before using the font. This seems to be a Slick error, and is a major problem so they should fix it before adding new features.
The only work-around is to render fonts last on your frame.
EDIT: Problem Solved!
add this line after you render your font:
GL11.glDisable(GL11.GL_TEXTURE_2D);
The Slick people should really add that line in as it causes so many bugs!

This is how I create fonts:
Font font = ...; //Your loaded font
float size = 20.0F;
UnicodeFont f = new UnicodeFont(font.deriveFont(0 /*normal*/, size));
f.addAsciiGlyphs();
ColorEffect e = new ColorEffect();
e.setColor(java.awt.Color.white);
f.getEffects().add(e);
try {
f.loadGlyphs();
} catch (SlickException e1) {
e1.printStackTrace();
}
return f;
Hope this helped!

Related

LWJGL: How to create two different OpenGL contexts?

GLFW.glfwMakeContextCurrent(window);
main = GLContext.createFromCurrent();
other = GLContext.createFromCurrent();
This is what I have tried so far, and in the other thread, I call
GL.setCurrent(other);
But all my OGL calls have no effect (calling genVertexArrays() returns 0).
I can't find any examples online of how to even create two different contexts let alone make one current in another thread.
Took me over 12 hours today to figure it out, but hey, it works and it's awesome. Anyways, the one thing that I lacked was ACTUAL EXAMPLES. So, for all of you who are currently in the position I was in, here you go:
import java.nio.FloatBuffer;
import nick.hacksoi.Program;
import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import org.lwjgl.opengl.GLContext;
import org.lwjgl.system.MemoryUtil;
public class MultiThreadTest
{
long mainWindow, otherWindow;
public void run()
{
if (GLFW.glfwInit() != GL11.GL_TRUE)
throw new IllegalStateException("Unable to initialize GLFW");
mainWindow = GLFW.glfwCreateWindow(1366, 768, "threading", MemoryUtil.NULL, MemoryUtil.NULL);
if (mainWindow == MemoryUtil.NULL)
throw new RuntimeException("Failed to create the GLFW window");
GLFW.glfwSetWindowPos(mainWindow, 1080 / 2 - 1366 / 4, 30);
GLFW.glfwShowWindow(mainWindow);
GLFW.glfwMakeContextCurrent(mainWindow);
GLContext.createFromCurrent();
GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GL11.GL_FALSE);
otherWindow = GLFW.glfwCreateWindow(1, 1, "", MemoryUtil.NULL, mainWindow);
if (otherWindow == MemoryUtil.NULL)
throw new RuntimeException("Failed to create the GLFW window");
Runner runner = new Runner();
Thread other = new Thread(runner);
other.start();
try
{
other.join();
} catch (InterruptedException e)
{
e.printStackTrace();
}
Program program = new Program("shaders/2d/simple.vs", "shaders/2d/simple.fs");
int vao = GL30.glGenVertexArrays();
GL30.glBindVertexArray(vao);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, runner.vbo);
GL20.glEnableVertexAttribArray(0);
GL20.glVertexAttribPointer(0, 2, GL11.GL_FLOAT, false, 0, 0);
GL30.glBindVertexArray(0);
GL20.glDisableVertexAttribArray(0);
GL11.glClearColor(0.5f, 0.5f, 1f, 1);
while (GLFW.glfwWindowShouldClose(mainWindow) != GL11.GL_TRUE)
{
GLFW.glfwPollEvents();
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
program.use();
{
GL30.glBindVertexArray(vao);
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 6);
GL30.glBindVertexArray(0);
}
program.unuse();
GLFW.glfwSwapBuffers(mainWindow);
}
}
private class Runner implements Runnable
{
public int vbo;
#Override
public void run()
{
GLFW.glfwMakeContextCurrent(otherWindow);
GLContext.createFromCurrent();
float[] vertices = new float[] { -1, -1, 0, 1, 1, -1 };
FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(6);
for (float f : vertices)
vertexBuffer.put(f);
vertexBuffer.flip();
vbo = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexBuffer, GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}
}
public static void main(String[] args)
{
new MultiThreadTest().run();
}
}
The main caveat that I didn't realize until playing around with this test code was that VertexArrayObjects are NOT shared between contexts; so, you generate them in your rendering thread.
Anyways, hope this helps someone. It would've saved me hours of pain and suffering.
You need to create another window with glfwCreateWindow (but it does not need to be visible). See the GLFW context guide.

Running LWJGL on a Raspberry Pi

I need to port something written in Java with LWJGL to a Raspberry. Im using Raspbian and tried oracles java version and some called "openjdk" or something like that.
With both versions I get this Exception:
java.lang.UnsatisfiedLinkError: org.lwjgl.opengl.GLContext.nLoadOpenGLLibrary()V
when creating the "Display".
I already searched for some solution, but they refer to OpenGLES and I did never use OpenGLES nor I found any download to use it.
I have no clue what to do and what information you might need, just comment if you need some more input.
edit:
Well you might want my current sourcecode too:
import java.io.File;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.PixelFormat;
public class Main {
private int fps;
private long lastFPS, lastTime;
Main() {
createWindow();
init2D();
loop();
}
private void init2D() {
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 500, 400, 0, -1, 1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glDisable(GL11.GL_DEPTH_TEST);
lastFPS = getTime();
getDelta();
}
private long getTime() {
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}
public void updateFPS() {
if (getTime() - lastFPS > 1000) {
Display.setTitle(Integer.toString(fps));
fps = 0;
lastFPS += 1000;
}
fps++;
}
private int getDelta() {
long time = getTime();
int delta = (int) (time - lastTime);
lastTime = time;
return delta;
}
private void loop() {
float rot = 0;
while(!Display.isCloseRequested()&&!Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glLoadIdentity();
int x = Display.getDisplayMode().getWidth()/2;
int y = Display.getDisplayMode().getHeight()/2;
GL11.glTranslated(x, y, 0);
GL11.glRotated(rot, 0, 0, 1);
if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
GL11.glColor3f(1, 0, 0);
} else {
GL11.glColor3f(1, 1, 1);
}
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2d(-50, -50);
GL11.glVertex2d(50, -50);
GL11.glVertex2d(50, 50);
GL11.glVertex2d(-50, 50);
GL11.glEnd();
rot+=0.05*getDelta();
Display.update();
updateFPS();
}
}
private void createWindow() {
try {
Display.setDisplayMode(new DisplayMode(500, 400));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
} // Window is created in this try-catch
try {
Assetloader.load();
} catch (Exception e) {
} // Load Assets
}
public static void main(String args[]) {
System.setProperty("org.lwjgl.librarypath", new File("native").getAbsolutePath());
new Main();
}
}
Raspberry Pi's only support OpenGL ES 2.0 and they're isn't any way around that.
OpenGL ES has no immediate mode rendering (glBegin, glVertex, glColor). You'll have to load your geometry into VBO's and render that way.
You'll also need to write basic shaders and pass your projection and modelview matrices in via glUniformMatrix.
Immediate mode rendering is pretty much only available on the desktop, iPhone's, Android's and Blackberry's use only OpenGL ES and pretty much any game console will use an API very similiar to OpenGL ES.
Bottom line: You'll need to ditch the immediate mode rendering and learn to use the ES spec.
These links should help...
http://www.khronos.org/registry/gles/specs/2.0/es_full_spec_2.0.25.pdf
http://www.khronos.org/files/opengles_shading_language.pdf

How to keep the image onto the screen?

import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.URL;
import javax.imageio.*;
import java.awt.Component;
public class ImageExample3 extends Applet
{
private BufferedImage img;
int counter = 0;
private BufferedImage next2;
Point lll;
public void init ()
{
resize (500, 500);
try
{
URL url = new URL (getCodeBase (), "Pacman.png");
img = ImageIO.read (url);
}
catch (IOException e)
{
}
repaint ();
}
public void paint (Graphics g)
{
if (counter == 0)
{
g.drawImage (img, 0, 0, null);
Dimension appletSize = this.getSize ();
lll = getLocationOnScreen ();
try
{
next2 = new Robot ().createScreenCapture (new Rectangle (lll.x, lll.y, 500, 500));
}
catch (java.awt.AWTException e)
{
}
g.drawImage (next2, 0, 0, null);
System.out.println (lll);
counter++;
}
}
}
What I am trying to do is to load a picture and then paint it, and then in the paint method, I used a point to tell me the location of the applet window on the screen and take a screenshot of it and then draw it on the applet. However, I want this to happen only once so I add a counter but whenever I run it, it draws the image and then gets replaced by a white box. What can I do to fix it?
The screen constantly needs to refresh. To show graphics, it paints an image every time the screen refreshes by calling the update method. The update method clears the screen by painting it white. Then, the update method calls the paint method (or repaint depending on context).
Because you only paint graphics once, your graphics are painted over by a blank screen and not refreshed. You need to paint your graphics every time that the screen refreshes, or they will be erased.
Always paint, but only take the screenshot once. Here's an example of the paint method:
public void paint (Graphics g)
{
g.drawImage (img, 0, 0, null);
Dimension appletSize = this.getSize ();
lll = getLocationOnScreen ();
if (counter == 0) //try taking the screenshot when counter is 0
{
try
{
next2 = new Robot ().createScreenCapture (new Rectangle (lll.x, lll.y, 500, 500));
}
catch (java.awt.AWTException e) {}
}
if (next2 != null) //if you took the screenshot and saved it to next2, draw it
g.drawImage (next2, 0, 0, null);
System.out.println (lll);
counter++;
}

Java LWJGL / Slick-Util texture.bind() binds the same texture everytime

I've been programming for a few years now and wanted to try Java LWJGL, and people recommended using the Slick-Util library so I set all of it up, etc. and I've been working on several projects, never encountering this problem. However, I've encountered a problem where when binding a texture to openGL, the same incorrect texture is instead bound every time. I ran into this problem first with a project with a bunch of classes etc, and chose to try to condense the problem into one jar file.
So before I show the code, I wanna explain how the example file works. Basically, there are four PNG's within my project. They are contained in res/textures/character/texturesHere. The names of the PNG's consist as: char_d.png, char_u.png, char_l.png, char_r.png. I've checked all four and all four have different textures of the character's direction, as it should be. This is to prove that the resources of the project are NOT the problem. Here is the entire problem condensed into a "Main" class file.
package main_p;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.*;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import static org.lwjgl.opengl.GL11.*;
public class Main
{
public final int srcWidth = 1280;
public final int srcHeight = 720;
public float x = 150;
public float y = 150;
public float w = 40;
public float h = 40;
public Texture char_down;
public Texture char_right;
public Texture char_left;
public Texture char_up;
public Main()
{
try
{
Display.setTitle("Escape Alpha v0.0.1");
Display.setDisplayMode(new DisplayMode(srcWidth, srcHeight));
Display.create();
}
catch (LWJGLException e)
{
e.printStackTrace();
}
initGL();
initTextures();
loop();
}
public void loop()
{
glClear(GL_COLOR_BUFFER_BIT);
while (!Display.isCloseRequested())
{
glBegin(GL_QUADS);
char_left.bind();
glTexCoord2f(0, 0);
glVertex2f(x, y);
glTexCoord2f(1, 0);
glVertex2f(x + w, y);
glTexCoord2f(1, 1);
glVertex2f(x + w, y + h);
glTexCoord2f(0, 1);
glVertex2f(x, y + h);
glEnd();
Display.update();
Display.sync(60);
}
Display.destroy();
}
public void initGL()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, srcWidth, srcHeight, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
public void initTextures()
{
char_down = loadTextureFrom("character/char_d.png");
char_up = loadTextureFrom("character/char_u.png");
char_left = loadTextureFrom("character/char_l.png");
char_right = loadTextureFrom("character/char_r.png");
}
public Texture loadTextureFrom(String path)
{
try
{
return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/textures/" + path)));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
public static void write(Object ob)
{
System.out.println(ob);
}
public static void main(String[] args)
{
new Main();
}
}
One thing I've noticed is that within the initTextures() method, if I change the LAST loaded texture, ANY texture I bind will end up rendering the last texture loaded in initTextures()
In this example I bind Texture char_left, but instead it uses char_right, because it was the last one loaded in my initTextures() method. I can prove this because if I change whichever texture is LAST loaded in my initTextures() method, it ends up rendering that one instead, again disregarding the texture I tell it to bind. Like I said, I've checked all of my PNG's multiple times, and they all look good, and show the character facing the correct direction. My loadTextureFrom(String path) method is as any other texture loader. Nothing special about it really. I actually am using this exact method in 90% of my 2D projects for Java LWJGL / Slick-Util, and I've only recently started running into this problem. I'm at a loss for words, and have never ran into this problem before. Any help is greatly appreciated. :)
Edit 1:
I just did an important test. I checked the HashCode of all four textures, AFTER loading them within the initTextures() method.
public void initTextures()
{
char_left = loadTextureFrom("character/char_l.png");
char_right = loadTextureFrom("character/char_r.png");
char_down = loadTextureFrom("character/char_d.png");
char_up = loadTextureFrom("character/char_u.png");
write(System.identityHashCode(char_left));
write(System.identityHashCode(char_right));
write(System.identityHashCode(char_down));
write(System.identityHashCode(char_up));
}
It returned this:
1364345882
1878339755
1246651385
1619367563
I think this truly proves that the initTextures() method isn't the problem. Once again, any help is appreciated! This kind of 100% holds me back from doing any more work on any projects :/ Thanks! :)
I know this is an old post but I don't want anyone who sees this to go answerless. Basically, the texture bind call has to be outsde the "glBegin". That's all the problem is. You cannot bind textures inside of the glBegin call. If you don't do this, gl will just bind the last texture loaded.
I dont know if this will actually help but you have a problem in your loop method. Basically you are not clearing you screen. You do glClear(GL_COLOR_BUFFER_BIT); before your loop. Another thing that might cause this sort of problem is that you actually have to enable and disable GL_BLEND in a loop. Also almost forgot you didn't enable glEnable(GL_TEXTURE_2D); in your initGl() method.
public void loop()
{
while (!Display.isCloseRequested())
{
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBegin(GL_QUADS);
char_left.bind();
glTexCoord2f(0, 0);
glVertex2f(x, y);
glTexCoord2f(1, 0);
glVertex2f(x + w, y);
glTexCoord2f(1, 1);
glVertex2f(x + w, y + h);
glTexCoord2f(0, 1);
glVertex2f(x, y + h);
glEnd();
glDisable(GL_BLEND);
Display.update();
Display.sync(60);
}
Display.destroy();
}
Hmm try this and see if it works. I use it in my 2d game it works there.
package game.utils;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.util.ResourceLoader;
public class TextureLoader {
private static Map<URL, Texture> texMap = new HashMap<>();
public static void bindTexture(URL tex) {
try {
Texture texture = texMap.get(tex);
if (texture == null) {
texture = loadTexture(tex);
}
texture.bind();
GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
} catch (IOException e) {
// TODO Auto-generated catch block.
e.printStackTrace();
}
}
public static void bindTexture(String tex) {
try {
bindTexture(ResourceLoader.getResource(tex));
} catch (Exception e) {
bindTexture("res/other/white.png");
}
}
private static Texture loadTexture(URL tex) throws FileNotFoundException, IOException {
System.out.printf("Loading texture %s (%s)%n", texMap.size(), tex.getFile());
Texture texture = org.newdawn.slick.opengl.TextureLoader.getTexture("PNG", tex.openStream());
texMap.put(tex, texture);
return texture;
}
}
You can change the "res/other/white.png" to whatever your missing texture file is. Also the glTexParameterf lines are for disabling mipmap/filtering/whatever it's called.

How can I display text with Slick?

I have been trying to display text with ninjacave's tutorials and lot's of others but I can't get it to work.
Could someone give me a link to a tutorial that works?
Thanks.
The code is below:
Main Class:
package oregon.client;
import oregon.src.*;
import org.lwjgl.*;
import org.lwjgl.opengl.*;
import static org.lwjgl.opengl.GL11.*;
public class Oregon {
public static Settings settings = new Settings();
public GameController controls = new GameController();
public FPSCounter fps = new FPSCounter();
public void init() {
try {
if (settings.fullscreen) {
Display.setDisplayModeAndFullscreen(Display
.getDesktopDisplayMode());
} else if (!settings.fullscreen) {
Display.setDisplayModeAndFullscreen(new DisplayMode(
settings.defaultWidth, settings.defaultHeight));
}
Display.setVSyncEnabled(true);
Display.create();
} catch (LWJGLException e) {
stop(e);
}
initOpenGL();
start();
}
public void initOpenGL() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, settings.defaultWidth, settings.defaultHeight, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
}
public void debug() {
}
public void openGL() {
}
public void start() {
while (!Display.isCloseRequested()) {
controls.keyboard();
fps.tick();
debug();
openGL();
Display.update();
Display.sync(100);
}
Display.destroy();
}
public static void stop() {
System.exit(0);
Display.destroy();
}
public static void stop(Exception e) {
e.printStackTrace();
System.exit(0);
Display.destroy();
}
public static void setFullscreen() {
try {
Display.setDisplayModeAndFullscreen(Display.getDesktopDisplayMode());
} catch (LWJGLException e) {
stop(e);
}
}
public static void removeFullscreen() {
try {
Display.setDisplayModeAndFullscreen(new DisplayMode(
settings.defaultWidth, settings.defaultHeight));
} catch (LWJGLException e) {
stop(e);
}
}
public static void main(String args[]) {
Oregon oregon = new Oregon();
oregon.init();
}
}
That was the code for the main-class.
EDIT:- This is a picture of what I'm getting with Jesse B's code.
DavidB mentions in his answer using the graphics instance to drawString(). This will use your current font. This means you should call graphics.setFont() first, otherwise it will use the default system font. This also makes it hard to remember (in various methods of your game) which is the 'current' font.
Alternatively, you can initialize a font instance and use it directly to drawString. From Slick2D Wiki...
// initialise the font
Font font = new Font("Verdana", Font.BOLD, 20);
TrueTypeFont trueTypeFont = new TrueTypeFont(font, true);
// render some text to the screen
trueTypeFont.drawString(20.0f, 20.0f, "Slick displaying True Type Fonts", Color.green);
This requires that all supported platforms can resolve the font name. A better option is to embed the font in your JAR by placing it it your resources directory.
EDIT
After seeing the picture you posted, my guess is that you don't have the font loading correctly. Try this instead...
graphics.drawString("Test drawing string.",20.0f,20.0f);
If this works, it confirms the font isn't working. If you want to use custom fonts, you will have to add your font file into your JAR and reference it as a resource.
Download the Slick2D library and import the jar.
Inside your class to cover the scope of the whole class:
Font font;
TrueTypeFont ttf;
Your init method
public void init(GameContainer gc){
font = new Font("Verdana", Font.BOLD, 20);
ttf = new TrueTypeFont(font, true);
}
Your render method:
public void render(GameContainer gc, Graphics g){
//render code here
ttf.drawString("Hello, World!")
}
According to this tutorial, you can call drawString from your graphics object in the render method.
g.drawString("Hello World!",200,200);

Categories