This repository has been archived by the owner on Sep 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
132 lines (104 loc) · 4.28 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
#include "editor/Utility.h"
#include "editor/Document.h"
#include "editor/Content.h"
#include "editor/Cursor.h"
using namespace std;
int main(int argc, char *argv[]) {
/*==========< SDL Init >==============================================================================================*/
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window = SDL_CreateWindow("loop",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
0);
assert(window);
SDL_Renderer *renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_SOFTWARE);
assert(renderer);
SDL_Texture *screen = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_RGB888,
SDL_TEXTUREACCESS_STREAMING,
SCREEN_WIDTH,
SCREEN_HEIGHT);
u32 *screenPixels = (u32 *) calloc(SCREEN_WIDTH * SCREEN_HEIGHT, sizeof(u32));
assert(screenPixels);
TTF_Init();
char fontPath[] = "fonts/JetBrainsMono-Regular.ttf";
int fontSize = 20;
TTF_Font *font = TTF_OpenFont(fontPath, fontSize);
SDL_Color color = {40, 0, 0};
/*============< Main >================================================================================================*/
Cursor cursor(font);
Document document;
document.init("input.txt");
Content content(&document, &cursor);
/*======< Create SDL Window>==========================================================================================*/
bool done = false;
char ascii;
while (!done) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
SDL_UpdateTexture(screen, NULL, screenPixels, SCREEN_WIDTH * sizeof(u32));
SDL_RenderClear(renderer);
memset(screenPixels, 240, SCREEN_WIDTH * SCREEN_HEIGHT * sizeof(u32));
if (event.type == SDL_QUIT) {
done = true;
break;
}
if (event.type == SDL_KEYDOWN) {
SDL_Keycode code = event.key.keysym.sym;
int currLine = cursor.getLineNumber();
int currChar = cursor.getCharNumber();
switch (code) {
case SDLK_ESCAPE:
done = true;
document.saveFile("output.txt");
break;
case SDLK_ENTER:
content.shiftDown();
break;
case SDLK_UP:
content.shiftUp();
break;
case SDLK_DOWN:
content.shiftDown();
break;
case SDLK_LEFT:
content.shiftLeft();
break;
case SDLK_RIGHT:
content.shiftRight();
break;
case SDLK_DELETE:
document.deletePos(currLine, currChar);
break;
case SDLK_BACKSPACE:
document.deletePos(currLine, currChar-1);
content.shiftLeft();
break;
case SDLK_TAB:
for (int i = 0; i < 4; i++)
content.shiftRight();
default:
ascii = (char) code;
content.shiftRight();
if (isValid(ascii))
document.insertPos(currLine, cursor.getCharNumber() - 1, ascii);
break;
}
}
cursor.fill(screenPixels);
SDL_RenderCopy(renderer, screen, NULL, NULL);
content.renderDocument(font, color, renderer);
SDL_RenderPresent(renderer);
SDL_Delay(10);
}
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
document.printToConsole();
return EXIT_SUCCESS;
}