English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. C/C++

 
 
Thread Tools
Old 28 November 2013, 23:48   #1
AlfaRomeo
A1200 040 SAM440EP 667
AlfaRomeo's Avatar
 
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 793
Help with Amiga C

Hi guys,

I´m trying to make a 'Matrix effect' while learn Amiga C and will need a function that found the screen pens that are nearest my own chosen colors.
I think I found some functions that could serve me as example inside some code produced by Harry Piru Sintonen :

Code:
static ULONG mk32gun(UBYTE val)
{
  ULONG n;

  n = val & 0xf;
  n = (n << 4) | n;

  n = (val << 24) |
      (n << 16) |
      (n << 8) |
      (n << 0);

return n;
}

WORD *allocpens(struct Screen *scr)
{
  if (GfxBase->LibNode.lib_Version >= 39)
  {
    WORD *pens;

    pens = AllocMem(sizeof(WORD) * NUMPENS, MEMF_ANY);
    if (pens)
    {
      struct ColorMap *cm;
      int i;

      cm = scr->ViewPort.ColorMap;

      for (i = 0; i < NUMPENS; i++)
      {
        pens[i] = ObtainBestPen(cm,
                  mk32gun((penrgb[i] & 0xff0000) >> 16),
                  mk32gun((penrgb[i] & 0x00ff00) >> 8),
                  mk32gun((penrgb[i] & 0x0000ff) >> 0),
                      OBP_Precision, PRECISION_GUI,
                      OBP_FailIfBad, FALSE,
                      TAG_DONE);
      }

      return pens;
    }
  }
As a C newbie there are some segments of code that I don´t understand what they do, like the code inside the function 'mk32gun' or the meaning of 'mk32gun((penrgb[i] & 0xff0000) >> 16),..'

Can someone explain what is the meaning of that code bits or comment the entire code so can help me understand what it do, please?

AlfaRomeo is offline  
Old 29 November 2013, 08:01   #2
ajk
Registered User
ajk's Avatar
 
Join Date: May 2010
Location: Helsinki, Finland
Posts: 1,178
Those are not Amiga specific operations, just normal C bit-banging It's probably worthwhile to get acquainted with them, you could start by reading this Wikipedia article.

The mk32gun() function is essentially replicating the lowest four bits of "val" six times and sticking them after the original "val", leaving you with a 32 bit result.

Those other three operations grab the individual 8-bit red, green and blue components from the combined value.

Last edited by ajk; 29 November 2013 at 08:09.
ajk is offline  
Old 29 November 2013, 09:50   #3
thomas
Registered User
thomas's Avatar
 
Join Date: Jan 2002
Location: Germany
Posts: 5,731
IMHO the mk32gun function is not correct. If I had to expand for excample 0xAB to 32 bits I would use 0xABABABAB and not 0xABBBBBBB like the function does.

So this is my mk32gun function:

Code:
ULONG mk32gun (UBYTE val)
{
ULONG n = val;
return ((n << 24) |
        (n << 16) |
        (n <<  8) |
         n       );
}
thomas is offline  
Old 29 November 2013, 11:24   #4
phx
Natteravn

phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 1,225
BTW, this thread doesn't belong here. I see no assembler code in it.

More suitable would have been Coders.Language or Coders.C/C++.
phx is offline  
Old 29 November 2013, 18:59   #5
AlfaRomeo
A1200 040 SAM440EP 667
AlfaRomeo's Avatar
 
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 793
Quote:
Originally Posted by phx View Post
BTW, this thread doesn't belong here. I see no assembler code in it.

More suitable would have been Coders.Language or Coders.C/C++.
Sorry, my fault.
@Moderator: feel free to change the place.

Guys, thanks for the help.
Knowing what that function is doing it will be easier understand the code. Will look at the code at night after leave my job.

Last edited by AlfaRomeo; 29 November 2013 at 19:12.
AlfaRomeo is offline  
Old 29 November 2013, 23:15   #6
prowler
Global Moderator

prowler's Avatar
 
Join Date: Aug 2008
Location: Sidcup, England
Posts: 10,300
Quote:
Originally Posted by AlfaRomeo View Post
@Moderator: feel free to change the place.
Moved to the Coders.C/C++ subforum.
prowler is offline  
Old 13 February 2014, 20:50   #7
AlfaRomeo
A1200 040 SAM440EP 667
AlfaRomeo's Avatar
 
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 793
Wich is the best way to anim some sprites inside a OS 3+ friendly window?
AlfaRomeo is offline  
Old 25 February 2014, 15:30   #8
AlfaRomeo
A1200 040 SAM440EP 667
AlfaRomeo's Avatar
 
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 793
Hello,

Please, can someone tell me why the sprite doesn´t appear in the screen on the next code?

#include <exec/types.h>
#include <graphics/gfx.h>
#include <graphics/GfxBase.h>
#include <graphics/sprite.h>
#include <graphics/gfxmacros.h>
#include <intuition/IntuitionBase.h>
#include <intuition/screens.h>
#include <hardware/custom.h>
#include <hardware/dmabits.h>
#include <libraries/dos.h>

#include <clib/graphics_protos.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/alib_stdio_protos.h>

#include <stdlib.h>

struct GfxBase *GraphBase=NULL;
struct IntuitionBase *IntuiBase=NULL;
extern struct Custom custom;

#pragma chip

UWORD sprite_data[]={
0, 0, /* position control */
0xffff, 0x0000, /* image data line 1, color 1 */
0xffff, 0x0000, /* image data line 2, color 1 */
0x0000, 0xffff, /* image data line 3, color 2 */
0x0000, 0xffff, /* image data line 4, color 2 */
0x0000, 0x0000, /* image data line 5, transparent */
0x0000, 0xffff, /* image data line 6, color 2 */
0x0000, 0xffff, /* image data line 7, color 2 */
0xffff, 0xffff, /* image data line 8, color 3 */
0xffff, 0xffff, /* image data line 9, color 3 */
0, 0 /* reserved, must init to 0 0 */
};

void main(int argc, char **argv){
struct SimpleSprite sprite={0};
struct ViewPort *VPort;
WORD spriteNum;
SHORT deltaMove, ktr1, ktr2, colorReg;
struct Screen *MyScr;
int returnCode;

returnCode=RETURN_OK;

GraphBase=(struct GfxBase *)OpenLibrary("graphics.library",0);
if(GraphBase == NULL)
returnCode=RETURN_FAIL;
else {
IntuiBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0);
if(IntuiBase == NULL)
returnCode=RETURN_FAIL;
else {
MyScr=LockPubScreen(NULL);
if(MyScr==NULL)
returnCode=RETURN_FAIL;

VPort=&MyScr->ViewPort;
UnlockPubScreen(NULL,MyScr);
spriteNum = GetSprite(&sprite, 2);
if(spriteNum==-1)
returnCode=RETURN_WARN;
else {
colorReg = 16 +((spriteNum & 0x06) <<1);
printf("colorReg= %d\n", colorReg);

SetRGB32(VPort, colorReg +1, 12, 3, 8);
SetRGB32(VPort, colorReg +2, 13, 13, 13);
SetRGB32(VPort, colorReg +3, 4, 4, 15);

sprite.x=0; //Initialize pos & size info
sprite.y=0; //to match that shown in sprite_data
sprite.height=9; //so later system knows layout of data

/* Intall sprite data and move sprite to start position */
ChangeSprite(NULL, &sprite, (APTR)sprite_data);
MoveSprite(NULL, &sprite, 30,0);

for(ktr1=0, deltaMove=1; ktr1<6; ktr1++, deltaMove = -deltaMove){
for(ktr2=0; ktr2<100; ktr2++){
MoveSprite(NULL, &sprite, (LONG)(sprite.x + deltaMove), (LONG)(sprite.y + deltaMove));
WaitTOF(); //Wait for next frame to move again
}
}
FreeSprite((WORD)spriteNum);
}
CloseLibrary((struct Library *)IntuiBase);
}
CloseLibrary((struct Library *)GraphBase);
}
exit(returnCode);
}
AlfaRomeo is offline  
Old 25 February 2014, 17:03   #9
aragon
Registered User
aragon's Avatar
 
Join Date: Aug 2003
Location: hamburg
Posts: 55
The RKM example program ssprite.c is working. I assume you used that as a template?

I think that the problem is that you want to display your sprite on the default Public Screen (WB). Perhaps the SPRITE flag is not set for that screen?

Code:
ViewModes = the appropriate argument for the data type ViewPort.Modes.
           These include:
           HIRES for this screen to be HIRES width.
           LACE for the display to switch to interlace.
           SPRITES for this screen to use sprites (the pointer
		sprite is always displayed)
           DUALPF for dual-playfield mode (not supported yet)
	    [For V36: The ViewModes field is superseded by a TagItem with
	    tag value SA_DisplayID.]
aragon is offline  
Old 25 February 2014, 17:14   #10
thomas
Registered User
thomas's Avatar
 
Join Date: Jan 2002
Location: Germany
Posts: 5,731
Quote:
Originally Posted by aragon View Post
I think that the problem is that you want to display your sprite on the default Public Screen (WB). Perhaps the SPRITE flag is not set for that screen?
How could it? I see the mouse pointer on every screen opened by Intuition. Don't you see it, too?


Quote:
Originally Posted by AlfaRomeo View Post
Please, can someone tell me why the sprite doesn´t appear in the screen on the next code?
I tried your example and it works. So

1. do you use a graphics card? Hardware sprites only appear on native screens, not on RTG screens.

2. is your background black? You set all three colors to black, so your sprite would be invisible on a black background.

Unrelated:

3. you must not rename the base variables for libraries. That the program compiled and runs anyway for you shows that you don't need to open libraries for your compiler, it does this automatically. If it wouldn't, you would see "undefined symbol IntuitionBase" and "GfxBase" errors.


Not so unrelated:

4. you must not unlock the screen before you stop using it. If you do, the screen can disappear while your program still runs and it will crash.
thomas is offline  
Old 25 February 2014, 17:19   #11
aragon
Registered User
aragon's Avatar
 
Join Date: Aug 2003
Location: hamburg
Posts: 55
Quote:
Originally Posted by thomas View Post
How could it? I see the mouse pointer on every screen opened by Intuition. Don't you see it, too?
Of course, but it clearly states that the pointer sprite is always displayed...

You are right that the sprite does not appear on an RTG screen, switching the WB to a native mode made the example work.

Last edited by aragon; 25 February 2014 at 17:26.
aragon is offline  
Old 25 February 2014, 17:30   #12
AlfaRomeo
A1200 040 SAM440EP 667
AlfaRomeo's Avatar
 
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 793
Thanks for the help friends.

I didn´t touch in the Sprites flag and, as I see the mouse, so Sprites flag is ON.

But thomas was right, I do use a graphics card in my UAE config, so I presume I couldn´t use hardware sprites
Will try with virtual sprites instead.
AlfaRomeo is offline  
Old 20 February 2015, 01:43   #13
AlfaRomeo
A1200 040 SAM440EP 667
AlfaRomeo's Avatar
 
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 793
While trying to compile an old utility it keeps giving a type conflict error
in the line DateStamp(&tnow); from the sub-routine below:

Code:
USHORT SetTime(pointer, numpos, pos)
register USHORT *pointer;
register USHORT numpos;
register USHORT *pos;
{
    register int i;
    struct DateStamp tnow;
    USHORT hour10, hour1, min10, min1;
    static USHORT hour10_old = 99;
    static USHORT hour1_old = 99;
    static USHORT min10_old = 99;

    DateStamp(&tnow);

    hour1  = (tnow.ds_Minute / 60) % 12;
    hour1  = (hour1 == 0) ? 12 : hour1;
    hour10 = hour1 / 10;
    hour1  = hour1 % 10;

    min1  = tnow.ds_Minute % 60;
    min10 = min1 / 10;
    min1  = min1 % 10;

    if ( hour10_old != hour10 ) {
        for ( i=0; i < numpos; i++ ) {
            if ( hour10 == 1 )
                pointer[pos[i]] |= 0x8000;
            else
                pointer[pos[i]] &= 0x7fff;
        }
        hour10_old = hour10;
    }

    if ( hour1_old != hour1 ) {
        for ( i=0; i < numpos; i++ ) {
            pointer[pos[i]] &= 0xc7ff;
            pointer[pos[i]] |= (digits[hour1][i] << 11);
        }
        hour1_old = hour1;
    }

    if ( min10_old != min10 ) {
        for ( i=0; i < numpos; i++ ) {
            pointer[pos[i]] &= 0xff1f;
            pointer[pos[i]] |= (digits[min10][i] << 5);
        }
        min10_old = min10;
    }

    for ( i=0; i < numpos; i++ ) {
        pointer[pos[i]] &= 0xfff1;
        pointer[pos[i]] |= (digits[min1][i] << 1);
    }

	return((USHORT)(tnow.ds_Tick/TICKS_PER_SECOND));
}
Help needed so could find why the error
AlfaRomeo is offline  
Old 20 February 2015, 09:36   #14
thomas
Registered User
thomas's Avatar
 
Join Date: Jan 2002
Location: Germany
Posts: 5,731
Probably missing function prototype for DateStamp(). You need one of #include <proto/dos.h> or #include <clib/dos_protos.h>
thomas is offline  
Old 20 February 2015, 16:53   #15
AlfaRomeo
A1200 040 SAM440EP 667
AlfaRomeo's Avatar
 
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 793
You´re right, it was the missed prototype for the DateStamp().
Thanks for the help thomas.
AlfaRomeo is offline  
Old 20 February 2015, 18:00   #16
Minuous
Coder/webmaster/gamer
Minuous's Avatar
 
Join Date: Oct 2001
Location: Canberra/Australia
Posts: 1,836
Quote:
Originally Posted by AlfaRomeo View Post
will need a function that found the screen pens that are nearest my own chosen colors.
If that is all you need, you should use FindColor().
Minuous is online now  
Old 06 March 2015, 14:52   #17
AlfaRomeo
A1200 040 SAM440EP 667
AlfaRomeo's Avatar
 
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 793
Quote:
Originally Posted by Minuous View Post
If that is all you need, you should use FindColor().
Thanks,
my code now works with ObtainBestPen()
AlfaRomeo is offline  
Old 06 March 2015, 15:19   #18
AlfaRomeo
A1200 040 SAM440EP 667
AlfaRomeo's Avatar
 
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 793
How I could concatenate a string with a qualifier key result so it could be used with the Text() function?

Like used with the printf() function:
Code:
printf("Qualifier = %lx, Raw = %d\n", iMsg->Qualifier, iMsg->Code);
but now I pretend display the result in my own window like:
Code:
Text(RP,"Quality = "+ iMsg->Qualifier,16);
With
Code:
char *string=iMsg->Qualifier;
the result is a big string with some strange characters


asprintf() could do but it didn´t exist in Amiga C
AlfaRomeo is offline  
Old 06 March 2015, 15:44   #19
ajk
Registered User
ajk's Avatar
 
Join Date: May 2010
Location: Helsinki, Finland
Posts: 1,178
How about just regular sprintf()? Allocate the buffer for it yourself either statically or with malloc().
ajk is offline  
Old 06 March 2015, 16:17   #20
AlfaRomeo
A1200 040 SAM440EP 667
AlfaRomeo's Avatar
 
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 793
Great, so simple and works so well..

I´ve tried some complex solutions, neglecting the simple ones, and all they failed

Thanks ajk
AlfaRomeo is offline  
 


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

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 06:40.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2018, vBulletin Solutions Inc.
Page generated in 0.09120 seconds with 12 queries