Working with screens using the C programming language on the Amiga is a first for me. Remember how easy it was in Amos? I had seen some C code using screens before and frankly it scared me. Actually it is not really difficult to open a screen on the Amiga using C. As with all my articles I will try to take all the difficult information that is out there on a particular subject and make it understandable. The same goes for the example source code that is out there. I come across so many examples that even make me scratch my head. Especially when you can write in a much better and understandable way.

What steps do we need to take in order to open a screen?

Step 1 – Declare a pointer that will hold our screen structure (Line 10)
Step 2 – Setting up our screen’s parameters (Line 12 till 27)
Step 3 – Open the Intuition Library and Interface (Line 33 till 44)
Step 4 – Opening our screen and check if we managed to open it (Line 45 till 48)
Step 5 – Cleaning up by closing our screen, Intuition Library and it’s Interface (Line 50 till 79)

00 #include <stdio.h>
01 #include <exec/types.h>
02 #include <intuition/intuition.h>
03
04 #include <proto/exec.h>
05 #include <proto/intuition.h>
06 
07 struct Library *IntuitionBase = NULL;
08 struct IntuitionIFace *IIntuition = NULL;
09 
10 struct Screen *my_screen = NULL;
11
12 /* Declare and initialize our screen structure */
13 struct NewScreen my_new_screen=
14 {
15 0, /* LeftEdge */
16 0, /* TopEdge */
17 320, /* Width */
18 200, /* Height */
19 3, /* Depth */
20 0, /* DetailPen */
21 1, /* BlockPen */ 
22 CUSTOMSCREEN, /* Type of screen */
23 SPRITES, /* ViewModes option */
24 NULL, /* Font option */
25 NULL, /* Title of the screen */
26 NULL /* Gadgets option */
27 };
28
29 int main (void)
30 {
31
32 /* Opening the Intuition Library */
33 IntuitionBase = IExec->OpenLibrary("intuition.library", 50L);
34
35 /* Could we open the Intuition Library? */
36 if (IntuitionBase != NULL)
37 {
38
39 /* Open the Intuition Interface */
40 IIntuition = (struct IntuitionIFace *) IExec->GetInterface (IntuitionBase, "main", 1, NULL);
41
42 /* Did we get the Intuition Interface? */
43 if (IIntuition != NULL)
44 {
45 my_screen = IIntuition->OpenScreen(&my_new_screen);
46
47 if (my_screen !=NULL)
48 {
49
50 /* If the screen is still open we need to close it */
51 IIntuition->CloseScreen(my_screen);
52 }
53
54 /* We could not open our screen */
56 else
57 {
58 printf ("Unable to open our screen!\n");
59 }
60
61 /* If the Intuition Library Interface is open, close it */
62 IExec->DropInterface((struct Interface *)IIntuition);
63 }
64 
65 /* We could not open the Intuition Library Interface */
66 else
67 {
68 printf ("Unable to open the Intuition Library Interface!\n");
69 }
70
71 /* If the Intuition Library is open, close it */
72 IExec->CloseLibrary(IntuitionBase);
73 }
74
75 /* We could not open the Intuition Library */
76 else
77 {
78 printf ("Unable to open the Intuition Library!\n");
79 }
80
81 return 0;
82 }

When you compile the code and run it you will see it flashes a black screen for just a second. This is because we have not instruct our little example to do anything with our screen. We will do a lot more in the coming articles regarding screens.

Let’s analyse the source code.

10 struct Screen *my_screen = NULL;

Line 10 will set up a pointer called “my_screen” which will hold our screen structure. In other words if we open our screen successfully we will store the screens info in the “my_screen” pointer. We are going to use this to make our screen do things. To put it simple; every time we want to do things with our screen we need to refer to which screen we would like to use which means we will refer to “my_screen”. If my wife asks me to get a certain pair of shoes she will always refer to one of the 200 boxes she has. Without this info I would not be able to know what to do and shout some error codes.

12 struct NewScreen my_new_screen =
14 {
15 0, /* LeftEdge */
16 0, /* TopEdge */
17 320, /* Width */
18 200, /* Height */
19 3, /* Depth */
20 0, /* DetailPen */
21 1, /* BlockPen */
22 CUSTOMSCREEN, /* Type of screen */
23 SPRITES, /* ViewModes option */
24 NULL, /* Font option */
25 NULL, /* Title of the screen */
26 NULL /* Gadgets option */
27 };

Line 12 till 27 will define our screens parameters. Here we will define for example the width and height of our screen. For the moment we will not pay much attention to this. Going in to depth on this might just scare you away. Our screen will be of the type CUSTOMSCREEN and the width is 320 and the height is 200 is all you need to know at this stage. The width and height speaks for itself and a CUSTOMSCREEN means that we control how it looks, talks and breaths. In order words we decide the width, hight, colours etc.

45 my_screen = IIntuition->OpenScreen(&my_new_screen);

Line 45 will open our screen and store the info in our “my_screen” pointer. The part “(&my_new_screen)” tells the system how our screen should look like.

47 if (my_screen !=NULL)
48 {

Line 47-48 will check if we managed to open the screen. If not the program will jump to line 55-58 and display the message that the screen could not be opened.

51 IIntuition->CloseScreen(my_screen);

Line 51 will close our screen once we are done. Remember I was talking about the fact that we would use our “my_screen” pointer every time we would do something with our screen? Here we are closing our screen and referring to which screen we would like to close. In other words my wife asked me to put her pair of shoes back in box number 145. Next time we will actually keep our screen open until we decide it should be closed. Happy coding!

Advertisements