Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (777)
Games in Android Showcase (231)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: hadezbladez (posted 2018-11-16 13:41:33, viewed 154 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299   300   301   302   303   304   305   306   307   308   309   310   311   312   313   314   315   316   317   318   319   320   321   322   323   324   325   326   327   328   329   330   331   332   333   334   335   336   337   338   339   340   341   342   343   344   345   346   347   348   349   350   351   352   353   354   355   356   357   358   359   360   361   362   363   364   365   366   367   368   369   370   371   372   373   374   375   376   377   378   379   380   381   382   383   384   385   386   387   388   389   390   391   392   393   394   395   396   397   398   399   400   401   402   403   404   405   406   407   408   409   410   411   412   413   414   415   416   417   418   419   420   421   422   423   424   425   426   427   428   429   430   431   432   433   434   435   436   437   438   439   440   441   442   443   444   445   446   447   448   449   450   451   452   453   454   455   456   457   458   459   460   461   462   463   464   465   466   467   468   469   470   471   472   473   474   475   476   477   478   479   480   481   482   483   484   485   486   487   488   489   490   491   492   493   494   495   496   497   498   499   500   501   502   503   504   505   506   507   508   509   510   511   512   513   514   515   516   517   518   519   520   521   522   523   524   525   526   527   528   529   530   531   532   533   534   535   536   537   538   539   540   541   542   543   544   545   546   547   548   549   550   551   552   553   554   555   556   557   558   559   560   561   562   563   564   565   566   567   568   569   570   571   572   573   574   575   576   577   578   579   580   581   582   583   584   585   586   587   588   589   590   591   592   593   594   595   596   597   598   599   600   601   602   603   604   605   606   607   608   609   610   611   612   613   614   615   616   617   618   619   620   621   622   623   624   625   626   627   628   629   630   631   632   633   634   635   636   637   638   639   640   641   642   643   644   645   646   647   648   649   650   651   652   653   654   655   656   657   658   659   660   661   662   663   664   665   666   667   668   669   670   671   672   673   674   675   676   677   678   679   680   681   682   683   684   685   686   687   688   689   690   691   692   693   694   695   696   697   698   699   700   701   702   703   704   705   706   707   708   709   710   711   712   713   714   715   716   717   718   719   720  
public class GameEngine implements Runnable{
   ////System////
   //more in-depth << this is only add-on for coding 
   private ServerSocket programServerSocket;
   //monitor
   private DisplayMode dispMode;
   private GraphicsEnvironment gEnv; private GraphicsDevice gDev;
   private GraphicsConfiguration gCon;
   private final int monitor_width, monitor_height;//the monitor screen hardware
   //info system
   public static String TITLE;
   public static int windowFrame_width, windowFrame_height;//the window frame screen that popping up
   public static int game_width, game_height;// the game screen that is in window frame screen || yes this use final || this become important when dealing with game class
   private static double scaleWidth, scaleHeight;
   private int fullscreen_int;
   //static event system
   private boolean ready, running;
   private int shutDown_sequence; public final static int closingSequence = 10, allClosed = 100;
   private long durationToClosing;
   //quick event interruption
   private boolean sleeps;
   
   ////essentials////
   //core
   private WindowsEventInformation windowInformationMemory; private Dimension window_dimension;
   private JFrame windowFrame; private Canvas canvas; private Thread thread;
   private int safeMethodProgramRuns; private final static int SAFETY_FIRST_MODE = 12200, SAFETY_FIRST_HANGSEC_MODE = 13300,CANCER_RISK_MODE = 21100;
   private int exitStatusNumber;
   //option
   public static int performanceProcess; public final static int fullThrottle = 10, safeMemory = 20;
   public static double masterMusicVolume, masterSoundVolume;
   public static boolean masterMusic_continueFocus;
   public static Music stateMusicLevel;
   //render
   private BufferStrategy bufferStrat; private Graphics graphicRender;
   private byte fps; private float multiple_theTimingOfDelta;
   //class
   public static GameEngine INSTANCE;
   public static Assets assets; public static STMemoryPlayer mainMemory; public static boolean end_fame;
   private KeyBoardManager keyInput; private StateManager stateManager;
   //image addition
   private boolean blackScreenBoolean;
   private float alphaBlackScreen;
   
   public void splitUp_collapser(){}
   public GameEngine() {
      try {//server check
         //well i think this server is only 4 code testing ||  program run once
         programServerSocket = new ServerSocket(65535, 1, InetAddress.getLocalHost() );
         System.out.println("GameEngine.GameEngine() = " + programServerSocket.getLocalPort() );
         System.out.println("GameEngine.GameEngine() + program starts with socket. user = " + programServerSocket.getInetAddress().getHostName() );
      }
      catch (SocketException e){//it might be show the pop-ups program that is already in use
         System.err.println("GameEngine.GameEngine() + programs already running");
         e.printStackTrace();
         System.exit(1);
      }
      catch (IOException e) {e.printStackTrace();}
      
      //check os
      String osName = System.getProperties().getProperty("os.name");
      if(osName.equals("Windows 7") ){}
      
      System.out.println("GameEngine.GameEngine() + os name = " + osName);
      System.out.println("=============================================");
      
      //the first initialize
      gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment(); gDev = gEnv.getDefaultScreenDevice();
      gCon = gDev.getDefaultConfiguration(); dispMode = gDev.getDisplayMode();
      System.out.println("GameEngine.GameEngine() checking graphic_device = ");
      System.out.println("translucent = " + gDev.isWindowTranslucencySupported(WindowTranslucency.TRANSLUCENT) );
      System.out.println("perpixel_translucent = " + gDev.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSLUCENT) );
      System.out.println("perpixel_transparent = " + gDev.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSPARENT) );
      System.out.println("display change = " + gDev.isDisplayChangeSupported() );
      System.out.println("full screen = " + gDev.isFullScreenSupported() );
      
      System.out.println();
      
      System.out.println("GameEngine.GameEngine() checking display mode detail = ");
      System.out.println("bit depth = " + dispMode.getBitDepth() );
      System.out.println("refresh rate = " + dispMode.getRefreshRate() );
      
      System.out.println();
      
      monitor_width = dispMode.getWidth(); monitor_height = dispMode.getHeight();
      game_width = 800; game_height = 600;//u might not need to change this || this thing may be not in here
      fullscreen_int = 0;//change full-screen in here
      
      TITLE = "ExFate : SEN (UNDER DEV)";
      ready = false;
      
      if(gEnv != null && gDev != null && dispMode != null){
         if(fullscreen_int >  1){fullscreen_int = 0;}//this thing doesnt make any sense it might be for testing stuff for not going to fullscreen
         
         if(monitor_width > 800 && monitor_height > 600){//choosing windowFrame and scale the game
            //solve the inconsistent canvas size and jFrame size || if windowFrame is resize-able the inconsistency increase 10 both width and height
            //the scale on eclipse JFrame are fucked up so bad
            windowFrame_width = game_width + 6; windowFrame_height = game_height + 28;
            scaleWidth = 1; scaleHeight = 1;
         }
         else if(monitor_width <= 800 || monitor_height <= 600){
            System.err.println("reducing frame and scale the games");
            if(monitor_width <= 640 || monitor_height <= 480){
               windowFrame_width = (game_width/2) + 6; windowFrame_height = (game_height/2) + 28;
               scaleWidth = 0.5d; scaleHeight = 0.5d;
            }
            else if(monitor_width <= 800 || monitor_height <= 600){
               windowFrame_width = (game_width*8/10) + 6; windowFrame_height = (game_height*8/10) + 28;
               scaleWidth = 0.8d; scaleHeight = 0.8d;
            }
            else{
               System.err.println("what? are u still using monitor that below 640x480 ?");System.exit(1);
            }
         }
         else{System.err.println("screen error and something interrupted"); System.exit(1);}
         
         //initialize frame, canvas and dimension
         canvas = new Canvas(gDev.getDefaultConfiguration() );
         windowFrame = new JFrame(gDev.getDefaultConfiguration() );
         window_dimension = new Dimension(windowFrame_width, windowFrame_height);
      }
      if(canvas != null){//controls still not used
         canvas.setIgnoreRepaint(true);
         canvas.addKeyListener(null);
         canvas.addMouseListener(null);
         canvas.addMouseMotionListener(null);
      }
      if(windowFrame != null){
         //canvas.setMaximumSize(new Dimension(game_width, game_height) );
         //canvas.setMinimumSize(new Dimension(game_width, game_height) );
         //canvas.setPreferredSize(new Dimension(game_width, game_height) );
         canvas.setBounds(0, 0, 800, 600);//what is this?
         canvas.setIgnoreRepaint(true);
         
         //JFrame.setDefaultLookAndFeelDecorated(false);//<<what this shit?
         ArrayList<BufferedImage> icons = new ArrayList<BufferedImage>();
         icons.add(ImageLoader.loadImage_static(getClass().getResourceAsStream("/gem64x64.png") ) );
         icons.add(ImageLoader.loadImage_static(getClass().getResourceAsStream("/gem32x32.png") ) );
         icons.add(ImageLoader.loadImage_static(getClass().getResourceAsStream("/gem16x16.png") ) );
         windowFrame.setIconImages(icons);//not used for multiple icons
         icons.clear();icons = null;
         
         windowFrame.setIgnoreRepaint(true);
         windowFrame.setAlwaysOnTop(false);
         windowFrame.setTitle(TITLE);
         windowFrame.setSize(window_dimension);
         windowFrame.setResizable(false);//no resize
         windowFrame.setLocationRelativeTo(null);//center screen
         windowFrame.setFocusable(true);
         
         windowInformationMemory = new WindowsEventInformation(this);
         windowFrame.addWindowListener(windowInformationMemory);
         setFullScreen(fullscreen_int);
         windowFrame.add(canvas);//solve the lag issue using canvas or no
         
         for(int i = 0; i < windowFrame.getComponents().length; i++){System.out.println("GameEngine.GameEngine() component = " + windowFrame.getComponent(i) );}
         
         ready = true;
      }
      if(windowFrame != null && gDev != null && ready){running = false; start();}
   }
   
   public void start(){
      if(thread != null){stop(11);}
      if(running) {stop(12);}
      
      running = true; safeMethodProgramRuns = CANCER_RISK_MODE;
      thread = new Thread(this, TITLE + "main-thread");
      preinit();
      thread.start();
      
      System.out.println("GameEngine.start() + " + thread.getName() );
      safeMethodProgramRuns = SAFETY_FIRST_HANGSEC_MODE;
   }
   public void run() {
      System.out.println("GameEngine.run() is thread alive = " + thread.isAlive() );
      threadSleeping(500);
      windowFrame.setVisible(true);
      if(canvas != null){canvas.requestFocus();}
      else if(canvas == null){windowFrame.requestFocus();}
      
      window_dimension = null;
      System.gc();
      
      //initialize variable game updates || gups_init = game update per sec
      byte gups_init = fps;
      boolean limit_theMultiplyDelta = false;
      multiple_theTimingOfDelta = 1;
      bufferStrat = null;graphicRender = null;
      
      //this guys must throw some exception right?
      canvas.createBufferStrategy(3);
      bufferStrat = canvas.getBufferStrategy();
      
      //gameLoop_1(gups_init, limit_theMultiplyDelta);
      gameLoop_2(gups_init, limit_theMultiplyDelta);
      
      //last stretch of running phase
      if(!windowInformationMemory.isWindowsClosed() ){}
      if(exitStatusNumber != 1){closingSequencial(exitStatusNumber);}
   }
   
   //stop, closing and exit family
   private void closingSequencial(int exit){
      shutDown_sequence = closingSequence; durationToClosing = 150;//its really near half sec for last closing
      
      if(shutDown_sequence == closingSequence){//using while prompt things?
         if(TinySound.isInitialized() ){
            TinySound.shutdown();
            System.out.println("\nGameEngine.stop() + shutdown lib");
         }
         else if(!TinySound.isInitialized() ){System.out.println("GameEngine.stop() + no lib to stop");}
         try {Thread.sleep(durationToClosing);}
         catch (InterruptedException e) {e.printStackTrace(); System.exit(1);}
         
         if(programServerSocket != null){
            try {
               programServerSocket.close();
               if(programServerSocket.isClosed() ){programServerSocket = null;System.out.println("GameEngine.stop() + server closed");}
               else if(!programServerSocket.isClosed() ){programServerSocket.close(); programServerSocket = null; System.err.println("GameEngine.stop() + something error when server closed");}
            }
            catch (IOException e) {e.printStackTrace(); System.exit(1);}
         }
         try {Thread.sleep(durationToClosing);}
         catch (InterruptedException e) {e.printStackTrace(); System.exit(1);}
         
         bufferStrat.dispose(); bufferStrat = null;
         graphicRender.dispose(); graphicRender = null;
         windowFrame.removeAll();
         windowFrame.dispose();
         canvas.removeNotify();
         
         shutDown_sequence = allClosed;
         
         try {Thread.sleep(durationToClosing);}
         catch (InterruptedException e) {e.printStackTrace(); System.exit(1);}
      }
      
      if(shutDown_sequence == allClosed){
         //exit must be in here not outer
         if(exit == 0){System.out.println("GameEngine.stop() + exit with no problem bro");}
         else if(exit == 1){System.out.println("GameEngine.closingSequencial() what is this? some bug must be in here"); System.exit(-100);}
         else if(exit == 2){System.out.println("GameEngine.stop() + exit from game");}
         
         else if(exit == 10){System.err.println("GameEngine.stop() + while running error"); System.exit(1);}
         else if(exit == 11){System.err.println("GameEngine.stop() + program's running more than 1"); System.exit(1);}
         else if(exit == 12){System.err.println("GameEngine.stop() + startup error / running error"); System.exit(1);}
         else if(exit == 15){System.err.println("GameEngine.stop() + load image errors"); System.exit(1);}
         else if(exit == 16){System.err.println("GameEngine.stop() + load audio errors"); System.exit(1);}
         
         else if(exit == 20){System.err.println("GameEngine.stop() + engine error "); System.exit(1);}
         else if(exit == 21){System.err.println("GameEngine.stop() + thread error "); System.exit(1);}
         else if(exit == 100){System.err.println("GameEngine.stop() + something really error there"); System.exit(1);}
         
         
         try {Thread.sleep(durationToClosing);}
         catch (InterruptedException e) {e.printStackTrace(); System.exit(1);}
      }
      else if(shutDown_sequence != allClosed){
         System.err.println("GameEngine.stop() + closing error");
         System.exit(1);
      }
   }
   public void stop(int exit){
      if(!running){System.exit(2);return;}
      running = false;
      exitStatusNumber = exit;
   }
   public void closingExit_using_X_only(){
      //x close sequence it will jump through the this.running method and destroying all
      
      if(!running){System.exit(2);return;}//this is work after pass running false variables
      exitStatusNumber = 1;
      
      shutDown_sequence = closingSequence; durationToClosing = 150;//its really near half sec for last closing
      if(shutDown_sequence == closingSequence){//using while prompt things?
         if(TinySound.isInitialized() ){
            TinySound.shutdown();
            System.out.println("\nGameEngine.stop() + shutdown lib");
         }
         else if(!TinySound.isInitialized() ){System.out.println("GameEngine.stop() + no lib to stop");}
         threadSleeping(durationToClosing);
         
         if(programServerSocket != null){
            try {
               programServerSocket.close();
               if(programServerSocket.isClosed() ){programServerSocket = null;System.out.println("GameEngine.stop() + server closed");}
               else if(!programServerSocket.isClosed() ){programServerSocket.close(); programServerSocket = null; System.err.println("GameEngine.stop() + something error when server closed");}
            }
            catch (IOException e) {e.printStackTrace(); System.exit(1);}
         }
         threadSleeping(durationToClosing);
         
         shutDown_sequence = allClosed;
         
         threadSleeping(durationToClosing);
      }
      
      if(shutDown_sequence == allClosed){
         running = false;
         
         bufferStrat.dispose(); bufferStrat = null;
         graphicRender.dispose(); graphicRender = null;
         windowFrame.removeAll();
         windowFrame.dispose();
         //canvas.removeNotify();//<<this might cause some error
         
         //exit must be in here not outer
         System.out.println("GameEngine.stop() + X windows close");
         
         try {Thread.sleep(durationToClosing);}
         catch (InterruptedException e) {e.printStackTrace(); System.exit(1);}
      }
      else if(shutDown_sequence != allClosed){
         System.err.println("GameEngine.stop() + closing error");
         System.exit(1);
      }

   }
   public void exitCMD_interactively(){
      try {
         thread.join();
         System.out.println("GameEngine.stop() + thread is alive = " + thread.isAlive() );
         threadSleeping(durationToClosing);
         
         System.out.print("GameEngine.stop() + exiting");
         for(int i = 0; i < 8; i++){
            if(i < 4){
               threadSleeping(durationToClosing);
            }
            else if(i > 4){}
            System.out.print(" .");
         }

      }
      catch (InterruptedException e) {e.printStackTrace();System.exit(0);}
   }

   private void preinit() {
      assets = null; mainMemory = null;
      INSTANCE = this;
      stateMusicLevel = null;
      System.out.println("GameEngine.preinit() + " + StaticStuff.getInstance().getClassReference() ); 
      end_fame = false;
      
      performanceProcess = safeMemory;
      masterMusicVolume = 0.7d; masterSoundVolume = 0.5d; masterMusic_continueFocus = false;
      
      if(!TinySound.isInitialized() ){
         TinySound.init();
         System.out.println("GameEngine.preinit() = initialize audio lib");
      }
      
      System.out.println("=============================================");
      System.out.println();
      

      fps = 60;sleeps = false;
      blackScreenBoolean = false;alphaBlackScreen = 1f;
      
      stateManager = new StateManager();
      //TODO
      //stateManager.addState(new MenuState(this, stateManager, MenuState.DEV_START) );
      stateManager.addState(new MenuState(this, stateManager, MenuState.FRESH_START) );//the string is menu
      //stateManager.addState(new MenuState(this, stateManager, MenuState.SCARY_START) );
      //stateManager.addState(new MenuState(this, stateManager, MenuState.TRUE_END) );
      //stateManager.listTheState();
      keyInput = new KeyBoardManager(this); //keyboard input
      if(canvas != null){canvas.addKeyListener(keyInput);}
      else if(canvas == null){windowFrame.addKeyListener(keyInput);}
      
      System.gc();
   }
   private void systemControls(){
      //its controling the game when systems is interrupted by something outer from the games 4th wall breaking?
      if(!sleeps){
         if(!windowInformationMemory.isFocusedInFront() ){sleeps = true;}
         
         if(stateManager.getCurrentState() instanceof MenuState){
            if(((MenuState)stateManager.getCurrentState() ).isPause() ){
               ((MenuState)stateManager.getCurrentState() ).setPause(false);
            }
         }
      }
      else if(sleeps){
         if(windowInformationMemory.isFocusedInFront() ){sleeps = false;}
         if(stateManager.getCurrentState() instanceof GameLevelState){
            if(!((GameLevelState) stateManager.getCurrentState() ).isEventCountBoolean() ){
               if(!((GameLevelState) stateManager.getCurrentState() ).isPause() ){
                  ((GameLevelState) stateManager.getCurrentState() ).pauseInitializement();
                  ((GameLevelState) stateManager.getCurrentState() ).setPause(true);
               }
            }
         }
         else if(stateManager.getCurrentState() instanceof MenuState){
            if(!((MenuState)stateManager.getCurrentState() ).isPause() ){
               ((MenuState)stateManager.getCurrentState() ).pauseInitializement();
               ((MenuState)stateManager.getCurrentState() ).setPause(true);
            }
         }
         else{}//it might be there will be something like in event, menu, and any other state
      }
   }
   void gameLoop_1(byte gups_init, boolean limit_theMultiplyDelta_init){
      //initialize variable
      long now = 0; long lastTime = System.nanoTime();
      double delta = 0;
      double timePerTick = 1000 * 1000 * 1000;
      if(multiple_theTimingOfDelta == 0){this.running = false;}
      
      while(this.running){//looping a variable many times different from top
         now = System.nanoTime();
         delta += ((now - lastTime) * gups_init / timePerTick) * multiple_theTimingOfDelta;
         lastTime = now;
         systemControls();//prerender();
         
         if(delta >= 1){
            if(safeMethodProgramRuns == CANCER_RISK_MODE){
               //there is some code that can run through it and doesnt happen anything
               //but if there is some code that can hang the game forever || well we maybe need this for debugging somethings
               update();
               render();
               delta--;
            }
            else if(safeMethodProgramRuns == SAFETY_FIRST_MODE){
               //this will might be upgraded more for the upper ones || because im lazy adding them 1 by 1 i use this
               //it can bypass this code if the exception set first and therefore will not trigger this exception
               //any exception that fault the game will trigger exit and not gonna hang
               try {
                  update();
                  render();
                  delta--;
               }
               catch (Exception e) {
                  e.printStackTrace();
                  stop(20);
               }
            }
            else if(safeMethodProgramRuns == SAFETY_FIRST_HANGSEC_MODE){
               //any exception that fault the game will trigger exit and hang it 4 a 1 sec
               try {
                  update();
                  render();
                  delta--;
               }
               catch (Exception e) {
                  e.printStackTrace();
                  try {Thread.sleep(1000);}
                  catch (InterruptedException e1) {
                     e1.printStackTrace();
                     stop(21);
                  }
                  stop(20);
               }
               finally{}//<< i dont know what to do in here
            }
         }
         
         if(limit_theMultiplyDelta_init){limitMultiplyDeltaTiming();}
         
         
         try {Thread.sleep(1);}
         catch (InterruptedException e) {e.printStackTrace(); stop(21);}
      }
   }
   void gameLoop_2(byte gups_init, boolean limit_theMultiplyDelta_init){
      //this is for when the game is done
      //gups (it means updating and rendering all of it)
      //idea 1 : reduce memory usage while inactive after the game paused
      //idea 2 : trigger the time to reduce memory usage while inactive
      //idea 3 : it maybe better use only sleeps this and thread.sleep
      
      //what about java memory heap || not only physical memory we need to control many things
      //System.gc(); ||if we use this it smooth the pattern not a sawtooth pattern
      
      //a very dope ideas || but it works
      //if still dev we dont need to do this || its already optimal
      
      //init variable
      long now = 0; long lastTime = System.nanoTime();
      double delta = 0;
      double timePerTick = 1000 * 1000 * 1000;
      double checkFPS = 0; int fps = 0, pps = 0;
      if(multiple_theTimingOfDelta == 0){this.running = false;}
      
      while(this.running){
         now = System.nanoTime();
         delta += ((now - lastTime) * gups_init / timePerTick) * multiple_theTimingOfDelta;
         checkFPS += (now - lastTime);
         lastTime = now;
         systemControls();//prerender();
         
         if(!sleeps){
            //so thread.sleeps is reducing memory consumption || yeah we can maximize this to option
            if(delta >= 1){
               fps++;
               if(safeMethodProgramRuns == CANCER_RISK_MODE){update();render();delta--;}
               else if(safeMethodProgramRuns == SAFETY_FIRST_MODE){
                  try {update();render();delta--;}
                  catch (Exception e) {e.printStackTrace();stop(20);}
               }
               else if(safeMethodProgramRuns == SAFETY_FIRST_HANGSEC_MODE){
                  try {update();render();delta--;}
                  catch (Exception e) {
                     e.printStackTrace();
                     try {Thread.sleep(1000);}
                     catch (InterruptedException e1) {e1.printStackTrace();stop(21);}
                     stop(20);
                  }
               }
            }
            else if(delta > 2){delta = 1.9999; System.err.println("rendering errors");}
            
            if(performanceProcess == safeMemory){
               try {Thread.sleep(1);}
               catch (InterruptedException e) {e.printStackTrace();}
            }
            else if(performanceProcess == fullThrottle){}
            pps++;
            if(checkFPS > 1000 * 1000 * 1000){
               checkFPS -= 1000 * 1000 * 1000;
               System.out.println("fps = " + fps + " pps = " + pps);
               fps = 0; pps = 0;
            }

         }
         else if(sleeps){//this is for the delta limiter || limiting while thread not doing anything usefull
            if(stateMusicLevel != null){
               if(!masterMusic_continueFocus && stateMusicLevel.playing() ){stateMusicLevel.pause();}
            }
            if(delta > 2){//isnt this thing wrong? dont know
               if(safeMethodProgramRuns == CANCER_RISK_MODE){delta = 1.99999;render();}
               else if(safeMethodProgramRuns == SAFETY_FIRST_MODE){
                  try {delta = 1.99999; render();}
                  catch (Exception e) {e.printStackTrace();stop(20);}
               }
               else if(safeMethodProgramRuns == SAFETY_FIRST_HANGSEC_MODE){
                  try {delta = 1.99999;render();}
                  catch (Exception e) {
                     e.printStackTrace();
                     try {Thread.sleep(1000);}
                     catch (InterruptedException e1) {e1.printStackTrace();stop(21);}
                     stop(20);
                  }
                  finally{}//<< i dont know what to do in here
               }
            }
            
            try {Thread.sleep(100);}
            catch (InterruptedException e) {e.printStackTrace();}
         }
         
         if(limit_theMultiplyDelta_init){limitMultiplyDeltaTiming();}
      }
      
   }
   private void update(){stateManager.update();}
   private void render(){
      try {
         if(canvas != null){canvasRender_setup1();}
         else if(canvas == null){jframeRender_setup1();}
      }
      catch(Exception e) {e.printStackTrace();stop(20);}
      finally{if(graphicRender != null && running){bufferStrat.getDrawGraphics().dispose();} }
   }
   
   //limit
   private void limitMultiplyDeltaTiming(){
      if(multiple_theTimingOfDelta > 4f){multiple_theTimingOfDelta = 4f;}
      if(multiple_theTimingOfDelta < 0.00001f){multiple_theTimingOfDelta = 0.00001f;}
   }
   private void threadSleeping(long milliSecondToSleep){
      try {Thread.sleep(milliSecondToSleep);}
      catch (InterruptedException e) {e.printStackTrace();}
   }
   
   //render methods
   private void canvasRender_setup1(){//canvas.getBufferStrategy().getDrawGraphics();
      //this might be not to be used in here
      //bufferStrat = canvas.getBufferStrategy();
      //if(bufferStrat == null){canvas.createBufferStrategy(3);return;}
      //System.out.println(canvas.getWidth() + "||" + canvas.getHeight() + "||" + windowFrame.getWidth() + "||" + windowFrame.getHeight() );
      graphicRender = bufferStrat.getDrawGraphics();//<< content restored become true with this after that false
      //System.out.println(bufferStrat.contentsRestored() );
      //System.out.println(bufferStrat.contentsLost() );//<< i dont get where this must be
      
      if(graphicRender instanceof Graphics2D){//it really is making the game awesome
         //((Graphics2D)graphicRender).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
         ((Graphics2D)graphicRender).setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
      }
      
      graphicRender.setClip(0, 0, windowFrame_width, windowFrame_height);//<<the render screen of the game || so outside will not be rendered
      graphicRender.clearRect(0, 0, windowFrame_width , windowFrame_height);//clear screen so there is no glitch render stuff
      
      //this is altering all of game image || we need some variable to do this?? || this can't be used as scale game screen || there a single rays of hope
      // yes many problem we use gameEngine.instance.getCanvas.screensize >> need to change to gameEngine.gameScreen
      ((Graphics2D)graphicRender).scale(scaleWidth, scaleHeight);//this is to scaling all game image || maybe it not just image it will scaling all of it
      ((Graphics2D)graphicRender).translate(0, 0);//this is moving all game image || so it does affect bottom
      
      //draw here after statemanager
      stateManager.render(graphicRender);
      if(blackScreenBoolean){
         fadeImage(graphicRender, alphaBlackScreen);
         graphicRender.setColor(Color.BLACK);
         graphicRender.fillRect(0, 0, game_width, game_height);
         fadeImage(graphicRender, 1f);
      }
      /*if(sleeps){
         graphicRender.setColor(Color.BLACK);
         graphicRender.fillRect(0, 0, game_width, game_height);
      }*/
      if(running){
         if(bufferStrat != null && graphicRender != null){
            if(!bufferStrat.contentsLost() ){graphicRender.dispose(); bufferStrat.show();}//draw end || what is the graphicRend dispose?
            else if(bufferStrat.contentsLost() ){System.err.println("GameEngine.canvasRender_setup1() buffer strategy contents are lost"); stop(20);}
         }
         Thread.yield();
      }

   }
   private void jframeRender_setup1(){//windowFrame.getBufferStrategy().getDrawGraphics();
      bufferStrat = windowFrame.getBufferStrategy();
      if(bufferStrat == null){windowFrame.createBufferStrategy(3); return;}
      
      graphicRender = bufferStrat.getDrawGraphics();
      graphicRender.setClip(0, 0, windowFrame_width, windowFrame_height);
      graphicRender.clearRect(0, 0, windowFrame_width , windowFrame_height);
      
      ((Graphics2D)graphicRender).scale(scaleWidth, scaleHeight);
      ((Graphics2D)graphicRender).translate(0, 25);//this is moving all game image || so it does affect bottom || the jframe is not setted right
      
      //draw here after statemanager
      stateManager.render(graphicRender);
      if(blackScreenBoolean){
         fadeImage(graphicRender, alphaBlackScreen);
         graphicRender.setColor(Color.BLACK);
         graphicRender.fillRect(0, 0, game_width, game_height);
         fadeImage(graphicRender, 1f);
      }
      /*if(sleeps){
         graphicRender.setColor(Color.BLACK);
         graphicRender.fillRect(0, 0, game_width, game_height);
      }*/
      
      if(!bufferStrat.contentsLost() ){bufferStrat.show();}
      Thread.yield();
   }
   
   //getter and setter
   public JFrame getWindow() {return windowFrame;}
   public Canvas getCanvas() {return canvas;}
   public WindowsEventInformation getWindowInformationMemory() {return windowInformationMemory;}
   public int getShutDown_sequence() {return shutDown_sequence;}
   public GraphicsConfiguration getgCon() {return gCon;}
   public float getMultiple_theTimingOfDelta() {return multiple_theTimingOfDelta;}
   public boolean isSleeps() {return sleeps;}
   public boolean isBlackScreenBoolean() {return blackScreenBoolean;} public float getAlphaBlackScreen() {return alphaBlackScreen;}
   public int getExitStatusNumber() {return exitStatusNumber;}
   
   public void setBlackScreenBoolean(boolean blackScreenBoolean) {this.blackScreenBoolean = blackScreenBoolean;}
   public void setAlphaBlackScreen(float alphaBlackScreen) {this.alphaBlackScreen = alphaBlackScreen;}//now  u can control blackScreen in other places
   public void setSleeps(boolean sleeps) {this.sleeps = sleeps;}
   public void setMultiple_theTimingOfDelta(float multiple_theTimingOfDelta) {this.multiple_theTimingOfDelta = multiple_theTimingOfDelta;}
   public void setRunning(boolean running) {this.running = running;}
   public void setExitStatusNumber(int exitStatusNumber) {this.exitStatusNumber = exitStatusNumber;}
   
   //key controls
   public void keyPressed(KeyEvent e) {stateManager.keyPressed(e);}
   public void keyReleased(KeyEvent e) {stateManager.keyReleased(e);}
   public void keyTyped(KeyEvent e) {/*we don't need to use these*/}
   
   //commented method
   /** private void setFullScreen(int fullscreen)
   @param fullscreen 0 = no fullscreen and decorated
   @param fullscreen 1 = no fullscreen and undecorated
   @param fullscreen 2 = light fullscreen
   @param fullscreen 3 = heavy fullscreen
   */ private void setFullScreen(int fullscreen){
      if(!gDev.isFullScreenSupported() ){return;}
      
      switch(fullscreen){
         case 0 : windowFrame.setUndecorated(false); break;
         case 1 : windowFrame.setUndecorated(true); break;
         case 2 : windowFrame.setUndecorated(true); windowFrame.setExtendedState(JFrame.MAXIMIZED_BOTH); break;
         case 3 : windowFrame.setUndecorated(true); gDev.setFullScreenWindow(windowFrame);  break;
      }
   }
   /** private void fadeImage(Graphics g, float alpha)
    * this method is only for gameEngine and does not have usage in other places
    * 
    * */ private void fadeImage(Graphics g, float alpha){
      Graphics2D g2d = (Graphics2D)g;
      g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha) );
   }
   BufferedImage convertToGrayScale(BufferedImage img){
         BufferedImage processedImage = img;
         int rgb, r, g, b, grayLevel, gray;
         
         //transform all pixel
         for(int x = 0; x < processedImage.getWidth();x++){
            for(int y = 0; y < processedImage.getHeight(); y++){
               //process 1 pixel to gray
               rgb = processedImage.getRGB(x, y);
               r = (rgb >> 16) & 0xff; g = (rgb >> 8) & 0xFF; b = (rgb & 0xFF);
               
               grayLevel = (r + g + b) / 3;
               gray = (grayLevel << 16) + (grayLevel << 8) + grayLevel;
               
               processedImage.setRGB(x, y, gray);
            }
         }
         
         return processedImage;
      }
   //unused method that used for run method. that maybe needed in the future
   void init(){}//not used || if there's nothing wrong in stateManager preInit before run
   void prerender(){}//not used
   
   
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 
hadezbladez (276 views)
2018-11-16 13:46:03

hadezbladez (153 views)
2018-11-16 13:41:33

hadezbladez (286 views)
2018-11-16 13:35:35

hadezbladez (68 views)
2018-11-16 13:32:03

EgonOlsen (2123 views)
2018-06-10 19:43:48

EgonOlsen (2147 views)
2018-06-10 19:43:44

EgonOlsen (1360 views)
2018-06-10 19:43:20

DesertCoockie (1955 views)
2018-05-13 18:23:11

nelsongames (1598 views)
2018-04-24 18:15:36

nelsongames (2245 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!