Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import java.io.File;
- import java.awt.image.BufferedImage;
- import org.lwjgl.Sys;
- import org.lwjgl.input.Keyboard;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.GL11;
- import org.lwjgl.opengl.DisplayMode;
- /**
- * Basic game
- *
- * @author Name <email>
- * @version 1.0
- */
- public class Main {
- /** Game title */
- public static final String GAME_TITLE = "My Game";
- /** Desired frame time */
- private static final int FRAMERATE = 60;
- /** Exit the game */
- private static boolean finished;
- /** Angle of rotating square */
- private static float angle;
- public static String IMG = "matty.jpg";
- /**
- * Application init
- * @param args Commandline args
- */
- public static void main(String[] args) {
- boolean fullscreen = (args.length == 1 && args[0].equals("-fullscreen"));
- try {
- init(fullscreen);
- run();
- } catch (Exception e) {
- e.printStackTrace(System.err);
- Sys.alert(GAME_TITLE, "An error occured and the game will exit.");
- } finally {
- cleanup();
- }
- System.exit(0);
- }
- public static void read_file(int [][] pix)
- {
- BufferedImage img;
- try{
- img = ImageIO.read(new File(IMG));
- pix = new int[img.getHeight()][img.getWidth()];
- for(int i=0;i<img.getHeight();i++)
- {
- for(int j = 0; j < img.getWidth(); j++){
- pix[i][j] = img.getRGB(j,i);
- int r,g,b;
- r=(pix[i][j] >> 16) & 0xFF;
- g = (pix[i][j] >> 8) & 0xFF;
- b = pix[i][j] & 0xFF;
- System.out.println(r+"-"+g+"-"+b);
- }
- }
- }
- catch(IOException e){
- //default error
- System.out.println(e);
- }
- }
- private static void render() {
- int[][] pix = new int[6][6];
- read_file(pix);
- GL11.glMatrixMode(GL11.GL_PROJECTION);
- GL11.glLoadIdentity();
- GL11.glOrtho(0, Display.getDisplayMode().getWidth(), 0, Display.getDisplayMode().getHeight(), -1, 1);
- GL11.glMatrixMode(GL11.GL_MODELVIEW);
- // clear the screen
- GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
- // center square according to screen size
- GL11.glPushMatrix();
- GL11.glTranslatef(Display.getDisplayMode().getWidth() / 2, Display.getDisplayMode().getHeight() / 2, 0.0f);
- GL11.glBegin(GL11.GL_QUADS);
- for(int i=0;i<5;i++)
- for(int j=0;j<5;j++)
- {
- GL11.glVertex2i(-50*i, -50*i);
- GL11.glVertex2i(50*i, -50*i);
- GL11.glVertex2i(50*i, 50*i);
- GL11.glVertex2i(-50*i, 50*i);
- System.out.println(pix[i][j]);
- }
- GL11.glEnd();
- GL11.glPopMatrix();
- }
- private static void init(boolean fullscreen) throws Exception {
- // Create a fullscreen window with 1:1 orthographic 2D projection (default)
- Display.setTitle(GAME_TITLE);
- // Enable vsync if we can (due to how OpenGL works, it cannot be guarenteed to always work)
- Display.setVSyncEnabled(true);
- Display.setDisplayMode(new DisplayMode(640,480));
- // Create default display of 640x480
- Display.create();
- }
- /**
- * Runs the game (the "main loop")
- */
- private static void run() {
- while (!finished) {
- // Always call Window.update(), all the time - it does some behind the
- // scenes work, and also displays the rendered output
- Display.update();
- // Check for close requests
- if (Display.isCloseRequested()) {
- finished = true;
- }
- // The window is in the foreground, so we should play the game
- else if (Display.isActive()) {
- logic();
- render();
- Display.sync(FRAMERATE);
- }
- // The window is not in the foreground, so we can allow other stuff to run and
- // infrequently update
- else {
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- }
- logic();
- // Only bother rendering if the window is visible or dirty
- if (Display.isVisible() || Display.isDirty()) {
- render();
- }
- }
- }
- }
- /**
- * Do any game-specific cleanup
- */
- private static void cleanup() {
- // Close the window
- Display.destroy();
- }
- /**
- * Do all calculations, handle input, etc.
- */
- private static void logic() {
- // Example input handler: we'll check for the ESC key and finish the game instantly when it's pressed
- if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
- finished = true;
- }
- // Rotate the square
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement