added 2d png support

This commit is contained in:
h4570
2020-12-01 17:26:10 +01:00
parent 3841487388
commit 510199164c
17 changed files with 317 additions and 80 deletions
+1
View File
@@ -32,6 +32,7 @@ public:
~Point();
void set(const float &t_x, const float &t_y);
void rotate(const float &t_angle, const float &t_x, const float &t_y);
void set(const Point &v);
const void print() const;
};
+47 -10
View File
@@ -17,6 +17,13 @@
#include "./texture_link.hpp"
#include "../include/utils/debug.hpp"
#include <vector>
#include <gs_psm.h>
enum TextureType
{
TEX_TYPE_RGB = GS_PSM_24, // BMP
TEX_TYPE_RGBA = GS_PSM_32 // PNG
};
/**
* Class which contains texture data.
@@ -46,15 +53,17 @@ public:
const u8 &getHeight() const { return height; };
const TextureType &getType() const { return _type; };
u32 getTextureLinksCount() const { return static_cast<u32>(texLinks.size()); };
texwrap_t *getWrapSettings() { return &wrapSettings; };
/**
* Returns always width * width * 3.
* 3 because of RGB
* Returns always width * width * 3/4.
* 3 for RGB, 4 for RGBA.
*/
u32 getDataSize() const { return width * height * 3; };
u32 getDataSize() const { return _type == TEX_TYPE_RGB ? width * height * 3 : width * height * 4; };
/**
* Texture data, used by renderer.
@@ -83,6 +92,12 @@ public:
return -1;
};
/**
* Returns index of link.
* -1 if not found.
*/
inline const s32 getIndexOfLink(const u32 &t_spriteId) const { return getIndexOfLink(t_spriteId, 0); };
// ----
// Setters
// ----
@@ -92,7 +107,7 @@ public:
* Do not call this method unless you know what you do.
* Should be called by data loader.
*/
void setSize(const u8 &t_width, const u8 &t_height);
void setSize(const u8 &t_width, const u8 &t_height, const TextureType &t_type);
/**
* Do not call this method unless you know what you do.
@@ -101,8 +116,8 @@ public:
void setHeight(const u8 &t_val) { height = t_val; }
/**
* Set texture name.
* Should be the file name without extension
* Do not call this method unless you know what you do.
* Should be called by data loader.
*/
void setData(const u32 &t_index, const unsigned char &t_val) { data[t_index] = t_val; }
@@ -119,9 +134,12 @@ public:
// Other
// ----
/** Assign texture to mesh and mesh material */
/** Assign texture to mesh and mesh material. */
void addLink(const u32 &t_meshId, const u32 &t_materialId);
/** Assign texture to sprite. */
void addLink(const u32 &t_spriteId);
const u8 &isNameSet() const { return _isNameSet; };
const u8 &isSizeSet() const { return _isSizeSet; };
@@ -135,13 +153,31 @@ public:
return false;
};
void removeLink(const u32 &t_index) { texLinks.erase(texLinks.begin() + t_index); }
const u8 isLinkedWith(const u32 &t_spriteId) const
{
s32 index = getIndexOfLink(t_spriteId);
if (index != -1)
return true;
else
return false;
};
void removeLink(const u32 &t_meshId, const u32 &t_materialId)
void removeLinkByIndex(const u32 &t_index) { texLinks.erase(texLinks.begin() + t_index); }
void removeLinkBySprite(const u32 &t_spriteId)
{
s32 index = getIndexOfLink(t_spriteId);
if (index != -1)
removeLinkByIndex(index);
else
PRINT_ERR("Cant remove link, because it was not found!");
}
void removeLinkByMesh(const u32 &t_meshId, const u32 &t_materialId)
{
s32 index = getIndexOfLink(t_meshId, t_materialId);
if (index != -1)
removeLink(index);
removeLinkByIndex(index);
else
PRINT_ERR("Cant remove link, because it was not found!");
}
@@ -150,6 +186,7 @@ private:
void setDefaultWrapSettings();
texwrap_t wrapSettings;
char *name;
TextureType _type;
std::vector<TextureLink> texLinks;
u32 id;
u8 width, height, _isNameSet, _isSizeSet;
+67
View File
@@ -0,0 +1,67 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2020, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#ifndef _TYRA_SPRITE_
#define _TYRA_SPRITE_
#include <models/math/point.hpp>
#include <draw_types.h>
#include <draw_buffers.h>
/**
* Class which have contain 2D image data.
*/
class Sprite
{
public:
Sprite();
~Sprite();
Point position, size;
float scale;
color_t color;
clutbuffer_t clut;
// ----
// Getters
// ----
/** Auto generated unique Id. */
inline const u32 &getId() const { return id; };
// ----
// Setters
// ----
// ----
// Other
// ----
/** Flip texture horizontally. */
inline void flipHorizontally(const u8 &t_set) { _flipH = t_set; };
/** Flip texture vertically. */
inline void flipVertically(const u8 &t_set) { _flipV = t_set; };
/** Check if texture is vertically flipped. */
inline const u8 &isFlippedVertically() const { return _flipV; };
/** Check if texture is horizontally flipped. */
inline const u8 &isFlippedHorizontally() const { return _flipH; };
private:
u32 id;
u8 _isSizeSet, _flipH, _flipV;
void setDefaultColor();
void setDefaultLODAndClut();
};
#endif
+4 -1
View File
@@ -15,7 +15,10 @@
struct TextureLink
{
u32 meshId, materialId;
/** Mesh id or sprite id */
u32 meshId;
/** Mesh material id or 0 if is sprite */
u32 materialId;
};
#endif
+5 -4
View File
@@ -17,6 +17,7 @@
#include "gif_sender.hpp"
#include "vif_sender.hpp"
#include "../models/math/plane.hpp"
#include "../models/sprite.hpp"
#include "../models/screen_settings.hpp"
#include "../models/render_data.hpp"
#include "./texture_repository.hpp"
@@ -39,6 +40,8 @@ public:
/// --- Draw: PATH3
void draw(Sprite &t_sprite);
/**
* Draw many meshes with lighting information.
* Slowest way of rendering (PATH 3, using EE and GIF).
@@ -103,14 +106,12 @@ public:
TextureRepository *getTextureRepository() { return &textureRepo; };
void drawRectangle();
private:
void changeTexture(const Mesh &t_mesh, u32 t_materialId);
void changeTexture(MeshTexture *t_tex);
u32 lastTextureId;
texbuffer_t textureBuffer;
u8 isTextureVRAMAllocated, isVSyncEnabled;
void allocateTextureBuffer(u16 t_width, u16 t_height);
void allocateTextureBuffer(MeshTexture *t_texture);
void deallocateTextureBuffer();
void flipBuffers();
void beginFrameIfNeeded();
@@ -17,6 +17,13 @@
#include "../models/mesh.hpp"
#include "../models/mesh_frame.hpp"
#include "../loaders/bmp_loader.hpp"
#include "../loaders/png_loader.hpp"
enum TextureFormat
{
BMP,
PNG
};
/** Class responsible for intializing draw env, textures and buffers */
class TextureRepository
@@ -35,6 +42,18 @@ public:
u32 getTexturesCount() const { return static_cast<u32>(textures.size()); };
/**
* Returns single texture.
* NULL if not found.
*/
MeshTexture *getBySprite(const u32 &t_spriteId)
{
for (u32 i = 0; i < textures.size(); i++)
if (textures[i]->isLinkedWith(t_spriteId))
return textures[i];
return NULL;
}
/**
* Returns single texture.
* NULL if not found.
@@ -79,20 +98,22 @@ public:
// Other
// ----
/** Add unlinked texture.
* NOTICE: BMP (RGB 888 24bit, without color information) is suppored only!
/**
* Add unlinked texture.
* @param t_subfolder Relative path. Ex.: "textures/"
* @param t_name Filename without extension. Ex.: "water"
*
* @param t_format if you want to use BMP, be sure that you have
* BMP with RGB 888 24bit, without color information.
*/
MeshTexture *add(char *t_subfolder, char *t_name);
MeshTexture *add(char *t_subfolder, char *t_name, TextureFormat t_format);
/**
* Add linked textures in given subpath for mesh material names.
* NOTICE: BMP (RGB 888 24bit, without color information) is suppored only!
* @param t_path Relative path where textures should be searched
* @param t_path Relative path where textures should be searched.
* @param t_format if you want to use BMP, be sure that you have
* BMP with RGB 888 24bit, without color information.
*/
void addByMesh(char *t_path, Mesh &mesh);
void addByMesh(char *t_path, Mesh &mesh, TextureFormat t_format);
/**
* Remove texture from repository.
@@ -115,7 +136,8 @@ public:
private:
std::vector<MeshTexture *> textures;
BmpLoader loader;
BmpLoader bmpLoader;
PngLoader pngLoader;
};
#endif