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
|
#include <UIClass.h>
#include <ft2build.h>
#include FT_FREETYPE_H
extern Player player;
extern World *currentWorld;
static FT_Library ftl;
static FT_Face ftf;
static GLuint ftex;
static unsigned int fontSize;
namespace ui {
int mousex, mousey;
bool debug = false;
void init(const char *ttf){
if(FT_Init_FreeType(&ftl)){
std::cout<<"Error! Couldn't initialize freetype."<<std::endl;
abort();
}
if(FT_New_Face(ftl,ttf,0,&ftf)){
std::cout<<"Error! Couldn't open "<<ttf<<"."<<std::endl;
abort();
}
}
void setFontSize(unsigned int fs){
fontSize=fs;
FT_Set_Pixel_Sizes(ftf,0,fontSize);
}
void putString(const float x,const float y,const char *s){
unsigned int i=0,j;
float xo=x,yo=y,w,h;
char *buf;
do{
if(s[i]=='\n'){
xo=x;
yo-=fontSize*.0022;
}else{
if(FT_Load_Char(ftf,s[i],FT_LOAD_RENDER)){
std::cout<<"Error! Invalid character."<<std::endl;
return;
}
//glActiveTexture(GL_TEXTURE0);
glGenTextures(1,&ftex);
glBindTexture(GL_TEXTURE_2D,ftex);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
buf=(char *)malloc(ftf->glyph->bitmap.width*ftf->glyph->bitmap.rows*4);
for(j=0;j<ftf->glyph->bitmap.width*ftf->glyph->bitmap.rows;j++){
buf[j*4]=255;
buf[j*4+1]=255;
buf[j*4+2]=255;
buf[j*4+3]=ftf->glyph->bitmap.buffer[j]?255:0;
}
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,ftf->glyph->bitmap.width,ftf->glyph->bitmap.rows,0,GL_RGBA,GL_UNSIGNED_BYTE,buf);
w=ftf->glyph->bitmap.width*(2.0/SCREEN_WIDTH);
h=ftf->glyph->bitmap.rows *(2.0/SCREEN_HEIGHT);
if(s[i]=='\''||
s[i]=='\"'||
s[i]=='-'||
s[i]=='*'){
yo+=fontSize*.001;
}
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,ftex);
glBegin(GL_QUADS);
glColor3ub(255,255,255);
glTexCoord2f(0,1);glVertex2f(xo,yo);
glTexCoord2f(1,1);glVertex2f(xo+w,yo);
glTexCoord2f(1,0);glVertex2f(xo+w,yo+h);
glTexCoord2f(0,0);glVertex2f(xo,yo+h);
glEnd();
glDisable(GL_TEXTURE_2D);
if(s[i]=='\''||
s[i]=='\"'||
s[i]=='-'||
s[i]=='*'){
yo-=fontSize*.001;
}
xo+=w+(fontSize*.0002);
free(buf);
}
}while(s[i++]);
}
void putText(const float x,const float y,const char *str,...){
va_list args;
char *buf;
buf=(char *)calloc(128,sizeof(char));
va_start(args,str);
vsnprintf(buf,128,str,args);
va_end(args);
putString(x,y,buf);
free(buf);
}
void msgBox(const char *str,...){
va_list args;
va_start(args,str);
glColor3ub(0,0,0);
glRectf(-1,.6,1,1);
setFontSize(24);
putText(-1,1-24*.0022,str,args);
va_end(args);
}
void handleEvents(){
static bool space=false;
float thing;
SDL_Event e;
while(SDL_PollEvent(&e)){
switch(e.type){
case SDL_MOUSEMOTION:
mousex=e.motion.x;
mousey=e.motion.y;
break;
case SDL_WINDOWEVENT:
switch(e.window.event){
case SDL_WINDOWEVENT_CLOSE:
gameRunning = false;
break;
}
case SDL_KEYDOWN:
if(e.key.keysym.sym == SDLK_d) player.right = true;
if(e.key.keysym.sym == SDLK_a) player.left = true;
if(e.key.keysym.sym == SDLK_LSHIFT) player.speed = 3;
if(e.key.keysym.sym == SDLK_SPACE){
if(!space&&player.vel.y<=0){
space=true;
player.loc.y += HLINE*1.2;
player.vel.y += .003;
}
}
if(e.key.keysym.sym == SDLK_i){
if(currentWorld->behind){
thing=(currentWorld->getWidth()-currentWorld->behind->getWidth())/2;
if(player.loc.x>thing-1&&
player.loc.x<thing-1+currentWorld->behind->getWidth()){
player.loc.x-=thing;
memset(&player.vel,0,sizeof(vec2));
currentWorld=currentWorld->behind;
}
}
}
if(e.key.keysym.sym == SDLK_k){
if(currentWorld->infront){
player.loc.x+=(currentWorld->infront->getWidth()-currentWorld->getWidth())/2;
memset(&player.vel,0,sizeof(vec2));
currentWorld=currentWorld->infront;
}
}
if(e.key.keysym.sym == SDLK_F3){
debug = !debug;
}
break;
case SDL_KEYUP:
if(e.key.keysym.sym == SDLK_d) player.right = false;
if(e.key.keysym.sym == SDLK_a) player.left = false;
if(e.key.keysym.sym == SDLK_LSHIFT) player.speed = 1.0;
if(e.key.keysym.sym == SDLK_SPACE)
if(player.vel.y<=.001)space=false;
if(e.key.keysym.sym == SDLK_ESCAPE) gameRunning = false;
break;
}
}
}
}
|