⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 renderer.java

📁 NeHe用java与OpenGL结合教程源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package demos.nehe.lesson21;

import com.sun.opengl.util.BufferUtil;
import demos.common.ResourceRetriever;
import demos.common.TextureReader;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Random;

class Renderer implements GLEventListener {
    private Random random = new Random();
    private boolean[][] vline = new boolean[11][11];										// Keeps Track Of Verticle Lines
    private boolean[][] hline = new boolean[11][11];										// Keeps Track Of Horizontal Lines
    private boolean filled;												// Done Filling In The Grid?
    private boolean gameover;											// Is The Game Over?
    private boolean anti = true;											// Antialiasing?

    private float delay;												// Enemy Delay
    private int adjust = 3;											// Speed Adjustment For Really Slow Video Cards
    private int lives = 5;											// Player Lives
    private int level = 1;											// Internal Game Level
    private int level2 = level;										// Displayed Game Level
    private int stage = 1;											// Game Stage

    private GameObject player = new GameObject();										// Player Information
    private GameObject[] enemy = new GameObject[9];									// Enemy Information
    private GameObject hourglass = new GameObject();									// Hourglass Information

    private int steps[] = {1, 2, 4, 5, 10, 20};					// Stepping Values For Slow Video Adjustment

    private int textures[] = new int[2];											// Font Texture Storage Space
    private int base;
    private boolean resetGame = false;
    private boolean moveRight = false;
    private boolean moveLeft = false;
    private boolean moveDown = false;
    private boolean moveUp = false;
    private AudioSample dieSample;
    private AudioSample hourglassSample;
    private AudioSample freezeSample;
    private AudioSample completerSample;

    private ByteBuffer stringBuffer = BufferUtil.newByteBuffer(256);

    private long lastUpdateTime;

    public void moveUp(boolean move) {
        moveUp = move;
    }

    public void moveDown(boolean move) {
        moveDown = move;
    }

    public void moveLeft(boolean move) {
        moveLeft = move;
    }

    public void moveRight(boolean move) {
        moveRight = move;
    }

    public void resetGame() {
        resetGame = true;
    }

    private void resetObjects() {										// Reset Player And Enemies
        player.x = 0;												// Reset Player X Position To Far Left Of The Screen
        player.y = 0;												// Reset Player Y Position To The Top Of The Screen
        player.fx = 0;											// Set Fine X Position To Match
        player.fy = 0;											// Set Fine Y Position To Match

        for (int i = 0; i < (stage * level); i++) {				// Loop Through All The Enemies
            enemy[i] = new GameObject();
            enemy[i].x = 5 + (int) (Math.random() * 6);							// Select A Random X Position
            enemy[i].y = (int) Math.random() * 11;							// Select A Random Y Position
            enemy[i].fx = enemy[i].x * 60;					// Set Fine X To Match
            enemy[i].fy = enemy[i].y * 40;					// Set Fine Y To Match
        }
    }

    private void loadGLTextures(GL gl) throws IOException {
        String tileNames [] = {"demos/data/images/font.png", "demos/data/images/Image.png"};

        gl.glGenTextures(2, textures, 0);

        for (int i = 0; i < 2; i++) {
            TextureReader.Texture texture = TextureReader.readTexture(tileNames[i]);
            //Create Nearest Filtered Texture
            gl.glBindTexture(GL.GL_TEXTURE_2D, textures[i]);

            gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
            gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);

            gl.glTexImage2D(GL.GL_TEXTURE_2D,
                    0,
                    3,
                    texture.getWidth(),
                    texture.getHeight(),
                    0,
                    GL.GL_RGB,
                    GL.GL_UNSIGNED_BYTE,
                    texture.getPixels());
        }
    }

    void buildFont(GL gl) {									// Build Our Font Display List
        base = gl.glGenLists(256);									// Creating 256 Display Lists
        for (int i = 0; i < 256; i++)						// Loop Through All 256 Lists
        {
            float cx = (float) (i % 16) / 16.0f;						// X Position Of Current Character
            float cy = (float) (i / 16) / 16.0f;						// Y Position Of Current Character

            gl.glNewList(base + i, GL.GL_COMPILE);					// Start Building A List
            gl.glBegin(GL.GL_QUADS);								// Use A Quad For Each Character
            gl.glTexCoord2f(cx, 1.0f - cy - 0.0625f);			// Texture Coord (Bottom Left)
            gl.glVertex2d(0, 16);							// Vertex Coord (Bottom Left)
            gl.glTexCoord2f(cx + 0.0625f, 1.0f - cy - 0.0625f);	// Texture Coord (Bottom Right)
            gl.glVertex2i(16, 16);							// Vertex Coord (Bottom Right)
            gl.glTexCoord2f(cx + 0.0625f, 1.0f - cy);			// Texture Coord (Top Right)
            gl.glVertex2i(16, 0);							// Vertex Coord (Top Right)
            gl.glTexCoord2f(cx, 1.0f - cy);					// Texture Coord (Top Left)
            gl.glVertex2i(0, 0);							// Vertex Coord (Top Left)
            gl.glEnd();										// Done Building Our Quad (Character)
            gl.glTranslated(15, 0, 0);							// Move To The Right Of The Character
            gl.glEndList();										// Done Building The Display List
        }														// Loop Until All 256 Are Built
    }

    private void glPrint(GL gl, int x, int y, int set, String message)	// Where The Printing Happens
    {
        if (set > 1) {												// Did User Choose An Invalid Character Set?
            set = 1;												// If So, Select Set 1 (Italic)
        }
        gl.glEnable(GL.GL_TEXTURE_2D);								// Enable Texture Mapping
        gl.glLoadIdentity();										// Reset The Modelview Matrix
        gl.glTranslated(x, y, 0);									// Position The Text (0,0 - Bottom Left)
        gl.glListBase(base - 32 + (128 * set));							// Choose The Font Set (0 or 1)

        if (set == 0) {												// If Set 0 Is Being Used Enlarge Font
            gl.glScalef(1.5f, 2.0f, 1.0f);							// Enlarge Font Width And Height
        }

        if (stringBuffer.capacity() < message.length()) {
            stringBuffer = BufferUtil.newByteBuffer(message.length());
        }

        stringBuffer.clear();
        stringBuffer.put(message.getBytes());
        stringBuffer.flip();
        gl.glCallLists(message.length(), GL.GL_UNSIGNED_BYTE, stringBuffer);		// Write The Text To The Screen
        gl.glDisable(GL.GL_TEXTURE_2D);								// Disable Texture Mapping
    }

    public void init(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();

        for (int i = 0; i < 9; i++)
            enemy[i] = new GameObject();

        try {
            loadGLTextures(gl);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        buildFont(gl);

        gl.glShadeModel(GL.GL_SMOOTH);                            //Enables Smooth Color Shading
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);               //This Will Clear The Background Color To Black
        gl.glClearDepth(1.0);                                  //Enables Clearing Of The Depth Buffer
        gl.glEnable(GL.GL_DEPTH_TEST);
        gl.glDepthFunc(GL.GL_LEQUAL);                             //The Type Of Depth Test To Do
        gl.glHint(GL.GL_LINE_SMOOTH_HINT, GL.GL_NICEST);  // Really Nice Perspective Calculations
        gl.glEnable(GL.GL_BLEND);										// Enable Blending
        gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);		// Type Of Blending To Use
        gl.glEnable(GL.GL_TEXTURE_2D);							// Enable 2D Texture Mapping
        resetObjects();							// Reset Player / Enemy Positions

        try {
            dieSample = new AudioSample(ResourceRetriever.getResourceAsStream("demos/data/samples/die.wav"));
            completerSample = new AudioSample(ResourceRetriever.getResourceAsStream("demos/data/samples/complete.wav"));
            freezeSample = new AudioSample(ResourceRetriever.getResourceAsStream("demos/data/samples/freeze.wav"));
            hourglassSample = new AudioSample(ResourceRetriever.getResourceAsStream("demos/data/samples/hourglass.wav"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void update() {
        if (!gameover)							// If Game Isn't Over And Programs Active Move Objects
        {
            for (int loop1 = 0; loop1 < (stage * level); loop1++)		// Loop Through The Different Stages
            {
                if ((enemy[loop1].x < player.x) && (enemy[loop1].fy == enemy[loop1].y * 40)) {
                    enemy[loop1].x++;						// Move The Enemy Right

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -