Debian Codegraph @

Oodle/src/CImgLoader.cpp

raw
1 #include "sys/stat.h"
2 #include "oodle.h"
3
4 CSurface* CImgLoader::loadImage(const string& fileName, bool alpha, bool nopath)
5 {
6 LOG("Attempting to load image " << fileName, 3, LOG_INFO);
7 try
8 {
9 string file = "";
10 if (!nopath)
11 file = (getPath() + "img/" + fileName);
12 else
13 file = fileName;
14
15 CSurface* s = new CSurface( IMG_Load(file.c_str()) );
16 if (alpha)
17 {
18 s->displayFormatAlpha();
19 }
20 else
21 {
22 s->displayFormat();
23 }
24 return s;
25 }
26 catch(ex_illegalArguments)
27 {
28 _THROWEX(ex_sdlImageLoadFailed, "SDL Image Load Failed", "CImgLoader", "loadImage", "fileName = " << fileName);
29 }
30 }
31
32 list<CSurface*> CImgLoader::loadBigassBitmap(const string& fileName, int width, int height, int padding, bool alpha, bool nopath)
33 {
34 LOG("Attempting to load bigass bitmap from file " << fileName <<
35 " [ width=" << width << ", height=" << height << ", padding=" << padding << " ]", 3, LOG_INFO);
36
37 string file = "";
38 if (!nopath)
39 file = (getPath() + fileName);
40 else
41 file = fileName;
42
43 SDL_Surface* tmp = IMG_Load(file.c_str());
44 if (!tmp)
45 {
46 _THROWEX(ex_sdlImageLoadFailed, "SDL Image Load Failed", "CImgLoader", "loadBigassBitmap",
47 "fileName = " << fileName << ", width = " << width << ", height = " << height << ", padding = " << padding);
48 }
49 SDL_SetAlpha(tmp, 0, 0);
50
51 CSurface surf(tmp);
52
53 // first make sure image has correct dimensions
54 int surfaceWidth = surf.getWidth();
55 int surfaceHeight = surf.getHeight();
56
57 if (padding != 0)
58 {
59 if ( ! ((surfaceWidth % width) % padding == 0) || ! ((surfaceHeight % height) % padding == 0) ) // the remainder of surfaceWidth / tileWidth should be some multiple of the padding
60 {
61 _THROWEX(ex_illegalArguments, "Illegal frame width, height, and/or padding for given file", "CImgLoader", "loadBigassBitmap",
62 "fileName = " << fileName << ", width = " << width << ", height = " << height << ", padding = " << padding);
63 }
64 }
65 else
66 {
67 if ( ! ((surfaceWidth % width) == 0) || ! ((surfaceHeight % height) == 0) ) // the remainder of surfaceWidth / tileWidth should be some multiple of the padding
68 {
69 _THROWEX(ex_illegalArguments, "Illegal frame width, height, and/or padding for given file", "CImgLoader", "loadBigassBitmap",
70 "fileName = " << fileName << ", width = " << width << ", height = " << height << ", padding = " << padding);
71 }
72 }
73 int numCols = surfaceWidth / width;
74 int numRows = surfaceHeight / height;
75 list<CSurface*> tmpList;
76
77 for (int y = 0; y < numRows; y++)
78 {
79 for (int x = 0; x < numCols; x++)
80 {
81 int pixX = x * (width + padding);
82 int pixY = y * (height + padding);
83
84 CSurface* s2 = surf.getSection(pixX, pixY, width, height);
85 if (alpha)
86 {
87 s2->displayFormatAlpha();
88 }
89 else
90 {
91 s2->displayFormat();
92 }
93 tmpList.push_back(s2);
94 }
95 }
96 return tmpList;
97 }
98
99 string CImgLoader::getPath()
100 {
101 const string testImg = "fonts/Vera.ttf";
102
103 static string imgPath = "";
104 struct stat st;
105
106 if (imgPath != "")
107 {
108 return imgPath;
109 }
110 else
111 {
112 #ifdef PKGDATADIR
113 if (stat((PKGDATADIR + ("/assets/" + testImg)).c_str(), &st) == 0)
114 {
115 imgPath = PKGDATADIR + string("/assets/");
116 }
117 else if (stat(("./assets/" + testImg).c_str(), &st) == 0)
118 #else
119 if (stat(("./assets/" + testImg).c_str(), &st) == 0)
120 #endif
121 {
122 imgPath = "./assets/";
123 }
124 else if (stat(("../assets/" + testImg).c_str(), &st) == 0)
125 {
126 imgPath = "../assets/";
127 }
128 else if (stat(("./" + testImg).c_str(), &st) == 0)
129 {
130 imgPath = "./";
131 }
132 else if (stat(("./TetrisAttack/assets/" + testImg).c_str(), &st) == 0)
133 {
134 imgPath = "./TetrisAttack/assets/";
135 }
136 else
137 {
138 LOG("Unable to locate the image path", 2, LOG_ERROR);
139 _THROWEX(ex_sdlImageLoadFailed, "unable to locate the image path",
140 "CImgLoader", "getPath", "");
141 }
142
143 LOG("Using " << imgPath << " for loading images", 1, LOG_INFO);
144 return imgPath;
145 }
146 }