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 ... 14 15 [16] 17 18 ... 21
226
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).

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

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

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

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

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

232
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

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

234
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

235
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);

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

237
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. :)


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

239
General discussions / Re: How do i use SFML with microsoft live share?
« on: September 12, 2021, 06:53:22 am »
When you debug in live share, the program is only executed on the host. Other people in the share can look at the source code and access the debugger, but they don't have a local copy running.
If you want other people to see what the program is displaying, you'll need to use a screen share program like Zoom or Discord.

240
Graphics / Re: SF::Text not properly rendering
« on: September 01, 2021, 05:50:10 am »
The Get_Font_By_Name function returns a reference to a Font. But it only contains a return for the font found path, if a font wasn't found there's no return, meaning it is returning an invalid reference.
But that's a separate issue (assuming you don't ask for a font with the wrong name).

The main problem appears to be ThisFont in _LoadFonts. The header comments for sfml's loadFromMemory say that the buffer must remain valid for the life of the font (so sounds like it doesn't copy the data, just uses it). But ThisFont is temporary, it is deleted when it goes out of scope after each font in the loop.
You'll need to store the data you get back from the database in something more persistent, copy it into a block of heap memory and store the pointer and size of that block in the Font_t struct as well.

Pages: 1 ... 14 15 [16] 17 18 ... 21
anything