Qbasicnews.com

Full Version: the Cell C ver ( and some questions)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
1. How can I declare a dynamic 2d array in C?
ie.

Code:
float  buffer[320][200]   //gives me  array size too large

I'm assuming it's declared static but I want to allocate it on the heap. And reference it as " buffer[x][y]"

2. How do I pass 2d arrays in fucntions.
like in 1d,

Code:
int array[10];
    funk( array);

or
   funk(&array[0]);



now for the code: (enjoy!!!)


Code:
/*
cell texture renderer and dissolve effect
unoptimized c/asm code and spaghetti city coding style.
compile with Turbo C++ 3.0
Bug: How the hell do I make dynamic arrays in c that can be
    referenced as array[x][y] instead of using a pointer?
    Algo for dissolve from abe/Graphics gems.
    Cell tutorial by Blackhawk

    Relsoft 2004

*/

#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <stdlib.h>
#include <mem.h>
#include <malloc.h>
#include <dos.h>
#include <time.h>

//Try to define txmax as 320 and tymax as 200
#define txmax 128
#define tymax 64
#define maxpoints 32

void pcopy(char far *screen, char far *page);
void pset(char far *layer, int x, int y, char clr);
void cls(char far *page, unsigned char c );
void relmode13h(void);
void wait();
void dissolve(char far *dest, unsigned char c);
void pcopy_dissolve(char far *dest, char far *source);
void setpal(int i, char r, char g, char b);
float dist(float x, float y, float xc[], float yc[]);
char point(char far *page, int x, int y);

void main()
{
    int counter;
    int x, y, i;
    char c;
    char far *page_ptr;
    char far *screen_ptr;
    char far *ptr;
    float distbuffer[txmax][tymax];
    float xcoords[maxpoints];
    float ycoords[maxpoints];
    float mindist = 999999999, maxdist = 0;
    char image[txmax][tymax];
    char image2[txmax][tymax];


    screen_ptr = (char far*) 0xa0000000;
    page_ptr = (char far*) farmalloc(64000);

    if (page_ptr == 0)
    {
        clrscr();
        printf("\n Cannot allocate 64000 bytes!!!!");
        getch();
        return;
    }

    randomize();
    for (y = 0; y < tymax; y++)
    {
        for (x = 0; x < txmax; x++)
            distbuffer[x][y] = 0;
    }



    for (i = 0; i < maxpoints; i++)
    {
        xcoords[i] = rand() % txmax;
        ycoords[i] = rand() % tymax;
    }


    for (y = 0; y < tymax; y++)
    {
        for (x = 0; x < txmax; x++)
        {
            float d;
            float tx, ty;
            tx = fabs(x - (txmax/2));
            ty = fabs(y - (tymax/2));
            //to make the cell tile, use the absolute distance of
            // x and y from x and y / 2
            d = dist(tx, ty, xcoords, ycoords);
            distbuffer[x][y] = d;
            if (d < mindist) mindist = d;
            if (d > maxdist) maxdist = d;
        }
    }

    relmode13h();

    for (i = 0; i < 256; i++)
        setpal(i, i/4, i/4, i/4);



    //draw cell
    for (y = 0; y < tymax; y++)
    {
        for (x = 0; x < txmax; x++)
        {
            float c, c2;
            char col;
            c = (distbuffer[x][y] - mindist) / (maxdist - mindist);
            if (c < 0) c = 0;
            if (c > 1) c = 1;
            c = c;
            c2 =1 - c;
            col = c * 255;
            pset(screen_ptr, x, y, col);
            col = c2 * 255;
            pset(screen_ptr, x+128, y, col);

        }
    }

    for (y = 0; y < tymax; y++)
    {
        for (x = 0; x < txmax; x++)
        {
            image[x][y] = point(screen_ptr, x, y);
            image2[x][y] = point(screen_ptr, x+128, y);
        }
    }

    for (y = 0; y < 200; y += tymax)
    {
        for (x = 0; x < 320; x += txmax)
        {
            int tx, ty;
            for (ty = 0; ty < tymax; ty++)
            {
                for (tx = 0; tx < txmax; tx++)
                    pset(screen_ptr, x+tx, y+ty, image2[tx][ty]);
            }
        }
    }

    getch();
    counter=0;

    for (y = 0; y < 200; y += tymax)
    {
        for (x = 0; x < 320; x += txmax)
        {
            int tx, ty;
            for (ty = 0; ty < tymax; ty++)
            {
                for (tx = 0; tx < txmax; tx++)
                    pset(page_ptr, x+tx, y+ty, image2[tx][ty]);
            }
        }
    }

    getch();

    do{
        counter++;
        ptr = screen_ptr;
        for (y = 0; y < 200; y++)
        {
            for (x = 0; x < 320; x++)
                {
                c = image[x &(txmax-1)][(y+counter) &(tymax-1)];
                *ptr = c;
                ptr++;
                }
        }
        wait();
    }while(!kbhit());

    pcopy_dissolve(screen_ptr, page_ptr);
    getch();
    dissolve(screen_ptr, 05);
    getch();
    farfree(page_ptr);

}

void relmode13h()
{


    asm{
        mov ax, 0013h
        int 10h
        }

}

void pset(char far *layer, int x, int y, char clr)
{
    if ((x > -1) && (x < 320) && (y > -1) && (y < 200))
    {
        asm{
            mov ax, x
            mov cx, y
            les di, layer
            xchg cl, ch
            mov di, cx
            shr di, 2
            add di, cx
            add di, ax
            mov al, clr
            mov es:[di], al
            }
    }
}

void pcopy(char far *screen, char far *page)
{
    asm{
        push ds
        push di
        push si
        lds si, page
        les di, screen
        cld
        mov cx, 32000
        rep movsw
        pop si
        pop di
        pop ds
        }
}

void cls(char far *page, unsigned char c )
{
    asm{
        push di
        les di, page
        mov al, c
        mov ah, al
        cld
        mov cx, 32000
        rep stosw
        pop di
        }
}

void wait()
{
    while ( inportb(0x3da) & 8 );
    while ( !(inportb(0x3da) & 8) );

}

void dissolve(char far *dest, unsigned char c)
{
    asm{
        push di
        les     di, dest
        mov     cx, 0ffffh
        mov     di, 1
        mov     dx, 1024
        }
dis1:
    asm{
        mov     al, c            //ds:[di] if from source
        mov     es:[di],al
        shr     di,1
        jnc     noc              //di is calculated with an algorithm from
        xor     di,0b400h        //Graphic Gems I (Digital Dissolve effect)
        }
noc:
    asm{
        dec     dx
        jnz     ndel
        mov     dx, 1024         //every 900:th pixel make a delay by
        }
        wait();
ndel:
    asm{
        loop dis1
        pop di
        }
}


void pcopy_dissolve(char far *dest, char far *source)
{
    asm{
        push ds
        push si
        push di
        lds        si, source
        les     di, dest
        mov     cx, 0ffffh
        mov     di, 1
        mov     dx, 900
        }
dis1:
    asm{
        mov     al, ds:[di]      //ds:[di] if from source
        mov     es:[di],al
        shr     di,1
        jnc     noc              //di is calculated with an algorithm from
        xor     di,0b400h        //Graphic Gems I (Digital Dissolve effect)
        }
noc:
    asm{
        dec     dx
        jnz     ndel
        mov     dx,900
        }
        wait();
ndel:
    asm{
        loop dis1
        pop di
        pop si
        pop ds
        }
}

void setpal(int i, char r, char g, char b)
{
    asm{
        mov dx, 03c8h
        mov ax, i
        out dx, al
        inc dx
        mov al, r
        out dx, al
        mov al, g
        out dx, al
        mov al, b
        out dx, al
       }
}

char point(char far *page, int x, int y)
{
    char c;
    c = *(page+((y<<6) +(y<<8) + x));
    return (c);
}
float dist(float x, float y, float xc[], float yc[])
{
    float mindist = 999999999;
    int i;
    float a, b, d;

    for (i = 0; i < maxpoints; i++)
    {
        a = (xc[i] - x) * (xc[i] - x);
        b = (yc[i] - y) * (yc[i] - y);
        d = sqrt(a + b);
        if (d < mindist) mindist = d;
    }
    return (mindist);

}
ello
Tryed to compile it with djgpp, but it gave me a load of errors. I know you said to compile it with turbo cm but I don't have it.
DJGPP(being 32 bit) requires special coding to get some of the direct memory access functions to work.

Why use 2D arrays when you can fake 'em easy enough with 1D arrays or pointers.

I think for a dynamic array - do something like this:
Code:
char thing[];
I'm not 100% sure about that, however.
In this case direct addressing is much better than pointers.

so I need to dim it like 2d arrays instead of farmalloc()

Von: http://www.upseros.net/compiladores/comp...php#arriba

DL TurboC++3
You can make 2d arrays dynamically using a pointer to a pointer eg

Code:
long **p;

If look up pointers to pointers. This the common way of dynamically created 2d arrays if you want more info just post.

Quote:2. How do I pass 2d arrays in fucntions.
like in 1d,
It depends what your funcion asks for, check this article for more info:
http://www.iota-six.co.uk/c/f3_passing_a...ctions.asp

Hope that helps.
Tnx Rel!
Code:
­  File  Edit  Search  Run  Compile  Debug  Project  Options    Window  Help
┌──────────────────────────────────── 3D.C ──────────────────────────────1─────┐
│void pset(char far *layer, int x, int y, char clr)                            â”‚
│{                                                                             │
│   if ((x > -1) && (x < 320) && (y > -1) && (y < 200))                        â”‚
│   {                                                                          â”‚
│      asm{                                                                    â”‚
│         mov ax, x                                                            â”‚
│         mov cx, y                                                            â”‚
│         les di, layer                                                        â”‚
│         xchg cl, ch                                                          â”‚
│         mov di, cx                                                           │
│    --shr di, 2 <----                                                      â”‚
│         add di, cx                                                           │
│         add di, ax                                                           │
│         mov al, clr                                                          â”‚
│         mov es:[di], al                                                      â”‚
╔═[■]══════════════════════════════ Message ═════════════════════════════2═[↑]═╗
║ Compiling 3D.C:                                                              â–²
║•Error 3D.C 210: 286/287 instructions not enabled                             ▒
║                                                                              â–’
║                                                                              â– 
║                                                                              â–¼
╚═◄■▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒►─┘
I get that error.
Von: Be sure to check the 286/ 287 instructions on the compiler options.

Hard:

**p "need a lil more explanation)

BTW, I figured passing 2d arrays to functions already. :*) Made a lil whacked particle torus with it. :*)

I declared it like

Code:
float mat[4][4];

void rotmat(float mat[4][4]);
Hello again, now got past that problem... Now it says: Cannot allocate 64000 bytes!!!! Cry
rel, i think this declaration would be better :

void rotmat(float mat[][]);

and why dont you want to use pointers?
Pages: 1 2