Merge remote-tracking branch 'origin/master'

Conflicts:
	src/MAndEngine/ImageCreator.java
	src/MAndEngine/Utils.java
master
marcus13345 2015-04-11 23:20:05 -04:00
commit d144576b31
6 changed files with 87 additions and 127 deletions

View File

@ -7,8 +7,10 @@ import java.util.ArrayList;
import java.util.Scanner; import java.util.Scanner;
/** /**
* also a bit of an app helper class thing * this class is a jack of all trades when it comes to information about apps.
* well... i renamed it so, now it just is an app helper class thing. * 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 * @author Marcus
* *
*/ */

View File

@ -72,12 +72,6 @@ public abstract interface BasicApp {
*/ */
public abstract Color getColor(); public abstract Color getColor();
/**
* the framerate that this should be run at.
* @return
*/
public abstract int getFramerate();
/** /**
* should this window be resizable * should this window be resizable
* @return * @return
@ -97,5 +91,6 @@ public abstract interface BasicApp {
public abstract void click(); public abstract void click();
public abstract void updateDimensions(int width, int height);
} }

View File

@ -21,6 +21,13 @@ import javax.swing.*;
public class Engine extends Canvas implements KeyListener, MouseMotionListener, MouseListener, ContainerListener, ComponentListener { 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 * to track the x and y
*/ */
@ -42,19 +49,11 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener,
*/ */
private static AppHelper appInitializer; 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 * variables to track the fps, DON'T WORRY ABOUT IT, PAST YOU HAS YOU
* COVERED. * 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; private static int framesInCurrentSecond = 0, FPS = 0;
@ -82,6 +81,7 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener,
* IN CFG. * IN CFG.
*/ */
private static int WIDTH = 800, HEIGHT = 600, app = 0; 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. * 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 * 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.<br/>
* public until i figure out how to work with the changing resolution.
*/ */
private static Engine staticMain; public static Engine staticMain;
/** /**
* debug level. * 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 * SRSLY CALL DYS ONCE. DAS IT. ALL YOU GET. ONE SHOT. because this is a
* static engine, yeah * static engine, yeah
* @param retina
*/ */
public Engine(String[] classes, boolean showLoading) { public Engine(String[] classes, boolean showLoading, boolean retina) {
// frame.setVisible(true); // frame.setVisible(true);
this.retina = retina;
// set static object // set static object
staticMain = this; staticMain = this;
@ -164,7 +173,7 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener,
frame.addContainerListener(this); frame.addContainerListener(this);
frame.addComponentListener(this); frame.addComponentListener(this);
requestFocus(); //requestFocus();
if (showLoading) if (showLoading)
frame.setVisible(true); frame.setVisible(true);
@ -191,7 +200,7 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener,
switchApps(0); switchApps(0);
createBuffer();
} }
@ -204,6 +213,8 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener,
frame.setVisible(true); frame.setVisible(true);
long nanos = System.nanoTime();
// now we do stuff. // now we do stuff.
while (running) { while (running) {
// FPS STUFF WORRY NOT, ITS ALL GOOD. MOVE ALONG. // FPS STUFF WORRY NOT, ITS ALL GOOD. MOVE ALONG.
@ -215,29 +226,21 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener,
} }
framesInCurrentSecond++; framesInCurrentSecond++;
// tick stuff
tick();
// paint the same stuff
repaint(); repaint();
tick();
tickTime = (System.nanoTime() - nanos)/1000d;
deltaTime = tickTime * timeScale;
nanos = System.nanoTime();
// FRAMERATE OVERCLOCKING AND SUCH, MOVE ALONG. //no sleeping because we now operate on delta time.
try {
if (!overclock)
Thread.sleep((long) Math.floor(sleepTime - (System.currentTimeMillis() - startTime)));
else
Thread.sleep(0);
lag = false;
} catch (Exception e) {
lag = true;
}
} }
} }
/** /**
* makes a buffer and stuff, called with new windows and things. MOVE ALONG * makes a buffer and stuff, called with new windows and things. MOVE ALONG
*/ */
private static void createBuffer() { private void createBuffer() {
buffer = (new BufferedImage(WIDTH, HEIGHT, BufferedImage.TRANSLUCENT)); buffer = (new BufferedImage(BUFFER_WIDTH, BUFFER_HEIGHT, BufferedImage.TRANSLUCENT));
g2 = (Graphics2D) buffer.getGraphics(); g2 = (Graphics2D) buffer.getGraphics();
} }
@ -248,14 +251,15 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener,
public void update(Graphics g) { public void update(Graphics g) {
// Graphics g2 = buffer.getGraphics(); // Graphics g2 = buffer.getGraphics();
/*
if (buffer.getWidth() != WIDTH || buffer.getHeight() != HEIGHT) { if (buffer.getWidth() != WIDTH || buffer.getHeight() != HEIGHT) {
System.out.println("bork " + buffer.getWidth()); System.out.println("bork " + buffer.getWidth());
System.out.println("bork " + WIDTH); System.out.println("bork " + WIDTH);
createBuffer(); createBuffer();
} }
*/
paint(g2); 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) { public static boolean switchApps(int i) {
try { try {
log("pausing " + apps[app].getTitle());
apps[app].pauseApp(); apps[app].pauseApp();
app = i; 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]); setWindowProperties(apps[app]);
log("Started up " + apps[app].getTitle()); apps[app].initialize();
apps[app].resumeApp();
frame.pack(); Engine.frame.requestFocus();
// because we now use the ONE buffer system... yeah
// lets do something about thaaaaaaaaat...
return true; return true;
@ -296,7 +291,7 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener,
* @param app * @param app
*/ */
private static void setWindowProperties(BasicApp 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 fps
* @param resizable * @param resizable
*/ */
private static void setWindowProperties(Dimension dimension, int fps, boolean resizable) { private static void setWindowProperties(Dimension dimension, boolean resizable) {
frame.setResizable(resizable); frame.setResizable(resizable);
staticMain.setSize(dimension); staticMain.setSize(dimension);
frame.pack(); frame.pack();
frame.setLocationRelativeTo(null); frame.setLocationRelativeTo(null);
WIDTH = dimension.width; WIDTH = dimension.width;
HEIGHT = dimension.height; HEIGHT = dimension.height;
setFramerate(fps);
frame.setResizable(resizable); 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..... 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 // show fps if debug level high enough
if (debug > 0) if (debug > 0)
g.drawString("FPS: " + FPS, 20, 20); g.drawString("FPS: " + FPS, 20, 20);
if (overclock)
g.drawString("Overclocking!", 20, 35);
g.setColor(Color.RED); g.setColor(Color.RED);
if (lag) if (lag)
g.fillOval(10, 10, 10, 10); 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) { } catch (Exception e) {
g.setFont(largerFont); g.setFont(largerFont);
g.setColor(Color.BLACK); g.setColor(Color.BLACK);
@ -376,32 +367,12 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener,
private void tick() { private void tick() {
apps[app].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 @Override
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
apps[app].keyPressed(e); apps[app].keyPressed(e);
if (e.getKeyCode() == KeyEvent.VK_O && keys[KeyEvent.VK_CONTROL]) {
overclock = !overclock;
}
keys[e.getKeyCode()] = true; keys[e.getKeyCode()] = true;
} }
@ -420,43 +391,44 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener,
System.exit(0); System.exit(0);
} }
private static void setFramerate(int fps) {
frameSync = fps;
sleepTime = 1000 / frameSync;
}
@Override @Override
public void mouseDragged(MouseEvent e) { public void mouseDragged(MouseEvent e) {
mouseX = e.getX(); updateMouse(e);
mouseY = e.getY();
} }
@Override @Override
public void mouseMoved(MouseEvent e) { public void mouseMoved(MouseEvent e) {
mouseX = e.getX(); updateMouse(e);
mouseY = e.getY();
} }
@Override @Override
public void mouseClicked(MouseEvent arg0) { public void mouseClicked(MouseEvent e) {
} }
@Override @Override
public void mouseEntered(MouseEvent arg0) { public void mouseEntered(MouseEvent e) {
} }
@Override @Override
public void mouseExited(MouseEvent arg0) { public void mouseExited(MouseEvent e) {
} }
@Override @Override
public void mousePressed(MouseEvent arg0) { public void mousePressed(MouseEvent e) {
mouse = true; updateMouse(e);
apps[app].click(); 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 @Override
public void mouseReleased(MouseEvent arg0) { public void mouseReleased(MouseEvent e) {
updateMouse(e);
mouse = false; mouse = false;
} }
@ -487,8 +459,6 @@ public class Engine extends Canvas implements KeyListener, MouseMotionListener,
@Override @Override
public void componentResized(ComponentEvent e) { public void componentResized(ComponentEvent e) {
setSize(getPreferredSize()); setSize(getPreferredSize());
System.out.println("HEIGHT: " + HEIGHT);
System.out.println("WIDTH: " + WIDTH);
WIDTH = getSize().width; WIDTH = getSize().width;
HEIGHT = getSize().height; HEIGHT = getSize().height;
createBuffer(); createBuffer();

View File

@ -99,7 +99,7 @@ public class ImageCreator {
double power = width > height ? width : height; double power = width > height ? width : height;
power *= 2; power *= 2;
power = (int) (log(2, power) + .5); 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 int DATA_SIZE = (int) Math.pow(2, power) + 1;
final double SEED = 1000.0; final double SEED = 1000.0;
double[][] data = new double[DATA_SIZE][DATA_SIZE]; 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 i = 0; i < DATA_SIZE; i++) {
for (int j = 0; j < DATA_SIZE; j++) { 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++) { for (int i = 0; i < DATA_SIZE; i++) {
@ -162,7 +165,6 @@ public class ImageCreator {
BufferedImage _return = new BufferedImage(width, height, BufferedImage.TRANSLUCENT); BufferedImage _return = new BufferedImage(width, height, BufferedImage.TRANSLUCENT);
_return.getGraphics().drawImage(img, 0 - (img.getWidth() / 8), 0 - (img.getHeight() / 8), null); _return.getGraphics().drawImage(img, 0 - (img.getWidth() / 8), 0 - (img.getHeight() / 8), null);
return _return; return _return;
} }
private static double log(double b, double x) { private static double log(double b, double x) {

View File

@ -1,7 +1,7 @@
package MAndEngine; package MAndEngine;
public class Utils { public class Utils {
public static int rand(int i, int j) { public static int rand(int low, int high) {
return (int)(Math.random()*(j - i) + i); return (int)(Math.random() * (high - low + 1) + low);
} }
} }

View File

@ -7,11 +7,9 @@ import java.util.Formatter;
import java.util.Scanner; import java.util.Scanner;
/** /**
* to note, will not work on Mac yet. * 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
* edit: WILL WORK ON MAC MOTHER FUCKERS * windows because i used backslashes and not the respective seperative character.
*
* edit: idek if this will work on macs because app data...
* *
* @author Marcus * @author Marcus
* *
@ -62,8 +60,8 @@ public class Variable {
String str = getValueFromFile(); String str = getValueFromFile();
// if we could not load a value from the file // if we could not load a value from the file
// AKA didnt fucking exist. // AKA didnt exist.
// ORRRRRRR if you were an ass, and forced // ORRRRRRR if you forced
// the value. // the value.
if (str == null) { if (str == null) {
this.value = value; this.value = value;
@ -113,9 +111,6 @@ public class Variable {
f.format("" + value); f.format("" + value);
f.close(); f.close();
} catch (Exception e) { } catch (Exception e) {
// if(weArriveHere){
// we.are("fucked");
// }
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -134,19 +129,15 @@ public class Variable {
private void createFile() { private void createFile() {
//make the directory because god knows, java can't do that for us //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.... //when we say we want a new file in an unknown folder noooooo....
//jackass java File file = new File(fileDir);
File f = new File(fileDir); file.mkdirs();
f.mkdirs();
//no onto the file itself. create the object //no onto the file itself. create the object
f = new File(filePath); file = new File(filePath);
try { try {
//hopefully make the file... //hopefully make the file...
f.createNewFile(); file.createNewFile();
} catch (IOException e) { } catch (IOException e) {
// if(weArriveHere){
// we.are("fucked");
// }
e.printStackTrace(); e.printStackTrace();
} }
} }