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.


Messages - newbie123

Pages: [1] 2
1
General / slow things down a bit
« on: January 08, 2012, 04:38:47 pm »
Quote from: "texus"
I tested the following code with SFML2 and it should work.
I think that if u are using SFML 1.6 that you should change the 1000 into a 1.

Code: [Select]
while(Window.IsOpened())
{
    if( up )
    {
        if (Clock.GetElapsedTime() >= 1000)
        {
            ++Time;
            Clock.Reset();
        }
    }
    else
    {
        if (Clock.GetElapsedTime() >= 1000)
        {
            --Time;
            Clock.Reset();
        }
    }

    if( Time >= 170 )
        up = false;

    if( Time <= 0 )
        up = true;
           
}


wow thank you so very much its working perfectly.
but i could sworn that i have tried this way before posting and it didn't work. hmmm...

anyways thank you very much it working like a charm. :D

2
General / slow things down a bit
« on: January 08, 2012, 04:11:03 pm »
Quote from: "texus"
You only reset the clock when up is false, also do this when it is true.


There are two ways I see to fix the problem:

1) Does it has to be 170?  If not then you can slow it down by letting it count to e.g. 17000.

2) Change it to something like this:
Code: [Select]
if (up)
{
    if (Clock.GetElapsedTime() > 25)
    {
        ++Time;
        Clock.Reset();
    }
}

By changing the 25 you can adjust the speed.


if i reset clock when its false and true the time variable will stay at 0 and will not increment.

1) yes it has to be 170 because I'm changing the transparency of an image using time. I'm creating a darkness / night effect.

2) i can't do it this way because the program will wait 25 second till it start incrementing time.

what i want to do is increment "time" every second till it reach 170 and then decrement it till it reach 0 and so on, to create my night effect.

3
General / slow things down a bit
« on: January 08, 2012, 03:53:25 pm »
Quote from: "texus"
Do something like this to also make the clock count backwards (this is an example code, not exactly what you have to write):
Code: [Select]

sf::Clock Clock;
int Time = 0;

while (...)
{
    if (up)
    {
        Time += Clock.GetElapsedTime();
        Clock.Reset();
    }
    else
    {
        Time -= Clock.GetElapsedTime()
        Clock.Reset();
    }
}


well i did this but its still going fast
Code: [Select]

int Time = 0;
bool up = true;

while(Window.IsOpened())
{
if( up )
{
Time += Clock.GetElapsedTime();
}
else
{
Time -= Clock.GetElapsedTime();
Clock.Reset();
}

if( Time >= 170 )
up = false;

if( Time <= 0 )
up = true;


Sprite.SetColor(sf::Color(15,10,63, Time));
std::cout<<Time<<"\n";
}

4
General / slow things down a bit
« on: January 08, 2012, 03:20:27 pm »
Hello
I'm trying to make a counter from 0 to 170 and then from 170 to 0. I got it working but its counting very fast. Is there a way to slow the counting down a bit.

I tried to use Clock.GetTime(); but the problem is that the clock count from 0 to 170 for example but i want it to also count backwords from 170 to 0.

here what i got

Code: [Select]

#include <SFML\Graphics.hpp>
#include <iostream>

int main()
{
sf::RenderWindow Window(sf::VideoMode(800, 632, 32), "Tower Defence");
sf::Image Image;
sf::Sprite Sprite;

Image.LoadFromFile("light.png");
Sprite.SetImage(Image);
Sprite.SetPosition(0, 32);
Sprite.SetColor(sf::Color(14,25,52));

sf::Clock Clock;
int Time = 0;
bool up = true;

while(Window.IsOpened())
{
sf::Event Event;
Window.SetFramerateLimit(150);
Window.UseVerticalSync(true);
while (Window.GetEvent(Event))
{
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
Window.Close();

}


if( up )
{
++Time;
}
else
{
--Time;
}

if( Time >= 170 )
up = false;

if( Time <= 0 )
up = true;


Sprite.SetColor(sf::Color(15,10,63, Time));
std::cout<<Time<<"\n";

Window.Clear();
Window.Draw(Sprite);
Window.Display();
}
return 0;
}
[quote][/quote]

5
General / How to stop scrolling when player reach end of the map
« on: December 23, 2011, 08:06:08 am »
Hello
as the title say how can i stop scrolling when i reach a certain position?

Code: [Select]


#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    sf::RenderWindow App(sf::VideoMode(800, 600), "SFML Views");

    sf::Image BackgroundImage;
    if (!BackgroundImage.LoadFromFile("background.png"))
        return EXIT_FAILURE;

    sf::Sprite Background1(BackgroundImage);
Background1.SetPosition(0, 0);

sf::Sprite Background2(BackgroundImage);
Background2.SetPosition(500, 0);

sf::Sprite Background3(BackgroundImage);
Background3.SetPosition(0, 500);

sf::Sprite Background4(BackgroundImage);
Background4.SetPosition(500, 500);


    sf::Vector2f Center(500, 500);
    sf::Vector2f HalfSize(400, 300);
    sf::View View(Center, HalfSize);


    while (App.IsOpened())
    {

        sf::Event Event;
        while (App.GetEvent(Event))
        {

        }


        float Offset = 200.f * App.GetFrameTime();
if(App.GetInput().GetMouseX() <=100) View.Move(-Offset, 0);
if(App.GetInput().GetMouseX() >=700) View.Move( Offset, 0);
if(App.GetInput().GetMouseY() <=100) View.Move( 0, -Offset);
if(App.GetInput().GetMouseY() >=500) View.Move( 0, Offset);



        App.SetView(View);

        App.Clear();

        App.Draw(Background1);
App.Draw(Background2);
App.Draw(Background3);
App.Draw(Background4);


        App.SetView(App.GetDefaultView());

        App.Display();
    }

    return 0;
}

6
Graphics / bullet keep following mouse direction
« on: December 18, 2011, 07:59:36 am »
Quote from: "Tex Killer"
You must store it on your own, out of the Sprite class, and use it from wherever you've stored. You can make a container class, that have a Sprite inside, and this vector as well.


can please give me an example. I have never use Vectors in sfml.

7
Graphics / bullet keep following mouse direction
« on: December 17, 2011, 08:47:04 pm »
Hello
I have a problem where the bullet keeps following the mouse instead of taking mouse angle and direction and keep going.

I have this code and someone fixed it for me but i there is a small problem. The guy who fixed it used Sprite.SetVector and the problem is there is no Vector function in sprite class.

can you guys please help.

My code
Code: [Select]
float Speed = 10;

float BulletX = Sprite[i].GetPosition().x;
float BulletY = Sprite[i].GetPosition().y;
                       

float AngleX = MouseX - BulletX;
float AngleY = MouseY - BulletY;

float vectorLength = sqrt(AngleX*AngleX + AngleY*AngleY);

float DirectionX = AngleX / vectorLength;
float DirectionY = AngleY / vectorLength;

float VelocityX = DirectionX * Speed;
float VelocityY = DirectionY * Speed;

Sprite[i].Move (VelocityX, VelocityY);


Fixed code
Code: [Select]

// This code is called only when the bullet is fired
float bullet_x=Sprite[i].GetPosition().x;
float bullet_y=Sprite[i].GetPosition().y;
float angle_x=MouseX-bullet_x;
float angle_y=MouseY-bullet_y;
float vector_length=sqrt(angle_x*angle_x + angle_y*angle_y);

Sprite[i].SetVector(angle_x/vector_length, angle_y/vector_length);  // Each bullet stores a direction vector


// This code is called every update
float velocity_x = Sprite[i].GetVector().x * Speed;
float velocity_y = Sprite[i].GetVector().y * Speed;
Sprite[i].Move(velocity_x, velocity_y);


Hoe can I do this ?
Sprite.SetVector(angle_x/vector_length, angle_y/vector_length);  //
Sprite.GetVector().x

8
Network / problem when trying to send a packet in UDP
« on: September 13, 2011, 03:15:14 am »
i give up -_-

i'm going to learn Winsock 2. i know its 10 times harder but i might have some luck learning it.

9
Network / problem when trying to send a packet in UDP
« on: September 12, 2011, 04:56:31 pm »
Quote from: "Laurent"
Quote
in the first SFML tutorial where you send a "char Buffer[]="i'm client" it works perfectly with both UDP and TCP, but when i try to use packets it doesn't work.

You should use the sample directly, and only replace the char[] buffer with a packet -- rather than rewriting it entirely. That would seriously limit the number of potential mistakes.


its not working, there is something wrong either with my PC or the class packet.
i'm getting the same Socket state which is 3 (error).

when i do this i get Socket state 3 (error)

Server:
Code: [Select]

SendPacket << message;


Client:
Code: [Select]

ReceivePacket >> message;



However when i do this i get Socket state 1 which is (Done) but the data is corrupted.

Code: [Select]

SendPacket >>message;


Client:
Code: [Select]

ReceivePacket << message;


Please try take my code and run it on your machine and see if you get the same results, maybe i have a virus or something.

here is the code:

Client
Code: [Select]

#include <SFML\Network.hpp>
#include <iostream>

void RunClient(unsigned short Port)
{
sf::SocketUDP Client;
sf::IPAddress Address;
sf::Packet ReceivePacket;

if (!Client.Bind(Port))
{
std::cout<<"Could not listen";
}

while (true)
{
sf::Socket::Status status = Client.Receive(ReceivePacket, Address, Port);
switch(status)
{
case sf::Socket::Disconnected:
 std::cout << "Disconnected" << std::endl;
 break;

case sf::Socket::Error:
 std::cout << "Socket Status:" << status << std::endl;
 break;

case sf::Socket::Done:
{
std::string message;
ReceivePacket >> message;
std::cout << "IP: " << Address << std::endl << "Port: "<< Port << std::endl << "Message: " << message<< std::endl;
break;
}
case sf::Socket::NotReady:
 std::cout << "NotReady" << std::endl;
 break;
}
}
Client.Close();
}

int main()
{
const unsigned short Port = 4000;
RunClient(Port);
system("pause");
return 0;
}


Server:
Code: [Select]

#include <SFML\Network.hpp>
#include <iostream>

void RunServer(unsigned short Port)
{
sf::SocketUDP Server;
sf::IPAddress Address;
sf::Packet SendPacket;

std::cout << "type the ip address you want to send data to" << std::endl;
std::cin >> Address;

if (Address.IsValid())
std::cout << "Valid" << std::endl;

std::cout << "Press Enter to send packet." <<std::endl;
getchar();

char input = getchar();

std::string message = "PLEASE WORK!!!";

SendPacket << message;

while(input == 10)
{
if (Server.Send(SendPacket, Address , Port) != sf::Socket::Done)
{
std::cout<<"Could not send data";
}
input = getchar();

        if(input == 'z')
break;
}
Server.Close();
}

int main()
{
const unsigned short Port = 4000;
RunServer(Port);
system("pause");
return 0;
}

10
Network / problem when trying to send a packet in UDP
« on: September 12, 2011, 04:06:31 pm »
Quote from: "Laurent"
Is it exactly the same error, after pressing enter 3 times?

127.0.0.1 should be ok. But check the validity of the address in your code, just to be sure you don't mistype it.


yes its the same exact error and i check the validity of the address and its valid.

this thing is really really driving me crazy i dont know what the hell is wrong with it.

in the first SFML tutorial where you send a "char Buffer[]="i'm client" it works perfectly with both UDP and TCP, but when i try to use packets it doesn't work.

i'll post the updated code above.

11
Network / problem when trying to send a packet in UDP
« on: September 12, 2011, 02:51:50 pm »
Quote from: "Laurent"
The address that you pass to Send in the server is uninitialized, you're sending to a random address.

The "choose the address you want to connect to" piece of code should be in the server, not the client since the client only receives.


ok i'm not sure if i understood you correctly, is this what you mean i should do?

move this piece of code from client to server

Code: [Select]

std::cout << "type the server ip address you want to connect to" << std::endl;
std::cin >> Address;


i did that but i still get the some error. should i use other ip than 127.0.0.1?

Server
Code: [Select]

void RunServer(unsigned short Port)
{
sf::SocketUDP Server;
sf::IPAddress Address;
sf::Packet SendPacket;

std::cout << "type IP " << std::endl;
std::cin >> Address;

std::cout << "Press Enter to send packet." <<std::endl;
getchar();

char input = getchar();

PersonData PersonData1 = {12, "Bill", 1.32f};

SendPacket << PersonData1;

while(input == 10)
{
if (Server.Send(SendPacket, Address , Port) != sf::Socket::Done)
{
std::cout<<"Could not send data";
}
input = getchar();

        if(input == 'z')
break;
}
Server.Close();
}


Client
Code: [Select]

void RunClient(unsigned short Port)
{
sf::SocketUDP Client;
sf::IPAddress Address;
sf::Packet ReceivePacket;

if (!Client.Bind(Port))
{
std::cout<<"Could not listen";
}

while (true)
{
sf::Socket::Status status = Client.Receive(ReceivePacket, Address, Port);
switch(status)
{
case sf::Socket::Disconnected:
 std::cout << "Disconnected" << std::endl;
 break;

case sf::Socket::Error:
 std::cout << "Error sending" << std::endl;
 break;

case sf::Socket::Done:
std::cout << "Done" << std::endl;
 break;

case sf::Socket::NotReady:
 std::cout << "NotReady" << std::endl;
 break;
}

PersonData PersonData2;

if (ReceivePacket >> PersonData2)
{
std::cout << "Age: " << PersonData2.Age << "Name: " << PersonData2.Name << "Hight: "<< PersonData2.Height << std::endl;
}

}
Client.Close();
}

12
Network / problem when trying to send a packet in UDP
« on: September 12, 2011, 02:30:27 pm »
Quote from: "Laurent"
What's the output of both the client and the server?


Client EXE
http://www.mediafire.com/?7585wacih8grhhy

Server EXE
http://www.mediafire.com/?ou2ms3whwma8mgh



By fantasyxii


By fantasyxii


By fantasyxii


By fantasyxii


By fantasyxii


By fantasyxii

13
Network / problem when trying to send a packet in UDP
« on: September 12, 2011, 02:06:02 pm »
Quote from: "Laurent"
So it's a new problem, you don't receive garbage anymore


yah the new problem is that i don't receive garbage anymore because i changed the operator overloading signs "<<".


Quote from: "Laurent"
where does it return an error state?

after i press Enter key 3 times in the server i get an error in the client.

14
Network / problem when trying to send a packet in UDP
« on: September 12, 2011, 01:43:45 pm »
Quote from: "Laurent"
If you have updated your code, you should show us the new version.


i posted the new version above, but i'm still getting Socket state = 3 (error).

15
Network / problem when trying to send a packet in UDP
« on: September 12, 2011, 01:04:18 pm »
please guys someone help its been 3 days and i still could not fix this problem .

i alos posted here but no one could fix my problem too.
http://www.gamedev.net/topic/610454-problem-when-trying-to-send-a-packet-in-udp-sfml/

Pages: [1] 2
anything