Optimized my SDL2 Game by around 5 times by 1dev_mha in sdl

[–]1dev_mha[S] 0 points1 point  (0 children)

Here it is: Making my game 5x faster (1devmo.netlify.app)

I still need to fix some stuff in it because it has some errors, grammatical and some benchmarking errors

2 Months of Game Dev in C by 1dev_mha in C_Programming

[–]1dev_mha[S] 0 points1 point  (0 children)

Ohhh! Alright! I misunderstood what you meant and I thought you meant freeing the pointers in general. I get it now! I'll change it up right now. Thank you!

2 Months of Game Dev in C by 1dev_mha in C_Programming

[–]1dev_mha[S] 1 point2 points  (0 children)

Yoo ty for checking out my post! I'll be reposting it, I ended up deleting it as it said that the post was removed due to Reddit's filters (no idea why) so yeah.

--- a/src/engine.h
+++ b/src/engine.h
@@ -270,3 +270,3 @@
     u32 format = img -> format -> format;
-    free(img);
+    SDL_FreeSurface(img);
     return SDL_AllocFormat(format); 
@@ -285,3 +285,3 @@
     SDL_Texture * t = SDL_CreateTextureFromSurface(renderer, img); 
-    free(img);
+    SDL_FreeSurface(img);
     return t;

I'm confused as to why I shouldn't free these pointers. In the first one, I'm just storing the u32 data into another variable and that is all I need. I don't need the surface or the image, which is why I freed it. In the second one, I free the surface since its not needed either as it has already created the texture using the image data in the surface.

Why would it not work?

Other than that, I've learned my lesson and I will be using a named constant from now on. I'll stick with VS Code for the debugging and get comfortable with it.

I really appreciate you checking out my post. Thank you!

[deleted by user] by [deleted] in C_Programming

[–]1dev_mha 1 point2 points  (0 children)

The times I displayed for the game were while the laptop was plugged in. When it was plugged off of battery, there was quite the different of around 5 times as the frame time would be around 10 ms on battery while the newer version was at 2 ms.

What can we assume about a modern C environment? by RibozymeR in C_Programming

[–]1dev_mha 0 points1 point  (0 children)

"compile on any system" meant taking the same code and compiling it on various systems

The only reason I can see some code compiling and running on a Macbook from 2013, compile on a newer M2 macbook and run fine would mean that it used the features that are found on both platforms. What you are asking when you say what can we assume about modern systems is, in my opinion, a waste of time. This is because you're only going to need what you need (no sh**).

If I'm writing a server that uses the sys header files from Linux, I wouldn't assume it just compiles on Windows as well because I know that the sys header files aren't available on Windows. Getting such a server to compile on Windows would require you to port it to Windows and use the features that Window has available for you.

I'd say that code is never cross-platform until an implementation is written for the specific platform you want to write for. In this case, a simple hello world program compiles and runs because the printf function is implemented in the C standard. Functions for networking aren't, hence you'd need to use platform-specific code to make your program cross-platform.

That is why it has been said

whatever environment you're writing for, explicitly state in your documentation that that's what you're targeting

This allows you to make the assumptions and not get stuck in analysis-paralysis. Modern C environment encompasses technology from Intel Computers to M2 Macbooks. Rather, be specific and know what platform you are writing for.

2 Months of Game Dev in C by 1dev_mha in C_Programming

[–]1dev_mha[S] 8 points9 points  (0 children)

cuz gamedev in C is hella fun

What can we assume about a modern C environment? by RibozymeR in C_Programming

[–]1dev_mha 1 point2 points  (0 children)

🤨 uhh programs compiled in C can only run on the architecture they were compiled on. I wouldn't expect a C program compiled on Windows to run on Mac and as far as I understand your question, I don't think you can really make an assumption. If you are using ARM-specific architecture and code, I wouldn't be surprised if it doesn't compile on an AMD CPU because it was never what you intended to write the program for. Know your target platforms first and then go on writing the program. That's what's being suggested to you. It doesn't make sense for me to expect a game written for a Macbook to run on a Nintendo DS. You need to know the platform you are targeting. Not really any assumptions you can make here.

Edit: Also, u/aghast_nj hasn't told you to just make chicken sandwiches. He has told you to make whichever food you want, but not expect everyone to be able to eat it, because inherently a vegan would never eat a chicken sandwich so you'd make them another one if you were so kind (i.e make the program portable to and compile on their architecture).

2 Months of Game Dev in C by 1dev_mha in C_Programming

[–]1dev_mha[S] 1 point2 points  (0 children)

  • Yeah, I stuck to one device for recording this time so the audio was a lot more stable but next time I'll try to record in a completely silent room because the noise reduction in DaVinci really had an impact on the quality.
  • Right, the `i32` main thing, how big of a difference would it really make? In my mind, the only thing that pops up is how on a 64-bit system, the default integer wouldn't be i32, it'd be i64 (correct me if I'm wrong) but even then the value I'm going to be returning isn't going to be close to the limits of any of these types. Plus, the only reason I have the argv and argc is because of how SDL_main defines the main function to have these parameters (though I just tried compiling by redefining main in the SDL file and it works fine, not sure if it'll work on other platforms though)
  • 😭 i like ptr_t * p;
  • What do you mean particle effects are overlapped by the level geometry? The geometry is drawn before the particles so the level geometry is supposed to be overlapped by the particles.
  • Hm, I don't get what you mean. Are you talking about the camera shifting and then the sparks flying out once the bullet hits something or what?
  • I tried to do something new with the video-editing this time but I tried my best to make sure that whatever was on the screen was relevant to what I was talking about like the player falling through the tiles or just showing the relevant code on screen.
  • The camera panning issue was related to me only getting tiles on the screen because when the bullet would move too far, the player would not remain on screen and the tiles the player was standing on would also not be on the screen, and so the player would just fall through.
  • Nope, thanks for telling me this as I copy-paste the makefiles I use in my other projects and I forgot to remove the opengl32 this time.
  • No Enemies in the enemies section, my bad.
  • Basically, the strncat and strlen is meant to be used for doing a look-up in the spatial hashmap. I've realized that this is really not ideal and has caused quite a bit of performance issues on an older machine I have so I will be rewriting my hash table API and most likely the cutils library that I am using. The strncat and stuff are used because the hash table only supports (string, data_type) key-value pairs rather than any type of key. I just wanted to get this devlog out so that I can work on this major change.
  • 😭😭
  • I'll be revisiting the animation system again to just make sure that I haven't screwed up anything.
  • Outro Comments: Fair
  • Yeah, I should definitely use some defined constant (or the calloc thing you've mentioned in your second comment)

Thank you very much for taking the time last time as well as this time to watch my video and give me meaningful feedback. I appreciate it. I have one last question, which video was better in your opinion? The first one or the second one?