diff --git a/level.cpp b/level.cpp index beea548..d85aa07 100644 --- a/level.cpp +++ b/level.cpp @@ -4,9 +4,11 @@ #define WIDTH 5 #define HEIGHT 5 -static sf::RenderWindow* window = nullptr; - static void drawGrid(); +static void drawGridLine(unsigned int step, bool isHorizontal); +static sf::Vertex getGridLineVertex(unsigned int n, unsigned int maxDimension, bool isStart, bool isHorizontal); + +static sf::RenderWindow* window = nullptr; static unsigned int level[WIDTH * HEIGHT] = { 1, 1, 1, 1, 1, @@ -16,7 +18,6 @@ static unsigned int level[WIDTH * HEIGHT] = { 1, 0, 1, 0, 1, }; - int level_init(sf::RenderWindow* renderWindow) { printf("level_init()\n"); @@ -39,32 +40,13 @@ void level_end() static void drawGrid() { - const unsigned int padding = 5; const sf::Vector2u windowSize = window->getSize(); - const unsigned int stepX = windowSize.x/WIDTH; - const unsigned int stepY = windowSize.y/HEIGHT; + unsigned int padding = 5; + unsigned int stepX = windowSize.x/WIDTH; + unsigned int stepY = windowSize.y/HEIGHT; - for (unsigned int x = 0; x < WIDTH; x++) { - if (x == 0) continue; - sf::Vertex line[] = - { - sf::Vertex(sf::Vector2f(x * stepX, 0)), - sf::Vertex(sf::Vector2f(x * stepX, windowSize.x)) - }; - - window->draw(line, 2, sf::Lines); - } - - for (unsigned int y = 0; y < HEIGHT; y++) { - if (y == 0) continue; - sf::Vertex line[] = - { - sf::Vertex(sf::Vector2f(0, y * stepY)), - sf::Vertex(sf::Vector2f(windowSize.y, y * stepY)) - }; - - window->draw(line, 2, sf::Lines); - } + drawGridLine(stepX, true); + drawGridLine(stepY, false); for (unsigned int x = 0; x < WIDTH; x++) { for (unsigned int y = 0; y < HEIGHT; y++) { @@ -77,3 +59,38 @@ static void drawGrid() } } } + +static void drawGridLine(unsigned int step, bool isHorizontal) +{ + unsigned int lines = isHorizontal? WIDTH : HEIGHT; + + for (unsigned int n = 0; n < lines; n++) { + if (n == 0) continue; + unsigned int offset = n * step; + unsigned int maxDimension = lines * step; + sf::Vertex line[] = + { + getGridLineVertex(offset, maxDimension, true, isHorizontal), + getGridLineVertex(offset, maxDimension, false, isHorizontal) + }; + + window->draw(line, 2, sf::Lines); + } +} + +static sf::Vertex getGridLineVertex(unsigned int offset, unsigned int maxDimension, bool isStart, bool isHorizontal) +{ + sf::Vertex start; + sf::Vertex end; + + if (isHorizontal) { + start = sf::Vertex(sf::Vector2f(offset, 0)); + end = sf::Vertex(sf::Vector2f(offset, maxDimension)); + } + else { + start = sf::Vertex(sf::Vector2f(0, offset)); + end = sf::Vertex(sf::Vector2f(maxDimension, offset)); + } + + return isStart? start : end; +} diff --git a/main.cpp b/main.cpp index e97c3e3..7ecdd48 100644 --- a/main.cpp +++ b/main.cpp @@ -6,6 +6,7 @@ int main() { view_init(); while (view_update()); + view_end(); return 0; } diff --git a/view.cpp b/view.cpp index ff7a737..73612e0 100644 --- a/view.cpp +++ b/view.cpp @@ -4,6 +4,8 @@ #include "level.h" +static int handleKeyCode(sf::Keyboard::Key key); + static sf::Uint32 style = sf::Style::Titlebar; static sf::RenderWindow window(sf::VideoMode(500, 500), "Raycasting", style); @@ -20,9 +22,14 @@ int view_update() sf::Event event; while (window.pollEvent(event)) { - if (event.type == sf::Event::Closed) { - window.close(); - return 0; + switch (event.type) { + case sf::Event::Closed: + return 0; + case sf::Event::KeyPressed: + if (handleKeyCode(event.key.code)) continue; + return 0; + default: + continue; } } @@ -37,5 +44,17 @@ void view_end() { printf("view_end()\n"); level_end(); - return; + + window.close(); +} + +static int handleKeyCode(sf::Keyboard::Key key) +{ + switch (key) { + case sf::Keyboard::Escape: + case sf::Keyboard::Q: + return 0; + default: + return 1; + } }