-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
303 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
|
@@ -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; | ||
}; | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.