• 0

C++/SDL Game Design Guidance


Question

I posted this over on gamedev.net but haven't had any hits yet, hoping some of the bright minds here can give me some guidance.

I have started working on writing a 2D Isometric style game. The idea is to have a persistant world (each tile is unique) and sent from the server where it is then generated (tile object) and then drawn when needed.

The way the game code works right now is that all objects have Draw() and Update() functions that are called every loop by it's parent "Screen" (Static objects, with their own controls/objects/references) Draw() and Update() functions, which in turn are called during the game loop. Basically:

While (!Exited)

ActiveScreen->Update(); -> In turn updating all child objects

ActiveScreen->Draw(); -> In turn drawing all the child objects

While our FPS > Max wait

Inside the Update() functions are things such as positioning, reloading certain objects I free to prevent leaks, checking sdl events (mouse movements, key presses, etc).

Then the Draw() functions determine if that object is visible on the screen, if it is marked visible, if the texture is valid, etc then it runs the SDL Functions to draw it (blit).

The screens Draw() function first clears the screen, runs all the draw functions for the objects that have been programmed then it refreshes the screen so that the changes are visible.

First question, is the above the right concept? I used things I had read/sdl tutorials/way XNA did things to put that bit together. If anyone has a better suggestion I am all ears.

Second question, even though I have the checks to ensure that things show on screen I notice that with a large tile array it slows right down even though it is only displaying a small portion, should I have an array that is filled on the update function that holds all the tile objects instead of looping through all of them at draw time? As each tile is it's own thing and done isometrically I need to redraw each visible tile every time so that they stack properly, so updating only 1 tile at a time isn't doable as it will end up overlapping in the wrong way.

I am sure I will have more questions as I go along. I am pretty experienced with C# and I understand C++ enough to make it work/work right however I am not that well versed in game logic as I do application programming for a living. I want to do this in C++ with SDL instead of using a pre-built solution (XNA,MonoGame,etc) as this will teach me a lot more than using things I am already comfortable with, and a lot of the logic is already handled in.

Link to comment
https://www.neowin.net/forum/topic/1156484-csdl-game-design-guidance/
Share on other sites

8 answers to this question

Recommended Posts

  • 0

I don't have any professional experience with game development, but I have often found it helpful to look at other's source code to see how things are done. I have learned a lot about various programming languages and frameworks that way.

Maybe it would help if you traced through Raven's Jedi Academy source. It is written in C++ and makes use of SDL, just like your game. You might also consider looking at the Ioquake3 source code because it also uses the Quake 3 engine. Effectively, the code is similar because they are both based on the same engine, but ioquake3 has many more features and bug-fixes since Quake 3 has been open-source for much longer.

  • 0

Thanks for those links :)

I am definitely not well versed in game programming, and haven't really had to control things 100%. With .NET 90% of the stuff is already done for me. The one suggestion I have received so far from gamedev was to use space partitioning (treat multiple objects as 1 group and treat them all the same).

This is what I posted in response.

Yea right now I do something like the following for drawing the tiles for example

In gamescreen.cpp's Draw() function

if (ActiveMap != NULL {

ActiveMap->Draw();

}

Active Map's Draw Function()

for (int i = ; i < Tiles.size(); i++) {

if (Tiles[i]->XDrawPos() > -140 && Tiles[i]->XDrawPos() < Client::ActiveWindow->Width &&

Tiles[i]->YDrawPos() > -140 && Tiles[i]->YDrawPos() < Client::ActiveWindow->Height) {

Tiles[i]->Draw();

}

}

Tiles Draw() Is basically just an SDL_Blit of the tile rect on the games sdl_surface at the X/Y Draw position.

I'm not entirely sure on how to grid it, unless I do x > X and x < Z = Group 1 and if pos > X and < Z then draw Group 1. Because I would essentially draw 4 grids each time (as you could be halfway between one and another). Doing this would put 90% of the processing as part of the world loading. However I plan on getting tile info from the server which means I will still need to do processing as I load a tile block.

The drawing of the tiles is where I notice the slow down, if I don't have it drawing tiles/large number of tiles it runs fine. Also, with the other visiblity I just use a boolean inside the object ie)

TextboxLogin->Visible = true/false

And in the draw I just do an

if (Visible) {

Draw it;

}

  • 0

A good way to get more performance is to only draw portions of the screen that have changed. Drawing only visible objects is another.

I currently have properties with X and Y position which is offset by the camera, to which I only draw the objects inside the view port, I also have a Visible boolean which I can specify to draw or not. I was considering redrawing just portions, but the way I have the tiles I have to redraw them all. An option I could do, is separate the tile and edge graphics and only draw the side (edge graphics) on the map edge, then only redraw specific tiles (if a character is overlapping it and moves/other sprite interacts). I could have an isdirty option or something.

  • 0

Unless you're rendering tens of thousands of sprites, if performance is an issue then your drawing code is just plain inefficient. Even crappy integrated graphics can render a lot of sprites without issues if done properly. I'm not familiar with SDL so I don't know how well it performs. Does it use OpenGL acceleration to draw 2d graphics? XNA has a SpriteBatch object that batches draw calls sorted by texture to optimize performance; perhaps there is some similar functionality in SDL - or you'll have to implement it yourself.

Your idea of having every object have a draw and update method is very similar to XNA's basic architecture, however feel free to break from this model as needed. Not every object should be drawable, and not every object should be updated every tick either. I personally tend to have only high-level classes (managers) have an update() method, and sometimes draw if they're responsible for drawing something (like the UI manager or terrain renderer). It's important to maintain a healthy level of separation between gameplay logic and rendering.

  • 0

Unless you're rendering tens of thousands of sprites, if performance is an issue then your drawing code is just plain inefficient. Even crappy integrated graphics can render a lot of sprites without issues if done properly. I'm not familiar with SDL so I don't know how well it performs. Does it use OpenGL acceleration to draw 2d graphics? XNA has a SpriteBatch object that batches draw calls sorted by texture to optimize performance; perhaps there is some similar functionality in SDL - or you'll have to implement it yourself.

Your idea of having every object have a draw and update method is very similar to XNA's basic architecture, however feel free to break from this model as needed. Not every object should be drawable, and not every object should be updated every tick either. I personally tend to have only high-level classes (managers) have an update() method, and sometimes draw if they're responsible for drawing something (like the UI manager or terrain renderer). It's important to maintain a healthy level of separation between gameplay logic and rendering.

I am not rendering that many, at most maybe.. 200 sprites (including the tiles). And the game slows right now. I am not sure how SDL Does it, but as far as I know it does have HWA. I am assuming my drawing code is inefficient but I am not sure how.

I tried to follow how I would do it in XNA (Update/Draw) functions. I will do the following on a draw:

Clear Screen

Draw Nameplates/Portraits

Draw World Tiles

Draw NPCS/Characters top -> bottom (proper layering)

Draw Any Text/Speech

Draw the GUI Controls

I use the update mostly for applying settings/determining if something should be visible or not. Pretty much everything has an update (in the way I am doing it), but the updates are more for applying the font/positioning the font for rendering.

Right now game logic is done primarily through the update procedures and with function calls.

The Rendering/Drawing is more or less the application of the updates. It is essentially a graphical snapshot of what the updates say is happening.

Also, on a side note the memory management is pretty good, using pointers to objects lets me have 3000+ tiles and use pretty much the exact same memory.

  • 0

Which version of SDL are you using? Are you positive you're using hardware acceleration? What resolution are you targeting?

I know from experience using Allegro there's two rendering paths [in Allegro]: software (very slow) and hardware acceleration (very fast). Allegro 4.x used software while Allegro 5.x uses hardware acceleration. I believe the same could be said for SDL, but I'm not experienced with SDL outside of using it just for OpenGL context creation.

  • 0

Which version of SDL are you using? Are you positive you're using hardware acceleration? What resolution are you targeting?

I know from experience using Allegro there's two rendering paths [in Allegro]: software (very slow) and hardware acceleration (very fast). Allegro 4.x used software while Allegro 5.x uses hardware acceleration. I believe the same could be said for SDL, but I'm not experienced with SDL outside of using it just for OpenGL context creation.

SDL 1.2 at first I think I was using SW (SW_SURFACE) read about using HW_SURFACE going to try it and see what it does. But as far as I know it does have HWA (which is the main reason I chose it).

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Interesting image choice... reminds me of the human centipede poster
    • Get $50 of aloSIM Mobile Data Traveler eSim credit for just $24.97 by Steven Parker Today's highlighted deal comes via our Apps + Software section of the Neowin Deals store, where you can save 50% off aloSIM Mobile Data Traveler Lifetime eSim Credit: Pay $24.97 for $50. Stay connected affordably in 120+ countries/regions with your own lifetime eSIM! An eSIM is a digital SIM card. It's basically just mobile data. Once it's activated on your device, it can connect you to data networks in other countries – giving you an internet connection with NO roaming charges. With aloSIM, you can load prepaid eSIM data packages onto your phone, tablet, or computer. Your lifetime eSIM never expires, so it's yours forever and there are never any monthly charges. You'll get $50 in eSIM data credit, which is almost always enough to cover all your data roaming needs for a full year. But if you run out of data, you can always top up your lifetime eSIM and stay connected internationally. Pay $24.97 for a lifetime eSIM with $50 in travel data credit Use your eSIM to join data networks in 120+ countries Install your lifetime eSIM on a compatible device to roam on local data networks Your lifetime eSIM never expires, and can be topped up with more data anytime Many data packages cost as little as $4.50 and last 7 days. Depending on the package you choose, the length of time varies. Good to know Length of access: lifetime For NEW customers only Instant digital redemption Once you add your $50 credit to your aloSim account you have up to 12-months to use it — after that your credit will expire When you pay for a data plan you also get a free phone number (via Hushed) for the same duration of your plan that was purchased - IE 7 day eSim plan gives you a free 7-day phone number Purchased coupon must be redeemed and used within 12 months This deal is not stackable (one offer per aloSIM account) A $4.50 data package will last 7 days The data DOES expire, and you WILL NOT have any leftover data for your next trip unless it takes place within the validity period. While the eSIM never expires, the actual data package is only valid for the length of time stated at purchase (i.e. seven days after activation, 30 days after activation, etc.) So if you buy a seven-day package and only use a tiny bit, that package is still going to expire after seven days. Access options: mobile (check compatibility) Max number of device(s): 1 Updates included Here's the deal: This aloSIM Mobile Data Traveler eSim $50 Credit normally costs ... $50, but it can be yours for just $24.97 for a limited time, a saving of $25 (50% off). For specifications, and license info please click the link below. Get this aloSIM Mobile Data Traveler eSim for just $24.97 (was $50) Although priced in U.S. dollars, this deal is available for digital purchase worldwide. Support queries If you have queries or need support for any of the Neowin Deals, please use the contact form here. Neowin Deals are managed and sold by StackCommerce who represent Neowin on an affiliate basis. Why we post these deals We post these because we earn commission on each sale so as not to rely solely on advertising, which many of our readers block. It all helps toward paying staff reporters, servers and hosting costs. So for those that keep moaning and complaining, be thankful we're still online for you to even do that. Other ways to support Neowin Whitelist Neowin by not blocking our ads Create a free member account to see fewer ads Make a donation to support our day to day running costs Subscribe to Neowin - for $14 a year, or $28 a year for an ad-free experience Disclosure: Neowin benefits from revenue of each sale made through our branded deals site powered by StackCommerce.
    • WordArt was cool. We now have color fonts as a substitute although Word only supports COLRv0 and COLRv1 (Fraud OS 11 only). The OpenType SVG color font format needs to be supported by Office. Adobe's apps support it
  • Recent Achievements

    • First Post
      DrWankel earned a badge
      First Post
    • Reacting Well
      DrWankel earned a badge
      Reacting Well
    • Week One Done
      Supreme Spray LV earned a badge
      Week One Done
    • One Month Later
      Genuinetonerink- Dubai earned a badge
      One Month Later
    • Week One Done
      Genuinetonerink- Dubai earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      498
    2. 2
      +Edouard
      158
    3. 3
      PsYcHoKiLLa
      90
    4. 4
      Steven P.
      74
    5. 5
      Michael Scrip
      72
  • Tell a friend

    Love Neowin? Tell a friend!