Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SDL/SDL.h"
- #include <ctime>
- #include <stdlib.h>
- #include "surfaces.h"
- #include "classes.h"
- void load_sprite(int x, int y, int bx, int by, int width, int height, SDL_Surface *source, SDL_Surface *destination) {
- SDL_Rect rect;
- SDL_Rect offset;
- rect.x = bx;
- rect.y = by;
- rect.w = width;
- rect.h = height;
- offset.x = x;
- offset.y = y;
- SDL_BlitSurface(source, &rect, destination, &offset);
- }
- void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination) {
- SDL_Rect rect;
- rect. x = x;
- rect.y = y;
- SDL_BlitSurface(source, NULL, destination, &rect);
- }
- int main(int argc, char *args[]) {
- SDL_Init(SDL_INIT_EVERYTHING);
- srand(time(NULL));
- Uint8 *key;
- bool gameLoop = true;
- SDL_Event event;
- //Load classes.h var's
- cPlayer.x = 640/2;
- cPlayer.y = 480/2;
- cPoint.x = rand() % (640 - 16) + 0;
- cPoint.y = rand() % (480 - 16) + 0;
- apply_surface(0, 0, background, screen);
- SDL_Flip(screen);
- //Gameloop
- while(gameLoop == true) {
- SDL_PumpEvents();
- key = SDL_GetKeyState(NULL);
- if(key[SDLK_DOWN] || key[SDLK_s]) {
- cPlayer.y++;
- }
- if(key[SDLK_UP] || key[SDLK_w]) {
- cPlayer.y--;
- }
- if(key[SDLK_RIGHT] || key[SDLK_d]) {
- cPlayer.x++;
- }
- if(key[SDLK_LEFT] || key[SDLK_a]) {
- cPlayer.x--;
- }
- if(key[SDLK_ESCAPE]) {
- gameLoop = false;
- }
- if(event.type == SDL_QUIT) {
- gameLoop = false;
- }
- apply_surface(0, 0, background, screen);
- load_sprite(cPlayer.x, cPlayer.y, 0, 0, 16, 16, tileset, screen);
- load_sprite(cPoint.x, cPoint.y, 16, 0, 16, 16, tileset, screen);
- if(cPlayer.y > 480) {
- cPlayer.y = 0;
- } else if(cPlayer.y < 0) {
- cPlayer.y = 480;
- }
- if(cPlayer.x > 640) {
- cPlayer.x = 0;
- } else if(cPlayer.x < 0) {
- cPlayer.x = 640;
- }
- if(cPoint.y>cPlayer.y && cPoint.y<cPlayer.y+16) {
- if(cPoint.x>cPlayer.x && cPoint.x<cPlayer.x+16) {
- cPoint.x = rand() % (640 - 16) + 0;
- cPoint.y = rand() % (480 - 16) + 0;
- }
- }
- SDL_Delay(30/1000);
- SDL_Flip(screen);
- }
- SDL_FreeSurface(background);
- SDL_FreeSurface(tileset);
- SDL_Quit();
- }
Advertisement
Add Comment
Please, Sign In to add comment