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

Author Topic: changing number of pixels, with SFML/Window.hpp  (Read 2340 times)

0 Members and 1 Guest are viewing this topic.

rustam2735

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: changing number of pixels, with SFML/Window.hpp
« Reply #1 on: October 24, 2012, 10:40:48 am »
How do you check that you got only 400x400 pixels?
I'd say you use the viewport thingy wrong, but then again I've no idea about OpenGL...

I strongly advise you against the use of SFML 1.6, it's over 2 years old, contains many bugs and lacks a lot of features... ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: changing number of pixels, with SFML/Window.hpp
« Reply #2 on: October 24, 2012, 03:41:20 pm »
Quote
I'd say you use the viewport thingy wrong, but then again I've no idea about OpenGL...

The default viewport is the size of the window if I'm not mistaken. I think you only need a viewport if you want openGL to only use a part of the window or if you resize the window, but I'm new to OpenGL myself, so I can't assure anything.

Quote
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.

Try playing around with gluPerspective, it can give you some crazy results as well as very decent ones depending on what you want to do. I don't really know how you got to know that it only uses 400x400 pixels.

You could also just use glOrtho if you only want OpenGL for 2D rendering, though it is a bit weird on it's behavior, it allows you to do stuff you are normally unable to do (making the texture to be the one moving according to your vertexes). At least play around with all of that while you get an answer from Laurent or someone else that truly knows openGL.
« Last Edit: October 24, 2012, 03:49:30 pm by masskiller »
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: changing number of pixels, with SFML/Window.hpp
« Reply #3 on: October 24, 2012, 03:51:51 pm »
I won't answer until he explains what "I have noticed that my window has 400x400 pixels" means ;)

The description of the problem is not clear enough.
Laurent Gomila - SFML developer

rustam2735

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: changing number of pixels, with SFML/Window.hpp
« Reply #4 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.