Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Networking & Multiplayer / Re: NIO problem sending data when multiple requests in the queue on: 2014-03-15 18:59:50
ok thanks for the details guys. I've decided to add a termination character to the end of each packet so I can detect if I have multiple packets being sent to the read.
2  Game Development / Networking & Multiplayer / NIO problem sending data when multiple requests in the queue on: 2014-03-15 13:24:29
I have my networking code set up so if we wish to send data we simply add it to the queue and mark the selectionkey for OP_WRITE

1  
2  
3  
4  
5  
6  
7  
8  
   public void send( byte[] message) throws IOException{
      if(this.socketChannel == null){
         throw new SocketException("Socket connection has closed!");
      }
     
      messages.add(message);
      selectionKey = selectionKey.interestOps(SelectionKey.OP_WRITE);
   }


in my handleWrite() method I simply loop over all the data we find in the queue and send it

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
   public void handleWrite(SelectionKey key) throws IOException
   {
      this.socketChannel = (SocketChannel) key.channel();
     
                //error checking removed to conserve space
     
      while (!messages.isEmpty()) {
         writeBuffer = ByteBuffer.wrap(messages.peek());
         
         if(socketChannel.write(writeBuffer) == 0){
            break;
         }
           
         if (writeBuffer.remaining() > 0) {
            break;
         }
         writeBuffer.compact();
         messages.remove(); //everything went well so remove head
     }
     
      selectionKey.interestOps(SelectionKey.OP_READ);      
   }


in my read method I simply take the bytes I get and transform it into a packet object

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
   public Packet handleRead(SelectionKey key, PacketFactory factory) throws IOException{
     
      this.socketChannel = (SocketChannel) key.channel();
     
           //error checking removed to conserve space
     
      int bytesRead = 0;
      readBuffer.clear();
     
      try {
         bytesRead = this.socketChannel.read(readBuffer);
         readBuffer.flip();
      } catch (IOException e) {
         log.message("connection closed at: " + this.socketChannel.getRemoteAddress(), Log.INFO);
         key.cancel();
         close();
      }
     
      //error checking removed to conserve space
     byte[] data = readBuffer.array();
   
      return factory.buildPacket(transformer.getType(data), transformer.getData(data));
   }


my problem arises from the fact of when there are mutliple messages in the queue. So lets say I have requested to send a packet 3 times [3]:FOO:[3]:BAR, [3]:FOO:[3]:BAR, [3]:FOO:[3]:BAR. I would expect my read to be called 3 times and return 3 FOO packets with each one having the arguments BAR. Instead it returns 1 big packet FOO with the arguments BAR,FOO,BAR,FOO,BAR.

whats the best way to split this up? Have I done something wrong with my reading and writing or is this behavour to be expected? should I instead change my protocol so packets have a termination character so we know when a new packet begins?
3  Game Development / Networking & Multiplayer / correct way to create a packetHandler class on: 2013-12-26 00:48:18
In my game every packet type has a enum value assigned to it (so when packets come in between the server and the client I can tell what the packet is meant for e.g. is it a login packet, or a ping packet). My server currently has a class to handle the packets that the server gets sent:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public class PacketHandler {
   
   public static void HandlePacket(byte[] data, InetAddress ipAddress, int port){
      String message = PacketUtils.GetDataFromByteStream(data);
      Packets type = PacketUtils.GetPacketIdFromByteStream(data);
     
      switch(type) {
         case INVALID:
                                //do whatever if we get an invalid packet
           break;
         case ALIVE:
                                //do whatever if we get an alive packet
           break;
         case DISCONNECT:
                                //do whatever if we get an disconnect packet
           break;
         default:
            break;
      }
   }


in the switch statement we will perform whatever action we need to depending on what packet we recieve. My problem is this is really going to be hard to maintain. Is there a better and cleaner way of doing this. Having all of this code in one file is going to make this file really big.

Any inputs into the matter would be amazing. I really want to know what are some cleaner ways of doing this expecally from a OOPS stand point.
4  Game Development / Newbie & Debugging Questions / java2d drawImage confusion on: 2013-12-19 00:48:37
In my program I am drawing an image with

1  
g.drawImage(image, 32, 32,  96  , 96, 11, 0, 13, 2, null);


I would expect this to start drawing the part of the image specified at {11,0}, {13,2} in the location in my program at {32,32} and finish at {96,96} however it does not! the srcx and srcy results act normally and they do grab the image that they need to. However something is going wrong with the dcx and the dcy parameters

http://i.imgur.com/gw0qVAO.png

as we can clearly see for a start the image is 32 by 32 (I would expect a 64 by 64 square) and it is definitely not  being drawn from {32,32}  {96,96} can someone shed some light on whats going wrong?
5  Game Development / Networking & Multiplayer / Re: kryonet hide methods from serialization on: 2013-04-03 14:21:28
sorry I didn't reply to this topic sooner. I didn't think I needed to post the error as I knew it was definitely to do with how the class was being registered (as when I removed the helper methods the error disappeared).

anyway I've sorted the probem now by going the simple route and getting rid of the methods.
6  Game Development / Networking & Multiplayer / kryonet hide methods from serialization on: 2013-04-02 13:54:34
I have some packets that have helper methods for initalizing such as this

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
   public String reason = "No reason give. Please contact the development team if you see this message!";
   
   private String[] reasonList =
                        {"Invalid Username.",
                         "Invalid Password.",
                         "This account has been banned." ,
                         "The server is currently in maintenance mode."
                        };
   
   public Packet4LoginFailed(int index){
      reason = reasonList[index];
   }


I have registered the classes in the right order in both the server and the client (the registration is done in a shared class). However it errors. If I removed the constructor the packet no longer errors. I have a feeling this is to do with how kryo serializes the class. Is there anyway I can hide the constructor from serialization. I'm presuming there is some kind of annotation, however I couldn't find anything in the kryonet google project page.

edit
the constructor is just 1 example of when I'd like to hide a method Some of my packets have other helper methods to make sure I initialize it properly.
7  Java Game APIs & Engines / OpenGL Development / LWJGL Rectangle Textures (new to openGl) on: 2012-12-29 23:06:33
ok so I'm trying to make a spritesheet and I'm at the point where I'm trying to only get part of the spritesheet drawn

   
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  
public SpriteSheetSprite(Texture texture, int x, int  y, int maxCol, int maxRows){
      this.texture = texture;
      this.x = x;
      this.y = y;
      this.maxCol = maxCol;
      this.maxRows = maxRows;
           
      glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture.getTextureID());
      //texture.bind();
  }

   @Override
   public void Render() {
      glBegin(GL_QUADS);
         glTexCoord2f(0,0);
         glVertex2i(x,y);//top left
        glTexCoord2f(32,0);
         glVertex2i(x + getWidth(),y);//top right
        glTexCoord2f(32,32);
         glVertex2i(x+ getWidth(),y + getHeight());//bottem right
        glTexCoord2f(0,32);
         glVertex2i(x,y + getHeight());//bottem left
     glEnd();
     
   }


Using the above code my quad gets an image which is the spritesheet stretched over and over, that is to be expected as we're no longer wrapping the texture around the quad from 0,0 to 1,1

The problem I'm having is that when I switch OpenGl from GL_TEXTURE_2D to GL_TEXTURE_RECTANGLE_ARB the texture no longer wants to be applied to the quad (I just get a white rectangle). Here is my code for initalizing open gl

1  
2  
3  
4  
5  
6  
7  
   private void startOpenGl(){
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glOrtho(0,Settings.WIDTH, Settings.HEIGHT,0,1,-1);
      glMatrixMode(GL_MODELVIEW);
      glEnable(GL_TEXTURE_RECTANGLE_ARB);
   }


any help on the matter would be awesome
8  Java Game APIs & Engines / Engines, Libraries and Tools / slick2d and tiled on: 2011-06-02 14:00:52
I want to start messing around with slick2d however I'm having issues, I've set up and I can run a slick2d program fine however I can't seem to load a map.

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  
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;

public class Game extends BasicGame
{
   private TiledMap grassMap;
   
    public Game()
    {
        super("Zidsal");
    }

    public static void main(String[] arguments)
    {
        try
        {
            AppGameContainer app = new AppGameContainer(new Game());
            app.setDisplayMode(1000, 1000, false);
            app.start();
        }
        catch (SlickException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void init(GameContainer container) throws SlickException
    {
       grassMap = new TiledMap("data/grassMap.tmx");
    }

    @Override
    public void update(GameContainer container, int delta) throws SlickException
    {
    }

    public void render(GameContainer container, Graphics g) throws SlickException
    {
       grassMap.render(0, 0);
    }
}


in my data folder I have my map grassMap.tmx and the tileset grass.png however when I try and run the program I get this exception
Quote
Jun 02 12:57:56 BST 2011 ERROR:Unsupport tiled map type: base64,zlib (only gzip base64 supported)
org.newdawn.slick.SlickException: Unsupport tiled map type: base64,zlib (only gzip base64 supported)

any help would be great as I have no idea whats going on.
9  Game Development / Newbie & Debugging Questions / Re: swing and java2d [Applet] on: 2011-02-16 00:24:26
EDIT
fixed it /facepalm had to remove the paint(g) my failt >.< now if anyone could help me with the second problem of giving me an idea of how I'd draw a swing compoment onto the game screen would be great
What do you mean exactly? Do you want to put a button in the middle of your GameDraw?

something along those lines a button, JTextField any kind of componentjust so I can see how its done as its not as simple as adding it to the Jframe >.<
10  Game Development / Newbie & Debugging Questions / Re: swing and java2d [Applet] on: 2011-02-15 18:00:32
thanks for the help so far guys just got back from school and starte trail and error. I've gotten to this stage

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  
package Client.Gui;

import Client.Gui.GameDraw;
import javax.swing.*;
import java.awt.*;

public class GameApplet extends JApplet{

// private GameDraw gd;
private JTextField chatInput;  
 private JTextField chatBox;


   public void init() {
        createGUI();
       // gd.startGame();
  }

   public void paint( Graphics g ) {

   }

   public void stop(){

   }

   public void destroy (){

   }

    private void createGUI()
  {

    Container c = getContentPane();
    c.setLayout( new BorderLayout() );

    //gd = new GameDraw();
  // c.add(gd, "Center");

    JPanel ctrls = new JPanel();  
    ctrls.setLayout(new GridLayout(2, 1, 1, 1));

    chatInput = new JTextField("Welcome to my test game");
    chatBox = new JTextField("test");

    ctrls.add(chatInput);
    ctrls.add(chatBox);
    c.add(ctrls, "Center");

  }  
}


I've commented the entire game panel out so on the applet it should just be drawing the jpanel and those JTextFields, however the error is still happerning. Any help would be nice.

EDIT
if it helps to know this is the only class which touches chatInput, chatBox and ctrls JPanel

EDIT
fixed it /facepalm had to remove the paint(g) my failt >.< now if anyone could help me with the second problem of giving me an idea of how I'd draw a swing compoment onto the game screen would be great
11  Game Development / Newbie & Debugging Questions / swing and java2d [Applet] on: 2011-02-14 22:26:19
well I'm trying to program in a simple gui into my game and I've kind of hit a snag which is stopping me from progressing.
In my game I have 2 JFrame (just like in KGPIJ wormchase example). In the first Frame I have the main game screen in which you can see the eye saw game and below that we have the second Jframe which has two JTextField's. However when the game loads up the Textfields are not drawn properly and when the game is resized they mess up again. It is only after I give them focus that they draw properly. I'm presuming I have to call repaint for the compoment but I'd have no idea how to do this. Its also annoying me because in Oreillys code he does not have these issues (His program runs perfectly). Below is a screenshow to show the issue.



my code for this can be seen below
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
 private void createGUI()
  {

    Container c = getContentPane();
    c.setLayout( new BorderLayout() );

    gd = new GameDraw();
    c.add(gd, "Center");

    JPanel ctrls = new JPanel();  
    ctrls.setLayout(new GridLayout(2, 1, 1, 1));

    chatInput = new JTextField("Welcome to my test game");
    chatBox = new JTextField("test");

    ctrls.add(chatInput);
    ctrls.add(chatBox);
    c.add(ctrls, "South");

  }  


createGui gets called in  public void init() {}

Secondaly so I do not have to keep bugging you guys as I know I'll have issues with this would someone be able to give me advice in adding in swing compoments to the Jframe in which the game is drawn. Is what I'm aiming for here is a character window. The issue I'm having here is that if I try and add a compoment to the games JFrame it just takes up the entire screen >.<.

12  Game Development / Newbie & Debugging Questions / Re: Recording FPS help on: 2011-02-12 23:15:37
thank you very much <3
13  Game Development / Newbie & Debugging Questions / Recording FPS help on: 2011-02-12 22:59:42
hi their I've been lurking on these forums for the last week or so and today I decided to make an account after being stuck with these issues (yay for leeching). I'm trying to make a game with java2d and I'm having issues recording the fps.

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  
//variables used for finding fps
   private long prevStatsTime;
    double fps = 0;
    int frameCount = 0;
    long totalElapsedTime = 0L;

    public void run(){
        .....
        prevStatsTime = System.nanoTime();
        while(running = true){
             .....
             beforeTime = System.nanoTime();    
        }
        ......
        recordInfo();
    }

    public void recordInfo()
    {
         long realElapsedTime = System.nanoTime() - prevStatsTime;


        frameCount++;
        totalElapsedTime += realElapsedTime;
        if(totalElapsedTime > 0)
        fps = (((double)frameCount/totalElapsedTime)*1000000000L);
        prevStatsTime = System.nanoTime();
    }

    public void drawInfo(Graphics g)
    {
         ...
        g.drawString("FPS: " + fps, 0, 30);
    }


the issue I'm having is that the fps slowly decreases so obviously its not recording the fps >.< any help would be really great I would prefer a nudge in the right direction rather then you flat out explaining it to me if you wouldn't mind.

many thanks
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (37 views)
2014-04-15 18:08:23

BurntPizza (33 views)
2014-04-15 03:46:01

UprightPath (49 views)
2014-04-14 17:39:50

UprightPath (31 views)
2014-04-14 17:35:47

Porlus (48 views)
2014-04-14 15:48:38

tom_mai78101 (69 views)
2014-04-10 04:04:31

BurntPizza (129 views)
2014-04-08 23:06:04

tom_mai78101 (229 views)
2014-04-05 13:34:39

trollwarrior1 (193 views)
2014-04-04 12:06:45

CJLetsGame (200 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!