Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML book source code port  (Read 8571 times)

0 Members and 1 Guest are viewing this topic.

kalimatas

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
SFML book source code port
« on: November 03, 2014, 10:04:52 pm »

Hello guys!

For the last seven months I've been working on porting C++ source code of the SFML book to Java. And now I’m almost done! You can find the code on github.

I'm almost done, because new Mac OS X release and Java NIO package definitely don't want me to finish, and here I really need your help. I have several problems now (most of them in Network chapter), which I don't know (or I just missing something obvious) how to solve, because of the lack of Java knowledge.

One the problems is - bloom effect (which is done by means of shaders) doesn't work on Mac OS X 10.10, but it worked on 10.9.

The other big problem is - network packets from the last chapter come with big delays. This part is the most difficult, because JSFML doesn't implement SFML network library, but relays on native Java implementation.

The list of other issues you can find here.

So I'm asking you to help me to finish my work. I've been struggling with these problems for the last two weeks and couldn't resolve out.

Thank you in advance! I'll really appreciate any help!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: SFML book source code port
« Reply #1 on: November 03, 2014, 10:16:14 pm »
Wow, this looks really promising! Thanks a lot for porting our book's code to Java and sharing your work! :)

Could you be more specific about the problems? And please focus on one topic per thread, don't hesitate to open further ones if necessary. But keep in mind that for Java-specific questions, like using sockets in the most efficient way, other platforms such as Java forums or StackOverflow can probably help better.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

pdinklag

  • Sr. Member
  • ****
  • Posts: 330
  • JSFML Developer
    • View Profile
    • JSFML Website
Re: SFML book source code port
« Reply #2 on: November 05, 2014, 08:02:30 am »
Looks very good indeed!

I have not read the SFML book so I'm not into the examples at all. I also never really thought of the "use case" of translating C++ SFML applications to Java, so there might indeed be some trouble here and there. I'm actually very pleasantly surprised that the only issues seem to occur in the networking part, which I didn't even port. :)

Java's network library works pretty well, but I'm not sure how close or different it is from the SFML approach. I don't really have the time these days to look through your whole networking code, so as Nexus put it, you should be a little more specific with the exact issues and give us proper introduction - the "minimal reproducible example" approach. ;)

Though he might be right, your questions will most likely be specific to Java networking. That's not exactly on-topic in this forum, but I'd still be willing to help. After all, networking is a part of SFML, and the reason I didn't port SFML's networking features is because I believe the Java networking comes with all you need. Might as well prove that in this forum. :)
JSFML - The Java binding to SFML.

kalimatas

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: SFML book source code port
« Reply #3 on: November 05, 2014, 09:17:32 am »
Thanks for the replies!

I'll definitely try to ask for help on some Java specific resources, but I thought it would be more productive to communicate with people with SFML background  :)

Later I'll open a new thread on my networking problem where I'll describe it in more details.

Thanks again!

cx3

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: SFML book source code port
« Reply #4 on: February 27, 2015, 08:16:08 am »
Your code... OMG! Something i always needed. Really nice, elegant and clear. Very easy to extend! Artwork

cx3

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: SFML book source code port
« Reply #5 on: March 06, 2015, 11:39:43 pm »
Please join my posts :)

Basing on Chapter 5 of Kalimatas code, I am trying to add walls that does not to allow to cross them.

/**
/**
 * @(#)GameMap.java
 *
 *
 * @author cx3
 * @version 1.00 2015/3/6
 */



package com.github.kalimatas.c05_States;

import org.jsfml.graphics.Texture;
import org.jsfml.graphics.RenderWindow;
import org.jsfml.system.Vector2f;
import org.jsfml.graphics.Sprite;
import org.jsfml.system.Vector2i;
import org.jsfml.graphics.ConstTexture;
import org.jsfml.graphics.RenderTarget;
import org.jsfml.graphics.RenderStates;
import org.jsfml.graphics.Transform;
import org.jsfml.system.Time;

import java.nio.file.*;
import java.util.ArrayList;

import java.io.IOException;
import java.io.*;
//import java.util.HashMap;

import java.nio.file.Paths;
import java.io.FileInputStream;

import java.lang.Number;


class Block
{
        public char     type;
        public Vector2i vec;
       
        public Block(){type='.'; vec = new Vector2i(0,0); }
       
        public Block(char btype, Vector2i vector){ type=btype; vec=vector;}
       
        public char     getLeft() { return type;  }
        public Vector2i getRight(){ return vec; }
}



public class GameMap //extends SceneNode//implements org.jsfml.graphics.Drawable
{
        Texture                      wallBlock;
        ArrayList<Block>     gameMap = new ArrayList<Block>();;
        RenderWindow             window;


        public
                GameMap(){}


public
        GameMap(String  mapPath, String  blockImagePath)
    {
        try
            {
                wallBlock = new Texture();
                wallBlock.loadFromStream(new FileInputStream(new File(blockImagePath)));
           
                File file = new File(mapPath);
               
                    FileInputStream fis         = null;
                    BufferedInputStream bis = null;
                    DataInputStream dis         = null;
       
           
                        fis = new FileInputStream(file);
                       
                        // Here BufferedInputStream is added for fast reading.
                        bis = new BufferedInputStream(fis);
                        dis = new DataInputStream(bis);
                       
                        String
                                xs   = dis.readLine()
                                ys   = dis.readLine();
                                       
                        int
                                x = Integer.parseInt(xs),
                                y = Integer.parseInt(ys);
                       
                        for (int i=0; i<x; ++i)
                        {
                                String s = dis.readLine();
                               
                            for (int j=0; j<s.length(); ++j)
                                gameMap.add
                                ( new Block
                                          (
                                               s.charAt(j),
                                                   new Vector2i(0+i*128, 0+j*72)
                                          )
                                 );
                              }
                       
                              // dispose all the resources after using them.
                              fis.close();
                              bis.close();
                              dis.close();
       
            } catch (Exception e){
           
              e.printStackTrace();
            }
    String s2 = "LOADED: "+gameMap.size()+" blocks";
    try{throw new RuntimeException(s2);}catch(Exception e){e.printStackTrace();}
    }
   
    /*
    public void setWindow(RenderWindow rewi)
    {
        window = rewi;
    }*/

   
   
    /*@Override
    public final void draw(RenderTarget target, RenderStates states)
    {
        // Apply transform of current node
        RenderStates rs = new RenderStates(states, Transform.combine(states.transform, getTransform()));

        // Draw node and children with changed transform
        drawCurrent(target, rs);
        drawChildren(target, rs);
    }*/

 
 
  /*
    //@Override
    public void draw(RenderTarget target, RenderStates states)
    {
        for (Block block : gameMap)
        {
                if (block.getLeft()!='.')
                {
                        Sprite s = new Sprite(wallBlock);
                        Vector2i v = block.getRight();
                        s.setOrigin(new Vector2f(v.x, v.y));
                        s.setPosition(new Vector2f(v.x, v.y));
                       
                        target.draw(s);
                        /*RenderStates rs = new RenderStates
                                (
                                        states,
                                        Transform.combine
                                                (
                                                        states.transform, getTransform()
                                                )
                                );*/

                }
        }
    }*/
   
    public void draw2(RenderWindow r)
    {
        for (Block block : gameMap)
        {
                if (block.getLeft()!='.')
                {
                        Sprite s = new Sprite(wallBlock);
                        Vector2i v = block.getRight();
                        s.setOrigin(v.x, v.y);
                        s.setPosition(v.x, v.y);
                       
                        r.draw(s);
                        /*RenderStates rs = new RenderStates
                                (
                                        states,
                                        Transform.combine
                                                (
                                                        states.transform, getTransform()
                                                )
                                );*/

                }
        }
    }
   
}
   
 

This is one of various tests i made, none of them works. Even if i had working class that displays map well, where in game loop while(window.isOpen()) should I write code to be run correctly with all scenes? I feel myself like i saw programming first time, lol. Please help :)

edit:
After some editing of code, at World`s class (World.java) in method adaptPlayerVelocity(..)

i wrote as a last line: 
Code: [Select]
Application.GAME_MAP.draw2(this.window);
Application`s has field GAME_MAP which is public static GameMap.

Result: draws only one block in left up corner of scrollable world, instead of correct position that are stored in text file:

Code: [Select]
7
5
...x...
...x...
...x...
...x...
...x...

7 - seven sprites in row, only x is to be drawn
5 - five rows

--------

I want achieve an effect that world is scrolled but loaded map is constant - background is animated and does not influence for player or enemies - I will implement enemies in next years without a sleep ;p
« Last Edit: March 07, 2015, 12:32:28 am by cx3 »

 

anything