| 1 | /** |
| 2 | Copyright (c) テクニカル諏訪子 |
| 3 | |
| 4 | Permission is hereby granted to any person obtaining a copy of the software |
| 5 | Tetris (the "Software") to use, modify, merge, copy, publish, distribute, |
| 6 | sublicense, and/or sell copies of the Software, subject to the following conditions: |
| 7 | |
| 8 | 1. **Origin Attribution**: |
| 9 | - You must not misrepresent the origin of the Software; you must not claim |
| 10 | you created the original Software. |
| 11 | - If the Software is used in a product, you must either: |
| 12 | a. Provide clear attribution in the product's documentation, user interface, |
| 13 | or other visible areas, **OR** |
| 14 | b. Pay the original developers a fee they specify in writing. |
| 15 | 2. **Usage Restriction**: |
| 16 | - The Software, or any derivative works, dependencies, or libraries |
| 17 | incorporating it, must not be used for censorship or to suppress freedom of |
| 18 | speech, expression, or creativity. Prohibited uses include, but are not |
| 19 | limited to: |
| 20 | - Censorship of so-called "hate speech", visuals, non-mainstream opinions, |
| 21 | ideas, or objective reality. |
| 22 | - Tools or systems designed to restrict access to information or |
| 23 | artistic works. |
| 24 | 3. **Notice Preservation**: |
| 25 | - This license and the above copyright notice must remain intact in all copies |
| 26 | of the source code. |
| 27 | |
| 28 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 29 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 30 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 31 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 32 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE |
| 33 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 34 | */ |
| 35 | #include <tetris/sys/imgman.hh> |
| 36 | |
| 37 | namespace tetris::sys { |
| 38 | ImgMan &ImgMan::Get() { |
| 39 | static ImgMan instance; |
| 40 | return instance; |
| 41 | } |
| 42 | |
| 43 | Texture2D ImgMan::LoadTexture(const string &name, const u8 *data, i32 dataSize) { |
| 44 | if (m_Textures.contains(name)) { |
| 45 | return m_Textures[name]; |
| 46 | } |
| 47 | |
| 48 | string ext = ".png"; |
| 49 | if (name.find("jpg") != string::npos || name.find("jpeg") != string::npos) { |
| 50 | ext = ".jpg"; |
| 51 | } else if (name.find("png") != string::npos) { |
| 52 | ext = ".png"; |
| 53 | } |
| 54 | |
| 55 | Image img = LoadImageFromMemory(ext.c_str(), data, dataSize); |
| 56 | if (img.data == nullptr) { |
| 57 | TraceLog(LOG_WARNING, "テキスチャーの読み込みに失敗: %s", name.c_str()); |
| 58 | return Texture2D{}; |
| 59 | } |
| 60 | |
| 61 | Texture2D tx = LoadTextureFromImage(img); |
| 62 | UnloadImage(img); |
| 63 | |
| 64 | SetTextureFilter(tx, TEXTURE_FILTER_BILINEAR); |
| 65 | |
| 66 | m_Textures[name] = tx; |
| 67 | return tx; |
| 68 | } |
| 69 | |
| 70 | Texture2D ImgMan::GetTexture(const string &name) { |
| 71 | auto it = m_Textures.find(name); |
| 72 | if (it != m_Textures.end()) return it->second; |
| 73 | return Texture2D{}; |
| 74 | } |
| 75 | |
| 76 | void ImgMan::DrawBackground(Color tint) { |
| 77 | Texture2D tx = GetTexture(m_CurBg); |
| 78 | if (tx.id == 0) return; |
| 79 | |
| 80 | DrawTexturePro(tx, |
| 81 | { 0, 0, (f32)tx.width, (f32)tx.height }, |
| 82 | { 0, 0, (f32)GetScreenWidth(), (f32)GetScreenHeight() }, |
| 83 | { 0, 0 }, 0.f, tint); |
| 84 | } |
| 85 | |
| 86 | void ImgMan::UnloadAll() { |
| 87 | for (auto &[name, tx] : m_Textures) UnloadTexture(tx); |
| 88 | m_Textures.clear(); |
| 89 | } |
| 90 | |
| 91 | void ImgMan::SetBackground(const string &name) { |
| 92 | if (m_Textures.contains(name)) m_CurBg = name; |
| 93 | } |
| 94 | } // namespace tetris::sys |