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

Pages: [1]
1
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~

 

2
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

3
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~


4
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;
}

 

5
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;
}

 

6
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?




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

8
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?

9
General / turn-based countdown
« on: April 02, 2019, 10:25:21 pm »
#ifndef SFML_STATIC
#define SFML_STATIC

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

int main()
{


   sf::RenderWindow window;

   window.create(sf::VideoMode(800, 600), "TITLE", sf::Style::Close | sf::Style::Resize);

   window.setFramerateLimit(60);
   sf::Font font;

   
   if (!font.loadFromFile("Enchanted Land_0.otf"))
   {

   }

   sf::Clock clock;
   sf::Time time;
   int seconds{ 10 };
   std::string counter = std::to_string(seconds);


   sf::Text text;
   text.setFillColor(sf::Color::Red);
   text.setString(counter);
   text.setFont(font);
   sf::Event events;

   clock.restart();
   while (window.isOpen())
   {
      time = clock.getElapsedTime();

      std::cout << (int)time.asSeconds() << std::endl;

      if ((int)time.asSeconds() == 1)
      {
         counter = std::to_string(seconds);
         text.setString(counter);
         --seconds;
         clock.restart();

         if (seconds < 0)
         {
            text.setString("GAME OVER");
            text.setPosition(window.getSize().x / 2, window.getSize().y / 2);
            
         }
      }

         
      

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

         if (events.type == sf::Event::KeyPressed)
         {
            if (events.key.code == sf::Keyboard::A)
            {
               window.close();
            }
         }
         
      }


      window.clear(sf::Color::Blue);

      window.draw(text);

      window.display();
   }


}

#endif





Does anyone know a better/more efficient way to make a countdown implementation?
Or am i on the right track with this?


The thing is that i need a countdown for a turn-based game state.

each player has 10 seconds at her disposal before the the turn changes to the second player.

the countdown should be visible somewhere on the screen like it is here.

10
Window / Menu-Bug invloving sf::Event::keyPressed
« on: March 25, 2019, 11:51:33 pm »
if (this->mainMenuActive)
{
   mainMenuEvents();
}

if (this->startMenuActive)
{
   this->startMenuEvents();
}

if (this->resourceMenuActive)
{
   this->resourceMenuEvents();
}

void Game::mainMenuEvents()
{

   switch (this->events.key.code)
   {
      case sf::Keyboard::Down:
      {

         ++this->mainMenuHighlight;
         if (this->mainMenuHighlight > MAIN_COMMAND)
         {
            this->mainMenuHighlight = MAIN_RESOURCE;
         }

      }break;


      case sf::Keyboard::Up:
      {

         --this->mainMenuHighlight;
         if (this->mainMenuHighlight < MAIN_RESOURCE)
         {
            this->mainMenuHighlight = MAIN_COMMAND;
         }

      }break;


      case sf::Keyboard::Enter:
      {
         this->mainMenuActive = false;

         if (this->mainMenuHighlight == MAIN_RESOURCE)
         {            
            this->resourceMenuActive = true;
            std::cout << "Resource: " << resourceMenuActive << std::endl;
         }         

      }break;

      case sf::Keyboard::Escape:
      {

         this->mainMenuActive = false;
         this->startMenuActive = true;

      }break;
   }
}


void Game::startMenuEvents()
{
   switch (this->events.key.code)
   {
   case sf::Keyboard::Down:
   {
      ++this->startMenuHighlight;
      if (this->startMenuHighlight > START_EXIT_GAME)
      {
         this->startMenuHighlight = START_NEW_GAME;
      }

      std::cout << "start: " << this->startMenuHighlight << std::endl;

   } break;

   case sf::Keyboard::Up:
   {
      --this->startMenuHighlight;
      if (this->startMenuHighlight < START_NEW_GAME)
      {
         this->startMenuHighlight = START_EXIT_GAME;
      }

      std::cout << "start: " << this->startMenuHighlight << std::endl;

   } break;

   case sf::Keyboard::Enter:
   {
      if (this->startMenuHighlight == START_NEW_GAME)
      {
         this->startMenuActive = false;
         this->mainMenuActive = true;
      }

      if (this->startMenuHighlight == START_EXIT_GAME)
      {
         this->gameIsRunning = false;
      }
   }

   }
}


void Game::resourceMenuEvents()
{
   
   switch (this->events.key.code)
   {
      case sf::Keyboard::Down:
      {

         ++this->resourceMenuHighlight;
         if (this->resourceMenuHighlight > CHEMIST)
         {
            this->resourceMenuHighlight = BANKMAN;
         }

      }break;


      case sf::Keyboard::Up:
      {

         --this->resourceMenuHighlight;
         if (this->resourceMenuHighlight < BANKMAN)
         {
            this->resourceMenuHighlight = CHEMIST;
         }

      }break;

      case sf::Keyboard::Enter:
      {
         if (resourceMenuHighlight == BANKMAN)
         {
            ++this->overviewInfoValue;
         }

      } break;

      case sf::Keyboard::Escape:
      {
         this->resourceMenuActive = false;
         this->mainMenuActive = true;
      }break;
   }
}


----(THIS IS JUST A LITTLE BIT OF CODE FROM THE PROGRAM)----

My program renders 3 menus  at the moment: startMenu -> mainMenu -> resourceMenu.

all menus handle sf::Event::KeyPressed: (Enter/Up/Down).

in resourceMenu when i press enter a number gets updated on the screen.

THE PROBLEM is that when i press enter from the mainMenu-Screen, the resourceMenu also registers that Enter was pressed and thus the resource number gets updated as soon as the resourceMenu is rendered to the screen.



What i want to achieve is this:

mainMenu (if Enter is pressed) -> resourceMenu displayed (if Enter is pressed) -> update resourceValue.



what i have:

mainMenu (if Enter is pressed) -> resourceMenu displayed (and since you pressed Enter in mainMenu resourceValue will be updated from that keyPress)


i guess i want to separate the sf::keyboard::Enter from each menu and make it so that the menus don't respond to eachothers keypress-Events.



Thanks in advance!



Pages: [1]
anything