English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language

 
 
Thread Tools
Old 11 September 2016, 11:50   #61
TroyWilkins
Registered User
 
TroyWilkins's Avatar
 
Join Date: Jan 2015
Location: Melbourne, Australia
Posts: 548
Quote:
Originally Posted by Zetr0 View Post
How I would write this, as if I was explaining it to a student I would initially write is thus -

Code:
void main( void )
{
    int ppoints[ 9 ];
    
    ppoints[ 0 ] = 0;             // since these are assigns 
    ppoints[ 3 ] = 0;             // and do not change in the code
    ppoints[ 4 ] = 200;             // its worthless to repeat the assign
    ppoints[ 7 ] = 200;             // better to assign then first 
    ppoints[ 8 ] = 0;              // and take only those that change into the loop
                                          // = 100 less commands ;)

    for (int candy=0 ; candy < 201; candy +=10 )
    {
        ppoints[ 1 ] = candy;
        ppoints[ 2 ] = (200 - candy);
        ppoints[ 5 ] = (200 - candy);
        ppoints[ 6 ] = candy;
        ppoints[ 9 ] = candy;
        PolyDraw( &rastPort, 5, ppoints);
    }
}
Have a look at that, obviously there are better / quicker ways - but since this is your first foray in C/C++ we should keep it simple to begin =)
Ahh, yes, of course, thank you, good thinking.

Now, I'm almost at my wits end, I can't work out what I';m doing wrong with PolyDraw, I keep getting:
Code:
argument type incorrect; expecting "WORD *", found "int *"
I'm sure it's something silly, but I'm stuck as to what I'm doing wrong, or what to do about it.
TroyWilkins is offline  
Old 11 September 2016, 12:16   #62
Zetr0
Ya' like it Retr0?
 
Zetr0's Avatar
 
Join Date: Jul 2005
Location: United Kingdom
Age: 49
Posts: 9,768
*double post*

Last edited by Zetr0; 11 September 2016 at 13:06.
Zetr0 is offline  
Old 11 September 2016, 12:17   #63
Zetr0
Ya' like it Retr0?
 
Zetr0's Avatar
 
Join Date: Jul 2005
Location: United Kingdom
Age: 49
Posts: 9,768
Quote:
Originally Posted by TroyWilkins View Post
Ahh, yes, of course, thank you, good thinking.
This is the point of C/C++ it gets you to have a process of thinking - modularization, implementation then continual refinement

Quote:
Originally Posted by TroyWilkins View Post
Now, I'm almost at my wits end, I can't work out what I';m doing wrong with PolyDraw, I keep getting:
Code:
argument type incorrect; expecting "WORD *", found "int *"
I'm sure it's something silly, but I'm stuck as to what I'm doing wrong, or what to do about it.
WORD is usually 16bits, an INT is usually 32bits on a 32bit system - this is where we start treading on compiler and machine specific differences but fear not its quite intuitive after a while of hair pulling -

try the following -
Code:
    WORD ppoints[ 9 ];
    
    ppoints[ 0 ] = 0;         // since these are assigns 
    ppoints[ 3 ] = 0;         // and do not change in the code
    ppoints[ 4 ] = 200;       // its worthless to repeat the assign
    ppoints[ 7 ] = 200;       // better to assign then first 
    ppoints[ 8 ] = 0;         // and take only those that change into the loop
                              // = 100 less commands ;)

    for (WORD candy=0 ; candy < 201; candy +=10 )
    {
        ppoints[ 1 ] = candy;
        ppoints[ 2 ] = (200 - candy);
        ppoints[ 5 ] = (200 - candy);
        ppoints[ 6 ] = candy;
        ppoints[ 9 ] = candy;
        PolyDraw( &rastPort, 5, ppoints);
    }
}
This is known as *Casting and it refers to the container type of your variables. Different containers have different methods of operations that can be done on them.

Last edited by Zetr0; 11 September 2016 at 12:24.
Zetr0 is offline  
Old 11 September 2016, 12:38   #64
TroyWilkins
Registered User
 
TroyWilkins's Avatar
 
Join Date: Jan 2015
Location: Melbourne, Australia
Posts: 548
Ahh, thank you. Now it builds correctly, without errors, but hangs straight after opening the window. Hmm...
TroyWilkins is offline  
Old 11 September 2016, 12:57   #65
Daedalus
Registered User
 
Daedalus's Avatar
 
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,334
@The Goto argument

Look, this argument breaks out all over the internet (and in the pub too if your friends are so inclined) whenever Goto is mentioned, and rarely, if ever, reaches an agreement.

To be fair, I can't remember ever using it to break out of nested loops - I see how it would make the code simpler but I don't think I've needed to get out of such heavy nesting in such a critical hurry as to warrant it. I'm trying to remember the last time I actually used a Goto, but probably when trying to write super tight timing in an interrupt while developing firmware. ASM might've been a better choice there, but then you're back to using JMP, effectively what Goto compiles to.

Also, for me anyway, line numbers went out with the 8-bits. Using Goto with a label is of course totally different readability- and maintenance-wise than a line number, similar to constants versus magic numbers and not dissimilar to calling a function by name. Not sure where line numbers come into it at all.

At the end of the day, it's heavily discouraged but it works fine. It's up to the individual programmer whether they want to get into it or not. Use it provided you're happy with the potential consequences, or don't if you want to follow best practices.
Daedalus is offline  
Old 11 September 2016, 13:18   #66
Zetr0
Ya' like it Retr0?
 
Zetr0's Avatar
 
Join Date: Jul 2005
Location: United Kingdom
Age: 49
Posts: 9,768
Quote:
Originally Posted by TroyWilkins View Post
Ahh, thank you. Now it builds correctly, without errors, but hangs straight after opening the window. Hmm...
Okay thats interesting, lets try something static

Code:
void main( void )
{
      WORD point_array[] = {4, 4, 19, 4, 19,19, 4,19, 4, 4    };
      PolyDraw( &rastPort, 5, point_array);
}
See if that displays anything - if so it might be something I have not done correctly in the logic.

Quote:
Originally Posted by Daedalus View Post
@The Goto argument

Look, this argument breaks out all over the internet (and in the pub too if your friends are so inclined) whenever Goto is mentioned, and rarely, if ever, reaches an agreement.
I am sure wars have been fought for less =)

Quote:
Originally Posted by Daedalus View Post
To be fair, I can't remember ever using it to break out of nested loops - I see how it would make the code simpler but I don't think I've needed to get out of such heavy nesting in such a critical hurry as to warrant it. I'm trying to remember the last time I actually used a Goto, but probably when trying to write super tight timing in an interrupt while developing firmware. ASM might've been a better choice there, but then you're back to using JMP, effectively what Goto compiles to.
Indeed, if you are going to use "goto" you might as well use ASM or in-line ASM - which I have seen is about as readable as tea-leaves in a cyclone on even medium sized projects

Quote:
Originally Posted by Daedalus View Post
Also, for me anyway, line numbers went out with the 8-bits. Using Goto with a label is of course totally different readability- and maintenance-wise than a line number, similar to constants versus magic numbers and not dissimilar to calling a function by name. Not sure where line numbers come into it at all.
"Back in the Day" it was a line number based statement, once Borland Turbo C++ (v2) came out you could use essentially a jump into point - but as you mentioned there is no difference from this and a regular function call.

Quote:
Originally Posted by Daedalus View Post
At the end of the day, it's heavily discouraged but it works fine. It's up to the individual programmer whether they want to get into it or not. Use it provided you're happy with the potential consequences, or don't if you want to follow best practices.
In a nut shell, the use of "goto" is just poor methodology - there are better ways to do it.
Zetr0 is offline  
Old 11 September 2016, 13:24   #67
TroyWilkins
Registered User
 
TroyWilkins's Avatar
 
Join Date: Jan 2015
Location: Melbourne, Australia
Posts: 548
I decided to do it with move and draw instead, and got it working.



I'll come back to PolyDraw and work out what is going on there another day.
TroyWilkins is offline  
Old 11 September 2016, 13:33   #68
Zetr0
Ya' like it Retr0?
 
Zetr0's Avatar
 
Join Date: Jul 2005
Location: United Kingdom
Age: 49
Posts: 9,768
@TroyWilkins

I am assuming you have set the raster port and pens?

[edit]
Yes you have - well done indeed!



here's some code that "should work" - its been a while since I have Amiga Coded

Code:
#define BLACK 1
int main ( void )
{
	WORD ppoints[ 9 ];
	struct BitMap bitMap = {0};
	struct RastPort rastPort = {0};
 
	/* Initialize the RastPort and link the BitMap to it. */
	InitRastPort(&rastPort);
	rastPort.BitMap = &bitMap;

	SetAPen(&rastPort, BLACK)
	
	ppoints[ 0 ] = 0;   // since these are assigns 
	ppoints[ 3 ] = 0;   // and do not change in the code
	ppoints[ 4 ] = 200; // its worthless to repeat the assign
	ppoints[ 7 ] = 200; // better to assign then first 
	ppoints[ 8 ] = 0;   // and take only those that change into the loop
			    // = 100 less commands ;)

	for (WORD candy=0 ; candy < 201; candy +=10 )
	{
		ppoints[ 1 ] = candy;
		ppoints[ 2 ] = (200 - candy);
		ppoints[ 5 ] = (200 - candy);
		ppoints[ 6 ] = candy;
		ppoints[ 9 ] = candy;
		PolyDraw( &rastPort, 5, ppoints);
	}

	// Dump data to CLI
	for( peter = 0; peter < 10; peter ++)
		print("point[%d] = %d\n", peter, ppoints[ peter ]);
	
	return 0;
}

Last edited by Zetr0; 11 September 2016 at 13:44.
Zetr0 is offline  
Old 11 September 2016, 13:47   #69
TroyWilkins
Registered User
 
TroyWilkins's Avatar
 
Join Date: Jan 2015
Location: Melbourne, Australia
Posts: 548
This is what I've got:
Code:
#include <stdio.h>
#include <exec/types.h>
#include <graphics/gfx.h>
#include <intuition/intuition.h>
#include <dos/dos.h>

/* Prototypes for system functions. */
#include <proto/exec.h>
#include <proto/graphics.h>
#include <proto/intuition.h>
#include <proto/dos.h>

/* Prototypes for our functions. */
int program_init(void);
void program_loop(void);
void clean_up(void);

/* Global variables. */
struct GfxBase *GfxBase = NULL;
struct IntuitionBase *IntuitionBase = NULL;
struct Window *MyWindow = NULL;
int c;

int main(void)
   {
      int error;
      error = program_init(); /* Open needed libraries and the main window. */
      if ( !error )
      {
         program_loop(); /* If everything was initialized, execute the main program code. */
      }
      clean_up(); /* We must clean up before we exit. */
      return 0;
   }
int program_init(void)
   {
      int result = 0;
      /* First open the graphics.library. */
      GfxBase = (struct GfxBase *)OpenLibrary( "graphics.library", 0L );
      if ( GfxBase != NULL)
      {
         /* Second open the intuition.library. */
         IntuitionBase = (struct IntuitionBase *)OpenLibrary( "intuition.library", 0L );
         /* Was the library opened? */
         if ( IntuitionBase != NULL )
         {
            /* Since the library was opened, we can open a window. */
            MyWindow = (struct Window *)OpenWindowTags( NULL,
                            WA_Left, 20,
                            WA_Top, 20,
                            WA_Width, 210,
                            WA_Height, 214,
                            WA_Title, (ULONG)"Troys Draw Demo Win",
                            WA_DepthGadget, TRUE,
                            WA_CloseGadget, TRUE,
                            WA_SizeGadget, FALSE,
                            WA_DragBar, TRUE,
                            WA_GimmeZeroZero, TRUE,
                            WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_MOUSEBUTTONS | IDCMP_MOUSEMOVE,
                            TAG_END );
            /* Was the window opened? */
            if ( MyWindow == NULL )
            {
               /* The window was not opened so display a message. */
               printf( "Unable to open the window!\n" );
               result = -1;
            }
         }
         else
         {
            /* The intuition.library was not opened so display a message. */
            printf( "Unable to open the intuition.library!\n" );
            result = -2;
         }
      }
      else
      {
         /* The graphics.library was not opened so display a message. */
         printf( "Unable to open the graphics.library!\n" );
         result = -3;
      }
      return result;
   }
void clean_up(void)
   {
      /* If the window is open, close it. */
      if ( MyWindow != NULL )
      { 
         CloseWindow( MyWindow );
      }
      /* If the intuition.library is open, close it. */
      if ( IntuitionBase != NULL )
      {
         CloseLibrary( (struct Library *)IntuitionBase );
      }
      /* If the graphics.library is open, close it. */
      if ( GfxBase != NULL )
      {
         CloseLibrary( (struct Library *)GfxBase );
      }
      return;
   }
void program_loop(void)
   {
      /* Main program loop. */
		for ( c = 0; c < 201; c +=5 ){
		Move(MyWindow->RPort, 0, c);
		Draw(MyWindow->RPort, 200-c,0);
		Draw(MyWindow->RPort, 200,200-c);
		Draw(MyWindow->RPort, c,200);
		Draw(MyWindow->RPort, 0,c);
		}
		Delay( 500L );
	}
Which gives this output (on a hires non-interlaced PAL screen):
TroyWilkins is offline  
Old 11 September 2016, 14:00   #70
TroyWilkins
Registered User
 
TroyWilkins's Avatar
 
Join Date: Jan 2015
Location: Melbourne, Australia
Posts: 548
I found this while trying to find examples of PolyDraw actually being used, which looks interesting:
http://www.memphisamigagroup.net/dis....c?noconvert=1
TroyWilkins is offline  
Old 11 September 2016, 14:01   #71
Zetr0
Ya' like it Retr0?
 
Zetr0's Avatar
 
Join Date: Jul 2005
Location: United Kingdom
Age: 49
Posts: 9,768
@TroyWilkins

I see you are coding under the power of darkness - this is how nature intended such dark arts sir

Code:
void program_loop( void )
{
      /* Main program loop. */

	WORD ppoints[ 9 ];
	
	ppoints[ 0 ] = 0;   // since these are assigns 
	ppoints[ 3 ] = 0;   // and do not change in the code
	ppoints[ 4 ] = 200; // its worthless to repeat the assign
	ppoints[ 7 ] = 200; // better to assign then first 
	ppoints[ 8 ] = 0;   // and take only those that change into the loop
			    // = 100 less commands ;)

	for (WORD candy=0 ; candy < 201; candy +=10 )
	{
		ppoints[ 1 ] = candy;
		ppoints[ 2 ] = (200 - candy);
		ppoints[ 5 ] = (200 - candy);
		ppoints[ 6 ] = candy;
		ppoints[ 9 ] = candy;
		// lets try this dereference -> Mmmm Pointers ;)
		PolyDraw(MyWindow->RPort, 5, ppoints);
	}

	/* Dump data to CLI
	for( fred = 0; fred < 10; fred ++)
		print("%d", ppoints[ fred ]);*/	
}
Zetr0 is offline  
Old 11 September 2016, 14:10   #72
TroyWilkins
Registered User
 
TroyWilkins's Avatar
 
Join Date: Jan 2015
Location: Melbourne, Australia
Posts: 548
Quote:
Originally Posted by Zetr0 View Post
@TroyWilkins

I see you are coding under the power of darkness - this is how nature intended such dark arts sir
Power of darkness? Sorry my friend, you've lost me there?!?

I tried the changes you suggested, and here's what I got:
TroyWilkins is offline  
Old 11 September 2016, 14:15   #73
Zetr0
Ya' like it Retr0?
 
Zetr0's Avatar
 
Join Date: Jul 2005
Location: United Kingdom
Age: 49
Posts: 9,768
@TroyWilkins

In Tasmania / Australia it must be about 10pm ?

Yes my mistake, I dereferenced the RastPort pointer incorrectly.
Zetr0 is offline  
Old 11 September 2016, 14:16   #74
TroyWilkins
Registered User
 
TroyWilkins's Avatar
 
Join Date: Jan 2015
Location: Melbourne, Australia
Posts: 548
Quote:
Originally Posted by Zetr0 View Post
@TroyWilkins

In Tasmania / Australia it must be about 10pm ?

Yes my mistake, I dereferenced the RastPort pointer incorrectly.
Ahh, yes, 10:15pm right now Hence why I'm about to call it quits for now.

I've been threatening to quit this since about 7pm, I think I should do so soon, hahaha
TroyWilkins is offline  
Old 11 September 2016, 14:25   #75
Zetr0
Ya' like it Retr0?
 
Zetr0's Avatar
 
Join Date: Jul 2005
Location: United Kingdom
Age: 49
Posts: 9,768
@TroyWilkins

Fear not I do most of my best work under the power of darkness

Lets try this -
Code:
void program_loop( void )
{
      /* Main program loop. */

	WORD ppoints[ 9 ];
	
	ppoints[ 0 ] = 0;   // since these are assigns 
	ppoints[ 3 ] = 0;   // and do not change in the code
	ppoints[ 4 ] = 200; // its worthless to repeat the assign
	ppoints[ 7 ] = 200; // better to assign then first 
	ppoints[ 8 ] = 0;   // and take only those that change into the loop
			    // = 100 less commands ;)

	Move (&RPort, 0, 0 ); // make sure we are in the right place

	for (WORD candy=0 ; candy < 201; candy +=10 )
	{
		ppoints[ 1 ] = candy;
		ppoints[ 2 ] = (200 - candy);
		ppoints[ 5 ] = (200 - candy);
		ppoints[ 6 ] = candy;
		ppoints[ 9 ] = candy;
		PolyDraw(&RPort, 5, ppoints);
	}
	
	// I think the data was initially outside either the X/Y boundary
	// of the Raster Port and or BitMap
	
}
been a while coding on the miggy - looks like I know what I am going to be doing this afternoon =D
Zetr0 is offline  
Old 11 September 2016, 15:50   #76
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
I am pretty sure SAS C doesn't accept the C99 feature of declaring a 'for' index inside the 'for'.

Just keep your declarations on top of the function.

On your working example, bring the declaration of 'c' variable inside the void program_loop(void). It's good practise to keep it local if you can.
alkis is offline  
Old 11 September 2016, 15:51   #77
Thorham
Computer Nerd
 
Thorham's Avatar
 
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 47
Posts: 3,751
Quote:
Originally Posted by TroyWilkins View Post
Oooookay, I think enough has been said about the merits (or lack thereof) of goto now...
Indeed. Sorry for bringing it up, I should've known better than to open that can of worms.
Thorham is offline  
Old 11 September 2016, 16:25   #78
Zetr0
Ya' like it Retr0?
 
Zetr0's Avatar
 
Join Date: Jul 2005
Location: United Kingdom
Age: 49
Posts: 9,768
Quote:
Originally Posted by alkis View Post
I am pretty sure SAS C doesn't accept the C99 feature of declaring a 'for' index inside the 'for'.

Just keep your declarations on top of the function.

On your working example, bring the declaration of 'c' variable inside the void program_loop(void). It's good practise to keep it local if you can.
hmmm I will test this later as I might be wrong but I do remember writing out the following syntax quite often.

for( int index =0; blah ; blah )

and if memory serves I would write this loop out a lot, when chunking data from FAST to CHIP

for( register int index =0; blah ; blah )

I used to use Dice / Lattice C as well as SAS - hmmm
Zetr0 is offline  
Old 11 September 2016, 16:58   #79
davideo
Amiga Tomcat
 
davideo's Avatar
 
Join Date: Sep 2007
Location: Boston Lincs
Posts: 1,500
@Zetr0

I found some of your coding at one point. I believe it was on Aminet but can't seem to locate it now!!
davideo is offline  
Old 11 September 2016, 16:58   #80
Samurai_Crow
Total Chaos forever!
 
Samurai_Crow's Avatar
 
Join Date: Aug 2007
Location: Waterville, MN, USA
Age: 49
Posts: 2,186
In C89 you have to start a new code block to declare a new local variable. Of course you can just use a set of curly braces to do that...
Samurai_Crow is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Amiga Coding: where to begin? Mr Softy Coders. General 27 01 March 2017 11:32
Amiga Emulation Beginner: Questions beaglelover New to Emulation or Amiga scene 1 18 February 2016 04:11
Got new amiga, some questions about coding defcon8 Coders. Asm / Hardware 2 06 April 2015 08:37
Any of you Coding on Amiga? Amiga Forever Coders. General 42 31 January 2012 02:58
Interested in coding on the amiga. Gandalf Coders. General 7 16 August 2011 10:30

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 22:36.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.12972 seconds with 14 queries