1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
#include <SFML/Graphics.hpp>
#include <iostream>
#include <ios>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include "main.h"
using namespace std;
//Hardcoded in.
bool running = true, runningSecondary = true;
const int XSize = 800, YSize = 600;
double countX = 0;
double countY = 0;
using sf::Thread;
//Draws the ball and returns the circle object back.
sf::CircleShape drawCircle(sf::RenderWindow* window2, sf::CircleShape circle = sf::CircleShape(100.f))
{
cout << "Render Window: " << window2->getPosition().x << endl;
circle.setFillColor(sf::Color::Green);
window2->clear();
window2->draw(circle);
window2->display();
return circle;
}
//Changes the position of the ball.
void changePos(bool reverse, char choice)
{
if(reverse)
{
switch (choice)
{
case 'b':
countX -= .05;
countY -= .05;
break;
case 'x':
countX -= .05;
break;
case 'y':
countY -= .05;
break;
}
}
else
{
switch (choice)
{
case 'b':
countX += .05;
countY += .05;
break;
case 'x':
countX += .05;
break;
case 'y':
countY += .05;
break;
}
}
}
//Animates the moving of the ball.
void animate(animateThread& at)
{
sf::CircleShape circle = at.circle;
sf::RenderWindow* window2 = at.renderWindow; //A value of type int cannot be used to initialize an entity of type sf::renderwindow
int circleDiameter = circle.getRadius() * 2;
while(runningSecondary)
{
if(countX >= 799 - circleDiameter)
{
cout << circle.getPosition().x << " XXXXXX" << endl;
changePos(true, 'x');
}
else if(countY >= 599 - circleDiameter)
{
cout << circle.getPosition().y << " YYYYYYYYY" << endl;
changePos(true, 'y');
}
else if((countX >= 799 - circleDiameter) && (countY >= 599 - circleDiameter))
{
changePos(true, 'b');
}
else{changePos(false, 'b'); }
circle.setPosition(countX,countY);
drawCircle(window2, circle);
}
}
int main()
{
sf::Window window(sf::VideoMode(XSize, YSize), "OpenGL", sf::Style::Default, sf::ContextSettings(32)); //OpenGl Window
sf::RenderWindow* window2 = &sf::RenderWindow(sf::VideoMode(XSize,YSize), "Graphics"); //SFML Graphics Window
window.setVerticalSyncEnabled(true);
glDisable(GL_DEPTH_TEST);
sf::CircleShape circle = drawCircle(window2); //Draw the circle in the origin.
//Main event loops.
while (running)
{
// handle events
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
// end the program
running = false;
}
else if (event.type == sf::Event::Resized)
{
// adjust the viewport when the window is resized
glViewport(0, 0, event.size.width, event.size.height);
}
}
sf::Event Event_Window2;
while(window2->pollEvent(Event_Window2))
{
if(Event_Window2.type == sf::Event::Closed)
{
running = false;
}
else if(Event_Window2.type == sf::Event::KeyPressed)
{
if(Event_Window2.key.code == sf::Keyboard::P)
{
runningSecondary = false;
}
}
else if (Event_Window2.type == sf::Event::Resized)
{
// adjust the viewport when the window is resized
glViewport(0, 0, Event_Window2.size.width, Event_Window2.size.height);
}
}
//Creates a struct containing the initial circle object and the window.
animateThread at;
at.circle = circle;
at.renderWindow = window2; //No suitable conversion function from sf::RenderWindow to int exists
//Threads the animation so it doesn't interfere with the Event catching.
//animate is the name of the method, at is the struct object.
Thread animationThread(&animate, at);
animationThread.launch();
//OpenGL Implementation Attempt.
// clear the buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// draw...
glBegin(GL_TRIANGLES);
glVertex3f(100.0f, 100.0f, 0.0f);
glVertex3f(150.0f, 100.0f, 0.0f);
glVertex3f(125.0f, 50.0f, 0.0f);
glEnd();
// end the current frame (internally swaps the front and back buffers)
window.display();
}
// release resources...
return 0;
}
|