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

Pages: [1] 2
1
Graphics / Re: Getting and drawing an inputted string.
« on: January 31, 2017, 02:22:14 pm »
I don't know if I'm using the very latest version of C++. Currently using Code::Blocks 16.01 release which supports C++11. And okay I see what you're saying, thank you!

2
Graphics / Re: Getting and drawing an inputted string.
« on: January 30, 2017, 10:43:45 pm »
That may have been the issue, and I am checking for the type, here's the full event loop:
while (currentWindow->pollEvent(GUIevent)){

     if(GUIevent.type == sf::Event::KeyPressed){
          if(GUIevent.key.code == sf::Keyboard::Escape){
               visible = false;
          }
          if(GUIevent.key.code == sf::Keyboard::BackSpace){
               if(inputString.size() != 0){
                    inputString.pop_back();
                    currentText.setString(inputString);
               }
          }

           //handle RETURN key

     }
     else if (GUIevent.type == sf::Event::TextEntered){
          if (GUIevent.text.unicode < 128){
               inputString += static_cast<char>(GUIevent.text.unicode);
               currentText.setString(inputString);
          }
     }
}
 

but I'm getting a compile error as pop_back() is not a member of std::string. I can think of a few other ways to remove the last character so I might try those out and see what happens.

3
Graphics / Re: Getting and drawing an inputted string.
« on: January 30, 2017, 10:13:37 pm »
Ah okay, that makes sense.
I updated it so it only adjusts the currentText when necessary:

if (GUIevent.type == sf::Event::TextEntered){
     if (GUIevent.text.unicode < 128){
          inputString += static_cast<char>(GUIevent.text.unicode);
          currentText.setString(inputString);
     }
}
if(GUIevent.key.code == sf::Keyboard::BackSpace){
     if(!inputString.empty()){
          inputString.erase(inputString.size() - 1);
          currentText.setString(inputString);
     }
}
 

and I fixed the draw function:
void GUI_loadFile::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
        //...

        if(!inputString.empty()){
                target.draw(currentText);
        }
}
 

There's something weird about the delete part, might have something to do with how I erase the character, not sure. But it draws properly! Thank you.

4
Graphics / Getting and drawing an inputted string.
« on: January 30, 2017, 09:21:18 pm »
I've spent a few hours on this looking up tutorials and other posts about this, but can't find anything to help me.

What I want to do is get input from the user, such as a filename, but echo what they've typed so far on the window.

Here's a simple version of what I got so far:

In game loop:

//outside of event loop..
std::string inputString;

//...inside loop
if (GUIevent.type == sf::Event::TextEntered){
            if (GUIevent.text.unicode < 128){
                inputString += static_cast<char>(GUIevent.text.unicode);
            }
        }else if(GUIevent.type == sf::Event::KeyPressed){
            if(GUIevent.key.code == sf::Keyboard::BackSpace){
                if(!inputString.empty()){
                    inputString.erase(inputString.size() - 1);
                }
            }
//...end loop
 

In draw function:

//Declaration:
sf::Text currentText;

//Function
void GUI_loadFile::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
        if(!inputString.empty()){
        const sf::String newString(std::string(inputString));

        currentText.setString(newString); //causes error here!

        target.draw(currentText);
        }
}
 

So essentially the draw function is called every iteration of the loop and the events above edit the string that gets cast as a sf::String and drawn as a sf::Text object. 

The error reads :
error: passing 'const sf::Text' as 'this' argument of 'void sf::Text::setString(const sf::String&)' discards qualifiers [-fpermissive]|
error: conversion from 'const sf::String(std::string) {aka const sf::String(std::basic_string<char>)}' to 'const sf::String' is ambiguous

Is there anyway to get the edited `std::string` into a `const std::string` or `const sf::String` so I can pass it to the text object? I've messed around with sstream and ostreams but couldn't get that to work either.

5
General / Re: Draw function, Iterators, Bounds error
« on: February 21, 2016, 06:01:45 am »
Okay, it seems I've solved my problem and I'm not sure how. Everything works as it should, finally!

Thanks again for all the help. +1

6
General / Re: SFML book, chapter 2 issue
« on: February 21, 2016, 02:59:08 am »
however this is a bit messy and not very modular so I will do as Nexus suggested and put it in it's own file. As for using the c++11 strongly typed enum, from what I can gather, I would literally just use, for example, enum class textureId, and not use the namespace, because the strongly typed nature of enum class means that the enums are safely locked behind texturedId::textureName, which was the whole point of using a namespace. Is this correct?

I believe that's what he was saying.
You can check out more on these Strongly Typed Enums here: http://www.cprogramming.com/c++11/c++11-nullptr-strongly-typed-enum-class.html

7
General / Re: Draw function, Iterators, Bounds error
« on: February 21, 2016, 01:33:22 am »
Actually, the button class stores buttonBackground and buttonText as protected! Reread the header I posted.
I see you friended the GUI class. Using the keyword friend does most of the time indicate a code smell, i.e. bad design, since friended classes break the encapsulation and there are usually better ways to go about things like that.

Your problem might well be, because of:
Button(..., sf::Font font);

You are making a copy of the font object, which gets destroyed as soon as the Button's constructor is left, leaving a reference in the text object to a non-existing font object.

Okay, I fixed the encapsulation thing, now they're separate but communicating, however it still won't draw the buttons to the screen.

The GUI background draws fine and works as intended, however the buttons can't be seen for some reason.
Here's the fixed draw function:

In GUIsystem.cpp
void GUI::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
        if(visible == false)
                return;

        states.transform *= getTransform();
        target.draw(background);
        for(int i = 0; i < buttonReference; i++)
        {
                std::cout<< "drawing button " << i << std::endl;
                target.draw(buttons[i].buttonBackground);
                target.draw(buttons[i].buttonText);
        }

}
 

The "drawing button" iterates fine from 0-2 as it should, but the buttons can't be seen.
Here's the fixed header file and the segment where the draw function is called (might be relevant)

GUIsystem.h
class Button;
class GUI : public sf::Transformable, public sf::Drawable
{
public:
        GUI();
        //dimensions, position, background color, border color
        GUI(sf::Vector2f dimensions, sf::Vector2f position, sf::Color BackgroundColor, sf::Color borderColor);
        /*returns a number as which refers to the button. Used to handle choice logic
        dimensions, position, text, button color, border color textcolor, font name ".txt"
        */

        int addButton(sf::Vector2f dimensions, sf::Vector2f position, const std::string& text, sf::Color buttonColor, sf::Color borderColor, sf::Color textColor, sf::Font* font);
        int update(sf::Vector2f &);
        void show();
        void hide();
        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;

        bool visible;
        std::vector<Button> buttons;
private:
        sf::Vector2f dimensions;
        sf::Color backgroundColor;
        sf::Color borderColor;

        sf::RectangleShape background;
        int buttonReference;
};

class Button
{
public:
        Button();
        Button(sf::Vector2f dimensions, sf::Vector2f position, const std::string& text, sf::Color buttonColor, sf::Color borderColor, sf::Color textColor, sf::Font* font);
        bool checkHovering(sf::Vector2i);
        void hovering();
        void notHovering();
        sf::RectangleShape getButtonBackground();
        sf::Text getButtonText();

        sf::RectangleShape buttonBackground;
        sf::Text buttonText;

private:
        sf::Color textColor;
        sf::Font* textFont;
};
 

In level1.cpp
        gameWindow.setView(view);
        gameWindow.clear();
        gameWindow.draw(smap);
        gameWindow.draw(*player);
        gameWindow.draw(*enemy);
        gameWindow.draw(*tree);
        gameWindow.draw(pauseMenu);

        gameWindow.display();
 

I imagine it's a problem with the draw function, maybe the shape/text objects I'm trying to draw don't exist or something?

**EDIT**
I whipped up a simple version of the GUI system in another application and it works completely fine...

Here's that source

main.cpp
        sf::RenderWindow renderWindow(sf::VideoMode(640,480), "Color");
        sf::Event event;

        GUI menu(sf::Vector2f(50,150), sf::Vector2f(250,50));
        menu.addButton(sf::Vector2f(40,15), sf::Vector2f(255, 60));
        menu.addButton(sf::Vector2f(40,15), sf::Vector2f(255, 80));
        menu.addButton(sf::Vector2f(40,15), sf::Vector2f(255, 100));

        renderWindow.setFramerateLimit(30);

        while (renderWindow.isOpen())
        {
                while(renderWindow.pollEvent(event))
                {
                        if(event.type == sf::Event::EventType::Closed)
                                renderWindow.close();
                        if(event.type == sf::Event::KeyPressed)
                        {
                                if(event.key.code == sf::Keyboard::Escape)
                                {
                                        if(menu.visible)
                                                menu.hide();
                                        else
                                                menu.show();
                                }
                        }
                }


                renderWindow.clear();
               
                renderWindow.draw(menu);

                renderWindow.display();
 

GUI.h

#include "SFML\Graphics.hpp"

class Button;
class GUI : public sf::Transformable, public sf::Drawable
{
public:
        GUI();
        //dimensions, position, background color, border color
        GUI(sf::Vector2f dimensions, sf::Vector2f position);
       
        //dimensions, position, text, button color, border color textcolor, font name ".txt"
        void addButton(sf::Vector2f dimensions, sf::Vector2f position);
        void show();
        void hide();

        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;

        bool visible;
        std::vector<Button> buttons;
private:
        sf::RectangleShape background;
};

class Button
{
public:
        Button();
        Button(sf::Vector2f dimensions, sf::Vector2f position);
        sf::RectangleShape buttonBackground;
};
 

GUI.cpp
#include "GUI.h"

GUI::GUI()
{}
GUI::GUI(sf::Vector2f dimensions, sf::Vector2f position)
{
        background.setPosition(position);
        background.setSize(dimensions);
        background.setFillColor(sf::Color::Green);
        visible = false;
}
void GUI::addButton(sf::Vector2f dimensions, sf::Vector2f position)
{
        buttons.push_back(Button(dimensions,position));
}
void GUI::show()
{
        visible = true;
}
void GUI::hide()
{
        visible = false;
}
void GUI::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
        if(!visible)
                return;

        states.transform *= getTransform();
        target.draw(background);
        for(int i = 0; i < 3; i++)
        {
                target.draw(buttons[i].buttonBackground);
        }
}
Button::Button(sf::Vector2f dimensions, sf::Vector2f position)
{
        buttonBackground.setSize(dimensions);
        buttonBackground.setPosition(position);
        buttonBackground.setFillColor(sf::Color::Blue);
}
 

Really not sure what the problem is here. Thanks for all the help so far by the way, everything's been cake since. Not sure why this system is messing with me so much.

8
General / Re: Draw function, Iterators, Bounds error
« on: February 19, 2016, 04:29:15 am »
I'll just post the github link to everything. https://github.com/AtomicAustin/TheBungus.git
Keep in mind I haven't been coding with readability in mind nor have I documented pretty much anything...

Though any criticism is welcome! This is my first real project and I welcome anything constructive.

This problem only involves level1.cpp and GUIsystem.cpp btw.

9
General / Re: Draw function, Iterators, Bounds error
« on: February 19, 2016, 04:22:57 am »
Button* iterator is a pointer to an instance of Button, while Button has the private members buttonBackground and buttonText. How do you expect to access private members? The code you provided should not compile, so my guess is that this is not the real/full code. ;)

If you can't figure out what goes out of range with the debugger, you could always make the for loop with normal iterators or an index and then see what is going on.

Also there's no need for the this->, you can access member variables directly.

Actually, the button class stores buttonBackground and buttonText as protected! Reread the header I posted.

On another note, I dug way deeper into the Autos report and found this-

+      buttons   { size=3 }   std::vector<Button,std::allocator<Button> >
+      iterate   {buttonBackground={m_size={x=160.000000 y=40.0000000 } } buttonText={m_string={m_string={...} } m_font=...} ...}   Button
+      iterate.buttonBackground   {m_size={x=160.000000 y=40.0000000 } }   sf::RectangleShape
-      iterate.buttonText   {m_string={m_string={...} } m_font=0x000000acdcf5ea48 {m_library=0x0000000000000000 m_face=0xcccccccccccccc00 ...} ...}   sf::Text
   +      sf::Drawable   {...}   sf::Drawable
   +      sf::Transformable   {m_origin={x=0.000000000 y=0.000000000 } m_position={x=473.000000  y=133.000000 } m_rotation=0.000000000 ...}   sf::Transformable
   +      m_string   {m_string={...} }   sf::String
    -      m_font   0x000000acdcf5ea48 {m_library=0x0000000000000000 m_face=0xcccccccccccccc00 m_streamRec=0xccccccccff000000 ...}   const sf::Font *
      m_library   0x0000000000000000   void *
      m_face   0xcccccccccccccc00   void *
      m_streamRec   0xccccccccff000000   void *
    +      m_refCount   0xcccccccccccccccc {???}   int *
    +      m_info   {family=<Error reading characters of string.> }   sf::Font::Info

I know it's a little crazy to read all that, but pay attention to the last line. I think that's the problem, did some googling and typically this arises when there was a temporary string sent to this object or something.

10
General / [SOLVED] Draw function, Iterators, Bounds error
« on: February 19, 2016, 03:54:23 am »
So I'm implementing a GUI system to my game.

The problem is, when I go to draw the rectangle shape and text, both contained in one structure, I'm getting a bounds error.

Relevant code:

Draw function found in GUIsystem.cpp
void GUI::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
//if the GUI isn't visible, return
        if(visible == false)
                return;

        states.transform *= getTransform();

//THE PROBLEM***
        for(Button* iterate : this->buttons)
        {
                std::cout << "drawing button" << std::endl;
                target.draw(iterate->buttonBackground);
                target.draw(iterate->buttonText);
//debugger breaks here, doesn't pass a single iteration but does display the "drawing button" to the console
//claims the problem is buttonText, getting a bounds error
        }

//ignore this chunk, same thing as above but not using an iterator
//same exact problem though (bounds error problem with buttonText)
        /*for(int i = 0; i < buttonReference-1; i++)
        {
                target.draw(buttons[i]->buttonBackground);
                target.draw(buttons[i]->buttonText);
        }
        */


//can't assume this is right, program breaks before this
        std::cout << "drawing background" << std::endl;
        target.draw(background);

}
 

Header for GUI class:
class GUI : public sf::Transformable, public sf::Drawable
{
public:
        GUI();
//dimensions, position, background color, border color
        GUI(sf::Vector2f dimensions, sf::Vector2f position, sf::Color BackgroundColor, sf::Color borderColor);

//returns a number as which refers to the button. Used in game loop to handle the players choice
//dimensions, position, text, button color, border color textcolor, font
        int addButton(sf::Vector2f dimensions, sf::Vector2f position, const std::string& text, sf::Color buttonColor, sf::Color borderColor, sf::Color textColor, sf::Font font);

//updates the entire GUI, iterates over all the buttons to check if mouse is hovering or not
        int update();

//simply switches visible bool
        void show();
        void hide();

//should draw the background, the button background and the button's text
        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;

        bool visible;
private:

        sf::Vector2f dimensions;
        sf::Color backgroundColor;
        sf::Color borderColor;

        sf::RectangleShape background;

//IMPORTANT - vector of pointers to buttons **this is iterated through in the draw function
        std::vector<Button*> buttons;

//a reference used to know which button was clicked on
        int buttonReference;
};

The header for the Button class:
class Button
{
public:
        Button();

//constructs the button, this is called within the GUI constructor
        Button(sf::Vector2f dimensions, sf::Vector2f position, const std::string text, sf::Color buttonColor, sf::Color borderColor, sf::Color textColor, sf::Font font);

//handles when mouse is hovering over a button
        void hovering();

//handles when mouse is no longer hovering over a button
        void notHovering();
        friend GUI;
protected:

//the actual shape drawn
        sf::RectangleShape buttonBackground;

//the text drawn
        sf::Text buttonText;

        sf::Color textColor;
        sf::Font* textFont;
};
 

OUTPUT:
First-chance exception at 0x00007FF6503C035E in The game.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
Unhandled exception at 0x00007FF6503C035E in The game.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

AUTOS:
+      this   0x0000008493f0ed00 {visible=true dimensions={x=0.000000000 y=0.000000000 } backgroundColor={r=0 '\0' ...} ...}   GUI *

I'm thinking it might have something to do with the Button class not inheriting "Drawable", yet it's member variables are trying to be drawn but through a class which does inherit "Drawable" (GUI), though I'm not sure.

I can link the entire source for this through github if it's necessary, I know the abstraction makes it harder to find the problem. If there's any specific thing I should also include let me know. Any help would be great.

Thanks!

11
General / Re: sf::Text::getLocalBounds causes segmentation fault?
« on: December 31, 2015, 08:01:15 pm »
What is the exact error message? This looks right.

12
General / Re: Event Handling Deadzones?
« on: December 26, 2015, 08:44:54 pm »
Perfect, that worked. I'm getting careless haha.

Thanks all!

13
General / Re: Event Handling Deadzones?
« on: December 26, 2015, 08:09:43 pm »
Okay it's my keyRelease function inside the event loop. I made the console print my velocity at each call of the move() function and for some reason it's being set to zero.

The place that does that is inside my event loop:
if(sf::Event::KeyReleased)
     player->keyReleased(event);
 

which looks like
void Player::keyReleased(sf::Event event)
{
        switch(event.key.code)
                        {
                        case sf::Keyboard::W: velocity.y = 0; releaseDirection(velocity); break;
                        case sf::Keyboard::S: velocity.y = 0; releaseDirection(velocity); break;
                        case sf::Keyboard::A: velocity.x = 0; releaseDirection(velocity); break;
                        case sf::Keyboard::D: velocity.x = 0; releaseDirection(velocity); break;
                        }
}
 

Once again, I don't understand events very well or how the event loop works so I'm going to read around a bit.

14
General / Re: Event Handling Deadzones?
« on: December 26, 2015, 07:57:37 pm »
It works exactly like when you hold down a key in a text box here.
The event triggers once, then there is a slight delay (defined in your OS) until it triggers faster.

And yeah, if you want something continuous the answer is in the first reply, use sf::Keyboard. ;)

Yes! This helped, I didn't understand the part about my OS so I tried the real-time input method but put it inside my event loop, which was dumb.

If I had to guess, I'd say that walking() is your "start the animation" call?
If this is so, the testing of the clock to not allow it to start on the first event means that it will not start until the second event, which will be be after the initial keyboard repeat delay. Either start it immediately when you set the movement or consider using real-time input.

I tried real-time input but did it wrong, now it works as I want.

-------------------------------------------------------------------------------------------------------------------

However I've encountered another problem. Now the movement isn't working correctly but the animation is very smooth.

The animation rolls continuously and smoothly but the sprite doesn't move continuously. It moves for roughly a second then stops and will move a pixel or so randomly.

My code right now, I changed some stuff up:
 while (gameWindow.isOpen())
    {

                if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                        player->moveUp(clock);
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
                        player->moveDown(clock);
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
                        player->moveLeft(clock);
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
                        player->moveRight(clock);
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
                        gameWindow.close();

                sf::Event event;

        while (gameWindow.pollEvent(event))
        {
                        if(event.type == sf::Event::Closed)
                                gameWindow.close();
                        if(sf::Event::KeyReleased)
                                player->keyReleased(event);
                }

                enemy->path(*player, *clock);

                enemy->move(*player);
               
                if(!player->getBox().intersects(enemy->getBox()))
                        player->move();
 


The player moveUP/DOWN/LEFT/RIGHT. All that's really important here is to see that I set the velocity, the rest is the animation which works fine.
void Player::moveUp(sf::Clock* clock)
{
        velocity.y=-1;
        yDirection(-1);
        if(clock->getElapsedTime().asSeconds() > 0.1f)
        {
                walking();
                clock->restart();
        }
}
void Player::moveDown(sf::Clock* clock)
{
        velocity.y=1;
        yDirection(1);
        if(clock->getElapsedTime().asSeconds() > 0.1f)
        {
                walking();
                clock->restart();
        }
}
void Player::moveLeft(sf::Clock* clock)
{
        velocity.x=-1;
        xDirection(-1);
        if(clock->getElapsedTime().asSeconds() > 0.1f)
        {
                walking();
                clock->restart();
        }
}
void Player::moveRight(sf::Clock* clock)
{
        velocity.x=1;
        xDirection(1);
        if(clock->getElapsedTime().asSeconds() > 0.1f)
        {
                walking();
                clock->restart();
        }
}
 

The move function, very important. It's called every frame and adds velocity to positon. Might be part of the problem:

void Player::move()
{
        position.x +=velocity.x;
        if(position.x < 0 || position.x > (wSize.x-19))
                position.x -= velocity.x;

        position.y += velocity.y;
        if(position.y < 0 || position.y > (wSize.y-25))
                position.y -= velocity.y;

        msprite.setPosition(position);
}
 

I'm not sure what the problem is, I've been looking at it all day, might have made a careless mistake. I want to say that the event loop is still being triggered by the key presses, but I really don't understand how the loop works to begin with.

15
General / Re: Event Handling Deadzones?
« on: December 25, 2015, 10:17:30 pm »
What you can try is setting up a boolean flag for each move direction and change them during event handling, instead of directly calculating velocity. Evaluate the flags in your update method then.

This isn't my problem, it's not the movement. It's the animation. The sprite will move, the animation will not trigger until about a second give or take a few milliseconds.
If you read how I implement the animation, at every movement key keydown event i adjust the velocity and call the animation function to show the next sprite in the spritesheet texture. This keeps happening until the key is released. So it should be a smooth continuous thing, however there is a weird delay as described and then it animates completely fine.

A few seconds is a long time and pretty extreme.

I was wrong to say it's a few seconds, more like one second give or take a few milliseconds.
It iterates fine after that short pause though, but every movement key down event has that initial delay. I'll try to post a barebones example of what's going on.

Pages: [1] 2
anything