-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
488 lines (441 loc) · 13.4 KB
/
main.cpp
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#include<iostream>
#include<glad/glad.h>
#include<GLFW/glfw3.h>
#include<string>
#include<map>
#include<vector>
#include<fstream>
#include<algorithm>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include<ft2build.h>
#include FT_FREETYPE_H
static float SCR_WIDTH = 800;
static float SCR_HEIGHT = 600;
struct Character {
unsigned int TextureID;
glm::ivec2 Size;
glm::ivec2 Bearing;
unsigned int Advance;
};
void RenderText(unsigned int shader, std::string text, float x, float y, float scale, glm::vec3 color);
std::map<GLchar, Character> Characters;
FT_Library ft;
unsigned int VAO, VBO;
unsigned int focus = 0;
void charCallback(GLFWwindow* window, unsigned int codepoint);
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
void framebufferSizeCallback(GLFWwindow* window, int width, int height);
void scrollCallback(GLFWwindow* window, double xoffset, double yoffset);
void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods);
void replaceAll(std::string& str, const std::string& from, const std::string& to);
std::vector<std::string> lines;
unsigned int currentLine = 0;
float scale = 0.75;
bool overwrite = false;
unsigned int program;
int h = 0;
glm::vec2 chSize;
glm::vec2 chBearing;
std::string fileName = "KT Editor.cpp";
int main(int argc, char** argv)
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(800, 600, "KT Editor", NULL, NULL);
glfwSetCharCallback(window, charCallback);
glfwSetKeyCallback(window, keyCallback);
glfwSetFramebufferSizeCallback(window, framebufferSizeCallback);
glfwSetScrollCallback(window, scrollCallback);
glfwSetMouseButtonCallback(window, mouseButtonCallback);
glfwMakeContextCurrent(window);
gladLoadGL();
program = glCreateProgram();
{
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
const char* source =
"#version 330 core\n"
"layout (location = 0) in vec4 vertex;\n"
"out vec2 TexCoords;\n"
"uniform mat4 projection;\n"
"void main()\n"
"{\n"
" gl_Position = projection * vec4(vertex.xy, 0.0, 1.0);\n"
" TexCoords = vertex.zw;\n"
"}\n";
glShaderSource(vertexShader,1, &source, 0);
glCompileShader(vertexShader);
glAttachShader(program, vertexShader);
glDeleteShader(vertexShader);
}
{
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
const char* source =
"#version 330 core\n"
"in vec2 TexCoords;\n"
"out vec4 color;\n"
"uniform sampler2D text;\n"
"uniform vec3 textColor;\n"
"void main()\n"
"{\n"
" vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r);\n"
" color = vec4(textColor, 1.0) * sampled;\n"
"}\n";
glShaderSource(fragmentShader, 1, &source, 0);
glCompileShader(fragmentShader);
glAttachShader(program, fragmentShader);
glDeleteShader(fragmentShader);
}
glLinkProgram(program);
glm::mat4 projection = glm::ortho(0.0f, static_cast<float>(SCR_WIDTH), 0.0f, static_cast<float>(SCR_HEIGHT));
glUseProgram(program);
glUniformMatrix4fv(glGetUniformLocation(program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
if (FT_Init_FreeType(&ft))
{
std::cout << "--Could not init FreeType Library" << std::endl;
return EXIT_FAILURE;
}
const char* font_name = "consolab.ttf";
FT_Face face;
if (FT_New_Face(ft, font_name, 0, &face)) {
std::cout << "ERROR::FREETYPE: Failed to load font" << std::endl;
return -1;
}
else {
FT_Set_Pixel_Sizes(face, 0, 48);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
for (unsigned char c = 0; c < 128; c++)
{
if (FT_Load_Char(face, c, FT_LOAD_RENDER))
{
std::cout << "ERROR::FREETYTPE: Failed to load Glyph" << std::endl;
continue;
}
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RED,
face->glyph->bitmap.width,
face->glyph->bitmap.rows,
0,
GL_RED,
GL_UNSIGNED_BYTE,
face->glyph->bitmap.buffer
);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Character character = {
texture,
glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
static_cast<unsigned int>(face->glyph->advance.x)
};
Characters.insert(std::pair<char, Character>(c, character));
}
glBindTexture(GL_TEXTURE_2D, 0);
}
FT_Done_Face(face);
FT_Done_FreeType(ft);
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
std::map<char, bool> key;
if (argc > 1)
{
std::ifstream file(argv[1]);
std::string line;
while (std::getline(file, line))
{
replaceAll(line, "\t", " ");
lines.push_back(line);
}
file.close();
fileName = argv[1];
}
lines.push_back(std::string());
chSize = Characters['a'].Size;
chBearing = Characters['a'].Bearing;
double cTime = 0;
double pTime = 0;
while(!glfwWindowShouldClose(window))
{
cTime = glfwGetTime();
float deltaTime = cTime - pTime;
pTime = cTime;
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
int last = (SCR_HEIGHT / (chSize.y * scale * 1.5f) + ((h) / (chSize.y * scale * 1.5f))) + 1;
glUseProgram(program);
for (int i = (int)((h) / (chSize.y * scale * 1.5f)); i < (lines.size()>last ? last : lines.size());i++)
{
RenderText(program, std::to_string(i + 1), 1, SCR_HEIGHT - ((i+1) * (chSize.y * scale * 1.5f))+h, scale, glm::vec3(0.2f, 0.3f, 0.8f));
RenderText(program, lines[i], (scale* (Characters['a'].Advance >> 6))*3.5, SCR_HEIGHT - ((i+1) * chSize.y * scale * 1.5f)+h, scale, glm::vec3(sinf(cTime) * 0.5 + 0.5f, cosf(cTime) * 0.5 + 0.5f, 0.5f));
}
if (focus == lines[currentLine].size() or !overwrite)
RenderText(program, "|", (scale * (Characters['a'].Advance >> 6))*3 + (focus * (scale * (Characters['a'].Advance>>6))), SCR_HEIGHT - ((currentLine+1) * (chSize.y * scale * 1.5f))+h, scale, glm::vec3(sinf(cTime) * 0.5 + 0.5f, cosf(cTime) * 0.5 + 0.5f, 0.5f));
else
RenderText(program, "[]", (scale* (Characters['a'].Advance >> 6))*3 + (focus * (scale * (Characters['a'].Advance >> 6))), SCR_HEIGHT - ((currentLine+1) * (chSize.y * scale * 1.5f))+h, scale, glm::vec3(sinf(cTime) * 0.5 + 0.5f, cosf(cTime) * 0.5 + 0.5f, 0.5f));
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return EXIT_SUCCESS;
}
void charCallback(GLFWwindow* window, unsigned int codepoint)
{
if (codepoint > 127) codepoint = 287;
if (overwrite)
{
if (lines[currentLine].size() == focus)
lines[currentLine] += codepoint;
else
lines[currentLine][focus] = codepoint;
}
else
{
if (lines[currentLine].size() == focus)
lines[currentLine] += codepoint;
else
lines[currentLine].insert(lines[currentLine].begin() + focus, codepoint);
}
focus++;
}
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
switch (scancode)
{
case 14:
if (lines[currentLine].size() and action)
{
if (focus)
{
lines[currentLine].erase(lines[currentLine].begin() + focus-1);
focus--;
}
else
{
lines[currentLine - 1] += lines[currentLine];
lines.erase(std::next(lines.begin(), currentLine));
currentLine--;
focus = lines[currentLine].size();
}
}
else if(lines[currentLine].size()==0 and action and lines.size()>1 and currentLine)
{
lines.erase(std::next(lines.begin(), currentLine));
currentLine--;
focus = lines[currentLine].size();
}
break;
case 328:
if (action and currentLine)
{
currentLine--;
if(focus == lines[currentLine+1].size())
focus = lines[currentLine].size();
else if (focus > lines[currentLine].size())
{
focus = lines[currentLine].size();
}
}
break;
case 336:
if (action and lines.size() - 1 != currentLine)
{
currentLine++;
if (focus == lines[currentLine-1].size())
focus = lines[currentLine].size();
else if (focus > lines[currentLine].size())
focus = lines[currentLine].size();
}
break;
case 331:
if (!mods and action)
{
if (focus)
focus--;
else
{
if (currentLine)
{
currentLine--;
focus = lines[currentLine].size();
}
}
}
break;
case 333:
if (!mods and action)
{
if (lines[currentLine].size() > focus)
focus++;
else if(lines.size()-1!=currentLine)
{
focus = 0;
currentLine++;
}
}
break;
case 28:
if(!mods and action)
{
lines.insert(lines.begin() + currentLine +1, lines[currentLine].substr(focus,lines[currentLine].size()));
lines[currentLine].erase(focus, lines[currentLine].size());
currentLine++;
focus = lines[currentLine].size();
}
break;
case 15:
if (!mods and action)
{
focus += 3;
lines[currentLine] += " ";
}
break;
case 327:
if (!mods and action)
{
focus = 0;
}
else if (mods == 2 and action)
{
focus = 0;
currentLine = 0;
}
break;
case 335:
if (!mods and action)
{
focus = lines[currentLine].size();
}
else if (mods == 2 and action)
{
currentLine = lines.size() - 1;
focus = lines[currentLine].size();
}
break;
case 47:
if(mods==2 and action)lines[currentLine] += glfwGetClipboardString(window);
break;
case 31:
if (mods == 2 and action == 1)
{
std::ofstream file(fileName);
for (int i = 0; i < lines.size(); i++)
file << lines[i].c_str() << "\n";
file.close();
}
break;
case 78:
if (mods == 2 and action and scale <= 2)
scale += 0.02;
break;
case 74:
if (mods == 2 and action and scale >= 0.2f)
scale -= 0.02;
break;
case 82:
if (mods == 2 and action == 1)
{
overwrite = !overwrite;
}
break;
}
}
void framebufferSizeCallback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
glm::mat4 projection = glm::ortho(0.0f, static_cast<float>(width), 0.0f, static_cast<float>(height));
glUseProgram(program);
glUniformMatrix4fv(glGetUniformLocation(program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
SCR_WIDTH = width;
SCR_HEIGHT = height;
}
void scrollCallback(GLFWwindow* window, double xoffset, double yoffset)
{
if((h or yoffset==-1))
h += -yoffset*30;
}
void RenderText(unsigned int shader, std::string text, float x, float y, float scale, glm::vec3 color)
{
glUniform3f(glGetUniformLocation(shader, "textColor"), color.x, color.y, color.z);
glActiveTexture(GL_TEXTURE0);
glBindVertexArray(VAO);
std::string::const_iterator c;
for (c = text.begin(); c != text.end(); c++)
{
Character ch = Characters[*c];
float xpos = x + ch.Bearing.x * scale;
float ypos = y - (ch.Size.y - ch.Bearing.y) * scale;
float w = ch.Size.x * scale;
float h = ch.Size.y * scale;
float vertices[6][4] = {
{ xpos, ypos + h, 0.0f, 0.0f },
{ xpos, ypos, 0.0f, 1.0f },
{ xpos + w, ypos, 1.0f, 1.0f },
{ xpos, ypos + h, 0.0f, 0.0f },
{ xpos + w, ypos, 1.0f, 1.0f },
{ xpos + w, ypos + h, 1.0f, 0.0f }
};
glBindTexture(GL_TEXTURE_2D, ch.TextureID);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDrawArrays(GL_TRIANGLES, 0, 6);
x += (ch.Advance >> 6) * scale;
}
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
}
void replaceAll(std::string& str, const std::string& from, const std::string& to)
{
if (from.empty())
return;
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos))!= std::string::npos)
{
str.replace(start_pos, from.length(), to);
start_pos += to.length();
}
}
void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
{
if (!button and action == 1 and !mods)
{
double xpos, ypos;
int xfocus;
glfwGetCursorPos(window, &xpos, &ypos);
if(overwrite)
xfocus = (xpos - (scale * (Characters['a'].Advance >> 6)) * 3.5f) / ((scale * (Characters['a'].Advance >> 6)));
else
xfocus = (xpos - (scale * (Characters['a'].Advance >> 6)) * 3) / ((scale * (Characters['a'].Advance >> 6)));
int yfocus = (int)((ypos + h) / (chSize.y * scale * 1.5f));
if (yfocus >= 0 and yfocus < lines.size())
{
if(xfocus >= 0 )
{
if (xfocus > lines[yfocus].size())
xfocus = lines[yfocus].size();
currentLine = yfocus;
focus = xfocus;
}
}
}
}