So here are some proggies:
1 and 2 works seamlessly as I didn't use a D buffer and it seems pointer to screen works. But 3 just bombs. Why?
I even used mostly C only code.
The ones with //?????? seems to be the problem in #3.
#1
Code:
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>
#include <math.h>
void setscreen13(void);
void writergb(char cindex, char r, char g, char b);
void main()
{
int x, y;
int c, i;
char red, green, blue;
int short counter;
int rot, xoffs, yoffs;
char far *screen;
char far *pixel_ptr;
int sin1[2048];
int sin2[2048];
int sin3[2048];
screen = (char far*) 0xa0000000;
for (i=0; i<2048;i++)
{
sin1[i] = sin((float)(i)/64)*256;
sin2[i] = sin((float)(i)/16)*28;
sin3[i] = sin((float)(i)/64)*64;
}
setscreen13();
for (i=0; i<256;i++)
{
red = 32 - 31 * sin((float) i * 3.141593 /32);
green = 32 - 31 * sin((float) i * 3.141593 /64);
blue = 32 - 31 * sin((float) i * 3.141593 /28);
writergb(i, red, green, blue);
}
counter = 0;
rot = 0;
xoffs = 0;
yoffs = 0;
do
{
counter++;
rot = 128 * (((counter && 1) == 1) || 1);
pixel_ptr = screen;
for (y =0; y<200; y++)
{
for (x =0; x<319; x++)
{
rot = -rot;
c = sin1[(128+x+rot)&2047] + sin3[(y)&2047]+ sin2[(counter+x+y)&2047] + sin1[(x+y)&1023];
*(pixel_ptr) = (char)c;
pixel_ptr++;
}
pixel_ptr++;
}
} while(!kbhit());
}
void setscreen13(void)
{
asm mov ax, 0013h
asm int 10h
}
void writergb(char cindex, char r, char g, char b)
{
asm mov dx, 03c8h
asm mov al, cindex
asm out dx, al
asm inc dx
asm mov al, r
asm out dx, al
asm mov al, g
asm out dx, al
asm mov al, b
asm out dx, al
}
#2
Code:
/*This works!!!*/
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <alloc.h>
#include <string.h>
void xsetscreen13(void);
void xputpixel(char far *layer, int x, int y, char c);
void xwritergb(char cindex, char r, char g, char b);
void pcopy(char far *dest, char far *source);
void xwait();
void xcls(char far *dest, unsigned char c);
struct startype
{
float x;
float y;
float z;
float zvel;
};
void main()
{
int x, y;
int i;
char clr;
int distance;
char red, green, blue;
char far *vpage;
char far *screen;
float sx, sy, sz;
struct startype stars[301];
vpage = (char far*)malloc(64000);
screen = (char far*)0xa0000000;
randomize();
xsetscreen13();
for (i=0; i<256; i++)
{
red = i/4;
green= i/4;
blue = i/4;
xwritergb((char)i, red,green,blue);
}
for (i=0; i<301; i++)
{
stars[i].x = -60 + rand() % 120;
stars[i].y = -60 + rand() % 120;
stars[i].z = (rand()) % 255;
stars[i].zvel = 1 + rand() % 2;
}
sx = 0;
sy = 0;
sz = 0;
distance = 0;
do
{
//xcls(vpage, 0);
//erase loop
for (i=0; i<301; i++)
{
sx = stars[i].x;
sy = stars[i].y;
sz = stars[i].z;
distance = (256 - sz);
if (distance > 0)
{
x = 160 + (256 * sx / distance);
y = 100 - (256 * sy / distance);
}
clr = 0;
xputpixel (screen, x, y, clr);
}
for (i=0; i<301; i++)
{
stars[i].z = stars[i].z + stars[i].zvel;
if (stars[i].z > 255)
stars[i].z = 0;
sx = stars[i].x;
sy = stars[i].y;
sz = stars[i].z;
distance = (256 - sz);
if (distance > 0)
{
x = 160 + (256 * sx / distance);
y = 100 - (256 * sy / distance);
}
clr = (char)sz & 255;
xputpixel (screen, x, y, clr);
}
xwait();
//pcopy(screen, vpage);
}while(!kbhit());
free(vpage);
}
void xsetscreen13(void)
{
asm{
mov ax, 0013h
int 10h
}
}
void xputpixel(char far *layer, int x, int y, char c)
{
if ((x>=0) && (x<320) && (y>=0) && (y<200))
layer[(y<<8) + (y<<6) + x] = c;
}
void xwritergb(char cindex, char r, char g, char b)
{
asm {
mov dx, 03c8h
mov al, cindex
out dx, al
inc dx
mov al, r
out dx, al
mov al, g
out dx, al
mov al, b
out dx, al
}
}
void xwait()
{
while (inportb(0x3da) & 0x08);
while (!(inportb(0x3da) & 0x08));
}
void pcopy(char far *dest, char far *source)
{
memcpy(dest,source, 64000);
}
void xcls(char far *dest, unsigned char c)
{
memset(dest, c, 64000);
}
#3: This won't work
Code:
/* This bombs!!! Uses a dbuffer(suppossedly)*/
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <alloc.h>
#include <string.h>
void xsetscreen13(void);
void xputpixel(char far *layer, int x, int y, char c);
void xwritergb(char cindex, char r, char g, char b);
void pcopy(char far *dest, char far *source);
void xwait();
void xcls(char far *dest, unsigned char c);
struct startype
{
float x;
float y;
float z;
float zvel;
};
void main()
{
int x, y;
int i;
char clr;
int distance;
char red, green, blue;
char far *vpage;
char far *screen;
float sx, sy, sz;
struct startype stars[301];
vpage = (char far*)malloc(64000);
screen = (char far*)0xa0000000;
randomize();
xsetscreen13();
for (i=0; i<256; i++)
{
red = i/4;
green= i/4;
blue = i/4;
xwritergb((char)i, red,green,blue);
}
for (i=0; i<301; i++)
{
stars[i].x = -60 + rand() % 120;
stars[i].y = -60 + rand() % 120;
stars[i].z = (rand()) % 255;
stars[i].zvel = .1 + rand() % 2;
}
sx = 0;
sy = 0;
sz = 0;
distance = 0;
do
{
xcls(vpage, 0); //???????
for (i=0; i<301; i++)
{
stars[i].z = stars[i].z + stars[i].zvel;
if (stars[i].z > 255)
stars[i].z = 0;
sx = stars[i].x;
sy = stars[i].y;
sz = stars[i].z;
distance = (256 - sz);
if (distance > 0)
{
x = 160 + (256 * sx / distance);
y = 100 - (256 * sy / distance);
}
clr = (char)sz & 255;
xputpixel (vpage, x, y, clr); //???????
}
xwait();
pcopy(screen, vpage); //???????
}while(!kbhit());
free(vpage);
}
void xsetscreen13(void)
{
asm{
mov ax, 0013h
int 10h
}
}
void xputpixel(char far *layer, int x, int y, char c)
{
if ((x>=0) && (x<320) && (y>=0) && (y<200))
layer[(y<<8)+(y<<6)+x] = c;
}
void xwritergb(char cindex, char r, char g, char b)
{
asm {
mov dx, 03c8h
mov al, cindex
out dx, al
inc dx
mov al, r
out dx, al
mov al, g
out dx, al
mov al, b
out dx, al
}
}
void xwait()
{
while (inportb(0x3da) & 0x08);
while (!(inportb(0x3da) & 0x08));
}
void pcopy(char far *dest, char far *source)
{
memcpy(dest,source, 64000);
}
void xcls(char far *dest, unsigned char c)
{
memset(dest, c, 64000);
}