-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.cpp
More file actions
36 lines (32 loc) · 1.01 KB
/
background.cpp
File metadata and controls
36 lines (32 loc) · 1.01 KB
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
#include "background.hpp"
Background::Background(SDL_Surface* display)
{
this->display = display;
this->bg_surf = NULL;
}
Background::~Background()
{
if(bg_surf != NULL)
SDL_FreeSurface(bg_surf);
}
bool Background::loadFile(std::string filename)
{
if(bg_surf != NULL)
SDL_FreeSurface(bg_surf);
if((bg_surf = IMG_Load(filename.c_str())) != NULL)
return true;
else
return false;
}
void Background::draw(int x, int y)
{
SDL_Rect bg_pos = { x % bg_surf->w, y % bg_surf->h, 320, 240 }; // TODO make width + height variable (window size)
SDL_Rect dst_pos = { 0, 0, 320, 240 };
SDL_BlitSurface(bg_surf, &bg_pos, display, &dst_pos);
if((x % bg_surf->w) + 320 > bg_surf->w) // fill in the rest when we're scrolling "between" instances of the bg
{ // TODO make this work for vertical scrolling as well
SDL_Rect bg_fill_pos = { 0, y % bg_surf->h, 320, 240 };
SDL_Rect fill_dst = { bg_surf->w - (x % bg_surf->w), 0, 320, 240 };
SDL_BlitSurface(bg_surf, &bg_fill_pos, display, &fill_dst);
}
}