Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
Texture.cpp
1/*
2Copyright (C) 2016-2018,2020,2021,2025 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16#include <string>
17#include <cstring>
18#include <cassert>
19#include "aeongames/Texture.hpp"
20#include "Decoder.h"
21
22namespace AeonGames
23{
24 Texture::~Texture() = default;
25
26 bool RegisterImageDecoder ( const std::string& aMagick, const std::function < bool ( Texture&, size_t, const void* ) > & aDecoder )
27 {
28 return Decoder<Texture>::RegisterDecoder ( aMagick, aDecoder );
29 }
30
31 bool UnregisterImageDecoder ( const std::string& aMagick )
32 {
33 return Decoder<Texture>::UnregisterDecoder ( aMagick );
34 }
35
36 bool DecodeImage ( Texture& aTexture, const void* aBuffer, size_t aBufferSize )
37 {
38 return Decoder<Texture>::Decode ( aTexture, aBuffer, aBufferSize );
39 }
40
41 bool DecodeImage ( Texture& aTexture, const std::string& aFileName )
42 {
43 return Decoder<Texture>::Decode ( aTexture, aFileName );
44 }
45
46 void Texture::Resize ( uint32_t aWidth, uint32_t aHeight, const uint8_t* aPixels, Format aFormat, Type aType )
47 {
48 mWidth = aWidth;
49 mHeight = aHeight;
50 mFormat = aFormat;
51 mType = aType;
52 mPixels.resize ( aWidth * aHeight * GetPixelSize ( mFormat, mType ) );
53 if ( aPixels != nullptr )
54 {
55 memcpy ( mPixels.data(), aPixels, mPixels.size() );
56 }
57 else
58 {
59 memset ( mPixels.data(), 0, mPixels.size() );
60 }
61 }
62
63 void Texture::WritePixels ( int32_t aXOffset, int32_t aYOffset, uint32_t aWidth, uint32_t aHeight, Format aFormat, Type aType, const uint8_t* aPixels )
64 {
66 assert ( mType == aType && mFormat == aFormat );
67 uint8_t* cursor = mPixels.data() + ( ( aYOffset * mWidth ) + aXOffset );
68 size_t pixel_size = GetPixelSize ( mFormat, mType );
69 for ( size_t i = 0; i < aHeight; ++i )
70 {
71 memcpy ( cursor, aPixels + ( i * pixel_size * aWidth ), pixel_size * aWidth );
72 }
73 }
74
75 uint32_t Texture::GetWidth() const
76 {
77 return mWidth;
78 }
79 uint32_t Texture::GetHeight() const
80 {
81 return mHeight;
82 }
84 {
85 return mFormat;
86 }
88 {
89 return mType;
90 }
91
92 const std::vector<uint8_t>& Texture::GetPixels() const
93 {
94 return mPixels;
95 }
96
97 void Texture::LoadFromMemory ( const void* aBuffer, size_t aBufferSize )
98 {
99 DecodeImage ( *this, aBuffer, aBufferSize );
100 }
101
103 {
104 mPixels.clear();
105 mType = Type::Unknown;
106 mFormat = Format::Unknown;
107 mWidth = 0;
108 mHeight = 0;
109 }
110}
static bool Decode(T &aOutput, uint32_t aId)
Decodes a resource identified by its CRC32 id.
Definition Decoder.h:39
static bool RegisterDecoder(const std::string &aMagick, const std::function< bool(T &, size_t, const void *) > &aDecoder)
Registers a decoder function for the given magic byte sequence.
Definition Decoder.h:88
static bool UnregisterDecoder(const std::string &aMagick)
Unregisters the decoder for the given magic byte sequence.
Definition Decoder.h:105
Represents a 2D texture image resource.
Definition Texture.hpp:33
DLL void Resize(uint32_t aWidth, uint32_t aHeight, const uint8_t *aPixels=nullptr, Format aFormat=Format::Unknown, Type aType=Type::Unknown)
Resizes the texture and optionally replaces pixel data.
Definition Texture.cpp:46
DLL void Unload() final
Releases all texture data and resets the texture to an empty state.
Definition Texture.cpp:102
DLL uint32_t GetWidth() const
Returns the texture width in pixels.
Definition Texture.cpp:75
DLL Format GetFormat() const
Returns the pixel format.
Definition Texture.cpp:83
DLL const std::vector< uint8_t > & GetPixels() const
Returns a reference to the raw pixel data.
Definition Texture.cpp:92
Type
Pixel component data type.
Definition Texture.hpp:51
@ Unknown
Unspecified or invalid type.
Definition Texture.hpp:52
DLL ~Texture()
Destructor.
Format
Pixel channel layout format.
Definition Texture.hpp:40
@ Unknown
Unspecified or invalid format.
Definition Texture.hpp:41
DLL Type GetType() const
Returns the pixel component type.
Definition Texture.cpp:87
DLL void LoadFromMemory(const void *aBuffer, size_t aBufferSize) final
Loads texture data from a raw memory buffer.
Definition Texture.cpp:97
DLL void WritePixels(int32_t aXOffset, int32_t aYOffset, uint32_t aWidth, uint32_t aHeight, Format aFormat, Type aType, const uint8_t *aPixels)
Writes pixel data into a sub-region of the texture.
Definition Texture.cpp:63
DLL uint32_t GetHeight() const
Returns the texture height in pixels.
Definition Texture.cpp:79
<- This is here just for the literals
Definition AABB.hpp:31
DLL bool RegisterImageDecoder(const std::string &aMagick, const std::function< bool(Texture &, size_t, const void *) > &aDecoder)
Registers an image decoder for a specific file magic identifier.
Definition Texture.cpp:26
DLL bool UnregisterImageDecoder(const std::string &aMagick)
Unregisters a previously registered image decoder.
Definition Texture.cpp:31
DLL bool DecodeImage(Texture &aTexture, const void *aBuffer, size_t aBufferSize)
Decodes image data from a memory buffer into a Texture.
Definition Texture.cpp:36