I am making a C++ SDL based game engine, so far I have done several key parts like the…
- User
I am making a C++ SDL based game engine, so far I have done several key parts like the ResourceManager, InputManager, ScreenManager, and currenly finishing the Renderer, and its related classes Surface, Texture, Rect, Point, Line, etc. What I am doing is wrapping the original SDL methods while also adding my own methods like for example the Circle class. I need help with finishing the Renderer class.
I am at this step, doing the Viewport and the camera. I have done the SetViewport method which receives a Rect, but now I am torn apart into adding a Camera class since I already have a Transform struct, with same methods.
The original plan was this:
class Camera { public: FPoint position; float zoom; float rotation;
Camera() : position(0, 0), zoom(1), rotation(0) {} void SetPosition(float x, float y) { position.x = x; position.y = y; } void SetZoom(float zoomLevel) { zoom = zoomLevel; } void SetRotation(float rotationDegrees) { rotation = rotationDegrees; } // Other camera methods…};
class Renderer { public: void SetCamera(const Camera& camera) { // Calculate the transformation matrix based on camera position, zoom, and rotation, if necessary // To apply rotation you would also adjust the rendering calls accordingly }
But now I ended up having a Transform struct: struct Transform { enum class Flip { None, Horizontal, Vertical, Both };
FPoint scale; float rotation; Transform::Flip flip; Transform();};
Should I add the Camera class, considering I am currently doing the Renderer and Camera class is an high level compared to the Renderer, or I should just add a Transform struct to an overload of SetViewport like this: void SetViewport(const Rect& viewport) const; // original void SetViewport(const Transform& transform[or camera?]) const; // overload, similar to SetCamera
- Assistant
- User
I changed the code a little now it is like this: but I don't know if I should store the Window information when creating the Renderer, maybe there is a way to get the Window without storing these sizes:
class Renderer { public: Renderer::Renderer(Window window, bool vsync) { renderer = SDL_CreateRenderer(window.GetWindow(), -1, 0); //rest of the code } void SetCamera(const Camera& camera) { // Calculate the transformation matrix based on camera position, zoom, and rotation, if necessary
// In SDL, you might do something like this: SDL_Rect viewport; viewport.x = static_cast<int>(camera.position.x); viewport.y = static_cast<int>(camera.position.y); viewport.w = static_cast<int>(windowWidth / camera.zoom); // Assuming you store window width and height somewhere viewport.h = static_cast<int>(windowHeight / camera.zoom); SDL_RenderSetViewport(internalSDLRenderer, &viewport); // TODO: Apply rotation you would also adjust the rendering calls accordingly // Help me finish this code } // … The rest of Renderer methods …private: SDL_Renderer* renderer; int windowWidth, windowHeight; // Should I store these or calculate, I don't need them by any other methods };
- Assistant
- User
Two things,
- why did you change: viewport.x = static_cast(camera.position.x); to viewport.x = static_cast(camera.position.x - (windowWidth * 0.5f) / camera.zoom); ?
- I already have the RenderTexture with the transformation, can we use that to render the rotation or not?
void Renderer::RenderTexture(const Texture& texture, const Rect* srcRect, const Rect* dstRect, const Transform& transform) { SDL_Rect dstRectTransformed = dstRect ? dstRect->GetRect() : SDL_Rect{0, 0, texture.GetSize().GetX(), texture.GetSize().GetY()};
dstRectTransformed.w = static_cast<int>(dstRectTransformed.w * transform.scale.GetX()); dstRectTransformed.h = static_cast<int>(dstRectTransformed.h * transform.scale.GetY()); SDL_RendererFlip sdlFlip; switch (transform.flip) { case Transform::Flip::Horizontal: sdlFlip = SDL_FLIP_HORIZONTAL; break; case Transform::Flip::Vertical: sdlFlip = SDL_FLIP_VERTICAL; break; case Transform::Flip::Both: sdlFlip = static_cast<SDL_RendererFlip>(SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL); break; case Transform::Flip::None: default: sdlFlip = SDL_FLIP_NONE; break; } SDL_RenderCopyEx(renderer, texture.GetTexture(), srcRect ? &srcRect->GetRect() : nullptr, &dstRectTransformed, static_cast<double>(transform.rotation), nullptr, sdlFlip); - Assistant
- User
I think I will keep the rotation out of the camera, since I don't plan to overcomplicate the engine, if I ever need to rotate I can just rotate the entire scene and each object inside instead of the camera, I don't plan on doing advance rendering techniques since this is a 2D game engine and will be mainly for RPG games
- Assistant
- User
OK we finished the Viewport and Camera stage, now the next part is this: 9. Batching and Performance: Design your Renderer to support efficient rendering by batching draw calls together where possible.
What does this mean? What do I have to do in this point?
- Assistant
- User
My current Renderer implementation is an immediate one, which Render everything as soon as the caller needs them. Each of the rendering methods like RenderPoint, RenderLine, RenderVertices, etc. Do the Rendering instantly, so I haven't implemented anything yet in this regards of batching.
- Do I really need it if my Renderer is like this, with this immediate design I have right now?
- If I have to implement this, do I have to modify each Rendering function I have to accomodate this?
- I don't know if this technique is related to a 2D game engine, is it?
- Is this the simplest way to do it? If so how will I have to change every rendering functions I have?
- SDL_RenderCopy
- SDL_RenderPresent
- SDL_RenderDrawRect
- SDL_RenderFillRect
- SDL_RenderDrawPoint
- etc.
- Assistant
- User
OK Since this is a big change to my project I will add this in my TO-DO list and move to a different thing, since for this I will have to change the entire design, so I will do this later when performance is more needed. I haven't even finished a single project so I first need to finish an small engine release so I can work from there.
- Assistant
That sounds like a prudent plan. It's often a good strategy to first build a minimum viable product (MVP) that meets your immediate needs and then iterate on it as you go. This way, you can have a working engine sooner, which you can use to develop actual games. If and when you experience performance bottlenecks, you can come back to the idea of batching.
Prioritization is key in software development, especially in game development where there are so many possible features and optimizations to consider. For now, focusing on completing the essential features of your engine and having a base to work from is important. This allows you to test your designs in practice, which is a valuable learning experience and can inform which optimizations, like batching, will actually be most beneficial for your projects.
When you do circle back to add optimizations like draw call batching, you'll likely have a much better understanding of your engine's actual performance needs, and you can implement solutions that are tailored to the use cases you've encountered in your games.
Good luck with your engine development, and enjoy the process! Remember, game engines evolve over time, and it's entirely okay to address issues like performance when they become relevant.
- User
- Assistant
- User
- I already do since Renderer has a destructor, so it gets destroyed in it.
- I already have error handling in most of my functions.
- Don't know what do you mean by clipping and why do I need it? How can Implement it?
- Layers and z-order I plan on doing that in future, probably when I do to the Map class, I don't plan on adding the layers to the renderer itself.
- I already have the ResourceManager with takes care of surfaces and fonts but not textures, since textures are created and used and not re-used like the surfaces.
- I don't know about shaders but I plan on supporting them in the late future, probably after I finish the entire engine, since I don't care much about GPU right now.
- I will keep that in mind but first I need to be able to run something.
- I haven't done any documentation, but I plan on adding that in the late future.
- Assistant
- Clipping: Clipping in rendering is a technique used to define a region in which rendering can take place. Anything drawn outside this region is not displayed. Clipping is useful for UI elements where you need to clip content to a window or scrollable area, or in games for rendering only a portion of a larger map.
- User
I have a question, If I expose a public method in a class of my engine, that uses another library for example: SDL_Renderer* Renderer::GetRenderer() const; In the user of my library even thought they won't use this function, do they have to include the SDL library just to use my own library or if they don't use this method there won't be a need to include it?
- Assistant
- User
In my case I made these SDL method public because I use them my own other classes, but I doesn't want to be public to the library users. But if you say they won't don't need to include the SDL library if they don't use it by default, then I won't have any problems right?
- Assistant
If you are using SDL methods within your own classes inside your library and exposing the SDL functionality through utility or helper methods, users of your library will not need to include SDL headers or link against SDL directly as long as they do not use any of the SDL-specific functionality themselves.
- User
explain this - Library Linking: Even though your library users don’t need to include SDL headers, your compiled library will still depend on SDL. This means that when users link against your library, they must also link against SDL, whether using dynamic or static linking. This is because your library’s implementation relies on SDL.? I will compile my library into a single file or maybe multiple files but my library will incliude SDL why does the user have to include them too?
- Assistant
- User
Which way is better? Static is a single file compared to several but a big one vs several smaller files, but I would have to include these files in my release zip file because I don't want the user to be installing SDL on their own. My current .so compiled file size is 3.42MB I think this doesn't include the SDL right? If it did it would be much bigger than this.
- Assistant
- User
I did the ldd but some extra libraries appeared, what are these for? Do I need to include those in my distribution in the future? linux-vdso.so.1 (0x00007fff847aa000) libSDL2-2.0.so.0 => /lib64/libSDL2-2.0.so.0 (0x00007f58d9e6a000) libm.so.6 => /lib64/libm.so.6 (0x00007f58d9d83000) libc.so.6 => /lib64/libc.so.6 (0x00007f58d9a00000) /lib64/ld-linux-x86-64.so.2 (0x00007f58da111000) libasound.so.2 => /lib64/libasound.so.2 (0x00007f58d9c77000)
- Assistant