Create basic level grid representation.
This commit is contained in:
parent
b9470696f6
commit
fe02b79946
79
level.cpp
Normal file
79
level.cpp
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
10
level.h
Normal file
10
level.h
Normal file
@ -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
|
2
main.cpp
2
main.cpp
@ -4,8 +4,6 @@
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("Hello meme!\n");
|
||||
|
||||
view_init();
|
||||
while (view_update());
|
||||
|
||||
|
19
view.cpp
19
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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user