From e993b3042ded5e23beb207e20c099c8f4fb3564b Mon Sep 17 00:00:00 2001 From: HaeoL Date: Fri, 27 Feb 2015 15:53:14 -0800 Subject: [PATCH 1/2] insert summary here MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit needs a bit of cleaning up but I’m lazy --- BattleTurf/BattleTurf.cpp | 50 ++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/BattleTurf/BattleTurf.cpp b/BattleTurf/BattleTurf.cpp index b5cc814..3d0cd83 100644 --- a/BattleTurf/BattleTurf.cpp +++ b/BattleTurf/BattleTurf.cpp @@ -1,22 +1,56 @@ +#include #include int main() { - sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!"); - sf::CircleShape shape(100.f); - shape.setFillColor(sf::Color::Green); + const int width = 800; + const int height = 800; + sf::RenderWindow window(sf::VideoMode(width, height), "Prototype"); + sf::Vector2i pos; + sf::RectangleShape* past; - while (window.isOpen()) - { + const int board_width = 15; + const int board_height = 15; + float box_width = width/board_width; + float box_height = height/board_height; + + sf::RectangleShape boxes[board_width][board_height]; + + for (int i = 0; i < board_height; i++) { + for (int j = 0; j < board_width; j++) { + boxes[i][j].setSize(sf::Vector2f(box_width, box_height)); + boxes[i][j].setPosition(j*(box_width), i*(box_height)); + boxes[i][j].setOutlineColor(sf::Color::Black); + boxes[i][j].setOutlineThickness(2); + } + } + + while (window.isOpen()) { sf::Event event; - while (window.pollEvent(event)) - { + while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); + if (event.type == sf::Event::MouseMoved) { + if (past) past->setFillColor(sf::Color::White); + pos = sf::Mouse::getPosition(window); + boxes[(int)(pos.y/box_height)][(int)(pos.x/box_width)].setFillColor(sf::Color::Cyan); + past = &boxes[(int)(pos.y/box_height)][(int)(pos.x/box_width)]; + //std::cout << "x:" << pos.x << "y: " << pos.y << std::endl; //DEBUG + //std::cout << "x:" << (int)(pos.x/box_width) << "y: " << (int)(pos.y/box_height) << std::endl; //DEBUG + } + + if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) { + //text.setPosition(55 * (currentbox / 10), 55 * ( currentbox % 10)); + } } window.clear(); - window.draw(shape); + for (int i = 0; i < board_height; i++) { + for (int j = 0; j < board_width; j++) { + window.draw(boxes[i][j]); + } + } + window.display(); } From 0af444148464067fd221a926d25fe1496baae971 Mon Sep 17 00:00:00 2001 From: HaeoL Date: Fri, 6 Mar 2015 13:23:03 -0800 Subject: [PATCH 2/2] time included less cpu usage --- BattleTurf/BattleTurf.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/BattleTurf/BattleTurf.cpp b/BattleTurf/BattleTurf.cpp index 3d0cd83..f5595de 100644 --- a/BattleTurf/BattleTurf.cpp +++ b/BattleTurf/BattleTurf.cpp @@ -1,8 +1,10 @@ #include +#include #include -int main() -{ +void sleep(float seconds); + +int main() { const int width = 800; const int height = 800; sf::RenderWindow window(sf::VideoMode(width, height), "Prototype"); @@ -31,10 +33,10 @@ int main() if (event.type == sf::Event::Closed) window.close(); if (event.type == sf::Event::MouseMoved) { - if (past) past->setFillColor(sf::Color::White); + if (past) past->setFillColor(sf::Color::White); //past starts out undefined, therefore false pos = sf::Mouse::getPosition(window); boxes[(int)(pos.y/box_height)][(int)(pos.x/box_width)].setFillColor(sf::Color::Cyan); - past = &boxes[(int)(pos.y/box_height)][(int)(pos.x/box_width)]; + past = &boxes[(int)(pos.y/box_height)][(int)(pos.x/box_width)]; //past declared, therefore true //std::cout << "x:" << pos.x << "y: " << pos.y << std::endl; //DEBUG //std::cout << "x:" << (int)(pos.x/box_width) << "y: " << (int)(pos.y/box_height) << std::endl; //DEBUG } @@ -52,7 +54,14 @@ int main() } window.display(); + sleep(1); } return 0; } + +void sleep(float seconds) { + clock_t temp; + temp = clock () + seconds * CLOCKS_PER_SEC ; + while (clock() < temp) {} +}