FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
graphic/C_surface.hpp
1/*
2 * Copyright 2024 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_GRAPHIC_C_SURFACE_HPP_INCLUDED
18#define _FGE_GRAPHIC_C_SURFACE_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21#include "FastEngine/C_rect.hpp"
22#include "FastEngine/C_vector.hpp"
23#include "FastEngine/graphic/C_color.hpp"
24#include "SDL_render.h"
25#include <cstdint>
26#include <filesystem>
27#include <optional>
28
29namespace fge
30{
31
32#ifdef FGE_DEF_SERVER
33namespace vulkan
34{
35
36class Context;
37
38} // namespace vulkan
39#endif
40
54class FGE_API Surface
55{
56public:
57 Surface();
58#ifdef FGE_DEF_SERVER
59 //TODO: this is here in order to be interchangeable easily with TextureImage
60 //when building for the server or client target.
61 //For future, I want to remove that and have a TextureImage server version
62 //instead of switching between Surface <-> TextureImage with the fge::TextureType alias
63 explicit Surface([[maybe_unused]] fge::vulkan::Context const& r) :
64 Surface()
65 {}
66#endif
67 Surface(int width, int height, fge::Color const& color = {0, 0, 0, 255});
68 Surface(Surface const& r);
69 Surface(Surface&& r) noexcept;
70 explicit Surface(SDL_Surface* newSurface);
71 ~Surface();
72
73 Surface& operator=(Surface const& r);
74 Surface& operator=(Surface&& r) noexcept;
75
79 void clear();
80
81 bool create(int width, int height, fge::Color const& color = {0, 0, 0, 255});
82 bool loadFromFile(std::filesystem::path const& filePath);
83 bool loadFromMemory(void const* data, std::size_t size);
84
91 bool saveToFile(std::filesystem::path const& filePath) const;
92
93 [[nodiscard]] fge::Vector2i getSize() const;
94
101 void createMaskFromColor(fge::Color const& color, uint8_t alpha = 0);
102
103 bool setPixel(int x, int y, fge::Color const& color);
104 [[nodiscard]] std::optional<fge::Color> getPixel(int x, int y) const;
105
106 void setCircle(int x, int y, unsigned int radius, fge::Color const& color);
107
108 void flipHorizontally();
109 void flipVertically();
110
121 bool blitSurface(Surface const& src, std::optional<SDL_Rect> const& srcRect, std::optional<SDL_Rect>& dstRect);
122
132 bool fillRect(std::optional<SDL_Rect> const& rect, fge::Color const& color);
133
144 bool addBorder(int borderSize, fge::Color const& color);
145
153 void set(SDL_Surface* surface);
159 [[nodiscard]] SDL_Surface* get() const;
160
167 [[nodiscard]] fge::Vector2f normalizeTextureCoords(fge::Vector2i const& coords) const;
174 [[nodiscard]] fge::RectFloat normalizeTextureRect(fge::RectInt const& rect) const;
175
176private:
177 SDL_Surface* g_surface;
178};
179
180} // namespace fge
181
182#endif //_FGE_GRAPHIC_C_SURFACE_HPP_INCLUDED
Definition C_color.hpp:35
Abstraction of SDL_Surface.
Definition graphic/C_surface.hpp:55
bool saveToFile(std::filesystem::path const &filePath) const
Save the surface to a PNG format file.
SDL_Surface * get() const
Get the SDL_Surface pointer.
bool blitSurface(Surface const &src, std::optional< SDL_Rect > const &srcRect, std::optional< SDL_Rect > &dstRect)
Blit a surface on this surface.
bool fillRect(std::optional< SDL_Rect > const &rect, fge::Color const &color)
Fill a rectangle section of the surface with a color.
void createMaskFromColor(fge::Color const &color, uint8_t alpha=0)
Create a transparent mask from a color.
fge::RectFloat normalizeTextureRect(fge::RectInt const &rect) const
Convert a pixel rectangle to a texture rectangle (0.0f to 1.0f)
void clear()
Destroy the surface.
fge::Vector2f normalizeTextureCoords(fge::Vector2i const &coords) const
Convert some pixel coordinates to texture coordinates (0.0f to 1.0f)
bool addBorder(int borderSize, fge::Color const &color)
Add a border to the surface with a specific color.
void set(SDL_Surface *surface)
Set a new surface.
Vulkan context.
Definition C_context.hpp:70