gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Learning C++ and need help

#154344 - Alphanoob - Tue Apr 15, 2008 12:58 am

Well, here it is. This is a continuation of a post I made in the DS dev forum that led to silent_code teaching me all of that wonderful magic that you guys call coding. He suggested I move it here, so I did. Last thing I asked:

when you say:
"int ruffle = waffle * baffle & waffle;"

what does the baffle & waffle part do?
oh, and how do you know whether a variable you declare is global or local?

#154345 - Dwedit - Tue Apr 15, 2008 1:08 am

Are you sure the punctuation in that line of code is correct? The & and * operators have completely different meanings in certain places.

If your line is transcribed correctly, then * is the multiplication operator, and & is the bitwise AND operator, but nobody uses the bitwise and operator without parentheses ( ) around it, which is why I'm not sure if you transcribed the line correctly. The makes of the C standard screwed up in operator precedence order, and gave bitwise AND a much lower priority than it should have in most actual uses.

Edit: finally found the post in question...
That section of code is garbage. Don't try to learn anything from it.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#154350 - Alphanoob - Tue Apr 15, 2008 1:55 am

rofl =), ok. I thought it was a pretty awesome code myself =). I wasn't sure if it was something basic or just silent_code having a little fun with his example code. as for my second question though; how do you make an integer global or local?

#154353 - Dwedit - Tue Apr 15, 2008 2:52 am

A variable is global as long it is declared outside a function.

Code:

int some_global=7;

int some_function()
{
  int some_local=8;
}

_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."


Last edited by Dwedit on Tue Apr 15, 2008 2:53 am; edited 1 time in total

#154354 - sajiimori - Tue Apr 15, 2008 2:52 am

Hey Alpha, I like helping people learn to program, but there's way too much material to learn exclusively by asking questions on a forum. It'll take you forever to learn that way, and a lot of people will wonder why you're not reading a book.

Here's one that I know is good:

http://www.amazon.com/dp/0672326965

If you can't find your answers in there, or if you have any questions about the material, please post again. :)

#154355 - Alphanoob - Tue Apr 15, 2008 2:57 am

Another question (yes, I have even more of them). Is there a simple way to draw lines that I am missing? The way I have been doing it involves putting something like this inside a while loop I am using.

while(1)

if (X <= 256)
{VRAM_A[(tileX)+(tileY)*SCREEN_WIDTH] = RGB15(0,0,31);
X ++;
}


This draws it pixel by pixel, and when trying to use it for anything that involves a position for the line that isn't set, it can be a real hassle. Any help to get me beyond this pixel by pixel approach would be much appreciated.

-Alphanoob

edit: for the record, I am using a book, but being able to ask questions on a forum helps me understand better if there is something that I dont fully understand. My book just teaches it for the PC, and a little extra info from all of you DS/GBA devvers is very helpful, as that is what I intend to use this knowledge for.

#154356 - tepples - Tue Apr 15, 2008 3:02 am

Alphanoob wrote:
when you say:
"int ruffle = waffle * baffle & waffle;"

what does the baffle & waffle part do?

When fully parenthesized per C++ precedence rules, it becomes this:
Code:
int ruffle = ((waffle * baffle) & waffle);

This statement creates a new local variable of type 'int', named 'ruffle'. (If there is already a global variable by the name 'ruffle', it becomes hidden for the rest of this code block.) Then it multiplies 'waffle' by 'baffle', bitwise ANDs the result with 'waffle', and then puts that value into the variable 'ruffle'.

Alphanoob wrote:
Another question (yes, I have even more of them). Is there a simple way to draw lines that I am missing?

Do you want horizontal lines, vertical lines, 45 degree diagonal lines, or arbitrary diagonal lines? In what video mode are you working and why?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#154357 - Alphanoob - Tue Apr 15, 2008 3:03 am

that last bit about the book is why I haven't asked questions that are far noobier than these. If you look at my posts on the original thread, you will see that I do know some programming, just not enough to write anything overly useful =)

#154358 - Alphanoob - Tue Apr 15, 2008 3:07 am

just horizontal and vertical lines for now, I am working in VRAM_A, and that is because I have no clue how to work in much of anything else so far. seriously, I am still pretty out of the loop on all of the different background modes and video modes, and have just been fiddling around with example code to the point where I understand how some of the basic commands work... I could post the source code that I am working on, but it is kinda long. It moves 4 pixels around, blocks you from leaving the screen, and if you touch a box in the middle, it displays some text. I am working on a kind of mock up tile graphics system right now, as I have no clue how to use the one provided in my lib.

#154359 - silent_code - Tue Apr 15, 2008 3:26 am

<OT>
@Dwedit:
man, you hurt my feelings with that.

Dwedit wrote:
Edit: finally found the post in question...
That section of code is garbage. Don't try to learn anything from it.

would you mind to clarify? please, with regards to the subject. thanks.

ps: i'm serious.
pps: only if that *nobody* doesn't know the propper operator orders. did you ever actually teach anyone yourself before?
</OT>


Last edited by silent_code on Tue Apr 15, 2008 3:43 am; edited 3 times in total

#154360 - Alphanoob - Tue Apr 15, 2008 3:32 am

hey silent. welcome to the party =). I think that DW might have missed the comment before your code that told what it was made to demonstrate. It made perfect sense, just added a little fun (and some creative jibberish =)).

#154362 - silent_code - Tue Apr 15, 2008 3:37 am

SO IF SOMEONE IS RESPONDING IN A WAY THAT CAN BE TAKEN AS OFFENDING, SHOULDN'T THAT PERSON READ THE WHOLE DAMN POST *FIRST*? HOW ABOUT HAVING A LITTLE FUN WHILE LEARNING, HUH???

or is this just some kind of kindergraten behaviour? sorry, but i'm off.

good luck AN.

ps: good i'm not resentful. but who cares anyway? tepples explained very well. that was what i have intended to write instead of this junk. sorry about the caps, though. bye.

#154363 - Alphanoob - Tue Apr 15, 2008 3:40 am

silent, wanna take a look at the confusing-tacular source code I am workin on? I think if I pm u it, I will cut out the intended stick person, as I just tested it and he ended up as 3 dots, lol. How bout it though? (you might get a kick out of it)

#154364 - silent_code - Tue Apr 15, 2008 3:48 am

although i have taken the sources of beginners and helped them in the past, i might have to disappoint you: i have some personal stuff (them doctors etc) to do the next few days.

but if you don't mind waiting some 48 hours or so, then feel free to pm me the code and i will have a look at it asap.

i'm going to be now. it's 4:47 am. and i'm angry. i won't be able to sleep. damn. sorry for the rant, i'm quite sleepy.

good night folks.

#154365 - Alphanoob - Tue Apr 15, 2008 3:51 am

night, and I will pm u the code, as I can wait a while. I think I will add some comments, otherwise it may make even less sense to you than it does to me. talk to u l8er =).

-Alphanoob

#154371 - sgeos - Tue Apr 15, 2008 8:10 am

silent_code wrote:
Dwedit wrote:
That section of code is garbage. Don't try to learn anything from it.

would you mind to clarify? please, with regards to the subject. thanks.

I did not read the post, but the variable names frightened me at first sight. They break the rule of "do not give variables similar names". They may break the rule of "give variables meaningful names", although that rule can only be taken so far when applied to theoretical code examples. Furthermore, I think the line is too busy. Just as every function should do one thing and do it, so should individual lines (in general). Personally, I would rewrite it is as:
Code:
int ruffle = waffle * baffle;
ruffle &= waffle;

The above is still missing something very important. The above code is not an idiom, and expecting anyone to know what you are doing just by looking at it is foolish. This is a case where the code needs to be commented.
Code:
// Set the ruffle setting for the waffle iron.  (or whatever you are actually doing)
int ruffle = waffle * baffle;

// Mask the setting - see spec for details.  (or whatever you are actually doing)
ruffle &= waffle;

If the variables had meaningful names, the code may have been self documenting instead of confusing.

For what it is worth, I could see using int "va = vb * (vc && vd);". This sets va to vb if both vc and vd are true, otherwise va is set to zero. The following C program, tested with gcc, illustrates a possible usage:
Code:
// start header file
typedef int BOOLEAN;
#define TRUE  1
#define FALSE 0
// end header file

#include <stdio.h>

BOOLEAN gHostileWorld;

int getDangerLevel(int pDangerLevel, BOOLEAN pHostileArea)
{
  // return supplied danger if both the area and the world are hostile
  int dangerLevel = pDangerLevel * (pHostileArea && gHostileWorld);
  return dangerLevel;
}

// this is a goofy debug function
void printDanger(int pDangerLevel, BOOLEAN pHostileArea, BOOLEAN pHostileWorld)
{
  int dangerLevel;

  // get danger level
  gHostileWorld   = pHostileWorld;
  dangerLevel = getDangerLevel(pDangerLevel, pHostileArea);
  // print results
  printf("DL: %d (%d)\nAH: %d\nWH: %d\n\n",
    dangerLevel, pDangerLevel, pHostileArea, gHostileWorld);
}

int main(void)
{
  printDanger(4, TRUE,  TRUE);
  printDanger(5, FALSE, TRUE);
  printDanger(6, TRUE,  FALSE);
  printDanger(7, FALSE, FALSE);
  return 0;
}

silent_code wrote:
pps: only if that *nobody* doesn't know the propper operator orders. did you ever actually teach anyone yourself before?

I follow the BEDMAS, parentheses around everything else coding convention. It works in all languages, including math. Code should be written for people and the exact order of operations left to compilers.

Dwedit wrote:
A variable is global as long it is declared outside a function.

Remember to always indicated that a variable is global. I prefix all globals with a lowercase g, as in:
Code:
int gGlobal;
BOOLEAN gGameOver;
setting_t gSettings;

-Brendan

#154381 - silent_code - Tue Apr 15, 2008 1:26 pm

oh boy, i better shut up with my crazy humor.

@sgeos: all you've written is perfectly right.

@general: but for a beginner and especially for a totally random example, that needs no meaning and sense, but to show when declaring is needed, my example was "good enough".

i had in mind to show him, step by step how to make it meaningful and usable by showing him something "bad", then turn it into something "good".
but right before i could make the next step, people freak out on me, without seeing that. yeah, i could have posted that, but honestly i didn't expect *that*.

somehow i think people don't get my idea, so i'll probably step back for a while.

bye, guys.

ps: with c++ you don't need globals, use public static members instead... or is that wrong?

EDIT: now i'm quite sure noone has a clue about what i'm writing here, so i'll simply stop commenting on it. this is getting more than silly.


Last edited by silent_code on Tue Apr 15, 2008 8:32 pm; edited 1 time in total

#154397 - yaustar - Tue Apr 15, 2008 5:59 pm

silent_code wrote:
ps: with c++ you don't need globals, use public static members instead... or is that wrong?

Functionally, they both exactly the same, just one is in a class scope which helps prevent name clashing.

Code:
// Set the ruffle setting for the waffle iron.  (or whatever you are actually doing)
int ruffle = waffle * baffle;

// Mask the setting - see spec for details.  (or whatever you are actually doing)
ruffle &= waffle;

I would go one further and say that this code should be in a function with a meaningful name that describes what this is doing.
_________________
[Blog] [Portfolio]

#154422 - Alphanoob - Wed Apr 16, 2008 1:40 am

im just gonna edit out my stupid posts. I was having a *really* bad day, and didnt fully read everything. rly sry all.

Last edited by Alphanoob on Wed Apr 16, 2008 9:04 pm; edited 1 time in total

#154427 - sgeos - Wed Apr 16, 2008 2:26 am

sajiimori is right about learning from a book. If all you want to learn right now is grammar, read the text. Keep in mind, however, that there is far more to programming than grammar. One step at a time, I suppose.

Alphanoob wrote:
but I feel that you guys who are whining about a fun bit of coding are even less helpful than that teacher

If you are not open to what people are trying to teach you, then you are wasting our time. Again, I suggest picking up the basics from your book before asking general questions; I suspect most of what people will try to teach you is precisely the information you won't find in your text, unless your question is very specific.

Chances are, you'll struggle with the text for weeks or months before you "get it". If you have any specific questions, feel free to ask.

-Brendan

#154428 - Alphanoob - Wed Apr 16, 2008 2:43 am

EDIT: sorry for freaking out. As I said earlier, been having a bad day. That code and those tips you posted were helpful sgeos. I made this post mostly out of annoyance for the people who came into the discussion before you and wouldnt stop bothering with details that seemed quite trivial. If you read the original thread you would get it, lol. No offense meant by all this stuff, I was just in a bad mood.

Last edited by Alphanoob on Wed Apr 16, 2008 9:05 pm; edited 1 time in total

#154468 - Alphanoob - Wed Apr 16, 2008 8:44 pm

so, what exactly *does* the & symbol do in something such as:
Z = (X+Y) & N? Is it just for -if statements as a condition, or does it do something else that I don't know about? (once again, sorry about my rant earlier)

#154473 - Dwedit - Wed Apr 16, 2008 8:59 pm

& is the bitwise and operator
To understand how it works, convert both operands to binary, then do logical AND on each bit.

126 & 41
01111110 = 126
00101001 = 41

00101000 = 40
Basically, it erases bits wherever there is a zero.
It's most often used for "bit masking", or performing modulo with a power of 2.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#154474 - sajiimori - Wed Apr 16, 2008 9:06 pm

Also see the "operators" section in your book.

#154475 - Alphanoob - Wed Apr 16, 2008 9:10 pm

I think I will just avoid that one for now. I was confused, because when I made my program (and I am now sure I must have done something wrong, although it worked fine...) I used one & rather than && for the logical one, and it worked as I intended. Maybe the compiler is just smart like that, but I guess I will just be careful of how I write it in the future. edit: Operators... good idea.

#154476 - Alphanoob - Wed Apr 16, 2008 9:14 pm

I got a bit of a question about what an error I keep getting means, and I dont think my book will be tellin me this one... at least not where I am in it. When I try to compile my code, it says something about me making a static declaration of a function after a non-static one. Anyone know what I did and how to fix it?

#154494 - wintermute - Thu Apr 17, 2008 12:17 am

When you want to ask about error output from the compiler then it's helpful to quote the exact error or warning from the build process.

I assume the error you're asking about is similar to this

Code:

template.cpp:70: error: 'void swapBuffers()' was declared 'extern' and later 'static'
template.cpp:16: error: previous declaration of 'void swapBuffers()'
make[1]: *** [template.o] Error 1


in which case you have a prototype for a function which is later declared static in the function body. This can be fixed by either removing the static keyword from the function body or adding it to the prototype.
_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog

#154500 - Alphanoob - Thu Apr 17, 2008 12:50 am

my apologies, but what do you mean by "static keyword"? Is that a variable name, a variable type, or what? I have never encountered that term before.

#154564 - Alphanoob - Fri Apr 18, 2008 4:19 am

anyone? what exactly would a static keyword be? here are the 2 things that were mentioned in the error.

Code:
void Tile2(int tileX,int tileY);
//This one came first

void Tile2(int tileX,int tileY)
//This one came multiple lines later, at the top of the funtion's definition.

#154565 - silent_code - Fri Apr 18, 2008 4:32 am

if no one else will do it, i'll answer that question... after i get some sleep.

see ya.

ps: static *is* a keyword, just like sizeof, delete, new, class, struct etc. ;^)


Last edited by silent_code on Fri Apr 18, 2008 4:34 am; edited 1 time in total

#154566 - Alphanoob - Fri Apr 18, 2008 4:34 am

Ty man. Night. So its like 5:30 where you are. I would be tired too. Just for reference, I am referring to that big section I commented out in the code I sent you.

#154651 - sgeos - Sat Apr 19, 2008 9:03 pm

sajiimori wrote:
Also see the "operators" section in your book.

Actually, this is a case where I'm going to disagree. One should really learn about boolean logic before picking up a book.

Alphanoob wrote:
so, what exactly *does* the & symbol do in something such as

Boolean logic is not specific to any particular language, and need it for control flow (if, for, while). Without control flow, your programs can't do anything useful. So, my advice is, forget about &, &&, |, ||, !, ~, ^ and friends for a while, and read about boolean logic.

When boolean logic makes sense, the following explanation should be fairly easy to grasp.
&& is a boolean AND. & is a logical AND (ie, AND is performed on every bit).

For what it is worth, in C:
0 is FALSE
anything else is TRUE

So, (5 && 10) is TRUE (some non-zero value, probably 1).

-Brendan

#154665 - Alphanoob - Sun Apr 20, 2008 2:36 am

lol, funny you say that. Boolean logic is pretty much what I am doing in math class. We just started probabilities (in functions, stats, and trig class), and were using those funny u = (union = "or") and n = (intersection = "and") lol. I do kinda get that stuff, especially from programming in basic, when those are about all that you have at your disposal for useful commands. =)