desdemona

spejs invejders

Dec 20th, 2011
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.87 KB | None | 0 0
  1. extern "C" {
  2. #include"SDL.h"
  3. }
  4. #define _USE_MATH_DEFINES
  5. #include<math.h>
  6. #include<stdio.h>
  7. #include<string.h>
  8. #if defined(WINDOWS) || defined(WIN32) || defined(_WIN32)
  9. #include<windows.h>
  10. #endif
  11.  
  12. #define the2PI 2*(3,14159)
  13.  
  14. //The attributes of the screen
  15. const int SCREEN_WIDTH = 640;
  16. const int SCREEN_HEIGHT = 480;
  17. const int SCREEN_BPP = 32;
  18.  
  19. //The surfaces
  20. SDL_Surface *background = NULL;
  21. SDL_Surface *alien = NULL;
  22. SDL_Surface *screen = NULL;
  23. SDL_Surface *charset = NULL;
  24. SDL_Surface *spaceship = NULL;
  25. SDL_Surface *bullet = NULL;
  26.  
  27.  
  28. bool shot=false;
  29. int y=430;
  30. double alienY=0;
  31. bool quit=false;
  32.  
  33. int alienstab[6][6];
  34.  
  35.  
  36. void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
  37. {
  38.     //Holds offsets
  39.     SDL_Rect offset;
  40.  
  41.     //Get offsets
  42.     offset.x = x;
  43.     offset.y = y;
  44.  
  45.     //Blit
  46.     SDL_BlitSurface( source, clip, destination, &offset );
  47. }
  48.  
  49. // DrawString teraz jest write_string
  50. // narysowanie napisu txt na powierzchni screen, zaczynając od punktu (x, y)
  51. // charset to bitmapa 128x128 zawierająca znaki
  52. void write_string(SDL_Surface *screen, int x, int y, const char *text, SDL_Surface *charset)
  53. {
  54.     int px, py, c;
  55.     SDL_Rect s, d;
  56.     s.w = 8;
  57.     s.h = 8;
  58.     d.w = 8;
  59.     d.h = 8;
  60.     while(*text)
  61.     {
  62.         c = *text & 255;
  63.         px = (c % 16) * 8;
  64.         py = (c / 16) * 8;
  65.         s.x = px;
  66.         s.y = py;
  67.         d.x = x;
  68.         d.y = y;
  69.         SDL_BlitSurface(charset, &s, screen, &d);
  70.         x += 8;
  71.         text++;
  72.     };
  73. };
  74.  
  75. void write_time(SDL_Surface *screen, SDL_Surface *charset, double totaltime)
  76. {
  77.     int width, height;
  78.     char text[128];
  79.  
  80.     apply_surface(0, 0, background, screen, NULL);
  81.  
  82.     width = screen->w;
  83.     height = screen->h;
  84.  
  85.    
  86.     sprintf(text, "doskonale bawisz sie juz %.1lf sekund", totaltime);
  87.     write_string(screen, width / 2 - strlen(text) * 8 / 2, 10, text, charset);
  88.     sprintf(text, "Esc lub krzyzyk - wyjscie", totaltime);
  89.     write_string(screen, width / 2 - strlen(text) * 8 / 2, 26, text, charset);
  90. };
  91.  
  92. void move_aliens(SDL_Surface *screen, SDL_Surface *alien, double delta)
  93. {
  94.     double alienV=100;
  95.  
  96.     int alienw=44;
  97.     int alienh=36;
  98.  
  99.     int x=0;
  100.     int y=0;
  101.  
  102.     alienY+=alienV*delta;
  103.  
  104.     apply_surface(0, alienY, alien, screen, NULL);
  105.     apply_surface(alienw+1, alienY, alien, screen, NULL);
  106.  
  107.     if(alienY+alienh>SCREEN_HEIGHT)
  108.     {
  109.         quit=true;
  110.     }
  111. }
  112.  
  113. void draw_bullet(int x, int y, SDL_Surface *bullet)
  114. {
  115.     if(y>10)
  116.     {
  117.         apply_surface(x, y, bullet, screen, NULL);
  118.     }
  119.     else
  120.     {
  121.         shot=false;
  122.         y=430;
  123.     }
  124. }
  125.  
  126. void clean_up()
  127. {
  128.     // zwolnienie powierzchni
  129.     SDL_FreeSurface(background);
  130.     SDL_FreeSurface(alien);
  131.     SDL_FreeSurface(charset);
  132.     SDL_FreeSurface(spaceship);
  133.     SDL_FreeSurface(bullet);
  134.  
  135.     SDL_Quit();
  136. }
  137.  
  138. // WinMain
  139. #if defined(WINDOWS) || defined(WIN32) || defined(_WIN32)
  140. int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR str, int show) {
  141. #else
  142. int main(int argc, const char **argv) {
  143. #endif
  144.  
  145.     int t1, t2;
  146.     double totaltime;
  147.     double angle;
  148.     double delta;
  149.     SDL_Event event;
  150.  
  151.     double shipV = 500;
  152.     int bulletV= 300;
  153.  
  154.  
  155.  
  156.     Uint8 *keystates = SDL_GetKeyState(NULL);
  157.  
  158.  
  159.     if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
  160.     {
  161.         printf("error: %s\n", SDL_GetError());
  162.         return 1;
  163.     };
  164.  
  165.     screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
  166.  
  167.  
  168.     if(screen == NULL)
  169.     {
  170.         printf("SerVideoMode error: %s\n", SDL_GetError());
  171.         return 1;
  172.     };
  173.  
  174.     //Set the window caption
  175.     SDL_WM_SetCaption( "spejs inwejders", NULL );
  176.  
  177.  
  178.     // wyłączenie widoczności kursora myszy
  179.     SDL_ShowCursor(SDL_DISABLE);
  180.  
  181.     // wczytanie obrazka cs8x8.bmp
  182.     charset = SDL_LoadBMP("CS8X8.BMP");
  183.     // ustawienie w powierzchni charset koloru przezroczystego
  184.     // od tej chwili pixele koloru 0x000000 na tej powierzchni (czarne)
  185.     // będą przezroczyste
  186.     SDL_SetColorKey(charset, SDL_SRCCOLORKEY, 0x000000);
  187.  
  188.     alien = SDL_LoadBMP("elien.bmp");
  189.     SDL_SetColorKey(alien, SDL_SRCCOLORKEY, 0x000000);
  190.  
  191.     spaceship = SDL_LoadBMP("mem.bmp");
  192.     SDL_SetColorKey(spaceship, SDL_SRCCOLORKEY, 0xFF0000);
  193.  
  194.     bullet = SDL_LoadBMP("rainbow.bmp");
  195.  
  196.     background = SDL_LoadBMP("chet.bmp");
  197.  
  198.     for(int i=0; i>6; i++)
  199.     {
  200.         for(int j=0; j>6; j++)
  201.         {
  202.             alienstab[i][j]=1;
  203.         }
  204.     }
  205.  
  206.     //Time management
  207.     totaltime=0;
  208.     //t1 is constant during whole thing
  209.     t1 = SDL_GetTicks();
  210.  
  211.     //Apply the bg to the screen
  212.     apply_surface( 0, 0, background, screen );
  213.  
  214.     //Update the screen
  215.     if( SDL_Flip( screen ) == -1 )
  216.     {
  217.         return 1;
  218.     }
  219.  
  220.     double shipX=320;
  221.     bool keyup=false;
  222.     int x;
  223.  
  224.  
  225.     while(quit==false)
  226.     {
  227.         t2 = SDL_GetTicks();
  228.  
  229.         // w tym momencie t2-t1 to czas w milisekundach,
  230.         // jaki uplynal od ostatniego narysowania ekranu
  231.         // delta to czas ostatniej klatki w sekundach
  232.         delta = (t2 - t1) * 0.001;
  233.         t1 = t2;
  234.         totaltime +=delta;
  235.  
  236.         write_time(screen, charset, totaltime);
  237.         move_aliens(screen, alien, delta);
  238.         apply_surface(shipX, 430, spaceship, screen, NULL);
  239.  
  240.         if(shot==true)
  241.         {
  242.             y-=bulletV*delta;
  243.             draw_bullet(x, y, bullet);
  244.         }
  245.  
  246.         //Update the screen
  247.         if( SDL_Flip( screen ) == -1 )
  248.         {
  249.             return 1;
  250.         }
  251.  
  252.         //While there's events to handle
  253.         while( SDL_PollEvent( &event ) )
  254.         {
  255.             //If the user has Xed out the window
  256.             if( event.type == SDL_QUIT )
  257.             {
  258.                 //Quit the program
  259.                 quit = true;
  260.             }
  261.  
  262.             if( event.type == SDL_KEYDOWN )
  263.             {
  264.                 switch( event.key.keysym.sym )
  265.                 {
  266.                 case SDLK_ESCAPE:
  267.                     {
  268.                         quit = true;
  269.                         break;
  270.                     }
  271.                 case SDLK_SPACE:
  272.                     {
  273.                         if(shot==false)
  274.                         {
  275.                             x=shipX+18;
  276.                             y=430;
  277.                             shot=true;
  278.                         }
  279.                         break;
  280.                     }
  281.                 }
  282.             }
  283.         }
  284.  
  285.         //If left is pressed
  286.         if( keystates[ SDLK_LEFT ] )
  287.         {
  288.             if(shipX>0)
  289.             {
  290.                 shipX-=delta*shipV;
  291.                    
  292.             }
  293.         }
  294.  
  295.         //If right is pressed
  296.         if( keystates[ SDLK_RIGHT ] )
  297.         {
  298.             if(shipX<590)
  299.             {
  300.                 shipX+=delta*shipV;
  301.             }
  302.         }
  303.     }
  304.  
  305.     clean_up();
  306.     return 0;
  307.  
  308.     };
Advertisement
Add Comment
Please, Sign In to add comment