I Solved it no need to worry about this. But if you want to know what I did, I just made a nooby mistake and forgot to set it as public static void :cranky:
If it is then maybe you had problem in calling the method.
I dont think it was that, I think its that I just needed to set both of my methods (the caller and the called) to static because after I did that it was fixed
I undertstand if you need the called method to be static
MyClass.staticMethod()
but static caller method?
On the off-chance you might be using the static keyword unnecessarily (which might quickly snowball into making everything static - a big no no), since mostly only the 'public static void main()" method is static. Here’s a quick rundown on how you use a class and its methods typically.
To use a class and its methods, you first need to create a new instance of the class with the new keyword. This is usually done in the ‘public static void main’ method - the method that starts off the application. Ie, create a new instance of the class and save it inside a variable for later use.
For example:
public class Dog {
// public method
public void bark() {
System.out.println("Woof!");
}
// Start the application
public static void main(String[] argv) {
Dog dog;
dog = new Dog();
// Use dog's public method
dog.bark();
}
}
thanks, but what if I want to call an external class. And when do u use static ???
What do you mean by an external class?
To use external classes/libraries in your program you usually have to actually include these files in your program. Ie, copy pasting them into your project folder.
Classes in the same package can see and talk to each other by default. If you declare the class “public” it can be used by anyone.
You can sort classes into different packages ( basically just regular folders ) and in order to use classes inside these packages/folders you need to specify the package name before the class.
Consider we have two packages, one called “main” and the other “somepackage”. Both packages have a class named “Dog”.
To specify wich Dog class you’re using you need to write either “main.Dog dog = new main.Dog()” or “somepackage.Dog dog = new somepackage.Dog()”;
Or you can use the import java keyword to tell the compiler to include the public classes from a specific package. Ex:
import somepackage.*; // import all public classes from package
...
Dog dog = new Dog(); // compiler now knows to look for the Dog class in somepackage package.
Specifying the classpaths also lets you use “external classes”.
I really recommend for you to read the java oracle beginners tutorials. They explain very well the gist of what java is about.
Static method is usually used on singleton class. You don’t want to instance it because it is used by other so many classes. For example a class that holds boring functions but stateless such as lang.Math.
Argh, did you edit the original post? Now we’ve got an “orphaned” thread where nobody knows what was originally being talked about, left over in the archives for all time.
Cas
@princec
nevermind, they all started responding after he removed his original post :
@deadteck
you are on the wrong track (on the wood way, haha) - read up on the difference between Classes and Instances…
(hint: a Class is more of a concept or blueprint, while an instance is an Object made with the help of a Class - you usually won’t interact with Classes other than using them in conjunction with new - public static void main() being a historic and imho confusing exception…)
I’m the devil who caused all of this
Ok, i think I understand what you are all getting to but im still getting an error about different statics and stuff
Render class
package com.game.Fearless;
import static org.lwjgl.opengl.GL11.GL_ALPHA_TEST;
import static org.lwjgl.opengl.GL11.GL_BACK;
import static org.lwjgl.opengl.GL11.GL_BLEND;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_COMPILE;
import static org.lwjgl.opengl.GL11.GL_CULL_FACE;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST;
import static org.lwjgl.opengl.GL11.GL_FOG;
import static org.lwjgl.opengl.GL11.GL_FOG_COLOR;
import static org.lwjgl.opengl.GL11.GL_FOG_DENSITY;
import static org.lwjgl.opengl.GL11.GL_FOG_END;
import static org.lwjgl.opengl.GL11.GL_FOG_HINT;
import static org.lwjgl.opengl.GL11.GL_FOG_MODE;
import static org.lwjgl.opengl.GL11.GL_FOG_START;
import static org.lwjgl.opengl.GL11.GL_LINEAR;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_NEAREST;
import static org.lwjgl.opengl.GL11.GL_NICEST;
import static org.lwjgl.opengl.GL11.GL_ONE_MINUS_SRC_ALPHA;
import static org.lwjgl.opengl.GL11.GL_PERSPECTIVE_CORRECTION_HINT;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.GL_RGBA;
import static org.lwjgl.opengl.GL11.GL_SRC_ALPHA;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_MAG_FILTER;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_MIN_FILTER;
import static org.lwjgl.opengl.GL11.GL_TRIANGLES;
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_BYTE;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glBindTexture;
import static org.lwjgl.opengl.GL11.glBlendFunc;
import static org.lwjgl.opengl.GL11.glCallList;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glClearColor;
import static org.lwjgl.opengl.GL11.glColor4f;
import static org.lwjgl.opengl.GL11.glCullFace;
import static org.lwjgl.opengl.GL11.glDeleteTextures;
import static org.lwjgl.opengl.GL11.glDisable;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glEndList;
import static org.lwjgl.opengl.GL11.glFog;
import static org.lwjgl.opengl.GL11.glFogf;
import static org.lwjgl.opengl.GL11.glFogi;
import static org.lwjgl.opengl.GL11.glGenLists;
import static org.lwjgl.opengl.GL11.glGenTextures;
import static org.lwjgl.opengl.GL11.glHint;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glNewList;
import static org.lwjgl.opengl.GL11.glRotatef;
import static org.lwjgl.opengl.GL11.glTexCoord2f;
import static org.lwjgl.opengl.GL11.glTexImage2D;
import static org.lwjgl.opengl.GL11.glTexParameteri;
import static org.lwjgl.opengl.GL11.glTranslatef;
import static org.lwjgl.opengl.GL11.glVertex3d;
import static org.lwjgl.opengl.GL11.glVertex3f;
import static org.lwjgl.opengl.GL11.glViewport;
import static org.lwjgl.util.glu.GLU.gluPerspective;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.Display;
import de.matthiasmann.twl.utils.PNGDecoder;
import de.matthiasmann.twl.utils.PNGDecoder.Format;
public class Render extends Fearless {
static float rotationx = Input.rotation.x;
static float rotationy = Input.rotation.y;
static float rotationz = Input.rotation.z;
static float positionx = Input.position.x;
static float positiony = Input.position.y;
static float positionz = Input.position.z;
public void render() {
int endTexture = glGenTextures();
{
InputStream in = null;
try {
in = new FileInputStream("bin/images/floor.png");
PNGDecoder decoder = new PNGDecoder(in);
ByteBuffer buffer = BufferUtils.createByteBuffer(4
* decoder.getWidth() * decoder.getHeight());
decoder.decode(buffer, decoder.getWidth() * 4, Format.RGBA);
buffer.flip();
in.close();
glBindTexture(GL_TEXTURE_2D, endTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(),
decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE,
buffer);
glBindTexture(GL_TEXTURE_2D, 0);
} catch (FileNotFoundException ex) {
System.err.println("Failed to find the texture files.");
} catch (IOException ex) {
System.err.println("Failed to load the texture files.");
}
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov,
(float) Display.getWidth() / (float) Display.getHeight(),
zNear, zFar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glEnable(GL_ALPHA_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_FOG);
{
FloatBuffer fogColours = BufferUtils.createFloatBuffer(4);
fogColours.put(new float[] { fogColor.r, fogColor.g, fogColor.b,
fogColor.a });
glClearColor(fogColor.r, fogColor.g, fogColor.b, fogColor.a);
fogColours.flip();
glFog(GL_FOG_COLOR, fogColours);
glFogi(GL_FOG_MODE, GL_LINEAR);
glHint(GL_FOG_HINT, GL_NICEST);
glFogf(GL_FOG_START, fogNear);
glFogf(GL_FOG_END, fogFar);
glFogf(GL_FOG_DENSITY, 0.005f);
}
int ceilingDisplayList = glGenLists(1);
glNewList(ceilingDisplayList, GL_COMPILE);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3f(-gridSize, ceilingHeight, -gridSize);
glTexCoord2f(gridSize * 10 * tileSize, 0);
glVertex3f(gridSize, ceilingHeight, -gridSize);
glTexCoord2f(gridSize * 10 * tileSize, gridSize * 10 * tileSize);
glVertex3f(gridSize, ceilingHeight, gridSize);
glTexCoord2f(0, gridSize * 10 * tileSize);
glVertex3f(-gridSize, ceilingHeight, gridSize);
glEnd();
glEndList();
int wallDisplayList = glGenLists(1);
glNewList(wallDisplayList, GL_COMPILE);
glBegin(GL_QUADS);
glEnd();
glEndList();
int floorDisplayList = glGenLists(1);
glNewList(floorDisplayList, GL_COMPILE);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3f(-gridSize, floorHeight, -gridSize);
glTexCoord2f(0, gridSize * 10 * tileSize);
glVertex3f(-gridSize, floorHeight, gridSize);
glTexCoord2f(gridSize * 10 * tileSize, gridSize * 10 * tileSize);
glVertex3f(gridSize, floorHeight, gridSize);
glTexCoord2f(gridSize * 10 * tileSize, 0);
glVertex3f(gridSize, floorHeight, -gridSize);
glEnd();
glEndList();
int objectDisplayList = glGenLists(1);
glNewList(objectDisplayList, GL_COMPILE);
{
double topPoint = 0.75;
glBegin(GL_TRIANGLES);
glColor4f(1, 1, 0, 1f);
glVertex3d(0, topPoint, -5);
glColor4f(0, 0, 1, 1f);
glVertex3d(-1, -0.75, -4);
glColor4f(0, 0, 1, 1f);
glVertex3d(1, -.75, -4);
glColor4f(1, 1, 0, 1f);
glVertex3d(0, topPoint, -5);
glColor4f(0, 0, 1, 1f);
glVertex3d(1, -0.75, -4);
glColor4f(0, 0, 1, 1f);
glVertex3d(1, -0.75, -6);
glColor4f(1, 1, 0, 1f);
glVertex3d(0, topPoint, -5);
glColor4f(0, 0, 1, 1f);
glVertex3d(1, -0.75, -6);
glColor4f(0, 0, 1, 1f);
glVertex3d(-1, -.75, -6);
glColor4f(1, 1, 0, 1f);
glVertex3d(0, topPoint, -5);
glColor4f(0, 0, 1, 1f);
glVertex3d(-1, -0.75, -6);
glColor4f(0, 0, 1, 1f);
glVertex3d(-1, -.75, -4);
glEnd();
glColor4f(1, 1, 1, 1);
}
glEndList();
getDelta();
lastFPS = getTime();
while (running) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
int delta = getDelta();
glBindTexture(GL_TEXTURE_2D, endTexture);
glEnable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glCallList(floorDisplayList);
glCallList(ceilingDisplayList);
glCallList(wallDisplayList);
glEnable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glBindTexture(GL_TEXTURE_2D, 0);
glCallList(objectDisplayList);
glLoadIdentity();
glRotatef(rotationx, 1, 0, 0);
glRotatef(rotationy, 0, 1, 0);
glRotatef(rotationz, 0, 0, 1);
glTranslatef(positionx, positiony, positionz);
}
if (resizable) {
if (Display.wasResized()) {
glViewport(0, 0, Display.getWidth(), Display.getHeight());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, (float) Display.getWidth()
/ (float) Display.getHeight(), zNear, zFar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
}
if (printFPS) {
updateFPS();
}
Display.update();
if (vsync) {
Display.sync(60);
}
if (Display.isCloseRequested()) {
running = false;
}
while (!running) {
glDeleteTextures(endTexture);
// glDeleteLists(floorDisplayList, 1);
// glDeleteLists(ceilingDisplayList, 1);
// glDeleteLists(wallDisplayList, 1);
// glDeleteLists(objectDisplayList, 1);
Display.destroy();
System.exit(0);
}
}
}
Main class
package com.game.Fearless;
import com.game.Fearless.Render;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GLContext;
import org.lwjgl.util.vector.Vector3f;
import org.newdawn.slick.Color;
public class Fearless {
public static final boolean resizable = false;
public static volatile boolean running = true;
public static Vector3f position = new Vector3f(0, 0, 0);
public static Vector3f rotation = new Vector3f(0, 0, 0);
public static float zNear = 0.00001f;
public static final int gridSize = 100;
public static final float tileSize = 0.4f;
public static float zFar = 100000f;
public static float fogNear = 120f;
public static float fogFar = 100f;
public static Color fogColor = new Color(0f, 0f, 0f, 1f);
public static final boolean fullscreen = false;
public static int walkingSpeed = 105;
public static int mouseSpeed = 2;
public static final boolean vsync = true;
public static boolean printFPS = false;
public static final int maxLookUp = 85;
public static final int maxLookDown = -85;
public static final float ceilingHeight = 200;
public static final float floorHeight = -1;
public static int fov = 100;
private static int fps;
public static long lastFPS;
public static long lastFrame;
protected static int getDelta() {
long currentTime = getTime();
lastFrame = getTime();
int delta = (int) (currentTime - lastFrame);
return delta;
}
public static long getTime() {
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}
public static void updateFPS() {
if (getTime() - lastFPS > 1000) {
if (printFPS) {
System.out.println("FPS: " + fps);
}
fps = 0;
lastFPS += 1000;
}
fps++;
}
public void render() {
Display.update();
if (!Display.isCloseRequested()) {
gameLoop();
}
Render.render();
}
private void gameLoop() {
Fearless fearless;
fearless = new Fearless();
fearless.render();
}
public static void main(String[] args) {
Fearless fearless;
fearless = new Fearless();
fearless.render();
try {
if (fullscreen) {
Display.setDisplayModeAndFullscreen(Display
.getDesktopDisplayMode());
} else {
Display.setResizable(resizable);
Display.setDisplayMode(new DisplayMode(800, 600));
}
Display.setTitle("Fearless Pre-Alpha 0.01");
Display.setVSyncEnabled(vsync);
Display.create();
} catch (LWJGLException ex) {
System.err.println("Display initialization failed.");
System.exit(1);
}
if (fullscreen) {
Mouse.setGrabbed(true);
} else {
Mouse.setGrabbed(false);
}
if (!GLContext.getCapabilities().OpenGL11) {
System.err
.println("Your OpenGL version doesn't support the required functionality.");
Display.destroy();
System.exit(1);
}
if (Display.isCloseRequested()) {
Display.destroy();
System.exit(1);
}
}
}
After painfully read your code, I found that you call static methods getDelta, getTime and updateFPS of super-class from child-class. NO. Just make it “protected void blah()”. You make it static when you want to call Fearless.getTime(). In inheritance, you also make instance of super-class.
Also there’re duplicates code on your main and gameLoop
So i should make getTime, getDelta and updateFPS protected void … , and what about the error I get about the static problems, or do i also put the Fearless.render and Render.render method to protected ???
Depends on your need. Protected only provides access for child-class and itself. Read more about these access type. My google-fu shows
link1
link2
Thanks a lot ;D
you know you can do
import static org.lwjgl.opengl.GL11.*
yea, I shouldve done that