Created
November 12, 2014 00:00
-
-
Save Jakz/8d56562ea4c519d83b36 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <conio.h> | |
| #include <dos.h> | |
| #include <math.h> | |
| #include <memory.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <time.h> | |
| #include <sys/timeb.h> | |
| #include "keyhandl.h" | |
| #include <sys/nearptr.h> | |
| #include <dpmi.h> | |
| #include <go32.h> | |
| #include <sys/segments.h> | |
| //VARIABILI PER LA CURVA DEL TESTO | |
| float val_x = 0; | |
| float val_y[8]; | |
| float cos_ = 0; | |
| float cos_value = 0.04; | |
| float x_speed = 1; | |
| float amplitude = 30; | |
| //BUFFER VGA | |
| unsigned char buffer_vga[320*200]; | |
| unsigned char *vga=(unsigned char*)(0xa0000+__djgpp_conventional_base); | |
| //FONT | |
| unsigned char carattere[55][25]; | |
| void apri_vga(void) { | |
| union REGS reg; | |
| reg.x.ax = 0x13; | |
| int86(0x10,®,®); | |
| } | |
| void chiudi_vga(void) { | |
| union REGS reg; | |
| reg.x.ax = 0x03; | |
| int86(0x10,®,®); | |
| } | |
| void pixel(int x,int y,unsigned char color) { | |
| if ((x<0) || (x>=320) || (y<0) || (y>=200)) return; | |
| buffer_vga[x+(y*320)] = color; | |
| } | |
| void cls(unsigned char color) { | |
| memset(buffer_vga,color,320*200); | |
| } | |
| void wait_retrace(void) { | |
| while(1) { | |
| if (inp(0x03da)!=0x08) break; } | |
| } | |
| void swap_buffers(void) { | |
| memcpy(vga,buffer_vga,320*200); | |
| } | |
| void blendImage( unsigned char *image1, unsigned char *image2, int coef ) | |
| { | |
| for (int i=0; i<64000; i++) | |
| { | |
| buffer_vga[i] = image1[i] + ( ( image2[i] - image1[i] ) * coef >> 8 ); | |
| } | |
| } | |
| void masked_blit(int sprite_x, int sprite_y, int pos_x, int pos_y, unsigned char *sprite) { | |
| unsigned char mask_color = 0x00; | |
| for (int i=0;i<sprite_y;++i) { | |
| for (int j=0;j<sprite_x;++j) | |
| if (sprite[i+(j*sprite_x)] != mask_color) buffer_vga[(pos_x+j)+((pos_y+i)*320)] = sprite[i+(j*sprite_x)]; | |
| } | |
| } | |
| void set_palette(int begin, int end, unsigned char palette[][3]) { | |
| for (int i=begin;i<end;++i) { | |
| outp (0x03C8,i); | |
| outp (0x03C9,palette[i][1]); | |
| outp (0x03C9,palette[i][2]); | |
| outp (0x03C9,palette[i][3]); } | |
| } | |
| void get_color(unsigned char i, unsigned char &R, unsigned char &G, unsigned char &B) { | |
| outp (0x03C7,i); | |
| R = inp (0x03C9); | |
| G = inp (0x03C9); | |
| B = inp (0x03C9); | |
| } | |
| void set_color( unsigned char i, unsigned char r, unsigned char g, unsigned char b ) { | |
| outp(0x03c8,i); | |
| outp(0x03c9,r); | |
| outp(0x03c9,g); | |
| outp(0x03c9,b); | |
| } | |
| unsigned char keystatus[128]; | |
| void newkeyboard(void) | |
| { | |
| unsigned char key=inportb(0x60); | |
| keystatus[key&127]=(key&128)^128; | |
| outportb(0x20,0x20); | |
| } | |
| void endnewkeyboard(void){} | |
| _go32_dpmi_seginfo oldkeyboard,keywrap; | |
| char keyset=0; | |
| void setkeyboard(void) | |
| { | |
| int x; | |
| __dpmi_meminfo m; | |
| for(x=0;x<128;x++) keystatus[x]=0; | |
| if(keyset) return; | |
| m.address=(int)keystatus; | |
| m.size=128; | |
| __dpmi_lock_linear_region(&m); // lock data | |
| m.address=(int)newkeyboard; | |
| m.size=(int)endnewkeyboard-(int)newkeyboard; | |
| __dpmi_lock_linear_region(&m); // lock code | |
| keywrap.pm_offset=(int)newkeyboard; | |
| keywrap.pm_selector=_my_cs(); | |
| _go32_dpmi_allocate_iret_wrapper(&keywrap); // setup wrapper | |
| _go32_dpmi_get_protected_mode_interrupt_vector(0x9,&oldkeyboard); | |
| _go32_dpmi_set_protected_mode_interrupt_vector(0x9,&keywrap); | |
| keyset=1; | |
| } | |
| void resetkeyboard(void) | |
| { | |
| if(!keyset) return; | |
| _go32_dpmi_set_protected_mode_interrupt_vector(0x9,&oldkeyboard); | |
| _go32_dpmi_free_iret_wrapper(&keywrap); | |
| keyset=0; | |
| } | |
| void blit_cos_words(void) { | |
| cos_ += cos_value; | |
| for (int i=0;i<8;++i) val_y[i] = 100 + (cos(cos_-(0.2*i))*amplitude); | |
| if (val_x < 320) val_x += x_speed; | |
| else val_x = 0; | |
| masked_blit(5,5,val_x-(0*6),val_y[0],carattere[17]); | |
| masked_blit(5,5,val_x-(1*6),val_y[1],carattere[4]); | |
| masked_blit(5,5,val_x-(2*6),val_y[2],carattere[10]); | |
| masked_blit(5,5,val_x-(3*6),val_y[3],carattere[2]); | |
| masked_blit(5,5,val_x-(4*6),val_y[4],carattere[0]); | |
| masked_blit(5,5,val_x-(5*6),val_y[5],carattere[9]); | |
| masked_blit(5,5,val_x-(6*6),val_y[6],carattere[8]); | |
| masked_blit(5,5,val_x-(7*6),val_y[7],carattere[7]); | |
| } | |
| void apri_font(void) { | |
| FILE *font = fopen("font.raw","r+b"); | |
| for (int j=0;j<55;++j) | |
| fread(carattere[j], 1, 25, font); | |
| fclose(font); | |
| } | |
| void fade_to_black(void) { | |
| unsigned char r=1,g=1,b=1; | |
| int count = 0; | |
| while (1) { | |
| if (count == 64) break; | |
| count = 0; | |
| delay(30); | |
| wait_retrace(); | |
| for (int i=0;i<64;++i) { | |
| get_color(i,r,g,b); | |
| if (r > 0) r--; else r =0; | |
| if (g > 0) g--; else g =0; | |
| if (b > 0) b--; else b =0; | |
| if ((r==0) && (g==0) && (b==0)) ++count; | |
| set_color(i,r,g,b); } | |
| } | |
| } | |
| void star_field(void) { | |
| struct TStar | |
| { | |
| float x; | |
| float y; | |
| unsigned char plane; | |
| }; | |
| set_color(0,0,0,0); | |
| set_color(1,15,15,15); | |
| set_color(2,30,30,30); | |
| set_color(3,45,45,45); | |
| set_color(4,60,60,60); | |
| TStar *stars; | |
| stars = new TStar[256]; | |
| for (int i=0;i<256;++i) | |
| { | |
| stars[i].x = rand() % 320; | |
| stars[i].y = rand() % 200; | |
| stars[i].plane = rand() % 5; | |
| } | |
| while (1) { | |
| delay(10); | |
| wait_retrace(); | |
| cls(0); | |
| for (int i=0;i<256;++i) | |
| { | |
| stars[i].x += (1+(float)stars[i].plane)*0.15; | |
| if (stars[i].x > 160) | |
| { | |
| stars[i].y += ((stars[i].x-160)*(stars[i].x-160))/20000; | |
| } | |
| if ((stars[i].y > 200) || (stars[i].x > 320)) { stars[i].x = 0; stars[i].y = rand() % 200; } | |
| pixel(stars[i].x,stars[i].y,stars[i].plane); | |
| } | |
| if (keystatus[KEY_ESC]) {resetkeyboard(); chiudi_vga(); exit(1);} | |
| if (keystatus[KEY_UP]) cos_value+= 0.0002; | |
| if (keystatus[KEY_DOWN]) cos_value-= 0.0002; | |
| if (keystatus[KEY_LEFT]) x_speed-= 0.02; | |
| if (keystatus[KEY_RIGHT]) x_speed+= 0.02; | |
| if (keystatus[KEY_MINUS_PAD]) amplitude-= 0.1; | |
| if (keystatus[KEY_PLUS_PAD]) amplitude+= 0.1; | |
| if (keystatus[KEY_R]) {cos_value = 0.04; x_speed = 1; amplitude = 30;} | |
| if (keystatus[KEY_2]) break; | |
| blit_cos_words(); | |
| swap_buffers(); | |
| } | |
| } | |
| void cross_fade(void) { | |
| int counter; | |
| long long start_time = uclock(); | |
| for (int i=0;i<256;++i) set_color(i,i>>2,i>>2,i>>2); | |
| FILE *img1 = fopen("image1.raw","r+b"); | |
| FILE *img2 = fopen("image2.raw","r+b"); | |
| unsigned char *image1; | |
| unsigned char *image2; | |
| image1 = (unsigned char *)malloc(64000); | |
| image2 = (unsigned char *)malloc(64000); | |
| fread(image1, 1, 64000, img1); | |
| fread(image2, 1, 64000, img2); | |
| while (1) { | |
| int timing = (uclock()-start_time>>14)&0x3ff; | |
| if (timing<256) memcpy( buffer_vga, image1, 64000 ); | |
| else if (timing<512) blendImage( image1, image2, timing-256 ); | |
| else if (timing<768) memcpy( buffer_vga, image2, 64000 ); | |
| else blendImage( image2, image1, timing-768 ); | |
| if (keystatus[KEY_ESC]) {resetkeyboard(); chiudi_vga(); exit(1);} | |
| swap_buffers(); | |
| } | |
| fclose(img1); | |
| fclose(img2); | |
| } | |
| //>>>>>>>>>>>>>>>>>>>>>>INIZIO MAIN<<<<<<<<<<<<<<<<<<<<<<< | |
| main() { | |
| __djgpp_nearptr_enable(); | |
| setkeyboard(); | |
| apri_vga(); | |
| wait_retrace(); | |
| apri_font(); | |
| star_field(); | |
| fade_to_black(); | |
| cross_fade(); | |
| resetkeyboard(); | |
| chiudi_vga(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment