Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include "Player.h"
- #include <time.h>
- #include <math.h>
- #include <iostream>
- #include <array>
- #include <algorithm>
- void poll_player_controller(std::array<Player, 2> arr)
- {
- unsigned int counter = 0;
- for(Player &p : arr)
- {
- if (sf::Joystick::isConnected(counter))
- {
- // joystick number 0 is connected
- float axis_x = sf::Joystick::getAxisPosition(0, sf::Joystick::X) / 100.0;
- float axis_y = sf::Joystick::getAxisPosition(0, sf::Joystick::Y) / 100.0;
- axis_x += sf::Joystick::getAxisPosition(0, sf::Joystick::PovX) / 100.0;
- axis_y += sf::Joystick::getAxisPosition(0, sf::Joystick::PovY) / 100.0;
- axis_x += sf::Joystick::isButtonPressed(counter, 19) - sf::Joystick::isButtonPressed(counter, 18);
- axis_y += sf::Joystick::isButtonPressed(counter, 17) - sf::Joystick::isButtonPressed(counter, 16);
- std::cout << axis_x << std::endl;
- std::cout << axis_y << std::endl;
- p.direction = atan2(axis_y, axis_x);
- axis_x + axis_y != 0.0 ? p.speed = 0.2 : p.speed = 0.0;
- }
- counter++;
- }
- }
- void update_player(Player &p, sf::RenderWindow &w)
- {
- // Trig functions to move at angle and speed
- p.x += cos(p.direction) * p.speed;
- p.y += sin(p.direction) * p.speed;
- unsigned int window_width = w.getSize().x;
- unsigned int window_height = w.getSize().y;
- // Out of bounds wrapping
- if(p.x >= window_width){p.x -= window_width;}
- if(p.y >= window_height){p.y -= window_height;}
- if(p.x < 0){p.x += window_width;}
- if(p.y < 0){p.y += window_height;}
- // Update the circle child object
- p.circle.setRadius(p.radius);
- p.circle.setPosition(p.x - p.radius, p.y - p.radius);
- }
- void draw_player(Player &p, sf::RenderWindow &window)
- {
- window.draw(p.circle);
- }
- int main()
- {
- // Create the main window
- sf::RenderWindow main_window(sf::VideoMode(800, 600),
- "KingOfCircles98DoubleSpecialEdition");
- srand(time(NULL));
- std::array<Player, 2> game_players;
- for (unsigned char i = 0; i < 2; i++)
- {
- char r = rand() % 256;
- char g = rand() % 256;
- char b = rand() % 256;
- unsigned int rand_x = rand() % main_window.getSize().x;
- unsigned int rand_y = rand() % main_window.getSize().y;
- float temp_direction = rand() * 6.28;
- float temp_speed = 0.0; //.1;
- game_players[i] = Player(i, rand_x, rand_y, 64, temp_speed,
- temp_direction);
- sf::Color temp_color(r, g, b, 200);
- game_players[i].circle.setFillColor(temp_color);
- }
- // Start the game loop
- while (main_window.isOpen())
- {
- // Process events
- sf::Event event;
- while (main_window.pollEvent(event))
- {
- // Close window : exit
- if (event.type == sf::Event::Closed)
- main_window.close();
- }
- // Clear screen
- main_window.clear();
- poll_player_controller(game_players);
- for(Player &i : game_players)
- {
- update_player(i, main_window);
- draw_player(i, main_window);
- }
- // Update the window
- main_window.display();
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement