Amiga.org

Operating System Specific Discussions => Amiga OS => Amiga OS -- Development => Topic started by: Kronos on July 31, 2006, 03:56:49 PM

Title: Basic Amiga-API coding
Post by: Kronos on July 31, 2006, 03:56:49 PM
From time to time there pops up a thread where people complain that learning the Amiga-API would be so hard and that no good resources would be available.

Now, there is the the Amiga-C-Manual, but thats quite outdated, and there is the Yahoo-maillist amiga_bcg but here you will find all sorts of skill-levels and question mixed up.

And then you have the occasional tutorial in print-magazines, but I allways found they were trying to do to much at a time overwhelming the reader, and making it hard to figure out where the tutorial ends, and where the pseudo-functionallity starts.

I therefore decided *fanzy fanfare* to do my own !!

It's very basic
It's targeted a users with basic C-skills
It's examples won't try to be usefull in any kind
It's a one proble at a time approach
It expects the reader to look up further options and arguments himself

Preparing yourself for the ride:

http://home.arcor.de/dietmar_eilert/site/cubic/download.htm (http://home.arcor.de/dietmar_eilert/site/cubic/download.htm)

Download atleast the Demo-versoin (should suffice), buying the full package is offcourse an even better idea (and no I'm not getting paid for writing this  :pissed: ), you may also wish to download the C/C++ Add-On Package, unless you know how to setup and use a bare compiler&documentation.

After installing, set Compiler to gcc (press blue box in toolbar), and target to amigaos3 (gcc 2.95.3). Useing other compilers and targets should be possible, but for the moment we should keep the possible problems to a minimum.
Now create a new project (the tilted white paper)and make it an Amiga project. After doing that open the created file main.c and paste the tutorial-source into it (yes, I'm sure there are more elegant ways of doing it, but for the moment this will do).

You should now be able to call "make all" (the 4 connected blue boxes) and "run" (white on green arrow).

O.k. here are the 1st chapters:

Intuition1 (http://steamdraw.de/intuition1.c)
Intuition2 (http://steamdraw.de/intuition2.c)

Once you got them working you should right-click some of the coloured keyword, selcect "look up word" (or something similar) to see other related functions, structures and tags.
I will not explain all of them, only those that aren't self-explaining and/or important.

As noted above these were written for OS3.x, no change is needed to adapt them for MorphOS-native (and same should go for AROS), adapting them to OS4-native would either need a pre-processor statement switching of the use of interfaces, or actully adding the code for interfaces. As none of this changes the way the API and it's functions work, this isn't neccasary until you have grown out of tutorials.
Title: Re: Basic Amiga-API coding
Post by: Heinz on July 31, 2006, 04:28:06 PM
The examples both compiles for Aros without any problems (using AmiDevCpp).

Am I allowed to upload them as example projects to the AmiDevCpp Homepage ?

http://amidevcpp.amiga-world.de/exampleupload.php (http://amidevcpp.amiga-world.de/exampleupload.php)
Title: Re: Basic Amiga-API coding
Post by: Kronos on July 31, 2006, 04:29:56 PM
Sure, consider them freeware in the most orginal meaning of the word.
Title: Re: Basic Amiga-API coding
Post by: Hans_ on July 31, 2006, 04:52:20 PM
I haven't had a chance to look at them yet, but this is a good idea. I'm sure someone can give you a small #ifdef, that will allow your code to compile in OS4 without requiring modifications.

EDIT: Make that a few #ifdefs, because of OS4's library Interface system

Hans
Title: Re: Basic Amiga-API coding
Post by: Kronos on July 31, 2006, 04:59:30 PM
@Hans
AFAIK there is a way to switch back to the normal way to access the API, which would require only 1 #ifdef at the start.

Everything else would make things much to complicted for something that is intended to be simple.
Title: Re: Basic Amiga-API coding
Post by: Slash on July 31, 2006, 05:05:46 PM
Just a couple of suggestions, and me being pedantic, but:

1) The 'int main()' line should probably be written as 'int main(int argc, char** argv)'

2) Probably a personal preference, but I'd make sure my window pointer was NULL when it is declared; i.e. 'struct Window *win = NULL'

3) Not as important in this instance, but after the window was closed, I'd reset the win variable to NULL after the window has been closed; i.e. 'win = NULL;'

4) Truth statements should be encapsulated within another set of parenthesis; i.e. 'if(win = OpenWindowTags(NULL,TAG_DONE))' would become if((win = OpenWindowTags(NULL, TAG_DONE))!=NULL) { ... } or if((win = OpenWindowTags(NULL, TAG_DONE))) { ... }.

Always compile with the GCC option -Wall and you'll catch these warnings.

Good idea though. :-)
Title: Re: Basic Amiga-API coding
Post by: Kronos on July 31, 2006, 05:27:19 PM
@slash

1) I know that, but the point is keep it as simple as possible, not to teach finer details of C.

2) Since the 1st use of a that variable is assigning it a value there is no point in presetting it to one. It also makes it harder to keep track of those that need to be presetted, and doing it to every pointer would also bloat of both source and code.

3) pretty much the same as 2)

4) never heard of that one (and have no idea what it should be good for)
Title: Re: Basic Amiga-API coding
Post by: _yak_ on July 31, 2006, 05:48:51 PM
@ Kronos

4) Possible scenario:

You write your code and accidently put assignment operator (=) instead of comparision (==) in some 'if' or loop. If you have -Wall switch enabled, GCC will generate a warning telling you that this may be possibly an error. You go there and if you see that this indeed should be an assignment and not a comparision, you put the parenthesis around it to tell GCC that it's ok this way. If GCC was right, you fix the bug.

Btw, good idea with the tutorial.
Title: Re: Basic Amiga-API coding
Post by: redrumloa on July 31, 2006, 05:49:42 PM
@Kronos

Great initiative! :-D
Title: Re: Basic Amiga-API coding
Post by: Kronos on July 31, 2006, 05:54:28 PM
@_yak_

Maybe I should at some point in time really start reading the compiler-docs further than what I need at that moment  :rtfm:
Title: Re: Basic Amiga-API coding
Post by: itix on July 31, 2006, 06:36:27 PM
Many warnings activated by -Wall option in GCC are just little pedantic. Maybe not important to beginners even.
Title: Re: Basic Amiga-API coding
Post by: Kronos on July 31, 2006, 07:19:38 PM
Added 3rd chapter (yes I'm THAT bored)

Basic reacting on user-input, menus are next and watching more than 1 window will be chapter 5.

intuition3 (http://steamdraw.de/intuition3.c)

After that I will move to the topic of graphics, simple drawing, blitting, chunky-images and window-refresh.

Would like to see some comments wether I'm  moving to fast or too slow.
Title: Re: Basic Amiga-API coding
Post by: nyteschayde on July 31, 2006, 09:15:47 PM
Can you do a demo on how to create a button that, say... closed the window. I'd like to see a simple example on how to use widgets and events.

It's interesting to know we don't have to open/close the various libBases.

Gabriel
Title: Re: Basic Amiga-API coding
Post by: Karlos on July 31, 2006, 10:09:29 PM
I hope that request doesn't turn into a Reaction v MUI v Gadtools v GTK wrapper thingy type flamefest :lol:
Title: Re: Basic Amiga-API coding
Post by: Heinz on July 31, 2006, 10:21:24 PM
Quote

Karlos wrote:
I hope that request doesn't turn into a Reaction v MUI v Gadtools v GTK wrapper thingy type flamefest :lol:


Why bother when there is ZUNE :lol:
Title: Re: Basic Amiga-API coding
Post by: Karlos on July 31, 2006, 10:42:02 PM
Isn't that just a MUI compatible GUI system taken from AROS?

Ah, forget it - going too OT...
Title: Re: Basic Amiga-API coding
Post by: AJCopland on July 31, 2006, 10:55:27 PM
Hi really good idea to write these, major thumbs up.

Noticed though that your link to the third intuition tutorial is set as intuition1.c (http://steamdraw.de/intuition1.c) instead of intuition3.c (http://steamdraw.de/intuition3.c)

Keep it up!

Andy
Title: Re: Basic Amiga-API coding
Post by: Dagon on August 01, 2006, 02:21:28 AM
If you do also a double buffer example which is more intuitive than that in the RKM that would be great :)
Title: Re: Basic Amiga-API coding
Post by: Kronos on August 01, 2006, 05:04:19 PM
@Karlos

No point for a flamefest, MUI rocks everything else sucks ....


I might at some later time do a simple GadTools-example (as GadTools is a s simple as it gets), and later on something bout MUI, since MUI is the only GUI that I use myself.

Nothing that would stop any given Triton-troll to to an Triton-t5utorial.

@AJCopland

Fixed that.

@Dagon

Does that mean I have to dig out my Dev-CD, search for that example, and on top of it find a way to make it even worse ???

Puih, thats 3 wishes at a time, and a chocolate-egg wound solve this one....
Title: Re: Basic Amiga-API coding
Post by: Karlos on August 01, 2006, 06:16:20 PM
MUI Schmooey... :lol:

BTW, I like the tutorial idea, I must say :-)
Title: Re: Basic Amiga-API coding
Post by: Kronos on August 01, 2006, 06:16:36 PM
Added chapter 4, basic menus.

intuition4 (http://steamdraw.de/intuition4.c)
Title: Re: Basic Amiga-API coding
Post by: Piru on August 01, 2006, 07:10:06 PM
@Kronos
Quote
Added chapter 4, basic menus.

It would crash less if you terminated the mymenu array with NM_END entry.
Title: Re: Basic Amiga-API coding
Post by: Kronos on August 01, 2006, 08:50:42 PM
 :oops:  :oops:  :oops:  :oops:  :oops:

Fixed.
Title: Re: Basic Amiga-API coding
Post by: Karlos on August 01, 2006, 09:15:37 PM
In itself, this is a valuable lesson:

Never assume because something compiles and appears to run fine that it's working properly.

Heck, I've been bitten a few times by making this assumption.
Title: Re: Basic Amiga-API coding
Post by: guest1955 on August 01, 2006, 11:04:11 PM
The first 3 tutorials compile and run fine using the AROS-sdk and gcc under Linux, but when I try to compile the 4th, I get:

intuition4.c:15: warning: pointer targets in initialization differ in signedness
intuition4.c:16: warning: pointer targets in initialization differ in signedness
intuition4.c:17: warning: pointer targets in initialization differ in signedness
intuition4.c:18: warning: pointer targets in initialization differ in signedness
intuition4.c:19: warning: pointer targets in initialization differ in signedness
intuition4.c:20: warning: pointer targets in initialization differ in signedness
intuition4.c:21: warning: pointer targets in initialization differ in signedness
intuition4.c:22: warning: pointer targets in initialization differ in signedness
intuition4.c:23: warning: pointer targets in initialization differ in signedness
intuition4.c: In function ‘main’:
intuition4.c:56: warning: assignment makes pointer from integer without a cast
intuition4.c:62: warning: assignment from incompatible pointer type
intuition4.c:66: warning: passing argument 1 of ‘(void *)*(&((SysBase) + 4294967044u)->vec)’ from incompatible pointer type
There are undefined symbols in 'a.out':
         U FreeMenus

Anyone got any ideas?
Title: Re: Basic Amiga-API coding
Post by: gklka on August 01, 2006, 11:38:11 PM
I get this error message:

gcc o/gcc-classic-stable/main.o -lauto -o bin/gcc-classic-stable/example4
/gg/lib/libamiga.a(createmenus.o)(.text+0x4): undefined reference to `GadToolsBase'
/gg/lib/libamiga.a(freemenus.o)(.text+0x4): undefined reference to `GadToolsBase'
collect2: ld returned 1 exit status
make: *** [bin/gcc-classic-stable/example4] Error 1
Done.

What shall I do? What's wrong?
Title: Re: Basic Amiga-API coding
Post by: Kronos on August 02, 2006, 04:14:37 AM
@gklka

Either open GadTools by hand, or switch to "libnix" in the compiler-options (linker options).
Title: Re: Basic Amiga-API coding
Post by: Kronos on August 02, 2006, 04:19:33 AM
@tangletown

Again, GadTools seems to not been opened, dunno whether a compiler-switch will help for AROS.

The warnings can be ignored atm, but I assume it is the last "0" in each of the lines, replacing them with "NULL" might work.
Title: Re: Basic Amiga-API coding
Post by: gklka on August 02, 2006, 11:14:25 AM
@Kronos

Switching to libnix works, thanks!

So we can use menus - what's next? :)

PS: Window title in example 4 is still "intuition3"! ;)
Title: Re: Basic Amiga-API coding
Post by: Kronos on August 06, 2006, 10:36:17 AM
Intuition5 (http://www.steamdraw.de/intuition5.c) getting input from multiple windows has just beed uploaded.
Title: Re: Basic Amiga-API coding
Post by: r-tea on August 07, 2006, 06:07:33 PM
@ Kronos
I might at some later time do a simple GadTools-example (as GadTools is a s simple as it gets), and later on something bout MUI, since MUI is the only GUI that I use myself.

A great idea with this tutorial Kronos!
I'll be watching this topic and wait for MUI one impatiently! :)
My dream are basic examples using MUI-hooks for handling events instead of building a high stack Aplication_Return_IDs within program's main loop.
And it would be great if those hooks will be easy to compile with vbcc :)
Title: Re: Basic Amiga-API coding
Post by: Kronos on August 08, 2006, 03:28:59 PM
@r-tea

Hooks for handling (input?)events ??

There is the notify-system for normal input, and MUIC_Window has a function MUIM_Window_AddEventHandler, which will call any given object (needs to be a costum-class, but one can't do much in MUI without atleast one of those) whenever an event happens (and matches the pattern you supplied).

Not gonna do a full example on this one, but drop me a mail and I send you some code-snippets from SteamDraw covering this.
Title: Re: Basic Amiga-API coding
Post by: Piru on August 08, 2006, 04:09:16 PM
This Intuition5 example has some serious bugs in it, for example it FreeSignal()s a signal bit that is still in use. Also it makes system leak signal bits as it pokes a live msgport created by the system (intuition). This is a big no-no. The code will also reference zeropage at while(im = GetMsg(windows[sNr]->UserPort) when the window closegadget has been hit: windows[sNr] = NULL; is done, but the loop is not terminated.

Finally, it's rather silly example as it will run out of signals anyway, as it in fact allocates TWO signals per window, and leaks every 2nd of them. It also has tons of warnings, which are easy to spot and fix with -Wall.

Edit: I will not rewrite it, but instead provide a link to my shared msgport example code:
sharedidcmp.c (http://www.iki.fi/sintonen/src/misc/sharedidcmp.c)

The example above truly allows unlimited number (limited only by the amount of free memory) windows per single process.
Title: Re: Basic Amiga-API coding
Post by: Kronos on August 08, 2006, 05:23:38 PM
Intuition5 has now been removed....

Thats what ones gets trying to do something from memory, years after learning it by trial&error...

Next up GFX, and I'll try to really stay with the basics this time  :oops:
Title: Re: Basic Amiga-API coding
Post by: BrandonLee on August 08, 2006, 05:36:29 PM
@Kronos

Just thought I'd drop a line to congratulate this initiative.
I'm just starting to learn C and this tutorial couldn't have come in a better time.

Thanks!
Title: Re: Basic Amiga-API coding
Post by: r-tea on September 11, 2006, 07:55:06 AM
Is the lesson completed?  :-)
Title: Re: Basic Amiga-API coding
Post by: BrandonLee on October 27, 2006, 06:01:18 PM
Sadly, it seems it's over...