FifeGUI 0.3.0
A C++ GUI library designed for games.
backends/opengl/imageloader.hpp
1// SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause
2// SPDX-FileCopyrightText: 2004 - 2008 Olof Naessén and Per Larsson
3// SPDX-FileCopyrightText: 2013 - 2026 Fifengine contributors
4
5#ifndef INCLUDE_FIFECHAN_BACKENDS_OPENGL_SDLIMAGELOADER_HPP_
6#define INCLUDE_FIFECHAN_BACKENDS_OPENGL_SDLIMAGELOADER_HPP_
7
8// Standard library includes
9#include <algorithm>
10#include <span>
11#include <string>
12#include <vector>
13
14// Platform config include
15#include "fifechan/platform.hpp"
16
17// Project headers (subdirs before local)
18#include <fifechan/backends/opengl/image.hpp>
19#include <fifechan/backends/sdl3/imageloader.hpp>
20#include <fifechan/exception.hpp>
21
22namespace fcn::opengl
23{
27 class ImageLoader : public fcn::sdl3::ImageLoader
28 {
29 public:
30 fcn::Image* load(std::string const & filename, bool convertToDisplayFormat) override
31 {
32 SDL_Surface* loadedSurface = loadSDLSurface(filename);
33
34 if (loadedSurface == nullptr) {
35 std::string const msg = "Unable to load image file: " + filename;
36 throwException(msg);
37 }
38
39 SDL_Surface* surface = convertToStandardFormat(loadedSurface);
40 SDL_DestroySurface(loadedSurface);
41
42 if (surface == nullptr) {
43 std::string const msg = "Not enough memory to load: " + filename;
44 throwException(msg);
45 }
46
47 std::vector<unsigned int> packedPixels(
48 static_cast<size_t>(surface->w) * static_cast<size_t>(surface->h));
49
50 // Read pixels using SDL_ReadSurfacePixel to get correct R,G,B,A
51 for (int y = 0; y < surface->h; ++y) {
52 for (int x = 0; x < surface->w; ++x) {
53 unsigned char r = 0;
54 unsigned char g = 0;
55 unsigned char b = 0;
56 unsigned char a = 0;
57 SDL_ReadSurfacePixel(surface, x, y, &r, &g, &b, &a);
58 // Pack as R in bits 0-7, G in 8-15, B in 16-23, A in 24-31
59 size_t const idx =
60 static_cast<size_t>(x) + ((static_cast<size_t>(y) * static_cast<size_t>(surface->w)));
61 packedPixels.at(idx) = r | (g << 8) | (b << 16) | (a << 24);
62 }
63 }
64 fcn::Image* image =
65 new fcn::opengl::Image(packedPixels, surface->w, surface->h, convertToDisplayFormat);
66 SDL_DestroySurface(surface);
67
68 return image;
69 }
70 };
71} // namespace fcn::opengl
72
73#endif // INCLUDE_FIFECHAN_BACKENDS_OPENGL_SDLIMAGELOADER_HPP_
Abstract holder for image data.
Definition image.hpp:34
fcn::Image * load(std::string const &filename, bool convertToDisplayFormat) override
Loads an image, optionally converting it to display format.
OpenGL-specific implementation of Image.
SDL3-specific implementation of ImageLoader.
virtual SDL_Surface * convertToStandardFormat(SDL_Surface *surface)
Convert a surface to a standard internal format (internal).
virtual SDL_Surface * loadSDLSurface(std::string const &filename)
Load an SDL_Surface from disk (internal).
void throwException(std::string const &message, std::source_location location=std::source_location::current())
Throw an Exception capturing the current source location.