From fe02b79946164d9149f2c88695b4454e8b34cf58 Mon Sep 17 00:00:00 2001
From: Sheldon Lee <sheldon@sheldonlee.com>
Date: Fri, 31 Mar 2023 01:08:02 +0100
Subject: [PATCH] Create basic level grid representation.

---
 level.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 level.h   | 10 +++++++
 main.cpp  |  2 --
 view.cpp  | 19 +++++++++----
 view.h    |  1 +
 5 files changed, 104 insertions(+), 7 deletions(-)
 create mode 100644 level.cpp
 create mode 100644 level.h

diff --git a/level.cpp b/level.cpp
new file mode 100644
index 0000000..beea548
--- /dev/null
+++ b/level.cpp
@@ -0,0 +1,79 @@
+#include "level.h"
+#include <stdio.h>
+
+#define WIDTH  5
+#define HEIGHT 5
+
+static sf::RenderWindow* window = nullptr;
+
+static void drawGrid();
+
+static unsigned int level[WIDTH * HEIGHT] = {
+	1, 1, 1, 1, 1,
+	0, 0, 0, 0, 0,
+	1, 0, 1, 0, 1,
+	0, 0, 0, 0, 0,
+	1, 0, 1, 0, 1,
+};
+
+
+int level_init(sf::RenderWindow* renderWindow)
+{
+	printf("level_init()\n");
+	window = renderWindow;
+	return 1;
+}
+
+int level_update()
+{
+	if (!window) return 0;
+	drawGrid();
+	return 1;
+}
+
+void level_end()
+{
+	printf("level_end()\n");
+	return;
+}
+
+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;
+
+	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);
+	}
+
+	for (unsigned int x = 0; x < WIDTH; x++) {
+		for (unsigned int y = 0; y < HEIGHT; y++) {
+			if (!level[y * HEIGHT + x]) continue;
+
+			sf::RectangleShape rectangle(sf::Vector2f(stepY - padding*2, stepY - padding*2));
+			rectangle.setPosition(x * stepX + padding, y * stepY + padding);
+
+			window->draw(rectangle);
+		}
+	}
+}
diff --git a/level.h b/level.h
new file mode 100644
index 0000000..cf6c38a
--- /dev/null
+++ b/level.h
@@ -0,0 +1,10 @@
+#ifndef LEVEL_H
+#define LEVEL_H
+
+#include <SFML/Graphics.hpp>
+
+int level_init(sf::RenderWindow* renderWindow);
+int level_update();
+void level_end();
+
+#endif
diff --git a/main.cpp b/main.cpp
index 69c2479..e97c3e3 100644
--- a/main.cpp
+++ b/main.cpp
@@ -4,8 +4,6 @@
 
 int main()
 {
-	printf("Hello meme!\n");
-
 	view_init();
 	while (view_update());
 
diff --git a/view.cpp b/view.cpp
index b477482..ff7a737 100644
--- a/view.cpp
+++ b/view.cpp
@@ -1,13 +1,16 @@
-#include <SFML/Graphics.hpp>
 #include "view.h"
+#include <SFML/Graphics.hpp>
+#include <stdio.h>
+
+#include "level.h"
 
 static sf::Uint32 style = sf::Style::Titlebar;
-static sf::RenderWindow window(sf::VideoMode(1600, 900), "SFML works!", style);
-static sf::CircleShape shape(450.f);
+static sf::RenderWindow window(sf::VideoMode(500, 500), "Raycasting", style);
 
 int view_init()
 {
-	shape.setFillColor(sf::Color::Green);
+	printf("view_init()\n");
+	level_init(&window);
 	return 1;
 }
 
@@ -24,9 +27,15 @@ int view_update()
 	}
 
 	window.clear();
-	window.draw(shape);
+	if (!level_update()) return 0;
 	window.display();
 
 	return 1;
 }
 
+void view_end()
+{
+	printf("view_end()\n");
+	level_end();
+	return;
+}
diff --git a/view.h b/view.h
index 2e3c2f1..5772eb9 100644
--- a/view.h
+++ b/view.h
@@ -3,5 +3,6 @@
 
 int view_init();
 int view_update();
+void view_end();
 
 #endif