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

Author Topic: Help!  (Read 2783 times)

0 Members and 1 Guest are viewing this topic.

TajiTakeo

  • Newbie
  • *
  • Posts: 12
    • View Profile
Help!
« on: April 08, 2013, 09:48:47 pm »
I put my question here because I don't really know where else to put it.
I have a weird problem that i've never had before, I can't find a soulution to the problem.
When I start the game and I am controlling my sprite up,down,left,right it moves one pixel to a direction, stops for a sec, and then it moves weirdly foward. It almost looks like it is lagging foward. But if i remove the Event I can control the sprite normaly with no issues. But the problem is if I have no event the window gets get fucked up, and I can't max window or move window, and get the message "Program not responding".
I want to control the sprite just as good as I can when I play without events.

Sorry, I know I have explained my problem badly. But it is hard to explain this problem, and my english is bad.
But if anyone understand what I am trying say and knows a solution please reply.

Here is my main.cpp if it is of any help to understand. I don't think other code is relevant to this problem.

Main.cpp
#include <SFML/Graphics.hpp>
#include "data.hpp"
#include "controls.hpp"
#include "window.hpp"
#include "menu.hpp"
#include "act.hpp"

int main()
{
        Data.datainit();

        app.window.create(sf::VideoMode(videowidth,videoheight,32),"Game Engine");

        app.window.setFramerateLimit(fps);

    while(app.window.isOpen())
    {
                sf::Event events;

                if(app.window.pollEvent(events))
                {
                        if (events.type == sf::Event::Closed) app.window.close();
                       
                        Act.actinit();
                }
    }
        return 0;
}

massive_potato

  • Newbie
  • *
  • Posts: 37
    • View Profile
Re: Help!
« Reply #1 on: April 08, 2013, 10:11:35 pm »
For one thing, you should be using a while loop to poll your events, not an if-statement. You can check out the documentation for that here: http://sfml-dev.org/documentation/2.0/classsf_1_1Event.php. When you use an if-statement, you'll only be polling a single event out of a set of events that might include more that just one event, instead of polling all of the events per frame.

Also, are you using events to handle your key-presses? It works much more smoothly to use real-time input via the sf::Keyboard class. You can read up on that here: http://sfml-dev.org/documentation/2.0/classsf_1_1Keyboard.php and here: http://sfml-dev.org/tutorials/2.0/window-inputs.php and here: http://sfml-dev.org/tutorials/2.0/window-events.php

As to why you need the event, without event handling you can't close or interact with the window at all. That's why the window gets messed up as you described. The events allow you to interact with the window (minimize, maximize, close, etc.).

Could you also please include the code for "controls.hpp". Since you described your issue as stemming from controlling your sprite, another issue might be present in that code.

TajiTakeo

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Help!
« Reply #2 on: April 08, 2013, 10:38:19 pm »
Thank you for taking your time to help me  ;D
I am now using a while loop instead of if-statement.

Here is the code you asked for.
I will now check up on the links you gave me :)

#ifndef CONTROLS_H
#define CONTROLS_H

#include "data.hpp"

class movement
{
        public:

                void moveinit()
                {
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                        {
                                Data.player.move(0, -2);
                        }

                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                        {
                                Data.player.move(0, 2);
                        }

                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        {
                                Data.player.move(-2, 0);
                        }

                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        {
                                Data.player.move(2, 0);
                        }
                }

}Movement;

class camera : public sf::View
{
        public:

                void camerainit()
                {
                        float X = Data.player.getPosition().x;
                        float Y = Data.player.getPosition().y;
                        sf::View::setCenter(X,Y);
                }

}Camera;

class buttons
{
        public:

                void buttonsinit()
                {
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                        {
                                app.window.close();
                        }

                }
}Buttons;


#endif
 

massive_potato

  • Newbie
  • *
  • Posts: 37
    • View Profile
Re: Help!
« Reply #3 on: April 09, 2013, 04:00:09 am »
From what I can tell, nothing appears to be wrong with your controls. You're not using events to get real-time input, so that's not the source of the problem! :D

As far as what your problem is, you'll need to post the rest of your code, since the problem doesn't appear to be coming from your main. A good site for posting code would be www.github.com. You can make a free account there or host your code anonymously here: https://gist.github.com/

Once the remainder of your code is up, the problem should be fairly easy to diagnose and solve!

TajiTakeo

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Help!
« Reply #4 on: April 09, 2013, 06:47:01 pm »
Ok! i've uploaded the code on github.
https://github.com/TajiTakeo/Ludowicus-Motor-A1

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Help!
« Reply #5 on: April 09, 2013, 08:29:49 pm »
As far as what your problem is, you'll need to post the rest of your code, since the problem doesn't appear to be coming from your main.
A better tip would be to reduce the code to a minimal and complete example. Most people won't look at external pages for the source code. To get as much help as possible, one should try to keep the code as small as possible.

Furthermore, the thread title is as bad as it could possibly be (which is also explained in my link).
« Last Edit: April 09, 2013, 08:32:23 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

TajiTakeo

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Help!
« Reply #6 on: April 09, 2013, 10:36:17 pm »
I will keep that in mind. Thanks for reply Nexus :)

 

anything