Welcome! Log In Create A New Profile

Advanced

Getting started, languages, syntax?

Posted by DigitalMan 
Getting started, languages, syntax?
October 06, 2008 03:20AM
I am a fairly experienced programmer, but the Wii seems to be rather unique.

First off, I see a lot of questions here about C++ coding. However, the sample file that came with DevKitPro is a .c file, indicating plain C language. Is this just an incorrectly named source file, or do I have to go through some procedure to be able to work with C++?

Also, I notice a "->" showing up in some places, specifically "if(rmode->viTVMode&VI_NON_INTERLACE)". I know it's not a pointer, and I don't think it's a comparison - but, due to its nature, I can't do a web search on that syntax. Could anyone tell me exactly what it does?

And finally, what I really need is a reference for all the unique commands. Windows C++ and VB have MSDN, and Java/J2ME has a full reference on the Sun site, but this seems to have nothing. VIDEO_WaitVSync()? VIDEO_SetBlack(FALSE)? What do these do, when do I call them? Obviously I can't ask here for every single thing, so any location to find it all would be appreciated.

A basic getting started tutorial might be nice, too - like, a "Hello world" that doesn't require resetting the Wii after it's run. Just a basic intro on where to put loops, and how to interact with a controller to end the program and return to the menu.
Re: Getting started, languages, syntax?
October 06, 2008 04:57AM
1) Renaming the source file to .cpp is sufficient to get g++/dkP to compile C++ source. You can work with either C or C++.

2) rmode is indeed a pointer, and -> has no other use other than accessing fields/members from a pointer (besides the rare case of using operator overloading, anyway).

3) You've got existing homebrew examples/programs and the documentation to go on (that page is just generated from metadata in the headers).

4) Hey, the home button returns to loader, you don't have to reset the whole machine :P... Actually the template example shows basic Wiimote code, loop structure, and return to loader, just like you requested.
There are a few other examples:
- Wii Examples - Goes through one 2D example as well as 5 3D (GX) examples.
- Old miscellaneous Wii examples but still useful.
I guess I'd recommend starting to learn libwiisprite since it's easy for beginners.



Edited 4 time(s). Last edit at 10/06/2008 05:00AM by AerialX.
Re: Getting started, languages, syntax?
October 06, 2008 06:04AM
Quote
AerialX
rmode is indeed a pointer, and -> has no other use other than accessing fields/members from a pointer (besides the rare case of using operator overloading, anyway).

Interesting. At one point, I knew about all there was to know about standard (non-Windows-API) C++ programming, and I don't even recall pointers having members or fields. I'm certainly not saying you're wrong, obviously - I just need to find some place with detailed information on this, assuming it's not Wii-specific.

Quote

Hey, the home button returns to loader, you don't have to reset the whole machine :P

Are you sure? I compiled the file in the examples/wii/template folder exactly how it came, and it does run - but it won't quit. No response to any Wii-mote buttons, Gamecube pad buttons, or even the reset button; it has to be completely turned off by holding the power button. Though, it was being run using SendElf, not off the SD card.

Thank you for the information though, now I think I can make some progress!
Re: Getting started, languages, syntax?
October 06, 2008 12:05PM
Quote
DigitalMan
Quote
AerialX
rmode is indeed a pointer, and -> has no other use other than accessing fields/members from a pointer (besides the rare case of using operator overloading, anyway).

Interesting. At one point, I knew about all there was to know about standard (non-Windows-API) C++ programming, and I don't even recall pointers having members or fields. I'm certainly not saying you're wrong, obviously - I just need to find some place with detailed information on this, assuming it's not Wii-specific.

A pointer can technically point to anything, in the rmode case, it was pointing to a data structure, which has fields.
Re: Getting started, languages, syntax?
October 07, 2008 02:39AM
Simple comparison of . and -> operators:
typedef struct MyStruct {
	int Foo;
};

MyStruct S, *SP = &S; //SP is pointer to S
S.Foo = 42;
printf("%d\n", SP->Foo); //prints 42
Re: Getting started, languages, syntax?
October 07, 2008 03:46AM
Quote
HyperHacker
Simple comparison of . and -> operators:
typedef struct MyStruct {
	int Foo;
};

MyStruct S, *SP = &S; //SP is pointer to S
S.Foo = 42;
printf("%d\n", SP->Foo); //prints 42

Excellent, thank you! I am familiar with the use of . (especially with all the VB and Java work I've done), so that makes perfect sense now. Not quite sure why one would bother using pointers like that to begin with, but at least I know how it works.

And now I finally know how to printf variables, that'll come in handy during test programs.



Edited 1 time(s). Last edit at 10/07/2008 07:35AM by DigitalMan.
Re: Getting started, languages, syntax?
October 07, 2008 09:19AM
You'll use pointers a lot when you want to pass data structures between functions. On a really basic level, it'll be faster, because it will only send a 32bit point to your structure, instead of all the data contained within it.

From a VB point, it's the equivalent of ByVal and ByRef... ByVal passes a copy of the exact value, thus you cannot make changes to source, ByRef passes a pointer to your variable, which allows you to modify the data from the calling procedure.

VB Example
Sub Main()
 Dim myX As Integer

 myX = 2
 MulX2v(myX) ' myX will still equal 2, because the value is passed
 MulX2r(myX) ' myX will actually equal 4, because the variable is passed, and not the value
End Sub

Sub MulX2v(ByVal X As Integer)
 X = X * 2
End Sub

Sub MulX2r(ByRef X As Integer)
 X = X * 2
End Sub

In C, it would look like this

void Main()
{
 int myX;

 myX = 2;
 MulX2v(myX); // myX still equals 2, passed value, not variable
 MulX2r(&myX); // myX equals 4, passed variable, not value
}

void MulX2v(int X)
{
 X = X * 2;
}

void MulX2r(int *X)
{
 X = X * 2;
}
Re: Getting started, languages, syntax?
October 07, 2008 06:22PM
Holy C failure, Batman!

A pointer is a variable that holds a memory address of some other data. You can use various functions to read data from the memory address stored in the pointer.


// Functions must be declared before they can be used :P
void multi_2v(int x)
{
  x = x * 2; 
  // This statement will have no real effect, except using up some cycles and memory.
}

void multi_2r(int *x)
{
  *x = *x * 2;  
  // for pointers, "var" gives you access to the stored memory address, 
  // and "*var" lets you access the variable stored at that 
  // memory address (interpreted as pointer type)
}

int main()
{
  int my_x = 2;

  multi_2v(my_x); // Passed as the value of the variable
  printf("%d\n", my_x); // Prints 2

  multi_2r(&my_x); // Passed as a the memory address of the variable (pointer)
  printf("%d\n", my_x); // Prints 4
  
  return 0; // return plx kthx
}

You may have been thinking of C++ "pass by reference" (different from pointers).
void multi_2(int &x)
{
  x = x + 2; 
}
Pass by reference works like pass by pointer, but you don't have to dereference your pointer each time you want to use the data at the memory address. Nothing that special about it.



Edited 1 time(s). Last edit at 10/07/2008 06:30PM by tona.
Re: Getting started, languages, syntax?
October 07, 2008 09:33PM
I've adjusted mine to use a data structure
#include <stdio.h>


typedef struct myDataStruct
{
 int varX;
} myDataStruct;

void MulX2v(myDataStruct X);
void MulX2r(myDataStruct *X);

void main()
{
 myDataStruct myData;

 myData.varX = 2;

 MulX2v(myData); // myX still equals 2, passed value, not variable

 printf("%d\n", myData.varX); // prints 2

 MulX2r(&myData); // myX equals 4, passed variable, not value

 printf("%d\n", myData.varX); // prints 4
}

void MulX2v(myDataStruct X)
{
 X.varX = X.varX * 2;
}

void MulX2r(myDataStruct *X)
{
 X->varX = X->varX * 2;
}

It compiles and runs correctly. Am I mixing C and C++? I must admit, neither have really been my strong point.
Re: Getting started, languages, syntax?
October 07, 2008 10:04PM
As said above, the devkitpro GX and VI functions are the reverse-engineered counterparts to the Nintendo ones, by the same names. As such, the Nintendo Revolution VI (Video Interface Library) and GX (Graphics Library) 250 page official documentation (confidential) would probably be helpful to you. Don't ask where to find them though!
Re: Getting started, languages, syntax?
October 08, 2008 07:08AM
Quote
whodares
I've adjusted mine to use a data structure
*snip*
It compiles and runs correctly. Am I mixing C and C++? I must admit, neither have really been my strong point.
Your code there will work just fine. The '->' operator accesses a member of an object at a pointer. It would be similar to something like (*X).varX, except that's just crazy.
Re: Getting started, languages, syntax?
October 08, 2008 07:22AM
Interesting; I thought I had a handle on pointers, but apparently not. I have a line:

if (counter <= 5) loops(&counter);

Which calls:

void loops(int *num){
printf("Line test %d.\n", *num);
*num++;
}

I realize using pointers here is useless, it's just a test, which is so far failing. When I was directly sending the "counter" variable and incrementing "counter" in the main loop, I got 6 lines, just as expected. However, now it infinitely repeats the actual address of the counter variable. Oddly enough, removing the * from the printf and increment lines has the identical result. Its function is remarkably similar to the working examples given, so I'm not sure where it went awry.
Re: Getting started, languages, syntax?
October 08, 2008 01:40PM
Watch your order of operations. You're inadvertantly doing pointer arithmetic; the ++ is increasing the pointer (so it makes it point to another memory address) before the * does anything. Basically, what you want is "(*num)++;" to first resolve the pointer and then increase the value it points to.



Edited 2 time(s). Last edit at 10/08/2008 01:43PM by AerialX.
Re: Getting started, languages, syntax?
October 08, 2008 02:04PM
Wow. It was just that simple. I spent all night looking at various C pointer tutorials, and not a single one of them mentioned order of operations (though it was educational!). Come to think of it, beyond typical math, order of operations hasn't been discussed in any tutorial or book I've seen for any programming language. Hopefully it won't cause such a problem in the future...

At any rate, now initialization is working, console is working, math is working, pointers are working, and controller input is working. I think I'm finally ready to enter the under-documented hell that is GX programming!

Oh yeah. You can expect a lot more questions from me. Yeeah...

But thank you so much, all of you, for getting me this far! There's no way my web searches could have given these kinds of results so quickly.
Sorry, only registered users may post in this forum.

Click here to login