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

Pages: [1] 2
1
Graphics / Text does not render
« on: October 17, 2013, 05:29:45 pm »
I switched to new computer with Win 7 64bit(SFML 2.1) from Win XP 32bit(SFML 2.0) and now i cannot render text, I always got only black screen. Here is code:
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600, 32), "");
    window.setFramerateLimit(60);

    while(window.isOpen())
    {
        sf::Event event;
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
                window.close();
            if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
                window.close();
        }

        sf::Text text;
        text.setString("hello");
        text.setColor(sf::Color::Red);
        window.clear();
        window.draw(text);
        window.display();
    }

    return 0;
}
 

2
Graphics / Image and OpenGL does not work
« on: November 27, 2012, 08:21:14 pm »
Hi, I am using Image as texture source from .tga file but it does not work correctly. One texture is working great, but the second, that is loaded first, is showing only by going closer. Where can be the problem please? :-\
sf::Image image;
                image.loadFromFile(matProp.textureFile);
                GLuint generatedTex = 0;
                glGenTextures(1, &generatedTex);
                glBindTexture(GL_TEXTURE_2D, generatedTex);
                glTexImage2D(GL_TEXTURE_2D,
                             0,
                             GL_RGBA,
                             image.getSize().x,
                             image.getSize().y,
                             0,
                             GL_RGBA,
                             GL_UNSIGNED_BYTE,
                             image.getPixelsPtr());
                //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
                //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
                glGenerateMipmap(GL_TEXTURE_2D);
                matProp.texture = generatedTex;

3
Graphics / Shader and render texture
« on: April 22, 2012, 08:42:20 pm »
Hi, I want to create something like lights with using the shaders, but I do not know how to use one shader with different properties on the same render texture. I want to use it like "cycle for". Please can you tell me how to do this or another way? And I have another problem, now when I am drawing background image to the render texture, it is flipped vertically.
My code
Code: [Select]
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
#include <cstring>
#include <vector>

class CLight
{
    public:
        sf::Vector2f position;
        sf::Color color;
        float radius;

        CLight(sf::Vector2f pos, sf::Color col, float rad)
        {
            position = pos;
            color = col;
            radius = rad;
        }
};

int main()
{
    sf::RenderWindow App(sf::VideoMode(800,600,32),"SFML");
    App.setFramerateLimit(60);

    std::vector<CLight> lights;
    lights.push_back(CLight(sf::Vector2f(400,300), sf::Color(128,128,128), 200.f));

    sf::RenderTexture renderTexture;
    renderTexture.create(800,600);

    sf::Texture backTex;
    backTex.loadFromFile("image.png");
    backTex.setSmooth(false);
    sf::Sprite background(backTex);

    sf::Shader fragShader;

    if(!fragShader.loadFromFile("light.frag",sf::Shader::Fragment))
        App.close();

    fragShader.setParameter("texture", backTex);
    while(App.isOpen())
    {
        sf::Event event;
        while(App.pollEvent(event))
        {
            if((event.type == sf::Event::Closed) || (event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
                App.close();
        }

        float mouseX = float(sf::Mouse::getPosition(App).x);
        float mouseY = float(sf::Mouse::getPosition(App).y);

        std::cout<<mouseX<<" "<<mouseY<<std::endl;

        App.clear();
        renderTexture.clear();

        fragShader.setParameter("lightRad", lights[0].radius);
        fragShader.setParameter("lightPos", lights[0].position.x, 600.f-lights[0].position.y);
        fragShader.setParameter("lightCol", lights[0].color.r/255.f, lights[0].color.g/255.f, lights[0].color.b/255.f);

        sf::RenderStates shader(&fragShader);
        renderTexture.draw(background, shader);

        sf::Sprite drawTexture(renderTexture.getTexture());
        App.draw(drawTexture);

        App.display();
    }

    return 0;
}

Shader
Code: [Select]
uniform sampler2D texture;

uniform float lightRad; //radius
uniform vec2 lightPos; //position
uniform vec3 lightCol; //color

void main(void)
{
vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);
float dis = 1.0f - distance(vec2(gl_FragCoord), lightPos) / lightRad;
vec4 light = vec4(lightCol.r,lightCol.g,lightCol.b,dis);

vec4 col = pixel + light;
gl_FragColor = vec4(col.xyz, dis);
}

4
Graphics / SFML2 and Vertex Shader problem
« on: March 06, 2012, 09:36:00 am »
Hi, I am real beginner in GLSL. I have this GLSL vertex shader and it doesnt work as I want. According to me it should change the position of the rectangle by * 0.5, because SFML draw it like GL_QUAD with vertexes, but rect disapears. Can somebody help me please?
I also tried change last line to, but it doesnt work :( .
Code: [Select]
  gl_Position = gl_ModelViewProjectionMatrix * a;
Shader code
Code: [Select]
void main(void)
{
   vec4 a = gl_Vertex;
   a.x = a.x * 0.5;
   a.y = a.y * 0.5;

   gl_Position = a;
}

Here is my code
Code: [Select]
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

int main()
{
    sf::RenderWindow App(sf::VideoMode(800,600,32),"SFML");
    App.SetFramerateLimit(30);

    sf::Shader vertShader;

    if(!vertShader.LoadFromFile("scaleshader.vert",sf::Shader::Vertex))
        App.Close();

    while(App.IsOpen())
    {
        App.Clear();
        sf::Event event;
        while(App.PollEvent(event))
        {
            if((event.Type == sf::Event::Closed) || (event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Keyboard::Escape))
                App.Close();
        }

        sf::RenderStates shader(&vertShader);
        sf::RectangleShape rect(sf::Vector2f(200,100));
        rect.SetPosition(300,250);
        rect.SetFillColor(sf::Color(255,0,0,255));

        App.Draw(rect,shader);

        App.Display();
    }

    return 0;
}

5
Graphics / class sf::Sprite has no member named GetGlobalBounds SFML2.0
« on: December 30, 2011, 07:24:47 pm »
I have SFML 2.0, and I have this code, but it says error
 'class sf::Sprite' has no member named 'GetGlobalBounds'|
what am I doing wrong please?
Code: [Select]
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>

int main()
{
    sf::RenderWindow App(sf::VideoMode(800,800,32),"SFML");
    App.SetFramerateLimit(60);
    sf::Texture Tex;
    Tex.LoadFromFile("quad.png");
    Tex.SetSmooth(false);
    sf::Sprite pic(Tex);
    pic.SetOrigin(50,50);
    float x=400, y=300;
    pic.SetPosition(x,y);
    float angle=0;
    while(App.IsOpened())
    {
        App.Clear();
        sf::Event event;
        while(App.PollEvent(event))
        {
            if((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Keyboard::Escape)) App.Close();
            if(sf::Keyboard::IsKeyPressed(sf::Keyboard::Left))
                angle--;
            if(sf::Keyboard::IsKeyPressed(sf::Keyboard::Right))
                angle++;
        }
        pic.SetRotation(angle);
        App.Draw(pic);
        sf::FloatRect c = pic.GetGlobalBounds();
        App.Display();
    }

    return 0;
}




6
Graphics / Points of my rotated picture
« on: September 16, 2011, 08:45:53 pm »
Is there any way how can I get positions of my 4 points of my picture when it is rotated from SFML please? because I am currently doing stuff around collision detection rotated rectangles and I need these points. :roll:

7
Graphics / how to disable font smoothing
« on: August 15, 2011, 05:33:39 pm »
Can somebody tell me how can I set smoothing of font to false please?

8
Window / How to move window without title bar
« on: August 14, 2011, 06:57:55 pm »
Hi, I am creating a little program and I am using window without titlebar, but I wanna move it, can you please tell me how can I do it? because this code doesnt work. It put window in the top of the left corner and near it but no elsewhere.  :roll:

Code: [Select]

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>

int main()
{
    sf::RenderWindow App(sf::VideoMode(103,31),"Keygen",sf::Style::None);

    sf::Image images[4];
    images[0].LoadFromFile("keygen.png");
    images[1].LoadFromFile("textbox.png");
    images[2].LoadFromFile("generate.png");
    images[3].LoadFromFile("exit.png");
    sf::Sprite sprites[4];
    for(int i=0;i<4;i++)
    {
        images[i].SetSmooth(false);
        sprites[i].SetImage(images[i]);
    }
    sprites[1].SetPosition(6,6);
    sprites[2].SetPosition(31,16);
    sprites[3].SetPosition(75,16);

    bool move = false;
    while(App.IsOpened())
    {
        sf::Event Event;
        App.GetEvent(Event);

        if(!move)
            if(App.GetInput().IsMouseButtonDown(sf::Mouse::Left))
                move = true;

        if(move && App.GetInput().IsMouseButtonDown(sf::Mouse::Left))
            App.SetPosition(App.GetInput().GetMouseX()-51,App.GetInput().GetMouseY()-15);

        else
            move = false;

        App.Clear();
        App.Draw(sprites[0]);

        int mx = App.GetInput().GetMouseX();
        int my = App.GetInput().GetMouseY();
        if(mx >= 6 && mx <= 96 && my >= 6 && my <= 14)
            App.Draw(sprites[1]);
        else if(mx >= 31 && mx <= 73 && my >= 16 && my <= 24)
            App.Draw(sprites[2]);
        else if(mx >= 75 && mx <= 96 && my >= 16 && my <= 24)
        {
            App.Draw(sprites[3]);
            if((Event.Type == sf::Event::MouseButtonPressed) && (Event.MouseButton.Button == sf::Mouse::Left))
                App.Close();
        }

        App.Display();
        sf::Sleep(0.010);
    }

    return 0;
}

9
Window / Multiple use of key
« on: June 30, 2011, 08:08:56 pm »
I use Escape as exit in main menu and also as return from sub menu, I tried to make blocking loop when Escape is pressed for a little time like few seconds or more that only return to menu and no close all program.
Here is my code, I tried to change it many times but it doesnt work good.
Please give me addvice if you can. :roll:
This is my waiting code with mentioned blocking loop after draw score in the display.
Code: [Select]
while(1)
    {
        sf::Event Event;
        App.GetEvent(Event);

        if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
        {
            sf::Event Event;
            App.GetEvent(Event);
            while(App.GetInput().IsKeyDown(sf::Key::Escape))
            {
                sf::Event Event;
                App.GetEvent(Event);
                sf::Sleep(0.05);
            }
            break;
        }

        if(Event.Type == sf::Event::Closed)
            return -1;


        sf::Sleep(0.05);
    }

10
Window / Wrong size of window
« on: June 05, 2011, 11:32:01 am »
I used this command, but it creates windows with 111 and 200
Code: [Select]
sf::RenderWindow App(sf::VideoMode(100, 200, 32), "Eye");
Where can be problem?

11
Graphics / Angle change my position
« on: June 04, 2011, 08:22:53 pm »
I have a problem with SetRotation function, she changes position of my picture and I dont know why, look the video with error

My picture


Source
Code: [Select]
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <cmath>
#include <iostream>

typedef sf::Vector2<double> Point;

int main()
{
    sf::RenderWindow App(sf::VideoMode(100, 200, 32), "Eye");

    double radius = 7.5;
    int dir = 0;

    sf::Image imageEye;
    imageEye.LoadFromFile("eye.png");
    imageEye.SetSmooth(false);
    sf::Sprite spriteEye(imageEye);

    Point eye(50,100);

    while (App.IsOpened())
    {

        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();

            // Escape key : exit
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                App.Close();
        }

        if (App.GetInput().IsKeyDown(sf::Key::Space))
        {
            dir = 1;
        }

        else
        dir = 0;

        if(dir)
        {
            eye.x -= cos(45 * 3.14 / 180) * 0.5;
            eye.y += sin(45 * 3.14 / 180) * 0.5;
        }

        else
        {
            eye.x += cos(45 * 3.14 / 180) * 0.5;
            eye.y += sin(45 * 3.14 / 180) * 0.5;
        }

        App.Clear(sf::Color(64,64,64));

        if(dir)
            spriteEye.SetRotation(45);

        else
            spriteEye.SetRotation(135);

        spriteEye.SetPosition(eye.x,eye.y);
        App.Draw(spriteEye);
       
        App.Display();

        std::cout<<"eye x = "<< eye.x<<std::endl;
        std::cout<<"eye y = "<< eye.y<<std::endl;

        sf::Sleep(0.025f);
    }


    return EXIT_SUCCESS;
}

12
General / Collision between line and ball problem
« on: May 31, 2011, 08:21:50 pm »
I am trying to make my own collision detection between line and ball but it doesnt working correctly. Green circle should be shown only when it is collision. Please can someone chcek my code?  :roll:
My collision detection plan:

bug that I mentioned

Code: [Select]
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <cmath>
#include <iostream>

using std::cout;
using std::endl;

struct point
{
    float x;
    float y;
    float distance;
};

bool Check(point & Point, float CircX, float CircY)
{
    float CircAngle = atan2(CircY,CircX);
    cout<<"CircAngle = "<<CircAngle<<" radians, "<<CircAngle*180/3.14<<" degrees"<<endl;

    float PointAngle = atan2(Point.y,Point.x);
    cout<<"PointAngle = "<<PointAngle<<" radians, "<<PointAngle*180/3.14<<" degrees"<<endl;

    float angle = CircAngle - PointAngle;
    cout<<"angle = "<<angle<<" radians, "<<angle *180/3.14<<" degrees"<<endl;

    if(angle < 0)
    {
        angle *= -1;
        cout<<"changed by multiplication"<<endl;
        cout<<"angle = "<<angle<<" radians, "<<angle *180/3.14<<" degrees"<<endl;
    }

    float distance_circle_point = sqrt( pow(Point.x - CircX,2) + pow(Point.y - CircY,2) );
    cout<<"distance_circle_point = "<<distance_circle_point<<endl;

    float distance = sin(angle) * distance_circle_point;
    cout<<"distance = "<<distance<<endl;

    cout<<endl<<endl;

    if(distance <= 20)
        return true;

    return false;
}

int main()
{
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "animation");

    float x=400, y=300;

    point p1 = {100,100}, p2 = {300,300};

    while (App.IsOpened())
    {

        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();

            // Escape key : exit
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                App.Close();
        }

        if (App.GetInput().IsKeyDown(sf::Key::Up))
            y-=2;

        if (App.GetInput().IsKeyDown(sf::Key::Down))
            y+=2;

        if (App.GetInput().IsKeyDown(sf::Key::Right))
            x+=2;

        if (App.GetInput().IsKeyDown(sf::Key::Left))
            x-=2;

        App.Clear();

        if(Check(p1,x,y))
           App.Draw(sf::Shape::Circle(20, 20, 20, sf::Color(0,255,0)));

        App.Draw(sf::Shape::Line(p1.x, p1.y, p2.x, p2.y, 1, sf::Color(0,255,128)));
        App.Draw(sf::Shape::Circle(x, y, 20, sf::Color(255,128,0)));
        App.Display();
        sf::Sleep(0.025f);
    }


    return EXIT_SUCCESS;
}

13
Graphics / Static Image and Sprite doesnt work in class
« on: April 28, 2011, 02:52:01 pm »
I have this code, but when I try to compile it, it says, please can somebody helps me? please a lot  :cry:  :roll:
code
Code: [Select]
/*
 * File:   Bullet.hpp
 * Author: Adamv
 *
 * Created on April 25, 2011, 6:11 PM
 */

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <cmath>

#ifndef BULLET_HPP
#define BULLET_HPP

#define PI 3.14159265

class cBullet
{
        private:
            static sf::Image imgBullet;
            static sf::Sprite sprBullet;
            bool active;
            float x;
            float y;
            float angle;
            float distance;
           
        public:
            cBullet();
            bool IsActive();
            void SetActive(bool status, float mx, float my);
            void Move();
            void SetAngle(float mangle);
            void Draw(sf::RenderWindow & App);
            ~cBullet() {} ;
};

#endif /* BULLET_HPP */


errors
Code: [Select]
"/bin/make" -f nbproject/Makefile-Release.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/c/Documents and Settings/Adamv/Desktop/CombatShooter'
"/bin/make"  -f nbproject/Makefile-Release.mk dist/Release/MinGW-Windows/combatshooter.exe
make[2]: Entering directory `/c/Documents and Settings/Adamv/Desktop/CombatShooter'
mkdir -p build/Release/MinGW-Windows
rm -f build/Release/MinGW-Windows/main.o.d
g++.exe    -c -O2 -I/F/CodeBlocks/SFML-1.6-sdk-windows-mingw/SFML-1.6/include -MMD -MP -MF build/Release/MinGW-Windows/main.o.d -o build/Release/MinGW-Windows/main.o main.cpp
mkdir -p build/Release/MinGW-Windows
rm -f build/Release/MinGW-Windows/Bullet.o.d
g++.exe    -c -O2 -I/F/CodeBlocks/SFML-1.6-sdk-windows-mingw/SFML-1.6/include -MMD -MP -MF build/Release/MinGW-Windows/Bullet.o.d -o build/Release/MinGW-Windows/Bullet.o Bullet.cpp
mkdir -p dist/Release/MinGW-Windows
g++.exe     -o dist/Release/MinGW-Windows/combatshooter build/Release/MinGW-Windows/main.o build/Release/MinGW-Windows/Bullet.o -L/F/CodeBlocks/SFML-1.6-sdk-windows-mingw/SFML-1.6/lib /F/CodeBlocks/SFML-1.6-sdk-windows-mingw/SFML-1.6/lib/libsfml-graphics.a /F/CodeBlocks/SFML-1.6-sdk-windows-mingw/SFML-1.6/lib/libsfml-window.a /F/CodeBlocks/SFML-1.6-sdk-windows-mingw/SFML-1.6/lib/libsfml-system.a
f:/codeblocks/mingw/bin/../lib/gcc/mingw32/4.4.1/../../../../mingw32/bin/ld.exe: warning: auto-importing has been activated without --enable-auto-import specified on the command line.
This should work unless it involves constant data structures referencing symbols from auto-imported DLLs.
build/Release/MinGW-Windows/Bullet.o:Bullet.cpp:(.text+0x110): undefined reference to `cBullet::sprBullet'
build/Release/MinGW-Windows/Bullet.o:Bullet.cpp:(.text+0x132): undefined reference to `cBullet::sprBullet'
build/Release/MinGW-Windows/Bullet.o:Bullet.cpp:(.text+0x14a): undefined reference to `cBullet::sprBullet'
build/Release/MinGW-Windows/Bullet.o:Bullet.cpp:(.text+0x1d2): undefined reference to `cBullet::imgBullet'
build/Release/MinGW-Windows/Bullet.o:Bullet.cpp:(.text+0x213): undefined reference to `cBullet::imgBullet'
build/Release/MinGW-Windows/Bullet.o:Bullet.cpp:(.text+0x227): undefined reference to `cBullet::imgBullet'
build/Release/MinGW-Windows/Bullet.o:Bullet.cpp:(.text+0x22e): undefined reference to `cBulletInfo: resolving vtable for sf::Sprite by linking to __imp___ZTVN2sf6SpriteE (auto-import)
::sprBullet'
build/Release/MinGW-Windows/Bullet.o:Bullet.cpp:(.text+0x322): undefined reference to `cBullet::imgBullet'
build/Release/MinGW-Windows/Bullet.o:Bullet.cpp:(.text+0x363): undefined reference to `cBullet::imgBullet'
build/Release/MinGW-Windows/Bullet.o:Bullet.cpp:(.text+0x377): undefined reference to `cBullet::imgBullet'
build/Release/MinGW-Windows/Bullet.o:Bullet.cpp:(.text+0x37e): undefined reference to `cBullet::sprBullet'
collect2: ld returned 1 exit status
make[2]: *** [dist/Release/MinGW-Windows/combatshooter.exe] Error 1make[2]: Leaving directory `/c/Documents and Settings/Adamv/Desktop/CombatShooter'
make[1]: Leaving directory `/c/Documents and Settings/Adamv/Desktop/CombatShooter'

make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 4s)
 


but this is  not important because it says this in the past and it works
 
Code: [Select]
f:/codeblocks/mingw/bin/../lib/gcc/mingw32/4.4.1/../../../../mingw32/bin/ld.exe: warning: auto-importing has been activated without --enable-auto-import specified on the command line.

14
Graphics / Sprite rotating problem
« on: April 25, 2011, 02:12:17 pm »
I have very simple vector program but it doesnt work. It moves to different direction as the angle of sprite. Please how can I fix this problem?
My code
Code: [Select]
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <cmath>
#include <sstream>
#include <string>

static inline std::string int2Str(float x)
{
      std::stringstream type;
      type << x;
      return type.str();
}

#define PI 3.14159265

int main()
{
    sf::RenderWindow App(sf::VideoMode(800, 600), "ShootingGun");
   
    sf::Image imgGun;
    imgGun.LoadFromFile("gun.png");
    imgGun.SetSmooth(false);
    sf::Sprite Gun(imgGun);
   
    float x = 400, y = 300, angle = 0, lenght = 5;
   
    App.SetFramerateLimit(200);
   
    while(App.IsOpened())
    {
        sf::Event Event;
        while (App.GetEvent(Event))
                {
                        if (Event.Type == sf::Event::Closed)
                        {
                            App.Close();
                        }
               
                        if(Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Up)
                        {
                            x = x - lenght * cos(angle*PI/180);
                            y = y - lenght * sin(angle*PI/180);
                           
                        }
                       
                        if(Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Down)
                        {
                            x = x + lenght * cos(angle*PI/180);
                            y = y + lenght * sin(angle*PI/180);
                        }
                       
                        if(Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Right)
                        {
                            angle += 1;
 
                            Gun.Rotate(1);
                        }
                       
                        if(Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Left)
                        {
                            angle -= 1;
                           
                            Gun.Rotate(-1);
                        }
                }
       
        Gun.SetPosition(x,y);
       
        App.Clear();
        sf::String text("x: " + int2Str(x) + "y: " + int2Str(y) + "angle: " + int2Str(angle)
        + "sprite angle: " + int2Str(Gun.GetRotation()), sf::Font::GetDefaultFont());
        text.SetPosition(0.f,0.f);
        App.Draw(text);
        App.Draw(Gun);
        App.Display();
    }
   
    return 0;
}


my picture, it is only simple straight line

15
Network / Problem with using TCP connecting
« on: April 04, 2011, 03:11:08 pm »
I am learning network programming and I am trying examples from tutorial but I cannot understand how it cans work when it giving me a error message.



And my second question is why it doesnt work when I write the IP from whatismyip.com as ServerAddress. I have Smart Security 4 and Windows Firewall on.



Please explain it to me :roll: .
Server.cpp

Code: [Select]
#include <SFML/Network.hpp>
#include <iostream>

int main()
{
    // Create a TCP socket for communicating with clients
    unsigned short Port = 4567;
    sf::SocketTCP Server;

    // Listen to a port for incoming connections
    if (!Server.Listen(Port))
        std::cout << "Error while starting listening\n";


    std::cout << "Server is listening to port " << Port << ", waiting for connections... " << std::endl;

    // Wait for a connection
    sf::IPAddress ClientAddress("95.102.145.1");
    sf::SocketTCP Client;
    Server.Accept(Client, &ClientAddress);
    std::cout << "Client connected : " << ClientAddress << std::endl;

    // Send a message to the client
    char ToSend[] = "Hi, I'm the server";
    if (Client.Send(ToSend, sizeof(ToSend)) != sf::Socket::Done)
        std::cout << "Error while sending message to the client\n";

    std::cout << "Message sent to the client : \"" << ToSend << "\"" << std::endl;

    // Receive a message back from the client
    char Message[128];
    std::size_t Received;
    if (Client.Receive(Message, sizeof(Message), Received) != sf::Socket::Done)
        std::cout << "Error while receiving message from the client\n";

    // Show the message
    std::cout << "Message received from the client : \"" << Message << "\"" << std::endl;

    // Close the sockets when we're done
    Client.Close();
    Server.Close();

    std::cin.get();

    return 0;
}


Client.cpp
Code: [Select]
#include <SFML/Network.hpp>
#include <iostream>

int main()
{
    // Ask for server address
    unsigned short Port = 4567;

    sf::IPAddress ServerAddress;

    do
    {
        std::cout << "Type address or name of the server to connect to : ";
        std::cin  >> ServerAddress;
    }
    while (!ServerAddress.IsValid());

    // Create a TCP socket for communicating with server
    sf::SocketTCP Client;

    // Connect to the specified server
    if (!Client.Connect(Port, ServerAddress))
        std::cout << "Error while connecting to server\n";

    std::cout << "Connected to server " << ServerAddress << std::endl;

    // Receive a message from the client
    char Message[128];
    std::size_t Received;
    if (Client.Receive(Message, sizeof(Message), Received) != sf::Socket::Done)
        std::cout << "Error while receiving message from the server\n";

    // Show it
    std::cout << "Message received from server : \"" << Message << "\"" << std::endl;

    // Define a message to send back to the server
    char ToSend[] = "Hi, I'm a client !";

    // Send the message
    if (Client.Send(ToSend, sizeof(ToSend)) != sf::Socket::Done)
        std::cout << "Error while sending message to the server\n";

    std::cout << "Message sent to server : \"" << ToSend << "\"" << std::endl;


    std::cin.get();
    std::cin.get();

    // Close the socket when we're done
    Client.Close();


    return 0;
}

Pages: [1] 2