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
Thanks