Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SDL/SDL.h"
- #include <ctime>
- #include <stdlib.h>
- #include "player.cpp"
- #include "platform.h"
- SDL_Surface *tileset = SDL_LoadBMP("images/tileset.bmp");
- SDL_Surface *screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
- void load_sprite(int x, int y, SDL_Surface *source, SDL_Surface *destination, int bx, int by, int width, int height) {
- SDL_Rect rect;
- SDL_Rect offset;
- offset.x = x;
- offset.y = y;
- rect.x = bx;
- rect.y = by;
- rect.w = width;
- rect.h = height;
- SDL_BlitSurface(source, &rect, destination, &offset);
- }
- void game() {
- srand(time(NULL));
- Uint8 *key;
- bool gameLoop = true;
- SDL_Surface *background = SDL_LoadBMP("images/background.bmp");
- Uint32 colorkey = SDL_MapRGB(tileset -> format, 255, 0, 255);
- SDL_SetColorKey(tileset, SDL_SRCCOLORKEY, colorkey);
- cPlayer.x = 640/2-16;
- cPlayer.y = 480-32;
- cPlayer.xspeed = 10;
- cPlayer.yspeed = 50;
- cPlayer.gravity = 5;
- cPlayer.grav = true;
- cPlatform.x[1] = 10;
- cPlatform.y[1] = 480-32;
- cPlatform.x[2] = 60;
- cPlatform.y[2] = 480-32;
- cPlatform.x[3] = 320;
- cPlatform.y[3] = 480-32;
- int mouse_x = 55;
- int mouse_y = 55;
- int currentTick = SDL_GetTicks();
- int maxFPS = 30;
- while(gameLoop) {
- SDL_PumpEvents();
- key = SDL_GetKeyState(NULL);
- SDL_GetMouseState(&mouse_x, &mouse_y);
- currentTick = SDL_GetTicks();
- load_sprite(0, 0, background, screen, 0, 0, 640, 480);
- /*
- for(int i=0; i<=3; i++) {
- load_sprite(cPlatform.x[i], cPlatform.y[i], tileset, screen, 32+2, 1, 32, 32);
- }
- */
- load_sprite(cPlayer.x, cPlayer.y, tileset, screen, 1, 1, 32, 32);
- playerMovment();
- if(key[SDLK_ESCAPE]) {
- gameLoop = false;
- }
- if(SDL_GetTicks() - currentTick < 1000 / maxFPS) {
- SDL_Delay(((1000/maxFPS)-(SDL_GetTicks() - currentTick)));
- }
- SDL_Flip(screen);
- }
- SDL_FreeSurface(tileset);
- SDL_FreeSurface(background);
- }
Advertisement
Add Comment
Please, Sign In to add comment