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

Pages: 1 ... 13 14 [15] 16 17 ... 20
211
General / Re: undefined reference to Error
« on: December 03, 2021, 03:17:38 pm »
I usually avoid makefiles at all costs, so this is my memory from about 20 years ago, but iirc...
In your original makefile, the line:
g++ -I src/include -c main.cpp
compiles the file main.cpp, which generates the main.o file.
Then
g++ main.o -o main -L src/lib -l sfml-graphics -l sfml-window -l sfml-system
says to take main.o and the sfml libraries and link them all together to output the final executable called main.
But game.cpp is never compiled into game.o, and game.o isn't linked with main.o.

CPP files are compiled independently. Then they need to be linked together into a single program.

So you'd need something like:
all: compile link

compile:
        g++ -I src/include -c main.cpp
        g++ -I src/include -c game.cpp


link:
        g++ main.o game.o -o main -L src/lib -l sfml-graphics -l sfml-window -l sfml-system
(Or maybe game.o main.o. I can't remember if the order there matters in that situation)

212
General / Re: undefined reference to Error
« on: December 02, 2021, 06:53:40 pm »
It looks like the g++ command line is only linking the main.o. It would also need game.o, which is where the do_something() function is.

213
General / Re: RenderTarget Pointer&Reference issue
« on: November 28, 2021, 08:10:38 pm »
Yep, the Entity pointer there (the "this" value) is null.
Maybe the code that set it up wasn't called, or something failed.

Values of pointers close to 0 (like 0x0000000000000108) are still from a null pointer. It's using the null as the base address of an object that doesn't exist, then offsetting from that to find member variables (resulting in the error at a near 0 value).

214
General / Re: RenderTarget Pointer&Reference issue
« on: November 28, 2021, 03:33:22 pm »
"Access violation reading location 0x0000000000000000" means the program is trying to read from location 0 in memory, which isn't allowed. This is typically caused by using a pointer which is null.
If you run the program in a debugger, it should stop on the actual location of the null dereference. The code you provided might not actually be where things are failing (if it is, then probably either window or entity are null).

215
General / Re: Text and Audio aren't working
« on: November 20, 2021, 01:18:16 am »
For the text, the problem is your font is a local variable in Window::setText(). Once that function ends, the font is deconstructed. As the header for Text::setFont says:
Quote
The font argument refers to a font that must exist as long as the text uses it. Indeed, the text doesn't store its own copy of the font, but rather keeps a pointer to the one that you passed to this function. If the font is destroyed and the text tries to use it, the behavior is undefined.
So the Font should be stored somewhere more permanent.

216
Cool. :)

The reference/copy thing is something to be careful of in other parts of SFML too, such as Texture and Sprite (which work a bit like SoundBuffer and Sound).

217
The new one has two bugs:
- buffer isn't assigned to the sound object (you'll need a sound.setBuffer(buffer) in there when it starts playing)
- auto buffer = soundBuffers.get(soundToPlay); will cause a similar scope destruction issue as before

For the second one, soundBuffers.get(soundToPlay) will return a reference to the SoundBuffer object. But the auto keyword ignores references. So buffer will be a SoundBuffer (a clone of the one in the resourceholder), not a reference to one. This clone is a local variable, and will go out of scope at the end of the function. SoundBuffers, on destruction, remove themselves from any Sound object playing them.
Changing the line to auto &buffer = soundBuffers.get(soundToPlay); should fix it. (So buffer will be a reference to a SoundBuffer that keeps existing)

Although another problem will arise. You need one Sound object for every simultaneous playing sound. If your manager has just the one Sound member, you can only play one sound at a time.

In my own sound manager, I create a new Sound object every time I play a sound. I store it in a collection. Every frame I scan the collection to find all sounds that have finished playing, and delete them.

It's a bit crappy, but it works for what I needed.
sound_manager.h:
#pragma once
#include <SFML/Audio.hpp>

namespace kage
{
        namespace SoundManager
        {
                sf::Sound *playSound(const std::string &filename);
                void update();
                void preload(const std::string& filename);
        }
}
 

sound_manager.cpp
#include <map>
#include <string>
#include "kage/sound_manager.h"


namespace kage
{
        namespace SoundManager
        {
                std::map<std::string, sf::SoundBuffer*>& getSoundBuffers()
                {
                        static std::map<std::string, sf::SoundBuffer*> s_soundBuffers;
                        return s_soundBuffers;
                }
               
                std::vector<sf::Sound*>& getSounds()
                {
                        static std::vector<sf::Sound*> s_sounds;
                        return s_sounds;
                }

                sf::Sound *playSound(const std::string &filename)
                {
                        sf::SoundBuffer *sb;
                        std::map<std::string, sf::SoundBuffer *>::iterator it = getSoundBuffers().find(filename);
                        if (it == getSoundBuffers().end())
                        {
                                sb = new sf::SoundBuffer;

                                if (!sb->loadFromFile(filename))
                                {
                                        delete sb;
                                        return 0;
                                }
                                getSoundBuffers()[filename] = sb;
                        }
                        else
                        {
                                sb = it->second;
                        }

                        sf::Sound *s = new sf::Sound(*sb);
                        s->play();
                        return s;
                }

                void update()
                {
                        for (int i = 0; i < getSounds().size(); ++i)
                        {
                                if (getSounds()[i]->getStatus() == sf::SoundSource::Stopped)
                                {
                                        delete getSounds()[i];
                                        getSounds().erase(getSounds().begin() + i);
                                        --i;
                                }
                        }
                }

                void preload(const std::string& filename)
                {
                        sf::SoundBuffer* sb;
                        std::map<std::string, sf::SoundBuffer*>::iterator it = getSoundBuffers().find(filename);
                        if (it == getSoundBuffers().end())
                        {
                                sb = new sf::SoundBuffer;

                                if (!sb->loadFromFile(filename))
                                {
                                        delete sb;
                                        return;
                                }
                                getSoundBuffers()[filename] = sb;
                        }
                }
        }
}

(Before anybody mentions, yes, preload and playSound duplicate a big chunk of code. I added preload later and didn't get around to refactoring the code)

When a non-looping sound has a status of sf::SoundSource::Stopped, it has finished playing and can be removed. Or you could use a pool system, reuse sounds that have stopped instead of making new ones.

This is an older version, my newer code uses the Entt (Entity Component System) to handle sounds and check if they are playing. But that's still a work in progress, and would be confusing if you aren't familiar with Entt (it's awesome, works great with SFML).

218
Your playSound function creates a Sound object, but it is a local variable. So when the end of playSound is reached, the Sound object goes out of scope and is deleted. The destructor of Sound stops the sound from playing.
You need to keep the Sound object alive for it to keep playing the sound.

219
Window / Re: Not return "canva" when window.display() ?
« on: November 06, 2021, 03:14:31 am »
Your piece of paper analogy is basically right.
SFML uses "double buffering". There are two graphics buffers: front and back. The front buffer is shown on the monitor. The back buffer is the target for draw operations. When display() is called, the front and back buffers are swapped, the previous back buffer becomes the current front buffer and is shown on the monitor, the previous front buffer is now the target for drawing.

If you want to do cumulative rendering (where each thing you draw remains in a single buffer), you can use a RenderTexture.
Have a look at the "Off-screen drawing" section of this tutorial page: https://www.sfml-dev.org/tutorials/2.5/graphics-draw.php
A RenderTexture can be used like a window, you draw into it the same way. Then every frame you draw the RenderTexture onto the window. While the window will still be double buffered, the RenderTexture itself isn't, so you don't

220
Graphics / Re: Snake creation not renders
« on: October 25, 2021, 11:22:22 pm »
You've got 6 rectangleshapes. The first 3 have positions but zero size. The last 3 have 100x100 size but no position.

std::vector<sf::RectangleShape> body(3);
This creates 3 rectangleshapes with default properties (0x0 size).

for (int i = 0; i < 3; i++)
    {
        body.push_back(sf::RectangleShape(sf::Vector2f(100, 100)));
        body.setFillColor(sf::Color::Green);
     }
The push_backs create 3 more rectangleshapes with 100x100 size (so there's now 6 total).

If you remove the (3) from the vector line it should work.

221
What does the contents of C:\Libraries\SFML-2.5.1\include look like?
The error is saying it can't find SFML/Graphics.hpp, so that means it would be looking for C:\Libraries\SFML-2.5.1\include\SFML\Graphics.hpp
Does that file exist in that exact location?

At a guess, maybe your headers are directly in C:\Libraries\SFML-2.5.1\include and not in C:\Libraries\SFML-2.5.1\include\SFML

222
General / Re: How to make setters
« on: September 26, 2021, 10:42:55 am »
this-> player-> setPBodyLeft (this-> player-> getPBody (). left) = j * BoxSize - this-> player-> getPBody (). width;
In this case you are trying to give two different values. The this->player>getPBody().left is passing as an argument and is just setting the left to itself. Effectively it's just doing pBody.left = pBody.left.

At the end is an assignment, but you can't assign to a void function.

Give this a try:
this-> player-> setPBodyLeft (j * BoxSize - this-> player-> getPBody (). width);

223
Graphics / Re: sf::RectangleShape.setFillColor() is not working
« on: September 26, 2021, 07:48:58 am »
You haven't shown any of the member declarations so I'm just guessing at types here, but the problem is the for loops in updateNodes.
In a loop like:
for (auto j : i)
j is a copy of the data from i, not the original.
So when you call update on j, you are setting the colour on a temporary duplicate of the node that is then thrown away. The original node is never updated.

Changing the loop to:
for (auto &j : i)
will make it use the actual nodes instead of copies. You'll probably want that for the for(auto i:nodes) one as well, but no idea what the actual data is there.

224
Graphics / Re: Rendering quads show nothing if texture is ussed
« on: September 14, 2021, 12:23:42 am »
Ah, my mistake.
9+ years of teaching SFML and I've never used a textured vertex array I guess. :)


225
Graphics / Re: Rendering quads show nothing if texture is ussed
« on: September 13, 2021, 07:35:16 pm »
One potential issue, the UV coordinates for a vertex aren't in pixels, they are 0-1 for full width and height. Higher is wrap around (which may not be enabled, I can't remember the default).
So for example {32.f, 16.f} should be {1.f, 1.f} (the texture isn't square, so 1,1 means full width (32 pixels) and full height (16 pixels)).

Pages: 1 ... 13 14 [15] 16 17 ... 20
anything