This commit is contained in:
2025-02-06 14:33:23 +01:00
parent 2a2313dff7
commit aaf3dfb40c
5 changed files with 203 additions and 29 deletions

View File

@@ -575,7 +575,7 @@ int secondPass(const char *source, uint8_t *code) {
// Memory reference: encode 32-bit address. // Memory reference: encode 32-bit address.
char addrStr[32]; char addrStr[32];
strncpy(addrStr, srcOperand + 1, strlen(srcOperand) - 2); strncpy(addrStr, srcOperand + 1, strlen(srcOperand) - 2);
addrStr[strlen(srcOperand) - 2] = '\\0'; addrStr[strlen(srcOperand) - 2] = '\0';
uint32_t memAddr = (uint32_t) strtoul(addrStr, NULL, 0); uint32_t memAddr = (uint32_t) strtoul(addrStr, NULL, 0);
code[addr++] = (memAddr >> 24) & 0xFF; code[addr++] = (memAddr >> 24) & 0xFF;
code[addr++] = (memAddr >> 16) & 0xFF; code[addr++] = (memAddr >> 16) & 0xFF;

132
main.c
View File

@@ -56,7 +56,7 @@ int init() {
printf("Renderer could not be created SDL_Error: %s\n", SDL_GetError()); printf("Renderer could not be created SDL_Error: %s\n", SDL_GetError());
return 1; return 1;
} }
smallFont = prepText(renderer, 10, "../PublicPixel.ttf", 255, 255, 255, 255); smallFont = prepText(renderer, 12, "../PublicPixel.ttf", 255, 255, 255, 255);
init_editor(&codeEditor, &smallFont, 50, 50, renderer); init_editor(&codeEditor, &smallFont, 50, 50, renderer);
activeEditor = &codeEditor; activeEditor = &codeEditor;
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT); SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
@@ -87,6 +87,128 @@ int render() {
return 0; return 0;
} }
SDL_Keycode ConvertKPToNonKP(SDL_Keycode keycode) {
switch (keycode) {
case SDLK_KP_0:
return SDLK_0;
case SDLK_KP_1:
return SDLK_1;
case SDLK_KP_2:
return SDLK_2;
case SDLK_KP_3:
return SDLK_3;
case SDLK_KP_4:
return SDLK_4;
case SDLK_KP_5:
return SDLK_5;
case SDLK_KP_6:
return SDLK_6;
case SDLK_KP_7:
return SDLK_7;
case SDLK_KP_8:
return SDLK_8;
case SDLK_KP_9:
return SDLK_9;
case SDLK_KP_PERIOD:
return SDLK_PERIOD;
case SDLK_KP_COMMA:
return SDLK_COMMA;
case SDLK_KP_DIVIDE:
return SDLK_SLASH;
case SDLK_KP_MULTIPLY:
return SDLK_ASTERISK;
case SDLK_KP_MINUS:
return SDLK_MINUS;
case SDLK_KP_PLUS:
return SDLK_PLUS;
case SDLK_KP_ENTER:
return SDLK_RETURN;
case SDLK_KP_EQUALS:
return SDLK_EQUALS;
case SDLK_KP_LEFTPAREN:
return SDLK_LEFTPAREN;
case SDLK_KP_RIGHTPAREN:
return SDLK_RIGHTPAREN;
case SDLK_KP_LEFTBRACE:
return SDLK_LEFTBRACKET;
case SDLK_KP_RIGHTBRACE:
return SDLK_RIGHTBRACKET;
case SDLK_KP_TAB:
return SDLK_TAB;
case SDLK_KP_BACKSPACE:
return SDLK_BACKSPACE;
case SDLK_KP_A:
return SDLK_a;
case SDLK_KP_B:
return SDLK_b;
case SDLK_KP_C:
return SDLK_c;
case SDLK_KP_D:
return SDLK_d;
case SDLK_KP_E:
return SDLK_e;
case SDLK_KP_F:
return SDLK_f;
case SDLK_KP_XOR:
return SDLK_CARET;
case SDLK_KP_PERCENT:
return SDLK_PERCENT;
case SDLK_KP_LESS:
return SDLK_LESS;
case SDLK_KP_GREATER:
return SDLK_GREATER;
case SDLK_KP_AMPERSAND:
return SDLK_AMPERSAND;
case SDLK_KP_DBLAMPERSAND:
return SDLK_AMPERSAND; // No direct match, best alternative
case SDLK_KP_VERTICALBAR:
return SDLK_BACKSLASH; // Vertical bar alternative
case SDLK_KP_DBLVERTICALBAR:
return SDLK_BACKSLASH; // No direct match
case SDLK_KP_COLON:
return SDLK_COLON;
case SDLK_KP_HASH:
return SDLK_HASH;
case SDLK_KP_SPACE:
return SDLK_SPACE;
case SDLK_KP_AT:
return SDLK_AT;
case SDLK_KP_EXCLAM:
return SDLK_EXCLAIM;
case SDLK_KP_MEMSTORE:
return SDLK_UNKNOWN; // No direct match
case SDLK_KP_MEMRECALL:
return SDLK_UNKNOWN;
case SDLK_KP_MEMCLEAR:
return SDLK_UNKNOWN;
case SDLK_KP_MEMADD:
return SDLK_PLUS;
case SDLK_KP_MEMSUBTRACT:
return SDLK_MINUS;
case SDLK_KP_MEMMULTIPLY:
return SDLK_ASTERISK;
case SDLK_KP_MEMDIVIDE:
return SDLK_SLASH;
case SDLK_KP_PLUSMINUS:
return SDLK_UNKNOWN;
case SDLK_KP_CLEAR:
return SDLK_UNKNOWN;
case SDLK_KP_CLEARENTRY:
return SDLK_UNKNOWN;
case SDLK_KP_BINARY:
return SDLK_UNKNOWN;
case SDLK_KP_OCTAL:
return SDLK_UNKNOWN;
case SDLK_KP_DECIMAL:
return SDLK_UNKNOWN;
case SDLK_KP_HEXADECIMAL:
return SDLK_UNKNOWN;
default:
return keycode; // If it's not a KP key, return it unchanged
}
}
int processEvent(SDL_Event e) { int processEvent(SDL_Event e) {
if (e.type == SDL_QUIT) { return 0; } if (e.type == SDL_QUIT) { return 0; }
else if (e.type == SDL_WINDOWEVENT && e.window.event == SDL_WINDOWEVENT_RESIZED) { else if (e.type == SDL_WINDOWEVENT && e.window.event == SDL_WINDOWEVENT_RESIZED) {
@@ -97,7 +219,7 @@ int processEvent(SDL_Event e) {
SDL_Rect viewport = {0, 0, newWidth, newHeight}; SDL_Rect viewport = {0, 0, newWidth, newHeight};
SDL_RenderSetViewport(renderer, &viewport); SDL_RenderSetViewport(renderer, &viewport);
} else if (e.type == SDL_KEYDOWN) { } else if (e.type == SDL_KEYDOWN) {
int keySym = e.key.keysym.sym; int keySym = ConvertKPToNonKP(e.key.keysym.sym);
switch (keySym) { switch (keySym) {
case SDLK_UP: case SDLK_UP:
if (activeEditor) { if (activeEditor) {
@@ -122,18 +244,20 @@ int processEvent(SDL_Event e) {
case SDLK_RETURN: case SDLK_RETURN:
case SDLK_RETURN2: case SDLK_RETURN2:
if (activeEditor && !activeEditor->readOnly) { if (activeEditor && !activeEditor->readOnly) {
insert_line_rel(activeEditor); insert_line_rel(activeEditor, renderer);
} }
break; break;
default: default:
break; break;
} }
if (activeEditor && !activeEditor->readOnly) { if (activeEditor && !activeEditor->readOnly && activeEditor->cursor_pos < MAX_LINE_WIDTH) {
if (keySym >= 32 && keySym <= 126) { if (keySym >= 32 && keySym <= 126) {
if (keySym > 0x60 && keySym < 0x7b) { if (keySym > 0x60 && keySym < 0x7b) {
keySym -= 0x20; keySym -= 0x20;
} }
insert_character(activeEditor, (keySym & 0xff), renderer); insert_character(activeEditor, (keySym & 0xff), renderer);
} else if (keySym == SDLK_BACKSPACE || keySym == SDLK_DELETE) {
remove_character(activeEditor, renderer);
} }
} }
} }

View File

@@ -6,21 +6,14 @@
BitmapFont BitmapFont
prepText(SDL_Renderer *renderer, unsigned char pxSize, const char *file, uint8_t r, uint8_t g, uint8_t b, uint8_t a) { prepText(SDL_Renderer *renderer, unsigned char pxSize, const char *file, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
const unsigned char ptSize = floor(pxSize * 0.75); TTF_Font *gFont = TTF_OpenFont(file, pxSize);
TTF_Font *gFont = TTF_OpenFont(file, ptSize);
BitmapFont out; BitmapFont out;
out.size = pxSize; out.size = pxSize;
out.color = (SDL_Color) {r, g, b, a}; out.color = (SDL_Color) {r, g, b, a};
unsigned char i = 0; unsigned char i = 0;
do { do {
char tmpOut[2] = {i, 0}; char tmpOut[2] = {i, 0};
out.surface[i] = TTF_RenderText_Solid(gFont, tmpOut, out.color); out.surface[i] = TTF_RenderText_Solid(gFont, tmpOut, out.color);
SDL_Rect dstRect;
dstRect.x = 0;
dstRect.y = 0;
dstRect.w = pxSize;
dstRect.h = pxSize;
out.texture[i] = SDL_CreateTextureFromSurface(renderer, out.surface[i]); out.texture[i] = SDL_CreateTextureFromSurface(renderer, out.surface[i]);
i++; i++;
} while (i < 255); } while (i < 255);

View File

@@ -17,8 +17,8 @@ void init_editor(TextEditor *editor, BitmapFont *font, int x, int y, SDL_Rendere
editor->cursor_line_offset = 0; editor->cursor_line_offset = 0;
editor->cursor_pos = 0; editor->cursor_pos = 0;
editor->readOnly = 0; editor->readOnly = 0;
editor->rect.x = 0; editor->rect.x = 2;
editor->rect.y = 0; editor->rect.y = 2;
editor->rect.w = MAX_LINE_WIDTH * (font->size + 1); editor->rect.w = MAX_LINE_WIDTH * (font->size + 1);
editor->rect.h = MAX_LINES_DISPLAY * (font->size + 4); editor->rect.h = MAX_LINES_DISPLAY * (font->size + 4);
editor->outRect = editor->rect; editor->outRect = editor->rect;
@@ -30,7 +30,7 @@ void init_editor(TextEditor *editor, BitmapFont *font, int x, int y, SDL_Rendere
} }
// Insert a new line at a specific position // Insert a new line at a specific position
void insert_line(TextEditor *editor, int position, const char *text) { void insert_line(TextEditor *editor, int position, const char *text, SDL_Renderer *renderer) {
if (editor->line_count >= MAX_LINES_ASM || position < 0 || position > editor->line_count) { if (editor->line_count >= MAX_LINES_ASM || position < 0 || position > editor->line_count) {
printf("Invalid position or max lines reached!\n"); printf("Invalid position or max lines reached!\n");
return; return;
@@ -47,11 +47,12 @@ void insert_line(TextEditor *editor, int position, const char *text) {
editor->lines[position].active = 1; editor->lines[position].active = 1;
editor->line_count++; editor->line_count++;
generate_string_display(editor, renderer);
} }
void insert_line_rel(TextEditor *editor) { void insert_line_rel(TextEditor *editor, SDL_Renderer *renderer) {
editor->cursor_pos = 0; editor->cursor_pos = 0;
insert_line(editor, editor->cursor_line + 1, ""); insert_line(editor, editor->cursor_line + 1, "", renderer);
editor->cursor_line++; editor->cursor_line++;
} }
@@ -80,6 +81,46 @@ void insert_character(TextEditor *editor, char ch, SDL_Renderer *renderer) {
generate_string_display(editor, renderer); generate_string_display(editor, renderer);
} }
void remove_character(TextEditor *editor, SDL_Renderer *renderer) {
if (editor->cursor_line < 0 || editor->cursor_line >= editor->line_count) {
printf("Invalid cursor position!\n");
return;
}
if (editor->cursor_pos == 0 && editor->line_count > 1) {
// Remove the current line and shift lines up
for (int i = editor->cursor_line; i < editor->line_count - 1; i++) {
strcpy(editor->lines[i].text, editor->lines[i + 1].text);
editor->lines[i].active = editor->lines[i + 1].active;
}
editor->lines[editor->line_count - 1].text[0] = '\0';
editor->lines[editor->line_count - 1].active = 0;
editor->line_count--;
if (editor->cursor_line >= editor->line_count) {
editor->cursor_line = editor->line_count - 1;
}
editor->cursor_pos = strlen(editor->lines[editor->cursor_line].text);
} else {
Line *line = &editor->lines[editor->cursor_line];
int len = strlen(line->text);
if (editor->cursor_pos <= 0 || editor->cursor_pos > len) {
printf("Position out of bounds!\n");
return;
}
// Shift characters to the left to remove the character
for (int i = editor->cursor_pos - 1; i < len; i++) {
line->text[i] = line->text[i + 1];
}
editor->cursor_pos--;
}
generate_string_display(editor, renderer);
}
// Move cursor // Move cursor
void move_cursor_relative(TextEditor *editor, int line_offset, int pos_offset) { void move_cursor_relative(TextEditor *editor, int line_offset, int pos_offset) {
int new_line = editor->cursor_line + line_offset; int new_line = editor->cursor_line + line_offset;
@@ -127,23 +168,37 @@ void generate_string_display(TextEditor *editor, SDL_Renderer *renderer) {
charRect.w = editor->font->size; charRect.w = editor->font->size;
charRect.h = editor->font->size; charRect.h = editor->font->size;
SDL_Rect outRect = charRect; SDL_Rect outRect = charRect;
outRect.x = 0; outRect.x = 3;
outRect.y = 0; outRect.y = 2;
for (int i = editor->cursor_line_offset; i < end_line; i++) { SDL_Rect cursorRect;
if (editor->lines[i].active) { cursorRect.x = 0;
strcat(editor->displayString, editor->lines[i].text); cursorRect.y = 0;
char *linePTR = editor->lines[i].text; cursorRect.w = 1;
cursorRect.h = editor->font->size;
for (int line = editor->cursor_line_offset; line < end_line; line++) {
if (editor->lines[line].active) {
strcat(editor->displayString, editor->lines[line].text);
char *linePTR = editor->lines[line].text;
int charIndex = 0;
while (*linePTR) { while (*linePTR) {
SDL_RenderCopy(renderer, editor->font->texture[*linePTR], &charRect, &outRect); SDL_RenderCopy(renderer, editor->font->texture[*linePTR], &charRect, &outRect);
outRect.x += charRect.w + 1; outRect.x += charRect.w + 1;
if (line == editor->cursor_line && charIndex == editor->cursor_pos - 1) {
cursorRect.x = outRect.x;
cursorRect.y = outRect.y;
}
charIndex++;
linePTR++; linePTR++;
} }
strcat(editor->displayString, "\n"); strcat(editor->displayString, "\n");
outRect.x = 0; outRect.x = 3;
outRect.y += charRect.h + 4; outRect.y += charRect.h + 1;
} }
} }
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &cursorRect);
SDL_SetRenderTarget(renderer, NULL); SDL_SetRenderTarget(renderer, NULL);
} }

View File

@@ -41,13 +41,15 @@ typedef struct {
void init_editor(TextEditor *editor, BitmapFont *font, int x, int y, SDL_Renderer *renderer); void init_editor(TextEditor *editor, BitmapFont *font, int x, int y, SDL_Renderer *renderer);
// Insert a new line at a specific position // Insert a new line at a specific position
void insert_line(TextEditor *editor, int position, const char *text); void insert_line(TextEditor *editor, int position, const char *text, SDL_Renderer *renderer);
// Insert a new line at a specific position // Insert a new line at a specific position
void insert_line_rel(TextEditor *editor); void insert_line_rel(TextEditor *editor, SDL_Renderer *renderer);
void editor_render(TextEditor *editor, SDL_Renderer * renderer); void editor_render(TextEditor *editor, SDL_Renderer * renderer);
void remove_character(TextEditor *editor, SDL_Renderer *renderer);
// Insert a character at the current cursor position // Insert a character at the current cursor position
void insert_character(TextEditor *editor, char ch, SDL_Renderer *renderer); void insert_character(TextEditor *editor, char ch, SDL_Renderer *renderer);