forked from bethgrace5/game
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbethanyA.cpp
More file actions
258 lines (226 loc) · 7.15 KB
/
Copy pathbethanyA.cpp
File metadata and controls
258 lines (226 loc) · 7.15 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <GL/glx.h>
#include <string.h>
#include "Sprite.h"
#include "functions.h"
//====================================================================
//
//=====================================================================
//Setting Up
//=====================================================================
// -- This sections includes all functions that changes and initialize
// the variables in the sprite class.
//Default Contructor
Sprite::Sprite(){
//when making a Sprite Variable, sets all variables within to 0 or NULL
imageName = NULL;
texture = 0;
clipX = clipY = 0;
clipWidth = clipHeight = 0;
index = 0;
setClip(0, 0);
imageHeight = 0;
imageWidth = 0;
tileAt = 0;
indexX = indexY = 0;
background = 0;
}
void Sprite::setIndex(int ind){
index = ind;
}
int Sprite::getIndex() {
return index;
}
void Sprite::insert(const char *filename, int x, int y){
//Gets the number of images place in a row and column x , y
//then gets the name of the image file
setFile(filename);
initSprite();
setClip(x, y);
setSize(imageWidth * clipX, imageHeight * clipY);
}
void Sprite::setFile(const char *filename){
//Change the imageName to ppm file. imageName is used in the initSprite();
imageName = filename;
strcpy(save, imageName);
}
void Sprite::setClip(int x, int y){
//User defined row and column. The images must evenly split apart from each
//other. ClipX and ClipY determines where to cut parts in the image.
row = x; column = y;
if(x > 0) clipX = (float)1/row;
if(y > 0) clipY = (float)1/column;
}
void Sprite::setSize(int x, int y){
//Sets the size of sprite shown
clipWidth = x;
clipHeight = y;
}
void Sprite::initSprite(){
//Get an image in input inside the texure
image = ppm6GetImage(imageName);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
// glEnable (GL_BLEND);
// glBlendFunc (GL_ONE, GL_ONE);
// glEnable(GL_BLEND);
// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
unsigned char *silhouetteData = buildAlphaData(image);
imageWidth = image->width;
imageHeight = image->height;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageWidth, imageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, silhouetteData);
delete [] silhouetteData;
}
void Sprite::reInitSprite(){
//Refresh the new Sprite Image
imageName = &save[0];
image = ppm6GetImage(imageName);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
unsigned char *silhouetteData = buildAlphaData(image);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageWidth, imageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, silhouetteData);
delete [] silhouetteData;
}
void Sprite::replaceTexture(GLuint take){
//It replaces the texture with the texture it got
//... but this function is not well defined yet
texture = take;
}
//=====================================================================
//Outter Functions
//=====================================================================
// These are functions that are use to call drawing events using the variables
// within this class. Also, giving out this class information
//This Draws a tile base on row number and column number
//example:
// 1 2 3 4
// 1 # # # #
// 2 # # # #
// 3 # # # #
//
void Sprite::drawTile(){
drawTile(getIndexX(), getIndexY());
}
void Sprite::drawTile(int row, int column){
//Need to check if 0;
int atX = row; int atY = column;
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glEnable(GL_ALPHA_TEST);
if(background == 0) glAlphaFunc(GL_GREATER, 0.0f);
else glAlphaFunc(GL_LESS, 1.0f);
glColor4ub(255,255,255,255);
glBegin(GL_QUADS);
int w = clipWidth;
int h = clipHeight;
if(!checkMirror()){
glTexCoord2f(atX*clipX , (atY*clipY)+clipY) ; glVertex2i(-w,-h);
glTexCoord2f(atX*clipX , atY*clipY) ; glVertex2i(-w,h);
glTexCoord2f((atX*clipX)+clipX, atY*clipY) ; glVertex2i(w,h);
glTexCoord2f((atX*clipX)+clipX, (atY*clipY)+clipY) ; glVertex2i(w,-h);
}else{
glTexCoord2f((atX*clipX)+clipX, (atY*clipY)+clipY) ; glVertex2i(-w,-h);
glTexCoord2f((atX*clipX)+clipX, atY*clipY) ; glVertex2i(-w,h);
glTexCoord2f(atX*clipX , atY*clipY) ; glVertex2i(w,h);
glTexCoord2f(atX*clipX , (atY*clipY)+clipY) ; glVertex2i(w,-h);
}
glEnd(); glPopMatrix();
glDisable(GL_TEXTURE_2D);
glDisable(GL_ALPHA_TEST);
}
void Sprite::drawTile(int row, int column, int w, int h){
//Need to check if 0;
int atX = row; int atY = column;
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.0f);
glColor4ub(255,255,255,255);
glBegin(GL_QUADS);
if(!checkMirror()){
glTexCoord2f(atX*clipX , (atY*clipY)+clipY) ; glVertex2i(-w,-h);
glTexCoord2f(atX*clipX , atY*clipY) ; glVertex2i(-w,h);
glTexCoord2f((atX*clipX)+clipX, atY*clipY) ; glVertex2i(w,h);
glTexCoord2f((atX*clipX)+clipX, (atY*clipY)+clipY) ; glVertex2i(w,-h);
}else{
glTexCoord2f((atX*clipX)+clipX, (atY*clipY)+clipY) ; glVertex2i(-w,-h);
glTexCoord2f((atX*clipX)+clipX, atY*clipY) ; glVertex2i(-w,h);
glTexCoord2f(atX*clipX , atY*clipY) ; glVertex2i(w,h);
glTexCoord2f(atX*clipX , (atY*clipY)+clipY) ; glVertex2i(w,-h);
}
glEnd(); glPopMatrix();
glDisable(GL_TEXTURE_2D);
glDisable(GL_ALPHA_TEST);
}
//This Draws a tile base on line # base on below example
// 1 2 3 4 5 6 7
// 8 9 10 11 12 13 14
//
void Sprite::drawTile(int atSet){
int atX = atSet, atY = 0;
atX = atSet % row;
atY = (int)(atSet/row);
drawTile(atX, atY);
}
//This will use the drawTile(int atSet)
//Continously calling this function will switch to the next sprite in the sheet
//Starting from the left then to right. It will do this for each row. Then reset
//back to the start.
void Sprite::drawSequence(){
drawTile(tileAt); tileAt++;
if(tileAt > row*column) tileAt = 0;
}
//Gives out the texture data/value
GLuint Sprite::textureBox(){
return texture;
}
int Sprite::getClipHeight(){
return clipHeight;
}
int Sprite::getClipWidth(){
return clipWidth;
}
int Sprite::getColumn(){
return column;
}
int Sprite::getRow(){
return row;
}
void Sprite::setMirror(bool reflect){
mirror = reflect;
}
bool Sprite::checkMirror(){
return mirror;
}
void Sprite::setBackground(bool back){
background = back;
}
bool Sprite::checkBackground(){
return background;
}
int Sprite::getIndexX(){
return indexX;
}
int Sprite::getIndexY(){
return indexY;
}
int Sprite::getIndexAt(){
return indexAt;
}
void Sprite::setIndexAt(int ind){
if(ind < (row*column)) ind = 0;
indexAt = ind;
}
void Sprite::setIndexXY(int x, int y){
if(x > row) x = 0;
if(y > column) y = 0;
indexX = x;
indexY = y;
}