VARIOS/JD_DEMO1.CPP
2021-09-12 19:56:57 +02:00

403 lines
8.7 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* ESPIRAL: dibuja espirales poligonales aleatorias */
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <dos.h>
#include <conio.h>
#include <graphics.h>
#include <stdio.h>
#define FONDO_BLANCO 0x40 // Ahora he puesto rojo sobre negro
#define FONDO_NEGRO 0x04
#define TAMA_MENSAJE 80
void muestra_mensaje(char far *);
void imprime(char *columna);
void corre_1izq(char far *p1);
void pone_atributos(void);
void copy_cadena(char *destino, char *origen);
char far *punt=(char far *) 0xb8000000;
char matriz[16];
char mensaje[TAMA_MENSAJE]="JD & VD: PRESENTAN..\0";
char far *dir, far *pfar;
char columna[16];
int speed=3;
char f=2;
/* constantes y variables globales */
#define PAUSA 0 /*3 segundos de pausa entre espirales*/
int maxcol; /*l¡mite de colores*/
int centroX; /*coordenadas del centro de la pantalla*/
int centroY;
double cuadra; /*factor de cuadratura*/
double pi; /*n£mero pi*/
double grad; /*constante de conversi¢n grados/radianes*/
int dimx,dimy; /*dimensiones pantalla (n£mero pixels)*/
int texto_grande(char *texto);
/* prototipos de funciones: */
extern int far Svga256_fdriver[];
int huge DetectVGA256()
{
int Vid=0;
return Vid;
}
void Inicia_Graficos(void);
void Muestra_256c(void);
void Espiral(void);
/********************************************************************
* main
********************************************************************/
void main(void)
{
Inicia_Graficos();
while (kbhit()) getch(); /*vac¡a buffer*/
Muestra_256c();
texto_grande("Esto es solo una demostacion");
texto_grande("de como puede ser nuestro");
texto_grande("DEMO_DEMO_DEMO");
while (! kbhit()) { /*sale con una tecla*/
Espiral(); /*dibuja una espiral*/
}
if (kbhit()) getch(); /*vac¡a posibles teclas*/
closegraph(); /*vuelve al modo texto*/
}
/********************************************************************
* Espiral: dibuja una espiral poligonal aleatoria.
********************************************************************/
void Espiral(void) /*<1>*/
{
double r; /* radio */
double ia; /* incremento  ngulo */
double ir; /* incremento radio */
double rlim; /* l¡mite radio */
double s,c,old_s,old_c; /* seno y coseno actuales <2> */
double ca,old_ca; /* coseno anterior */
double isin,icos; /* incremento seno y coseno */
int x,y;
int old_x,old_y;
int old_col;
int ang=0;
rlim = centroY*2.5; /*fija l¡mite*/
ia = (double)( (rand()%3600)/10.0); /*extrae incrementos <3>*/
ir = 1.0+(double)((rand()%200)/100.0);
isin = sin(ia*grad);
icos = cos(ia*grad);
setcolor( (old_col=1+rand()%96) ); /*y color, no negro <4>*/
cleardevice(); /*borra pantalla*/
////////// Comienza rotaci¢n sobre espiral: Aunque el resto del programa espiral
// es un parche de un ejemplo, esta funci¢n ha sido creada por J.D.
////////// Por favor no borre estas lineas...
ang=1;
do{
r = 0.0; /*parte del centro*/
old_s=sin(ang-1); s = sin(ang);
old_c=cos(ang-1); c = cos(ang);
x=old_x=centroX;
y=old_y=centroY;
while (r < rlim) {
moveto(old_x,old_y);
old_ca = old_c;
old_c = old_c * icos-old_s *isin;
old_s = old_s * icos+old_ca *isin;
old_x = centroX + (int)(floor(r* old_c * cuadra+0.5));
old_y = centroY + (int)(floor(r* old_s + 0.5));
setcolor(0);
lineto(old_x,old_y);
moveto(x,y);
ca = c; /*copia coseno*/
c = c * icos-s *isin; /*nuevo coseno <5>*/
s = s * icos+ca *isin; /*nuevo seno*/
x = centroX + (int)(floor(r * c * cuadra+0.5)); /*nuevas coordenadas <6>*/
y = centroY + (int)(floor(r * s + 0.5));
setcolor(old_col);
lineto(x,y); /*traza l¡nea*/
r += ir; /*incrementa radio*/
}
ang++;
}while( ang<180);
}
void Inicia_Graficos(void){
int Gd = 0;
int Gm = 0;
installuserdriver("Svga256",DetectVGA256);
/* If driver is linked with file, remove comments */
/* registerfarbgidriver(Svga256_fdriver); */
initgraph(&Gd,&Gm,"");
maxcol = getmaxcolor(); /*color m s alto*/
dimx = getmaxx()+1; /*y l¡mites pantalla*/
dimy = getmaxy()+1;
centroX = dimx/2; /*calcula centro <2>*/
centroY = dimy/2;
cuadra = (dimx/(double)dimy)/(4.0/3.0); /*cuadratura <3>*/
pi = 4.0*atan(1.0); /*n£mero pi <4>*/
grad = pi/180.0; /*constante convers. <5>*/
}
void Muestra_256c(void){
int a_x, a_y;
int x, y, col=0, col_e;
int x_x, y_y, X_x, Y_y;
a_x = dimx / 16;
a_y = dimy / 16;
if(maxcol==255)
for(y=0; y<16; y++)
for(x=0; x<16; x++) {
setfillstyle(1, col);
bar(x*a_x, y*a_y, a_x+(x*a_x), a_y+(y*a_y));
col++;
}
////// Elige un color, (no negro):
col=0;
col_e=(1+(rand()%2540)/10);
////// lo busca en la paleta:
for(y=0; y<16 && col!=col_e; y++)
for(x=0; x<16 && col!=col_e; x++) {
setfillstyle(1, 1);
bar(x*a_x, y*a_y, a_x+(x*a_x), a_y+(y*a_y));
delay(50);
setfillstyle(1, col);
bar(x*a_x, y*a_y, a_x+(x*a_x), a_y+(y*a_y));
col++;
}
////// lo amplia:
setfillstyle(1, col_e);
x_x = (x*a_x);
y_y = (y*a_y);
X_x = (a_x+(x*a_x));
Y_y = (a_y+(y*a_y));
while( x_x > 0 || X_x < 320 || y_y > 0 || Y_y < 200 ){
bar(x_x , y_y, X_x, Y_y );
if (x_x>0) x_x--;
if (X_x<320) X_x++;
if (y_y>0) y_y--;
if (Y_y<200) Y_y++;
}
char c[5];
settextstyle(0, 0, 24);
for(y=5; y>=0; y--){
for(x=0; x<5; x++) {
setfillstyle(1, 0);
bar(0,0,dimx,dimy);
setfillstyle(1, col_e);
bar(0,0,dimx,dimy);
}
sprintf(c, "%d", y);
outtextxy(64, 4, c);
}
}
int texto_grande(char *texto)
{
char *p=mensaje;
int ch=0,loop=0;
copy_cadena( p, texto);
// speed=atoi(argv[2]);
// if (((ch=(argv[3][0]))=='L') || ch=='l')
// loop=1;
// f=((argv[4][0]-'0')>1) ? 2 : 1;
closegraph(); /*vuelve al modo texto*/
// clrscr();
pone_atributos();
_setcursortype(_NOCURSOR);
asm {
push ax
push bx // Salvamos los registros principales ...
push cx // que ser n afectados por la interrupci¢n.
push dx
push es
push bp
mov ah,0x11
mov al,0x30
mov bh,6 // Juego de caracteres de la ROM-BIOS de 8*16
int 0x10 // Devuelve en ES:BP la direcci¢n del J. de chars.
}
pfar=(char far *) MK_FP(_ES, _BP);// pfar=DIR. del J. de Chars.
asm {
pop bp
pop es
pop dx // restauramos los registros
pop cx
pop bx
pop ax
}
do
muestra_mensaje(pfar);
while (!kbhit() && loop);
_setcursortype(_NORMALCURSOR);
punt=pfar=p=NULL;//Esto NO es necesario pero est  bien.
clrscr();
Inicia_Graficos();
return (0); //Si todo va bien retornamos 0.
}
void muestra_mensaje(char far *buffer)
{
int c,c1,si_no,i=0;
char ch;
si_no=0;
while (mensaje[i]) {
dir=buffer;
dir+=16*mensaje[i++];
for ( c=0; c<16; c++)
matriz[c]=*dir++;
for (c1=0; c1<8; c1++)
for ( c=0; c<16; c++){
ch=matriz[c];
ch&=0x80; // Para ver si el MSB est  a 1 o 0.
columna[c]=ch;
matriz[c]<<=1;
if (si_no++%2) { // Esto es para que no sean tan anchas
imprime(columna);//las letras (las dejo en la mitad)
delay(speed);
}
}
} // End while
} // End funci¢n muestra_mensaje
void imprime(char *columna)
{
int i;
char far *p1=(char far *) 0xb8000000;
// Aqui va una funci¢n que realiza un SCROLL horizontal a la izq.
for (i=0; i<79; i++) {
corre_1izq(p1+2);
p1+=2;
}
for ( i=0, p1=(char far *) 0xb8000000+(80*2)-2; i<16; i++, p1+=160*f)
if (*columna++) {
*p1='Û';
*(p1+160)='Û';
} else {
*p1=' ';
*(p1+160)=' ';
}
}
void corre_1izq(char far *p1)
{
int i;
char far *p=p1-2;
for ( i=0; i<25; i++) {
*p=*p1;
p+=160;
p1+=160;
}
}
void pone_atributos(void)
{
char far *p, far *p1=(char far *) 0xb8000000+1-2;
int i,j;
p=p1;
for (i=0; i<47; i++) {
p1+=2;
p=p1;
for (j=0; j<25; j++) {
*p=FONDO_BLANCO;
p+=160;
}
}
for (i=0; i<41; i++) {
p1+=2;
p=p1;
for (j=0; j<25; j++) {
*p=FONDO_NEGRO;
p+=160;
}
}
}
void copy_cadena(char *destino, char *origen){
while (*destino++=*origen++) /*nada*/;
}