-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSurface.cpp
More file actions
78 lines (68 loc) · 2.06 KB
/
Surface.cpp
File metadata and controls
78 lines (68 loc) · 2.06 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "Surface.h"
std::unordered_map<cairo_surface_t*, unsigned> Surface::surfaces;
Surface::Surface() {
mSurface = NULL;
}
Surface::Surface(const char* filePath) {
setSurface(filePath);
}
Surface::~Surface() {
safeDestroySurface();
}
// safe delete
void Surface::safeDestroySurface() {
if (mSurface == NULL) return; // nothing to be done
--surfaces[mSurface];
if (surfaces[mSurface] == 0) {
surfaces.erase(mSurface);
cairo_surface_destroy(mSurface);
}
mSurface = NULL;
}
// assignment operator
Surface& Surface::operator=(const Surface& surface) {
mSurface = surface.mSurface;
++surfaces[mSurface];
return *this;
}
// CCtor
Surface::Surface(const Surface& surface) {
mSurface = surface.mSurface;
++surfaces[mSurface];
}
void Surface::setSurface(const char* filePath) {
safeDestroySurface();
mSurface = cairo_image_surface_create_from_png (filePath);
++surfaces[mSurface];
switch(cairo_surface_status(mSurface)) {
case CAIRO_STATUS_SUCCESS:
return;
case CAIRO_STATUS_NULL_POINTER:
std::cerr << "Oops we messed up on our end." << std::endl <<
"Please notify Vaughn that Harry screwed up." << std::endl;
break;
case CAIRO_STATUS_NO_MEMORY:
std::cerr << "Out of memory" << std::endl;
break;
case CAIRO_STATUS_READ_ERROR:
std::cerr << "Error reading file" << std::endl;
break;
case CAIRO_STATUS_INVALID_CONTENT:
std::cerr << "Invalid Content" << std::endl;
break;
case CAIRO_STATUS_INVALID_FORMAT:
std::cerr << "Invalid Format" << std::endl;
break;
case CAIRO_STATUS_INVALID_VISUAL:
std::cerr << "Invalid Visual" << std::endl;
break;
default:
std::cerr << "Invalid Error" << std::endl;
break;
}
// should only reach this point if unsuccessful
safeDestroySurface();
}
cairo_surface_t* Surface::getSurface() const {
return mSurface;
}