This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How Would I Code This In C?, Java to C; I'm sure I need some * and malloc()'s in there.

Hi. Because of the lack of java support on my '51, I'm now attempting to learn C.

But it isn't going so great. C's complicated syntax is spanking me with compiler errors. I can see why there's courses for this language. It's not something you just start typing, and it works.

Anyway, I'm a noob when it comes to compiled languages. I have no idea what needs to be fixed. I figured since in C there's no garbage collector, it'd be a good idea to have a big list of all the surfaces loaded into memory, so I can keep track of them, and delete them through that object. Good concept anyway, right?

So I figured....arr[16] of pointers that points to arr[64] of pointers to surfaces. That combo allows up to 1024 surfaces, which is more than I could possibly use, while also keeping memory usage to ~(16*4 + n*64*4 + overhead) bytes.

Unfortunately, that concept maps very poorly into actual code, since my brain and fingers don't know how to get it working.

So anyways...Java:

public class imageCache
    {
    public int MaxChunks = 16;
    public int ChunkSize = 64;
    public int maxSize = MaxChunks*ChunkSize;
    public int Count = 0;

    public Chunk[] Chunks = new Chunk[MaxChunks];



    final public boolean outOfBounds(int iNum)
        {
        return (iNum < 0 || iNum >= maxSize);
        }



    final public SDL_Surface get(int iOffset)
        {
        if(outOfBounds(iOffset)) return null;
        int iChunk = Count >> 6;
        int iIndex = Count & 63;


        if(Chunks[iChunk] == null) return null;
        return Chunks[iChunk].Arr[iIndex]; // May be null
        }



    final public int add(SDL_Surface mySurf)
        {
        if(Count >= maxSize) return -1;
        int iChunk = Count >> 6;
        int iIndex = Count & 63;


        if(Chunks[iChunk] == null) Chunks[iChunk] = new Chunk(ChunkSize);
        Chunks[iChunk].Arr[iIndex] = mySurf;


        iIndex = Count;
        Count++;
        return iIndex; // Return Index/Offset
        }



    private class Chunk
        {
        public SDL_Surface Arr[];

        Chunk(int iSize)
            {
            Arr = new SDL_Surface[iSize];
            }
        }
    }

If anyone has the time to convert that, I'd be grateful.

-Kramy

Parents
  • "C's complicated syntax..."

    The syntax of 'C' is not complicated.

    Mastering how to use the language is, of course, an entirely different matter!

    "I can see why there's courses for this language"

    So take one!

    "It's not something you just start typing..."

    Certainly it isn't - so don't try to do that!

    Porting from one language to another requires a sound understanding of both languages!

    Programming embedded systems - especially small embedded systems based on the 8051 or similar - also requires a sound understanding of the underlying platform.

    See the 'Books' page for both general programming books, and books specifically about the 8051: http://www.keil.com/books/

    See also: www.8052.com/books.phtml

    For details of the 8051 architecture, you will need to study the so-called "bible" for the 8051:

    Chapter 1 - 80C51 Family Architecture:
    www.nxp.com/.../80C51_FAM_ARCH_1.pdf

    Chapter 2 - 80C51 Family Programmer's Guide and Instruction Set:
    www.nxp.com/.../80C51_FAM_PROG_GUIDE_1.pdf

    Chapter 3 - 80C51 Family Hardware Description:
    www.nxp.com/.../80C51_FAM_HARDWARE_1.pdf

    And, of course, the Data Sheet(s) for your particular processor, platform, and any other peripherals you are using.

    If you can't (or won't) do this yourself, try: http://www.keil.com/condb

Reply
  • "C's complicated syntax..."

    The syntax of 'C' is not complicated.

    Mastering how to use the language is, of course, an entirely different matter!

    "I can see why there's courses for this language"

    So take one!

    "It's not something you just start typing..."

    Certainly it isn't - so don't try to do that!

    Porting from one language to another requires a sound understanding of both languages!

    Programming embedded systems - especially small embedded systems based on the 8051 or similar - also requires a sound understanding of the underlying platform.

    See the 'Books' page for both general programming books, and books specifically about the 8051: http://www.keil.com/books/

    See also: www.8052.com/books.phtml

    For details of the 8051 architecture, you will need to study the so-called "bible" for the 8051:

    Chapter 1 - 80C51 Family Architecture:
    www.nxp.com/.../80C51_FAM_ARCH_1.pdf

    Chapter 2 - 80C51 Family Programmer's Guide and Instruction Set:
    www.nxp.com/.../80C51_FAM_PROG_GUIDE_1.pdf

    Chapter 3 - 80C51 Family Hardware Description:
    www.nxp.com/.../80C51_FAM_HARDWARE_1.pdf

    And, of course, the Data Sheet(s) for your particular processor, platform, and any other peripherals you are using.

    If you can't (or won't) do this yourself, try: http://www.keil.com/condb

Children
No data