diff --git a/src/MAndEngine/AppHelper.java b/src/MAndEngine/AppHelper.java index 23f2493..1f17322 100644 --- a/src/MAndEngine/AppHelper.java +++ b/src/MAndEngine/AppHelper.java @@ -7,8 +7,10 @@ import java.util.ArrayList; import java.util.Scanner; /** - * also a bit of an app helper class thing - * well... i renamed it so, now it just is an app helper class thing. + * this class is a jack of all trades when it comes to information about apps. + * it can tell you things about apps and as well, initialize a list of them + * so that you can later use them in your main menu app! + * * @author Marcus * */ diff --git a/src/MAndEngine/BasicApp.java b/src/MAndEngine/BasicApp.java index 9f2b09e..55384c4 100644 --- a/src/MAndEngine/BasicApp.java +++ b/src/MAndEngine/BasicApp.java @@ -72,12 +72,6 @@ public abstract interface BasicApp { */ public abstract Color getColor(); - /** - * the framerate that this should be run at. - * @return - */ - public abstract int getFramerate(); - /** * should this window be resizable * @return @@ -97,5 +91,6 @@ public abstract interface BasicApp { public abstract void click(); + public abstract void updateDimensions(int width, int height); } \ No newline at end of file diff --git a/src/MAndEngine/Engine.java b/src/MAndEngine/Engine.java index a5e1f55..1c78539 100644 --- a/src/MAndEngine/Engine.java +++ b/src/MAndEngine/Engine.java @@ -21,6 +21,13 @@ import javax.swing.*; public class Engine extends Canvas implements KeyListener, MouseMotionListener, MouseListener, ContainerListener, ComponentListener { + /** + * + */ + private static double tickTime = 0; + public static double timeScale = 1; + public static double deltaTime = 0; + /** * to track the x and y */ @@ -42,19 +49,11 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener, */ private static AppHelper appInitializer; - /** - * AER WE SUPER SPEEDING?!?!? - */ - private static boolean overclock = false; - - /** - * current framerate and time required to sleep to achieve that framerate. - */ - private static int frameSync = 50, sleepTime = 1000 / frameSync; - /** * variables to track the fps, DON'T WORRY ABOUT IT, PAST YOU HAS YOU * COVERED. + * present me here, im trusting you. and since i just implemented a way to have + * like 100k fps, thanks man for doing this right. */ private static int framesInCurrentSecond = 0, FPS = 0; @@ -82,6 +81,7 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener, * IN CFG. */ private static int WIDTH = 800, HEIGHT = 600, app = 0; + public static int BUFFER_WIDTH = 800, BUFFER_HEIGHT = 600; /** * this bit is important. its the array of apps that we reference later on. @@ -128,23 +128,32 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener, /** * the main Main object but staticed so we can like use it from that static - * context. its not final because its static but don't change it. + * context. its not final because its static but don't change it.
+ * public until i figure out how to work with the changing resolution. */ - private static Engine staticMain; + public static Engine staticMain; /** * debug level. */ - public static int debug = 0; + public static int debug = 2; + /** + * because retina support + */ + private static boolean retina; + /** * SRSLY CALL DYS ONCE. DAS IT. ALL YOU GET. ONE SHOT. because this is a * static engine, yeah + * @param retina */ - public Engine(String[] classes, boolean showLoading) { + public Engine(String[] classes, boolean showLoading, boolean retina) { // frame.setVisible(true); + this.retina = retina; + // set static object staticMain = this; @@ -164,7 +173,7 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener, frame.addContainerListener(this); frame.addComponentListener(this); - requestFocus(); + //requestFocus(); if (showLoading) frame.setVisible(true); @@ -191,7 +200,7 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener, switchApps(0); - createBuffer(); + } @@ -204,6 +213,8 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener, frame.setVisible(true); + long nanos = System.nanoTime(); + // now we do stuff. while (running) { // FPS STUFF WORRY NOT, ITS ALL GOOD. MOVE ALONG. @@ -215,29 +226,21 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener, } framesInCurrentSecond++; - // tick stuff - tick(); - // paint the same stuff repaint(); - - // FRAMERATE OVERCLOCKING AND SUCH, MOVE ALONG. - try { - if (!overclock) - Thread.sleep((long) Math.floor(sleepTime - (System.currentTimeMillis() - startTime))); - else - Thread.sleep(0); - lag = false; - } catch (Exception e) { - lag = true; - } + tick(); + tickTime = (System.nanoTime() - nanos)/1000d; + deltaTime = tickTime * timeScale; + nanos = System.nanoTime(); + + //no sleeping because we now operate on delta time. } } /** * makes a buffer and stuff, called with new windows and things. MOVE ALONG */ - private static void createBuffer() { - buffer = (new BufferedImage(WIDTH, HEIGHT, BufferedImage.TRANSLUCENT)); + private void createBuffer() { + buffer = (new BufferedImage(BUFFER_WIDTH, BUFFER_HEIGHT, BufferedImage.TRANSLUCENT)); g2 = (Graphics2D) buffer.getGraphics(); } @@ -248,14 +251,15 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener, public void update(Graphics g) { // Graphics g2 = buffer.getGraphics(); + /* if (buffer.getWidth() != WIDTH || buffer.getHeight() != HEIGHT) { System.out.println("bork " + buffer.getWidth()); System.out.println("bork " + WIDTH); createBuffer(); } - +*/ paint(g2); - g.drawImage(buffer, 0, 0, null); + g.drawImage(buffer, 0, 0, WIDTH, HEIGHT, null); } /** @@ -266,21 +270,12 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener, */ public static boolean switchApps(int i) { try { - log("pausing " + apps[app].getTitle()); apps[app].pauseApp(); app = i; - log("initializing " + apps[app].getTitle()); - apps[app].initialize(); - log("resuming " + apps[app].getTitle()); - apps[app].resumeApp(); - log("setting window properties"); setWindowProperties(apps[app]); - log("Started up " + apps[app].getTitle()); - - frame.pack(); - - // because we now use the ONE buffer system... yeah - // lets do something about thaaaaaaaaat... + apps[app].initialize(); + apps[app].resumeApp(); + Engine.frame.requestFocus(); return true; @@ -296,7 +291,7 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener, * @param app */ private static void setWindowProperties(BasicApp app) { - setWindowProperties(app.getResolution(), app.getFramerate(), app.getResizable()); + setWindowProperties(app.getResolution(), app.getResizable()); } /** @@ -306,16 +301,19 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener, * @param fps * @param resizable */ - private static void setWindowProperties(Dimension dimension, int fps, boolean resizable) { + private static void setWindowProperties(Dimension dimension, boolean resizable) { frame.setResizable(resizable); staticMain.setSize(dimension); frame.pack(); frame.setLocationRelativeTo(null); WIDTH = dimension.width; HEIGHT = dimension.height; - setFramerate(fps); frame.setResizable(resizable); - + BUFFER_HEIGHT = HEIGHT*(retina?2:1); + BUFFER_WIDTH = WIDTH*(retina?2:1); + apps[app].updateDimensions(BUFFER_WIDTH, BUFFER_HEIGHT); + staticMain.createBuffer(); + frame.pack(); } public void paint(Graphics g) {// oh..... @@ -348,17 +346,10 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener, // show fps if debug level high enough if (debug > 0) g.drawString("FPS: " + FPS, 20, 20); - if (overclock) - g.drawString("Overclocking!", 20, 35); g.setColor(Color.RED); if (lag) g.fillOval(10, 10, 10, 10); - g.setColor(Color.WHITE); - if (debug > 0) - if (!(log.size() == 0)) - for (int i = log.size() - 1; i >= 0; i--) - log.elementAt(i).render(g, WIDTH - 200, HEIGHT - 10 - (i * 12)); } catch (Exception e) { g.setFont(largerFont); g.setColor(Color.BLACK); @@ -376,32 +367,12 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener, private void tick() { apps[app].tick(); - for (int i = 0; i < log.size(); i++) - log.elementAt(i).tick(); - - int i = 0; - while (i < log.size()) { - if (!log.elementAt(i).getAlive()) - log.remove(i); - else - i++; - } - - while (log.size() > 10) { - log.pop(); - } - } - - public static void log(String s) { - log.insertElementAt(new LogItem(s, 100), 0); } @Override public void keyPressed(KeyEvent e) { apps[app].keyPressed(e); - if (e.getKeyCode() == KeyEvent.VK_O && keys[KeyEvent.VK_CONTROL]) { - overclock = !overclock; - } + keys[e.getKeyCode()] = true; } @@ -420,43 +391,44 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener, System.exit(0); } - private static void setFramerate(int fps) { - frameSync = fps; - sleepTime = 1000 / frameSync; - } - @Override public void mouseDragged(MouseEvent e) { - mouseX = e.getX(); - mouseY = e.getY(); + updateMouse(e); } @Override public void mouseMoved(MouseEvent e) { - mouseX = e.getX(); - mouseY = e.getY(); + updateMouse(e); } @Override - public void mouseClicked(MouseEvent arg0) { + public void mouseClicked(MouseEvent e) { } @Override - public void mouseEntered(MouseEvent arg0) { + public void mouseEntered(MouseEvent e) { } @Override - public void mouseExited(MouseEvent arg0) { + public void mouseExited(MouseEvent e) { } @Override - public void mousePressed(MouseEvent arg0) { - mouse = true; + public void mousePressed(MouseEvent e) { + updateMouse(e); apps[app].click(); + mouse = true; + } + + private void updateMouse(MouseEvent e) { + mouseX = (int)(((double)e.getX() / WIDTH)*BUFFER_WIDTH); + mouseY = (int)(((double)e.getY() / HEIGHT)*BUFFER_HEIGHT); + mouse = false; } @Override - public void mouseReleased(MouseEvent arg0) { + public void mouseReleased(MouseEvent e) { + updateMouse(e); mouse = false; } @@ -487,8 +459,6 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener, @Override public void componentResized(ComponentEvent e) { setSize(getPreferredSize()); - System.out.println("HEIGHT: " + HEIGHT); - System.out.println("WIDTH: " + WIDTH); WIDTH = getSize().width; HEIGHT = getSize().height; createBuffer(); @@ -500,4 +470,4 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener, // TODO Auto-generated method stub } -} \ No newline at end of file +} diff --git a/src/MAndEngine/ImageCreator.java b/src/MAndEngine/ImageCreator.java index d42e9a8..b30c33f 100644 --- a/src/MAndEngine/ImageCreator.java +++ b/src/MAndEngine/ImageCreator.java @@ -99,7 +99,7 @@ public class ImageCreator { double power = width > height ? width : height; power *= 2; power = (int) (log(2, power) + .5); - //not my code hell if i know how it works + //not my code final int DATA_SIZE = (int) Math.pow(2, power) + 1; final double SEED = 1000.0; double[][] data = new double[DATA_SIZE][DATA_SIZE]; @@ -147,7 +147,10 @@ public class ImageCreator { } for (int i = 0; i < DATA_SIZE; i++) { for (int j = 0; j < DATA_SIZE; j++) { - img.setRGB(i, j, (0xFF000000) | ((int) (((data[i][j] - min) / (max - min)) * 255) << 16) | ((int) (((data[i][j] - min) / (max - min)) * 255) << 8) | ((int) (((data[i][j] - min) / (max - min)) * 255))); + img.setRGB(i, j, (0xFF000000) | + ((int) (((data[i][j] - min) / (max - min)) * 255) << 16) | + ((int) (((data[i][j] - min) / (max - min)) * 255) << 8) | + ((int) (((data[i][j] - min) / (max - min)) * 255) << 0)); } } for (int i = 0; i < DATA_SIZE; i++) { @@ -162,7 +165,6 @@ public class ImageCreator { BufferedImage _return = new BufferedImage(width, height, BufferedImage.TRANSLUCENT); _return.getGraphics().drawImage(img, 0 - (img.getWidth() / 8), 0 - (img.getHeight() / 8), null); return _return; - } private static double log(double b, double x) { diff --git a/src/MAndEngine/Utils.java b/src/MAndEngine/Utils.java index a7a94ae..ecfd01e 100644 --- a/src/MAndEngine/Utils.java +++ b/src/MAndEngine/Utils.java @@ -1,7 +1,7 @@ package MAndEngine; public class Utils { - public static int rand(int i, int j) { - return (int)(Math.random()*(j - i) + i); + public static int rand(int low, int high) { + return (int)(Math.random() * (high - low + 1) + low); } } diff --git a/src/MAndEngine/Variable.java b/src/MAndEngine/Variable.java index 7388ffa..f6fc77b 100644 --- a/src/MAndEngine/Variable.java +++ b/src/MAndEngine/Variable.java @@ -7,11 +7,9 @@ import java.util.Formatter; import java.util.Scanner; /** - * to note, will not work on Mac yet. - * - * edit: WILL WORK ON MAC MOTHER FUCKERS - * - * edit: idek if this will work on macs because app data... + * helpful little class that creates a string variable that gets saved in application + * data. when you write to it, it gets saved as that file. and yes it only works on + * windows because i used backslashes and not the respective seperative character. * * @author Marcus * @@ -62,8 +60,8 @@ public class Variable { String str = getValueFromFile(); // if we could not load a value from the file - // AKA didnt fucking exist. - // ORRRRRRR if you were an ass, and forced + // AKA didnt exist. + // ORRRRRRR if you forced // the value. if (str == null) { this.value = value; @@ -113,9 +111,6 @@ public class Variable { f.format("" + value); f.close(); } catch (Exception e) { - // if(weArriveHere){ - // we.are("fucked"); - // } e.printStackTrace(); } } @@ -134,19 +129,15 @@ public class Variable { private void createFile() { //make the directory because god knows, java can't do that for us //when we say we want a new file in an unknown folder noooooo.... - //jackass java - File f = new File(fileDir); - f.mkdirs(); + File file = new File(fileDir); + file.mkdirs(); //no onto the file itself. create the object - f = new File(filePath); + file = new File(filePath); try { //hopefully make the file... - f.createNewFile(); + file.createNewFile(); } catch (IOException e) { - // if(weArriveHere){ - // we.are("fucked"); - // } e.printStackTrace(); } }