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

Author Topic: Another View Question  (Read 4586 times)

0 Members and 1 Guest are viewing this topic.

Astrof

  • Full Member
  • ***
  • Posts: 135
    • View Profile
Another View Question
« on: May 05, 2011, 07:50:16 am »
I'm having trouble wrapping my head around View.  Basically I want to create a world that say is 1024x768 but the screen size is only 800x600 (as example values).  I want the view to shift based on if an arrow key is pressed.  Kind of like Asteroids or Geometry Wars where the View centers on the player and the world moves around them.


Edit: Forgot to mention what I have so far.  I basically have some code that has a Sprite with a position at the center of the screen and whenever I push an arrow key I try to move the view 30 units.  The Sprite just stays and doesn't move.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Another View Question
« Reply #1 on: May 05, 2011, 08:07:12 am »
Sorry it's not clear: do you want to move the view and not the sprite, or move the sprite and make the view follow it?
Laurent Gomila - SFML developer

Astrof

  • Full Member
  • ***
  • Posts: 135
    • View Profile
Another View Question
« Reply #2 on: May 05, 2011, 08:45:05 am »
Er sorry the overall goal is when the sprite is moved follow it (until we reach the end and then move the sprite to the edge).  But for this simple test just to move the view when a key is pressed.  The Sprite doesn't change position.  


So basically:
I start with the Sprite centered on screen.  When a key is pressed the view moves (with the illusion of the Sprite shifting the other way).  I'm basically trying to play with the view and keeping the Sprite as a reference point.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Another View Question
« Reply #3 on: May 05, 2011, 08:52:59 am »
And what exactly is your problem? What have you tried? The view has SetPosition and Move functions, just like sprites, so it's easy to implement what you want.
Laurent Gomila - SFML developer

Astrof

  • Full Member
  • ***
  • Posts: 135
    • View Profile
Another View Question
« Reply #4 on: May 05, 2011, 07:15:33 pm »
I tried when I hit a key to move the view.  They key is pressed but nothing happens on screen (the Image of the Sprite just stays centered and nothing seems to move).
I did some debugging and the view is moving (the center is shifting) but nothing else happens.  What I expect to happen is the Sprite moving some direction (as the view moves towards or away from it).


EDIT: Okay so I added a App.SetView() Right before my draws.  That seemed to fix it.  However I thought that if I didn't change the view I didn't have to set the view every frame?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Another View Question
« Reply #5 on: May 05, 2011, 07:46:37 pm »
Are you using SFML 1.6 or 2.0? Is your view the active one in the window?
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Another View Question
« Reply #6 on: May 05, 2011, 08:10:48 pm »
Quote
Okay so I added a App.SetView() Right before my draws. That seemed to fix it. However I thought that if I didn't change the view I didn't have to set the view every frame?

Depends. With SFML 2.0 you need to call SetView every time the view changes, because the window stores its own copy.
With SFML 1.6 you only need to do this once, since the window keeps a pointer to your view.
Laurent Gomila - SFML developer

Astrof

  • Full Member
  • ***
  • Posts: 135
    • View Profile
Another View Question
« Reply #7 on: May 05, 2011, 08:13:42 pm »
I'm using SFML 2.0.  I don't change the View except for the initial SetView.  I only have one View that I'm using; the View I created and set.

Oh so if I Move the View it changes it but not a local copy?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Another View Question
« Reply #8 on: May 05, 2011, 08:25:09 pm »
Quote
Oh so if I Move the View it changes it but not a local copy?

Yes.
Laurent Gomila - SFML developer

Astrof

  • Full Member
  • ***
  • Posts: 135
    • View Profile
Another View Question
« Reply #9 on: May 05, 2011, 09:56:17 pm »
Ah yes this makes sense now since I was following the 1.6 tutorials.  

Just curious, what is the reason for the local copy instead of a pointer?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Another View Question
« Reply #10 on: May 05, 2011, 11:11:21 pm »
Quote
Just curious, what is the reason for the local copy instead of a pointer?

The reason is that you don't need to bother about owning/managing the view on your side, you can just build a view on the fly, call SetView and that's all.
Laurent Gomila - SFML developer

Astrof

  • Full Member
  • ***
  • Posts: 135
    • View Profile
Another View Question
« Reply #11 on: May 06, 2011, 01:31:28 am »
I'm confused as now instead I have to call a SetView every time I change the view?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Another View Question
« Reply #12 on: May 06, 2011, 07:33:47 am »
Why is it confusing? It seems more logical to me, there's no automagic update anyway, you explicitely call a function to change the view.
Laurent Gomila - SFML developer

Astrof

  • Full Member
  • ***
  • Posts: 135
    • View Profile
Another View Question
« Reply #13 on: May 06, 2011, 08:07:41 am »
To me it was easier to just have to set the View once and be done.  Now I need to keep a copy of the view where I move it and update it every time.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Another View Question
« Reply #14 on: May 06, 2011, 08:44:48 am »
Quote
To me it was easier to just have to set the View once and be done. Now I need to keep a copy of the view where I move it and update it every time.

I don't understand. You needed to keep the view before. Now you can just get it from the window and modify it whenever you want. Or you can keep things like before, and just call window.SetView everytime you modify it.

Programming is not always about making things as easy as possible, having a clean and robust design is important too ;)
Laurent Gomila - SFML developer

 

anything