started implementing the tilesystem, rendering is broken, all chunks are at 0,0

This commit is contained in:
nosch
2025-09-02 01:02:54 +02:00
parent 05e538117c
commit 904983d633
5 changed files with 95 additions and 31 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 296 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 682 B

+26 -13
View File
@@ -1,6 +1,7 @@
#include "../include/raylib-cpp.hpp"
#include "entities/entitybase.hpp"
#include "render/textures.hpp"
#include "tiles/tilebase.hpp"
void PlayerController(Entity &e, float deltatime) {
raylib::Vector2 acceleration = {0,0};
@@ -25,7 +26,10 @@ int main() {
SetTargetFPS(60);
Texture Texturemissing = GenDefaultTexture();
GenDefaultTexture();
Map gamemap;
gamemap.Init();
raylib::Rectangle playerrect{screenWidth / 2.0f, screenHeight / 2.0f,64,64};
Entity player{1, playerrect, "MainPlayer", 100, 100};
@@ -34,7 +38,7 @@ int main() {
entityTextures.LoadImage("./src/assets/playersprite.png");
tileTextures.LoadImage("./src/assets/BasicTile.png");
raylib::Texture placeholder("./src/assets/Dirt_01.png");
raylib::Texture placeholder("./src/assets/rock1.png");
Camera2D camera = { 0 };
camera.offset = { screenWidth/2.0f, screenHeight/2.0f };
@@ -67,18 +71,27 @@ int main() {
int offsetX = ((int)camera.target.x % bg.width);
int offsetY = ((int)camera.target.y % bg.height);
for (int i = -1; i <= GetScreenWidth() / bw + 1; i++) {
for (int j = -1; j <= GetScreenHeight() / bh + 1; j++) {
DrawTexture(
bg,
camera.target.x - offsetX - screenWidth / 2.0f + i * bw,
camera.target.y - offsetY - screenHeight / 2.0f + j * bh,
WHITE
);
}
}
//for (int i = -1; i <= GetScreenWidth() / bw + 1; i++) {
// for (int j = -1; j <= GetScreenHeight() / bh + 1; j++) {
// DrawTexture(
// bg,
// camera.target.x - offsetX - screenWidth / 2.0f + i * bw,
// camera.target.y - offsetY - screenHeight / 2.0f + j * bh,
// WHITE
// );
// }
//}
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) placeholder.Draw(GetMousePosition().x, GetMousePosition().y, WHITE);
//if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) placeholder.Draw(GetMousePosition().x, GetMousePosition().y, WHITE);
raylib::Rectangle fov{
camera.target.x - screenWidth / 2 * camera.zoom,
camera.target.y - screenHeight / 2 * camera.zoom,
screenWidth * camera.zoom,
screenHeight * camera.zoom,
};
gamemap.Draw(fov);
player.Draw();
+46 -13
View File
@@ -1,5 +1,6 @@
#include "tilebase.hpp"
#include "iostream"
// ----------------- Map -----------------
Map::Map()
@@ -17,12 +18,23 @@ Layer::Layer()
: id(0), DEBUG(false), owner(nullptr) {}
// ----------------- Chunk -----------------
Chunk::Chunk(Layer* Owner)
: position(0, 0), owner(Owner) {
Chunk::Chunk(Vector2 pos, Layer* Owner)
: position(pos), owner(Owner) {
for (int i = 0; i < 64; i++) {
tiles[i].owner = this;
tiles[i].id = i;
tiles[i].spriteid = 0;
}
}
Chunk::Chunk()
: position(0, 0), owner(nullptr) {}
: position(0, 0), owner(nullptr) {
for (int i = 0; i < 64; i++) {
tiles[i].owner = this;
tiles[i].id = i;
tiles[i].spriteid = 0;
}
}
// ----------------- Tile -----------------
Tile::Tile(ID Id, ID Spriteid, Chunk* Owner)
@@ -33,37 +45,58 @@ Tile::Tile()
// ----------------- Vector2Hash / Eq -----------------
std::size_t Vector2Hash::operator()(const Vector2& v) const noexcept {
std::size_t h1 = std::hash<float>{}(v.x);
std::size_t h2 = std::hash<float>{}(v.y);
std::size_t h1 = std::hash<int>{}((int)v.x);
std::size_t h2 = std::hash<int>{}((int)v.y);
return h1 ^ (h2 << 1);
}
bool Vector2Eq::operator()(const Vector2& a, const Vector2& b) const noexcept {
return (a.x == b.x && a.y == b.y);
return ((int)a.x == (int)b.x && (int)a.y == (int)b.y);
}
void Tile::Draw() {
int posx = owner->position.x * 64 * 8 + id % 8;
int posy = owner->position.y * 64 * 8 + id / 8;
int posx = owner->position.x * 64 * 8 + id % 8 * 64;
int posy = owner->position.y * 64 * 8 + id / 8 * 64;
std::cout << "Tile at "<< posx << ", " << posy << "\n";
std::cout << "Owner at "<< owner->position.x << ", " << owner->position.y << "\n";
DrawTextureEx(tileTextures.GetTexture(spriteid), Vector2({(float)posx,(float)posy}), 0, 1, WHITE);
}
void Chunk::Draw() {
void Chunk::Draw(raylib::Rectangle aera) {
for (int i = 0; i < 64; i++) {
int x = i % 8;
int y = i / 8;
raylib::Rectangle box{x * 64 + position.x * 8 * 64, y * 64 + position.y * 8 * 64, 64, 64};
//if (box.CheckCollision(aera)) {
tiles[i].Draw();
//}
}
}
void Layer::Draw() {
void Layer::Draw(raylib::Rectangle aera) {
for (auto it = chunks.begin(); it != chunks.end(); ++it) {
it->second.Draw();
raylib::Rectangle box{it->first.x * 64 * 8, it->first.y * 64 * 8, 64 * 8, 64 * 8};
//if (box.CheckCollision(aera)) {
it->second.Draw(aera);
//}
}
}
void Map::Draw() {
void Map::Draw(raylib::Rectangle aera) {
for (int i = 0; i < 4; i++) {
layers[i].Draw();
layers[i].Draw(aera);
}
}
void Map::Init() {
for (int x = -1; x < 2; x++) {
for (int y = -1; y < 2; y++) {
layers[0].chunks[Vector2{(float)x,(float)y}] = Chunk(Vector2{ (float)x, (float)y }, &layers[0]);
}
}
}
+22 -4
View File
@@ -5,6 +5,7 @@
#include <unordered_map>
#include <vector>
#include <array>
#include "iostream"
// Forward declarations
struct Chunk;
@@ -29,9 +30,9 @@ struct Chunk {
Tile tiles[64]; // dynamic instead of fixed array
Layer* owner;
void Draw();
void Draw(raylib::Rectangle aera);
Chunk(Layer* Owner);
Chunk(Vector2 pos, Layer* Owner);
Chunk(); // default constructor
};
@@ -49,19 +50,36 @@ struct Layer {
bool DEBUG;
Map* owner;
void Draw();
void Draw(raylib::Rectangle aera);
Layer(ID Id, Map* Owner);
Layer(); // default constructor
};
enum Actions{
place,
destroy,
rotate,
open,
close,
drop,
};
struct packet {
Actions action;
Vector2 position;
int data;
};
struct Map {
std::array<Layer, 4> layers; // ground/water/void/decoration
int worldtime;
bool DEBUG;
Map();
void Draw();
void Draw(raylib::Rectangle aera);
void Init();
void Update(float deltatime, packet datapacket);
};
#endif // !TILEBASE_HPP