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.


Topics - Ausche

Pages: [1]
1
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.

2
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!

3
General / Event Handling Deadzones? [SOLVED]
« on: December 24, 2015, 08:33:53 pm »
Hey all,
So I'm working on the basics of a 2D zelda-style game (as a hobby) and I've got a question about the event system.

The problem is that when I hold any movement key (WASD), my sprite animation will pause for a short period of time and then act as it should, updating the spriteRect every .1 second.

I took the problem to the console, making it print every time an event was detected, every time the animation function was called, and so on, to figure out what was causing it to pause.
I found that this happens for all events detected including ones I don't handle (like the mouse) and it's the event loop that's skipped whenever an event is held.

When I just press a movement key, it acts as it should and moves the spriteRect for a split second, then resets. The problem is only when an event is held down.

Here's the segments of code that matter-
level1.cpp:
gameWindow.setFramerateLimit(30);

    while (gameWindow.isOpen())
    {
               
                sf::Event event;

        while (gameWindow.pollEvent(event))
        {
                       
            if (event.type == sf::Event::Closed)
                gameWindow.close();

                        player->handle_input(event, clock);
                        std::cout << "event detected" << std::endl;

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

                enemy->move();
                player->move();
 

player.cpp handle_input function:
void Player::handle_input(sf::Event event, sf::Clock& clock)
{
        switch(event.type)
        {
        case sf::Event::KeyPressed:
                {
                        switch(event.key.code)
                        {
                        case sf::Keyboard::W:
                                {
                                        velocity.y = -1;
                                        direction(2);
                                        if(clock.getElapsedTime().asSeconds() > 0.1f)
                                        {
                                                walking();
                                                clock.restart();
                                        }
                                        break;
                                }
                        case sf::Keyboard::A:
                                {
                                        velocity.x = -1;
                                        direction(3);
                                        if(clock.getElapsedTime().asSeconds() > 0.1f)
                                        {
                                                walking();
                                                clock.restart();
                                        }
                                        break;
                                }
                        case sf::Keyboard::S:
                                {
                                        velocity.y = 1;
                                        direction(1);
                                        if(clock.getElapsedTime().asSeconds() > 0.1f)
                                        {
                                        walking();
                                        clock.restart();
                                        }
                                        break;
                                }
                        case sf::Keyboard::D:
                                {
                                        velocity.x = 1;
                                        direction(4);
                                        if(clock.getElapsedTime().asSeconds() > 0.1f)
                                        {
                                                walking();
                                                clock.restart();
                                        }
                                        break;
                                }
                        }
                        break;
                }
        case sf::Event::KeyReleased:
                {
                        switch(event.key.code)
                        {
                        case sf::Keyboard::W: velocity.y = 0; break;
                        case sf::Keyboard::S: velocity.y = 0; break;
                        case sf::Keyboard::A: velocity.x = 0; break;
                        case sf::Keyboard::D: velocity.x = 0; break;
                        }
                        break;
                }
               
        }
       
        if(velocity.x == 0 && velocity.y == 0)
        {
                spriteRect.left = 0;
                msprite.setTextureRect(spriteRect);
        }
}
 

player.cpp move():
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);
        box = msprite.getGlobalBounds();
}
 

animate.cpp walking()
void Animate::walking()
{
        if (spriteRect.left == (mdimensions.x * 3))
                spriteRect.left = 0;
       
        else
                spriteRect.left +=mdimensions.x;

        msprite.setTextureRect(spriteRect);
}
 

animate.cpp direction function:
void Animate::direction(int direction)
{
        switch(direction)
        {
                case 1: spriteRect.top = 0;     break;
                case 2: spriteRect.top = mdimensions.y;break;
                case 3: spriteRect.top = (mdimensions.y*2);break;
                case 4: spriteRect.top = (mdimensions.y*3);break;
        }

        msprite.setTextureRect(spriteRect);
}
 

The sprite DOES move smoothly, with no pause, it's just the animation that pauses for a little bit.
All I'm looking for here is to get rid of that weird intial pause when events are held down and have smooth animation.
I'm about to head out to go to work, but when I get back I'll be able to respond.

4
General / [SOLVED]Getting a LNK2019 error
« on: November 04, 2015, 02:57:22 am »
I've been working on this player class all day and now I get this error when I build everything after I passed all of the debugging. It's my first time really messing with headers/classes and SFML. Also I'm new to video game programming so I still lack a lot of the intuition.

These are my exact two errors-

19   error LNK2019: unresolved external symbol "public: bool __thiscall Player::load_player(void)" (?load_player@Player@@QAE_NXZ) referenced in function _main   C:\Users\OverLordSupreme\Documents\Visual Studio 2012\Projects\Playgrounds\Playgrounds\main.obj   DIM

Error   20   error LNK1120: 1 unresolved externals   C:\Users\OverLordSupreme\Documents\Visual Studio 2012\Projects\Playgrounds\Debug\DIM.exe   DIM

Forewarning- I commented out some code for various reasons, so everything not commented is how I intend for this program to work.

The main file:
#include <SFML/Graphics.hpp>
#include <math.h>
#include <iostream>
#include "tile_class.cpp"
#include "player.h"

using namespace sf;

int main()
{
    const int WINDOW_WIDTH = 512;
        const int WINDOW_HEIGHT = 256;
       

        RenderWindow window(VideoMode(512, 256), "DIM");
        window.setKeyRepeatEnabled(false);

        const int level[] =
        {
                0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        };

        TileMap map;
        if (!map.load("grass.png", Vector2u(32,32), level,16, 8))
        {return -1;}
       
        Player Hero;
        Hero.setPlayer(100,100,0,0,0,0,1,Vector2i(256,128));
        Hero.load_player();

    RectangleShape upper_border(Vector2f(WINDOW_WIDTH, 5));
        upper_border.setPosition(0,-5);

        RectangleShape lower_border(Vector2f(WINDOW_WIDTH, 5));
        lower_border.setPosition(0,WINDOW_HEIGHT);

        RectangleShape left_border(Vector2f(5, WINDOW_HEIGHT));
        left_border.setPosition(-5,0);

        RectangleShape right_border(Vector2f(5, WINDOW_HEIGHT));
        right_border.setPosition(WINDOW_WIDTH,0);

        while (window.isOpen())
    {
                FloatRect boundingBox = Hero.getGlobalBounds();
                FloatRect left_box = left_border.getGlobalBounds();
                FloatRect right_box = right_border.getGlobalBounds();
                FloatRect upper_box = upper_border.getGlobalBounds();
                FloatRect lower_box = lower_border.getGlobalBounds();

        Event event;
        while (window.pollEvent(event))
        {
            if (event.type == Event::Closed)
                window.close();
                       
                }

                double move_variable=.035*Hero.getSpeed();

                if (Keyboard::isKeyPressed(Keyboard::Left) && !boundingBox.intersects(left_box))
                {Hero.move(-move_variable,0);}

                if (Keyboard::isKeyPressed(Keyboard::Right) && !boundingBox.intersects(right_box))
                {Hero.move(move_variable,0);}
               
                if (Keyboard::isKeyPressed(Keyboard::Up) && !boundingBox.intersects(upper_box))
                {Hero.move(0,-move_variable);}

                if(Keyboard::isKeyPressed(Keyboard::Down) && !boundingBox.intersects(lower_box))
                {Hero.move(0,move_variable);}

                window.clear(Color::White);    
                window.draw(map);
                window.draw(left_border);
                window.draw(right_border);
                window.draw(upper_border);
                window.draw(lower_border);
                window.draw(Hero);
        window.display();
    }

    return 0;
}
 

The Player hpp file
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Drawable.hpp>
#include <SFML/Graphics.hpp>

using namespace sf;

class Player: public Sprite, public Texture
{
public:
        Player();
        void setPlayer(int current_health, int health_pool, int current_armor, int current_weapon, int weapon_upgrade, int inventory, int speed, Vector2i position);
        void setCurrentHealth(int);
        void setHealPool(int);
        void setCurrentArmor(int);
        void setCurrentWeapon(int);
        void setWeaponUpgrade(int);
        void setInventory(int);
        void setSpeed(int);
        void setPosition(Vector2i);

        int getCurrentHealth();
        int getHealPool();
        int getCurrentArmor();
        int getCurrentWeapon();
        int getWeaponUpgrade();
        int getInventory();
        int getSpeed();
        Vector2i getPosition();

        void playermove(Vector2i);
        bool load_player();

private:
        int player_states[7];
        Vector2i player_position;
        Sprite playerSprite;

        int current_health;
        int health_pool;
        int current_armor;
        int current_weapon;
        int weapon_upgrade;
        int inventory;
        int speed;
        Vector2i position;

};
 

And finally the player cpp file
#include <SFML/Graphics.hpp>
#include "player.h"
#include <iostream>

using namespace sf;
using namespace std;

Player::Player()
{
}
void Player::setPlayer(int current_health, int health_pool, int current_armor, int current_weapon, int weapon_upgrade, int inventory, int speed, Vector2i position)
{
        setCurrentHealth(current_health);
        setHealPool(health_pool);
        setCurrentArmor(current_armor);
        setCurrentArmor(current_armor);
        setCurrentWeapon(current_weapon);
        setWeaponUpgrade(weapon_upgrade);
        setInventory(inventory);
        setSpeed(speed);
        setPosition(position);
}

void Player::setCurrentHealth(int current_health)
        {player_states[0]=current_health;}

void Player::setHealPool(int health_pool)
        {player_states[1]=health_pool;}

void Player::setCurrentArmor(int current_armor)
        {player_states[2]=current_armor;}

void Player::setCurrentWeapon(int current_weapon)
        {player_states[3]=current_weapon;}

void Player::setWeaponUpgrade(int weapon_upgrade)
        {player_states[4]=weapon_upgrade;}

void Player::setInventory(int inventory)
        {player_states[5]=inventory;}

void Player::setSpeed(int speed)
        {player_states[6]=speed;}

void Player::setPosition(Vector2i position)
        {player_position=position;}

int Player::getCurrentHealth()
        {int current_health=player_states[0];
        return current_health;}

int Player::getHealPool()
        {int health_pool=player_states[1];
        return health_pool;}

int Player::getCurrentArmor()
        {int current_armor=player_states[2];
        return current_armor;}

int Player::getCurrentWeapon()
        {int current_weapon=player_states[3];
        return current_weapon;}

int Player::getWeaponUpgrade()
        {int weapon_upgrade=player_states[4];
        return weapon_upgrade;}

int Player::getInventory()
        {int inventory=player_states[5];
        return inventory;}

int Player::getSpeed()
        {int speed=player_states[6];
        return speed;}

Vector2i Player::getPosition()
        {Vector2i position=player_position;
        return position;}

bool load_player()
{
        Texture player_texture;
        if (!player_texture.loadFromFile("face_right"))
        {
                cerr<< "Texture error" << endl;
                return false;
        }
        Sprite player;
        player.setTexture(player_texture);
        return true;
}
 

Any help with this would be awesome! I did try to rename the whole project from "Playgrounds" to "DIM", I feel like that might be helpful to know.

Also while I'm posting this, if anyone could critique my coding that'd be great. Like I said I'm new to this stuff including C++ so I don't know if there's any more ideal ways to go about what I'm doing.

Pages: [1]
anything