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 - That Martin Guy

Pages: [1] 2
1
Graphics / Re: New to lighting - Confused about how to handle drawing
« on: April 20, 2020, 12:41:30 am »
From what I can tell, everything is working as it should now! Thanks for the help and great explanations!

2
Graphics / Re: New to lighting - Confused about how to handle drawing
« on: April 19, 2020, 11:10:45 pm »
IMPORTANT: remember to clear, draw, and display on the render texture too!
Without that, the previous buffer can be shown and sometimes it's flipped (this isn't necessarily the issue).

This seems to have been the issue - clearing and displaying fixes it!

Are you using views? The render texture can have the same view as the window to make it more simple.
Doing this breaks the positioning... again. I still think I need to do this, and I'm just not doing it correctly. I'm modifying the view coordinates in order to try and put the player at the center of it. This works for the actual camera, but not for the RenderTexture. This again puts the lighting rectangle at the bottom left of the screen, and neither the black RenderTexture nor the white lighting rectangle seem to move at all. My drawing code together with the camera logic looks like the following:

        public void GameLoop()
        {
            mainView = window.GetView();
            RenderTexture renderTexture = new RenderTexture(1920, 1080);

            while(window.IsOpen)
            {
                    CenterCameraOnUnit(selectedUnit);

                    renderTexture.SetView(mainView);
                    window.SetView(mainView);

                    window.Clear();

                    renderTexture.Clear();

                    RectangleShape shape = new RectangleShape(new Vector2f(120, 120));
                    renderTexture.Draw(shape);

                    //Draw sprites

                    RenderStates states = RenderStates.Default;
                    states.BlendMode = BlendMode.Multiply;

                    renderTexture.Display();

                    Sprite renderSprite = new Sprite(renderTexture.Texture);

                    window.Draw(renderSprite, states);

                    window.Display();
            }
        }

        private void CenterCameraOnUnit(Unit unit)
        {
            Vector2f newViewPos = new Vector2f(unit.position.X + 8, unit.position.Y + 8);
            if(newViewPos.X - mainView.Size.X / 2 < 0)
                newViewPos.X = mainView.Center.X;
            if(newViewPos.Y + mainView.Size.Y / 2 > window.DefaultView.Size.Y)
                newViewPos.Y = mainView.Center.Y;
            mainView.Center = newViewPos;
        }
 

In the image you can see the red character in the middle of the screen "peeking out" of the black RenderTexture. The character's Y-coordinate is negative, and demonstrates that the RenderTexture does not go above origin. The white rectangle in the bottom left is the lighting (blending disabled) and should be positioned at origin, which is slightly to the left of the red character.

3
Graphics / Re: New to lighting - Confused about how to handle drawing
« on: April 19, 2020, 12:44:19 pm »
I really like that explaination, thank you! The way I did it works very well, with just one issue - the origin position on the render texture seems to be messed up.

The code is currently as follows:
while(window.IsOpen)
{
    //..

    //Not sure if I should remake this texture every game loop or if I just need to instantiate it once before it
    RenderTexture renderTexture = new RenderTexture(1920, 1080);
   
    RectangleShape lightingShape = new RectangleShape(new Vector2f(120, 120))
    {
         //Position should be at (0, 0) by default, but I want to explicitly set it to make sure it's there
         Position = new Vector2f(0, 0)
    };

    renderTexture.Draw(lightingShape);

    //Regular drawing stuff...

    //Not sure if this is the best way to set the blend mode, but I could put it directly in the window Draw call
    RenderStates states = RenderStates.Default;
    states.BlendMode = BlendMode.Multiply;

    window.Draw(renderSprite, states);

    window.Display();
}
 
According to the above code it seems to me the lighting square should be at the top left. However, as you can see from the below picture, it is in the bottom left instead (blending disabled in order to make it clearer)

Setting the position to (200, 200) results in it moving up and to the right:


I tried using MapPixelToCoords in order to set it relative to the window rather than the map, but it seems to have no effect.

4
Graphics / New to lighting - Confused about how to handle drawing
« on: April 18, 2020, 09:46:22 pm »
In the past I've done lighting in games by simply drawing a black texture on top of everything and then cutting a hole in the sprite where the player is/where the lighting is supposed to originate. However, from what I can find SFML does not offer this option and therefore I wanted to check out a more dynamic solution.

After looking around for a good while I think I understand the general idea of how games do this in general, but I do not understand how to do this in SFML in particular.

From what I understand you do the following:
  • Draw a "dark" overlay, which can range in color and opacity depending on how much you want to see through it.
  • Draw a "lighting" source multiplicatively
  • Draw sprites on top of this

The problem is that I have no clue how to tell the game how to draw something multiplicatively. I also am not sure if this is how you approach the problem.

I am using SFML.net, for the record.

5
General / 9557 segmentation fault (core dumped)
« on: May 21, 2017, 12:19:36 pm »
I have installed a virtual machine which runs manjaro. This virtual machine is the one I use to program on. While working on a small game I encountered an error when trying to launch ./a.out. The code compiles fine when I use
Quote
g++ *.cc *.h *.cpp -std=c++14 -lsfml-system -lsfml-graphics -lsfml-window -Wall -Wextra
, but it immediatly closes when I try and run it, and gives me that error. I got this error while reworking my main menu code.

Game.cpp
#include <iostream>
#include "Game.h"
#include "MainMenu.h"

void Game::start()
{
        if(gameState != Uninitialized)
                return;
       
        window.create(sf::VideoMode(1024, 768), "Window");
        gameState = Game::Menu;

        while(!isExiting())
        {
                gameLoop();
        }

        window.close();
}

bool Game::isExiting()
{
        return gameState == Exiting;
}

void Game::gameLoop()
{
        switch(gameState)
        {
                case Menu:
                        handleMenu();
                        break;
                default:
                        break;
        }      

        sf::Event event;
        while(window.pollEvent(event))
        {
                switch(event.type)
                {
                        case sf::Event::Closed:
                                gameState = Exiting;
                                break;
                       
                        case sf::Event::MouseEntered:
                                window.setMouseCursorVisible(false);
                                break;
                        case sf::Event::MouseLeft:
                                window.setMouseCursorVisible(true);
                                break;

                        case sf::Event::KeyPressed:
                                if(gameState == Menu)
                                {
                                        MainMenu menu;
                                        menu.navigate(event);
                                }

                        default:
                                break;
                }
        }
        window.display();
}

void Game::handleMenu()
{
        window.setKeyRepeatEnabled(false);
        MainMenu menu;
        menu.show(window);
}

Game::GameState Game::gameState = Uninitialized;
sf::RenderWindow Game::window;

MainMenu.cpp
#include "MainMenu.h"
#include <string>

class MenuItem
{
        public:
                MenuItem(std::string textIn, bool activeDefault, float verticalOffset, sf::Font font)
                {
                        sf::VideoMode resolution = sf::VideoMode::getDesktopMode();
                        setText(textIn, sf::Vector2f(resolution.width / 2, resolution.height / 2 + verticalOffset), font);
                        setActive(activeDefault);
                }
                bool isActive()
                {
                        return active;
                }
                sf::Text getText()
                {
                        return text;
                }
                void setActive(bool isActive)
                {
                        active = isActive;
                }
                void setText(std::string string, sf::Vector2f position, sf::Font font)
                {
                        text.setString(string);
                        text.setFont(font);
                        text.setCharacterSize(24);
                        text.setPosition(position);
                       
                        if(this->isActive())
                        {
                                text.setFillColor(sf::Color::Green);
                        }
                        else
                        {
                                text.setFillColor(sf::Color::Blue);
                        }
                }
        private:
                bool active;
                sf::Text text;
};

void MainMenu::show(sf::RenderWindow& window)
{
        sf::Font font;
        font.loadFromFile("resources/fonts/arial.ttf");
       
        MenuItem playButton("Play", true, -50.f, font);
        MenuItem optionsButton("Options", false, 0.f, font);
        MenuItem exitButton("Exit", false, 50.f, font);

        window.draw(playButton.getText());
        window.draw(optionsButton.getText());
        window.draw(exitButton.getText());
}

//Just a stub for now
void MainMenu::navigate(sf::Event event);

}

Note that I'm garbage at linux, and I have done nothing on it except for use the terminal to program and make some folders. I did not even install SFML myself, a friend did it for me while I was AFK.

6
General / Re: Build error c2248 when passing a pointer in a function
« on: April 13, 2015, 05:00:29 pm »
void show(sf::RenderWindow window)
void getMenuResponse(sf::RenderWindow window)

You need to learn basic C++ before trying to use SFML. If you read the error it is quite clear what the problem is.

Quote
when passing a pointer in a function

You are not passing a pointer...

Quote
'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable'

You can not copy the window class, so either pass it as a pointer or reference to your function.
First of all, in the original program down i passed it by pointer, forgot to do it here.
Second of all, i could've sworn that i had tried doing as you told... Obviously i didnt, since its working now.  :P
Thanks for taking your time to answer this  :)

7
General / Build error c2248 when passing a pointer in a function
« on: April 13, 2015, 04:19:55 pm »
Hello!

So i get these folling errors when i try and run my program:
c:\users\axel\downloads\sfml-2.2\include\sfml\window\window.hpp(521): error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable'
1>          c:\users\axel\downloads\sfml-2.2\include\sfml\system\noncopyable.hpp(67) : see declaration of 'sf::NonCopyable::NonCopyable'
1>          c:\users\axel\downloads\sfml-2.2\include\sfml\system\noncopyable.hpp(42) : see declaration of 'sf::NonCopyable'
1>          This diagnostic occurred in the compiler generated function 'sf::Window::Window(const sf::Window &)'
1>c:\users\axel\downloads\sfml-2.2\include\sfml\graphics\rendertarget.hpp(419): error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable'
1>          c:\users\axel\downloads\sfml-2.2\include\sfml\system\noncopyable.hpp(67) : see declaration of 'sf::NonCopyable::NonCopyable'
1>          c:\users\axel\downloads\sfml-2.2\include\sfml\system\noncopyable.hpp(42) : see declaration of 'sf::NonCopyable'
1>          This diagnostic occurred in the compiler generated function 'sf::RenderTarget::RenderTarget(const sf::RenderTarget &)'
 

When i cut down the code to the one i show below, i found out its because i passed a pointer in a function:
//Note: this is obviously cut down
int main()
{
        sf::RenderWindow mainWindow;
        mainWindow.create(sf::VideoMode(1024, 768), "A window");

        show(mainWindow);
}
void show(sf::RenderWindow window)
{
        ..
        getMenuResponse(window);
        ..
}
void getMenuResponse(sf::RenderWindow window)
{
        ..
}

My question is what i can do to prevent this.


8
General / Re: My window doesnt have a color
« on: February 19, 2015, 05:49:38 pm »
This is alot of work just to report a problem. I might sound lazy, but right now i have a stomach flu (i think is the english name for it) and i problably shouldnt be on the computer. Give me a few days or so, and i may come back.

9
General / Re: AW: My window doesnt have a color
« on: February 19, 2015, 05:25:20 pm »
If that works, your code must be bugged.
Thats pretty much what i was asking for help for. Like you may have assumed, the example code from the visual studio tutorial did work.
Something i forgot to mention aswell, i use visual studio 2010 express if that helps.

10
General / Re: My window doesnt have a color
« on: February 19, 2015, 05:13:29 pm »
You meant from the code that i posted above? I thought you meant the one i tried a week or so ago that i posted somewhere around this forum.
Gave comments to the lines that changed.
#include "stdafx.h"
#include "Game.h"

Game::Game()
{
        sf::RenderWindow _mainWindow(sf::VideoMode(1024, 768, 32), "Pang!");
        _gameState = uninitialized;
        start();
}
void Game::start(void)
{
        if(_gameState != uninitialized)
        {
                return;
        }

        _gameState = playing;

        while(!isExiting())
        {
                gameLoop();
        }

        _mainWindow.close();
}
bool Game::isExiting()
{
        if(_gameState == exiting)
        {
                return true;
        }
        else
        {
                return false;
        }
}
void Game::gameLoop() //Where the actual change happened
{
        sf::Event currentEvent;
        while(_mainWindow.pollEvent(currentEvent)) //No longer contains the rendering
        {
                if(currentEvent.type == sf::Event::Closed)
                        {
                                 _gameState = exiting;
                        }
        }
        switch(_gameState) //Note that i moved this from the while loop
        {
                case playing:
                _mainWindow.clear(sf::Color::Red);
                _mainWindow.display();
        }
}

11
General / Re: My window doesnt have a color
« on: February 19, 2015, 04:44:22 pm »
Don't assume, check...

Also what did you change if it worked before?
Drives updated, still as white as ever. The only thing i recall changing is using the constructor to create the window instead of
_mainWindow.create(stuff);

12
General / Re: My window doesnt have a color
« on: February 19, 2015, 02:34:26 pm »
Since you have the debugger running, does it reach the clear statement?

Is your graphics driver installed/uptodate?
Yes, it reaches the clear statement AND the display.

Since ive had it show me a red screen before, i would assume that they are uptodate.

13
General / Re: AW: Re: My window doesnt have a color
« on: February 19, 2015, 01:29:40 pm »
Looks like you never changed the value of _gameState to playing.
1. I never saw that post.
2.
void Game::start(void)
{
    if(_gameState != uninitialized)
        return;

    _gameState = Game::playing; //I did actually, and changing it to _gameState = playing didnt work either

    while(!isExiting())
    {
        gameLoop();
    }

    _mainWindow.close();
}
I have the debugger to back me up. When it comes to the switch statement, it tells me that gamestate indeed is playing.

14
General / Re: My window doesnt have a color
« on: February 19, 2015, 01:16:19 pm »
It's not only weird or something one can change "later", but it's simply wrong. Rendering doesn't belong into the event loop, so don't do it.
Ok, thanks for telling me. However, it still doesnt change the color. Its as white as it always was.

15
General / Re: My window doesnt have a color
« on: February 19, 2015, 11:48:22 am »
I'm not sure if this is directly related to your problem, but your gameloop seems a bit weird to me. Right now you are only clearing and displaying the window if an event happens. Perhaps you want something like this instead?

Code: [Select]
while(_mainWindow.pollEvent(currentEvent))
{
   if(currentEvent.type == sf::Event::Closed)
   {
      _gameState = exiting;
   }
}

switch(_gameState)
{
   case playing:
      _mainWindow.clear(sf::Color::Red);
      _mainWindow.display();
}
Its supposed to be like that. Later, when i get further in the tutorial, i will do different stuff.

Pages: [1] 2