10-15-2004, 11:39 AM
1. How can I declare a dynamic 2d array in C?
ie.
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,
now for the code: (enjoy!!!)
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);
}