-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathmain.c
More file actions
337 lines (288 loc) · 10.9 KB
/
main.c
File metadata and controls
337 lines (288 loc) · 10.9 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
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <SDL2/SDL.h>
#define GLEW_STATIC
#include <GL/glew.h>
#define GL_GLEXT_PROTOTYPES
#include <SDL2/SDL_opengl.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include "./editor.h"
#include "./file_browser.h"
#include "./la.h"
#include "./free_glyph.h"
#include "./simple_renderer.h"
#include "./common.h"
#include "./lexer.h"
// TODO: Save file dialog
// Needed when ded is ran without any file so it does not know where to save.
// TODO: Jump forward/backward by a word
// TODO: Delete a word
void MessageCallback(GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar* message,
const void* userParam)
{
(void) source;
(void) id;
(void) length;
(void) userParam;
fprintf(stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
(type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""),
type, severity, message);
}
static Free_Glyph_Atlas atlas = {0};
static Simple_Renderer sr = {0};
static Editor editor = {0};
static File_Browser fb = {0};
// TODO: display errors reported via flash_error right in the text editor window somehow
#define flash_error(...) fprintf(stderr, __VA_ARGS__)
int main(int argc, char **argv)
{
Errno err;
FT_Library library = {0};
FT_Error error = FT_Init_FreeType(&library);
if (error) {
fprintf(stderr, "ERROR: could initialize FreeType2 library\n");
return 1;
}
// TODO: users should be able to customize the font
// const char *const font_file_path = "./VictorMono-Regular.ttf";
const char *const font_file_path = "./iosevka-regular.ttf";
FT_Face face;
error = FT_New_Face(library, font_file_path, 0, &face);
if (error == FT_Err_Unknown_File_Format) {
fprintf(stderr, "ERROR: `%s` has an unknown format\n", font_file_path);
return 1;
} else if (error) {
fprintf(stderr, "ERROR: could not load file `%s`\n", font_file_path);
return 1;
}
FT_UInt pixel_size = FREE_GLYPH_FONT_SIZE;
// TODO: FT_Set_Pixel_Sizes does not produce good looking results
// We need to use something like FT_Set_Char_Size and properly set the device resolution
error = FT_Set_Pixel_Sizes(face, 0, pixel_size);
if (error) {
fprintf(stderr, "ERROR: could not set pixel size to %u\n", pixel_size);
return 1;
}
if (argc > 1) {
const char *file_path = argv[1];
err = editor_load_from_file(&editor, file_path);
if (err != 0) {
fprintf(stderr, "ERROR: Could not read file %s: %s\n", file_path, strerror(err));
return 1;
}
}
const char *dir_path = ".";
err = fb_open_dir(&fb, dir_path);
if (err != 0) {
fprintf(stderr, "ERROR: Could not read directory %s: %s\n", dir_path, strerror(err));
return 1;
}
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "ERROR: Could not initialize SDL: %s\n", SDL_GetError());
return 1;
}
SDL_Window *window =
SDL_CreateWindow("ded",
0, 0,
SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
if (window == NULL) {
fprintf(stderr, "ERROR: Could not create SDL window: %s\n", SDL_GetError());
return 1;
}
{
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
int major;
int minor;
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
printf("GL version %d.%d\n", major, minor);
}
if (SDL_GL_CreateContext(window) == NULL) {
fprintf(stderr, "Could not create OpenGL context: %s\n", SDL_GetError());
return 1;
}
if (GLEW_OK != glewInit()) {
fprintf(stderr, "Could not initialize GLEW!");
return 1;
}
if (!GLEW_ARB_draw_instanced) {
fprintf(stderr, "ARB_draw_instanced is not supported; game may not work properly!!\n");
return 1;
}
if (!GLEW_ARB_instanced_arrays) {
fprintf(stderr, "ARB_instanced_arrays is not supported; game may not work properly!!\n");
return 1;
}
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
if (GLEW_ARB_debug_output) {
glEnable(GL_DEBUG_OUTPUT);
glDebugMessageCallback(MessageCallback, 0);
} else {
fprintf(stderr, "WARNING! GLEW_ARB_debug_output is not available");
}
simple_renderer_init(&sr);
free_glyph_atlas_init(&atlas, face);
editor.atlas = &atlas;
editor_retokenize(&editor);
bool quit = false;
bool file_browser = false;
while (!quit) {
const Uint32 start = SDL_GetTicks();
SDL_Event event = {0};
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT: {
quit = true;
}
break;
case SDL_KEYDOWN: {
if (file_browser) {
switch (event.key.keysym.sym) {
case SDLK_F3: {
file_browser = false;
}
break;
case SDLK_UP: {
if (fb.cursor > 0) fb.cursor -= 1;
}
break;
case SDLK_DOWN: {
if (fb.cursor + 1 < fb.files.count) fb.cursor += 1;
}
break;
case SDLK_RETURN: {
if (fb.cursor < fb.files.count) {
// TODO: go inside folders
const char *file_path = fb.files.items[fb.cursor];
// TODO: before opening a new file make sure you don't have unsaved changes
// And if you do, annoy the user about it. (just like all the other editors do)
err = editor_load_from_file(&editor, file_path);
if (err != 0) {
flash_error("Could not open file %s: %s", file_path, strerror(err));
} else {
file_browser = false;
}
}
}
break;
}
} else {
switch (event.key.keysym.sym) {
case SDLK_BACKSPACE: {
editor_backspace(&editor);
editor.last_stroke = SDL_GetTicks();
}
break;
case SDLK_F2: {
if (editor.file_path.count > 0) {
err = editor_save(&editor);
if (err != 0) {
flash_error("Could not save file currently edited file: %s", strerror(err));
}
} else {
// TODO: ask the user for the path to save to in this situation
flash_error("No where to save the text");
}
}
break;
case SDLK_F3: {
file_browser = true;
}
break;
case SDLK_RETURN: {
editor_insert_char(&editor, '\n');
editor.last_stroke = SDL_GetTicks();
}
break;
case SDLK_DELETE: {
editor_delete(&editor);
editor.last_stroke = SDL_GetTicks();
}
break;
case SDLK_a: {
if (event.key.keysym.mod & KMOD_CTRL) {
editor.selection = true;
editor.select_begin = 0;
editor.cursor = editor.data.count;
}
}
break;
case SDLK_UP: {
editor_update_selection(&editor, event.key.keysym.mod & KMOD_SHIFT);
editor_move_line_up(&editor);
editor.last_stroke = SDL_GetTicks();
}
break;
case SDLK_DOWN: {
editor_update_selection(&editor, event.key.keysym.mod & KMOD_SHIFT);
editor_move_line_down(&editor);
editor.last_stroke = SDL_GetTicks();
}
break;
case SDLK_LEFT: {
editor_update_selection(&editor, event.key.keysym.mod & KMOD_SHIFT);
editor_move_char_left(&editor);
editor.last_stroke = SDL_GetTicks();
}
break;
case SDLK_RIGHT: {
editor_update_selection(&editor, event.key.keysym.mod & KMOD_SHIFT);
editor_move_char_right(&editor);
editor.last_stroke = SDL_GetTicks();
}
break;
}
}
}
break;
case SDL_TEXTINPUT: {
if (file_browser) {
// TODO: file browser keys
} else {
const char *text = event.text.text;
size_t text_len = strlen(text);
for (size_t i = 0; i < text_len; ++i) {
editor_insert_char(&editor, text[i]);
}
editor.last_stroke = SDL_GetTicks();
}
}
break;
}
}
{
int w, h;
SDL_GetWindowSize(window, &w, &h);
// TODO(#19): update the viewport and the resolution only on actual window change
glViewport(0, 0, w, h);
}
Vec4f bg = hex_to_vec4f(0x181818FF);
glClearColor(bg.x, bg.y, bg.z, bg.w);
glClear(GL_COLOR_BUFFER_BIT);
if (file_browser) {
fb_render(&fb, window, &atlas, &sr);
} else {
editor_render(window, &atlas, &sr, &editor);
}
SDL_GL_SwapWindow(window);
const Uint32 duration = SDL_GetTicks() - start;
const Uint32 delta_time_ms = 1000 / FPS;
if (duration < delta_time_ms) {
SDL_Delay(delta_time_ms - duration);
}
}
return 0;
}