I am making a C++ sdl based game engine, where I am wrapping every SDL class, currently…
- User
I am making a C++ sdl based game engine, where I am wrapping every SDL class, currently i am doing the Window class, check and evaluate if what I have is good or not, or if I need to add something I missed:
Surface::Surface(const std::string& filePath) : surface(IMG_Load(filePath.c_str())) { if (surface == nullptr) { // ... error handling } }
Surface::Surface(const std::string& text, Font& font, int fontSize, const Color& color, bool antiAliasing = false) : surface(antiAliasing ? TTF_RenderUTF8_Blended(font.GetFont(fontSize), text.c_str(), { color.R, color.G, color.B, color.A }) : TTF_RenderUTF8_Solid(font.GetFont(fontSize), text.c_str(), { color.R, color.G, color.B, color.A })) { if (surface == nullptr) { // ... error handling } }
Surface::Surface(const std::string& text, Font& font, int fontSize, const Color& fgColor, const Color& bgColor) : surface(TTF_RenderUTF8_Shaded(font.GetFont(fontSize), text.c_str(), { fgColor.R, fgColor.G, fgColor.B, fgColor.A }, { bgColor.R, bgColor.G, bgColor.B, bgColor.A })) { if (surface == nullptr) { // ... error handling } }
Surface::~Surface() { if (surface != nullptr) { SDL_FreeSurface(surface); surface = nullptr; } }
Surface::Surface(Surface&& other) noexcept : surface(other.surface) { other.surface = nullptr; }
Surface& Surface::operator=(Surface&& other) noexcept { if (this != &other) { if (surface != nullptr) { SDL_FreeSurface(surface); } surface = other.surface; other.surface = nullptr; }
return *this;}
SDL_Surface* Surface::GetSurface() const { return surface; }
Point Surface::GetSize() const { return Point(surface->w, surface->h); }
BlendMode::Type Surface::GetBlendMode() const { SDL_BlendMode* sdlBlendMode = 0;
if (SDL_GetSurfaceBlendMode(surface, sdlBlendMode) != 0) { // ... error handling } return BlendMode::FromSDL(*sdlBlendMode);}
void Surface::SetBlendMode(BlendMode::Type blendMode) { SDL_BlendMode sdlBlendMode = BlendMode::ToSDL(blendMode); if (SDL_SetSurfaceBlendMode(surface, sdlBlendMode) != 0) { // ... error handling } }
Color Surface::GetColorMod() const { Uint8 r = 0; Uint8 g = 0; Uint8 b = 0; if (SDL_GetSurfaceColorMod(surface, &r, &g, &b) != 0) { // ... error handling }
return Color(r, g, b, 255);}
void Surface::SetColorMod(const Color& color) { if (SDL_SetSurfaceColorMod(surface, color.R, color.G, color.B) != 0) { // ... error handling } }
Uint8 Surface::GetAlphaMod() const { Uint8 a = 0;
if (SDL_GetSurfaceAlphaMod(surface, &a) != 0) { // ... error handling } return a;}
void Surface::SetAlphaMod(const Uint8& a) { if (SDL_SetSurfaceAlphaMod(surface, a) != 0) { // ... error handling } }
- Assistant
- User
- Assistant
- User
Ok to answer you:
- Yes error handling is done, just commented it for simplicity.
- I decided to opt out of the window flags as I don't want to complicate it more than it needs, with the current Set methods they should be enough, if not then I have a Set missing.
- Icon path is handled in the surface class, were it throws an exception if there is a problem when loading it.
- I got the variables in the first place for a way to avoid unnecesary queries, just checking the variable should be enough.
- I changed it now.
- See answer 4.
- I am going camel case and not following snake case like SDL.
- ChangeSize is already private.
- I decided to put the cursor visibility here, since was the first thing that came into my mind when thinking about a window, but maybe I could move it to the InputManager class or else.
- I already do in my code.
- The window position is set absolutely, I didn't plan on using relative positions or other similar like centered, maybe this is something I am missing.
- I don't understood you, I am using SDL_WINDOW_FULLSCREEN since is the method I first found.
- Assistant
- User
I didn't know that about the fullscreen mode, how can I integrate both ways to offer flexibility?
- Assistant
- User
but this will make fullscreen boolean not needed, right? with this mode I could know if it is windowed or not, just check if it is not FullScreen
- Assistant
Yes, you are correct. With the use of an enumeration that includes the
Windowedstate, you no longer need a separate boolean flag to track whether the window is in fullscreen mode or not. The enumeration can represent the entire window mode state on its own.You can eliminate the
fullScreenboolean from your class and rely exclusively on theFullscreenModeenum to determine the window's mode: - User
I prefixed everything like this Window::WindowMode::(rest), should I do this to be this explicit or is it not needed?
- Assistant
Using a nested enum class within your
Windowclass, as inWindow::WindowMode, can enhance the readability and scope management of your code by making it clear that the enum is specifically related to theWindowclass. Here's a breakdown of your options and when to use them:-
Nested enum class (
Window::WindowMode): If you are creating an enumeration that is specifically used in the context of the class and nowhere else, it is reasonable to nest it. This can help prevent naming conflicts and clarifies that the enum is an integral part of the class's interface.
-
- User
This is my current project structure, should I close the current Renderer release before moving to the Audio, Events, etc.?
BlendMode Camera Circle Color FLineSegment FLineStrip Font FPoint FRect GameScreen InputManager LineSegment LineStrip Point Rect Renderer ResourceManager ScreenManager Surface Texture Transform Vertex Window
- Assistant
- User
You made a few good points but here is more clarification:
- I can't assure it is stable as of now, it is in development since I first started with this project, so can't really go assuming it will stable at least not until i move to the official release and a demo.
- I haven't documented anything yet, I plan on leaving the documentation for a next commit.
- I haven't testing anything yet, I plan on doing this later when I finish writing it whole.
- I don't have any dependency but SDL only, so there is no other component I should be careful with. 5( There aren't any user yet, so far I am the only user.
- My roadmap is very flexible so I don't mind moving forward or staying a while if there is something missing.
Now considering these points, what class or functionality do you think I am missing for this releease?
- Assistant
- User
I am targeting 2D only, this is what I have:
- Basic drawing primitives (points, lines, rectangles, polygons). [done]
- Texture loading, handling, and rendering. [done]
- Transformation capabilities (translate, rotate, scale). [done]
- Camera and viewport management. [done]
- Basic lighting (for 3D). [N/A]
- Layering or sorting for 2D sprites. [TODO, Future commit]
- Support for rendering text. [done]
- Sprite batching to improve performance. [TODO: Future commit]
- Clipping or scissor tests to limit rendering to a particular area. [done]
- Basic shaders or programmable pipeline support (for more advanced 2D or 3D rendering). [Not done, don't know]
- Assistant
- User
OK good points this is my reply:
- Implement Layering and Sorting: I will do this in a next commit, when I am doing the Map class, I plan on adding the layers to the Map, when I go to higher level classes like: Sprite, Animation, SpriteSheet, etc. later. Maybe this is a bad idea but I haven't found anything better considering the layers I pictured them as tightly related to the Map.
- Implement Sprite Batching: Can't do this right now, my Renderer is immediate, I plan on descentralicing the Renderer in the next commit, moving all the rendering to the appropiate classes.
- Test with a Demo Project: I will do this soon, but I can't work on the engine and the demo at the same time.
- I will start the documentation after descentralizing the renderer, which will be in the next commit.
- I can't test right now that's why I am planning on commiting the changes but not releasing it yet.
- I will.
- Assistant
- User
Wait I found out I haven't implemented VertexSolid yet, I think that is a class I am missing, should I make it first before closing the commit?
- Assistant
The decision to include a
VertexSolidclass before closing the commit depends on the specific requirements of your renderer and the projects you plan to support with your game engine. - User
OK I will make it but how can I render them with my Renderer? My current vertex rending is like this:
void Renderer::RenderGeometry(const std::vector& vertices, const std::vector& indices, const Texture& texture) { RenderSDLGeometry(vertices, texture.GetTexture(), indices.data(), static_cast(indices.size())); }
although I think I already support the rendering of a vertex simple since I pass the nullptr to the texture in an overload:
void Renderer::RenderGeometry(const std::vector& vertices, const std::vector& indices) { RenderSDLGeometry(vertices, nullptr, indices.data(), static_cast(indices.size())); }
- Assistant
- User
Sorry forgot to paste the render sdl geometry class:
RenderSDLGeometry(const std::vector& vertices, SDL_Texture* texture, const int* indices, int indicesCount) { std::vector<SDL_Vertex> sdlVertices; VerticesToSDLVector(vertices, sdlVertices);
if (SDL_RenderGeometry(renderer, texture, sdlVertices.data(), static_cast<int>(sdlVertices.size()), indices, indicesCount) != 0) { std::string errorMsg = "ERROR: Unable to render vertices: " + std::string(SDL_GetError()); SDL_LogError(SDL_LOG_CATEGORY_ERROR, errorMsg.c_str()); throw std::runtime_error(errorMsg); }}
- Assistant
- User
But I can already simulate the VertexSimple with my Renderer methods, I could skip this and avoid creating this new class, or not?
- Assistant
If your current
RendererandVertexclasses are sufficient for simulating solid color rendering without the need for textures, then you indeed might not need a separateVertexSolidclass. Having fewer classes can simplify your engine and make it easier to maintain and understand.Consider the following points to determine if you can skip creating :