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

Pages: 1 2 [3]
31
Graphics / Re: OpenGL error INVALID_OPERATION occurring for no reason?
« on: February 08, 2020, 08:55:55 pm »
I am working on a simple 2d game in sfml, and I recently ran into a problem when trying to write a mod system for the game. Basically, the mod system works by loading a dll into memory, and calling functions from it that return a subclass of one defined in my game. The loading works fine and the object is instantiated correctly, but it appears to have broken part of my rendering code that worked before, namely this part:


void Environment::render(sf::RenderWindow& window) {
        for (Spawner* spawner : m_spawners) {
                spawner->render(window);
        }

        for (Enemy* enemy : m_enemies) {
                enemy->render(window);
        }

        m_player.render(window);

        sf::RectangleShape shape(sf::Vector2f(20, 400));
        shape.setFillColor(sf::Color::Black);
        window.draw(shape);

        shape.setPosition(420, 0);
        window.draw(shape);

        shape.setSize(sf::Vector2f(400, 20));
        shape.setPosition(0, 0);
        window.draw(shape);

        shape.setPosition(20, 420);
        window.draw(shape);
}

While the spawners are rendering, the console prints a bunch of opengl errors. After further investigation with the debugger (visual studio). It appears that my spawner->render() function is never being called, which is odd. I'm not entirely sure what is happening here as this code works fine with spawners that are instantiated in the executable and not loaded from the dll. I'm new to loading a dll at runtime, is there some sort of problem I'm missing here?

It is quite hard to see what's happening, but before I would ask for info about what does what, the fact that spawner->render is not called when it should be is enough to start debugging the problem urself. Put a break pointer on the for loop and look into the memory at m_spawners

32
Graphics / Re: Vector of structs which contains sprites
« on: February 08, 2020, 08:22:06 pm »
Im trying to do a vector of struct elements to draw them in RenderWindow;

my struct for non-ui objects:

struct element{
    sf::Texture texture;
    sf::Sprite sprite;
    sf::IntRect rect;
    sf::Text text;
};


my struct for ui objects:

struct uiElement: element{
    sf::Text text;
    bool clickable;
};


Im adding elements like that:

std::vector<uiElement> ui;

uiElement el;

el.texture.loadFromFile("../res/textures/background.jpg");
ui.push_back(el);
ui.back().sprite.setTexture(ui.back().texture);


and it works!But, only if my "std::vector<uiElement> ui;" has one element;

when i try to add more with cycle:

for (int i = 0;i < 4;i++){
                    uiElement btn;
                    btn.texture.loadFromFile("../res/textures/button.png");
                    ui.push_back(btn);
                    ui.back().sprite.setTexture(ui.back().texture);
}


draws only last object in my "std::vector<uiElement> ui;" and the other are just white

my drawing part of code:

for (auto & element : ui){
        window.draw(element.sprite);
}


How can I fix it??

thanks in advance :)

At the first glance the way u use the std::vector really drags my attention, and this is what I think it's happening in ur for loop:
- create a uiElement named btn
- load its texture
- !!! push into the vector a COPY of the btn
- !!! set texture of a sprite of an ANOTHER COPY from the vector

push_back() and back(), if I'm not wrong, both takes the parameter (or returns it) by value, which means u set the sprite for a completely different obj, that u discard after. U're pushing and getting back copies, and not the original obj which u try to draw later. A way that I use most of times would be to make ur vector of pointers (uiElement*). If u're a beginner this may not be the best method since u must take care of freeing the memory after and stuff. U could, as another example, pushing into the vector after u set the sprite. Another way is to rather than using back(), get the address of the last element in vector, maybe like "&ui[ui.size() - 1]" if I'm not wrong, and change stuff on the pointer u get (pointer = &obj)

33
General / Re: Problem Full static link SFML 2.5.1 on visual studio 2017
« on: February 07, 2020, 08:13:36 pm »
-------
Background to problem (skip if uninterested)
-------
I create new empty project with SFML 2.5.1 follow this guide: https://www.sfml-dev.org/tutorials/2.5/start-vc.php. I try to use full static linker.
-------

Operating system: Windows 10
Visual studio version: Visual studio 2017
SFML Version: 2.5.1-vc15-32-bit

In all configurations I have:
C/C++ >> General >> Additional Include Directories >> linked to the include folder
Linker >> General >> Additional Library Directories >> linked to the lib folder
C/C++ >> Preprocessor >> SFML_STATIC

in my release I have:
Linker >> input >> additional dependencies
opengl32.lib
winmm.lib
gdi32.lib
freetype.lib
sfml-system-s.lib
sfml-window-s.lib
sfml-graphics-s.lib


in Debug:
Linker >> input >> additional dependencies
freetype.lib
gdi32.lib
winmm.lib
opengl32.lib
sfml-system-s-d.lib
sfml-window-s-d.lib
sfml-graphics-s-d.lib

=============
But I can not build project successfully. It raised LNK2019 and LNK2001 error like that:
Error   LNK2019   unresolved external symbol __imp__GetDC@4 referenced in function "public: bool __thiscall sf::priv::CursorImpl::loadFromPixels(unsigned char const *,class sf::Vector2<unsigned int>,class sf::Vector2<unsigned int>)" (?loadFromPixels@CursorImpl@priv@sf@@QAE_NPBEV?$Vector2@I@3@1@Z)   SFMLExample   C:\CppTraining\TrainingCpp\SFMLExample\sfml-window-s-d.lib(CursorImpl.cpp.obj)   1   
Error   LNK2001   unresolved external symbol __imp__GetDC@4   SFMLExample   C:\CppTraining\TrainingCpp\SFMLExample\sfml-window-s-d.lib(WglContext.cpp.obj)   1   

I try to search but don't find the problem. Please help me !!
Thanks

Open the project properties and make sure that current configuration is on "Active", same as platform. Now recheck everything. If something is not right, then u found the problem

34
General / Re: basic text box
« on: February 07, 2020, 06:30:55 am »
I found most of what I needed in an old post - I can now save user input into a string and 'echo' each char to the screen at the location of a prompt like I want:

 while (window.pollEvent(e)) {
     if (e.type == Event::KeyPressed) {              
         if (e.type == Event::TextEntered) {
              if (e.text.unicode < 128) {
                  input += static_cast<char>(e.text.unicode);
                  output.setFillColor(Color::White);
                  output.setString(input);
              }
          }
      }
.
.
.
window.draw(prompt);
window.draw(output);

What I need now is a way to make the 'backspace' button work like it's supposed to. Will I have to manually pop a char off the end of the string for each "Keyboard::Backspace" detected, or does SFML have something for that?

If u have a std::string called input which u use to set the Text object, then u need to pop the last char from std::string. There is a fuction substr for std::string which will return a substring (basically u cut the string)

35
General / Re: Problem displaying sprite
« on: February 06, 2020, 08:00:52 pm »
I'll try thati, but it is weird, because it only happens with some textures, maybe its because of the DPI... But when using an image viewer it doesn't happen, but I'll definitely try that

U have to pay attention to what image viewer to use cause the ones that are more for daily users rather than devs apply antialiasing (probably matters just if work with low resolutions). With the bare eye I can some effects applied on the first one (in case u're sure they are the same file with same res)

36
General / Re: Problem displaying sprite
« on: February 06, 2020, 05:17:02 pm »
Ask and you shall receive  ;)

This is where the texture is loaded, those are just strings, and this function executes `loadFromFile` on the texture and saves a reference to it in an hashtable.
Code: [Select]
assets.loadTexture(GM_GRID_MINI, GRID_MINI);
This is where the texture is set to a Sprite (this->grid is a sf::Sprite, and assets is the hashtable)
Code: [Select]
this->grid.setTexture(this->data->assets.getTexture(GM_GRID_MINI));
this->grid.setOrigin(halfWidth, halfHeight); // Half the height and width of the sprite
this->grid.setPosition(x, y); // This is simplified, it is not just x and y, but it works fine

And this is where it is displayed:
Code: [Select]
this->data->window.clear(//COLOR);
this->data->window.draw(this->grid);
this->data->window.display()

I don't know what am I doing wrong

Look into the documentation. Idk if u ever worked with metal OpenGL, but u can load a texture with different modes. Those are available in SFML too under a way or another, so on Texture u have things such as setSmooth(). Try all of those

37
I'm late, but if u still watch this thread, then u should know it is not easy to indetify the problem without enough details. Where exactly it throws? The line. Does the player appears on the screen?

38
General / Re: basic text box
« on: February 06, 2020, 10:12:25 am »
If I were relatively new to C++ and SFML and were looking for a way to incorporate into my game a text box for user input - something vaguely like a dos shell - would there be a tutorial or guide you might suggest?

DOS shell with SFML? U could make an array of text. When receiving input and shell on focus, append the char to last text obj. On new line u could remove from top of array an obj and add at end another one. I recommend having some exp with C++ before diving in cause is not easy for beginners (maybe)

39
General / Re: Problem displaying sprite
« on: February 06, 2020, 10:05:33 am »
"Talk is cheap. Show me the code"

40
General / Re: How to approach multiple windows over multiple threads
« on: February 02, 2020, 05:36:54 pm »
I fixed the problem. Regardless the last code edit, I had to make sure to close() the window from the thread it was created. I expected this one to work from a different thread

If someone use this thread as reference for his problem, I had change the while within thread from while(window->isOpen()) to while(settings.radarActive) which leads to the close() function be actually called by the window destructor (since I do delete at end) rather than me manually (which I did in order to make window->isOpen() return false)

41
General / Re: How to approach multiple windows over multiple threads
« on: February 02, 2020, 04:12:36 pm »
I just seen the mistake. I don't handle events in the other thread, but indeed may cause problems later, so I updated how to thread works:

Code: [Select]
drawThread = new std::thread([&]() {
window = new sf::RenderWindow(sf::VideoMode(200, 200), "", sf::Style::None);
window->setFramerateLimit(fps);
handle = window->getSystemHandle();
while (window->isOpen()) {
window->clear();
for (auto circle : radar)
if (circle->getPosition().x != -99999)
window->draw(*circle);
window->display();
}
delete window;
window = 0;
});

The results are EXACTLY the same, regardless using sf::Context or setActive(). Do u have any idea from here?

42
General / How to approach multiple windows over multiple threads
« on: February 01, 2020, 05:28:58 pm »
To be honest, I work right now on a cheat for a specific game. I have to use threads as I need to perform different instructions at regular time intervals and I also don't want to block the main thread. While some of them are just for computation (such as a console, fetching game data, etc) I also have some that should handle different windows. As example: I need to make a thread which creates a window that will draw a radar. I managed it to open, but not to close, which means it works partially. Below is a resume of the part that involves SFML:

Code: [Select]
void modifyRadar() {
if (radarActive) {
radarWindow = new sf::RenderWindow(sf::VideoMode(200, 200), "", sf::Style::None);
radarWindow->setFramerateLimit(fps);
radarDrawThread = new std::thread([&]() {
while (radarWindow->isOpen()) {
radarWindow->clear();
for (auto circle : radar)
if (circle->getPosition().x != -99999)
radarWindow->draw(*circle);
radarWindow->display();
}
delete radarWindow;
radarWindow = 0;
});
return;
}
radarWindow->close();
radarDrawThread->join();
delete radarDrawThread;
}

All the variable are available in the outer scope. I did read posts about what I want to do and tried changes such as:

Code: [Select]
radarDrawThread = new std::thread([&]() {
sf::Context context;
radarWindow->setActive();
while (radarWindow->isOpen()) {
radarWindow->clear();
for (auto circle : radar)

In the first example, this is what stdout shows:

Code: [Select]
...

An internal OpenGL call failed in RenderTarget.cpp(152).
Expression:
   glClearColor(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f)
Error description:
   GL_INVALID_OPERATION
   The specified operation is not allowed in the current state.

An internal OpenGL call failed in RenderTarget.cpp(153).
Expression:
   glClear(GL_COLOR_BUFFER_BIT)
Error description:
   GL_INVALID_OPERATION
   The specified operation is not allowed in the current state.

An internal OpenGL call failed in Texture.cpp(782).
Expression:
   glBindTexture(GL_TEXTURE_2D, 0)
Error description:
   GL_INVALID_OPERATION
   The specified operation is not allowed in the current state.

...

The functions in the while loop are not hit anymore since the window has closed (expected behavior). When I use the example with sf::Context, when the sf::Context object has to destruct itself an exception is thrown:

Code: [Select]
Exception thrown: read access violation.
this-> was 0xFFFFFFFFFFFFFFE7.

I do realize that there may be better options for the thing I want to do, especially that I didn't shown the full code due to its size, and while I appreciate very much the opinions, I would like to know how to render different windows in different threads at least for knowledge. I tried different combinations of the sf::Context, setActive(), and the order the windows are created. Before the exception is thrown, this is what stdout has:

Code: [Select]
Failed to activate OpenGL context: The operation completed successfully.

From what I understand: to draw multiple windows u can use the default sf::Context and use setActive() to manage where the OpenGL calls go, or use multiple sf::Context and no need for setActive() since every window has its context. I also seen that setActive() shall be called also when I render a specific window in another thread rather than where it was made

Pages: 1 2 [3]
anything