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

Author Topic: Code Hints in Code::Blocks?(mostly solved)  (Read 6873 times)

0 Members and 1 Guest are viewing this topic.

BlitzCoder

  • Newbie
  • *
  • Posts: 11
    • View Profile
Code Hints in Code::Blocks?(mostly solved)
« on: April 24, 2010, 01:57:04 am »
I was not sure where this post belonged, since I don't know if it is a graphics error.

Currently I am writing a small version of Pong. My code works great so far, with one little issue. In the code editor itself (I am using code::blocks if it matters), using the . operator on my sprite is doing nothing. Other things like sf:: or App. work fine in the sense they show a list of usable options or at least let me type them myself. However, for some reason this is not working for my bluePaddleSprite when I try bluePaddleSprite.

Currently I am trying to add collision of sorts so it does not go out of bounds, and I was going to do this by checking if the bluePaddleSprite's y was in bounds of my 800x600 window, and if so set it back a few or something. Code follow:

 
Code: [Select]

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

int main()
{
    // Create the main rendering window
   sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Pong");
   sf::Image bluePaddle;

   if (!bluePaddle.LoadFromFile("bluePaddle.png"))
    {
    std::cout << "Error, bluePaddle.png failed to load";
    }

sf::Sprite bluePaddleSprite(bluePaddle);
bluePaddleSprite.SetY(100);
bluePaddleSprite.SetScaleY(1.5);


    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();

            // A key has been pressed
            if (Event.Type == sf::Event::KeyPressed)
            {
                // Escape key : exit
                if (Event.Key.Code == sf::Key::Escape)
                    App.Close();
            }
        }
        // Clear the screen
        App.Clear(sf::Color(0, 0, 0));

      if (App.GetInput().IsKeyDown(sf::Key::W))  {
         bluePaddleSprite.Move(0, 150 * App.GetFrameTime() * -1);
      } else if (App.GetInput().IsKeyDown(sf::Key::S)) {
        bluePaddleSprite.Move(0, 150 * App.GetFrameTime() * 1);
      }

      App.Draw(bluePaddleSprite);

        // Display window contents on screen
        App.Display();
    }


    return EXIT_SUCCESS;
}


It compiles and runs, but when I try to use the . with my sprite nothing happens. Any help would be very welcome.

Thank you.

nulloid

  • Full Member
  • ***
  • Posts: 134
    • View Profile
Code Hints in Code::Blocks?(mostly solved)
« Reply #1 on: April 24, 2010, 02:10:38 am »
Have you tried pressing ctrl+space?

Well, sometimes I encounter this problem, too, but I don't mind :) Btw, I think it is not related to SFML, but to code::block's parser (I use C::B, too). I think you should search for this problem in the code::block's forum. But you can wait for other's responses, maybe I'm talking stupid things... it happens :)

BlitzCoder

  • Newbie
  • *
  • Posts: 11
    • View Profile
Code Hints in Code::Blocks?(mostly solved)
« Reply #2 on: April 24, 2010, 02:25:56 am »
what does control + space do? Nothing happened for me. As I mentioned, stuff like App. and such work, just my sprite won't... I'll keep looking into it though, its currently holding everything up..

BlitzCoder

  • Newbie
  • *
  • Posts: 11
    • View Profile
Code Hints in Code::Blocks?(mostly solved)
« Reply #3 on: April 24, 2010, 02:47:59 am »
Well, although it does not seem to be working 100% it seems as long as I know the command it will work. On a slightly side topic:

Do you know, or know how to find, the x/y values for the sides of my window? I wanted to have it so
Code: [Select]

      if (bluePaddleSprite.GetPosition().y == -50)
      {
        bluePaddleSprite.SetY(100);
      }


would work (-50 can be any number, but is supposed to be the border of the window so the paddle can't go past it...)

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Code Hints in Code::Blocks?(mostly solved)
« Reply #4 on: April 24, 2010, 02:50:07 am »
What are talking about?

The code runs and moves as it's supposed to.

BTW you need App.SetFramerateLimit(60);

Also App.GetWidth()/App.GetHeight() will get what you need.

BlitzCoder

  • Newbie
  • *
  • Posts: 11
    • View Profile
Code Hints in Code::Blocks?(mostly solved)
« Reply #5 on: April 24, 2010, 03:00:31 am »
Quote from: "Ashenwraith"
What are talking about?

The code runs and moves as it's supposed to.

BTW you need App.SetFramerateLimit(60);

Also App.GetWidth()/App.GetHeight() will get what you need.


Maybe I did not word it right.. the code itself works fine, I was saying that when I was editing it bluePaddleSprite was not giving me any options when I used the . operator. I was confused (still am) because it was working before.

Edit: Thank you for the code though. I am new to SFML (2D in general)

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Code Hints in Code::Blocks?(mostly solved)
« Reply #6 on: April 24, 2010, 03:03:49 am »
If you have a problem with the IDE you need to explain what IDE you are using and what you are doing, NOT posting code.

We thought you were complaining about code hints in code::blocks

If you want the code hints box to appear in code::blocks then you must type your data type correctly and then press ctrl+space.

Otherwise you are relying on auto-hint to guess if you are taking a while to think and not pressing anything.

BlitzCoder

  • Newbie
  • *
  • Posts: 11
    • View Profile
Code Hints in Code::Blocks?(mostly solved)
« Reply #7 on: April 24, 2010, 03:13:47 am »
Quote from: "Ashenwraith"

If you have a problem with the IDE you need to explain what IDE you are using and what you are doing, not posting code.


Um.. I did say I was using code::blocks in my OP... I was not sure if the error was IDE or SFML related, and nothing on the C::B forums helped.

Anyways, moving past that, I used the App.GetHeight() to try to fix it but


Code: [Select]


      if (bluePaddleSprite.GetPosition().y == App.GetHeight())
      {
        bluePaddleSprite.SetY(100);
      }


Does not seem to work. I get the feeling I am making a simple error but I am not sure what that is. What I am trying to do is say "If the paddle hits the top (and after that version for the bottom), reset it". No go so far.

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Code Hints in Code::Blocks?(mostly solved)
« Reply #8 on: April 24, 2010, 03:19:17 am »
The origin of x,y coordinates is 0,0. Width and Height begins at 1,1.

You need to subtract the height/width of your object to the screen size.

Code: [Select]


if(bluePaddleSprite.GetPosition().x<App.GetWidth()-bluePaddle.GetWidth())
{

}


BTW, for archiving purposes plz change the name of your thread to [solved]Code Hints in Code::Blocks?

BlitzCoder

  • Newbie
  • *
  • Posts: 11
    • View Profile
Code Hints in Code::Blocks?(mostly solved)
« Reply #9 on: April 24, 2010, 03:22:08 am »
Quote from: "Ashenwraith"
The origin of x,y coordinates is 0,0. Width and Height begins at 1,1.

You need to subtract the height/width of your object to the screen size.

BTW, for archiving purposes plz change the name of your thread to [solved]Code Hints in Code::Blocks?


Changed title to [solved]Code Hints in Code::Blocks? as requested, but it really isn't solved since the original issue is still present, but w/e.

Edit: Thank you for the code snippet.

Edit 2: Is the "w" supposed to be a variable for width I need to make. or is it w/e the width of my screen is (IE 800)

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Code Hints in Code::Blocks?(mostly solved)
« Reply #10 on: April 24, 2010, 03:24:39 am »
Well what is the problem?

I thought we finally figured it out.

BlitzCoder

  • Newbie
  • *
  • Posts: 11
    • View Profile
Code Hints in Code::Blocks?(mostly solved)
« Reply #11 on: April 24, 2010, 03:27:32 am »
Quote from: "Ashenwraith"
Well what is the problem?

I thought we finally figured it out.


Nah, still now giving me options when I use bluePaddleSprite. but it seems if I know what to put its ok.. w/e, close enough, I was just confused as to why it suddenly stopped giving me fill options.

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Code Hints in Code::Blocks?(mostly solved)
« Reply #12 on: April 24, 2010, 03:29:34 am »
type: bluePaddle.

Make sure the caret is after the .

Then press ctrl+space

BlitzCoder

  • Newbie
  • *
  • Posts: 11
    • View Profile
Code Hints in Code::Blocks?(mostly solved)
« Reply #13 on: April 24, 2010, 03:34:48 am »
Quote from: "Ashenwraith"
type: bluePaddle.

Make sure the caret is after the .

Then press ctrl+space


works with bluePaddle fine, but not with bluePaddleSprite.

Also, that formula you gave me.. now I can move the paddle at all because it keeps resetting...
Code: [Select]

      if(bluePaddleSprite.GetPosition().x<App.GetHeight()-(800+20))
      {
        bluePaddleSprite.SetY(100);
      }


I know that is wrong, but no matter what values I change it seems to not work. (I mean, the paddle moves, but snaps back immediately). Maybe I read the code wrong...

Edit: sorry, I misunderstood your code, thank you for clarifying. Sorry for all the trouble I have caused :(

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Code Hints in Code::Blocks?(mostly solved)
« Reply #14 on: April 24, 2010, 03:36:50 am »
Check my updated code.

You need to pay attention as I was just grabbing stuff from my own code.

x is for width, y is for height.