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

Pages: [1]
1
General / Smooth Movement
« on: November 10, 2014, 02:03:21 am »
I'm trying to make a sprite move at a constant and smooth rate by using a constant speed multiplied by the time elapsed from last frame.

sf::Clock clk;
clk.restart();
    while (window.isOpen())
    {
               
        sf::Event event;
            float dt;
        while (window.pollEvent(event) || true)
        {
                        dt = clk.restart().asSeconds();

            if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
                        {
                                sprite.move(playerSpeed*dt,0);
                        }

                        window.clear();
                        window.draw(sprite);
                        window.display();
        }
    }
 

This does not result in smooth movement. Occasionally the dt value will spike from 0.0003 to .0010 or something like that, causing the sprite to jump forward even though the difference in time is negligible.

Is there some way to account for these small variations or am I approaching smooth movement wrong all together?

Thanks.

2
Graphics / Re: Unable to draw a sf::Text Object
« on: May 15, 2014, 08:08:11 am »
Problem solved!

Guess I should read the documentation more carefully.

3
Graphics / *Solved* Unable to draw a sf::Text Object
« on: May 15, 2014, 06:20:57 am »
I'm trying to make a Menu object. So far I've only added the buttons, which contain a RectangleShape body and Text. When trying to draw the text portion of a button to the window I get errors.


//MenuTest.h
#include <SFML/Graphics.hpp>
#include <string>
#include <vector>

class Menu1
{
        class Button1
        {
                sf::RectangleShape body;
                sf::Text text;
                void create(float x, float y);//sets button postion
                void setText();//ensures text is centered inside the button
        public:
                void setPosition(float x, float y);
                void drawButton(sf::RenderWindow &window);

                Button1(float w, float h, float x, float y, sf::Font f, std::string s="Button")
                    : body(sf::Vector2f(w,h)), text(s,f) {create(x,y);}
               
        };

        std::vector<Button1> MenuButtons;
public:
        void draw(sf::RenderWindow &window);
        void addButton(float w, float h, float x, float y, sf::Font f, std::string s="Button");
};
 
#include "MenuTest.h"

//Button1---
void Menu1::Button1::create(float x, float y)
{
        body.setOutlineThickness(-2);
        body.setFillColor(sf::Color::Black);
        body.setOutlineColor(sf::Color::White);
        setPosition(x,y);
}

void Menu1::Button1::setText()
{
//lot of code that makes sure text is aligned inside the button
//confirmed that this has no effect on the issue
}
void Menu1::Button1::setPosition(float x, float y)
{
        body.setPosition(x,y);
        setText();
}
void Menu1::Button1::drawButton(sf::RenderWindow &window)
{
        window.draw(body);
        window.draw(text);//<--- Error comes from here
}

//Menu1----
void Menu1::addButton(float w, float h, float x, float y, sf::Font f, std::string s)
{
        MenuButtons.push_back(Button1(w,h,x,y,f,s));
}
void Menu1::draw(sf::RenderWindow &window)
{
        for(int i=0;i<MenuButtons.size();i++)
                MenuButtons[i].drawButton(window);
}
 
#include "MenuTest.h"
#include <iostream>
void createMenu(sf::RenderWindow &window)
{
        Menu1 main;
        sf::Font font;
        if(!font.loadFromFile("Terminal.ttf"))
                std::cout<<"Font error\n";
        main.addButton(100,50,20,20,font);
        main.draw(window);
}


int main()
{
        sf::RenderWindow window(sf::VideoMode(1000,800),"Window");
        window.clear();
        createMenu(window);
       
        window.display();
        char a;
        std::cin>>a;
        window.close();
    return 0;
}
 

This causes an error at the line
Code: [Select]
window.draw(text); in Menu::Button::drawButton(). With the error "Access violation reading location (some memory address)"

I have confirmed that everything works up until the draw text line. I have also confirmed that the neither the setText function nor the vector are causing the problem by having Menu1 store just one button by itself and setting the text to some arbitrary point. The error still occurs. I've been looking a this for awhile and I'm at a loss as to what is happening.

Thanks for any help/suggestions.

Pages: [1]