Fixing loading problem
+ ImHex Pattern Code
This commit is contained in:
parent
72d1bf3bfb
commit
37a249dc62
9 changed files with 179 additions and 41 deletions
|
@ -55,9 +55,7 @@ target_link_libraries(colysis
|
||||||
|
|
||||||
target_compile_definitions(colysis PUBLIC "$<$<CONFIG:DEBUG>:_DEBUG>")
|
target_compile_definitions(colysis PUBLIC "$<$<CONFIG:DEBUG>:_DEBUG>")
|
||||||
|
|
||||||
target_include_directories(colysis PUBLIC include
|
target_include_directories(colysis PUBLIC include )
|
||||||
#${ALYSON_INCLUDE_DIR}
|
|
||||||
)
|
|
||||||
|
|
||||||
# put the assets folder path as a C preprocessor define
|
# put the assets folder path as a C preprocessor define
|
||||||
set(ASSETS_PATH "${CMAKE_CURRENT_SOURCE_DIR}/assets/")
|
set(ASSETS_PATH "${CMAKE_CURRENT_SOURCE_DIR}/assets/")
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
#include "Enemy.hpp"
|
#include "Enemy.hpp"
|
||||||
|
|
||||||
|
#include <ctgmath>
|
||||||
|
|
||||||
Enemy::Enemy(sf::Vector2f position) : rectangle(sf::Vector2f{100, 100})
|
Enemy::Enemy(sf::Vector2f position) : rectangle(sf::Vector2f{100, 100})
|
||||||
{
|
{
|
||||||
rectangle.move(position);
|
rectangle.move(position);
|
||||||
|
|
|
@ -9,12 +9,19 @@ Game::Game(sf::RenderWindow& window) : window(window)
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
void Game::run()
|
void Game::run()
|
||||||
{
|
{
|
||||||
|
#ifdef _DEBUG
|
||||||
std::cout << "Loading map... Version: " MAP_VERSION_STRING << std::endl;
|
std::cout << "Loading map... Version: " MAP_VERSION_STRING << std::endl;
|
||||||
auto start = std::chrono::system_clock::now();
|
auto start = std::chrono::system_clock::now();
|
||||||
|
#endif
|
||||||
|
|
||||||
auto err = map.load(ASSETS_PATH "/test.cymf");
|
auto err = map.load(ASSETS_PATH "/test.cymf");
|
||||||
|
|
||||||
|
#ifdef _DEBUG
|
||||||
auto end = std::chrono::system_clock::now();
|
auto end = std::chrono::system_clock::now();
|
||||||
auto milliseconds = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
|
auto milliseconds = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
|
||||||
std::cout << "Loading took: " << milliseconds << std::endl;
|
std::cout << "Loading took: " << milliseconds << std::endl;
|
||||||
|
#endif
|
||||||
|
|
||||||
if (err.code != 0)
|
if (err.code != 0)
|
||||||
{
|
{
|
||||||
std::cout << err.message << std::endl;
|
std::cout << err.message << std::endl;
|
||||||
|
|
|
@ -2,7 +2,9 @@
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
Map::Map() {}
|
bool operator==(uint32_t lhs, TileType rhs) {
|
||||||
|
return lhs == static_cast<uint32_t>(rhs);
|
||||||
|
}
|
||||||
|
|
||||||
bool operator&(uint8_t lhs, MapFlags rhs)
|
bool operator&(uint8_t lhs, MapFlags rhs)
|
||||||
{
|
{
|
||||||
|
@ -19,17 +21,47 @@ auto operator<=>(int lhs, TileType rhs)
|
||||||
return lhs <=> static_cast<int>(rhs);
|
return lhs <=> static_cast<int>(rhs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MapError Map::populate_vertex_array() {
|
||||||
|
if (!tiles) return {"Map: [Loading] Tilemap is not loaded", 2};
|
||||||
|
|
||||||
|
vertices.setPrimitiveType(sf::PrimitiveType::Triangles);
|
||||||
|
vertices.resize(map_size.x * map_size.y);
|
||||||
|
|
||||||
|
for (uint32_t x = 0; x < map_size.x; x++) {
|
||||||
|
for (uint32_t y = 0; y < map_size.y; ++y)
|
||||||
|
{
|
||||||
|
uint32_t tile = tiles[x + y * map_size.y];
|
||||||
|
/*
|
||||||
|
* -----
|
||||||
|
* --#++
|
||||||
|
* +++++
|
||||||
|
*
|
||||||
|
* + = other
|
||||||
|
* - = wall
|
||||||
|
* # = special
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {"Map: [Loading] Success", 0};
|
||||||
|
}
|
||||||
|
|
||||||
|
Map::Map() {}
|
||||||
|
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
MapError Map::load(const std::filesystem::path& path)
|
MapError Map::load(const std::filesystem::path& path)
|
||||||
{
|
{
|
||||||
// opening the file and checking the magic number
|
// opening the file and checking the magic number
|
||||||
std::ifstream file(path, std::ios::binary | std::ios::in);
|
std::ifstream file(path, std::ios::binary | std::ios::in);
|
||||||
char magic[4];
|
uint32_t magic;
|
||||||
file.read(magic, 4);
|
file.read(reinterpret_cast<char*>(&magic), 4);
|
||||||
|
|
||||||
if (std::memcmp(magic, "CYMF", 4) != 0) {
|
if (std::memcmp(&magic, "CYMF", 4) != 0) {
|
||||||
return {"Map: [Loading] Invalid magic number", 1};
|
return {"Map: [Loading] Invalid magic number: " + std::string(reinterpret_cast<const char *>(&magic), 4) + " (expected CYMF)", 1};
|
||||||
}
|
}
|
||||||
|
|
||||||
// reading the version
|
// reading the version
|
||||||
|
@ -53,7 +85,7 @@ MapError Map::load(const std::filesystem::path& path)
|
||||||
char* filepath_buffer = new char[filepath_buffer_size];
|
char* filepath_buffer = new char[filepath_buffer_size];
|
||||||
file.read(filepath_buffer, filepath_buffer_size);
|
file.read(filepath_buffer, filepath_buffer_size);
|
||||||
|
|
||||||
tileAtlas.load(std::filesystem::path(filepath_buffer));
|
|
||||||
delete[] filepath_buffer;
|
delete[] filepath_buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,19 +161,22 @@ sf::Vector2u Map::getSize() const
|
||||||
return {static_cast<uint32_t>(map_size.y), static_cast<uint32_t>(map_size.z)};
|
return {static_cast<uint32_t>(map_size.y), static_cast<uint32_t>(map_size.z)};
|
||||||
}
|
}
|
||||||
|
|
||||||
void Map::draw(sf::RenderTarget& target, const sf::RenderStates states) const
|
void Map::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
||||||
{
|
{
|
||||||
if (!tiles) return;
|
if (!tiles) return;
|
||||||
for (uint32_t x = 0; x < map_size.y; x++) {
|
|
||||||
for (uint32_t y = 0; y < map_size.z; ++y)
|
// apply the transform
|
||||||
{
|
states.transform *= getTransform();
|
||||||
target.draw(tileAtlas, states);
|
|
||||||
}
|
// apply the tileset texture
|
||||||
}
|
states.texture = &texture;
|
||||||
|
|
||||||
|
// draw the vertex array
|
||||||
|
target.draw(vertices, states);
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
|
#ifdef _DEBUG
|
||||||
void Map::debug()
|
void Map::debug()
|
||||||
{
|
{
|
||||||
std::cout << "Map Debug" << std::endl;
|
std::cout << "Map Debug" << std::endl;
|
||||||
|
@ -160,3 +195,4 @@ void Map::debug()
|
||||||
}
|
}
|
||||||
std::cout << "\t\tPlayer Spawn : " << player_spawn.x << ", " << player_spawn.y << std::endl;
|
std::cout << "\t\tPlayer Spawn : " << player_spawn.x << ", " << player_spawn.y << std::endl;
|
||||||
}
|
}
|
||||||
|
#endif
|
|
@ -66,18 +66,22 @@ enum class TileType : int
|
||||||
|
|
||||||
struct MapError
|
struct MapError
|
||||||
{
|
{
|
||||||
const char* message;
|
std::string message;
|
||||||
int code;
|
int code;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#include "TileAtlas.hpp"
|
#include "TileAtlas.hpp"
|
||||||
|
|
||||||
class Map : public sf::Drawable
|
class Map : public sf::Drawable, public sf::Transformable
|
||||||
{
|
{
|
||||||
static_assert(sizeof(MapSpecial)%4 == 0);
|
static_assert(sizeof(MapSpecial)%4 == 0);
|
||||||
|
|
||||||
TileAtlas tileAtlas = TileAtlas::defaultAtlas();
|
sf::Vector2u tile_size;
|
||||||
|
sf::Vector2u tile_count;
|
||||||
|
sf::Texture texture;
|
||||||
|
sf::VertexArray vertices;
|
||||||
|
|
||||||
|
|
||||||
sf::Vector3<size_t> map_size; // width * height, width, height
|
sf::Vector3<size_t> map_size; // width * height, width, height
|
||||||
int *tiles = nullptr;
|
int *tiles = nullptr;
|
||||||
|
@ -86,6 +90,9 @@ class Map : public sf::Drawable
|
||||||
|
|
||||||
std::vector<sf::Vector2u> enemy_spawns;
|
std::vector<sf::Vector2u> enemy_spawns;
|
||||||
sf::Vector2u player_spawn;
|
sf::Vector2u player_spawn;
|
||||||
|
|
||||||
|
MapError populate_vertex_array();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Map();
|
Map();
|
||||||
|
|
||||||
|
@ -95,7 +102,9 @@ public:
|
||||||
|
|
||||||
void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
||||||
|
|
||||||
|
#ifdef _DEBUG
|
||||||
void debug();
|
void debug();
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,39 +3,36 @@
|
||||||
TileAtlas TileAtlas::defaultAtlas()
|
TileAtlas TileAtlas::defaultAtlas()
|
||||||
{
|
{
|
||||||
TileAtlas atlas(sf::Vector2u{16, 16}, sf::Vector2u{16, 16}, ASSETS_PATH "/default_tilemap.png");
|
TileAtlas atlas(sf::Vector2u{16, 16}, sf::Vector2u{16, 16}, ASSETS_PATH "/default_tilemap.png");
|
||||||
atlas.sprite.setTexture(atlas.texture);
|
|
||||||
atlas.sprite.setTextureRect(sf::IntRect({0, 0}, {static_cast<int>(atlas.tile_size.x), static_cast<int>(atlas.tile_size.y)}));
|
|
||||||
return atlas;
|
return atlas;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TileAtlas::set(uint32_t x, uint32_t y)
|
void TileAtlas::set(uint32_t x, uint32_t y)
|
||||||
{
|
{
|
||||||
if (x >= tile_count.x || y >= tile_count.y) return;
|
if (x >= tile_count.x || y >= tile_count.y) return;
|
||||||
sprite.setTextureRect(sf::IntRect({static_cast<int>(x * tile_size.x), static_cast<int>(y * tile_size.y)}, sf::Vector2<int>(tile_size)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TileAtlas::TileAtlas(const sf::Vector2u tile_size, const sf::Vector2u tile_count, const std::filesystem::path& path) :
|
TileAtlas::TileAtlas(const sf::Vector2u tile_size, const sf::Vector2u tile_count, const std::filesystem::path& path) :
|
||||||
tile_size(tile_size), tile_count(tile_count), sprite(texture) { load(path); }
|
tile_size(tile_size), tile_count(tile_count) { load(path); }
|
||||||
|
|
||||||
TileAtlas::TileAtlas(sf::Vector2u tile_size, sf::Vector2u tile_count) : tile_size(tile_size), tile_count(tile_count), sprite(texture)
|
TileAtlas::TileAtlas(sf::Vector2u tile_size, sf::Vector2u tile_count) : tile_size(tile_size), tile_count(tile_count)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void TileAtlas::load(const std::filesystem::path& path)
|
void TileAtlas::load(const std::filesystem::path& path) {
|
||||||
{
|
|
||||||
if (!texture.loadFromFile(path))
|
if (!texture.loadFromFile(path))
|
||||||
{
|
{
|
||||||
throw std::runtime_error("TileAtlas: [Loading] Could not load tilemap -> Fatal");
|
throw std::runtime_error("TileAtlas: [Loading] Could not load tilemap -> Fatal");
|
||||||
}
|
}
|
||||||
sprite.setTexture(texture);
|
|
||||||
sprite.setTextureRect(sf::IntRect({0, 0}, {static_cast<int>(tile_size.x), static_cast<int>(tile_size.y)}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TileAtlas::draw(sf::RenderTarget& target, const sf::RenderStates states) const
|
void TileAtlas::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
||||||
{
|
{
|
||||||
if (texture.getSize().x == 0 || texture.getSize().y == 0)
|
// apply the transform
|
||||||
{
|
states.transform *= getTransform();
|
||||||
return;
|
|
||||||
}
|
// apply the tileset texture
|
||||||
target.draw(sprite, states);
|
states.texture = &texture;
|
||||||
|
|
||||||
|
// draw the vertex array
|
||||||
|
target.draw(vertices, states);
|
||||||
}
|
}
|
|
@ -1,12 +1,11 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
class TileAtlas : public sf::Drawable
|
class TileAtlas : public sf::Drawable, public sf::Transformable
|
||||||
{
|
{
|
||||||
sf::Vector2u tile_size;
|
sf::Vector2u tile_size;
|
||||||
sf::Vector2u tile_count;
|
sf::Vector2u tile_count;
|
||||||
sf::Texture texture;
|
|
||||||
sf::Sprite sprite;
|
|
||||||
public:
|
public:
|
||||||
explicit TileAtlas(sf::Vector2u tile_size, sf::Vector2u tile_count, const std::filesystem::path& path);
|
explicit TileAtlas(sf::Vector2u tile_size, sf::Vector2u tile_count, const std::filesystem::path& path);
|
||||||
TileAtlas(sf::Vector2u tile_size, sf::Vector2u tile_count);
|
TileAtlas(sf::Vector2u tile_size, sf::Vector2u tile_count);
|
||||||
|
@ -18,4 +17,7 @@ public:
|
||||||
void set(uint32_t, uint32_t);
|
void set(uint32_t, uint32_t);
|
||||||
|
|
||||||
void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
};
|
};
|
16
src/main.cpp
16
src/main.cpp
|
@ -3,13 +3,29 @@
|
||||||
|
|
||||||
#include "Game.hpp"
|
#include "Game.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
sf::RenderWindow window(sf::VideoMode({800, 450}), "Colysis", sf::Style::Close | sf::Style::Titlebar);
|
sf::RenderWindow window(sf::VideoMode({800, 450}), "Colysis", sf::Style::Close | sf::Style::Titlebar);
|
||||||
window.setMinimumSize(sf::Vector2u{800, 450});
|
window.setMinimumSize(sf::Vector2u{800, 450});
|
||||||
|
|
||||||
|
try {
|
||||||
Game game(window);
|
Game game(window);
|
||||||
game.run();
|
game.run();
|
||||||
|
}
|
||||||
|
catch (const std::runtime_error& e)
|
||||||
|
{
|
||||||
|
std::cerr << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
catch (const std::exception& e)
|
||||||
|
{
|
||||||
|
std::cerr << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
std::cerr << "Unknown error" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::flush(std::cerr);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
71
utils/MapFile.hexpat
Normal file
71
utils/MapFile.hexpat
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
#pragma endian little
|
||||||
|
|
||||||
|
struct Version {
|
||||||
|
u8 major;
|
||||||
|
u8 minor;
|
||||||
|
u8 patch;
|
||||||
|
};
|
||||||
|
|
||||||
|
bitfield OptionFlags {
|
||||||
|
bool CustomeTilemap : 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct TilemapInfo {
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Options {
|
||||||
|
OptionFlags flags;
|
||||||
|
if (flags.CustomeTilemap) {
|
||||||
|
u32 filepath_length;
|
||||||
|
char filepath[filepath_length];
|
||||||
|
TilemapInfo info;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct SpecialData {
|
||||||
|
u32 data;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Tile {
|
||||||
|
u32 type;
|
||||||
|
if (type == 0) {
|
||||||
|
SpecialData special;
|
||||||
|
}
|
||||||
|
}[[single_color]];
|
||||||
|
|
||||||
|
struct Map
|
||||||
|
{
|
||||||
|
u32 width;
|
||||||
|
u32 height;
|
||||||
|
u32 specials;
|
||||||
|
|
||||||
|
Tile tiles[width * height];
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Spawn {
|
||||||
|
u32 x,y;
|
||||||
|
}[[single_color]];
|
||||||
|
|
||||||
|
struct EnemySpawns {
|
||||||
|
u32 count;
|
||||||
|
Spawn spawns[count];
|
||||||
|
};
|
||||||
|
|
||||||
|
import std.io as io;
|
||||||
|
|
||||||
|
char magic[4] @$;
|
||||||
|
if (magic != "CYMF") {
|
||||||
|
io::warning("Magic does not match: \"" + magic + "\"; Expected: \"CYMF\"");
|
||||||
|
}
|
||||||
|
|
||||||
|
Version version @$ [[single_color]];
|
||||||
|
|
||||||
|
Options options @$;
|
||||||
|
|
||||||
|
|
||||||
|
Map map @$;
|
||||||
|
|
||||||
|
Spawn player @$ [[single_color]];
|
||||||
|
|
||||||
|
EnemySpawns enemies @$;
|
Loading…
Reference in a new issue