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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - rustam2735

Pages: [1]
1
Window / Re: moving a Sprite using class (.h file)
« on: November 21, 2012, 08:29:25 pm »
ok, sorry about this

2
Window / Re: moving a Sprite using class (.h file)
« on: November 21, 2012, 06:43:37 pm »
that was just a mistake. as you can see it is really messy. sorry for that.

there aren't any errors, it just that in the window my sprite only moves 7 space (because my moveSpeed=7) in any direction. but the problem i think is in the player.cpp where it resets coordinateX and coordinateY back to 10.

however if i dont use any classes, then my sprite moves perfectly around the window

this is my main.cpp with out any classes.


#include <SFML/Graphics.hpp>

#define sDown 0
#define sLeft 48
#define sUp 144
#define sRight 96

int main()
{
    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Sprite");
       
        sf::Image tempImage;
    if (!tempImage.LoadFromFile("walking2.png"))
        return EXIT_FAILURE;
       
    sf::Sprite playerSprite(tempImage);
       
        float velocityX=0, velocityY=0;
        float x=10,y=10, moveSpeed=7;
        int sourceX=0,sourceY=sDown;
       
    // 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();
                        if ((Event.Type==sf::Event::KeyPressed)&&(Event.Key.Code==sf::Key::Escape))
                                App.Close();
        }
                if (App.GetInput().IsKeyDown(sf::Key::Right)){
                        sourceY=sRight;
                        velocityX=moveSpeed;
                }
                else if (App.GetInput().IsKeyDown(sf::Key::Left)){
                        sourceY=sLeft;
                        velocityX=-moveSpeed;
                }
                else velocityX=0;
               
                if (App.GetInput().IsKeyDown(sf::Key::Down)){
                        sourceY=sDown;
                        velocityY=moveSpeed;
                }
                else if (App.GetInput().IsKeyDown(sf::Key::Up)){
                        sourceY=sUp;
                        velocityY=-moveSpeed;
                }
                else velocityY=0;
               
                x+=velocityX;
                y+=velocityY;
               
                if (velocityX!=0||velocityY!=0)
                        sourceX+=tempImage.GetWidth()/3;
                else  sourceX=0;
                if (sourceX==tempImage.GetWidth()) sourceX=0;
               
                App.Clear();
                playerSprite.SetSubRect(sf::IntRect(sourceX,sourceY,sourceX+tempImage.GetWidth()/3,sourceY+tempImage.GetHeight()/4));
                playerSprite.SetPosition(x,y);
               
               
                                App.SetFramerateLimit(10);
        // Clear screen
        App.Clear(sf::Color(255,255,255));
               
        // Display sprite in our window
        App.Draw(playerSprite);
               
        // Display window contents on screen
        App.Display();
    }
       
    return EXIT_SUCCESS;
}
 

3
Window / moving a Sprite using class (.h file)
« on: November 21, 2012, 05:03:29 pm »
Hi, I can make the sprite character move on the with out any problem with out using any classes or headers, however when ever I try make use of .h files my sprite resets back to the original position. it just wont save the curent location, always resets it to the original coordinates.

i know its really badly written and there some exces staff on it. Please any one help, I pretty sure it is something stupid that i am missing

player.h
#include <SFML/Graphics.hpp>
#include <iostream>
#include "animation.h"

class Player
{
public:
        void loadPlayer();
        void playerMovement(sf::RenderWindow &window);
        void drawPlayer(sf:: RenderWindow &window);
        int inc(sf::RenderWindow &window){
                        c=100;
                        if(window.GetInput().IsKeyDown(sf::Key::C)) {c++;
                                std::cout<<"c:"<<c<<std::endl;}
               
        }
                ~Player(){
                        c--;
                        std::cout<<"c:"<<c<<std::endl;
                }
protected:
        Animation playerAnimation;
        //playerAnimation.isActive();
private:
       
        sf::Image playerImage;
        sf::Sprite playerSprite;
        float VelocityX,velocityY;
        float coordinateX, coordinateY, moveSpeed;
        int sourceX, sourceY;
        static int c;
               
};
 

player.cpp

#include "player.h"

void Player::loadPlayer(){
        if(playerImage.LoadFromFile("player.png")){
                playerSprite.SetImage(playerImage);}
}
int Player::c=0;


void Player::playerMovement(sf::RenderWindow &window){
        int sDown=0;
        int sLeft=48;
        int sUp=144;
        int sRight=96;
        coordinateX=0,coordinateY=0;
        coordinateX=10, coordinateY=10;
        sourceX=0, sourceY=0;
        moveSpeed=7;
       
        if (window.GetInput().IsKeyDown(sf::Key::Right)){
                sourceY=sRight;
                coordinateX+=moveSpeed*window.GetFrameTime();//needs work
        }
        else if (window.GetInput().IsKeyDown(sf::Key::Left)){
                sourceY=sLeft;
                coordinateX=-moveSpeed;
        }
        else coordinateX=0;
       
        if (window.GetInput().IsKeyDown(sf::Key::Down)){
                sourceY=sDown;
                coordinateY=moveSpeed;
        }
        else if (window.GetInput().IsKeyDown(sf::Key::Up)){
                sourceY=sUp;
                coordinateY=-moveSpeed;
        }
        else coordinateY=0;
        playerAnimation.setPosition(1, coordinateX);
        playerAnimation.setPosition(2, coordinateY);
       
        coordinateX+=coordinateX;
        coordinateY+=coordinateY;
       
        if (coordinateX!=0||coordinateY!=0)
                sourceX+=playerImage.GetWidth()/3;
        else  sourceX=0;
        if (sourceX==playerImage.GetWidth()) sourceX=0;
       
        playerSprite.SetSubRect(sf::IntRect(sourceX,sourceY,sourceX+playerImage.GetWidth()/3,sourceY+playerImage.GetHeight()/4));
        playerSprite.SetPosition(coordinateX,coordinateY);
        window.Clear(sf::Color(c,255,255));    
        window.Draw(playerSprite);

        std::cout<<"velocityX:"<<coordinateX<<std::endl;


}

void Player::drawPlayer(sf:: RenderWindow &window){
        window.Draw(playerSprite);
}

 

animation.h
#include <SFML/Graphics.hpp>

class Animation
{
public:
        void setPosition(int axis, float value);
        bool isActive(bool active);
private:
        float coordinateX,coordinateY;
};
 

animation.cpp
#include "animation.h"

bool Animation::isActive(bool active){
        active=true;
}

void Animation::setPosition(int axis, float value){
        if(axis==1)
                value=coordinateX;
        else
                value=coordinateY;
       
        this->coordinateX=coordinateY;
        this->coordinateY=coordinateY;

}
 

main.cpp

int main()
{
    sf::RenderWindow window(sf::VideoMode(640, 480), "SFML Graphics");
       
        Player mainCharecter;
        mainCharecter.loadPlayer();

        int i=1;
        bool active=true;
        /*
        float velocityX=0,velocityY=0;
        float x=10, y=10;
        int sourceX=0, sourceY=0;
        */

    while (active==true)
    {
        sf::Event event;
        while (window.GetEvent(event))
        {
            if (event.Type == sf::Event::Closed||event.Key.Code==sf::Key::Escape)
                active=false;
                }
                mainCharecter.playerMovement(window);

                window.SetFramerateLimit(10);

        window.Display();
    }

    return EXIT_SUCCESS;
}

 

4
Window / Re: changing number of pixels, with SFML/Window.hpp
« on: October 24, 2012, 05:06:09 pm »
I was drawing a line a cross the window, and it was in the range of [-200,200]. i just realised what what was my problem, i was just being stupid, got everything mixed up (gltraslatef(0,0-200) thats why). I am sorry for my stupidity.


5
Window / changing number of pixels, with SFML/Window.hpp
« on: October 24, 2012, 10:13:48 am »
This might be very stupid, but how do you increase the number of pixels on the window. I have noticed that my window has 400x400 pixels (as far as I understand window app( sf::VideoMode(800,600,32)"title") doesn't change it).

and also in my code, every time i press 'R' (restart) it returns me to specific point and no matter how many time i press'R' it goes back to the same point.

#include <math.h>
#include <iostream>
#include <time.h>
#include <cstdio>
#include <SFML/Window.hpp>



void Circle(float radius,float x, float y){
        const float DEG2RAD = 3.14159/180;
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(x,y,-200);
        glBegin(GL_LINE_LOOP);
        for(int i=0; i<360; i++){
                float degInRad = i*DEG2RAD;
                glVertex2f(cos(degInRad)*radius,sin(degInRad)*radius);
        }
       
        glEnd();
}

bool checkCollision(float Ax, float Ay, float Aw, float Ah, float circleX, float circleY, float r) //Funcfion for checking collision
{
        if ( Ay+Ah < circleY ) return false; //if A is more to the left than B
        else if ( Ay > circleY+r ) return false; //if A is more to the right than B
        else if ( Ax+Aw < circleX ) return false; //if A is higher than B
        else if ( Ax > circleX+r ) return false; //if A is lower than B
       
        return true; //There is a collision because none of above returned false
}


int main()
{
    // Create the main window
    sf::Window App(sf::VideoMode(600,600,12), "bouncing ball");
        float myX=0;
        float myY=-190;
        float width=50;
        float hight=6;
        float circleX=10,circleY=130;
        float circleVelocityX=2; float circleVelocityY =2;
        float radius=5;
        srand(time(NULL));
        float randNumber=rand()%150;
       
        glClearDepth(1);
        glClearColor(1,1,1,0);
        glEnable(GL_DEPTH_TEST);
        glDepthMask(GL_TRUE);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(90, 1,1,500);
       
       
    // 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();
                       
            // Escape key : exit
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                App.Close();
                       
            // Resize event : adjust viewport
            if (Event.Type == sf::Event::Resized)
                glViewport(0, 0, Event.Size.Width, Event.Size.Height);
                        //moving left and right
                        if((Event.Type==sf::Event::KeyPressed)&&(Event.Key.Code==sf::Key::Left)) myX -=10 ;
                        if((Event.Type==sf::Event::KeyPressed)&&(Event.Key.Code==sf::Key::Right)) myX +=10 ;
                        if((Event.Type==sf::Event::KeyPressed)&&(Event.Key.Code==sf::Key::R)) {
                                circleX=randNumber; circleY=randNumber;
                                circleVelocityX=2;  
                                circleVelocityY =2;
                                radius=5;
                                circleX+=circleVelocityX;
                                circleY+=circleVelocityY;

                        }

        }
               
                //Behavier of the objects
                if(myX<-200) myX=-200;
                else if(myX>200-width)myX=200-width;
               
                circleX+=circleVelocityX;
                circleY+=circleVelocityY;
                if (circleX<-200) circleVelocityX=-circleVelocityX;
                else if(circleX>200)circleVelocityX=-circleVelocityX;
                if(circleY<-200) {
                        circleVelocityX=0;
                        circleVelocityY=0;
                        //Game over message goes here
                       
                }
                else if(circleY>200+radius)circleVelocityY=-circleVelocityY;
               
                if(checkCollision(myX, myY, width, hight, circleX, circleY, radius)){
                        //circleVelocityX=-circleVelocityX;
                        circleVelocityY=-circleVelocityY;
                }
               
        // Set the active window before using OpenGL commands
        // It's useless here because active window is always the same,
        // but don't forget it if you use multiple windows or controls
        App.SetActive();
               
        // Clear color buffer
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
               
        // Your drawing here...
                glMatrixMode(GL_MODELVIEW);
                glLoadIdentity();
                glTranslatef(0, 0, -200);
               
                glBegin(GL_QUADS);
                glColor3f(0,0,0);
                glVertex2f(myX,myY);
                glVertex2f(myX+width,myY);
                glVertex2f(myX+width,myY+hight);
                glVertex2f(myX,myY+hight);
                glEnd();
               
                Circle(radius,circleX,circleY);
                App.SetFramerateLimit(60);
        // Finally, display rendered frame on screen
        App.Display();
    }
       
    return EXIT_SUCCESS;
}
 

PS This is my first post and I am very new to openGL and sfml, i apologise in advance if this question has been answered before. and no laughing at my coding i know it is terrible :P

Thanks

Pages: [1]