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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - Grimlen

Pages: [1]
1
Java / getPlayingOffset() always 0?
« on: November 26, 2013, 08:56:59 am »
For some reason when I play my sound, the getPlayingOffset() always returns 0.
Even though it's clearly played for more than a few seconds, it's still 0:

    @Override
    public void mousePressed(java.awt.event.MouseEvent e) {
            if(sound != null){
                Time t = jt.sound.getPlayingOffset();
                long sndPos = (t.asMilliseconds());
                System.out.println("POS: " + sndPos);
            }
    }
 

And yes, I can hear the sound perfectly fine.
Why does it display 0?

Was this function not implemented yet?

2
Java / Trying to draw a line using VectorArray
« on: November 21, 2013, 07:55:48 am »
Just trying to draw a simple line. Nothing seems to be drawing.
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jsoundeditor;

import java.awt.Point;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.swing.JFileChooser;
import org.jsfml.audio.Sound;
import org.jsfml.audio.SoundBuffer;
import org.jsfml.graphics.Color;
import org.jsfml.graphics.RectangleShape;
import org.jsfml.graphics.RenderWindow;
import org.jsfml.graphics.Vertex;
import org.jsfml.graphics.VertexArray;
import org.jsfml.system.Vector2f;
import org.jsfml.window.VideoMode;
import org.jsfml.window.event.Event;

/**
 *
 * @author Taylor
 */

public class JSoundEditor {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) throws IOException {
        RenderWindow window = new RenderWindow();
        window.create(new VideoMode(600, 225), "Hello!");
        window.setFramerateLimit(30);

        while(window.isOpen()){
            window.clear(Color.BLUE);
            window.display();
            VertexArray v = new VertexArray();
            Vector2f pos;
            pos = new Vector2f(1.0f, 1.0f);
            Vertex v1 = new Vertex(pos);
            pos = new Vector2f(5.0f, 1.0f);
            Vertex v2 = new Vertex(pos);
            v.add(v1);
            v.add(v2);
            window.draw(v);
            for(Event event : window.pollEvents()){
                if(event.type == Event.Type.CLOSED){
                    window.close();
                }
                else if(event.type == Event.Type.MOUSE_BUTTON_PRESSED){
                    System.out.println("test");
                }
            }
        }
    }
}
 

I figured it might have been because I was clearing the window as WHITE so I changed it to blue
so I could see the color of the line. Didn't work.
I can't even change the color of the Vertices.
IE:
v1.color = Color.RED;

I get an error:
"cannot assign a value to a final variable color"

So why isn't it drawing anything?

3
Audio / Altering the samples, possible?
« on: November 12, 2013, 08:38:18 am »
Is it at all possible to alter the samples found in a buffer/sound file?

For example.
Let's say I make an audio editor.
In this editor I can display a sound via its samples on a grid/graph.

In this graph, I can drag my mouse over samples, cut them out, and paste them at
a different location.

So if I were to record myself saying:
"Hello World!"

And I cut the samples where I started saying: "World!", and pasted it before "Hello", I would get back:
"World! Hello"

But how would I do this via the buffer?
This is simple enough how to do it with just an array and display it back to the graph, but
what if I wanted to save the newly organized samples to a file, I would need to somehow
modify the buffer.

How would I do this?

4
General / SFML Win32 - Messages (ie: Scrollbars)
« on: October 04, 2013, 08:52:41 am »
I'm trying to setup scrollbars in a simple SFML window.
The main reason why I need scrollbars is I'm trying to make a simple wave editor using
the sound functions in SFML. The scrollbars will be used for scrolling through the wave (hscrolling) and also
zooming in on it (vscrolling).

However I'm having trouble just even getting simple events like: WM_CREATE to popup once I've
created a window. I'm also confused how to integrate win32 into SFML becaue of how it's setup.
There's no wndproc callback now...so how does it know which window is getting a callback?

Anyways here's my code, maybe someone can tell me how to get a simple WM_CREATE event working:
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <windows.h>
#include <cmath>

HWND button;


////////////////////////////////////////////////////////////
/// Function called whenever one of our windows receives a message
///
////////////////////////////////////////////////////////////
LRESULT CALLBACK onEvent(HWND handle, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        // Quit when we close the main window
        case WM_CLOSE :
        {
            PostQuitMessage(0);
            return 0;
        }

        // Quit when we click the "quit" button
        case WM_COMMAND :
        {
            if (reinterpret_cast<HWND>(lParam) == button)
            {
                PostQuitMessage(0);
                return 0;
            }
        }
    }

    return DefWindowProc(handle, message, wParam, lParam);
}


////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \param Instance : Instance of the application
///
/// \return Error code
///
////////////////////////////////////////////////////////////
INT WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR, INT)
{
    // Define a class for our main window
    WNDCLASS windowClass;
    windowClass.style         = 0;
    windowClass.lpfnWndProc   = &onEvent;
    windowClass.cbClsExtra    = 0;
    windowClass.cbWndExtra    = 0;
    windowClass.hInstance     = instance;
    windowClass.hIcon         = NULL;
    windowClass.hCursor       = 0;
    windowClass.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_BACKGROUND);
    windowClass.lpszMenuName  = NULL;
    windowClass.lpszClassName = TEXT("Test");
    RegisterClass(&windowClass);

    // Main Window
    HWND window = CreateWindow(TEXT("Test"), TEXT("SFML Win32"), WS_SYSMENU | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL, 200, 200, 660, 520, NULL, NULL, instance, NULL);

    // Add a button for exiting
    button = CreateWindow(TEXT("BUTTON"), TEXT("Quit"), WS_CHILD | WS_VISIBLE, 560, 440, 80, 40, window, NULL, instance, NULL);

    // Let's create two SFML views
    /*
        HWND view1 = CreateWindow(TEXT("STATIC"), NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 20,  20, 300, 400, window, NULL, instance, NULL);
    HWND view2 = CreateWindow(TEXT("STATIC"), NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 340, 20, 300, 400, window, NULL, instance, NULL);
    sf::RenderWindow SFMLView1(view1);
    sf::RenderWindow SFMLView2(view2);
        */

    // Create a clock for measuring elapsed time
    sf::Clock clock;

    // Loop until a WM_QUIT message is received
    MSG message;
    message.message = static_cast<UINT>(~WM_QUIT);
    while (message.message != WM_QUIT)
    {
        if (PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
        {
            // If a message was waiting in the message queue, process it
            TranslateMessage(&message);
                        switch(message.message){
                        case WM_CREATE:
                                MessageBox(window, TEXT("MESSAGE"), TEXT("Hello"), 0);
                                break;
                        }
            DispatchMessage(&message);
        }
        else
        {
            float time = clock.getElapsedTime().asSeconds();

            // Clear views
            /*
                        SFMLView1.clear();
            SFMLView2.clear();


            // Display each view on screen
            SFMLView1.display();
            SFMLView2.display();
                        */

        }
    }

    // Destroy the main window (all its child controls will be destroyed)
    DestroyWindow(window);

    // Don't forget to unregister the window class
    UnregisterClass(TEXT("Test"), instance);

    return EXIT_SUCCESS;
}
 

5
Network / UDP Network help with ports
« on: October 15, 2012, 05:19:56 am »
I've got a problem when it comes to ports.
I've got my server binded to port: 4567
My client is binded to port: 4568

There's a problem with this.
If I launch one client, it will successfully bind to port 4568.
If I launch another client, it will fail to bind to port 4568.

So how exactly would I launch two clients on the same computer?

6
Network / Whenever I receive a new message, my player stops drawing
« on: October 09, 2012, 11:09:47 pm »
Here's how my client/server works:

Client presses the LEFT key:
Sends byte: 0

Server reads for a byte:
Gets: 0
Decided to execute code:
    Create a new player and insert player into array

Later on in server on another thread:
   Draw players using window.draw(), iterating through vector of players

Client presses the RIGHT key:
Sends byte: 1

Server reads for a byte:
Get: 1
Decided to execute code:
    Show the IP of the player that sent this message
    //Up to this point, the server will stop drawing the player
   //In a very short instant upon each new message being received, the server will draw a white rectangle       //(the size of my player) and then draw the black background. Sometimes I even catch a glimpse of my player sprite, but it goes away.

I'm not sure why, after I receive byte: 1, the server stops drawing my player...

main.cpp:
/************************************************************
SERVER
************************************************************/

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>

using namespace std;
using namespace sf;

#include "ServPlayer.h"
#include "Sockets.h"

vector<ServPlayer> players;

bool checkIpAndPort(Data d){
        for (vector<ServPlayer>::iterator itr = players.begin(); itr != players.end(); ++itr) {
                if(itr->ip == d.sender && itr->port == d.port){
                        return true;
                }
        }
        return false;
}

void netThread(void *userData){
        bool running = *(bool*) userData;

        sf::IpAddress sender;

        // Create the UDP socket
        sf::UdpSocket socket;
        // Bind it (listen) to the port 4567
        if (!socket.bind(4567)){
                // Error...
        }

        while(running){
                Data d = readbyte(&socket);
                char b = d.byte;
                switch(b)
                {
                        case 0:
                                {
                                //Find out if the IP address is not in
                                        if(!checkIpAndPort(d)){
                                                ServPlayer p(d.sender, d.port);
                                                players.push_back(p);
                                                cout << "New Player: " << d.sender << " " << d.port << endl;
                                        }
                                        else{
                                                cout << "Hello!" << endl;
                                        }
                                break;
                                }


                        case 1:
                                {
                                        for (vector<ServPlayer>::iterator itr = players.begin(); itr != players.end(); ++itr) {
                                                if(itr->ip == d.sender && itr->port == d.port){
                                                        ServPlayer p = *itr;
                                                        cout << "Test: " << p.ip.toString() << endl;
                                                }
                                        }
                                }
                                break;
                }
        }
}

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
        bool running = true;
        sf::Thread thread(&netThread, &running);
        thread.launch();

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
                        if (event.type == sf::Event::Closed){
                                running = false;
                window.close();
                        }
        }

                window.clear();
                vector<ServPlayer>::iterator itr;
                for ( itr = players.begin(); itr != players.end(); ++itr )
                        window.draw(itr->spr_player);
        window.display();
    }
        running = false;
    return 0;
}
 

ServPlayer.cpp:
#include "ServPlayer.h"
#include <cstdlib>
#include <iostream>

ServPlayer::ServPlayer(){

}

ServPlayer::ServPlayer(IpAddress ip, unsigned short port){
        this->ip = ip;
        this->port = port;
        /* initialize random seed: */
        srand ( time(NULL) );

        /* generate secret number: */
        int num = rand() %10 + 1;
        std::cout << num << std::endl;

        if(!img_player.loadFromFile("Images\\spr_player.png")){
                //Error
        }
        spr_player.setTexture(img_player);
        spr_player.setPosition(50 + (50+num), 50 + (50+num));
        spr_player.setOrigin(img_player.getSize().x/2, img_player.getSize().y/2);
}
 

Is it possibly because:
- I'm using players as a global variable?
- I'm using a different thread?

7
Network / Check when someone has disconnected from my UDP socket?
« on: October 05, 2012, 11:10:44 pm »
In my UDP server, I'm trying to find out when a client has "disconnected".
Obviously, this makes more sense if I were using a TCP socket but UDP is not "connected".

One way I thought of was having the server constantly send a message to the client,
and if it was not able to contact it, we determine that client to be "disconnected", but this seems
highly inefficient.

Does anyone know the proper way to test if someone is disconnected?

I'm not talking about when the user closes the window, I mean if for any reason the user is actually
NOT in the server (i.e., client crashed, client's internet d/c, etc.)

8
Network / writebyte() implementation
« on: October 02, 2012, 11:08:24 pm »
I'm attempting to write a: writebyte() function in which the client
specified to write a byte of data to the server, and the server will read this data as a byte

Here's my writebyte():
void writebyte(sf::UdpSocket *socket, unsigned char b){
        if (socket->send(&b, sizeof(b), "127.0.0.1", 4567) != sf::Socket::Done){
                //Error
                cout << "message was not sent" << endl;
        }
}
 

And my readbyte():
unsigned char readbyte(UdpSocket *socket){
        unsigned char byte;
        std::size_t received;
        sf::IpAddress sender;
        unsigned short port;

        int rec = socket->receive(&byte, sizeof(byte), received, sender, port);
        if(rec != sf::Socket::Done){
                //Error
        }
        if(rec == sf::Socket::Disconnected)
                cout << "D/C" << endl;
        return byte;
}
 

I've so far gotten a few functions to work:
writeint()
writefloat() and
writeshort()

The server receives the data correctly.
However for writebyte() and readbyte(), the data is not read correctly.
For example, I've sent the following data using writebyte():
                                unsigned char c = 253;
                                writebyte(&socket, c);
 

Here's what the server writes:


Any idea what's wrong?

9
Graphics / SetSubRect()?
« on: October 01, 2012, 10:07:32 pm »
I noticed in SFML 2.0 the setSubRect() function appeared to be dropped from the sf::Sprite class...?
How exactly would I set a sub-rectangle of an sf::texture in order to animate it in SFML 2.0?

You could do this in 1.6 but it appears to have disappeared.

Example:
sf::Texture tex;
sf::Sprite spr;
..
spr.setTexture(tex);
spr.setSubRect() ... //non-existing in 2.0
 

10
System / Getting my thread to stop when I close my window
« on: September 24, 2012, 10:08:26 pm »
I'm attempting right now to make it so when I close my window, the thread stops by passing in a boolean (running) variable to my thread. However...when I close my window the thread does not appear to stop until I close the console window. The main window (RenderWindow) closes as expected, but the console window does not....Not sure what to do here.
/************************************************************
SERVER
************************************************************/

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>

using namespace std;

#include <SFML/Graphics.hpp>

void netThread(void *userData){
        bool running = *(bool*) userData;
        while(running)
            cout << running << endl;
}

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
        bool running = true;
        sf::Thread thread(&netThread, &running);
        thread.launch();

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
                        if (event.type == sf::Event::Closed){
                                running = false;
                window.close();
                        }
        }

        window.clear();
        window.display();
    }
        running = false;
    return 0;
}
 

11
Network / How do I set up a UDP connection for my server?
« on: September 23, 2012, 06:37:03 am »
Here's my current server code:
/************************************************************
SERVER
************************************************************/

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>

using namespace std;

#include <SFML/Graphics.hpp>

bool running = true;

void netThread(){
        char buffer[128];
        std::size_t received;
        sf::IpAddress sender;
        unsigned short port;

        // Create the UDP socket
        sf::UdpSocket socket;
        socket.setBlocking(false);
        // Bind it (listen) to the port 4567
        if (!socket.bind(4567)){
                // Error...
                cout << "Unable to bind UDP socket: 4567" << endl;
        }

        while(running){
                int rec = socket.receive(buffer, sizeof(buffer), received, sender, port);
                if(rec != sf::Socket::Done){
                        //Error
                }
                if(rec == sf::Socket::Disconnected)
                        cout << "D/C" << endl;
                // Show the address / port of the sender
                std::cout << sender << ":" << port << std::endl;
                // Show the message
                std::cout << buffer << std::endl; // "Hi guys !"
        }
}

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
        sf::Thread thread(&netThread);
        thread.launch();

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
                        if (event.type == sf::Event::Closed){
                                running = false;
                window.close();
                        }
        }

        window.clear();
        window.display();
    }
        running = false;
    return 0;
}
 

Can anyone make any suggestions as to what I should do for a UDP connection?
I'm trying to have multiple clients for my server, so...would I use just one socket? Or multiple sockets?
I'm assuming I just use one as there's no actual connection and it's just packets coming into
my server, however how would I differentiate different players on my game? By IP?
Or would I have them, for each message they send, send a special ID that uniquely identifies
who they are on the server...?

Not sure how to set this up.

12
Network / Server can only receive one message at a time?
« on: September 17, 2012, 09:16:17 am »
I haven't tested client -> server -> client yet, just client -> server for now.
Unfortunately, I'm only able to send one message per connection.
Client
int main()
{
        sf::SocketTCP Client;
    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(480, 320, 32), "Window");
        const sf::Input& Input = App.GetInput();
    cout << "Type LEFT to connect, RIGHT to send a message, UP to disconnect" << endl;
    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
                        if(Input.IsKeyDown(sf::Key::Left)){
                                if (Client.Connect(4567, "127.0.0.1") != sf::Socket::Done)
                                {
                                        // Error...
                                        cout << "error" << endl;
                                }
                                else cout << "connected" << endl;
                        }
                        else if(Input.IsKeyDown(sf::Key::Right)){
                                cout << "right pressed" << endl;
                                char Buffer[] = "Hi guys !";
                                if (Client.Send(Buffer, sizeof(Buffer)) != sf::Socket::Done)
                                {
                                        // Error...
                                        cout << "error sending message" << endl;
                                }
                        }
                        else if(Input.IsKeyDown(sf::Key::Up)){
                                Client.Close();
                                cout << "disconnected" << endl;
                        }
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        // Clear the screen (fill it with black color)
        App.Clear(sf::Color(255, 255, 255));

        // Display window contents on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}
 

Left button connects, right button sends a message: "Hi guys !", up button disconnects/closes the socket.
Now on my server...
Server
void ThreadFunction(void* UserData)
{
        Server *server = (Server*)UserData;
        char Buffer[128];
        std::size_t Received;

        while(true){
                sf::IPAddress ClientAddress;
                if (server->Listener.Accept(server->Client, &ClientAddress) != sf::Socket::Done)
                {
                        // Error...
                        //cout << "Error listening..." << endl;
                }
                else{
                        cout << "Got a connection!" << endl;
                }

                if (server->Client.Receive(Buffer, sizeof(Buffer), Received) != sf::Socket::Done)
                {
                        // Error...
                        continue;
                }
                else{
                        cout << "Message: " << Buffer << endl;
                }
        }
}
 

I can connect, and send a message, but just one message.
If I disconnect and connect again, I can send a message, but only one.

Not sure what's going on. I should be able to send multiple ones.

Pages: [1]