diff --git a/Makefile b/Makefile index 24c9a97..17bf4dd 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -TARGET := racer.elf +TARGET := test.elf ENGINEDIR := /tyra/engine #The Directories, Source, Includes, Objects, Binary and Resources diff --git a/inc/block.hpp b/inc/block.hpp new file mode 100644 index 0000000..52565a8 --- /dev/null +++ b/inc/block.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include + +namespace Test { + + class Block { + public: + Block(); + Block(const Block& v); + ~Block(); + + void updateModelMatrix(); + + Tyra::M4x4 translation, rotation, scale; + + // Result matrix after transformations + Tyra::M4x4 modelMatrix; + + Tyra::Color color; + + Tyra::Vec4 atlasOffset; + + Tyra::McpipBlock renderData; + + private: + void setRenderData(); + }; +} \ No newline at end of file diff --git a/inc/racer_game.hpp b/inc/racer_game.hpp deleted file mode 100644 index c2f5a14..0000000 --- a/inc/racer_game.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include - -namespace Racer { - -class RacerGame : public Tyra::Game { - public: - RacerGame(Tyra::Engine* engine); - ~RacerGame(); - - void init(); - void loop(); - - private: - Tyra::Engine* engine; - void loadTexture(); - void loadSprite(); - - Tyra::Sprite sprite; -}; - -} // namespace Racer diff --git a/inc/render_test.hpp b/inc/render_test.hpp new file mode 100644 index 0000000..191a055 --- /dev/null +++ b/inc/render_test.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include +#include "block.hpp" + +namespace Test { + +class RenderTest : public Tyra::Game { + public: + RenderTest(Tyra::Engine* engine); + ~RenderTest(); + + void init(); + void loop(); + + private: + Tyra::Engine* engine; + Tyra::Texture* textureAtlas; + + Tyra::MinecraftPipeline mcpip; + Tyra::Vec4 cameraPosition, cameraLookAt; + + void loadTexture(); + Block getBlock(const u8& atlasX, const u8& atlasY, const float& translationX); + void rotateBlock(Block& block); + std::vector blocks; + + + +}; + +} // namespace Test diff --git a/res/atlas.png b/res/atlas.png new file mode 100644 index 0000000..6c86ea5 Binary files /dev/null and b/res/atlas.png differ diff --git a/src/block.cpp b/src/block.cpp new file mode 100644 index 0000000..a4050b7 --- /dev/null +++ b/src/block.cpp @@ -0,0 +1,39 @@ +#include "block.hpp" + +namespace Test { + +using namespace Tyra; + +Block::Block() { + // Set Matrices to identity + translation = M4x4::Identity; + rotation = M4x4::Identity; + scale = M4x4::Identity; + + setRenderData(); +} + +// Copy constructor +Block::Block(const Block& v) { + translation = v.translation; + rotation = v.rotation; + scale = v.scale; + modelMatrix = v.modelMatrix; + color = v.color; + atlasOffset = v.atlasOffset; + + setRenderData(); +} +Block::~Block() {} + +void Block::updateModelMatrix() { + modelMatrix = translation * rotation * scale; +} + +void Block::setRenderData() { + renderData.color = &color; + renderData.model = &modelMatrix; + renderData.textureOffset = &atlasOffset; +} + +} diff --git a/src/main.cpp b/src/main.cpp index b9d8bde..1e24b8d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,11 @@ #include -#include "racer_game.hpp" +#include "render_test.hpp" int main() { - Tyra::Engine engine; - Racer::RacerGame game(&engine); + Tyra::EngineOptions options = { .writeLogsToFile = true }; + + Tyra::Engine engine(options); + Test::RenderTest game(&engine); engine.run(&game); SleepThread(); return 0; diff --git a/src/racer_game.cpp b/src/racer_game.cpp deleted file mode 100644 index 3970f08..0000000 --- a/src/racer_game.cpp +++ /dev/null @@ -1,58 +0,0 @@ -#include "racer_game.hpp" - -namespace Racer { - -using namespace Tyra; - -RacerGame::RacerGame(Engine* t_engine) { engine = t_engine; } -RacerGame::~RacerGame() { - engine->renderer.getTextureRepository().freeBySprite(sprite); -} - -void RacerGame::init() { - engine->renderer.setClearScreenColor(Color(32.0F, 32.0F, 32.0F)); - - loadSprite(); - - loadTexture(); -} - -void RacerGame::loop() { - auto& renderer = engine->renderer; - - renderer.beginFrame(); - - renderer.renderer2D.render(sprite); - - renderer.endFrame(); -} - -void RacerGame::loadSprite() { - const auto& screenSettings = engine->renderer.core.getSettings(); - - sprite.mode = SpriteMode::MODE_STRETCH; - - sprite.size = Vec2(256.0F, 128.0F); - - sprite.position = - Vec2(screenSettings.getWidth() / 2.0F - sprite.size.x / 2.0F, - screenSettings.getHeight() / 2.0F - sprite.size.y / 2.0F); - - TYRA_LOG("Sprite created"); -} - -void RacerGame::loadTexture() { - auto& renderer = engine->renderer; - - auto& textureRepository = renderer.getTextureRepository(); - - auto filepath = FileUtils::fromCwd("chicken.png"); - - auto* texture = textureRepository.add(filepath); - - texture->addLink(sprite.id); - - TYRA_LOG("Texture loaded and linked to sprite"); -} - -} // namespace Racer diff --git a/src/render_test.cpp b/src/render_test.cpp new file mode 100644 index 0000000..f8d8ee3 --- /dev/null +++ b/src/render_test.cpp @@ -0,0 +1,82 @@ +#include "render_test.hpp" + +namespace Test { + +using namespace Tyra; + +RenderTest::RenderTest(Engine* t_engine) : engine(t_engine) { + textureAtlas = nullptr; +} + +RenderTest::~RenderTest() { + if (textureAtlas != nullptr) { + engine->renderer.getTextureRepository().free(textureAtlas); + } +} + +void RenderTest::init() { + loadTexture(); + engine->renderer.setClearScreenColor(Color(32.0F, 32.0F, 32.0F)); + + cameraPosition = Vec4(0.0F, 0.0F, -10.0F); + cameraLookAt.unit(); + + mcpip.setRenderer(&engine->renderer.core); + TYRA_LOG("RendererCore set for MinecraftPipeline"); + + blocks.push_back(getBlock(0, 1, 2.0F)); + blocks.push_back(getBlock(4, 0, -2.0F)); + TYRA_LOG("Blocks created and added to the scene"); +} + +void RenderTest::loop() { + for (auto& block : blocks) { + rotateBlock(block); + } + + + engine->renderer.beginFrame(CameraInfo3D(&cameraPosition, &cameraLookAt)); + { + engine->renderer.renderer3D.usePipeline(&mcpip); + { + mcpip.render({&blocks[0].renderData}, textureAtlas); + + mcpip.render({&blocks[1].renderData}, textureAtlas, true); + } + + } + engine->renderer.endFrame(); +} + +void RenderTest::loadTexture() { + auto& repo = engine->renderer.getTextureRepository(); + textureAtlas = repo.add(FileUtils::fromCwd("atlas.png")); + TYRA_LOG("Texture atlas loaded"); +} + +Block RenderTest::getBlock(const u8& atlasX, const u8& atlasY, const float& translationX) { + Block result; + + const u8 texturesInAtlasRow = 16; + const float textureOffset = 1.0F / texturesInAtlasRow; + + result.atlasOffset = Vec4(atlasX * textureOffset, atlasY * textureOffset); + + result.color = Color(128.0F, 128.0F, 128.0F, 128.0F); + + result.translation.translateX(translationX); + + result.updateModelMatrix(); + + TYRA_LOG("Block created with atlas coordinates (" + std::to_string(atlasX) + ", " + std::to_string(atlasY) + ") and translationX " + std::to_string(translationX)); + + return result; +} + +void RenderTest::rotateBlock(Block& block) { + block.rotation.rotate(Vec4(0.01F, 0.02F, 0.03F)); + + block.updateModelMatrix(); +} + +} // namespace Test