-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbackground.cpp
51 lines (33 loc) · 1.27 KB
/
background.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Taylor Seale, Ryan Shea, Iheanyi Ekechukwu, Andrew Caron
// Fundamentals of Computing II: Final Project
// background.cpp: This is the implementation file for the Background class which handles the background of the game
#include "SDL/SDL.h"
#include "background.h"
#include <iostream>
using namespace std;
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* dest) {
//Temporary rectangle to hold the offsets
SDL_Rect offset; //Get the offsets (position)
offset.x = x;
offset.y = y;
//Blit the surface
SDL_BlitSurface(source, NULL, dest, &offset);
}
Background::Background(const char *image, SDL_Surface *screen){
dx = 0;
picture = SDL_LoadBMP(image); //load in the picture to be used as background
if(picture==NULL){ //if image doesn't load
cout << "Error opening image" << endl;
}
SDL_BlitSurface(picture,NULL,screen,NULL); //copy picture to the screen
SDL_Flip(screen); //update the screen
}
void Background::scroll(int velocity, SDL_Surface *screen){
dx -= velocity; //scroll
//if the background has gone too far
if(dx <= -picture->w){dx = 0;}
//show the background
apply_surface(dx,0,picture,screen);
apply_surface(dx + picture->w,0,picture,screen);
SDL_Flip(screen); //update the screen
}