tutorial 1 and 2

This commit is contained in:
h4570
2022-08-20 18:53:27 +02:00
parent d251df8704
commit b4b48ae479
31 changed files with 377 additions and 405 deletions
+26
View File
@@ -0,0 +1,26 @@
/*
# _____ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licensed under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#include "engine.hpp"
#include "tutorial_02.hpp"
/**
* In this tutorial we will learn how to:
* - Load png file and create texture from it
* - Create sprite class
* - Clear screen and render sprite
*/
int main() {
Tyra::Engine engine;
Tyra::Tutorial02 game(&engine);
engine.run(&game);
return 0;
}
+66
View File
@@ -0,0 +1,66 @@
/*
# _____ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licensed under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#include <tyra>
#include "tutorial_02.hpp"
namespace Tyra {
Tutorial02::Tutorial02(Engine* t_engine) : engine(t_engine) {
texture = nullptr;
}
Tutorial02::~Tutorial02() {
if (texture != nullptr) {
engine->renderer.core.texture.repository.free(texture);
}
}
void Tutorial02::init() {
engine->renderer.setClearScreenColor(Color(32.0F, 32.0F, 32.0F));
loadSprite();
loadTexture();
}
void Tutorial02::loop() {
auto& renderer = engine->renderer;
renderer.beginFrame();
renderer.renderer2D.render(sprite);
renderer.endFrame();
}
void Tutorial02::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 Tutorial02::loadTexture() {
auto& renderer = engine->renderer;
auto& rendererCore = renderer.core;
auto& textureRepository = rendererCore.texture.repository;
auto filepath = FileUtils::fromCwd("tyra.png");
texture = textureRepository.add(filepath);
texture->addLink(sprite.id);
TYRA_LOG("Texture loaded!");
}
} // namespace Tyra