LWJGL Rendert nicht

gerredtor

Mitglied
Hallo leute, hier ein teil meines codes der folgendes machen soll, er lädt das image in den player und rendert diesen, macht er aber nicht:

Main.java
Code:
import de.gerredtor.Entity.Entity;
import de.gerredtor.Entity.Player;
import de.gerredtor.game.Engine.Frame;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

public class Main extends Game{

    /**
     * Start the example
     */
    public void start() {
        initGL(800,600);
                Player p = new Player();
               
                p.x = 0;
                p.y = 0;
                p.width = 100;
                p.height = 100;
               
                Game.entitys.add(p);
               
        while (true) {

                        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
                       
                        this.update();
                        this.render(new Frame());
                                                
            Display.update();
            Display.sync(100);
                       
            if (Display.isCloseRequested()) {
                Display.destroy();
                System.exit(0);
            }
        }
    }
    /**
     * Initialise the GL display
     *
     * @param width The width of the display
     * @param height The height of the display
     */
    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);
        }
        GL11.glEnable(GL11.GL_TEXTURE_2D);              
               
        GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);         
            // enable alpha blending
            GL11.glEnable(GL11.GL_BLEND);
            GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
            GL11.glViewport(0,0,width,height);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, width, height, 0, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
               
              
    }
    /**
     * Main Class
     */
    public static void main(String[] args) {
        Main main = new Main();
        main.start();
    }
}

Game.java

Code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package de.gerredtor.game;

import de.gerredtor.Entity.Entity;
import de.gerredtor.Entity.Player;
import de.gerredtor.game.Engine.Frame;
import de.gerredtor.game.Engine.GameObject;
import java.util.ArrayList;

/**
*
* @author Christoph
*/
public class Game extends GameObject{

    public static ArrayList<Entity> entitys = new ArrayList<Entity>();
   
    @Override
    public void update() {
        for(Entity e : Game.entitys)
        {
            e.update();
        }
    }

    @Override
    public void render(Frame f) {
        for(Entity e : Game.entitys)
        {
            if(e instanceof Player)
            {
                Player p = (Player) e;
                         System.out.println(p.x + " " + p.y);
            e.draw(f);  
            }
        }
    }
   
}

Frame.java
Code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package de.gerredtor.game.Engine;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;

/**
*
* @author Christoph
*/
public class Frame {
   
    public Frame()
    {
       
    }
   
    public void drawString()
    {
       
    }
   
    public void drawImage(FrameImage fi,int x,int y)
    {
        Color.white.bind();
        fi.getTexture().bind();
    GL11.glBegin(GL11.GL_QUADS);
        GL11.glTexCoord2f(0,0);
        GL11.glVertex2f(x,y);
        GL11.glTexCoord2f(1,0);
        GL11.glVertex2f(x+fi.width,y);
        GL11.glTexCoord2f(1,1);
        GL11.glVertex2f(x+fi.width,y+fi.height);
        GL11.glTexCoord2f(0,1);
        GL11.glVertex2f(x,y+fi.height);
    GL11.glEnd();
    }
   
    public void drawLine()
    {

       
        GL11.glBegin(GL11.GL_LINES);
            GL11.glVertex2f(0, 0);
            GL11.glVertex2f(100, 100);
        GL11.glEnd();
    }
   
}

Entity.java

Code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package de.gerredtor.Entity;

import de.gerredtor.game.Engine.Frame;

/**
*
* @author Christoph
*/
public abstract class Entity {
       
    private String id;
   
    public Entity(String ID)
    {
        this.id = ID;
    }
   
    public abstract void draw(Frame f);

    public abstract void update();
   
}

Player.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package de.gerredtor.Entity;

import de.gerredtor.game.Engine.Frame;
import de.gerredtor.game.Engine.FrameImage;
import org.lwjgl.input.Keyboard;

/**
*
* @author Christoph
*/
public class Player extends Entity{
   
    private FrameImage fi;
    public int x;
    public int y;
    public int height;
    public int width;
   
    public Player() {
        super("Player");
       
        fi = new FrameImage("src/res/Downloads.png", width, height);
    }

    @Override
    public void draw(Frame f) {
        f.drawImage(fi, x, y);
    }

    @Override
    public void update() {
        if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
        this.y -= 1;
    }
       
        if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
        this.y += 1;
    }
       
        if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
        this.x -= 1;
    }
       
        if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
        this.x += 1;
    }
    }
   
   
   
   

}
 
Die Display.sync Methode sollte in jedem Frame ausgeführt werden. (ist nicht fürs Rendern wichtig aber für Konstante FPS)

Ausserdem solltest du sicherstellen, das die Reihenfolge der Funktionsaufrufe in der initGL Methode richtig ist.
 
Die Display.sync Methode sollte in jedem Frame ausgeführt werden. (ist nicht fürs Rendern wichtig aber für Konstante FPS)

Display.sync() klingt stark nach LWJGL 2. Baut euer Spiel lieber gleich auf LWJGL 3 auf!

Ich hatte auch mal angefangen, eine kleine 2D Game Engine zu schreiben, vllt. interessiert euch ja der Code davon:
https://jukusoft.com/2016/12/10/java-2d-game-engine/
https://jukusoft.com/2016/12/10/java-2d-game-engine/
Und hier hatte ich einmal ein kleines LWJGL 3 Tutorial begonnen, welches die Fenster & Context Erstellung, sowie die Initialisierung von LWJGL 3 erläutert:
https://jukusoft.com/2016/11/25/tutorial-serie-lwjgl-3-glfw-mit-opengl/
 

Zurück
Oben