Results 1 to 7 of 7

Thread: Really simple question... regarding arrays

  1. #1

    Thread Starter
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457

    Question Really simple question... regarding arrays

    Hi!

    I've been programming in VB for about 2 years now, but I only started C++ 3 hours ago with this article I found

    It basically explains how to make a DLL in C++ to handle arrays. But it only allows me to use 1 dimension arrays, and since this is for special effects in images, it needs to be 2d

    Here's the code, in VB. I tried to translate but have no idea of where to start to get the values from the arrays at the positions I want.

    Code:
    Public Sub SpecialBltArray(SrcArray() As Byte, DestArray() As Byte, ByVal dX As Long, ByVal dY As Long, SrcX As Long, SrcY As Long, SrcWid As Long, SrcHgt As Long, LookupTable() As Byte)
        Dim cX As Long, cY As Long 'Loop counters
        Dim AbsX As Long, AbsY As Long 'X/Y values in the destination. This is used twice, so I calculate it only once to speed up :)
        
        'Loop trough all pixels...
        For cX = 0 To SrcWid
            For cY = 0 To SrcHgt
                'See the resulting value of crossing the effect's R/G/B value with the
                'destination's R/G/B value, in the lookup table.
                AbsX = dX + cX
                AbsY = dY + cY
                DestArray(AbsX, AbsY) = LookupTable(DestArray(AbsX, AbsY), SrcArray(SrcX + cX, SrcY + cY))
            Next cY
        Next cX
    End Sub
    That's all. There's no need for array bound checks, because it's all handled in VB, as well as the lookup table. I just need C++ for that little extra speed, VB is kinda slow in this kind of stuff
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    You can easily use a 1d array almost just the same as if it were 2d. All any array is, is a block of memory split into elements. If you have a 1d array of 30 elements, that's the same amount of memory as a 2d array with dimensions [3][10]. You can translate the x and y coordinates you would use with a 2d array to an element number in a 1d array very easily, so long as you know the width of one line. So to get coord [x][y] you use [x + y*width].

    Anyway, your C++ function will look something like this:
    Code:
    typedef unsigned short BYTE;
    
    void SpecialBltArray(BYTE SrcArray[], BYTE DestArray[], long dX, long dY, long SrcX, long SrcY, long SrcWid, long SrcHgt, BYTE LookupTable[])
    {
        long cX, cY;       // loop counters
        long AbsX, AbsY;   // X/Y values in the destination.  This is usedtwice, so I calculate it only once to speed it up
    
        // loop through all pixels...
        for (cX=0; cX<=SrcWid; cX++)
            for (cY=0; cY<=SrcHgt; cY++)
            {
                // see the resulting value of crossing the effect's R/G/B value with the
                // destination's R/G/B value, in the lookup table.
                AbsX = dX + cX;
                AbsY = dY + cY;
                DestArray[Absx + AbsY*lDestPitch] = LookupTable[DestArray[BasX + AbsY*lDestPitch] + SrcArray[SrcX+cX + (SrcY+cY)*lSrcPitch]*lLookupPitch];
    
            }
    }
    The variables lDestPitch, lSrcPitch and lLookupPitch are the widths of one line of their respective arrays. You need to pass them in as parameters.

    Also note that there is a lot of scope for optimisation with those array accesses. If any of the widths is constant, you can use some bitshifts to get rid of some multiplications too.
    Harry.

    "From one thing, know ten thousand things."

  3. #3

    Thread Starter
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Hey, thank you a lot! You've saved my life (my game was getting too slow to play )

    Just 2 little things I noticed in your code: I didn't know about the BYTE type. They just told me it was something like "unsigned __something8". And it had an asterisk, and he said that you needed to increase the place where you were reading in the memory because C++ didn't support arrays (like array[10])...
    Also, if it's 0-based, I think I should use < instead of <= in the loops (unless it's something because of the 1d array, is it? ).

    And yes, the dest width is always 320. But I should write a function, not optimized, to blt to other surfaces
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  4. #4
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Well the BYTE type was one that I defined. Notice the typedef statement - that's just makeing "BYTE" an alias for "unsigned short" which is the C/C++ native type for a single byte with a value between 0 and 255.

    Who's the 'they' you're talking about?

    The for loops in the C function will iterate through exactly the same values of cX and cY as the for loops in your VB version. Your for loops start at 0, so do the for loops in the C version. They end at SrcWid and SrcHgt, and so do the loops in the C version. If you used < instead of <= then the last interation would be missed.
    Harry.

    "From one thing, know ten thousand things."

  5. #5

    Thread Starter
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Heh, ok thanks, now I understand

    (Sorry but VB World ate my other reply so this one is kinda short)

    "They" are actually "he". The guy that wrote the article: http://64.23.12.52
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  6. #6

    Thread Starter
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Sorry, but I tested your code and it doesn't work

    Here it is (with some modifications):

    Code:
    //#include "Windows.h"
    
    typedef unsigned short BYTE;
    
    //Create the function
    void _stdcall SpecialBltArray(BYTE SrcArray[], long TotalSrcWidth, BYTE DestArray[], long TotalDestWidth, long dX, long dY, long SrcX, long SrcY, long SrcWid, long SrcHgt, BYTE LookupTable[])
    {
        long cX, cY;       // loop counters
        //long AbsX, AbsY;   // X/Y values in the destination.  This is used twice, so I calculate it only once to speed it up
    	long DestPos, SrcPos;
    
        // loop through all pixels...
        for (cX=0; cX<=SrcWid; cX++)
            for (cY=0; cY<=SrcHgt; cY++)
            {
                // see the resulting value of crossing the effect's R/G/B value with the
                // destination's R/G/B value, in the lookup table.
                //AbsX = dX + cX;
                //AbsY = dY + cY;
    			DestPos = (dX + cX) + (dY + cY)*TotalDestWidth;
    			SrcPos = (SrcX + cX) + (SrcY + cY)*TotalSrcWidth;
                //DestArray[AbsX + AbsY*TotalDestWidth] = LookupTable[DestArray[AbsX + AbsY*TotalDestWidth] + SrcArray[SrcX+cX + (SrcY+cY)*TotalSourceWidth]*255];
    			DestArray[DestPos] = LookupTable[DestArray[DestPos] + SrcArray[SrcPos]*255];
    
            }
    }
    The definitions are like this:

    LIBRARY SpecialEffects
    EXPORTS
    SpecialBltArray

    What's wrong? If you need, I can send you the whole C++ and VB projects...
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  7. #7
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    I don't really know, I've never made a DLL before... I guess you mean you're getting compile errors and not runtime errors? *Shrug*
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width