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

Pages: [1] 2
1
General / Re: dynamic code loading problem
« on: August 13, 2019, 06:17:09 pm »
Try to call the DLL function before unloading the DLL.

I’ve tried that, it doesn’t make a difference.

2
General / dynamic code loading problem
« on: August 13, 2019, 01:08:55 pm »
When I change som attributes in the gameCode.cpp it does seem to succesfully reload the DLL during runtime
however, i don't see the changes on screen, that was made in the code.

i want it to reload during runtime.

CJsfml_002.cpp
#define SFML_STATIC
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include "SFML\Graphics.hpp"

#include <iostream>

float vel = 7.0f;

typedef void (*rectFP)(sf::RectangleShape *);

int main()
{
        sf::RenderWindow window(sf::VideoMode(960, 540), "SFML works!");
        window.setFramerateLimit(60);  
       
        sf::RectangleShape mRect(sf::Vector2f(40.0f, 40.0f));;
        mRect.setPosition(sf::Vector2f(30.0f, 30.0f)); 
        mRect.setFillColor(sf::Color::Green);

        // Getting pointer to the rect
        sf::RectangleShape *rect = &mRect;


        while (window.isOpen())
    {
                int count = 125;
               
               // Loading DLL
                CopyFile("gameCode.dll", "temp_gameCode.dll", false);
                HMODULE hm = LoadLibrary("temp_gameCode.dll");

                if(hm)
                {
                                std::cout << "LOAD LIB SUCCESS!\n";
                }
               
               // Loading DLL function
                rectFP rfp = (rectFP)GetProcAddress(hm, "CreateRect");

                if(rfp)
                {
                                std::cout << "load func SUCCESS!\n";
                }

               // Unloads DLL
                FreeLibrary(hm);
               
                // Using the DLL function
                rfp(rect);

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

        window.clear();
       
        // Drawing the rect that was changed by the DLL-function
        window.draw(*rect);

        window.display();

    }

    return 0;
}

 

gameCode.h
#define SFML_STATIC
#include "SFML\Graphics.hpp"

extern "C"
{
        __declspec(dllexport) void CreateRect(sf::RectangleShape *RS);
}      

 

gameCode.cpp
#include "gameCode.h"

__declspec(dllexport) void CreateRect(sf::RectangleShape *RS)
{
        RS->setSize(sf::Vector2f(100.0f, 200.0f));     
        RS->setFillColor(sf::Color::Blue);
        RS->setPosition(sf::Vector2f(100.0f, 100.0f));
}

 

build.bat
@echo off

:: IMPORTANT!  "=" has to come IMMEDIATLEY after VARIABLE NAME

SET compilerSwitches= /MD /EHsc -Zi -nologo
SET winLibs= user32.lib gdi32.lib winmm.lib Opengl32.lib ws2_32.lib advapi32.lib

SET sfmlDependLibsX64= /LIBPATH:"C:\SFML\SFML-2.5.1_x64\lib" freetype.lib openal32.lib flac.lib vorbisenc.lib vorbisfile.lib vorbis.lib ogg.lib
SET sfmlDependLibsX86= /LIBPATH:"C:\SFML\SFML-2.5.1_x86\lib" freetype.lib openal32.lib flac.lib vorbisenc.lib vorbisfile.lib vorbis.lib ogg.lib

SET sfmlLibsX64= sfml-system-s.lib sfml-graphics-s.lib sfml-window-s.lib sfml-audio-s.lib sfml-network-s.lib
SET sfmlLibsX86= sfml-system-s.lib sfml-graphics-s.lib sfml-window-s.lib sfml-audio-s.lib sfml-network-s.lib

If NOT EXIST ..\buildx64 mkdir ..\buildx64
If NOT EXIST ..\buildx86 mkdir ..\buildx86
If NOT EXIST ..\assets mkdir ..\assets

 pushd ..\buildx64
 
 cl %compilerSwitches% /I ..\include ..\src\CJsfml_002.cpp %winLibs% /link %sfmlDependLibsX64% %sfmlLibsX64%
 cl %compilerSwitches% /I ..\include ..\src\gameCode.cpp /LD %winLibs% /link %sfmlDependLibsX64% %sfmlLibsX64%
 
 popd
 
 @echo x64 build Done!
 
 ::------------------ x86 BUILD (set vcvarsx86) -----------------

 ::pushd ..\buildx86
 
 ::cl /MD /EHsc -Zi -nologo /I ..\include ..\src\CJsfml_002.cpp %winLibs% /link %sfmlDependLibsX86% %sfmlLibsX86%
 
 ::popd
 ::
 ::@echo x86 build Done!

del *.~ *.cpp~ *.bat~ *.un~ *.h~

 

3
SFML projects / Re: Trying to manage Hot loading code with SFML
« on: August 12, 2019, 08:42:26 am »
If you link to static SFML libraries, you must define the SML_STATIC preprocessor symbol, as stated in the tutorial.

Ohooo, lul that was it!

however when i use the extern "C" i get a warning

Code: [Select]
gameCode.h(6): warning C4190: 'CreateRect' has C-linkage specified, but returns UDT 'sf::RectangleShape' which is incompatible with C


is this something that one would have to worry about or can i ignore it?

i need to use extern "C" so that i can find the functions later and store it in a function pointer.

4
SFML projects / Trying to manage Hot loading code with SFML
« on: August 11, 2019, 09:49:12 pm »
Im trying to implement hot loading with SFML.

I manage to do it with a normal C Console application and the process goes like so:

1. create some code and compile it into a .DLL

2. load the code in my main-loop


However when i try to make a DLL with some SFML ckasses/functions I get a lot of unresolved externals, and i can't really understand how this would be solved.

any ideas??

first problem is making a DLL that has some classes/functions from the SFML-library.

gameCode.h:
#include "SFML\Graphics.hpp"

extern "C"
{
        __declspec(dllexport) void CreateRect(sf::Vertex &vert);
}      

 

gameCode.cpp:
#include "gameCode.h"

__declspec(dllexport) void CreateRect(sf::RectangleShape RS)
{
        RS.setSize(sf::Vector2f(2.0f, 2.0f));
       
}


 

build.bat
@echo off

:: IMPORTANT!  "=" has to come IMMEDIATLEY after VARIABLE NAME

SET compilerSwitches= /MD /EHsc -Zi -nologo
SET winLibs= user32.lib gdi32.lib winmm.lib Opengl32.lib ws2_32.lib advapi32.lib

SET sfmlDependLibsX64= /LIBPATH:"C:\SFML\SFML-2.5.1_x64\lib" freetype.lib openal32.lib flac.lib vorbisenc.lib vorbisfile.lib vorbis.lib ogg.lib
SET sfmlDependLibsX86= /LIBPATH:"C:\SFML\SFML-2.5.1_x86\lib" freetype.lib openal32.lib flac.lib vorbisenc.lib vorbisfile.lib vorbis.lib ogg.lib

SET sfmlLibsX64= sfml-system-s.lib sfml-graphics-s.lib sfml-window-s.lib sfml-audio-s.lib sfml-network-s.lib
SET sfmlLibsX86= sfml-system-s.lib sfml-graphics-s.lib sfml-window-s.lib sfml-audio-s.lib sfml-network-s.lib

If NOT EXIST ..\buildx64 mkdir ..\buildx64
If NOT EXIST ..\buildx86 mkdir ..\buildx86
If NOT EXIST ..\assets mkdir ..\assets

 pushd ..\buildx64
 
REM cl %compilerSwitches% /I ..\include ..\src\CJsfml_002.cpp %winLibs% /link %sfmlDependLibsX64% %sfmlLibsX64%
 cl %compilerSwitches% /I ..\include ..\src\gameCode.cpp /LD %winLibs% /link %sfmlDependLibsX64% %sfmlLibsX64%
 
 popd
 
 @echo x64 build Done!
 
 ::------------------ x86 BUILD (set vcvarsx86) -----------------

 ::pushd ..\buildx86
 
 ::cl /MD /EHsc -Zi -nologo /I ..\include ..\src\CJsfml_002.cpp %winLibs% /link %sfmlDependLibsX86% %sfmlLibsX86%
 
 ::popd
 ::
 ::@echo x86 build Done!

del *.~ *.cpp~ *.bat~ *.un~ *.h~

 

compilation Errors:
Code: [Select]
C:\SFML_Projects\SFML_CONSOLE_DEV\CJsfml_002\src>build
gameCode.cpp
   Creating library gameCode.lib and object gameCode.exp
gameCode.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __cdecl sf::RectangleShape::setSize(class sf::Vector2<float> const &)" (__imp_?setSize@RectangleShape@sf@@QEAAXAEBV?$Vector2@M@2@@Z) referenced in function CreateRect
gameCode.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __cdecl sf::RectangleShape::~RectangleShape(void)" (__imp_??1RectangleShape@sf@@UEAA@XZ) referenced in function CreateRect

5
General / batch file replace libraries with variable
« on: July 31, 2019, 01:04:33 pm »
I thought were supposed to be able to set all the libraries in one variable but it doesn't seem to work, if i replace the lin libraries with the variable i get a TON of error becuase the compiler don't seem to link the libraries.

How can this be solved?

THIS WORKS:
Code: [Select]
@echo off

IF NOT DEFINED winLibs SET winLibs = user32.lib gdi32.lib winmm.lib Opengl32.lib ws2_32.lib advapi32.lib
IF NOT DEFINED sfmlDependLibsX64 (SET sfmlDependLibsX64 = /LIBPATH:"C:\SFML\SFML-2.5.1_x64\lib" freetype.lib openal32.lib flac.lib vorbisenc.lib vorbisfile.lib vorbis.lib ogg.lib)
IF NOT DEFINED sfmlDependLibsX86 (SET sfmlDependLibsX86 = /LIBPATH:"C:\SFML\SFML-2.5.1_x86\lib" freetype.lib openal32.lib flac.lib vorbisenc.lib vorbisfile.lib vorbis.lib ogg.lib)

If NOT EXIST ..\buildx64 mkdir ..\buildx64
If NOT EXIST ..\buildx86 mkdir ..\buildx86

pushd ..\buildx64

cl /MD /EHsc -Zi -nologo /I ..\include ..\src\CJsfml_001.cpp /link user32.lib gdi32.lib winmm.lib Opengl32.lib ws2_32.lib advapi32.lib /LIBPATH:"C:\SFML\SFML-2.5.1_x64\lib" freetype.lib openal32.lib flac.lib vorbisenc.lib vorbisfile.lib vorbis.lib ogg.lib  sfml-system-s.lib sfml-graphics-s.lib sfml-window-s.lib sfml-audio-s.lib sfml-network-s.lib

popd

:: ------------------ x86 BUILD (set vcvarsx86) -----------------

:: pushd ..\buildx86
::
:: cl /MD /EHsc -Zi -nologo /I ..\include ..\src\CJsfml_001.cpp user32.lib gdi32.lib winmm.lib Opengl32.lib ws2_32.lib advapi32.lib /link /LIBPATH:"C:\SFML\SFML-2.5.1_x86\lib" freetype.lib openal32.lib flac.lib vorbisenc.lib vorbisfile.lib vorbis.lib ogg.lib  sfml-system-s.lib sfml-graphics-s.lib sfml-window-s.lib sfml-audio-s.lib sfml-network-s.lib
::
::
:: popd

del *.~ *.cpp~ *.bat~ *.un~


when i replace the libraires with the variables set at the top of the script it doesn't work.

THIS DOESN'T WORK:
Code: [Select]
@echo off

IF NOT DEFINED winLibs (SET winLibs = user32.lib gdi32.lib winmm.lib Opengl32.lib ws2_32.lib advapi32.lib)
IF NOT DEFINED sfmlDependLibsX64 (SET sfmlDependLibsX64 = /LIBPATH:"C:\SFML\SFML-2.5.1_x64\lib" freetype.lib openal32.lib flac.lib vorbisenc.lib vorbisfile.lib vorbis.lib ogg.lib)
IF NOT DEFINED sfmlDependLibsX86 (SET sfmlDependLibsX86 = /LIBPATH:"C:\SFML\SFML-2.5.1_x86\lib" freetype.lib openal32.lib flac.lib vorbisenc.lib vorbisfile.lib vorbis.lib ogg.lib)
IF NOT DEFINED sfmlLibsX64 (SET sfmlLibsX64 = /LIBPATH:"C:\SFML\SFML-2.5.1_x64\lib"sfml-system-s.lib sfml-graphics-s.lib sfml-window-s.lib sfml-audio-s.lib sfml-network-s.lib)
IF NOT DEFINED sfmlLibsX86 (SET sfmlLibsX86 = /LIBPATH:"C:\SFML\SFML-2.5.1_x86\lib"sfml-system-s.lib sfml-graphics-s.lib sfml-window-s.lib sfml-audio-s.lib sfml-network-s.lib)

If NOT EXIST ..\buildx64 mkdir ..\buildx64
If NOT EXIST ..\buildx86 mkdir ..\buildx86

pushd ..\buildx64

cl /MD /EHsc -Zi -nologo /I ..\include ..\src\CJsfml_001.cpp %winLibs% /link /LIBPATH:"C:\SFML\SFML-2.5.1_x64\lib" %sfmlDependLibsX64% %sfmlLibsX64%

popd

:: ------------------ x86 BUILD (set vcvarsx86) -----------------

:: pushd ..\buildx86
::
:: cl /MD /EHsc -Zi -nologo /I ..\include ..\src\CJsfml_001.cpp user32.lib gdi32.lib winmm.lib Opengl32.lib ws2_32.lib advapi32.lib /link /LIBPATH:"C:\SFML\SFML-2.5.1_x86\lib" freetype.lib openal32.lib flac.lib vorbisenc.lib vorbisfile.lib vorbis.lib ogg.lib  sfml-system-s.lib sfml-graphics-s.lib sfml-window-s.lib sfml-audio-s.lib sfml-network-s.lib
::
::
:: popd

del *.~ *.cpp~ *.bat~ *.un~


6
General / Re: Is this Efficient (sf::RectangleShape vs tilemaps) ??
« on: July 30, 2019, 10:01:45 pm »
oh, okay!

but this example here:
https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php

seems much more complicated than what i did, are there any differencies??

7
General / Is this Efficient (sf::RectangleShape vs tilemaps) ??
« on: July 30, 2019, 09:40:20 pm »
hey im wondering if this is efficient for say, writing a game like "breakOut"?

Or should one strive for using tilemaps instead of allocating many sf::RectangleShapes?

Say i would want to add some texture to the rectShapes, would this be really inefficient in the long run?

#define SFML_STATIC
#define WIN32_LEAN_AND_MEAN

#include "SFML\Graphics.hpp"

float vel = 0.02f;

int main()
{
        sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
        window.setFramerateLimit(60);  

        sf::RectangleShape tiles[10][10];
        for(int i = 0; i < 10; i++)
        {
                        for(int j = 0; j < 10; j++)
                        {
                                tiles[i][j].setPosition(sf::Vector2f((i * 10), ((j * 10) + 20)));
                                tiles[i][j].setSize(sf::Vector2f(5, 5));
                                tiles[i][j].setFillColor(sf::Color(20*i, 40, 140, 255));
                        }
        }

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


        window.clear();


                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                                for(int i = 0; i < 10; i++)
                                {
                                        for(int j = 0; j < 10; j++)
                                        {
                                                        tiles[i][j].move(sf::Vector2f(1.0f, 0.0f));
                                        }
                                }
                }
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                                for(int i = 0; i < 10; i++)
                                {
                                        for(int j = 0; j < 10; j++)
                                        {
                                                        tiles[i][j].move(sf::Vector2f(-1.0f, 0.0f));
                                        }
                                }
                }
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                                for(int i = 0; i < 10; i++)
                                {
                                        for(int j = 0; j < 10; j++)
                                        {
                                                        tiles[i][j].move(sf::Vector2f(0.0f, -1.0f));
                                        }
                                }
                }
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                {
                                for(int i = 0; i < 10; i++)
                                {
                                        for(int j = 0; j < 10; j++)
                                        {
                                                        tiles[i][j].move(sf::Vector2f(0.0f, 1.0f));
                                        }
                                }
                }

                for(int i = 0; i < 10; i++)
                {
                        for(int j = 0; j < 10; j++)
                        {

                                window.draw(tiles[i][j]);
                        }
                }


        window.display();
    }

    return 0;
}

 

8
General / Re: sfml-static linking
« on: July 30, 2019, 04:57:03 pm »
Allright it worked thanks!

btw i have another problem when i try to bundle the link libraries.
It works for the standard windows LIBS and the LIBS included with sfml (the freetype/vorbis etc..) but not the actual sfml libraries (sfml-graphics-s.lib etc....).

any idea?
heres my batch file which works FINE:

Code: [Select]
@echo off

REM set CompilerFlags = -Zi

SET winLibs = user32.lib gdi32.lib winmm.lib opengl32.lib ws2_32.lib advapi32.lib
SET sfmlDependLibs = /LIBPATH:..\lib freetype.lib openal32.lib flac.lib vorbisenc.lib vorbisfile.lib vorbis.lib ogg.lib

If NOT EXIST ..\build mkdir build

pushd ..\build

cl /MD /EHsc -Zi -nologo /I ..\include ..\src\CJsfml_001.cpp %winLibs% /link /LIBPATH:..\lib %sfmlDependLibs% sfml-system-s.lib sfml-graphics-s.lib sfml-window-s.lib sfml-audio-s.lib sfml-network-s.lib


popd


del *.~ *.cpp~ *.bat~ *.un~


this one, however (when i try to bundle the sfml-graphics-s.lib etc in the sfmlStaticRelLib variable), DOES NOT work:

Code: [Select]
@echo off

REM set CompilerFlags = -Zi

SET winLibs = user32.lib gdi32.lib winmm.lib opengl32.lib ws2_32.lib advapi32.lib
SET sfmlDependLibs = /LIBPATH:..\lib freetype.lib openal32.lib flac.lib vorbisenc.lib vorbisfile.lib vorbis.lib ogg.lib
SET sfmlStaticRelLibs = /LIBPATH:..\lib sfml-system-s.lib sfml-graphics-s.lib sfml-window-s.lib sfml-audio-s.lib sfml-network-s.lib

If NOT EXIST ..\build mkdir build

pushd ..\build

cl /MD /EHsc -Zi -nologo /I ..\include ..\src\CJsfml_001.cpp %winLibs% /link /LIBPATH:..\lib %sfmlDependLibs% %sfmlStaticRelLibs%

popd


del *.~ *.cpp~ *.bat~ *.un~


9
General / sfml-static linking
« on: July 30, 2019, 01:20:00 pm »
hey im getting a few unrseolved externals but i don't know how to resolve them! :)

here's my build.bat:

Code: [Select]
@echo off

REM set Windows_Libs = user32.lib gdi32.lib winmm.lib opengl32.lib ws2_32.lib
REM set SFML_Libs = sfml-graphics-s.lib sfml-window-s.lib sfml-audio-s.lib sfml-network-s.lib sfml-system-s.lib
REM set CompilerFlags = -Zi

If NOT EXIST ..\build mkdir build

pushd ..\build

cl /MD /EHsc -Zi /I ..\include ..\src\CJsfml_001.cpp user32.lib gdi32.lib winmm.lib opengl32.lib ws2_32.lib /link /LIBPATH:..\lib freetype.lib openal32.lib flac.lib vorbisenc.lib vorbisfile.lib vorbis.lib ogg.lib sfml-system-s.lib sfml-graphics-s.lib sfml-window-s.lib sfml-audio-s.lib sfml-network-s.lib

popd

and here are the unresolved externals:

Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27025.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

CJsfml_001.cpp
Microsoft (R) Incremental Linker Version 14.16.27025.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:CJsfml_001.exe
/debug
/LIBPATH:..\lib
freetype.lib
openal32.lib
flac.lib
vorbisenc.lib
vorbisfile.lib
vorbis.lib
ogg.lib
sfml-system-s.lib
sfml-graphics-s.lib
sfml-window-s.lib
sfml-audio-s.lib
sfml-network-s.lib
CJsfml_001.obj
user32.lib
gdi32.lib
winmm.lib
opengl32.lib
ws2_32.lib
sfml-window-s.lib(JoystickImpl.cpp.obj) : error LNK2019: unresolved external symbol __imp__RegCloseKey@4 referenced in function "class sf::String __cdecl `anonymous namespace'::getDeviceName(unsigned int,struct tagJOYCAPSW)" (?getDeviceName@?A0x8939b4a8@@YA?AVString@sf@@IUtagJOYCAPSW@@@Z)
sfml-window-s.lib(JoystickImpl.cpp.obj) : error LNK2019: unresolved external symbol __imp__RegOpenKeyExW@20 referenced in function "class sf::String __cdecl `anonymous namespace'::getDeviceName(unsigned int,struct tagJOYCAPSW)" (?getDeviceName@?A0x8939b4a8@@YA?AVString@sf@@IUtagJOYCAPSW@@@Z)
sfml-window-s.lib(JoystickImpl.cpp.obj) : error LNK2019: unresolved external symbol __imp__RegQueryValueExW@24 referenced in function "class sf::String __cdecl `anonymous namespace'::getDeviceName(unsigned int,struct tagJOYCAPSW)" (?getDeviceName@?A0x8939b4a8@@YA?AVString@sf@@IUtagJOYCAPSW@@@Z)
CJsfml_001.exe : fatal error LNK1120: 3 unresolved externals
 

here's the file im compiling, its just taken from the sfml tutorial page:

#define SFML_STATIC

#include "SFML\Graphics.hpp"

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

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

        window.clear();
        window.draw(shape);
        window.display();
    }

    return 0;
}

 

10
General / drag and select object problem
« on: May 09, 2019, 10:31:03 pm »

#define mouseRPosX mouseRect.getPosition().x
#define mouseRPosY mouseRect.getPosition().y
#define mouseRSizeX mouseRect.getSize().x
#define mouseRSizeY     mouseRect.getSize().y
#define rect1PosX rect1.getPosition().x
#define rect1PosY rect1.getPosition().y
#define rect1SizeX rect1.getSize().x
#define rect1SizeY rect1.getSize().y

 

i just defined some code for a rectshape and my drag ad select rectshape

int main()
{
        sf::RenderWindow window(sf::VideoMode(800, 600), "TITLE", sf::Style::Close | sf::Style::Resize);

        window.setFramerateLimit(60);

        sf::RectangleShape rect1;
       
        rect1.setSize(sf::Vector2f(50.0f, 50.0f));
        rect1.setOutlineThickness(1.0f);
        rect1.setOutlineColor(sf::Color::Red);

       
        sf::RectangleShape mouseRect;
        mouseRect.setSize(sf::Vector2f(0.0f, 0.0f));
        mouseRect.setOutlineThickness(1.0f);
        mouseRect.setOutlineColor(sf::Color::Green);
        mouseRect.setFillColor(sf::Color(0, 100, 0, 100));

        bool mouseSelecting = false;
        bool objectSelected = false;
        float mouseSelectPointX = 0;
        float mouseSelectPointY = 0;

        system("dir");

        while (window.isOpen())
        {
                sf::Event events;

                while (window.pollEvent(events))
                {
                        switch (events.type)
                        {
                                case sf::Event::Closed:
                                {
                                        window.close();
                                } break;

                                case sf::Event::MouseButtonPressed:
                                {

                                        if(events.mouseButton.button == sf::Mouse::Left)
                                        {                                      
                                                mouseSelecting = true;
                                                mouseSelectPointX = mousePosX_F;
                                                mouseSelectPointY = mousePosY_F;
                                       
                                                mouseRect.setPosition(sf::Vector2f(mouseSelectPointX, mouseSelectPointY));
                                        }

                                } break;

                                case sf::Event::MouseButtonReleased:
                                {
                                        if (events.mouseButton.button == sf::Mouse::Left)
                                        {
                                                system("cls");
                                                system("dir");
                                                mouseSelecting = false;
                                                mouseSelectPointX = mousePosX_F;
                                                mouseSelectPointY = mousePosY_F;
                                               
                                                if (((mouseRPosX + mouseRSizeX) >= rect1PosX &&
                                                      (mouseRPosY + mouseRSizeY) >= rect1PosY)           
                                                   )
                                                {
                                                        objectSelected = true;
                                                }
                                                else
                                                {
                                                        objectSelected = false;
                                                }


                                                std::cout << "rec1Pos-x: " <<rect1.getPosition().x << "\nrec1Pos-y: " << rect1.getPosition().y << std::endl;
                                                std::cout << "rect1Pos-x + rect1Siz-x: " << (rect1.getPosition().x + rect1.getSize().x) << "\nrect1Pos-y + rectSiz-y: " << (rect1.getPosition().y + rect1.getPosition().y) << std::endl;

                                                std::cout << "mouseRectPos-x: " << mouseRect.getPosition().x << "\nmouseRectPos-y: " << mouseRect.getPosition().y << std::endl;
                                                std::cout << "mouseRectPos-x + mouseRectSiz-x: " << (mouseRect.getPosition().x + mouseRect.getSize().x) << "\nmouseRectPos-y + mouseRectSiz-y: " << (mouseRect.getPosition().y + mouseRect.getSize().y) << std::endl;
                                               
                                               
                                                mouseRect.setPosition(sf::Vector2f(0.0f, 0.0f));

       
                                        }

                                } break;
                        }
                       
                }

                window.clear(sf::Color(0, 0, 25, 255));

                moveRect(rect1, 5.0f, 5.0f, window);

                //std::cout << "object selected: " << objectSelected << std::endl;

                if (mouseSelecting)
                {
                        mouseRect.setSize(sf::Vector2f(mousePosX_F - mouseSelectPointX, mousePosY_F - mouseSelectPointY));
                }
                else
                {
                        mouseRect.setSize(sf::Vector2f(0.0f, 0.0f));
                }

                if (objectSelected )
                {
                        rect1.setFillColor(sf::Color::Green);
                        if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Right))
                        {
                                rect1.move(0.5f, 0.5f);
                        }
                }
                else
                {
                        rect1.setFillColor(sf::Color::White);
                }
               
                window.draw(rect1);
                window.draw(mouseRect);
                window.display();



        }

        return 0;
}
 

if I press and drag toward +x(right) and + y(down) the "position" of "select rect" is in the top left corner which makes it easy to calculate collision  relative to the object beeing selected from there.

however

if I press and drag toward -x(left) and  -y(up) the"position" of  "select rect" is in the bottom right corner which makes it weird to calculate collision from there.

it feels like i've implemented something wrong here.

any ideas?




11
Network / Re: Connection with getPublicAdress()
« on: April 13, 2019, 11:19:35 pm »
Allright, thanks! :)

12
Network / Connection with getPublicAdress()
« on: April 13, 2019, 11:15:57 am »
I have two computers at Home which has the same public IP-adress.
The computers have diffrenet local IP’s.

Is it possile to send/receive using the public adress or must one use the local adress?

using the local adress works because i’ve tested it but when i change to the public adress, the server/client communication doesn’t work.

The program i’ve coded is just at simple testing program using UdpSockets nothing fancy.

13
Network / Re: why doesn't sf::udpSocket::anyport work here?
« on: April 10, 2019, 11:00:27 pm »
I think i solved myself.

when you open up two different programs, sf::UdpSocket::Anyport will generate two different ports for:

unsigned short clientPort;

thus the "client" will not receive the information sent from the server.

On the other hand when I assign a predetermined value for the port like:

unsigned short clientPort = 4001;

the clienPort will be the same in both programs running and therefore the "client will receive the information!

:)

14
Network / Re: why doesn't sf::udpSocket::anyport work here?
« on: April 10, 2019, 05:06:13 pm »
in this example i guess sf::UdpSocket::Anyport generates a port that's not occupied and assigns it to "port".

and that's what it does because the program displays the port with getlocalport().

the thing is that inside let's call it "server mode":

if (socket.send(buffer, sizeof(buffer), IP, clientPort) != sf::Socket::Done)

here clientPort == 0;

but in "client mode":

if (socket.bind(clientPort) != sf::Socket::Done)

here clientPort contains the port that was assigned with sf::UdpSocket::Anyport;


but when i hard code the ports with a value, like:

 unsigned short clientPort = 4001;

clientPort IS assigned 4001 in both "server mode" AND"client mode".



since i don't know what port would be availabe on some other computer, I thought it would be a good idea to let the program find a free port by itself rather than me hardcoding a port value...

please let me know if im thinking wrong here :)

15
Network / [SOLVED] why doesn't sf::udpSocket::anyport work here?
« on: April 10, 2019, 04:27:33 pm »
        char avsluta = 'n';
        char serverOrClient = '0';
        char buffer[200] = "";
        std::string message = "";
        sf::IpAddress IP = sf::IpAddress::getLocalAddress();
        unsigned short serverPort = sf::UdpSocket::AnyPort;
        unsigned short clientPort = sf::UdpSocket::AnyPort;[/glow]

        std::cout << "Your IP-address: " << IP << std::endl;

        std::cout << "Server (s) or client (c)?: ";
        std::cin >> serverOrClient;


        bool done = false;

        while (!done)
        {
                if (serverOrClient == 's')
                {
                        sf::UdpSocket socket;
                        if (socket.bind(serverPort) != sf::Socket::Done)
                        {
                                std::cout << "server kunde inte binda till port\n\n";
                        }
                        else
                        {
                                std::cout << "Server -  " << "port: " << socket.getLocalPort() << " IP-address: " << sf::IpAddress::getLocalAddress() << "\n\n";
                               
                        }
                        std::cout << "\n\nSend message: ";
                        std::cin >> buffer;

                        if (socket.send(buffer, sizeof(buffer), IP, clientPort) != sf::Socket::Done)
                        {
                                std::cout << "Kunde inte senda! \n\n";
                        }
                        else
                        {
                                std::cout << "Message sent: '" << buffer << "' to client at port: " << clientPort << "\n\n";
                        }
                       
                }
                else if (serverOrClient == 'c')
                {
                        sf::UdpSocket socket;
                        if (socket.bind(clientPort) != sf::Socket::Done)
                        {
                                std::cout << "Kunde inte binda! \n\n";
                        }
                        else
                        {
                                std::cout << "Client - " << "port: " << socket.getLocalPort() << " IP-address: " << sf::IpAddress::getLocalAddress() << "\n\n";
                        }

                        std::size_t received;
                        if (socket.receive(buffer, sizeof(buffer), received, IP, serverPort) != sf::Socket::Done)
                        {
                                std::cout << "kunde inte receiva! \n\n";
                        }
                        else
                        {
                                std::cout << "Sender: " << IP << " ||  Port : " << serverPort << std::endl;
                                std::cout << "meddelande mottaget: " << buffer << std::endl;
                        }


                }


                std::cout << "avsluta? j/n: ";
                std::cin >> avsluta;
                if (avsluta == 'j')
                {
                        done = true;
                }
        }
 

Hello! if i assign clientPort and serverPort manually with values like:

   unsigned short serverPort = 4000;
   unsigned short clientPort = 4001;


this code manages to send information from server to client.

but when i do this:

   unsigned short serverPort = sf::UdpSocket::AnyPort;
   unsigned short clientPort = sf::UdpSocket::AnyPort;


The server sends to port 0 and not the actuall clientPort.
So no message is reveived by the client.

why is this?

Pages: [1] 2
anything