Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (482)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (550)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Help! I R n00b  (Read 3074 times)
0 Members and 1 Guest are viewing this topic.
Offline princec

JGO Kernel


Medals: 362
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Posted 2006-12-07 23:43:42 »

2,255 bytes. That's how big my 4K entry is so far and all it does is open a double buffered window and listen for mouse and keyboard input.

How the hell do you guys get so much into 4K???

Cas Smiley

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  
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;

/**
 * 4k entry
 */


/**
 * @author foo
 */

public class G implements MouseMotionListener, MouseListener, KeyListener, WindowListener{

   static Frame w;
   static G g;
   static BufferStrategy s;

   public static void main(String[] a) {

      g = new G();
     
      // Create our frame
     w = new Frame("4k");
      w.addWindowListener(g);
      w.addKeyListener(g);
      w.addMouseListener(g);
      w.addMouseMotionListener(g);

      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
      w.setBounds((d.width - 320) / 2, (d.height - 320) / 2, 320, 320);
      w.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB), new Point(0, 0), ""));

      // Show our window
     w.setVisible(true);

      // Create a general double-buffering strategy
     w.createBufferStrategy(2);
      s = w.getBufferStrategy();

      // Main loop
     for (;;) {
         long t = System.nanoTime();

         // Render single frame
        do {
            do {
               // Get a new graphics context every time through the loop
              // to make sure the strategy is validated
              Graphics g = s.getDrawGraphics();

               // Draw background
              g.setColor(Color.black);
               g.fillRect(0, 0, 320, 320);

               // Dispose the graphics
              g.dispose();

               // Repeat the rendering if the drawing buffer contents
              // were restored
           } while (s.contentsRestored());

            // Display the buffer
           s.show();

            // Repeat the rendering if the drawing buffer was lost
        } while (s.contentsLost());

         // Sync to frame rate, always sleep at least 1ms
        long n = t + 1;
         while (n > t && (n - t) < 2000000) {
            try {
               Thread.sleep(1);
            } catch (InterruptedException e) {
            }
            n = System.nanoTime();
         }
      }
   }

   public void mouseDragged(MouseEvent e) {
   }

   public void mouseMoved(MouseEvent e) {
      mouseDragged(e);
   }

   public void mouseClicked(MouseEvent e) {
   }

   public void mousePressed(MouseEvent e) {
   }

   public void mouseReleased(MouseEvent e) {
   }

   public void mouseEntered(MouseEvent e) {
   }

   public void mouseExited(MouseEvent e) {
   }

   public void keyTyped(KeyEvent e) {
   }

   public void keyPressed(KeyEvent e) {
   }

   public void keyReleased(KeyEvent e) {
   }

   public void windowOpened(WindowEvent e) {
   }

   public void windowClosing(WindowEvent e) {
      System.exit(0);
   }

   public void windowClosed(WindowEvent e) {
   }

   public void windowIconified(WindowEvent e) {
   }

   public void windowDeiconified(WindowEvent e) {
   }

   public void windowActivated(WindowEvent e) {
   }

   public void windowDeactivated(WindowEvent e) {
   }

}

Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #1 - Posted 2006-12-07 23:55:26 »

2,255 bytes. That's how big my 4K entry is so far and all it does is open a double buffered window and listen for mouse and keyboard input.


Is that in a compressed JAR?  My final class sizes are around 10-11k.  Then you jar it up.  Then run it through Proguard.  This will shrink to the final 4k size.

Also, did you check out the WiKi: http://wiki.java.net/bin/view/Games/4KGamesDesign

One thing I can point out right away.  Don't use WindowListener to exit.  Use setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); instead.  Every method you create puts a method signature in the class file.  With WindowListener, that is 7 signatures.  The other way has none, just a call.

Quote
1  
2  
3  
4  
5  
   static Frame w;
   static G g;
   static BufferStrategy s;

   public static void main(String[] a) {

Class level variables take more space than instance variables.

Quote
1  
2  
3  
4  
  public static void main(String[] a) {
    Frame w;  //Should have your class extend JFrame instead of creating a Frame reference
   G g = new G();
    BufferStrategy s;
This should be smaller.

Offline princec

JGO Kernel


Medals: 362
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #2 - Posted 2006-12-08 00:10:09 »

I learn fast Wink
1,853 bytes to achieve the same results:
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  
import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

/**
 * 4k entry
 */


/**
 * @author foo
 */

@SuppressWarnings("serial")
public class G extends JFrame {

   BufferStrategy s;
   
   public static void main(String[] a) {
      new G();
   }
   
   G() {
      super("4k");

      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
      setBounds((d.width - 320) / 2, (d.height - 320) / 2, 320, 320);
      setCursor(Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB), new Point(0, 0), ""));

      // Show our window
     setVisible(true);

      // Create a general double-buffering strategy
     createBufferStrategy(2);
      s = getBufferStrategy();
     
      // Enable window, mouse, and keyboard events
     enableEvents
         (
               AWTEvent.MOUSE_EVENT_MASK
            |   AWTEvent.MOUSE_MOTION_EVENT_MASK
            |   AWTEvent.KEY_EVENT_MASK
         );
     
      setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      run();
   }
   
   void run() {
      // Main loop
     for (;;) {
         long t = System.nanoTime();
         
         logic();

         // Render single frame
        do {
            // The following loop ensures that the contents of the drawing
           // buffer
           // are consistent in case the underlying surface was recreated
           do {
               render();
            } while (s.contentsRestored());

            // Display the buffer
           s.show();

            // Repeat the rendering if the drawing buffer was lost
        } while (s.contentsLost());

         // Sync to frame rate, always sleep at least 1ms
        long n = t + 1;
         while (n > t && (n - t) < 2000000) {
            try {
               Thread.sleep(1);
            } catch (InterruptedException e) {
            }
            n = System.nanoTime();
         }
      }
   }

   synchronized void logic() {
   }
   
   void render() {
      // Get a new graphics context every time through the loop
     // to make sure the strategy is validated
     Graphics g = s.getDrawGraphics();

      // Draw background
     g.setColor(Color.black);
      g.fillRect(0, 0, 320, 320);

      // Game rendering here...
     
      // Dispose the graphics
     g.dispose();
   }
   
   @Override
   protected synchronized void processKeyEvent(KeyEvent e) {
   }
   
   @Override
   protected synchronized void processMouseEvent(MouseEvent e) {
   }
   
   @Override
   protected synchronized void processMouseMotionEvent(MouseEvent e) {
   }
   
}


Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline princec

JGO Kernel


Medals: 362
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #3 - Posted 2006-12-08 00:12:34 »

Think that's a decent enough framework for a whole smorgasbord of silly little games. I wonder what I'll come up with with those lovely 2,200 remaining bytes.

Has anybody any knowledge about sound effects? I'd quite like to do some sounds - notably missing from 4K entries by and large - you know, just simple saws and pulse waves through waveout.

Cas Smiley

Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #4 - Posted 2006-12-08 00:31:13 »

You can do all 3 types of events with processEvents... just check the type of the event and do the matching actions.

You can also do everything in the c'tor. That leaves you with: c'tor, main (which calls the c'tor) and processEvents.

You also might want to try different obfuscators (jarg, proguard, joga etc), zippers (7zip, kzip, bjwflate) and if you have more than one file you also should try zipmix (can be also found at the pngout page).

For fuzetsu I used: jarg->joga... once wtih kzip... once with bjwflate -> zipmix.

弾幕 ☆ @mahonnaiseblog
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2006-12-08 01:34:20 »

What oNyx said, and compile with -g:none.

Further, you might change your mind about spending so many bytes only to remove the mouse-pointer!

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #6 - Posted 2006-12-08 01:53:08 »

>compile with -g:none

Don't obfuscators strip all that already?

>Further, you might change your mind about spending so many bytes only to remove the mouse-pointer!

Thanks to Java2D's PLATFORM SPECIFIC way to create images... it needs to be cleared. Otherwise its either filled with opaque gray (Mac) or random bullcrap (Linux).

Also... on Mac you may lose your custom cursor. Whenever you regain focus you have to reset the cursor to the desired one. Gah! (Hadn't enough room for that rather massive fix.)

I still think that it was really bad that they decided against having a pre defined empty cursor.

So, my (obvious) advice: If your game is fine with the cursor, keep it.

弾幕 ☆ @mahonnaiseblog
Offline woogley
« Reply #7 - Posted 2006-12-08 02:42:50 »

you can also save bytes by just calling setLocation(10,10) rather than centering the window..

also calling show() instead of setVisible(true) will save a few bytes as well.
Offline EnderGT

Junior Member





« Reply #8 - Posted 2006-12-08 03:42:18 »

you can also save bytes by just calling setLocation(10,10) rather than centering the window..

what about calling setLocationRelativeTo(null)? not sure how it compares to your way, but it's still smaller than the way he's doing it and it does what he wants.
Offline princec

JGO Kernel


Medals: 362
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #9 - Posted 2006-12-08 10:55:40 »

Keep 'em coming, you're only helping me to win  Cool

Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline EnderGT

Junior Member





« Reply #10 - Posted 2006-12-08 14:26:10 »

Keep 'em coming, you're only helping me to win  Cool

Based on the code you've shown us, I'm not too worried about that happening   Grin

Besides, you've still got to come up with a good game idea. Good luck.
Offline g666

Junior Member





« Reply #11 - Posted 2006-12-08 18:56:39 »

Based on the code you've shown us, I'm not too worried about that happening   Grin

Besides, you've still got to come up with a good game idea. Good luck.

titan attacks 4k would be nice Smiley

desperately seeking sanity
Offline f.l.x

Senior Member


Projects: 3


there is no place like 127.0.0.1


« Reply #12 - Posted 2006-12-11 19:16:27 »

i have found that sometimes if/else sentences are smaller than '?' inline comparations, specially comparing ints, don't ask me why  Roll Eyes

On sounds, i'm working a litle one procedural, if i manage to get some more time to play with it (just got my first job as junior java programmer and i have to catch up  Grin ) i'll post something soon.

Litterarum radices amaras, fructus dulces
http://flx.proyectoanonimo.com
figth spam!
Offline nonnus29

Senior Member




Giving Java a second chance after ludumdare fiasco


« Reply #13 - Posted 2006-12-15 08:21:42 »

Quote
2,255 bytes. That's how big my 4K entry is so far and all it does is open a double buffered window and listen for mouse and keyboard input.

How the hell do you guys get so much into 4K???

LOL! NooB! YOU SUCK!!!!11

Check out the previous years threads, some people post source after the contest (miners4k hint hint).
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (14 views)
2014-08-22 19:31:30

atombrot (28 views)
2014-08-19 09:29:53

Tekkerue (25 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (15 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (61 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!