diff --git a/tutorials/10-sprite-sheet/Makefile b/tutorials/10-sprite-sheet/Makefile new file mode 100644 index 0000000..e7d6385 --- /dev/null +++ b/tutorials/10-sprite-sheet/Makefile @@ -0,0 +1,24 @@ +TARGET := tutorial_10.elf +ENGINEDIR := ../../engine + +#The Directories, Source, Includes, Objects, Binary and Resources +SRCDIR := src +INCDIR := inc +BUILDDIR := obj +TARGETDIR := bin +RESDIR := res +SRCEXT := cpp +VSMEXT := vsm +VCLEXT := vcl +VCLPPEXT := vclpp +DEPEXT := d +OBJEXT := o + +#Flags, Libraries and Includes +CFLAGS := +LIB := +LIBDIRS := +INC := -I$(INCDIR) +INCDEP := -I$(INCDIR) + +include ../Makefile.tutorials-base \ No newline at end of file diff --git a/tutorials/10-sprite-sheet/bin/.gitignore b/tutorials/10-sprite-sheet/bin/.gitignore new file mode 100644 index 0000000..44c5ea8 --- /dev/null +++ b/tutorials/10-sprite-sheet/bin/.gitignore @@ -0,0 +1,4 @@ +# Ignore everything in this directory +* +# Except this file +!.gitignore \ No newline at end of file diff --git a/tutorials/10-sprite-sheet/inc/tutorial_10.hpp b/tutorials/10-sprite-sheet/inc/tutorial_10.hpp new file mode 100644 index 0000000..3f14763 --- /dev/null +++ b/tutorials/10-sprite-sheet/inc/tutorial_10.hpp @@ -0,0 +1,34 @@ +/* +# _____ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2022, tyra - https://github.com/h4570/tyra +# Licensed under Apache License 2.0 +# Sandro Sobczyński +*/ + +#pragma once + +#include + +namespace Tyra { + +class Tutorial10 : public Game { + public: + Tutorial10(Engine* engine); + ~Tutorial10(); + + void init(); + void loop(); + + private: + void loadTexture(); + void loadSprite(); + + Engine* engine; + + Sprite sprite; +}; + +} // namespace Tyra diff --git a/tutorials/10-sprite-sheet/obj/.gitignore b/tutorials/10-sprite-sheet/obj/.gitignore new file mode 100644 index 0000000..44c5ea8 --- /dev/null +++ b/tutorials/10-sprite-sheet/obj/.gitignore @@ -0,0 +1,4 @@ +# Ignore everything in this directory +* +# Except this file +!.gitignore \ No newline at end of file diff --git a/tutorials/10-sprite-sheet/res/.gitignore b/tutorials/10-sprite-sheet/res/.gitignore new file mode 100644 index 0000000..44c5ea8 --- /dev/null +++ b/tutorials/10-sprite-sheet/res/.gitignore @@ -0,0 +1,4 @@ +# Ignore everything in this directory +* +# Except this file +!.gitignore \ No newline at end of file diff --git a/tutorials/10-sprite-sheet/run.ps1 b/tutorials/10-sprite-sheet/run.ps1 new file mode 100644 index 0000000..74646cc --- /dev/null +++ b/tutorials/10-sprite-sheet/run.ps1 @@ -0,0 +1,4 @@ +$ConfigFile = Join-Path $PSScriptRoot '../../windows-pcsx2.ps1' +. $ConfigFile + +RunPCSX2 diff --git a/tutorials/10-sprite-sheet/src/main.cpp b/tutorials/10-sprite-sheet/src/main.cpp new file mode 100644 index 0000000..087f27c --- /dev/null +++ b/tutorials/10-sprite-sheet/src/main.cpp @@ -0,0 +1,26 @@ +/* +# _____ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2022, tyra - https://github.com/h4570/tyra +# Licensed under Apache License 2.0 +# Sandro Sobczyński +*/ + +#include "engine.hpp" +#include "tutorial_10.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::Tutorial10 game(&engine); + engine.run(&game); + return 0; +} diff --git a/tutorials/10-sprite-sheet/src/tutorial_10.cpp b/tutorials/10-sprite-sheet/src/tutorial_10.cpp new file mode 100644 index 0000000..09db4e6 --- /dev/null +++ b/tutorials/10-sprite-sheet/src/tutorial_10.cpp @@ -0,0 +1,112 @@ +/* +# _____ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2022, tyra - https://github.com/h4570/tyra +# Licensed under Apache License 2.0 +# Sandro Sobczyński +*/ + +#include +#include "tutorial_10.hpp" + +namespace Tyra { + +Tutorial10::Tutorial10(Engine* t_engine) : engine(t_engine) {} + +Tutorial10::~Tutorial10() { + engine->renderer.getTextureRepository().freeBySprite(sprite); +} + +void Tutorial10::init() { + engine->renderer.setClearScreenColor(Color(32.0F, 32.0F, 32.0F)); + + /** Sprite contains rectangle information. */ + loadSprite(); + + /** Texture contains png image. */ + loadTexture(); +} + +void Tutorial10::loop() { + auto& renderer = engine->renderer; + + /** Begin frame will clear our screen. */ + renderer.beginFrame(); + + /** Render sprite. */ + renderer.renderer2D.render(sprite); + + /** End frame will perform vsync. */ + renderer.endFrame(); +} + +void Tutorial10::loadSprite() { + const auto& screenSettings = engine->renderer.core.getSettings(); + + sprite.mode = SpriteMode::MODE_STRETCH; + + /** Let's scale it down */ + sprite.size = Vec2(256.0F, 256.0F); + + /** Set it in screen center */ + 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 Tutorial10::loadTexture() { + /** + * Renderer has high layer functions, + * which allows to render: + * - Sprite (2D) + * - Mesh (3D) + * + * It uses ONLY low layer functions which are in renderer.core + */ + auto& renderer = engine->renderer; + + /** + * TextureRepository is a repository of textures. + * It is a singleton class, with all game textures. + * We are linking these textures with sprite's (2D) and mesh (3D) materials. + */ + auto& textureRepository = renderer.getTextureRepository(); + + /** + * Texture is stored in "res" directory. + * Content of "res" directory is automatically copied into + * "bin" directory, which contains our final game. + * + * File utils automatically add's device prefix to the path, + * based on current working directory. + * + * In PS2 world: + * - USB has a "mass:" prefix + * - Our PC in PS2Link has a "host:" prefix + * - Our PC in PCSX2 has a "host:" prefix + */ + auto filepath = FileUtils::fromCwd("atlas.png"); + + /** + * Tyra supports following PNG formats: + * 32bpp (RGBA) + * 24bpp (RGB) + * 8bpp, palletized (RGBA) + * 4bpp, palletized (RGBA) + * + * 8bpp and 4bpp are the fastest. + * All of these formats can be easily exported via GIMP. + */ + auto* texture = textureRepository.add(filepath); + + /** Let's assign this texture to sprite. */ + texture->addLink(sprite.id); + + TYRA_LOG("Texture loaded!"); +} + +} // namespace Tyra