Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern "C" {
- #include"SDL.h"
- }
- #define _USE_MATH_DEFINES
- #include<math.h>
- #include<stdio.h>
- #include<string.h>
- #if defined(WINDOWS) || defined(WIN32) || defined(_WIN32)
- #include<windows.h>
- #endif
- //The attributes of the screen
- const int SCREEN_WIDTH = 640;
- const int SCREEN_HEIGHT = 480;
- const int SCREEN_BPP = 32;
- //The surfaces
- SDL_Surface *background = NULL;
- SDL_Surface *alien = NULL;
- SDL_Surface *screen = NULL;
- SDL_Surface *charset = NULL;
- SDL_Surface *spaceship = NULL;
- SDL_Surface *bullet = NULL;
- bool shot=false;
- int shipwidth=50;
- int y=SCREEN_HEIGHT-shipwidth;
- double alienY=0;
- double alienX=0;
- bool quit=false;
- int alienstab[6][6];
- double direction=1;
- void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
- {
- //Holds offsets
- SDL_Rect offset;
- //Get offsets
- offset.x = x;
- offset.y = y;
- //Blit
- SDL_BlitSurface( source, clip, destination, &offset );
- }
- // DrawString teraz jest write_string
- // narysowanie napisu txt na powierzchni screen, zaczynając od punktu (x, y)
- // charset to bitmapa 128x128 zawierająca znaki
- void write_string(SDL_Surface *screen, int x, int y, const char *text, SDL_Surface *charset)
- {
- int px, py, c;
- SDL_Rect s, d;
- s.w = 8;
- s.h = 8;
- d.w = 8;
- d.h = 8;
- while(*text)
- {
- c = *text & 255;
- px = (c % 16) * 8;
- py = (c / 16) * 8;
- s.x = px;
- s.y = py;
- d.x = x;
- d.y = y;
- SDL_BlitSurface(charset, &s, screen, &d);
- x += 8;
- text++;
- };
- };
- void write_time(SDL_Surface *screen, SDL_Surface *charset, double totaltime)
- {
- int width, height;
- char text[128];
- apply_surface(0, 0, background, screen, NULL);
- width = screen->w;
- height = screen->h;
- sprintf(text, "doskonale bawisz sie juz %.1lf sekund", totaltime);
- write_string(screen, width / 2 - strlen(text) * 8 / 2, 10, text, charset);
- sprintf(text, "Esc lub krzyzyk - wyjscie", totaltime);
- write_string(screen, width / 2 - strlen(text) * 8 / 2, 26, text, charset);
- };
- void move_aliens(SDL_Surface *screen, SDL_Surface *alien, double delta)
- {
- double alienV=50;
- //const int SCREEN_WIDTH = 640;
- //const int SCREEN_HEIGHT = 480;
- int alienw=44;
- int alienh=36;
- int x=0;
- int y=0;
- alienX+=alienV*delta*direction;
- int d=5; //odlegosc miedzy alienami na szerokosc
- int h=3; //odlegolsc miedzy alienami na wysokosc
- for(x=0; x<6; x++)
- {
- for(y=0; y<6; y++)
- {
- if(alienstab[x][y]==1)
- {
- apply_surface(alienX + x*(alienw+d), alienY+(y*alienh), alien, screen, NULL);
- }
- }
- }
- if(alienX + x*(alienw+d) > SCREEN_WIDTH)
- {
- direction=-1;
- alienY=alienY+alienh;
- }
- if(alienX<0)
- {
- direction=1;
- alienY=alienY+alienh;
- }
- if(alienY+(y*alienh)+50 >SCREEN_HEIGHT)
- {
- quit=true;
- }
- }
- void draw_bullet(int x, int y, SDL_Surface *bullet)
- {
- if(y>10)
- {
- apply_surface(x, y, bullet, screen, NULL);
- }
- else
- {
- shot=false;
- y=SCREEN_HEIGHT-shipwidth;
- }
- }
- void clean_up()
- {
- // zwolnienie powierzchni
- SDL_FreeSurface(background);
- SDL_FreeSurface(alien);
- SDL_FreeSurface(charset);
- SDL_FreeSurface(spaceship);
- SDL_FreeSurface(bullet);
- SDL_Quit();
- }
- // WinMain
- #if defined(WINDOWS) || defined(WIN32) || defined(_WIN32)
- int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR str, int show) {
- #else
- int main(int argc, const char **argv) {
- #endif
- int t1, t2;
- double totaltime;
- double angle;
- double delta;
- SDL_Event event;
- double shipV = 500;
- int bulletV= 300;
- Uint8 *keystates = SDL_GetKeyState(NULL);
- if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
- {
- printf("error: %s\n", SDL_GetError());
- return 1;
- };
- screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
- if(screen == NULL)
- {
- printf("SerVideoMode error: %s\n", SDL_GetError());
- return 1;
- };
- //Set the window caption
- SDL_WM_SetCaption( "spejs inwejders", NULL );
- // wyłączenie widoczności kursora myszy
- SDL_ShowCursor(SDL_DISABLE);
- // wczytanie obrazka cs8x8.bmp
- charset = SDL_LoadBMP("CS8X8.BMP");
- // ustawienie w powierzchni charset koloru przezroczystego
- // od tej chwili pixele koloru 0x000000 na tej powierzchni (czarne)
- // będą przezroczyste
- SDL_SetColorKey(charset, SDL_SRCCOLORKEY, 0x000000);
- alien = SDL_LoadBMP("elien.bmp");
- SDL_SetColorKey(alien, SDL_SRCCOLORKEY, 0x000000);
- spaceship = SDL_LoadBMP("mem.bmp");
- SDL_SetColorKey(spaceship, SDL_SRCCOLORKEY, 0xFF0000);
- bullet = SDL_LoadBMP("rainbow.bmp");
- background = SDL_LoadBMP("chet.bmp");
- //alienstab is full of aliens. ;)
- for(int i=0; i<6; i++)
- {
- for(int j=0; j<6; j++)
- {
- alienstab[i][j]=1;
- }
- }
- //Time management
- totaltime=0;
- //t1 is constant during whole thing
- t1 = SDL_GetTicks();
- //Apply the bg to the screen
- apply_surface( 0, 0, background, screen );
- //Update the screen
- if( SDL_Flip( screen ) == -1 )
- {
- return 1;
- }
- double shipX=320;
- int x;
- while(quit==false)
- {
- t2 = SDL_GetTicks();
- // w tym momencie t2-t1 to czas w milisekundach,
- // jaki uplynal od ostatniego narysowania ekranu
- // delta to czas ostatniej klatki w sekundach
- delta = (t2 - t1) * 0.001;
- t1 = t2;
- totaltime +=delta;
- write_time(screen, charset, totaltime);
- move_aliens(screen, alien, delta);
- apply_surface(shipX, 430, spaceship, screen, NULL);
- if(shot==true)
- {
- y-=bulletV*delta;
- draw_bullet(x, y, bullet);
- }
- //Update the screen
- if( SDL_Flip( screen ) == -1 )
- {
- return 1;
- }
- //While there's events to handle
- while( SDL_PollEvent( &event ) )
- {
- //If the user has Xed out the window
- if( event.type == SDL_QUIT )
- {
- //Quit the program
- quit = true;
- }
- if( event.type == SDL_KEYDOWN )
- {
- switch( event.key.keysym.sym )
- {
- case SDLK_ESCAPE:
- {
- quit = true;
- break;
- }
- case SDLK_SPACE:
- {
- if(shot==false)
- {
- x=shipX+18;
- y=430;
- shot=true;
- }
- break;
- }
- }
- }
- }
- //If left is pressed
- if( keystates[ SDLK_LEFT ] )
- {
- if(shipX>0)
- {
- shipX-=delta*shipV;
- }
- }
- //If right is pressed
- if( keystates[ SDLK_RIGHT ] )
- {
- if(shipX<SCREEN_WIDTH-shipwidth)
- {
- shipX+=delta*shipV;
- }
- }
- }
- clean_up();
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment