Some refactoring
This commit is contained in:
parent
fe02b79946
commit
dd703c3221
71
level.cpp
71
level.cpp
@ -4,9 +4,11 @@
|
|||||||
#define WIDTH 5
|
#define WIDTH 5
|
||||||
#define HEIGHT 5
|
#define HEIGHT 5
|
||||||
|
|
||||||
static sf::RenderWindow* window = nullptr;
|
|
||||||
|
|
||||||
static void drawGrid();
|
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] = {
|
static unsigned int level[WIDTH * HEIGHT] = {
|
||||||
1, 1, 1, 1, 1,
|
1, 1, 1, 1, 1,
|
||||||
@ -16,7 +18,6 @@ static unsigned int level[WIDTH * HEIGHT] = {
|
|||||||
1, 0, 1, 0, 1,
|
1, 0, 1, 0, 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
int level_init(sf::RenderWindow* renderWindow)
|
int level_init(sf::RenderWindow* renderWindow)
|
||||||
{
|
{
|
||||||
printf("level_init()\n");
|
printf("level_init()\n");
|
||||||
@ -39,32 +40,13 @@ void level_end()
|
|||||||
|
|
||||||
static void drawGrid()
|
static void drawGrid()
|
||||||
{
|
{
|
||||||
const unsigned int padding = 5;
|
|
||||||
const sf::Vector2u windowSize = window->getSize();
|
const sf::Vector2u windowSize = window->getSize();
|
||||||
const unsigned int stepX = windowSize.x/WIDTH;
|
unsigned int padding = 5;
|
||||||
const unsigned int stepY = windowSize.y/HEIGHT;
|
unsigned int stepX = windowSize.x/WIDTH;
|
||||||
|
unsigned int stepY = windowSize.y/HEIGHT;
|
||||||
|
|
||||||
for (unsigned int x = 0; x < WIDTH; x++) {
|
drawGridLine(stepX, true);
|
||||||
if (x == 0) continue;
|
drawGridLine(stepY, false);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (unsigned int x = 0; x < WIDTH; x++) {
|
for (unsigned int x = 0; x < WIDTH; x++) {
|
||||||
for (unsigned int y = 0; y < HEIGHT; y++) {
|
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;
|
||||||
|
}
|
||||||
|
1
main.cpp
1
main.cpp
@ -6,6 +6,7 @@ int main()
|
|||||||
{
|
{
|
||||||
view_init();
|
view_init();
|
||||||
while (view_update());
|
while (view_update());
|
||||||
|
view_end();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
27
view.cpp
27
view.cpp
@ -4,6 +4,8 @@
|
|||||||
|
|
||||||
#include "level.h"
|
#include "level.h"
|
||||||
|
|
||||||
|
static int handleKeyCode(sf::Keyboard::Key key);
|
||||||
|
|
||||||
static sf::Uint32 style = sf::Style::Titlebar;
|
static sf::Uint32 style = sf::Style::Titlebar;
|
||||||
static sf::RenderWindow window(sf::VideoMode(500, 500), "Raycasting", style);
|
static sf::RenderWindow window(sf::VideoMode(500, 500), "Raycasting", style);
|
||||||
|
|
||||||
@ -20,9 +22,14 @@ int view_update()
|
|||||||
|
|
||||||
sf::Event event;
|
sf::Event event;
|
||||||
while (window.pollEvent(event)) {
|
while (window.pollEvent(event)) {
|
||||||
if (event.type == sf::Event::Closed) {
|
switch (event.type) {
|
||||||
window.close();
|
case sf::Event::Closed:
|
||||||
return 0;
|
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");
|
printf("view_end()\n");
|
||||||
level_end();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user