Some refactoring

This commit is contained in:
Sheldon Lee 2023-03-31 22:56:26 +01:00
parent fe02b79946
commit dd703c3221
3 changed files with 68 additions and 31 deletions

View File

@ -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;
}

View File

@ -6,6 +6,7 @@ int main()
{
view_init();
while (view_update());
view_end();
return 0;
}

View File

@ -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();
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;
}
}