12 Replies - 2991 Views - Last Post: 19 November 2012 - 04:47 PM Rate Topic: -----

#1 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

C++ - Moving a Camera Position (SDL)

Posted 18 November 2012 - 08:36 PM

Hello guys,
Belle again with another SDL question.

Recently, I finally got all of my basic set-ups involved in one 2D game.
This took a lot fo effort and great amounts of trial-and-error.

While reaching this goal, I figured I could move on to a bit different stradegy...
Moving images.
Is this right? Because this doesn't work:
void draw_sprite(int srcX, int srcY,
                 int dstX, int dstY,
                 int width, int height,
                 SDL_Surface* source, SDL_Surface* destination) {
     SDL_Rect src;
     src.x = srcX;
     src.y = srcY;
     src.w = width;
     src.h = height;
     
     SDL_Rect dst;
     dst.x = dstX;
     dst.y = dstY;
     dst.w = width;
     dst.h = height;

     if (player_ismoving && player_pos == 2) { //player_ismoving detects when the player is moving
          dstX -=1;                            //player_pos == 2 means if the player is facing right
     }
     
     SDL_BlitSurface(source, &src, destination, &dst);
}


SO why doesn't this work?
How can I fix it?

Thanks guys!
Belle (Anonymous Idon User)

Is This A Good Question/Topic? 0
  • +

Replies To: C++ - Moving a Camera Position (SDL)

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: C++ - Moving a Camera Position (SDL)

Posted 19 November 2012 - 02:57 AM

 if (player_ismoving && player_pos == 2)


Can you please explain what the above code does?

I am guessing your problem lies in this part of the code.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#3 MathewS  Icon User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 343
  • Joined: 14-May 02

Re: C++ - Moving a Camera Position (SDL)

Posted 19 November 2012 - 03:09 AM

If your not sure if a piece of code isn't working use break points.

The problem lies with that you are not storing the current dstX. If you pass in 100 as X then it will be set to 99 after the check, but then as soon as the method is fired again it will be 100 again.

You could either have the method alter the X variable directly or pass it in as a function.
Was This Post Helpful? 0
  • +
  • -

#4 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

Re: C++ - Moving a Camera Position (SDL)

Posted 19 November 2012 - 08:08 AM

View Postraghav.naganathan, on 19 November 2012 - 02:57 AM, said:

 if (player_ismoving && player_pos == 2)


Can you please explain what the above code does?

I am guessing your problem lies in this part of the code.

regards,
Raghav


Yeah, sorry it's not in my example. I have a character movement, and when a key (left, right, up, or down) player_ismoving turns true. When the key (left, right, up, and down) is not pressed, player_ismoving turns false.

player_pos is used for my character positions, it's 2D so their are position 4 positions. Down (1), Right (2), Up (3), and Left (4).
If both, player_ismoving and player_pos 2, this means the player is moving right.
Was This Post Helpful? 0
  • +
  • -

#5 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

Re: C++ - Moving a Camera Position (SDL)

Posted 19 November 2012 - 08:18 AM

View PostMathewS, on 19 November 2012 - 03:09 AM, said:

If your not sure if a piece of code isn't working use break points.

The problem lies with that you are not storing the current dstX. If you pass in 100 as X then it will be set to 99 after the check, but then as soon as the method is fired again it will be 100 again.

You could either have the method alter the X variable directly or pass it in as a function.


It's working properly, but I'm trying to do this to change the coordinates of the image.
You could relate it to this:
int x_coordinant = 30;
int y_coordinant = 30;

void change_coordinant(int x, int y) {
    x -= 10;
    y += 40;
}

int main() {
    change_coordinant(x_coordinant, y_coordinant);
}


Basically, I'm trying to make it change the x and y of the image so that it moves the "camera" or so-to-speak.
So how can I do this using my first example???
Was This Post Helpful? 0
  • +
  • -

#6 MathewS  Icon User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 343
  • Joined: 14-May 02

Re: C++ - Moving a Camera Position (SDL)

Posted 19 November 2012 - 09:04 AM

You don't change the dstX, dstY variables that are being passed in your method.
It looks like you are only ever passing in the initial values that you have set.

Oh and you create the SDL_Rect dst object before you change the dstX value, so it will be set to the value that is passed into the method, not the changed value.

View PostBlaze13, on 19 November 2012 - 03:18 PM, said:

View PostMathewS, on 19 November 2012 - 03:09 AM, said:

If your not sure if a piece of code isn't working use break points.

The problem lies with that you are not storing the current dstX. If you pass in 100 as X then it will be set to 99 after the check, but then as soon as the method is fired again it will be 100 again.

You could either have the method alter the X variable directly or pass it in as a function.


It's working properly, but I'm trying to do this to change the coordinates of the image.
You could relate it to this:
int x_coordinant = 30;
int y_coordinant = 30;

void change_coordinant(int x, int y) {
    x -= 10;
    y += 40;
}

int main() {
    change_coordinant(x_coordinant, y_coordinant);
}


Basically, I'm trying to make it change the x and y of the image so that it moves the "camera" or so-to-speak.
So how can I do this using my first example???


With the example you showed the coordinate would be set to 20, 70. But even if you ran it again it would still be 20, 70.

This post has been edited by MathewS: 19 November 2012 - 09:05 AM

Was This Post Helpful? 0
  • +
  • -

#7 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

Re: C++ - Moving a Camera Position (SDL)

Posted 19 November 2012 - 12:01 PM

View PostMathewS, on 19 November 2012 - 09:04 AM, said:

You don't change the dstX, dstY variables that are being passed in your method.
It looks like you are only ever passing in the initial values that you have set.

Oh and you create the SDL_Rect dst object before you change the dstX value, so it will be set to the value that is passed into the method, not the changed value.

View PostBlaze13, on 19 November 2012 - 03:18 PM, said:

View PostMathewS, on 19 November 2012 - 03:09 AM, said:

If your not sure if a piece of code isn't working use break points.

The problem lies with that you are not storing the current dstX. If you pass in 100 as X then it will be set to 99 after the check, but then as soon as the method is fired again it will be 100 again.

You could either have the method alter the X variable directly or pass it in as a function.


It's working properly, but I'm trying to do this to change the coordinates of the image.
You could relate it to this:
int x_coordinant = 30;
int y_coordinant = 30;

void change_coordinant(int x, int y) {
    x -= 10;
    y += 40;
}

int main() {
    change_coordinant(x_coordinant, y_coordinant);
}


Basically, I'm trying to make it change the x and y of the image so that it moves the "camera" or so-to-speak.
So how can I do this using my first example???


With the example you showed the coordinate would be set to 20, 70. But even if you ran it again it would still be 20, 70.


So if I were to put
if (player_ismoving && player_pos == 2) {
    dstX -= 1;
}

before the SDL_rect, it would work properly?


What if I did this in my example, then?:
int x_coordinant = 30;
int y_coordinant = 30;
bool gamerunning = true;

void change_coordinant(int x, int y) {
    x -= 1;
    y += 1;
}

int main() {
    while (gamerunning) {
        change_coordinant(x_coordinant, y_coordinant);
    }
}


Since my code is in a loop, would this change it every frame as well?

This post has been edited by Blaze13: 19 November 2012 - 12:02 PM

Was This Post Helpful? 0
  • +
  • -

#8 MathewS  Icon User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 343
  • Joined: 14-May 02

Re: C++ - Moving a Camera Position (SDL)

Posted 19 November 2012 - 12:23 PM

View PostBlaze13, on 19 November 2012 - 07:01 PM, said:

View PostMathewS, on 19 November 2012 - 09:04 AM, said:

You don't change the dstX, dstY variables that are being passed in your method.
It looks like you are only ever passing in the initial values that you have set.

Oh and you create the SDL_Rect dst object before you change the dstX value, so it will be set to the value that is passed into the method, not the changed value.


With the example you showed the coordinate would be set to 20, 70. But even if you ran it again it would still be 20, 70.


So if I were to put
if (player_ismoving && player_pos == 2) {
    dstX -= 1;
}

before the SDL_rect, it would work properly?

It would move it 1 pixel to the left, but not anymore.

View PostBlaze13, on 19 November 2012 - 07:01 PM, said:

What if I did this in my example, then?:
int x_coordinant = 30;
int y_coordinant = 30;
bool gamerunning = true;

void change_coordinant(int x, int y) {
    x -= 1;
    y += 1;
}

int main() {
    while (gamerunning) {
        change_coordinant(x_coordinant, y_coordinant);
    }
}


Since my code is in a loop, would this change it every frame as well?


It would only move for the first frame, then stay at that position.

With your change_coordinant method you are passing by value, which means it will copy the value that is passed into a local variable called x and y.

So you would need to reference the x_coordinant/y_coordinant in the change_coordinant for it to work.
Or you can pass a pointer/reference to the x/y variables into change_coordinant to alter the value.
Was This Post Helpful? 0
  • +
  • -

#9 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

Re: C++ - Moving a Camera Position (SDL)

Posted 19 November 2012 - 01:50 PM

View PostMathewS, on 19 November 2012 - 12:23 PM, said:

View PostBlaze13, on 19 November 2012 - 07:01 PM, said:

View PostMathewS, on 19 November 2012 - 09:04 AM, said:

You don't change the dstX, dstY variables that are being passed in your method.
It looks like you are only ever passing in the initial values that you have set.

Oh and you create the SDL_Rect dst object before you change the dstX value, so it will be set to the value that is passed into the method, not the changed value.


With the example you showed the coordinate would be set to 20, 70. But even if you ran it again it would still be 20, 70.


So if I were to put
if (player_ismoving && player_pos == 2) {
    dstX -= 1;
}

before the SDL_rect, it would work properly?

It would move it 1 pixel to the left, but not anymore.

View PostBlaze13, on 19 November 2012 - 07:01 PM, said:

What if I did this in my example, then?:
int x_coordinant = 30;
int y_coordinant = 30;
bool gamerunning = true;

void change_coordinant(int x, int y) {
    x -= 1;
    y += 1;
}

int main() {
    while (gamerunning) {
        change_coordinant(x_coordinant, y_coordinant);
    }
}


Since my code is in a loop, would this change it every frame as well?


It would only move for the first frame, then stay at that position.

With your change_coordinant method you are passing by value, which means it will copy the value that is passed into a local variable called x and y.

So you would need to reference the x_coordinant/y_coordinant in the change_coordinant for it to work.
Or you can pass a pointer/reference to the x/y variables into change_coordinant to alter the value.


Ah, so theirs no simpler way I can change image coordinants?
I'd have to do each manually?

Alright, thanks anyways. I have about 40 images set-up as a background, and I figured I could just do this to skip Copy&Pasting the same code for each image.
I'll just do something like:
int image1_x = 30;
int image1_y = 30;
int image2_x = 60;
int image2_y = 30;

bool gamerunning = true;

int main() {
  while (gamerunning) {
    image1.x = image1_x;
    image1.y = image1_y;
    if (player_ismoving && player_pos == 2 && player_x >= screen_width/1.5) {
        image1_x -= 1
    }
    image2.x = image2_x;
    image2.y = image2_y;
    if (player_ismoving && player_pos == 2 && player_x >= screen_width/1.5) {
        image2_x -= 1
    }
  }
}

See, that's 6 lines on two images just trying to make them move. With about 40, that's about 120 lines in moving position. For one movement position.

This post has been edited by Blaze13: 19 November 2012 - 01:54 PM

Was This Post Helpful? 0
  • +
  • -

#10 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 771
  • View blog
  • Posts: 2,250
  • Joined: 20-March 10

Re: C++ - Moving a Camera Position (SDL)

Posted 19 November 2012 - 02:03 PM

Quote

See, that's 6 lines on two images just trying to make them move. With about 40, that's about 120 lines in moving position. For one movement position.


Silly question time,..

Why do you need 40 images for a background... ?

Traditionally your background is one image....

Also as for games taking up a lot of repetitive coding...


Welcome to games programming....:) its kinda like that.....

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#11 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

Re: C++ - Moving a Camera Position (SDL)

Posted 19 November 2012 - 02:26 PM

View Postsnoopy11, on 19 November 2012 - 02:03 PM, said:

Quote

See, that's 6 lines on two images just trying to make them move. With about 40, that's about 120 lines in moving position. For one movement position.


Silly question time,..

Why do you need 40 images for a background... ?

Traditionally your background is one image....

Also as for games taking up a lot of repetitive coding...


Welcome to games programming....:)/> its kinda like that.....

Snoopy.


For simpler setup. My application will be online multiplayer with multiple images including trees, buildings, objects, weapons, Ect.
I have a function set for asking the image width and height, so It automatically makes it so you cannot walk over the top of a tree or building or whatever it is.
Without multiple images on the background, it'd take quite a bit longer to create everything.
Btw, by background, I mean any image on the ground. Guess background wasn't a good word to use.

Yeah, I realize so. This topic was just confusing me because I have so many functions for weapons, items, Ect. Where I can just type in
player_weapon(weapon_width, weapon_height, weapon_image);

And it will just automatically place the weapon in my characters hands (with movement, attacking, and walking).
That's what I was trying to do, but with image movement while walking. Turns out its not possible this way.
Was This Post Helpful? 0
  • +
  • -

#12 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 771
  • View blog
  • Posts: 2,250
  • Joined: 20-March 10

Re: C++ - Moving a Camera Position (SDL)

Posted 19 November 2012 - 02:53 PM

Well,

Traditionally you would just create a memory map..

You probably could reduce your 40 plus images to at least 20..


A memory map is just a map that says if your in that location you cannot turn left and move through that wall...

It does not rely on image info it just relies on position of the principal character....

Its still a lot of coding but anything that reduces your individual image use is good in my opinion.

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#13 MathewS  Icon User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 343
  • Joined: 14-May 02

Re: C++ - Moving a Camera Position (SDL)

Posted 19 November 2012 - 04:47 PM

I'm not going to say not to try and go for the multiplayer application as people always say that, and it isn't always true, it's the trial and error that counts. But you are in for a tough time :)/>

Anyway back on topic, you could still use the method to do those movements, what you need to learn about are pointers/referencing.

See once you learn about those you could then do something like this.

Object backgroundObjectOne;
Object backgroundObjectTwo;

bool gamerunning = true;

void change_coordinant(Object * obj) {
    obj.x -= 1;
    obj.y += 1;
}

int main() {
    while (gamerunning) {
          change_coordinant(&backgroundObjectOne);
          change_coordinant(&backgroundObjectTwo);
    }
}




But ideally you don't want to create a separate object for each one, you would want a list of objects then you would iterate though it.

This post has been edited by MathewS: 19 November 2012 - 04:47 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1