Skip to content

Commit

Permalink
minor adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
h4570 committed May 28, 2023
1 parent 20d2291 commit cab9347
Show file tree
Hide file tree
Showing 10 changed files with 303 additions and 275 deletions.
5 changes: 3 additions & 2 deletions engine/inc/renderer/core/2d/renderer_core_2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ class RendererCore2D {
void render(const Sprite& sprite,
const RendererCoreTextureBuffers& texBuffers, Texture* texture);

void setTextureMappingType(const PipelineTextureMappingType textureMappingType);

void setTextureMappingType(
const PipelineTextureMappingType textureMappingType);

private:
void setPrim();
void setLod();
Expand Down
13 changes: 8 additions & 5 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+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->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,7 +133,8 @@ void RendererCore2D::render(const Sprite& sprite,
context = !context;
}

void RendererCore2D::setTextureMappingType(const PipelineTextureMappingType textureMappingType){
void RendererCore2D::setTextureMappingType(
const PipelineTextureMappingType textureMappingType) {
lod.mag_filter = textureMappingType;
lod.min_filter = textureMappingType;
}
Expand Down
2 changes: 1 addition & 1 deletion engine/src/renderer/core/2d/sprite/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Sprite::Sprite() {
flipVertical = false;
size.set(32.0F, 32.0F);
position.set(100.0F, 100.0F);
offset.set(0.0F,0.0F);
offset.set(0.0F, 0.0F);
scale = 1.0F;
mode = MODE_REPEAT;
setDefaultColor();
Expand Down
35 changes: 0 additions & 35 deletions tutorials/10-sprite-sheet/inc/fontSprite.hpp

This file was deleted.

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
11 changes: 6 additions & 5 deletions tutorials/10-sprite-sheet/inc/tutorial_10.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@
# Copyright 2022-2023, tyra - https://github.com/h4570/tyra
# Licensed under Apache License 2.0
# Guido Diego Quispe Robles
# Sandro Sobczyński <[email protected]>
*/

#pragma once

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

namespace Tyra {

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

void init();
Expand All @@ -27,16 +26,18 @@ class Tutorial10 : public Game {
private:
void loadTexture();
void loadSprite();
void handlePad();

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

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

PipelineTextureMappingType textureFilter;
};
Expand Down
117 changes: 0 additions & 117 deletions tutorials/10-sprite-sheet/src/fontSprite.cpp

This file was deleted.

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
Loading

0 comments on commit cab9347

Please sign in to comment.