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

Author Topic: Rendering of Game appears to glitch back and forth, not sure how to fix it.  (Read 1490 times)

0 Members and 1 Guest are viewing this topic.

nicholsml

  • Newbie
  • *
  • Posts: 4
    • View Profile
So, I created this project for fun but had a question. Basically, the game involves a circle that cycles through the color spectrum, and you can use the arrow keys to "draw" with the circle. Essentially I just have no App.clear() unless the user presses a certain key.

However, when the stuff actually renders out, the trails that were left behind by the circle seem to glitch back and forth, instead of staying still as I would prefer. Is there any easy way to fix this, or will it require reworking the way I render the trails?

Here's the source for those interested. You may have an error with the instructions font if you decide to compile it, so make sure to remove the text bits from the code first.

Thanks in advance!

// sfml.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <SFML\Graphics.hpp>
#include <iostream>

using namespace std;







int _tmain(int argc, _TCHAR* argv[])
{
        sf::RenderWindow window(sf::VideoMode(1600,900), "CubeDraw");

        window.setFramerateLimit(300);



        sf::CircleShape Circle(50);

        sf::Font default;

        default.loadFromFile("imagine_font.ttf");

        sf::Text instructions;

        instructions.setFont(default);

        instructions.setCharacterSize(16);

        Circle.setFillColor(sf::Color(255,255,255));

        instructions.setString("C = Clear ,= Reduce Size /= Increase Size N = Rotate Left M = Rotate Right .= Reset Position");

        instructions.setPosition(window.getSize().x /5,window.getSize().y -200);

        bool phase1=false,phase2=false,phase3=false,phase4=false,phase5=false,phase6=false,phase7=false,phase8=false,XkeyPressed = false,YkeyPressed = false;

        double acceleration = 5,velocityX = 0,velocityY = 0,max=20;

        //RGB Spectrum, used later on
        int r = 255;
        int g = 0;
        int b = 0;


        bool a=Circle.getScale().x;

         


    while (window.isOpen())
    {

                        sf::Event event;
                        while (window.pollEvent(event))
                        {
                                        if (event.type == sf::Event::Closed)
                                                window.close();
                       
                        }

                        //Phases of color cycling. Probably more efficient ways of doing this, but not highest priority.
                        if(r == 255 && g == 0 && b == 0)
                        {
                                phase1 = true;
                        }

                        if(phase1 == true)
                        {
                                if(g<255)
                                {
                                        g++;
                                }
                                if(g==255)
                                {
                                        phase2 = true;
                                        phase1 = false;
                                }
                        }
                        if(phase2 == true)
                        {
                                if(r>0)
                                {
                                        r--;
                                }
                                if(g<255)
                                {
                                        g++;
                                }
                                if(r==0 && g==255)
                                {
                                        phase3 = true;
                                        phase2 = false;
                                }
                        }

                        if(phase3 == true)
                        {
                                if(g>225)
                                {
                                        g--;
                                }
                                if(b<255)
                                {
                                        b++;
                                }
                                if(g==225 && b==255)
                                {
                                        phase4 = true;
                                        phase3 = false;
                                }
                        }
                        if(phase4 == true)
                        {
                                if(g>0)
                                {
                                        g--;
                                }
                                if(g==0)
                                {
                                        phase5 = true;
                                        phase4 = false;
                                }
                        }
                        if(phase5 == true)
                        {
                                if(r<150)
                                {
                                        r++;
                                }
                                if(r==150)
                                {
                                        phase6 = true;
                                        phase5 = false;
                                }
                        }
                        if(phase6 == true)
                        {
                                if(r<255)
                                {
                                        r++;
                                }
                                if(b>175)
                                {
                                        b--;
                                }
                                if(r==255 && b==175)
                                {
                                        phase7 = true;
                                        phase6 = false;
                                }
                        }
                        if(phase7 == true)
                        {
                                if(r<255)
                                {
                                        r++;
                                }
                                if(b>0)
                                {
                                        b--;
                                }
                                if(r==255 && b==0)
                                {
                                        phase1 = true;
                                        phase7 = false;
                                }
                        }


                        //Set color to whatever phase it is in the spectrum
                        Circle.setFillColor(sf::Color(r,g,b));

                        Circle.move(velocityX,velocityY);

                        //Output velocities to console for bug testing
                        cout << "X VEL " << velocityX << "Y VEL " << velocityY << endl;
                        system("cls");

                        //Physics stuff
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                        {
                                YkeyPressed = true;
                                velocityY = velocityY-acceleration;
                        }
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                        {
                                YkeyPressed = true;
                                velocityY = velocityY+acceleration;
                        }
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        {
                                XkeyPressed = true;
                                velocityX = velocityX-acceleration;
                        }
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        {
                                XkeyPressed = true;
                                velocityX = velocityX+acceleration;
                        }

                        if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Up)&&!sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                        {
                                YkeyPressed = false;
                        }
                        if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Right)&&!sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        {
                                XkeyPressed = false;
                        }

                        //Setting max velocity
                        if (velocityX >= max)
                        {
                                velocityX = max;
                        }
                        if(velocityX <= -max)
                        {
                                velocityX = -max;
                        }
                        if(velocityY >= max)
                        {
                                velocityY = max;
                        }
                        if(velocityY <= -max)
                        {
                                velocityY = -max;
                        }

                        //While not pressing keys, slow down
                        if(XkeyPressed == false)
                        {

                                if(velocityX>0)
                                {
                                        velocityX = velocityX - acceleration;
                                }
                                if(velocityX<0)
                                {
                                        velocityX = velocityX + acceleration;
                                }


                        }
                        if(YkeyPressed == false)
                        {
                                if(velocityY>0)
                                {
                                        velocityY = velocityY - acceleration;
                                }
                                if(velocityY<0)
                                {
                                        velocityY = velocityY + acceleration;
                                }
                        }



                        //Keys to change game mechanics



                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Slash))//Increase player size
                        {
                                sf::Vector2f scale = Circle.getScale();
                                Circle.setScale(scale.x*1.05,scale.y*1.05);
                        }
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Comma))//Decrease player size
                        {
                                sf::Vector2f scale = Circle.getScale();
                                Circle.setScale(scale.x*0.95,scale.y*0.95);
                        }
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Period))//Reset player position, rotation, and scale to default
                        {
                                Circle.setScale(1,1);
                                Circle.setPosition(0,0);
                                Circle.setRotation(0);
                        }
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::C))//Clear screen
                        {
                                window.clear();
                        }
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::N))//Rotate player left
                        {
                                Circle.rotate(-5);
                        }
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::M))//Rotate player right
                        {
                                Circle.rotate(5);
                        }


                        window.draw(Circle);
                        window.draw(instructions);
                        window.display();
        }

    return 0;
}
« Last Edit: June 02, 2014, 10:22:48 pm by nicholsml »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Quote
Essentially I just have no App.clear() unless the user presses a certain key.

I'm not surprised you have graphics issues then.

http://www.sfml-dev.org/tutorials/2.1/graphics-draw.php

Quote
This clear/draw/display cycle is the only good way to draw things. Don't try other strategies, such as keeping pixels from the previous frame, "erasing" pixels, or drawing once and calling display multiple times. You'll get strange results due to double-buffering.

(and yes it is in red)

On a side note please use the [ code=cpp ] CODE HERE [ /code ] tags when posting code (without spaces of course).  ;)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

nicholsml

  • Newbie
  • *
  • Posts: 4
    • View Profile
Sorry, didn't realize the way to post the code tags on here was via that drop down, I couldn't find it at first so I used the quotes.

Anyways, What would be a viable alternative to the current method? I'm fairly new to SFML so I wouldn't really know where to begin with attempting to have the shape leave trails.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Quote
I'm fairly new to SFML so I wouldn't really know where to begin with attempting to have the shape leave trails.

Keep track of previous positions and every frame after clearing redraw your circle at each previous position.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

nicholsml

  • Newbie
  • *
  • Posts: 4
    • View Profile
Oh wow, I didn't realize it was that simple. How would you redraw the same shape in the previous position though? could you provide a code example? I mean obviously storing previous position in an int, but I'm talking about the actual drawing part.

Sorry to keep bothering you with more questions, but I really appreciate the help!

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
The same way you draw it at any position.  If you want to draw it in multiple positions, you simply draw it multiple times (changing the position each time).