Skip to content
This repository was archived by the owner on Sep 23, 2022. It is now read-only.

Commit 7e0fecf

Browse files
insertion working better
1 parent 7b666ef commit 7e0fecf

File tree

6 files changed

+54
-31
lines changed

6 files changed

+54
-31
lines changed

editor/Content.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
11
#include "Content.h"
22

3+
//constructor
34
Content::Content(Document *doc, Cursor *cur) {
45
this->document = doc;
56
this->cursor = cur;
67
}
78

9+
//renderDocument
10+
void Content::renderDocument(TTF_Font *font, SDL_Color color, SDL_Renderer *renderer) {
11+
char *line;
12+
int height = TTF_FontHeight(font);
13+
for (int i = 0; i < document->getLineCount(); i++) {
14+
line = document->getLine(i);
15+
int texW = 0;
16+
int texH = 0;
17+
SDL_Surface *surface = TTF_RenderText_Solid(font, line, color);
18+
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
19+
20+
SDL_QueryTexture(texture, NULL, NULL, &texW, &texH);
21+
SDL_Rect rect = {OFFSET_X, i * height, texW, texH};
22+
23+
SDL_RenderCopy(renderer, texture, NULL, &rect);
24+
}
25+
}
26+
27+
//shift Cursor
828
void Content::shiftLeft() {
929
if (cursor->x > OFFSET_X) {
1030
cursor->x -= cursor->charWidth;

editor/Content.h

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class Content {
1313
public:
1414
Content(Document *doc, Cursor *cur);
1515

16+
//rendering
17+
void renderDocument(TTF_Font *font, SDL_Color color, SDL_Renderer *renderer);
18+
1619
//cursor shifting
1720
void shiftUp();
1821
void shiftDown();

editor/Document.cpp

+11-9
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ void Document::init(string filename) {
1616
lineBuffer = (char **) malloc(lineCount * sizeof(char *));
1717

1818
for (int i = 0; i < lineCount; i++) {
19-
lineBuffer[i] = (char *) malloc(80 * sizeof(char));
20-
memset(lineBuffer[i], ' ', 80 * sizeof(char));
19+
lineBuffer[i] = (char *) malloc(LINE_LIMIT * sizeof(char));
20+
memset(lineBuffer[i], ' ', LINE_LIMIT * sizeof(char));
2121
}
2222

2323
//reset cursor
@@ -69,8 +69,8 @@ void Document::createLine(int lineNum, int numLines) {
6969

7070
//allocate memory for empty lines
7171
for (int i = prevCount; i < lineCount; i++) {
72-
lineBuffer[i] = (char *) malloc(80 * sizeof(char));
73-
memset(lineBuffer[i], ' ', 80 * sizeof(char));
72+
lineBuffer[i] = (char *) malloc(LINE_LIMIT * sizeof(char));
73+
memset(lineBuffer[i], ' ', LINE_LIMIT * sizeof(char));
7474
}
7575

7676
//shift any existing lines down
@@ -81,18 +81,20 @@ void Document::createLine(int lineNum, int numLines) {
8181

8282
void Document::shiftLineDown(int lineNum) {
8383
lineBuffer[lineNum + 1] = lineBuffer[lineNum];
84-
memset(lineBuffer[lineNum], ' ', 80 * sizeof(char));
84+
memset(lineBuffer[lineNum], ' ', LINE_LIMIT * sizeof(char));
8585
}
8686

8787
void Document::insertPos(int lineNum, int charNum, char c) {
8888
if (isValid(c))
89-
lineBuffer[lineNum][charNum] = c;
89+
for (int i = LINE_LIMIT; i >= charNum; i--)
90+
lineBuffer[lineNum][i + 1] = lineBuffer[lineNum][i];
91+
lineBuffer[lineNum][charNum] = c;
9092
}
9193

9294
void Document::deletePos(int lineNum, int charNum) {
93-
for(int i = charNum+1; i < 80; i++)
94-
lineBuffer[lineNum][i-1] = lineBuffer[lineNum][i];
95-
lineBuffer[lineNum][80] = ' ';
95+
for (int i = charNum + 1; i < LINE_LIMIT; i++)
96+
lineBuffer[lineNum][i - 1] = lineBuffer[lineNum][i];
97+
lineBuffer[lineNum][LINE_LIMIT] = ' ';
9698
}
9799

98100

editor/Utility.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
typedef uint32_t u32;
1818

1919
static bool isValid(char c) {
20-
if (c >= '!' && c <= '~')
20+
if ((c >= '!' && c <= '~') || c == ' ')
2121
return true;
2222
return false;
2323
}

main.cpp

+3-20
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,7 @@
55

66
using namespace std;
77

8-
void printLines(Document doc, TTF_Font *font, SDL_Color color, SDL_Renderer *renderer) {
9-
char *line;
10-
int height = TTF_FontHeight(font);
11-
for (int i = 0; i < doc.getLineCount(); i++) {
12-
line = doc.getLine(i);
13-
int texW = 0;
14-
int texH = 0;
15-
SDL_Surface *surface = TTF_RenderText_Solid(font, line, color);
16-
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
17-
18-
SDL_QueryTexture(texture, NULL, NULL, &texW, &texH);
19-
SDL_Rect rect = {OFFSET_X, i * height, texW, texH};
20-
21-
SDL_RenderCopy(renderer, texture, NULL, &rect);
22-
}
23-
}
8+
249

2510
int main(int argc, char *argv[]) {
2611
/*==========< SDL Init >==============================================================================================*/
@@ -66,7 +51,6 @@ int main(int argc, char *argv[]) {
6651
/*======< Create SDL Window>==========================================================================================*/
6752

6853
bool done = false;
69-
int frameNum = 0;
7054
char ascii;
7155
while (!done) {
7256
SDL_Event event;
@@ -93,8 +77,7 @@ int main(int argc, char *argv[]) {
9377
document.saveFile("output.txt");
9478
break;
9579
case SDLK_ENTER:
96-
document.createLine(currLine, 1);
97-
content.shiftPos(++currLine,0);
80+
content.shiftDown();
9881
break;
9982
case SDLK_UP:
10083
content.shiftUp();
@@ -129,7 +112,7 @@ int main(int argc, char *argv[]) {
129112

130113
cursor.fill(screenPixels);
131114
SDL_RenderCopy(renderer, screen, NULL, NULL);
132-
printLines(document, font, color, renderer);
115+
content.renderDocument(font, color, renderer);
133116
SDL_RenderPresent(renderer);
134117

135118
SDL_Delay(10);

output.txt

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1-
hello world
1+
a is this working
2+
b idk man `
3+
c �G�U
4+
d ��7
5+
e
6+
f
7+
28

39

410

11+
12+
13+
14+
15+
hello
16+
17+
18+
19+
520

0 commit comments

Comments
 (0)