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

Author Topic: Problem with scrolling screen  (Read 2019 times)

0 Members and 1 Guest are viewing this topic.

Davitosan

  • Newbie
  • *
  • Posts: 5
    • View Profile
Problem with scrolling screen
« on: December 14, 2010, 09:05:51 am »
Hello, everybody! First of all, let me say that I'm very new to programming, C++ and SFML in general. I'm learning C++ alongside SFML because my ultimate goal is to make video games. So please bear with me if I ask rather stupid questions :D

Well, in my project I've managed to do the following:

- Load images and draw the sprites on the screen.
- Move the playable character.
- Animate the playable character.
- Basic collision detection (using my own half-baked algorithm).
- Scroll the screen.

Being a newbie programmer, I was elated to see these results, but now I'm getting a strange problem which I can't figure out. Allow me to elaborate:

Originally, the game had only one screen, a 800x600 area with a same-sized background image, with the character moving and jumping around. In order to test the scrolling, I drew a second "screen" on the right of the original one, an exact copy of the first screen. When I run the game, it all works fine, except that when I get closer to the rightmost edge of the second screen, the program lags considerably. This problem corrects itself if I move the character back to the first screen.

Since both of the screens are being drawn all the time, I can't figure out why it would matter where exactly on the screen the character is at. I understand that the position of the View and the x,y coordinates of the character become larger, but I don't think it should matter that a couple of floats change value from, say, 600 to 1500.

The code used to scroll the screen is tied to the character class' walking function:

Code: [Select]
if (ventana.GetInput().IsKeyDown(sf::Key::Left) && (animar_x > 0) )
{
caminando_izq = true;
if (caminando_der == true) arranque = 50;
caminando_der = false;

///////// SCROLLING STARTS HERE

if ( (animar_x < (cam_x + 300)) && (cam_x >= 0) )
{cam_x = cam_x - (-arranque) * ventana.GetFrameTime(); cam_x2 = cam_x2 - (-arranque) * ventana.GetFrameTime();}


EDIT: I forgot to say the cam_x ends up in View.Move(cam_x,0)

//////// SCROLLING ENDS HERE

animar_x = animar_x - (-arranque) * ventana.GetFrameTime();
if (arranque > -400) arranque -= 10;
set_x(animar_x);
}

if (ventana.GetInput().IsKeyDown(sf::Key::Right) && (animar_x < 1500) )
{
caminando_der = true;
if (caminando_izq == true) arranque = -50;
caminando_izq = false;

if ( (animar_x > (cam_x2 - 300)) && (cam_x2 <= 1600) )
{cam_x = cam_x + arranque * ventana.GetFrameTime();cam_x2 = cam_x2 + arranque * ventana.GetFrameTime();}

animar_x = animar_x + arranque * ventana.GetFrameTime();
if (arranque < 400) arranque += 10;
set_x(animar_x);
}


(Do forgive the spanish in the code. I'm mexican :) )

Just in case it matters, my collision detection goes something like this:

I created a detector class:

Code: [Select]
class detector {

public:

float x;
float y;
bool chocar;

detector()
{
x = (mono.animar_x + 10); // animar_x and animar_y are the x,y coordinates of the character sprite.
y = mono.animar_y;  
chocar = false;
}

void detectar()
{
y = y + 900 * ventana.GetFrameTime();
x = (mono.animar_x + 10);
if (y > 600) y = mono.animar_y + 30; // This prevents the detector from leaving the screen, which it does when the program lags.
}

}det;


This detector will move downwards from the player at a rate faster than free fall of the player. When it reaches a platform class shape, chocar will turn true, and the platform instance will return the detector to its original position.

Code: [Select]
void plataforma::checar()
{
if ((det.chocar == false) && (det.y > limite_y) && (det.y < by2) && (det.x > bx) && (det.x < bx2))

// This IF basically checks if the detector is inside the shape's boundaries.
{
det.y = limite_y; // limite_y is the topmost edge of the shape.
det.chocar = true;
}
if (det.chocar == true)
{
mono.min_y = det.y;  // mono.min_y is the "y" value that the character will consider the "floor".
det.y = (mono.animar_y + 50); // Return the detector to its starting position.
det.chocar = false;
}
}



Maybe this collision detection process is slowing things down, but I'm not sure.

Also, I wanted to ask: Is an 800x600 image too big to use as background for each screen? Should I split it into smaller tiles and draw only the visible ones?

Well, thanks in advance for any comments.

EDIT: I'm using a lower end computer, with Linux Mint distro using fluxbox. Graphics processing is kinda slow. Games like superTux and Battle for Wesnoth have issues with graphics as well (they are slow and jerky). I supposed that my program was too simple for this to matter, but maybe it's my system...

Davitosan

  • Newbie
  • *
  • Posts: 5
    • View Profile
Problem with scrolling screen
« Reply #1 on: December 14, 2010, 06:40:16 pm »
I managed to keep the framerate fluid by tiling the background. So, just how big an image can I use on SFML before it starts getting slow? I'm trying to make a Castlevania clone (SNES level) and I would be using sprites of varying dimensions.

Terrydil

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Problem with scrolling screen
« Reply #2 on: December 15, 2010, 05:34:07 pm »
Limits on the sizes of images will likely come from hardware/drivers, not SFML (or any other library really).  Someone can correct me if I'm just way off on that.

 

anything