Register    Login    Forum    Search    FAQ

Board index » Everything




Post new topic Reply to topic  [ 14 posts ]  Go to page 1, 2  Next
Author Message
 Post Posted: December 19th, 2017, 6:00 am 
User avatar

Joined: January 15th, 2017, 9:16 pm
Posts: 28
Location: Ontario
I feel like you should create multiple sections of the forum, so I don't have to feel bad about clogging it up with my own content when I submit a game or ask a question. I can just put it in it's own little "quick questions" place. But that's besides the point.

In case my code is hard to read, what I am doing is drawing an angry eye (a right angled triangle) on one of my characters, and if his direction is 1(right) then it draws it normally. But if his direction is 2(left), then it mirrors the image.
Code:
   ex2 = 0;
   ey2 = 0;

   if (d == 1)
   {
      while (ey2 <= 6)
      {
         gfx.PutPixel(x + (w / 2) + (ex + ex2), y + ey + ey2, 0, 0, 0);
         ex2++;
         if (ex2 > ey2)
         {
            ex2 = 0;
            ey2++;
         }
      }
   }
   else if (d == 2)
   {
      while (ey2 <= 6)
      {
         gfx.PutPixel(x + (w / 2) + -(ex + ex2), y + ey + ey2, 0, 0, 0);
         ex2++;
         if (ex2 > ey2)
         {
            ex2 = 0;
            ey2++;
         }
      }
   }

What I am trying to do is avoid just duplicating the code, and instead make everything work with a single block. The only difference right now between the two is the little negative symbol in front of the (ex + ex2).

Of course, I could just merge ex and ex2 into a single unit and say "ex = -ex", but I want to keep both variables for convenience reasons. Can this be done?


Top 
 Profile  
Reply with quote  
 Post Posted: December 19th, 2017, 6:17 am 
 

Joined: November 30th, 2013, 7:40 pm
Posts: 656
Location: Merville, British Columbia Canada
you might try this
Code:
int result ;
// if d == 1 then result = 1 otherwise result = -1
 d == 1 ? result = 1 : result = -1;
// result * (ex + ex2) will make it positive or negative
while (ey2 <= 6)
      {
         gfx.PutPixel(x + (w / 2) + (result * (ex + ex2)), y + ey + ey2, 0, 0, 0);
         ex2++;
         if (ex2 > ey2)
         {
            ex2 = 0;
            ey2++;
         }
      }

_________________
Curiosity killed the cat, satisfaction brought him back


Top 
 Profile  
Reply with quote  
 Post Posted: December 19th, 2017, 6:21 am 
User avatar

Joined: January 15th, 2017, 9:16 pm
Posts: 28
Location: Ontario
Ahhh, that would work! I'm upset I didn't think of that myself. Thank you very much.


Top 
 Profile  
Reply with quote  
 Post Posted: December 19th, 2017, 6:32 am 
 

Joined: November 30th, 2013, 7:40 pm
Posts: 656
Location: Merville, British Columbia Canada
No problem

_________________
Curiosity killed the cat, satisfaction brought him back


Top 
 Profile  
Reply with quote  
 Post Posted: December 19th, 2017, 3:16 pm 
User avatar

Joined: January 15th, 2017, 9:16 pm
Posts: 28
Location: Ontario
Can I ask you a question MrGodin? (or anyone else reading this)

I try to avoid creating unnecessary ints in my project. By the end of my creations I always have dozens of single purpose ints like this(I realise your "result" int can be created and destroyed in the same function, but I mean in general) lying around everywhere. Is there a way around this, or do I just need to pull up my underwear, and accept that this is how it's going to be?


Top 
 Profile  
Reply with quote  
 Post Posted: December 19th, 2017, 5:03 pm 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2970
Location: Oklahoma, United States
Depending on what you are referring to, there are ways to avoid so may loose variables.

1) Throw away variables should not be global or stored in a class or struct. Instantiate them inside of functions like the int result in MrGodin's example, though I prefer to initialize on the same line as declare. const int return = d == 1 ? 1 : -1;
For one, this allows me to make my temporary variables const ( performance optimization, safety check ), and two I don't have to remember to initialize them before using them ( compiler error if used before initializing ).

2) Use for loops instead of while loops when possible. For loops allow you to initialize a variable inside the loops declaration and won't live past the loop, unlike the while loop where you must declare before the while and they live after the while.

3) Use the fact that functions that return values can be used directly instead of storing to a named temporary. const int result = 5 + Add(7,3); // result = 15;
The result of Add(7,3) is stored in memory, then added to 5 and that result is stored in const int result.

If you only need it inside a function, create it inside the function.
If you only need it inside a loop, create it inside a loop.
You can even use curly braces {} to limit the scope of the ints, so you can reinstantiate them with the same name.
Code:

// i only lives until end of loop
for( int i = 0; i < numPersons; ++i )
{
   
    {
        // Person and name live only in this block
        const Person& person = boy_name_list[i];
        const char* name = person.GetName();
        std::cout << "The boy at index " << i  << "is named: " << name << std::endl;
        std::cout << "They are: " << person.GetAge() << " years old." << std::endl;
    }
    // Person and name are destroyed

    {
        // Person and name live only in this block
        const Person& person = girl_name_list[i];
        const char* name = person.GetName();
        std::cout << "The girl at index " << i << "is named: " << name << std::endl;
        std::cout << "They are: " << person.GetAge() << " years old." << std::endl;
    }
    // Person and name are destroyed
}
// i is destroyed


I prefer to be a little verbose in my coding. It allows me to step through the code mentally and literally while debugging and see what the values are as I go. Plus, if you have functions returning values to functions as parameters, then you have to step through each of those functions before you get to the top level function and by that time, you've forgotten what the values were that were being passed in.

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
 Post Posted: December 19th, 2017, 6:33 pm 
User avatar

Joined: January 15th, 2017, 9:16 pm
Posts: 28
Location: Ontario
That is definitely some good advice. I will go back and alter my whole project. Thanks a bunch papa.

As far as taking notes goes (if that's what you meant by verbose), I do it sometimes, but I almost always forget, or it seems pointless at the time. I will work on it.


Top 
 Profile  
Reply with quote  
 Post Posted: December 19th, 2017, 7:02 pm 
 
I May Have Read You Post Wrong, But If You Meant This Shit

Code:
/***********************/
/****user variables*****/
int new_int = 0;
int int2 = 0;
int int3_the_revenge = 0;
int int4_Another_sequel = 0;
int wow_we're_at_5 = 0;
int Another_1 = 0;
int doubt_you_made_it = 0;
int This_Far = 0;
int Still_going = 0;
int y = 0;
int x = 0;
int x1 = 0;
int y1 = 0;
int I_got_bored = 0;
/*********************/

Then You Can Do This I Guess:
Code:
/***********************/
/****user variables*****/
int new_int, int2, int3_the_revenge, int4_Another_sequel, wow_we're_at_5, Another_1 , doubt_you_made_it, This_Far , Still_going , y, x,  x1, y1, I_got_bored;
/**********************/


Top 
  
Reply with quote  
 Post Posted: December 19th, 2017, 9:53 pm 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2970
Location: Oklahoma, United States
Radical wrote:
That is definitely some good advice. I will go back and alter my whole project. Thanks a bunch papa.

As far as taking notes goes (if that's what you meant by verbose), I do it sometimes, but I almost always forget, or it seems pointless at the time. I will work on it.


Verbose for me is:
Code:
// i only lives until end of loop
for( int i = 0; i < numPersons; ++i )
{   
    {
        // Person and name live only in this block
        const Person& person = boy_name_list[i];
        const char* name = person.GetName();
        std::cout << "The boy at index " << i  << "is named: " << name << std::endl;
        std::cout << "They are: " << person.GetAge() << " years old." << std::endl;
    }
    // Person and name are destroyed

    {
        // Person and name live only in this block
        const Person& person = girl_name_list[i];
        const char* name = person.GetName();
        std::cout << "The girl at index " << i << "is named: " << name << std::endl;
        std::cout << "They are: " << person.GetAge() << " years old." << std::endl;
    }
    // Person and name are destroyed
}
// i is destroyed


Not verbose:
Code:
for( int i = 0; i < numPersons; ++i )
{
        std::cout << "The boy at index " << i  << "is named: " << boy_name_list[i].GetName() << std::endl;
        std::cout << "They are: " << boy_name_list[i].GetAge() << " years old." << std::endl;
        std::cout << "The girl at index " << i << "is named: " << girl_name_list[i].GetName() << std::endl;
        std::cout << "They are: " << girl_name_list[i].GetAge() << " years old." << std::endl;
}
// i is destroyed


Yeah, I meant the code being verbose, I probably used the word incorrectly. Anyway, I prefer these little steps of assigning results to intermediate temporary variables for the reasons I mentioned in the last post. As far as comments go, I really only use them when I think I might forget what the code does in a few weeks.

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
 Post Posted: December 19th, 2017, 10:03 pm 
User avatar

Joined: January 15th, 2017, 9:16 pm
Posts: 28
Location: Ontario
Ah I see, so you think it's worth it to make less optimal code in exchange for readability? Honestly I prefer to code that way, I just have been trying to do it as little as possible because it felt sloppy to me.

I mean, I guess I don't really know how much of a hit it would make to the frame rate. But. for example, people talk about const making things more speedy and optimized, so I figured shorthand hard-to-read code would do the same.

@goldengamesTM lol I guess I could


Top 
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
 
Post new topic Reply to topic  [ 14 posts ]  Go to page 1, 2  Next

Board index » Everything


 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
cron