Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
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  
  Bitmask vs. booleans?  (Read 3310 times)
0 Members and 1 Guest are viewing this topic.
Offline appel

JGO Wizard


Medals: 51
Projects: 4


I always win!


« Posted 2005-12-27 02:40:35 »

I'm wondering if the class file is less in bytes if I were to implement the player controls (SHOOT,FORWARD,REVERSE,TURN_RIGHT,TURN_LEFT) in a single bitmask value (1,2,4,8,16) rather than defining 5 boolean variables as class member vars?

Which one is better, for the 4k games?

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline Anon666

Junior Devvie




aka Abuse/AbU5e/TehJumpingJawa


« Reply #1 - Posted 2005-12-27 03:28:05 »

For mouse and key input, I use this :-

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
   static final int MOUSE_MODIFIERS = 0;
   static final int MOUSE_X = 1;
   static final int MOUSE_Y = 2;
   
   static final int KEY_START_INDEX = 0; //because no keys on the keyboard map to ASCII 0,1 or 2 this can be safely set to 0
   int [] controls = new int[256+KEY_START_INDEX];

   public final void processEvent(AWTEvent e)
   {
      int [] ctrls = controls;
      if(e instanceof KeyEvent)
      {
         ctrls[(((KeyEvent)e).getKeyCode()&0xFF) +KEY_START_INDEX] = e.getID()&1
      }
      else
      {
         MouseEvent mouse = (MouseEvent)e;
         ctrls[MOUSE_MODIFIERS]=mouse.getModifiersEx();
         ctrls[MOUSE_X]=mouse.getX();
         ctrls[MOUSE_Y]=mouse.getY();
      }
   }
Offline appel

JGO Wizard


Medals: 51
Projects: 4


I always win!


« Reply #2 - Posted 2005-12-27 04:12:17 »

I was thinking more on the lines of:

(functional)
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  
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test extends Canvas implements KeyListener {
   private static final int
   FIRE = 1,
   FORWARD = 2,
   REVERSE = 4,
   TURN_LEFT = 8,
   TURN_RIGHT = 16;

   private int ACTION = 0;

   public static void main(String[] args) {
      new Test();
   }
   
   public Test() {
      JFrame w = new JFrame("Test");
      JPanel p = (JPanel)w.getContentPane();
      p.setSize(640,480);
      p.add(this);
      w.setBounds(0,0,640,480);
      w.setVisible(true);
      w.setResizable(false);
      w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      addKeyListener(this);
      requestFocus();
     
      createBufferStrategy(2);
      BufferStrategy strategy = getBufferStrategy();
      Graphics2D g = (Graphics2D)strategy.getDrawGraphics();

     
     
     
      while(true) {
         g.setColor(Color.WHITE);
         g.fillRect(0,0,640,480);
         
         g.setColor(Color.BLACK);
         if((ACTION&FIRE)==FIRE)
            g.drawString("User is firing.",20,20);
   
         if((ACTION&FORWARD)==FORWARD)
            g.drawString("User is going forward.",20,40);
   
         if((ACTION&REVERSE)==REVERSE)
            g.drawString("User is reversing.",20,60);
   
         if((ACTION&TURN_LEFT)==TURN_LEFT)
            g.drawString("User is turning left.",20,80);
         
         if((ACTION&TURN_RIGHT)==TURN_RIGHT)
            g.drawString("User is turning right.",20,100);
         
         g.drawString("Action value is :"+ACTION+"\n\n",20,120);
     
         strategy.show();
         
         try {
            Thread.sleep(100);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
     
      }
   }
     
     
   public void keyTyped(KeyEvent arg0) {
   }

   public void keyPressed(KeyEvent arg0) {
      switch(arg0.getKeyCode()) {
      case KeyEvent.VK_SPACE: ACTION = ACTION | FIRE; break;
      case KeyEvent.VK_W: ACTION = ACTION | FORWARD; break;
      case KeyEvent.VK_S: ACTION = ACTION | REVERSE; break;
      case KeyEvent.VK_A: ACTION = ACTION | TURN_LEFT; break;
      case KeyEvent.VK_D: ACTION = ACTION | TURN_RIGHT; break;
      }
   }

   public void keyReleased(KeyEvent arg0) {
      switch(arg0.getKeyCode()) {
      case KeyEvent.VK_SPACE: ACTION = ACTION ^ FIRE; break;
      case KeyEvent.VK_W: ACTION = ACTION ^ FORWARD; break;
      case KeyEvent.VK_S: ACTION = ACTION ^ REVERSE; break;
      case KeyEvent.VK_A: ACTION = ACTION ^ TURN_LEFT; break;
      case KeyEvent.VK_D: ACTION = ACTION ^ TURN_RIGHT; break;
      }
   }
}

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline noblemaster

« JGO Spiffy Duke »


Medals: 20
Projects: 10


Age of Conquest makes your day!


« Reply #3 - Posted 2005-12-27 05:48:00 »

Mhh, in case you are considering Java 5.0, I would definitly go with enums:

1  
2  
3  
public enum Actions { SHOOT,FORWARD,REVERSE,TURN_RIGHT,TURN_LEFT };
Action action = Actions.SHOOT;
...


It's more readable and you will be less likely to make a mistake. Adding another action
such as FIRE_ROCKET is easy. And there will be no performance problems in case
you were wondering about that.

Online Riven
« League of Dukes »

« JGO Overlord »


Medals: 835
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2005-12-27 09:38:46 »

This is a 4k contest, not a OO design challenge.

Anyway, maybe the compiler is smart enough, but I'd think that
ACTION = ACTION | SHOOT;
takes more bytes than
ACTION |= SHOOT;

Give it a try.

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

JGO Wizard


Medals: 51
Projects: 4


I always win!


« Reply #5 - Posted 2005-12-27 10:01:45 »

This is a 4k contest, not a OO design challenge.

Anyway, maybe the compiler is smart enough, but I'd think that
ACTION = ACTION | SHOOT;
takes more bytes than
ACTION |= SHOOT;

Give it a try.

My same thought.

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline nonnus29

Senior Devvie




Giving Java a second chance after ludumdare fiasco


« Reply #6 - Posted 2005-12-27 13:43:31 »

No 1.5, see the rules thread.
Offline woogley
« Reply #7 - Posted 2005-12-27 15:37:46 »

No 1.5, see the rules thread.

and see the actual rules page: http://javaunlimited.net/contests/java4k.php
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #8 - Posted 2005-12-27 21:35:49 »

Try both ways in a test file and see which class file ends up being bigger.

Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #9 - Posted 2005-12-29 05:22:25 »

Third "General Tip" on the 4K Design Article is:

Quote
Don't use global variables. Global variables require special meta-data in the class to identify. Method-local variables, however, are only stack entries and cost nothing extra to use.

Using bitwise flags would probably cost you more space in extra operations than just using a few booleans. Remember, the stack is essentially free.

BTW: You're micro-optimizing. That's a bad thing to do. Try to get your file size down in other ways. Smarter algos, better compression, reduction in method count, etc, are all methods with a far better cost/return ratio.

Quote
public enum Actions { SHOOT,FORWARD,REVERSE,TURN_RIGHT,TURN_LEFT };

That's a REALLY bad idea. I almost guarantee that such a trick would bloat the class with tons of unnecessary meta-data. Just think, each of those names is going to be stored somewhere in the class file! That's at least 39 bytes that you're never getting back. Much more once you start creating references to these objects in your code.

Java Game Console Project
Last Journal Entry: 12/17/04
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.

trollwarrior1 (29 views)
2014-11-22 12:13:56

xFryIx (71 views)
2014-11-13 12:34:49

digdugdiggy (50 views)
2014-11-12 21:11:50

digdugdiggy (44 views)
2014-11-12 21:10:15

digdugdiggy (38 views)
2014-11-12 21:09:33

kovacsa (62 views)
2014-11-07 19:57:14

TehJavaDev (67 views)
2014-11-03 22:04:50

BurntPizza (64 views)
2014-11-03 18:54:52

moogie (80 views)
2014-11-03 06:22:04

CopyableCougar4 (80 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!