Jump to content
Sign in to follow this  
Oddly

C/C++ How would you refactor this?

Recommended Posts

https://gitlab.com/salemgd/salem2d/blob/feature/game_world/src/system/objects/GameWorld.cpp

Got this file, the code is very C Like written in C++. I feel like there's a lot of repetition in it, but don't know how I would split it up, looking for suggestions. Checkout the Update function and the DrawMask and DrawFringe functions.

Share this post


Link to post
Share on other sites

Why do you have identical shift map code 4 times with just some index's changed? I dont want to read the math there because im lazy but im sure there is a way to do it programatically.

 

Also if your to lazy like I am, just make a function take a matrix and pass in the index's. 

Share this post


Link to post
Share on other sites
7 minutes ago, Mighty Professional said:

Why do you have identical shift map code 4 times with just some index's changed? I dont want to read the math there because im lazy but im sure there is a way to do it programatically.

 

Also if your to lazy like I am, just make a function take a matrix and pass in the index's. 

That's the point of the post, How can I refactor that?

Each function does different operations for the indexes. It's just a matrix, and one if statement shifts the maps in the current block down, so it clears the bottom 3 maps, moves row 1 to row 2 and row 0 to row 1 than loads row 1, while another function does almost the same thing but shifts maps left, so clear the right side, move col 1 to col 2 and col 0 to col 1 than load col 0. The code seems repetitive but each if statement is doing something else. I wanted to see if I could find a way to move the contents of each if statement into a single function, but wasn't sure how.

Share this post


Link to post
Share on other sites
7 minutes ago, Mighty Professional said:

Where is get_map_left and get_map_right etc declared? 

It's a function in the Map's header file

SALEM_API std::string& get_map_right() { return m_map_right; }

m_map_right gets loaded from the map's json file.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...