-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #154 from GuidoDQR/sprite-offset
* Added offset to 2D sprite (Guido Diego Quispe Robles) * Added tutorial with font and texture atlas (Guido Diego Quispe Robles)
- Loading branch information
Showing
15 changed files
with
566 additions
and
6 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 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
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 |
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,4 @@ | ||
# Ignore everything in this directory | ||
* | ||
# Except this file | ||
!.gitignore |
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 |
---|---|---|
@@ -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 |
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,4 @@ | ||
# Ignore everything in this directory | ||
* | ||
# Except this file | ||
!.gitignore |
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,4 @@ | ||
# Ignore everything in this directory | ||
* | ||
# Except this file | ||
!.gitignore |
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,4 @@ | ||
$ConfigFile = Join-Path $PSScriptRoot '../../windows-pcsx2.ps1' | ||
. $ConfigFile | ||
|
||
RunPCSX2 |
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 |
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,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; | ||
} |
Oops, something went wrong.