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 is spanking me with compiler errors.

    C's syntax isn't that much more complex than Java's - probably even less since it does not have classes, etc.

    Anyway, I'm a noob when it comes to compiled languages.

    Java is a compiled language. It's just not compiled to run on a particular processor, but rather a defined virtual machine.

    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?

    What object ? The C compiler does not know about objects. Programs in C usually aren't object-oriented (unless the programmer jumps through quite a few hoops).

    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.

    A standard '51 has 256 bytes of RAM ! Even if you set n to 1, your construct will not fit in there.

    And what is a surface ?

    In your code, I see references to SDL_something - as far as I am aware, the SDL library is used to program stuff on PCs, not on microcontrollers.

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

    Here's what you need to do:

    1. Get a good textbook on C, read it, understand it, try out the examples.
    2. Get some literature (including the datasheets) on the '51. Familiarize yourself with it, and especially throw most notions of "PC programming" overboard.
    3. Analyze the Java code and find out what it does. Write C code that does the same thing.

    If this is too much work, there's an easier alternative:

    1. Hire (and pay) a consultant to do the job for you.

Reply
  • C's complicated syntax is spanking me with compiler errors.

    C's syntax isn't that much more complex than Java's - probably even less since it does not have classes, etc.

    Anyway, I'm a noob when it comes to compiled languages.

    Java is a compiled language. It's just not compiled to run on a particular processor, but rather a defined virtual machine.

    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?

    What object ? The C compiler does not know about objects. Programs in C usually aren't object-oriented (unless the programmer jumps through quite a few hoops).

    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.

    A standard '51 has 256 bytes of RAM ! Even if you set n to 1, your construct will not fit in there.

    And what is a surface ?

    In your code, I see references to SDL_something - as far as I am aware, the SDL library is used to program stuff on PCs, not on microcontrollers.

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

    Here's what you need to do:

    1. Get a good textbook on C, read it, understand it, try out the examples.
    2. Get some literature (including the datasheets) on the '51. Familiarize yourself with it, and especially throw most notions of "PC programming" overboard.
    3. Analyze the Java code and find out what it does. Write C code that does the same thing.

    If this is too much work, there's an easier alternative:

    1. Hire (and pay) a consultant to do the job for you.

Children
No data