Skip to content

Commit

Permalink
Merge pull request #154 from GuidoDQR/sprite-offset
Browse files Browse the repository at this point in the history
* Added offset to 2D sprite (Guido Diego Quispe Robles)
* Added tutorial with font and texture atlas (Guido Diego Quispe Robles)
  • Loading branch information
h4570 authored May 29, 2023
2 parents a25594b + 87b9831 commit e63ae14
Show file tree
Hide file tree
Showing 15 changed files with 566 additions and 6 deletions.
2 changes: 1 addition & 1 deletion engine/inc/info/version.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Version {
~Version() {}

static const u8 major = 2;
static const u8 minor = 1;
static const u8 minor = 2;
static const u8 patch = 0;

static std::string toString() {
Expand Down
4 changes: 4 additions & 0 deletions engine/inc/renderer/core/2d/renderer_core_2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "debug/debug.hpp"
#include "renderer/core/2d/sprite/sprite.hpp"
#include "renderer/core/texture/renderer_core_texture_buffers.hpp"
#include "renderer/3d/pipeline/shared/pipeline_texture_mapping_type.hpp"
#include "renderer/core/texture/models/texture.hpp"
#include "renderer/renderer_settings.hpp"
#include <packet2_utils.h>
Expand All @@ -30,6 +31,9 @@ class RendererCore2D {
void render(const Sprite& sprite,
const RendererCoreTextureBuffers& texBuffers, Texture* texture);

void setTextureMappingType(
const PipelineTextureMappingType textureMappingType);

private:
void setPrim();
void setLod();
Expand Down
2 changes: 1 addition & 1 deletion engine/inc/renderer/core/2d/sprite/sprite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Sprite {
~Sprite();

u32 id;
Vec2 position, size;
Vec2 position, size, offset;
float scale;
Color color;
SpriteMode mode;
Expand Down
16 changes: 12 additions & 4 deletions engine/src/renderer/core/2d/renderer_core_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ void RendererCore2D::render(const Sprite& sprite,
else if (sizeY > sizeX)
texS = texMax / (sizeY / sizeX);

rect->t0.s = sprite.flipHorizontal ? texS : 0.0F;
rect->t0.t = sprite.flipVertical ? texT : 0.0F;
rect->t1.s = sprite.flipHorizontal ? 0.0F : texS;
rect->t1.t = sprite.flipVertical ? 0.0F : texT;
rect->t0.s =
sprite.flipHorizontal ? (texS + sprite.offset.x) : sprite.offset.x;
rect->t0.t = sprite.flipVertical ? (texT + sprite.offset.y) : sprite.offset.y;
rect->t1.s =
sprite.flipHorizontal ? sprite.offset.x : (texS + sprite.offset.x);
rect->t1.t = sprite.flipVertical ? sprite.offset.y : (texT + sprite.offset.y);

rect->color.r = sprite.color.r;
rect->color.g = sprite.color.g;
Expand Down Expand Up @@ -131,4 +133,10 @@ void RendererCore2D::render(const Sprite& sprite,
context = !context;
}

void RendererCore2D::setTextureMappingType(
const PipelineTextureMappingType textureMappingType) {
lod.mag_filter = textureMappingType;
lod.min_filter = textureMappingType;
}

} // namespace Tyra
1 change: 1 addition & 0 deletions engine/src/renderer/core/2d/sprite/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Sprite::Sprite() {
flipVertical = false;
size.set(32.0F, 32.0F);
position.set(100.0F, 100.0F);
offset.set(0.0F, 0.0F);
scale = 1.0F;
mode = MODE_REPEAT;
setDefaultColor();
Expand Down
24 changes: 24 additions & 0 deletions tutorials/10-sprite-sheet/Makefile
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions tutorials/10-sprite-sheet/bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
37 changes: 37 additions & 0 deletions tutorials/10-sprite-sheet/inc/font_sprite.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
# _____ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022-2023, tyra - https://github.com/h4570/tyra
# Licensed under Apache License 2.0
# Guido Diego Quispe Robles
*/

#pragma once

#include <tyra>

#define FONT_CHAR_SIZE 96

namespace Tyra {

class Font {
public:
Font();
void load(TextureRepository& repository, Renderer2D* renderer);
void free(TextureRepository& repository);
void drawText(const char* text, const int& x, const int& y, Color color);
void drawText(const std::string& text, const int& x, const int& y,
Color color);

private:
const static int chars[FONT_CHAR_SIZE];
const static int charWidths[FONT_CHAR_SIZE];

Renderer2D* renderer2D;
Sprite allFont;
std::array<Sprite, FONT_CHAR_SIZE> font;
};

} // namespace Tyra
45 changes: 45 additions & 0 deletions tutorials/10-sprite-sheet/inc/tutorial_10.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
# _____ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022-2023, tyra - https://github.com/h4570/tyra
# Licensed under Apache License 2.0
# Guido Diego Quispe Robles
*/

#pragma once

#include <tyra>
#include "font_sprite.hpp"

namespace Tyra {

class Tutorial10 : public Game {
public:
explicit Tutorial10(Engine* engine);
~Tutorial10();

void init();
void loop();

private:
void loadTexture();
void loadSprite();
void handlePad();

int padTimer;
Engine* engine;
Pad* pad;
Font font;

Sprite sprite;
Sprite spriteFlip;
Sprite spriteScale;
Sprite spriteStretch;
std::string strFilter;

PipelineTextureMappingType textureFilter;
};

} // namespace Tyra
4 changes: 4 additions & 0 deletions tutorials/10-sprite-sheet/obj/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
4 changes: 4 additions & 0 deletions tutorials/10-sprite-sheet/res/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
4 changes: 4 additions & 0 deletions tutorials/10-sprite-sheet/run.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$ConfigFile = Join-Path $PSScriptRoot '../../windows-pcsx2.ps1'
. $ConfigFile

RunPCSX2
109 changes: 109 additions & 0 deletions tutorials/10-sprite-sheet/src/font_sprite.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
# _____ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022-2023, tyra - https://github.com/h4570/tyra
# Licensed under Apache License 2.0
# Guido Diego Quispe Robles
*/

#include "font_sprite.hpp"

namespace Tyra {

const int Font::chars[FONT_CHAR_SIZE]{
' ', '!', '"', ' ', '$', '%', ' ', '{', '(', ')', ' ', '+', ',', '-',
'.', '/', '0', '1', '2', '3', '4', '5', '6', '2', '8', '9', ':', ';',
'<', '=', '>', '?', ' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', ' ', ' ', ' ', ' ', ' ', ' ', 'a', 'b', 'c', 'd', 'e',
'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z', '[', '}', ']', '~', ' '};

const int Font::charWidths[FONT_CHAR_SIZE]{
0, 1, 3, 0, 5, 9, 0, 9, 3, 3, 0, 5, 2, 2, 1, 4, 4, 2, 4, 4, 5, 4, 4, 4,
4, 4, 1, 2, 4, 5, 4, 4, 0, 6, 5, 5, 5, 4, 4, 5, 5, 1, 4, 5, 4, 7, 5, 5,
5, 5, 5, 5, 5, 5, 6, 7, 5, 5, 4, 0, 0, 0, 0, 0, 0, 5, 4, 4, 4, 4, 3, 4,
4, 1, 2, 4, 1, 7, 4, 4, 4, 4, 3, 4, 3, 4, 5, 7, 4, 4, 4, 2, 5, 2, 6, 0,
};

Font::Font() {}

void Font::load(TextureRepository& repository, Renderer2D* renderer) {
renderer2D = renderer;

float height = 16.0F;
float width = 16.0F;

allFont.mode = MODE_REPEAT;
allFont.size = Vec2(255, 127);

auto filepath = FileUtils::fromCwd("earthbound-Font.png");
auto* texture = repository.add(filepath);
texture->addLink(allFont.id);

int column = 0;
int arrow = 0;

for (int i = 0; i < FONT_CHAR_SIZE; i++) {
font[i].id = allFont.id;
font[i].mode = MODE_REPEAT;
font[i].size = Vec2(width, height);
font[i].offset = Vec2(width * column, height * arrow);
column++;

if (column == 16) {
arrow++;
column = 0;
}
}
}

void Font::free(TextureRepository& repository) {
repository.freeBySprite(allFont);
for (int i = 0; i < FONT_CHAR_SIZE; i++) {
repository.freeBySprite(font[i]);
}
}

void Font::drawText(const char* text, const int& x, const int& y, Color color) {
drawText(std::string(text), x, y, color);
}

void Font::drawText(const std::string& text, const int& x, const int& y,
Color color) {
int sizeText = text.size();

int offsetY = 0;
int offsetX = 0;

for (int i = 0; i < sizeText; i++) {
int fontPos = text[i];
Sprite fontSpr = font[0];

for (int j = 0; j < FONT_CHAR_SIZE; j++) {
if (fontPos == chars[j]) {
fontPos = j;
fontSpr = font[j];
fontSpr.color = color;
fontSpr.position = Vec2(x + offsetX, y + offsetY);
break;
}
}

if (fontPos == '\n') {
offsetY += 18;
offsetX = 0.0f;
} else {
if ((fontPos != ' ') && (fontPos != '\t')) {
renderer2D->render(fontSpr);
offsetX += charWidths[fontPos] + 2;
} else {
offsetX += 2;
}
}
}
}

} // namespace Tyra
26 changes: 26 additions & 0 deletions tutorials/10-sprite-sheet/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
# _____ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022-2023, tyra - https://github.com/h4570/tyra
# Licensed under Apache License 2.0
# Guido Diego Quispe Robles
*/

#include "engine.hpp"
#include "tutorial_10.hpp"

/**
* In this tutorial we will learn:
* - How works the offset of the sprite
* - How works the texture mapping
* - How to write text (font)
*/

int main() {
Tyra::Engine engine;
Tyra::Tutorial10 game(&engine);
engine.run(&game);
return 0;
}
Loading

0 comments on commit e63ae14

Please sign in to comment.