Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Lethn's Programming Questions Thread  (Read 53411 times)

0 Members and 2 Guests are viewing this topic.

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #105 on: August 16, 2013, 02:41:50 pm »
The problem I had was those explanations are very good for explaining the how and why of everything but I just needed to know what all the little bits of code did :D
« Last Edit: August 16, 2013, 02:44:25 pm by Lethn »

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #106 on: August 26, 2013, 06:21:37 am »
I've run into a brick wall at the moment so I thought I'd post things up here again, my code is almost working the way I want it to but this damn time step is still an issue. I've got scope errors popping up which should be all means be easy to fix but these classes ( or at least I think they're classes ) don't seem to work like what I'm used to. I have the source code for the SFML game development book with me and I scoured it for the usual int commands to match up but there aren't any so I'm probably looking at this all completely wrong.


#include <iostream>
#include <SFML/Graphics.hpp>
#include <sstream>

int main()
{

   {

    sf::RenderWindow window(sf::VideoMode(800, 600), "");


    const sf::Time TimePerFrame = sf::seconds ( 1.f / 60.f );
    int healthstats = 1;


    sf::Font arial;
    if ( !arial.loadFromFile ( "arial.ttf" ) )
    { }

    sf::RectangleShape health ( sf::Vector2f ( healthstats, 5 ) );
    health.setFillColor ( sf::Color::Cyan );
    health.setPosition ( 20, 10 );

    sf::Clock clock;
    sf::Time TimeSinceLastUpdate = sf::Time::Zero;



while ( window.isOpen() )
{
    sf::Time ElapsedTime = clock.restart ();
    TimeSinceLastUpdate += ElapsedTime;
    while ( TimeSinceLastUpdate > TimePerFrame )
    {
        TimeSinceLastUpdate -= TimePerFrame;

        processEvents();
        update ( TimePerFrame );
    }

            updateStatistics ( ElapsedTime );
            render();
    }



    if ( sf::Keyboard::isKeyPressed ( sf::Keyboard::A ) )

    {
        health.setScale ( healthstats, 5 );
        healthstats = healthstats - 1;
    }

    else if ( sf::Keyboard::isKeyPressed ( sf::Keyboard::D ) )
    {
        health.setScale ( healthstats, 5 );
        healthstats = healthstats + 1;
    }

sf::Event event;
while (window.pollEvent(event))
{

if (event.type == sf::Event::Closed)
window.close();
}
        window.clear();
        window.draw ( health );
        window.display();

    }
    return 0;
}


 


Quote


|38|error: 'processEvents' was not declared in this scope|
|39|error: 'update' was not declared in this scope|
|42|error: 'updateStatistics' was not declared in this scope|
|43|error: 'render' was not declared in this scope|
||=== Build finished: 4 errors, 0 warnings (0 minutes, 0 seconds) ===|


Here's all the information guys, hope it helps and as usual thanks again in advance for any advice you give me :) I'm just at a brick wall with this at the moment but I managed to fix some basic things wrong with it myself thankfully so it looks like I'm learning.

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #107 on: August 26, 2013, 06:38:03 am »
Those errors mean you haven't defined the functions "processEvents", "update", "updateStatistics", or "render". You need to define them before using them. If you've already defined them in another source (.cpp) file, include the header (.hpp) that correponds to that source file at the beginning of your source file that has the main function.
I use the latest build of SFML2

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #108 on: August 26, 2013, 06:49:43 am »
Yes, that's what I need to do, the problem is I don't see any example or explanation of where or how I'm supposed to set it all up? Is it a normal method like my int healthstats? Or is it something a bit more complicated using sf::Time etc? That's what I'm trying to find out, there doesn't seem to be any information in the book, I'll post the source code I'm working from too.

This is the SFML Game Development book code I'm working from, please note, I'm not putting together files at the moment, I'm just trying to get timestep working by itself so ignore the headers.


#include <Book/Game.hpp>
#include <Book/StringHelpers.hpp>


const float Game::PlayerSpeed = 100.f;
const sf::Time Game::TimePerFrame = sf::seconds(1.f/60.f);

Game::Game()
: mWindow(sf::VideoMode(640, 480), "SFML Application", sf::Style::Close)
, mTexture()
, mPlayer()
, mFont()
, mStatisticsText()
, mStatisticsUpdateTime()
, mStatisticsNumFrames(0)
, mIsMovingUp(false)
, mIsMovingDown(false)
, mIsMovingRight(false)
, mIsMovingLeft(false)
{
        if (!mTexture.loadFromFile("Media/Textures/Eagle.png"))
        {
                // Handle loading error
        }

        mPlayer.setTexture(mTexture);
        mPlayer.setPosition(100.f, 100.f);
       
        mFont.loadFromFile("Media/Sansation.ttf");
        mStatisticsText.setFont(mFont);
        mStatisticsText.setPosition(5.f, 5.f);
        mStatisticsText.setCharacterSize(10);
}

void Game::run()
{
        sf::Clock clock;
        sf::Time timeSinceLastUpdate = sf::Time::Zero;
        while (mWindow.isOpen())
        {
                sf::Time elapsedTime = clock.restart();
                timeSinceLastUpdate += elapsedTime;
                while (timeSinceLastUpdate > TimePerFrame)
                {
                        timeSinceLastUpdate -= TimePerFrame;

                        processEvents();
                        update(TimePerFrame);
                }

                updateStatistics(elapsedTime);
                render();
        }
}

void Game::processEvents()
{
        sf::Event event;
        while (mWindow.pollEvent(event))
        {
                switch (event.type)
                {
                        case sf::Event::KeyPressed:
                                handlePlayerInput(event.key.code, true);
                                break;

                        case sf::Event::KeyReleased:
                                handlePlayerInput(event.key.code, false);
                                break;

                        case sf::Event::Closed:
                                mWindow.close();
                                break;
                }
        }
}

void Game::update(sf::Time elapsedTime)
{
        sf::Vector2f movement(0.f, 0.f);
        if (mIsMovingUp)
                movement.y -= PlayerSpeed;
        if (mIsMovingDown)
                movement.y += PlayerSpeed;
        if (mIsMovingLeft)
                movement.x -= PlayerSpeed;
        if (mIsMovingRight)
                movement.x += PlayerSpeed;
               
        mPlayer.move(movement * elapsedTime.asSeconds());
}

void Game::render()
{
        mWindow.clear();       
        mWindow.draw(mPlayer);
        mWindow.draw(mStatisticsText);
        mWindow.display();
}

void Game::updateStatistics(sf::Time elapsedTime)
{
        mStatisticsUpdateTime += elapsedTime;
        mStatisticsNumFrames += 1;

        if (mStatisticsUpdateTime >= sf::seconds(1.0f))
        {
                mStatisticsText.setString(
                        "Frames / Second = " + toString(mStatisticsNumFrames) + "\n" +
                        "Time / Update = " + toString(mStatisticsUpdateTime.asMicroseconds() / mStatisticsNumFrames) + "us");
                                                         
                mStatisticsUpdateTime -= sf::seconds(1.0f);
                mStatisticsNumFrames = 0;
        }
}

void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed)
{      
        if (key == sf::Keyboard::W)
                mIsMovingUp = isPressed;
        else if (key == sf::Keyboard::S)
                mIsMovingDown = isPressed;
        else if (key == sf::Keyboard::A)
                mIsMovingLeft = isPressed;
        else if (key == sf::Keyboard::D)
                mIsMovingRight = isPressed;
}


 

I double checked the headers the code was linked to and there was nothing there that lead me to believe that I missed anything critical to get the code compiling.

Edit: OH LOL! Wait a minute, what's this? It looks like I missed a .hpp file hidden in the folders that's got all the declarations in it, I think this is the right thing to look at, let me know, I didn't think anything of it because it was named the same name but just a different file type.


#ifndef BOOK_GAME_HPP
#define BOOK_GAME_HPP

#include <SFML/Graphics.hpp>


class Game : private sf::NonCopyable
{
        public:
                                                                Game();
                void                                    run();
               

        private:
                void                                    processEvents();
                void                                    update(sf::Time elapsedTime);
                void                                    render();

                void                                    updateStatistics(sf::Time elapsedTime);
                void                                    handlePlayerInput(sf::Keyboard::Key key, bool isPressed);
               

        private:
                static const float              PlayerSpeed;
                static const sf::Time   TimePerFrame;

                sf::RenderWindow                mWindow;
                sf::Texture                             mTexture;
                sf::Sprite                              mPlayer;
                sf::Font                                mFont;
                sf::Text                                mStatisticsText;
                sf::Time                                mStatisticsUpdateTime;

                std::size_t                             mStatisticsNumFrames;
                bool                                    mIsMovingUp;
                bool                                    mIsMovingDown;
                bool                                    mIsMovingRight;
                bool                                    mIsMovingLeft;
};

#endif // BOOK_GAME_HPP


 

I should be able to fix it now with this I think.

Edit: Now I'm not so sure looking at it more carefully >_<
« Last Edit: August 26, 2013, 07:10:55 am by Lethn »

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Lethn's Programming Questions Thread
« Reply #109 on: August 27, 2013, 05:00:16 am »
AGAIN, LENTH, class file are a BASIC c++ knowledge that uses a HEADER file (*.h, or *.hpp) and (most of the time) a corresponding *.cpp of the same name.  We've all told you to get a good base knowledge of general c++ practices before trying to delve into SFML stuff.  This goes double for any code from the SFML book since it relies heavily on separate class files. 

HELL, even when you posted the 'Game.cpp' file from the SFML book it CLEARLY shows how its code is broken into the 'update', 'render', 'processEvents', and 'updateStatistics' method.  You literally posted your own solution to your problem and didn't realize it.  This says you have a long ways to go before you even understand simple OOP (object oriented programming) principles.  PLEASE PLEASE PLEASE take a step back from SFML and just try to program up examples and programs in straight c++ that just make use of the console.  If you can do that you will have a much better understanding of SFML later on. 

I already posted like a dozen example programs you can try to write in one of my earlier posts in this thread, please if you truly want to learn to program try and do those examples before moving on to SFML related stuff.  You have to learn to crawl before you can drive a car, which is essentially what you're trying to do.

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #110 on: August 27, 2013, 06:59:35 am »
Hatchet thanks LOL sorry, I just realised what had happened, I was so tired I didn't notice what was right in front of me ( I'd been up for several hours without sleep ) and now I can see it easily because I've got plenty of sleep and I'm waking up with a coffee now.

I feel like an idiot :P I do understand OOP but the problem was I was too tired to notice what the hell was going on! Remember people, don't program when suffering from lack of sleep! >_< I had already been making a lot of Jewellery settings beforehand, I feel stupid for doing that.

angry swearing: SADSAJD KSKA JSLDKKS KJDL KALSA.

Note: This will be easy to fix now, I swear I was so wasted I didn't notice a thing  ;D
« Last Edit: August 27, 2013, 07:31:42 am by Lethn »

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #111 on: September 07, 2013, 12:11:31 am »

#include <iostream>
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window ( sf::VideoMode(800, 600), "" );

    const sf::Time TimePerFrame = sf::seconds ( 1.f / 60.f );

sf::Texture playerimage;
if ( !playerimage.loadFromFile ( "player.png" ) )
{

}
sf::Sprite playersprite ( playerimage );
playersprite.setPosition ( 300, 200 );


sf::Clock clock;
sf::Time TimeSinceLastUpdate = sf::Time::Zero;

    while (window.isOpen())


    {
        sf::Time ElapsedTime = clock.restart();
        TimeSinceLastUpdate += ElapsedTime;
        while ( TimeSinceLastUpdate > TimePerFrame )

        {
            TimeSinceLastUpdate -= TimePerFrame;
        }

        sf::Event event;
        while (window.pollEvent(event))

                if ( sf::Keyboard::isKeyPressed ( sf::Keyboard::A ) )
    {
        playersprite.move ( -5.0f, 0.0f );
    }

    else if ( sf::Keyboard::isKeyPressed ( sf::Keyboard::D ) )

    {
        playersprite.move ( 5.0f, 0.0f );
    }
        {

            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear();
        window.draw ( playersprite );
        window.display();
    }

    return 0;
}

 

I just need to get the independent movement going as described by the book and in the source code and it should work great, there's also this annoying thing with update and processEvents which as you can see I've removed for now but I'll keep tweaking everything.

You know what helped me a lot guys? The highlighting in codeblocks! I had noticed it before but I didn't really understand what it was about but then I found when you drag-highlighted classes etc. it highlighted where they were all linked to each other on the entire page :D anyway, should be able to make some more interesting stuff once I get the independent movement right.
« Last Edit: September 07, 2013, 12:14:10 am by Lethn »

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #112 on: September 12, 2013, 12:09:28 pm »
So guys, I'm having a proper read through of the C++ primer book again and get everything in my head. Do any of you know how I can read through it without losing the will to live? If you don't have an suggestions I guess I'm just going to have to persevere >_< :D.
« Last Edit: September 12, 2013, 12:18:14 pm by Lethn »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Lethn's Programming Questions Thread
« Reply #113 on: September 12, 2013, 12:27:23 pm »
I actually found it interesting to learn more about the programming language and its capabilities. But if you consider C++ a necessary evil instead of a powerful tool, it will of course be difficult to remain motivated... And you should consider another, simpler language. Don't learn C++ if you don't like it.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #114 on: September 12, 2013, 12:39:35 pm »
I like C++, it's looks like a well thought out language and all the explanations make sense, it's just the book that's the problem and it's something that schools often do where they manage to make something that should be really interesting extremely boring. Just through reading the section about basic types and variables I've figured out how to use decrementing/incrementing properly for games.

You know Bitcoin was programmed in C++? :D Like I said, really interesting language but the book manages to make it boring even though it's giving me all the information I need.
« Last Edit: September 12, 2013, 12:47:59 pm by Lethn »

Josh_M

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Lethn's Programming Questions Thread
« Reply #115 on: September 12, 2013, 02:28:39 pm »
I don't really see how it can be so boring o_0. I've been through a 1400 page C++ book thoroughly in like 2 weeks before, and that was without access to even a basic compiler, so all my programming practice was on paper with painstaking (and terrible) error checking. It wasn't the most interesting thing in itself, but if you make your own exercises to do that involve what you're currently learning, it helps a bit too. But primarily, I think you just need a real interest in learning the language, nothing else is really going to cut it.

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #116 on: September 12, 2013, 03:14:30 pm »
I wonder if I'm explaining it right, but I can give an example, it's a bit like learning about gemstones and where they live. In geography I had to learn about sedimentary and igneous rocks etc. and the text books didn't mention that I could get shiny gemstones out of them which could end up being worth thousands of pounds and that this kind of knowledge was necessary if you wanted to do any sort of mining and exploration for gold/silver etc. so in the end you end up thinking that there's no real use for that kind of knowledge in real life.

The book has the same problem where it doesn't really talk about applications in the real world and just uses an excruciatingly boring example of a book store, as if the guy who wrote the book has only ever done that with such a powerful programming language in his life. Like I said, the informations great and I like the language but ugh :P the applications.
« Last Edit: September 12, 2013, 03:24:13 pm by Lethn »

model76

  • Full Member
  • ***
  • Posts: 231
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #117 on: September 13, 2013, 03:00:53 am »
Well, not all of us learn equally well from a book alone, so why not try gameinstitute?
That way you will have an instructor and tests to see if you understood the material well enough.

They have 2 courses on C++, and all the examples are game related in some way.
You can safely quit the 2nd course when it gets to the Microsoft specific stuff. You won't need that for SFML.

Worked well enough for me 6 years ago, so perhaps it will work for you now.

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #118 on: September 13, 2013, 11:05:40 am »
It looks interesting but the very fact that he said 'industry professionals' had me nearly running for the hills and I say this after having recently patched Rome 2 Total War which froze and crashed more than it had in the unpatched version for me. I'd have to get more feedback from real people but because of past experiences I think I'm done with crappy over-generalised courses run by people who know absolutely nothing about games design. Now if they were to quote someone who had vetted their work and could actually give me source code to look at or I actually knew by reputation ( like mojang or 2D Boy ) then I'd be interested.

I don't know if I mentioned this before but I actually went on a games design course once that was supposed to be 1/2 years ( long time ago :D can't remember well, I just remember how bad it was ) and the only reason me or anyone else there learned about actual games design was because we ignored what they were trying to teach us which was just stupid written assignments on the theory of it all which looked like it had been written up by school teachers during lunch. It may well be because they were a government run school they and were more concerned with looking good and getting good attendance records/passing their students than actually teaching anything.

This is why I'm perfectly happy struggling with the book and the code because at least I'm actually learning something about game development! :P

Edit: You just gave me an idea though model76 but I hang around on Bitcointalk and there are a bunch of programmers there who specialise in all sorts of languages. Maybe I could see if one of them would be interested in teaching me because I've found I do learn a lot better 1 on 1 and I've seen them openly offering to do it for Bitcoins. Some of them as you know even helped explain some of the SFML programming to me.
« Last Edit: September 15, 2013, 03:52:09 pm by Lethn »

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #119 on: September 22, 2013, 09:24:41 pm »
So I'm experimenting more now with the code I've learned and making things, I've had no trouble putting up text as you can see in this code, changing colour, no problem, basic positioning etc. is fine.


#include <iostream>
#include <SFML/Graphics.hpp>
#include <sstream>

int main()
{

    sf::RenderWindow window(sf::VideoMode(800, 600), "");

    sf::Font arial;
    if ( !arial.loadFromFile ( "arial.ttf" ) )
    { }


    sf::Text FontOne;
    FontOne.setFont ( arial );
    FontOne.setCharacterSize ( 12 );
    FontOne.setColor ( sf::Color::Blue );
    FontOne.setPosition ( 350, 120 );

    FontOne.setString ( "This is line one!" );


    sf::Text FontTwo;
    FontTwo.setFont ( arial );
    FontTwo.setCharacterSize ( 12 );
    FontTwo.setColor ( sf::Color::Green );
    FontTwo.setPosition ( 350, 240 );

    FontTwo.setString ( "This is line two" );


        sf::Text FontThree;
    FontThree.setFont ( arial );
    FontThree.setCharacterSize ( 12 );
    FontThree.setColor ( sf::Color::Red );
    FontThree.setPosition ( 350, 360 );

    FontThree.setString ( "This is line three" );

while (window.isOpen())
{

sf::Event event;
while (window.pollEvent(event))
{

if (event.type == sf::Event::Closed)
window.close();
}

        window.clear();
        window.draw ( FontOne );
        window.draw ( FontTwo );
        window.draw ( FontThree );
        window.display();

    }
    return 0;
}

 

I want to experiment more and see what I can do with the basic stuff I've learned though, does anyone have example code out there they can post? I'd like to see stuff like scrolling text ( Like you get in Zelda etc. with dialogue ) and things like that.

 

anything