include StdAfx include GL GL include Sprite Sprite Sprite float cx flo

 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
#include "StdAfx.h"
#include <GL\GL.h>
#include "Sprite.h"
Sprite::Sprite(float cx,float cy,float height,float width,float z)
{
frame=0;
vertex=new float [12];
textureCoord=new float [8];
texture=new unsigned int;
vertex[0]=cx-width/2;
vertex[1]=cy-height/2;
vertex[2]=z;
vertex[3]=cx+width/2;
vertex[4]=cy-height/2;
vertex[5]=z;
vertex[6]=cx+width/2;
vertex[7]=cy+height/2;
vertex[8]=z;
vertex[9]=cx-width/2;
vertex[10]=cy+height/2;
vertex[11]=z;
textureCoord[0]=0;
textureCoord[1]=1;
textureCoord[2]=1;
textureCoord[3]=1;
textureCoord[4]=1;
textureCoord[5]=0;
textureCoord[6]=0;
textureCoord[7]=0;
}
Sprite::~Sprite(void)
{
delete [] vertex;
delete [] textureCoord;
}
int Sprite::bindTexture(TextureStack *textureMan,int id){
unsigned char *pointer=textureMan->texPointers[id];
int texSizeW=textureMan->widthTex[id];
int texSizeH=textureMan->heightTex[id];
width=texSizeW;
height=texSizeH;
int glubin=textureMan->iglub[id];
glGenTextures(1,&texture[0]);
glBindTexture(GL_TEXTURE_2D,texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
if(glubin==3){
glTexImage2D(GL_TEXTURE_2D,0,3,texSizeW,texSizeH,0,GL_RGB,GL_UNSIGNED_BYTE,pointer);}
else
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,texSizeW,texSizeH,0,GL_RGBA,GL_UNSIGNED_BYTE,pointer);
return 0;}
void Sprite::render(void){
animation();
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glColor3f(1,1,1);
glBindTexture(GL_TEXTURE_2D,texture[0]);
glVertexPointer(3,GL_FLOAT,0,vertex);
glTexCoordPointer(2,GL_FLOAT,0,textureCoord);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawArrays(GL_QUADS,0,4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
}
void Sprite::animation(void){
float numframe=width/height;
float size=1/numframe;
if(frame>numframe)frame=0;
frame++;
textureCoord[0]=size*(frame-1);
textureCoord[2*1]=size*(frame);
textureCoord[2*2]=size*(frame);
textureCoord[2*3]=size*(frame-1);
}